mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-16 18:00:46 +00:00
Merge pull request #479 from gzliudan/get-account-info
add method `eth_getAccountInfo`
This commit is contained in:
commit
476bda6515
4 changed files with 70 additions and 0 deletions
|
|
@ -393,6 +393,10 @@ func (self *stateObject) Nonce() uint64 {
|
||||||
return self.data.Nonce
|
return self.data.Nonce
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *stateObject) Root() common.Hash {
|
||||||
|
return self.data.Root
|
||||||
|
}
|
||||||
|
|
||||||
// Never called, but must be present to allow stateObject to be used
|
// Never called, but must be present to allow stateObject to be used
|
||||||
// as a vm.Account interface that also satisfies the vm.ContractRef
|
// as a vm.Account interface that also satisfies the vm.ContractRef
|
||||||
// interface. Interfaces are awesome.
|
// interface. Interfaces are awesome.
|
||||||
|
|
|
||||||
|
|
@ -83,6 +83,14 @@ type StateDB struct {
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type AccountInfo struct {
|
||||||
|
CodeSize int
|
||||||
|
Nonce uint64
|
||||||
|
Balance *big.Int
|
||||||
|
CodeHash common.Hash
|
||||||
|
StorageHash common.Hash
|
||||||
|
}
|
||||||
|
|
||||||
func (self *StateDB) SubRefund(gas uint64) {
|
func (self *StateDB) SubRefund(gas uint64) {
|
||||||
self.journal = append(self.journal, refundChange{
|
self.journal = append(self.journal, refundChange{
|
||||||
prev: self.refund})
|
prev: self.refund})
|
||||||
|
|
@ -221,6 +229,16 @@ func (self *StateDB) GetNonce(addr common.Address) uint64 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetStorageRoot retrieves the storage root from the given address or empty
|
||||||
|
// if object not found.
|
||||||
|
func (self *StateDB) GetStorageRoot(addr common.Address) common.Hash {
|
||||||
|
stateObject := self.getStateObject(addr)
|
||||||
|
if stateObject != nil {
|
||||||
|
return stateObject.Root()
|
||||||
|
}
|
||||||
|
return common.Hash{}
|
||||||
|
}
|
||||||
|
|
||||||
func (self *StateDB) GetCode(addr common.Address) []byte {
|
func (self *StateDB) GetCode(addr common.Address) []byte {
|
||||||
stateObject := self.getStateObject(addr)
|
stateObject := self.getStateObject(addr)
|
||||||
if stateObject != nil {
|
if stateObject != nil {
|
||||||
|
|
@ -252,6 +270,28 @@ func (self *StateDB) GetCodeHash(addr common.Address) common.Hash {
|
||||||
return common.BytesToHash(stateObject.CodeHash())
|
return common.BytesToHash(stateObject.CodeHash())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (self *StateDB) GetAccountInfo(addr common.Address) *AccountInfo {
|
||||||
|
result := AccountInfo{}
|
||||||
|
|
||||||
|
stateObject := self.getStateObject(addr)
|
||||||
|
if stateObject == nil {
|
||||||
|
result.Balance = common.Big0
|
||||||
|
return &result
|
||||||
|
}
|
||||||
|
|
||||||
|
if stateObject.code != nil {
|
||||||
|
result.CodeSize = len(stateObject.code)
|
||||||
|
} else {
|
||||||
|
result.CodeSize, _ = self.db.ContractCodeSize(stateObject.addrHash, common.BytesToHash(stateObject.CodeHash()))
|
||||||
|
}
|
||||||
|
result.Nonce = stateObject.Nonce()
|
||||||
|
result.Balance = stateObject.Balance()
|
||||||
|
result.CodeHash = common.BytesToHash(stateObject.CodeHash())
|
||||||
|
result.StorageHash = stateObject.Root()
|
||||||
|
|
||||||
|
return &result
|
||||||
|
}
|
||||||
|
|
||||||
func (self *StateDB) GetState(addr common.Address, bhash common.Hash) common.Hash {
|
func (self *StateDB) GetState(addr common.Address, bhash common.Hash) common.Hash {
|
||||||
stateObject := self.getStateObject(addr)
|
stateObject := self.getStateObject(addr)
|
||||||
if stateObject != nil {
|
if stateObject != nil {
|
||||||
|
|
|
||||||
|
|
@ -623,6 +623,24 @@ func (s *PublicBlockChainAPI) GetCode(ctx context.Context, address common.Addres
|
||||||
return code, state.Error()
|
return code, state.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAccountInfo returns the information at the given address in the state for the given block number.
|
||||||
|
func (s *PublicBlockChainAPI) GetAccountInfo(ctx context.Context, address common.Address, blockNr rpc.BlockNumber) (map[string]interface{}, error) {
|
||||||
|
state, _, err := s.b.StateAndHeaderByNumber(ctx, blockNr)
|
||||||
|
if state == nil || err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
info := state.GetAccountInfo(address)
|
||||||
|
result := map[string]interface{}{
|
||||||
|
"address": address,
|
||||||
|
"balance": (*hexutil.Big)(info.Balance),
|
||||||
|
"codeSize": info.CodeSize,
|
||||||
|
"codeHash": info.CodeHash,
|
||||||
|
"nonce": info.Nonce,
|
||||||
|
"storageHash": info.StorageHash,
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetStorageAt returns the storage from the state at the given address, key and
|
// GetStorageAt returns the storage from the state at the given address, key and
|
||||||
// block number. The rpc.LatestBlockNumber and rpc.PendingBlockNumber meta block
|
// block number. The rpc.LatestBlockNumber and rpc.PendingBlockNumber meta block
|
||||||
// numbers are also allowed.
|
// numbers are also allowed.
|
||||||
|
|
|
||||||
|
|
@ -5329,6 +5329,13 @@ var methods = function () {
|
||||||
inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter]
|
inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter]
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var getAccountInfo = new Method({
|
||||||
|
name: 'getAccountInfo',
|
||||||
|
call: 'eth_getAccountInfo',
|
||||||
|
params: 2,
|
||||||
|
inputFormatter: [formatters.inputAddressFormatter, formatters.inputDefaultBlockNumberFormatter]
|
||||||
|
});
|
||||||
|
|
||||||
var getBlock = new Method({
|
var getBlock = new Method({
|
||||||
name: 'getBlock',
|
name: 'getBlock',
|
||||||
call: blockCall,
|
call: blockCall,
|
||||||
|
|
@ -5513,6 +5520,7 @@ var methods = function () {
|
||||||
getBalance,
|
getBalance,
|
||||||
getStorageAt,
|
getStorageAt,
|
||||||
getCode,
|
getCode,
|
||||||
|
getAccountInfo,
|
||||||
getBlock,
|
getBlock,
|
||||||
getBlockSigners,
|
getBlockSigners,
|
||||||
getStakerROI,
|
getStakerROI,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue