Skip to content
This repository has been archived by the owner on Jul 30, 2019. It is now read-only.

Commit

Permalink
Fix missing error check in voted list conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
cl9200 committed Feb 28, 2019
1 parent bfec919 commit 3f394a1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
20 changes: 20 additions & 0 deletions common/trie/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
Expand Down
4 changes: 2 additions & 2 deletions core/state/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions core/transaction/stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion rpc/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 3f394a1

Please sign in to comment.