From 3f394a1af5fbe7b7e2374d7de69d17eb2cd3a872 Mon Sep 17 00:00:00 2001 From: Cha Minkyoo Date: Thu, 28 Feb 2019 11:20:40 +0900 Subject: [PATCH] Fix missing error check in voted list conversion --- common/trie/iterator.go | 20 ++++++++++++++++++++ core/state/account.go | 4 ++-- core/transaction/stake.go | 10 ++++++++-- rpc/mapper.go | 7 ++++++- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/common/trie/iterator.go b/common/trie/iterator.go index aeee9c20..71871c00 100644 --- a/common/trie/iterator.go +++ b/common/trie/iterator.go @@ -122,6 +122,26 @@ func (t *Trie) getSubTrieWithMaxCommonPrefix(prefix []byte) ([]byte, []byte, err return curRootHash, route, nil } +// Keys returns all keys starting with the given prefix. +func (t *Trie) Keys(prefix []byte) ([][]byte, error) { + keys := make([][]byte, 0) + iter, err := t.Iterator(prefix) + if err != nil { + return nil, err + } + for { + exist, err := iter.Next() + if err != nil { + return nil, err + } + if !exist { + break + } + keys = append(keys, iter.Key()) + } + return keys, nil +} + func (it *Iterator) push(node *node, pos int, route []byte) { it.stack = append(it.stack, &IteratorState{node, pos, route}) } diff --git a/core/state/account.go b/core/state/account.go index 3fff1bf8..d403d216 100644 --- a/core/state/account.go +++ b/core/state/account.go @@ -173,8 +173,8 @@ func (acc *Account) ToBytes() ([]byte, error) { } // VotedSlice returns slice converted from Voted trie -func (acc *Account) VotedSlice() [][]byte { - return KeyTrieToSlice(acc.Voted) +func (acc *Account) VotedSlice() ([][]byte, error) { + return acc.Voted.Keys(nil) } // GetData returns value in account's data trie diff --git a/core/transaction/stake.go b/core/transaction/stake.go index 90e6919b..6e609441 100644 --- a/core/transaction/stake.go +++ b/core/transaction/stake.go @@ -71,7 +71,10 @@ func (tx *StakeTx) Execute(b *core.Block) error { return err } - voted := user.VotedSlice() + voted, err := user.VotedSlice() + if err != nil { + return err + } // Add user's stake to candidates' votePower for _, v := range voted { @@ -162,7 +165,10 @@ func (tx *UnstakeTx) Execute(b *core.Block) error { account.Points = account.Staking.DeepCopy() } - voted := account.VotedSlice() + voted, err := account.VotedSlice() + if err != nil { + return err + } err = b.State().PutAccount(account) if err != nil { diff --git a/rpc/mapper.go b/rpc/mapper.go index 62cb6a80..a5b42177 100644 --- a/rpc/mapper.go +++ b/rpc/mapper.go @@ -35,12 +35,17 @@ func coreAccount2rpcAccount(account *coreState.Account, curTs int64, address str if err != nil { return nil, err } + votedSlice, err := account.VotedSlice() + if err != nil { + return nil, err + } + voted := byteutils.BytesSlice2HexSlice(votedSlice) return &rpcpb.Account{ Address: address, Balance: account.Balance.String(), Nonce: account.Nonce, Staking: account.Staking.String(), - Voted: byteutils.BytesSlice2HexSlice(account.VotedSlice()), + Voted: voted, Points: account.Points.String(), Unstaking: account.Unstaking.String(), Alias: pbAlias.AliasName,