Skip to content

Commit

Permalink
Merge pull request #4556 from oasisprotocol/ptrus/fix/sanitycheck-sus…
Browse files Browse the repository at this point in the history
…pended-runtimes

consensus/sanity-checks: skip suspended runtimes when computing stake claims
  • Loading branch information
ptrus authored Mar 9, 2022
2 parents 857bc92 + c0f76e9 commit 8c72fa6
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions .changelog/4556.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
consensus/sanity-checks: skip suspended runtimes for computing stake claims
34 changes: 34 additions & 0 deletions go/genesis/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func TestGenesisSanityCheck(t *testing.T) {
ID: validPK,
}
signedTestEntity := signEntityOrDie(signer, testEntity)
testEntityAddress := staking.NewAddress(validPK)

kmRuntimeID := hex2ns("4000000000000000ffffffffffffffffffffffffffffffffffffffffffffffff", false)
testKMRuntime := &registry.Runtime{
Expand Down Expand Up @@ -907,4 +908,37 @@ func TestGenesisSanityCheck(t *testing.T) {
ID: 3,
})
require.Error(d.SanityCheck(), "pending upgrades not UpgradeMinEpochDiff apart")

// Sanity check entity stake claims.
d = testDoc()
// Enable stake claims check.
d.Registry.Parameters.DebugBypassStake = false
// Setup registry state.
d.Registry.Entities = []*entity.SignedEntity{signedTestEntity}
d.Registry.Runtimes = []*registry.Runtime{testKMRuntime, testRuntime}
// Setup ledger.
d.Staking.Ledger[testEntityAddress] = &staking.Account{
Escrow: staking.EscrowAccount{
Active: staking.SharePool{
Balance: *quantity.NewFromUint64(100),
TotalShares: *quantity.NewFromUint64(100),
},
},
}
d.Staking.Delegations[testEntityAddress] = map[staking.Address]*staking.Delegation{
testEntityAddress: {
Shares: *quantity.NewFromUint64(100),
},
}
require.NoError(d.Staking.TotalSupply.Add(quantity.NewFromUint64(100)), "TotalSupply.Add")
require.NoError(d.SanityCheck(), "sanity check for entity should pass")

// Increase runtime stake thresholds.
d.Staking.Parameters.Thresholds[staking.KindRuntimeCompute] = *quantity.NewFromUint64(10_000_000)
require.Error(d.SanityCheck(), "sanity check for entity should fail")

// Suspend the runtimes.
d.Registry.Runtimes = []*registry.Runtime{}
d.Registry.SuspendedRuntimes = []*registry.Runtime{testKMRuntime, testRuntime}
require.NoError(d.SanityCheck(), "sanity check for entity should pass")
}
3 changes: 3 additions & 0 deletions go/registry/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ type RuntimeLookup interface {

// AllRuntimes returns a list of all runtimes (including suspended ones).
AllRuntimes(ctx context.Context) ([]*Runtime, error)

// Runtimes returns active runtimes (not including suspended ones).
Runtimes(ctx context.Context) ([]*Runtime, error)
}

// VerifyRegisterEntityArgs verifies arguments for RegisterEntity.
Expand Down
4 changes: 4 additions & 0 deletions go/registry/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func (rl *mockRuntimeLookup) AllRuntimes(ctx context.Context) ([]*Runtime, error
panic("not implemented")
}

func (rl *mockRuntimeLookup) Runtimes(ctx context.Context) ([]*Runtime, error) {
panic("not implemented")
}

func TestVerifyNodeUpdate(t *testing.T) {
logger := logging.GetLogger("registry/api/tests")

Expand Down
17 changes: 15 additions & 2 deletions go/registry/api/sanity_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ func (g *Genesis) SanityCheck(
}
entities = append(entities, ent)
}
runtimes, err := runtimesLookup.AllRuntimes(context.Background())
allRuntimes, err := runtimesLookup.AllRuntimes(context.Background())
if err != nil {
return fmt.Errorf("registry: sanity check failed: could not obtain all runtimes from runtimesLookup: %w", err)
}
for _, rt := range runtimes {
for _, rt := range allRuntimes {
if publicKeyBlacklist[rt.EntityID] {
return fmt.Errorf("registry: sanity check failed: runtime '%s' owned by blacklisted entity: '%s'", rt.ID, rt.EntityID)
}
Expand All @@ -81,6 +81,11 @@ func (g *Genesis) SanityCheck(
if err != nil {
return fmt.Errorf("registry: sanity check failed: could not obtain node list from nodeLookup: %w", err)
}
// Skip suspended runtimes for computing stake claims.
runtimes, err := runtimesLookup.Runtimes(context.Background())
if err != nil {
return fmt.Errorf("registry: sanity check failed: could not obtain runtimes from runtimesLookup: %w", err)
}
// Check stake.
return SanityCheckStake(entities, stakeLedger, nodes, runtimes, stakeThresholds, true)
}
Expand Down Expand Up @@ -437,6 +442,14 @@ func (r *sanityCheckRuntimeLookup) AllRuntimes(ctx context.Context) ([]*Runtime,
return r.allRuntimes, nil
}

func (r *sanityCheckRuntimeLookup) Runtimes(ctx context.Context) ([]*Runtime, error) {
runtimes := make([]*Runtime, 0, len(r.runtimes))
for _, r := range r.runtimes {
runtimes = append(runtimes, r)
}
return runtimes, nil
}

// Node lookup used in sanity checks.
type sanityCheckNodeLookup struct {
nodes map[signature.PublicKey]*node.Node
Expand Down

0 comments on commit 8c72fa6

Please sign in to comment.