created structs

This commit is contained in:
Simon Jentzsch 2018-10-18 11:57:29 +02:00
parent 65c4bb2caf
commit d9100736b4

View file

@ -502,8 +502,24 @@ func (s *PublicBlockChainAPI) GetBalance(ctx context.Context, address common.Add
return (*hexutil.Big)(state.GetBalance(address)), state.Error() return (*hexutil.Big)(state.GetBalance(address)), state.Error()
} }
// Result structs for GetProof
type AccountResult struct {
Address common.Address `json:"address"`
AccountProof []string `json:"accountProof"`
Balance *hexutil.Big `json:"balance"`
CodeHash common.Hash `json:"codeHash"`
Nonce hexutil.Uint64 `json:"nonce"`
StorageHash common.Hash `json:"storageHash"`
StorageProof []StorageResult `json:"storageProof"`
}
type StorageResult struct {
Key string `json:"key"`
Value common.Hash `json:"value"`
Proof []string `json:"proof"`
}
// GetProof returns the Merkle-proof for a given account and optionally some storage keys. // GetProof returns the Merkle-proof for a given account and optionally some storage keys.
func (s *PublicBlockChainAPI) GetProof(ctx context.Context, address common.Address, storageKeys []string, blockNr rpc.BlockNumber) (map[string]interface{}, error) { func (s *PublicBlockChainAPI) GetProof(ctx context.Context, address common.Address, storageKeys []string, blockNr rpc.BlockNumber) (*AccountResult, error) {
state, _, err := s.b.StateAndHeaderByNumber(ctx, blockNr) state, _, err := s.b.StateAndHeaderByNumber(ctx, blockNr)
if state == nil || err != nil { if state == nil || err != nil {
return nil, err return nil, err
@ -512,7 +528,7 @@ func (s *PublicBlockChainAPI) GetProof(ctx context.Context, address common.Addre
storageTrie := state.StorageTrie(address) storageTrie := state.StorageTrie(address)
storageHash := types.EmptyRootHash storageHash := types.EmptyRootHash
codeHash := state.GetCodeHash(address) codeHash := state.GetCodeHash(address)
storageProof := make([]map[string]interface{}, len(storageKeys)) storageProof := make([]StorageResult, len(storageKeys))
// if we have a storageTrie, (which means the account exists), we can update the storagehash // if we have a storageTrie, (which means the account exists), we can update the storagehash
if storageTrie != nil { if storageTrie != nil {
@ -525,32 +541,21 @@ func (s *PublicBlockChainAPI) GetProof(ctx context.Context, address common.Addre
// create the proof for the storageKeys // create the proof for the storageKeys
for i, key := range storageKeys { for i, key := range storageKeys {
if storageTrie != nil { if storageTrie != nil {
storageProof[i] = map[string]interface{}{ storageProof[i] = StorageResult{key, state.GetState(address, common.HexToHash(key)), common.ToHexArray(state.GetStorageProof(address, common.HexToHash(key)))}
"key": key,
"value": state.GetState(address, common.HexToHash(key)),
"proof": common.ToHexArray(state.GetStorageProof(address, common.HexToHash(key))),
}
} else { } else {
storageProof[i] = map[string]interface{}{ storageProof[i] = StorageResult{key, common.Hash{}, []string{}}
"key": key,
"value": common.Hash{},
"proof": []string{},
}
} }
} }
// fill results for the account return &AccountResult{
fields := map[string]interface{}{ Address: address,
"address": address, AccountProof: common.ToHexArray(state.GetProof(address)),
"accountProof": common.ToHexArray(state.GetProof(address)), Balance: (*hexutil.Big)(state.GetBalance(address)),
"balance": (*hexutil.Big)(state.GetBalance(address)), CodeHash: codeHash,
"codeHash": codeHash, Nonce: hexutil.Uint64(state.GetNonce(address)),
"nonce": hexutil.Uint64(state.GetNonce(address)), StorageHash: storageHash,
"storageHash": storageHash, StorageProof: storageProof,
"storageProof": storageProof, }, state.Error()
}
return fields, state.Error()
} }
// GetBlockByNumber returns the requested block. When blockNr is -1 the chain head is returned. When fullTx is true all // GetBlockByNumber returns the requested block. When blockNr is -1 the chain head is returned. When fullTx is true all