2015-03-26 21:30:42 -07:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2015-03-31 14:08:21 -07:00
|
|
|
"fmt"
|
2015-04-12 16:41:50 -07:00
|
|
|
acm "github.com/tendermint/tendermint/account"
|
2015-04-01 17:30:16 -07:00
|
|
|
. "github.com/tendermint/tendermint/common"
|
2015-04-07 11:44:25 -07:00
|
|
|
ctypes "github.com/tendermint/tendermint/rpc/core/types"
|
2015-03-26 21:30:42 -07:00
|
|
|
)
|
|
|
|
|
2015-08-10 20:38:45 -07:00
|
|
|
func GenPrivAccount() (*ctypes.ResultGenPrivAccount, error) {
|
|
|
|
return &ctypes.ResultGenPrivAccount{acm.GenPrivAccount()}, nil
|
2015-03-26 21:30:42 -07:00
|
|
|
}
|
|
|
|
|
2015-07-11 18:01:21 -07:00
|
|
|
// If the account is not known, returns nil, nil.
|
2015-08-10 20:38:45 -07:00
|
|
|
func GetAccount(address []byte) (*ctypes.ResultGetAccount, error) {
|
2015-03-29 18:43:27 -07:00
|
|
|
cache := mempoolReactor.Mempool.GetCache()
|
2015-04-12 16:41:50 -07:00
|
|
|
account := cache.GetAccount(address)
|
|
|
|
if account == nil {
|
2015-07-11 18:01:21 -07:00
|
|
|
return nil, nil
|
2015-04-12 16:41:50 -07:00
|
|
|
}
|
2015-08-10 20:38:45 -07:00
|
|
|
return &ctypes.ResultGetAccount{account}, nil
|
2015-03-26 21:30:42 -07:00
|
|
|
}
|
|
|
|
|
2015-08-10 20:38:45 -07:00
|
|
|
func GetStorage(address, key []byte) (*ctypes.ResultGetStorage, error) {
|
2015-03-31 04:53:34 -07:00
|
|
|
state := consensusState.GetState()
|
|
|
|
account := state.GetAccount(address)
|
|
|
|
if account == nil {
|
2015-07-11 18:01:21 -07:00
|
|
|
return nil, fmt.Errorf("UnknownAddress: %X", address)
|
2015-03-31 04:53:34 -07:00
|
|
|
}
|
|
|
|
storageRoot := account.StorageRoot
|
2015-04-02 03:06:46 -07:00
|
|
|
storageTree := state.LoadStorage(storageRoot)
|
2015-03-31 04:53:34 -07:00
|
|
|
|
2015-04-17 17:39:50 -07:00
|
|
|
_, value := storageTree.Get(LeftPadWord256(key).Bytes())
|
2015-03-31 04:53:34 -07:00
|
|
|
if value == nil {
|
2015-08-10 20:38:45 -07:00
|
|
|
return &ctypes.ResultGetStorage{key, nil}, nil
|
2015-03-31 04:53:34 -07:00
|
|
|
}
|
2015-08-10 20:38:45 -07:00
|
|
|
return &ctypes.ResultGetStorage{key, value.([]byte)}, nil
|
2015-03-31 04:53:34 -07:00
|
|
|
}
|
|
|
|
|
2015-08-10 20:38:45 -07:00
|
|
|
func ListAccounts() (*ctypes.ResultListAccounts, error) {
|
2015-06-25 20:28:34 -07:00
|
|
|
var blockHeight int
|
2015-04-12 16:41:50 -07:00
|
|
|
var accounts []*acm.Account
|
2015-03-26 21:30:42 -07:00
|
|
|
state := consensusState.GetState()
|
|
|
|
blockHeight = state.LastBlockHeight
|
|
|
|
state.GetAccounts().Iterate(func(key interface{}, value interface{}) bool {
|
2015-04-12 16:41:50 -07:00
|
|
|
accounts = append(accounts, value.(*acm.Account))
|
2015-03-26 21:30:42 -07:00
|
|
|
return false
|
|
|
|
})
|
2015-08-10 20:38:45 -07:00
|
|
|
return &ctypes.ResultListAccounts{blockHeight, accounts}, nil
|
2015-03-26 21:30:42 -07:00
|
|
|
}
|
2015-03-31 14:08:21 -07:00
|
|
|
|
2015-08-10 20:38:45 -07:00
|
|
|
func DumpStorage(address []byte) (*ctypes.ResultDumpStorage, 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-07-11 18:01:21 -07:00
|
|
|
return nil, fmt.Errorf("UnknownAddress: %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)
|
2015-04-07 11:44:25 -07:00
|
|
|
storageItems := []ctypes.StorageItem{}
|
2015-04-08 12:30:49 -07:00
|
|
|
storageTree.Iterate(func(key interface{}, value interface{}) bool {
|
2015-04-07 11:44:25 -07:00
|
|
|
storageItems = append(storageItems, ctypes.StorageItem{
|
2015-03-31 14:08:21 -07:00
|
|
|
key.([]byte), value.([]byte)})
|
|
|
|
return false
|
|
|
|
})
|
2015-08-10 20:38:45 -07:00
|
|
|
return &ctypes.ResultDumpStorage{storageRoot, storageItems}, nil
|
2015-03-31 14:08:21 -07:00
|
|
|
}
|