tendermint/rpc/core/accounts.go

74 lines
2.2 KiB
Go
Raw Normal View History

package core
import (
2015-03-31 14:08:21 -07:00
"fmt"
acm "github.com/tendermint/tendermint/account"
2015-04-01 17:30:16 -07:00
. "github.com/tendermint/tendermint/common"
ctypes "github.com/tendermint/tendermint/rpc/core/types"
)
func GenPrivAccount() (*ctypes.ResponseGenPrivAccount, error) {
return &ctypes.ResponseGenPrivAccount{acm.GenPrivAccount()}, nil
}
func GetAccount(address []byte) (*ctypes.ResponseGetAccount, error) {
cache := mempoolReactor.Mempool.GetCache()
account := cache.GetAccount(address)
if account == nil {
account = &acm.Account{
Address: address,
PubKey: nil,
Sequence: 0,
Balance: 0,
Code: nil,
StorageRoot: nil,
}
}
return &ctypes.ResponseGetAccount{account}, nil
}
func GetStorage(address, key []byte) (*ctypes.ResponseGetStorage, error) {
state := consensusState.GetState()
account := state.GetAccount(address)
if account == nil {
return nil, fmt.Errorf("Unknown address: %X", address)
}
storageRoot := account.StorageRoot
storageTree := state.LoadStorage(storageRoot)
2015-04-08 12:30:49 -07:00
_, value := storageTree.Get(RightPadWord256(key).Bytes())
if value == nil {
return &ctypes.ResponseGetStorage{key, nil}, nil
}
return &ctypes.ResponseGetStorage{key, value.([]byte)}, nil
}
func ListAccounts() (*ctypes.ResponseListAccounts, error) {
var blockHeight uint
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.ResponseListAccounts{blockHeight, accounts}, nil
}
2015-03-31 14:08:21 -07:00
2015-04-09 16:39:58 -07:00
func DumpStorage(address []byte) (*ctypes.ResponseDumpStorage, error) {
2015-03-31 14:08:21 -07:00
state := consensusState.GetState()
2015-04-09 16:39:58 -07:00
account := state.GetAccount(address)
2015-03-31 14:08:21 -07:00
if account == nil {
2015-04-09 16:39:58 -07:00
return nil, fmt.Errorf("Unknown address: %X", address)
2015-03-31 14:08:21 -07:00
}
storageRoot := account.StorageRoot
2015-04-08 12:30:49 -07:00
storageTree := state.LoadStorage(storageRoot)
storageItems := []ctypes.StorageItem{}
2015-04-08 12:30:49 -07:00
storageTree.Iterate(func(key interface{}, value interface{}) bool {
storageItems = append(storageItems, ctypes.StorageItem{
2015-03-31 14:08:21 -07:00
key.([]byte), value.([]byte)})
return false
})
return &ctypes.ResponseDumpStorage{storageRoot, storageItems}, nil
2015-03-31 14:08:21 -07:00
}