From 655bfde076550d9da543642d06325140b9fe2081 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrej=20Buko=C5=A1ek?= Date: Fri, 22 May 2020 11:14:51 +0200 Subject: [PATCH] staking: Add missing events --- .buildkite/code.pipeline.yml | 4 +-- .changelog/2935.trivial.md | 0 go/consensus/tendermint/abci/mux.go | 1 + go/consensus/tendermint/apps/staking/fees.go | 14 +++++------ go/consensus/tendermint/apps/staking/query.go | 25 ++++++++++++++++++- .../tendermint/apps/staking/state/state.go | 18 +++++++++++++ go/oasis-test-runner/scenario/e2e/debond.go | 2 +- go/staking/api/sanity_check.go | 3 +++ .../fixture-data/debond/staking-genesis.json | 6 ++--- 9 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 .changelog/2935.trivial.md diff --git a/.buildkite/code.pipeline.yml b/.buildkite/code.pipeline.yml index bbf3335ee7a..c49e21706b0 100644 --- a/.buildkite/code.pipeline.yml +++ b/.buildkite/code.pipeline.yml @@ -232,7 +232,7 @@ steps: ############### - label: E2E tests parallelism: 7 - timeout_in_minutes: 9 + timeout_in_minutes: 11 command: - .buildkite/scripts/download_e2e_test_artifacts.sh - .buildkite/scripts/test_e2e.sh @@ -254,7 +254,7 @@ steps: ########################### - label: E2E tests - intel-sgx parallelism: 5 - timeout_in_minutes: 19 + timeout_in_minutes: 21 command: - .buildkite/scripts/download_e2e_test_artifacts.sh - .buildkite/scripts/test_e2e.sh diff --git a/.changelog/2935.trivial.md b/.changelog/2935.trivial.md new file mode 100644 index 00000000000..e69de29bb2d diff --git a/go/consensus/tendermint/abci/mux.go b/go/consensus/tendermint/abci/mux.go index 2c74e61fd49..5380b9f313b 100644 --- a/go/consensus/tendermint/abci/mux.go +++ b/go/consensus/tendermint/abci/mux.go @@ -792,6 +792,7 @@ func (mux *abciMux) DeliverTx(req types.RequestDeliverTx) types.ResponseDeliverT Codespace: module, Code: code, Log: err.Error(), + Events: ctx.GetEvents(), GasWanted: int64(ctx.Gas().GasWanted()), GasUsed: int64(ctx.Gas().GasUsed()), } diff --git a/go/consensus/tendermint/apps/staking/fees.go b/go/consensus/tendermint/apps/staking/fees.go index 06724324c08..3480f03f6a1 100644 --- a/go/consensus/tendermint/apps/staking/fees.go +++ b/go/consensus/tendermint/apps/staking/fees.go @@ -190,15 +190,15 @@ func (app *stakingApplication) disburseFeesVQ( if err = stakeState.SetAccount(ctx, *proposerEntity, acct); err != nil { return fmt.Errorf("failed to set next proposer account: %w", err) } - } - // Emit transfer event. - evt := &staking.TransferEvent{ - From: staking.FeeAccumulatorAccountID, - To: *proposerEntity, - Tokens: *nextProposerTotal, + // Emit transfer event. + evt := &staking.TransferEvent{ + From: staking.FeeAccumulatorAccountID, + To: *proposerEntity, + Tokens: *nextProposerTotal, + } + ctx.EmitEvent(abciAPI.NewEventBuilder(app.Name()).Attribute(KeyTransfer, cbor.Marshal(evt))) } - ctx.EmitEvent(abciAPI.NewEventBuilder(app.Name()).Attribute(KeyTransfer, cbor.Marshal(evt))) } // Pay the voters. diff --git a/go/consensus/tendermint/apps/staking/query.go b/go/consensus/tendermint/apps/staking/query.go index 24a4809e95f..9ea9035155e 100644 --- a/go/consensus/tendermint/apps/staking/query.go +++ b/go/consensus/tendermint/apps/staking/query.go @@ -78,7 +78,30 @@ func (sq *stakingQuerier) Accounts(ctx context.Context) ([]signature.PublicKey, } func (sq *stakingQuerier) AccountInfo(ctx context.Context, id signature.PublicKey) (*staking.Account, error) { - return sq.state.Account(ctx, id) + switch { + case id.Equal(staking.CommonPoolAccountID): + cp, err := sq.state.CommonPool(ctx) + if err != nil { + return nil, err + } + return &staking.Account{ + General: staking.GeneralAccount{ + Balance: *cp, + }, + }, nil + case id.Equal(staking.FeeAccumulatorAccountID): + fa, err := sq.state.LastBlockFees(ctx) + if err != nil { + return nil, err + } + return &staking.Account{ + General: staking.GeneralAccount{ + Balance: *fa, + }, + }, nil + default: + return sq.state.Account(ctx, id) + } } func (sq *stakingQuerier) Delegations(ctx context.Context, id signature.PublicKey) (map[signature.PublicKey]*staking.Delegation, error) { diff --git a/go/consensus/tendermint/apps/staking/state/state.go b/go/consensus/tendermint/apps/staking/state/state.go index f1e8260dfbb..2d75598e9c2 100644 --- a/go/consensus/tendermint/apps/staking/state/state.go +++ b/go/consensus/tendermint/apps/staking/state/state.go @@ -185,6 +185,10 @@ func (s *ImmutableState) Accounts(ctx context.Context) ([]signature.PublicKey, e } func (s *ImmutableState) Account(ctx context.Context, id signature.PublicKey) (*staking.Account, error) { + if !id.IsValid() { + return nil, fmt.Errorf("tendermint/staking: invalid account ID") + } + value, err := s.is.Get(ctx, accountKeyFmt.Encode(&id)) if err != nil { return nil, abciAPI.UnavailableStateError(err) @@ -805,6 +809,13 @@ func (s *MutableState) AddRewards( if err = s.SetDelegation(ctx, id, id, delegation); err != nil { return fmt.Errorf("tendermint/staking: failed to set delegation: %w", err) } + + ev := cbor.Marshal(&staking.AddEscrowEvent{ + Owner: staking.CommonPoolAccountID, + Escrow: id, + Tokens: *com, + }) + ctx.EmitEvent(api.NewEventBuilder(AppName).Attribute(KeyAddEscrow, ev)) } if err = s.SetAccount(ctx, id, ent); err != nil { @@ -926,6 +937,13 @@ func (s *MutableState) AddRewardSingleAttenuated( if err = s.SetDelegation(ctx, account, account, delegation); err != nil { return fmt.Errorf("tendermint/staking: failed to set delegation: %w", err) } + + ev := cbor.Marshal(&staking.AddEscrowEvent{ + Owner: staking.CommonPoolAccountID, + Escrow: account, + Tokens: *com, + }) + ctx.EmitEvent(api.NewEventBuilder(AppName).Attribute(KeyAddEscrow, ev)) } if err = s.SetAccount(ctx, account, ent); err != nil { diff --git a/go/oasis-test-runner/scenario/e2e/debond.go b/go/oasis-test-runner/scenario/e2e/debond.go index 4d08e5c1c55..22eec1a8042 100644 --- a/go/oasis-test-runner/scenario/e2e/debond.go +++ b/go/oasis-test-runner/scenario/e2e/debond.go @@ -58,7 +58,7 @@ func (s *debondImpl) Run(*env.Env) error { lockupQuery := staking.OwnerQuery{ Height: consensus.HeightLatest, } - if err := lockupQuery.Owner.UnmarshalHex("5555555555555555555555555555555555555555555555555555555555555555"); err != nil { + if err := lockupQuery.Owner.UnmarshalText([]byte("LQu4ZtFg8OJ0MC4M4QMeUR7Is6Xt4A/CW+PK/7TPiH0=")); err != nil { return fmt.Errorf("import lockup account ID: %w", err) } s.logger.Info("checking balance at beginning") diff --git a/go/staking/api/sanity_check.go b/go/staking/api/sanity_check.go index 9a4421ac204..8a598e2f4b8 100644 --- a/go/staking/api/sanity_check.go +++ b/go/staking/api/sanity_check.go @@ -42,6 +42,9 @@ func (p *ConsensusParameters) SanityCheck() error { // SanityCheckAccount examines an account's balances. // Adds the balances to a running total `total`. func SanityCheckAccount(total *quantity.Quantity, parameters *ConsensusParameters, now epochtime.EpochTime, id signature.PublicKey, acct *Account) error { + if !id.IsValid() { + return fmt.Errorf("staking: sanity check failed: account has invalid ID: %s", id) + } if !acct.General.Balance.IsValid() { return fmt.Errorf("staking: sanity check failed: general balance is invalid for account with ID: %s", id) } diff --git a/tests/fixture-data/debond/staking-genesis.json b/tests/fixture-data/debond/staking-genesis.json index 680b6a9f006..d513ad09adf 100644 --- a/tests/fixture-data/debond/staking-genesis.json +++ b/tests/fixture-data/debond/staking-genesis.json @@ -18,7 +18,7 @@ }, "total_supply": "1000", "ledger": { - "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=": { + "ADoWBAua0sJigqXO82hLaC0dDahZwm8bXQHNj7/xtKQ=": { "escrow": { "debonding": { "balance": "1000", @@ -28,8 +28,8 @@ } }, "debonding_delegations": { - "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=": { - "VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVU=": [ + "ADoWBAua0sJigqXO82hLaC0dDahZwm8bXQHNj7/xtKQ=": { + "LQu4ZtFg8OJ0MC4M4QMeUR7Is6Xt4A/CW+PK/7TPiH0=": [ { "shares": "500", "debond_end": 1