From 119616f8042ca7b42886817490b823fa1a72b971 Mon Sep 17 00:00:00 2001 From: Jernej Kos Date: Tue, 11 Feb 2020 12:10:58 +0100 Subject: [PATCH] go/registry: Refactor registry stake thresholds and claims --- .../tendermint/apps/registry/registry.go | 2 +- .../tendermint/apps/registry/transactions.go | 52 +++---------------- go/registry/api/api.go | 49 +++++++++++++++++ 3 files changed, 56 insertions(+), 47 deletions(-) diff --git a/go/consensus/tendermint/apps/registry/registry.go b/go/consensus/tendermint/apps/registry/registry.go index 9679d28cfb7..7635694eaa7 100644 --- a/go/consensus/tendermint/apps/registry/registry.go +++ b/go/consensus/tendermint/apps/registry/registry.go @@ -189,7 +189,7 @@ func (app *registryApplication) onRegistryEpochChanged(ctx *abci.Context, regist // Remove the stake claim for the given node. if !params.DebugBypassStake { - if err = stakeAcc.RemoveStakeClaim(node.EntityID, stakeClaimForNode(node.ID)); err != nil { + if err = stakeAcc.RemoveStakeClaim(node.EntityID, registry.StakeClaimForNode(node.ID)); err != nil { return fmt.Errorf("registry: onRegistryEpochChanged: couldn't remove stake claim: %w", err) } } diff --git a/go/consensus/tendermint/apps/registry/transactions.go b/go/consensus/tendermint/apps/registry/transactions.go index cfe08634d90..c03bf813845 100644 --- a/go/consensus/tendermint/apps/registry/transactions.go +++ b/go/consensus/tendermint/apps/registry/transactions.go @@ -3,9 +3,7 @@ package registry import ( "fmt" - "github.com/oasislabs/oasis-core/go/common" "github.com/oasislabs/oasis-core/go/common/cbor" - "github.com/oasislabs/oasis-core/go/common/crypto/signature" "github.com/oasislabs/oasis-core/go/common/entity" "github.com/oasislabs/oasis-core/go/common/node" "github.com/oasislabs/oasis-core/go/consensus/tendermint/abci" @@ -16,20 +14,6 @@ import ( staking "github.com/oasislabs/oasis-core/go/staking/api" ) -const ( - claimRegisterEntity = "registry.RegisterEntity" - claimRegisterNode = "registry.RegisterNode.%s" - claimRegisterRuntime = "registry.RegisterRuntime.%s" -) - -func stakeClaimForNode(id signature.PublicKey) staking.StakeClaim { - return staking.StakeClaim(fmt.Sprintf(claimRegisterNode, id)) -} - -func stakeClaimForRuntime(id common.Namespace) staking.StakeClaim { - return staking.StakeClaim(fmt.Sprintf(claimRegisterRuntime, id)) -} - func (app *registryApplication) registerEntity( ctx *abci.Context, state *registryState.MutableState, @@ -67,7 +51,7 @@ func (app *registryApplication) registerEntity( } if !params.DebugBypassStake { - if err = stakingState.AddStakeClaim(ctx, ent.ID, claimRegisterEntity, []staking.ThresholdKind{staking.KindEntity}); err != nil { + if err = stakingState.AddStakeClaim(ctx, ent.ID, registry.StakeClaimRegisterEntity, []staking.ThresholdKind{staking.KindEntity}); err != nil { ctx.Logger().Error("RegisterEntity: Insufficent stake", "err", err, "id", ent.ID, @@ -141,7 +125,7 @@ func (app *registryApplication) deregisterEntity(ctx *abci.Context, state *regis } if !params.DebugBypassStake { - if err = stakingState.RemoveStakeClaim(ctx, id, claimRegisterEntity); err != nil { + if err = stakingState.RemoveStakeClaim(ctx, id, registry.StakeClaimRegisterEntity); err != nil { panic(fmt.Errorf("DeregisterEntity: failed to remove stake claim: %w", err)) } } @@ -320,20 +304,8 @@ func (app *registryApplication) registerNode( // nolint: gocyclo return fmt.Errorf("failed to create stake accumulator cache: %w", err) } - claim := stakeClaimForNode(newNode.ID) - var thresholds []staking.ThresholdKind - if newNode.HasRoles(node.RoleKeyManager) { - thresholds = append(thresholds, staking.KindNodeKeyManager) - } - if newNode.HasRoles(node.RoleComputeWorker) { - thresholds = append(thresholds, staking.KindNodeCompute) - } - if newNode.HasRoles(node.RoleStorageWorker) { - thresholds = append(thresholds, staking.KindNodeStorage) - } - if newNode.HasRoles(node.RoleValidator) { - thresholds = append(thresholds, staking.KindNodeValidator) - } + claim := registry.StakeClaimForNode(newNode.ID) + thresholds := registry.StakeThresholdsForNode(newNode) if err = stakeAcc.AddStakeClaim(newNode.EntityID, claim, thresholds); err != nil { ctx.Logger().Error("RegisterNode: insufficient stake for new node", @@ -609,20 +581,8 @@ func (app *registryApplication) registerRuntime( // nolint: gocyclo // Make sure that the entity has enough stake. if !params.DebugBypassStake { - claim := stakeClaimForRuntime(rt.ID) - var thresholds []staking.ThresholdKind - switch rt.Kind { - case registry.KindCompute: - thresholds = append(thresholds, staking.KindRuntimeCompute) - case registry.KindKeyManager: - thresholds = append(thresholds, staking.KindRuntimeKeyManager) - default: - ctx.Logger().Error("RegisterRuntime: unknown runtime kind", - "runtime_id", rt.ID, - "kind", rt.Kind, - ) - return fmt.Errorf("registry: unknown runtime kind (%d)", rt.Kind) - } + claim := registry.StakeClaimForRuntime(rt.ID) + thresholds := registry.StakeThresholdsForRuntime(rt) if err = stakingState.AddStakeClaim(ctx, rt.EntityID, claim, thresholds); err != nil { ctx.Logger().Error("RegisterRuntime: Insufficent stake", diff --git a/go/registry/api/api.go b/go/registry/api/api.go index f7c2a7f60e5..ddd0b766a7c 100644 --- a/go/registry/api/api.go +++ b/go/registry/api/api.go @@ -21,6 +21,7 @@ import ( "github.com/oasislabs/oasis-core/go/common/sgx/ias" "github.com/oasislabs/oasis-core/go/consensus/api/transaction" epochtime "github.com/oasislabs/oasis-core/go/epochtime/api" + staking "github.com/oasislabs/oasis-core/go/staking/api" ) // ModuleName is a unique module name for the registry module. @@ -1285,3 +1286,51 @@ var DefaultGasCosts = transaction.Costs{ GasOpRuntimeEpochMaintenance: 1000, GasOpUpdateKeyManager: 1000, } + +const ( + // StakeClaimRegisterEntity is the stake claim identifier used for registering an entity. + StakeClaimRegisterEntity = "registry.RegisterEntity" + // StakeClaimRegisterNode is the stake claim template used for registering nodes. + StakeClaimRegisterNode = "registry.RegisterNode.%s" + // StakeClaimRegisterRuntime is the stake claim template used for registering runtimes. + StakeClaimRegisterRuntime = "registry.RegisterRuntime.%s" +) + +// StakeClaimForNode generates a new stake claim identifier for a specific node registration. +func StakeClaimForNode(id signature.PublicKey) staking.StakeClaim { + return staking.StakeClaim(fmt.Sprintf(StakeClaimRegisterNode, id)) +} + +// StakeClaimForRuntime generates a new stake claim for a specific runtime registration. +func StakeClaimForRuntime(id common.Namespace) staking.StakeClaim { + return staking.StakeClaim(fmt.Sprintf(StakeClaimRegisterRuntime, id)) +} + +// StakeThresholdsForNode returns the staking thresholds for the given node. +func StakeThresholdsForNode(n *node.Node) (thresholds []staking.ThresholdKind) { + if n.HasRoles(node.RoleKeyManager) { + thresholds = append(thresholds, staking.KindNodeKeyManager) + } + if n.HasRoles(node.RoleComputeWorker) { + thresholds = append(thresholds, staking.KindNodeCompute) + } + if n.HasRoles(node.RoleStorageWorker) { + thresholds = append(thresholds, staking.KindNodeStorage) + } + if n.HasRoles(node.RoleValidator) { + thresholds = append(thresholds, staking.KindNodeValidator) + } + return +} + +// StakeThresholdsForRuntime returns the staking thresholds for the given runtime. +func StakeThresholdsForRuntime(rt *Runtime) (thresholds []staking.ThresholdKind) { + switch rt.Kind { + case KindCompute: + thresholds = append(thresholds, staking.KindRuntimeCompute) + case KindKeyManager: + thresholds = append(thresholds, staking.KindRuntimeKeyManager) + default: + } + return +}