Skip to content

Commit 476bda6

Browse files
authored
Merge pull request #479 from gzliudan/get-account-info
add method `eth_getAccountInfo`
2 parents 5223454 + ece58f0 commit 476bda6

File tree

4 files changed

+70
-0
lines changed

4 files changed

+70
-0
lines changed

core/state/state_object.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,10 @@ func (self *stateObject) Nonce() uint64 {
393393
return self.data.Nonce
394394
}
395395

396+
func (self *stateObject) Root() common.Hash {
397+
return self.data.Root
398+
}
399+
396400
// Never called, but must be present to allow stateObject to be used
397401
// as a vm.Account interface that also satisfies the vm.ContractRef
398402
// interface. Interfaces are awesome.

core/state/statedb.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ type StateDB struct {
8383
lock sync.Mutex
8484
}
8585

86+
type AccountInfo struct {
87+
CodeSize int
88+
Nonce uint64
89+
Balance *big.Int
90+
CodeHash common.Hash
91+
StorageHash common.Hash
92+
}
93+
8694
func (self *StateDB) SubRefund(gas uint64) {
8795
self.journal = append(self.journal, refundChange{
8896
prev: self.refund})
@@ -221,6 +229,16 @@ func (self *StateDB) GetNonce(addr common.Address) uint64 {
221229
return 0
222230
}
223231

232+
// GetStorageRoot retrieves the storage root from the given address or empty
233+
// if object not found.
234+
func (self *StateDB) GetStorageRoot(addr common.Address) common.Hash {
235+
stateObject := self.getStateObject(addr)
236+
if stateObject != nil {
237+
return stateObject.Root()
238+
}
239+
return common.Hash{}
240+
}
241+
224242
func (self *StateDB) GetCode(addr common.Address) []byte {
225243
stateObject := self.getStateObject(addr)
226244
if stateObject != nil {
@@ -252,6 +270,28 @@ func (self *StateDB) GetCodeHash(addr common.Address) common.Hash {
252270
return common.BytesToHash(stateObject.CodeHash())
253271
}
254272

273+
func (self *StateDB) GetAccountInfo(addr common.Address) *AccountInfo {
274+
result := AccountInfo{}
275+
276+
stateObject := self.getStateObject(addr)
277+
if stateObject == nil {
278+
result.Balance = common.Big0
279+
return &result
280+
}
281+
282+
if stateObject.code != nil {
283+
result.CodeSize = len(stateObject.code)
284+
} else {
285+
result.CodeSize, _ = self.db.ContractCodeSize(stateObject.addrHash, common.BytesToHash(stateObject.CodeHash()))
286+
}
287+
result.Nonce = stateObject.Nonce()
288+
result.Balance = stateObject.Balance()
289+
result.CodeHash = common.BytesToHash(stateObject.CodeHash())
290+
result.StorageHash = stateObject.Root()
291+
292+
return &result
293+
}
294+
255295
func (self *StateDB) GetState(addr common.Address, bhash common.Hash) common.Hash {
256296
stateObject := self.getStateObject(addr)
257297
if stateObject != nil {

internal/ethapi/api.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,24 @@ func (s *PublicBlockChainAPI) GetCode(ctx context.Context, address common.Addres
623623
return code, state.Error()
624624
}
625625

626+
// GetAccountInfo returns the information at the given address in the state for the given block number.
627+
func (s *PublicBlockChainAPI) GetAccountInfo(ctx context.Context, address common.Address, blockNr rpc.BlockNumber) (map[string]interface{}, error) {
628+
state, _, err := s.b.StateAndHeaderByNumber(ctx, blockNr)
629+
if state == nil || err != nil {
630+
return nil, err
631+
}
632+
info := state.GetAccountInfo(address)
633+
result := map[string]interface{}{
634+
"address": address,
635+
"balance": (*hexutil.Big)(info.Balance),
636+
"codeSize": info.CodeSize,
637+
"codeHash": info.CodeHash,
638+
"nonce": info.Nonce,
639+
"storageHash": info.StorageHash,
640+
}
641+
return result, nil
642+
}
643+
626644
// GetStorageAt returns the storage from the state at the given address, key and
627645
// block number. The rpc.LatestBlockNumber and rpc.PendingBlockNumber meta block
628646
// numbers are also allowed.

internal/jsre/deps/web3.js

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)