forked from tendermint/tendermint
/
accounts.go
67 lines (60 loc) · 2.05 KB
/
accounts.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
package core
import (
"fmt"
acm "github.com/tendermint/tendermint/account"
. "github.com/tendermint/tendermint/common"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
)
func GenPrivAccount() (*ctypes.ResultGenPrivAccount, error) {
return &ctypes.ResultGenPrivAccount{acm.GenPrivAccount()}, nil
}
// If the account is not known, returns nil, nil.
func GetAccount(address []byte) (*ctypes.ResultGetAccount, error) {
cache := mempoolReactor.Mempool.GetCache()
account := cache.GetAccount(address)
if account == nil {
return nil, nil
}
return &ctypes.ResultGetAccount{account}, nil
}
func GetStorage(address, key []byte) (*ctypes.ResultGetStorage, error) {
state := consensusState.GetState()
account := state.GetAccount(address)
if account == nil {
return nil, fmt.Errorf("UnknownAddress: %X", address)
}
storageRoot := account.StorageRoot
storageTree := state.LoadStorage(storageRoot)
_, value := storageTree.Get(LeftPadWord256(key).Bytes())
if value == nil {
return &ctypes.ResultGetStorage{key, nil}, nil
}
return &ctypes.ResultGetStorage{key, value.([]byte)}, nil
}
func ListAccounts() (*ctypes.ResultListAccounts, error) {
var blockHeight int
var accounts []*acm.Account
state := consensusState.GetState()
blockHeight = state.LastBlockHeight
state.GetAccounts().Iterate(func(key interface{}, value interface{}) bool {
accounts = append(accounts, value.(*acm.Account))
return false
})
return &ctypes.ResultListAccounts{blockHeight, accounts}, nil
}
func DumpStorage(address []byte) (*ctypes.ResultDumpStorage, error) {
state := consensusState.GetState()
account := state.GetAccount(address)
if account == nil {
return nil, fmt.Errorf("UnknownAddress: %X", address)
}
storageRoot := account.StorageRoot
storageTree := state.LoadStorage(storageRoot)
storageItems := []ctypes.StorageItem{}
storageTree.Iterate(func(key interface{}, value interface{}) bool {
storageItems = append(storageItems, ctypes.StorageItem{
key.([]byte), value.([]byte)})
return false
})
return &ctypes.ResultDumpStorage{storageRoot, storageItems}, nil
}