Skip to content

Commit

Permalink
go: Transition staking code to use go/staking/api.Address
Browse files Browse the repository at this point in the history
  • Loading branch information
tjanez committed Jun 3, 2020
1 parent ccabdc7 commit dc0e670
Show file tree
Hide file tree
Showing 48 changed files with 878 additions and 657 deletions.
7 changes: 5 additions & 2 deletions go/consensus/tendermint/apps/keymanager/keymanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/oasisprotocol/oasis-core/go/keymanager/api"
keymanager "github.com/oasisprotocol/oasis-core/go/keymanager/api"
registry "github.com/oasisprotocol/oasis-core/go/registry/api"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
)

var emptyHashSha3 = sha3.Sum256(nil)
Expand Down Expand Up @@ -123,10 +124,12 @@ func (app *keymanagerApplication) onEpochChange(ctx *tmapi.Context, epoch epocht
// Suspend the runtime in case the registering entity no longer has enough stake to cover
// the entity and runtime deposits.
if !params.DebugBypassStake {
if err = stakeAcc.CheckStakeClaims(rt.EntityID); err != nil {
acctAddr := staking.NewFromPublicKey(rt.EntityID)
if err = stakeAcc.CheckStakeClaims(acctAddr); err != nil {
ctx.Logger().Warn("insufficient stake for key manager runtime operation",
"err", err,
"entity_id", rt.EntityID,
"entity", rt.EntityID,
"account", acctAddr,
)

// Suspend runtime.
Expand Down
4 changes: 3 additions & 1 deletion go/consensus/tendermint/apps/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
stakingState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/staking/state"
epochtime "github.com/oasisprotocol/oasis-core/go/epochtime/api"
registry "github.com/oasisprotocol/oasis-core/go/registry/api"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
)

var _ abci.Application = (*registryApplication)(nil)
Expand Down Expand Up @@ -190,7 +191,8 @@ func (app *registryApplication) onRegistryEpochChanged(ctx *api.Context, registr

// Remove the stake claim for the given node.
if !params.DebugBypassStake {
if err = stakeAcc.RemoveStakeClaim(node.EntityID, registry.StakeClaimForNode(node.ID)); err != nil {
acctAddr := staking.NewFromPublicKey(node.EntityID)
if err = stakeAcc.RemoveStakeClaim(acctAddr, registry.StakeClaimForNode(node.ID)); err != nil {
return fmt.Errorf("registry: onRegistryEpochChanged: couldn't remove stake claim: %w", err)
}
}
Expand Down
26 changes: 17 additions & 9 deletions go/consensus/tendermint/apps/registry/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,12 @@ func (app *registryApplication) registerEntity(
}

if !params.DebugBypassStake {
if err = stakingState.AddStakeClaim(ctx, ent.ID, registry.StakeClaimRegisterEntity, []staking.ThresholdKind{staking.KindEntity}); err != nil {
ctx.Logger().Error("RegisterEntity: Insufficent stake",
acctAddr := staking.NewFromPublicKey(ent.ID)
if err = stakingState.AddStakeClaim(ctx, acctAddr, registry.StakeClaimRegisterEntity, []staking.ThresholdKind{staking.KindEntity}); err != nil {
ctx.Logger().Error("RegisterEntity: Insufficient stake",
"err", err,
"id", ent.ID,
"entity", ent.ID,
"account", acctAddr,
)
return err
}
Expand Down Expand Up @@ -130,7 +132,8 @@ func (app *registryApplication) deregisterEntity(ctx *api.Context, state *regist
}

if !params.DebugBypassStake {
if err = stakingState.RemoveStakeClaim(ctx, id, registry.StakeClaimRegisterEntity); err != nil {
acctAddr := staking.NewFromPublicKey(id)
if err = stakingState.RemoveStakeClaim(ctx, acctAddr, registry.StakeClaimRegisterEntity); err != nil {
panic(fmt.Errorf("DeregisterEntity: failed to remove stake claim: %w", err))
}
}
Expand Down Expand Up @@ -307,11 +310,13 @@ func (app *registryApplication) registerNode( // nolint: gocyclo

claim := registry.StakeClaimForNode(newNode.ID)
thresholds := registry.StakeThresholdsForNode(newNode)
acctAddr := staking.NewFromPublicKey(newNode.EntityID)

if err = stakeAcc.AddStakeClaim(newNode.EntityID, claim, thresholds); err != nil {
if err = stakeAcc.AddStakeClaim(acctAddr, claim, thresholds); err != nil {
ctx.Logger().Error("RegisterNode: insufficient stake for new node",
"err", err,
"entity", newNode.EntityID,
"account", acctAddr,
)
return err
}
Expand Down Expand Up @@ -381,7 +386,8 @@ func (app *registryApplication) registerNode( // nolint: gocyclo
// Only resume a runtime if the entity has enough stake to avoid having the runtime be
// suspended again on the next epoch transition.
if !params.DebugBypassStake {
if err = stakeAcc.CheckStakeClaims(rt.EntityID); err != nil {
acctAddr := staking.NewFromPublicKey(rt.EntityID)
if err = stakeAcc.CheckStakeClaims(acctAddr); err != nil {
continue
}
}
Expand Down Expand Up @@ -579,11 +585,13 @@ func (app *registryApplication) registerRuntime( // nolint: gocyclo
if !params.DebugBypassStake {
claim := registry.StakeClaimForRuntime(rt.ID)
thresholds := registry.StakeThresholdsForRuntime(rt)
acctAddr := staking.NewFromPublicKey(rt.EntityID)

if err = stakingState.AddStakeClaim(ctx, rt.EntityID, claim, thresholds); err != nil {
ctx.Logger().Error("RegisterRuntime: Insufficent stake",
if err = stakingState.AddStakeClaim(ctx, acctAddr, claim, thresholds); err != nil {
ctx.Logger().Error("RegisterRuntime: Insufficient stake",
"err", err,
"entity_id", rt.EntityID,
"entity", rt.EntityID,
"account", acctAddr,
)
return err
}
Expand Down
7 changes: 5 additions & 2 deletions go/consensus/tendermint/apps/roothash/roothash.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/oasisprotocol/oasis-core/go/roothash/api/block"
"github.com/oasisprotocol/oasis-core/go/roothash/api/commitment"
scheduler "github.com/oasisprotocol/oasis-core/go/scheduler/api"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
)

// timerKindRound is the round timer kind.
Expand Down Expand Up @@ -135,10 +136,12 @@ func (app *rootHashApplication) onCommitteeChanged(ctx *tmapi.Context, epoch epo
// suspended anyway due to nobody being there to pay maintenance fees).
sufficientStake := true
if !empty && !params.DebugBypassStake {
if err = stakeAcc.CheckStakeClaims(rt.EntityID); err != nil {
acctAddr := staking.NewFromPublicKey(rt.EntityID)
if err = stakeAcc.CheckStakeClaims(acctAddr); err != nil {
ctx.Logger().Warn("insufficient stake for runtime operation",
"err", err,
"entity_id", rt.EntityID,
"entity", rt.EntityID,
"account", acctAddr,
)
sufficientStake = false
}
Expand Down
21 changes: 13 additions & 8 deletions go/consensus/tendermint/apps/scheduler/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,30 +134,35 @@ func (app *schedulerApplication) InitChain(ctx *abciAPI.Context, req types.Reque
expectedPower = 1
} else {
var account *staking.Account
account, err = stakeState.Account(ctx, n.EntityID)
acctAddr := staking.NewFromPublicKey(n.EntityID)
account, err = stakeState.Account(ctx, acctAddr)
if err != nil {
ctx.Logger().Error("couldn't get account for genesis validator entity",
"err", err,
"node_id", n.ID,
"entity_id", n.EntityID,
"node", n.ID,
"entity", n.EntityID,
"accont", acctAddr,
)
return fmt.Errorf("scheduler: getting account %s for genesis validator %s entity: %w",
n.EntityID,
return fmt.Errorf("scheduler: getting account %s for genesis validator %s of entity %s: %w",
acctAddr,
n.ID,
n.EntityID,
err,
)
}
expectedPower, err = scheduler.VotingPowerFromTokens(&account.Escrow.Active.Balance)
if err != nil {
ctx.Logger().Error("computing voting power from tokens failed",
"err", err,
"node_id", n.ID,
"entity_id", n.EntityID,
"node", n.ID,
"entity", n.EntityID,
"account", acctAddr,
"tokens", &account.Escrow.Active.Balance,
)
return fmt.Errorf("scheduler: getting computing voting power from tokens (node %s entity %s tokens %v): %w",
return fmt.Errorf("scheduler: getting computing voting power from tokens (node %s entity %s account %s tokens %v): %w",
n.ID,
n.EntityID,
acctAddr,
&account.Escrow.Active.Balance,
err,
)
Expand Down
34 changes: 25 additions & 9 deletions go/consensus/tendermint/apps/scheduler/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
epochtime "github.com/oasisprotocol/oasis-core/go/epochtime/api"
registry "github.com/oasisprotocol/oasis-core/go/registry/api"
scheduler "github.com/oasisprotocol/oasis-core/go/scheduler/api"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
)

var (
Expand Down Expand Up @@ -194,9 +195,13 @@ func (app *schedulerApplication) BeginBlock(ctx *api.Context, request types.Requ
)

if entitiesEligibleForReward != nil {
accounts := publicKeyMapToSortedSlice(entitiesEligibleForReward)
accountAddrs := []staking.Address{}
for _, entity := range publicKeyMapToSortedSlice(entitiesEligibleForReward) {
accountAddrs = append(accountAddrs, staking.NewFromPublicKey(entity))
}

stakingSt := stakingState.NewMutableState(ctx.State())
if err = stakingSt.AddRewards(ctx, epoch, &params.RewardFactorEpochElectionAny, accounts); err != nil {
if err = stakingSt.AddRewards(ctx, epoch, &params.RewardFactorEpochElectionAny, accountAddrs); err != nil {
return fmt.Errorf("tendermint/scheduler: failed to add rewards: %w", err)
}
}
Expand Down Expand Up @@ -428,8 +433,9 @@ func (app *schedulerApplication) electCommittee(

for _, n := range nodes {
// Check if an entity has enough stake.
acctAddr := staking.NewFromPublicKey(n.EntityID)
if stakeAcc != nil {
if err = stakeAcc.CheckStakeClaims(n.EntityID); err != nil {
if err = stakeAcc.CheckStakeClaims(acctAddr); err != nil {
continue
}
}
Expand Down Expand Up @@ -554,7 +560,8 @@ func (app *schedulerApplication) electValidators(
continue
}
if stakeAcc != nil {
if err := stakeAcc.CheckStakeClaims(n.EntityID); err != nil {
acctAddr := staking.NewFromPublicKey(n.EntityID)
if err := stakeAcc.CheckStakeClaims(acctAddr); err != nil {
continue
}
}
Expand Down Expand Up @@ -621,13 +628,20 @@ electLoop:
power = 1
} else {
var stake *quantity.Quantity
stake, err = stakeAcc.GetEscrowBalance(v)
acctAddr := staking.NewFromPublicKey(v)
stake, err = stakeAcc.GetEscrowBalance(acctAddr)
if err != nil {
return fmt.Errorf("failed to fetch escrow balance for entity %s: %w", v, err)
return fmt.Errorf(
"failed to fetch escrow balance for entity %s with account %s: %w",
v, acctAddr, err,
)
}
power, err = scheduler.VotingPowerFromTokens(stake)
if err != nil {
return fmt.Errorf("computing voting power for entity %s with balance %v: %w", v, stake, err)
return fmt.Errorf(
"computing voting power for entity %s with balance %v: %w",
v, stake, err,
)
}
}

Expand Down Expand Up @@ -683,12 +697,14 @@ func publicKeyMapToSliceByStake(
// Stable-sort the shuffled slice by descending escrow balance.
var balanceErr error
sort.SliceStable(entities, func(i, j int) bool {
iBal, err := stakeAcc.GetEscrowBalance(entities[i])
iAcctAddr := staking.NewFromPublicKey(entities[i])
iBal, err := stakeAcc.GetEscrowBalance(iAcctAddr)
if err != nil {
balanceErr = err
return false
}
jBal, err := stakeAcc.GetEscrowBalance(entities[j])
jAcctAddr := staking.NewFromPublicKey(entities[j])
jBal, err := stakeAcc.GetEscrowBalance(jAcctAddr)
if err != nil {
balanceErr = err
return false
Expand Down
4 changes: 3 additions & 1 deletion go/consensus/tendermint/apps/staking/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/oasisprotocol/oasis-core/go/consensus/tendermint/abci"
abciAPI "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/api"
stakingState "github.com/oasisprotocol/oasis-core/go/consensus/tendermint/apps/staking/state"
staking "github.com/oasisprotocol/oasis-core/go/staking/api"
)

var _ abci.TransactionAuthHandler = (*stakingApplication)(nil)
Expand All @@ -19,7 +20,8 @@ func (app *stakingApplication) GetSignerNonce(ctx context.Context, req *api.GetS
return 0, err
}

acct, err := q.AccountInfo(ctx, req.ID)
addr := staking.NewFromPublicKey(req.ID)
acct, err := q.AccountInfo(ctx, addr)
if err != nil {
return 0, err
}
Expand Down
Loading

0 comments on commit dc0e670

Please sign in to comment.