2015-03-26 21:30:42 -07:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2015-03-31 14:08:21 -07:00
|
|
|
"fmt"
|
2015-04-01 17:30:16 -07:00
|
|
|
"github.com/tendermint/tendermint/account"
|
|
|
|
. "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-04-07 11:44:25 -07:00
|
|
|
func GenPrivAccount() (*ctypes.ResponseGenPrivAccount, error) {
|
|
|
|
return &ctypes.ResponseGenPrivAccount{account.GenPrivAccount()}, nil
|
2015-03-26 21:30:42 -07:00
|
|
|
}
|
|
|
|
|
2015-04-07 11:44:25 -07:00
|
|
|
func GetAccount(address []byte) (*ctypes.ResponseGetAccount, error) {
|
2015-03-29 18:43:27 -07:00
|
|
|
cache := mempoolReactor.Mempool.GetCache()
|
2015-04-07 11:44:25 -07:00
|
|
|
return &ctypes.ResponseGetAccount{cache.GetAccount(address)}, nil
|
2015-03-26 21:30:42 -07:00
|
|
|
}
|
|
|
|
|
2015-04-07 11:44:25 -07:00
|
|
|
func GetStorage(address, slot []byte) (*ctypes.ResponseGetStorage, error) {
|
2015-03-31 04:53:34 -07:00
|
|
|
state := consensusState.GetState()
|
|
|
|
account := state.GetAccount(address)
|
|
|
|
if account == nil {
|
|
|
|
return nil, fmt.Errorf("Unknown address: %X", address)
|
|
|
|
}
|
|
|
|
storageRoot := account.StorageRoot
|
|
|
|
storage := state.LoadStorage(storageRoot)
|
|
|
|
|
|
|
|
_, value := storage.Get(RightPadWord256(slot).Bytes())
|
|
|
|
if value == nil {
|
2015-04-07 11:44:25 -07:00
|
|
|
return &ctypes.ResponseGetStorage{slot, nil}, nil
|
2015-03-31 04:53:34 -07:00
|
|
|
}
|
2015-04-07 11:44:25 -07:00
|
|
|
return &ctypes.ResponseGetStorage{slot, value.([]byte)}, nil
|
2015-03-31 04:53:34 -07:00
|
|
|
}
|
|
|
|
|
2015-04-07 11:44:25 -07:00
|
|
|
func ListAccounts() (*ctypes.ResponseListAccounts, error) {
|
2015-03-26 21:30:42 -07:00
|
|
|
var blockHeight uint
|
|
|
|
var accounts []*account.Account
|
|
|
|
state := consensusState.GetState()
|
|
|
|
blockHeight = state.LastBlockHeight
|
|
|
|
state.GetAccounts().Iterate(func(key interface{}, value interface{}) bool {
|
|
|
|
accounts = append(accounts, value.(*account.Account))
|
|
|
|
return false
|
|
|
|
})
|
2015-04-07 11:44:25 -07:00
|
|
|
return &ctypes.ResponseListAccounts{blockHeight, accounts}, nil
|
2015-03-26 21:30:42 -07:00
|
|
|
}
|
2015-03-31 14:08:21 -07:00
|
|
|
|
2015-04-07 11:44:25 -07:00
|
|
|
func DumpStorage(addr []byte) (*ctypes.ResponseDumpStorage, error) {
|
2015-03-31 14:08:21 -07:00
|
|
|
state := consensusState.GetState()
|
|
|
|
account := state.GetAccount(addr)
|
|
|
|
if account == nil {
|
|
|
|
return nil, fmt.Errorf("Unknown address: %X", addr)
|
|
|
|
}
|
|
|
|
storageRoot := account.StorageRoot
|
|
|
|
storage := state.LoadStorage(storageRoot)
|
2015-04-07 11:44:25 -07:00
|
|
|
storageItems := []ctypes.StorageItem{}
|
2015-03-31 14:08:21 -07:00
|
|
|
storage.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-04-07 11:44:25 -07:00
|
|
|
return &ctypes.ResponseDumpStorage{storageRoot, storageItems}, nil
|
2015-03-31 14:08:21 -07:00
|
|
|
}
|