/
account.go
73 lines (64 loc) · 1.74 KB
/
account.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
package tendermint
import (
esStore "github.com/celer-network/eth-services/store"
"github.com/celer-network/eth-services/store/models"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"github.com/vmihailenco/msgpack/v5"
)
const (
errStrDecodeAccount = "could not decode Account"
)
var (
prefixAccount = []byte("act")
)
func (store *TMStore) PutAccount(account *models.Account) error {
return set(store.nsAccount, account.Address.Bytes(), account)
}
func (store *TMStore) GetAccount(fromAddress common.Address) (*models.Account, error) {
var account models.Account
err := get(store.nsAccount, fromAddress.Bytes(), &account)
if err != nil {
return nil, err
}
return &account, nil
}
func (store *TMStore) GetAccounts() ([]*models.Account, error) {
iter, err := store.nsAccount.Iterator(nil, nil)
if err != nil {
return nil, errors.Wrap(err, errStrCreateIter)
}
defer iter.Close()
var accounts []*models.Account
for ; iter.Valid(); iter.Next() {
value := iter.Value()
var account models.Account
err = msgpack.Unmarshal(value, &account)
if err != nil {
return nil, toDecodeAccountError(err)
}
accounts = append(accounts, &account)
}
if len(accounts) == 0 {
return nil, esStore.ErrNotFound
}
return accounts, nil
}
func (store *TMStore) GetNextNonce(address common.Address) (int64, error) {
account, err := store.GetAccount(address)
if err != nil {
return 0, err
}
return account.NextNonce, nil
}
func (store *TMStore) SetNextNonce(address common.Address, nextNonce int64) error {
account, err := store.GetAccount(address)
if err != nil {
return err
}
account.NextNonce = nextNonce
return store.PutAccount(account)
}
func toDecodeAccountError(err error) error {
return errors.Wrap(err, errStrDecodeAccount)
}