/
util.go
112 lines (99 loc) · 3.71 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) 2018 IoTeX
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package accountutil
import (
"math/big"
"github.com/pkg/errors"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/state"
)
type noncer interface {
Nonce() uint64
}
// SetNonce sets nonce for account
func SetNonce(i noncer, state *state.Account) {
if i.Nonce() > state.Nonce {
state.Nonce = i.Nonce()
}
}
// LoadOrCreateAccount either loads an account state or creates an account state
func LoadOrCreateAccount(sm protocol.StateManager, encodedAddr string) (*state.Account, error) {
var account state.Account
addr, err := address.FromString(encodedAddr)
if err != nil {
account = state.EmptyAccount()
return &account, errors.Wrap(err, "failed to get address public key hash from encoded address")
}
addrHash := hash.BytesToHash160(addr.Bytes())
_, err = sm.State(&account, protocol.LegacyKeyOption(addrHash))
if err == nil {
return &account, nil
}
if errors.Cause(err) == state.ErrStateNotExist {
account.Balance = big.NewInt(0)
account.VotingWeight = big.NewInt(0)
if _, err := sm.PutState(account, protocol.LegacyKeyOption(addrHash)); err != nil {
return nil, errors.Wrapf(err, "failed to put state for account %x", addrHash)
}
return &account, nil
}
return nil, err
}
// LoadAccount loads an account state
func LoadAccount(sr protocol.StateReader, addrHash hash.Hash160) (*state.Account, error) {
var account state.Account
if _, err := sr.State(&account, protocol.LegacyKeyOption(addrHash)); err != nil {
if errors.Cause(err) == state.ErrStateNotExist {
account = state.EmptyAccount()
return &account, nil
}
return nil, err
}
return &account, nil
}
// StoreAccount puts updated account state to trie
func StoreAccount(sm protocol.StateManager, addr address.Address, account *state.Account) error {
addrHash := hash.BytesToHash160(addr.Bytes())
_, err := sm.PutState(account, protocol.LegacyKeyOption(addrHash))
return err
}
// Recorded tests if an account has been actually stored
func Recorded(sr protocol.StateReader, addr address.Address) (bool, error) {
var account state.Account
_, err := sr.State(&account, protocol.LegacyKeyOption(hash.BytesToHash160(addr.Bytes())))
if err == nil {
return true, nil
}
if errors.Cause(err) == state.ErrStateNotExist {
return false, nil
}
return false, err
}
// AccountState returns the confirmed account state on the chain
func AccountState(sr protocol.StateReader, encodedAddr string) (*state.Account, error) {
a, _, err := AccountStateWithHeight(sr, encodedAddr)
return a, err
}
// AccountStateWithHeight returns the confirmed account state on the chain with what height the state is read from.
func AccountStateWithHeight(sr protocol.StateReader, encodedAddr string) (*state.Account, uint64, error) {
addr, err := address.FromString(encodedAddr)
if err != nil {
return nil, 0, errors.Wrap(err, "error when getting the pubkey hash")
}
pkHash := hash.BytesToHash160(addr.Bytes())
var account state.Account
h, err := sr.State(&account, protocol.LegacyKeyOption(pkHash))
if err != nil {
if errors.Cause(err) == state.ErrStateNotExist {
account = state.EmptyAccount()
return &account, h, nil
}
return nil, h, errors.Wrapf(err, "error when loading state of %x", pkHash)
}
return &account, h, nil
}