implement eth_exportAccount (#331)

This commit is contained in:
noot
2020-06-17 14:23:36 -04:00
committed by GitHub
parent 94f6acc651
commit 9f573690a6
8 changed files with 123 additions and 6 deletions
+1
View File
@@ -21,6 +21,7 @@ const (
QueryBloom = types.QueryBloom
QueryLogs = types.QueryLogs
QueryAccount = types.QueryAccount
QueryExportAccount = types.QueryExportAccount
)
// nolint
+29
View File
@@ -42,6 +42,8 @@ func NewQuerier(keeper Keeper) sdk.Querier {
return queryLogs(ctx, keeper)
case types.QueryAccount:
return queryAccount(ctx, path, keeper)
case types.QueryExportAccount:
return queryExportAccount(ctx, path, keeper)
default:
return nil, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown query endpoint")
}
@@ -195,3 +197,30 @@ func queryAccount(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error)
}
return bz, nil
}
func queryExportAccount(ctx sdk.Context, path []string, keeper Keeper) ([]byte, error) {
addr := ethcmn.HexToAddress(path[1])
var storage []types.GenesisStorage
err := keeper.CommitStateDB.ForEachStorage(addr, func(key, value ethcmn.Hash) bool {
storage = append(storage, types.NewGenesisStorage(key, value))
return false
})
if err != nil {
return nil, err
}
res := types.GenesisAccount{
Address: addr,
Balance: keeper.GetBalance(ctx, addr),
Code: keeper.GetCode(ctx, addr),
Storage: storage,
}
bz, err := codec.MarshalJSONIndent(keeper.cdc, res)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
+1
View File
@@ -29,6 +29,7 @@ func (suite *KeeperTestSuite) TestQuerier() {
// {"logs bloom", []string{types.QueryLogsBloom, "0x0"}, func() {}, true},
{"logs", []string{types.QueryLogs, "0x0"}, func() {}, true},
{"account", []string{types.QueryAccount, "0x0"}, func() {}, true},
{"exportAccount", []string{types.QueryExportAccount, "0x0"}, func() {}, true},
{"unknown request", []string{"other"}, func() {}, false},
}
+3
View File
@@ -19,6 +19,7 @@ const (
QueryBloom = "bloom"
QueryLogs = "logs"
QueryAccount = "account"
QueryExportAccount = "exportAccount"
)
// QueryResProtocolVersion is response type for protocol version query
@@ -99,3 +100,5 @@ type QueryResAccount struct {
CodeHash []byte `json:"codeHash"`
Nonce uint64 `json:"nonce"`
}
type QueryResExportAccount = GenesisAccount
+2 -2
View File
@@ -221,7 +221,7 @@ func (so *stateObject) markSuicided() {
// commitState commits all dirty storage to a KVStore.
func (so *stateObject) commitState() {
ctx := so.stateDB.ctx
store := prefix.NewStore(ctx.KVStore(so.stateDB.storeKey), KeyPrefixStorage)
store := prefix.NewStore(ctx.KVStore(so.stateDB.storeKey), AddressStoragePrefix(so.Address()))
for key, value := range so.dirtyStorage {
delete(so.dirtyStorage, key)
@@ -333,7 +333,7 @@ func (so *stateObject) GetCommittedState(_ ethstate.Database, key ethcmn.Hash) e
// otherwise load the value from the KVStore
ctx := so.stateDB.ctx
store := prefix.NewStore(ctx.KVStore(so.stateDB.storeKey), KeyPrefixStorage)
store := prefix.NewStore(ctx.KVStore(so.stateDB.storeKey), AddressStoragePrefix(so.Address()))
rawValue := store.Get(prefixKey.Bytes())
if len(rawValue) > 0 {
+2 -1
View File
@@ -717,7 +717,8 @@ func (csdb *CommitStateDB) ForEachStorage(addr ethcmn.Address, cb func(key, valu
}
store := csdb.ctx.KVStore(csdb.storeKey)
iterator := sdk.KVStorePrefixIterator(store, AddressStoragePrefix(so.Address()))
prefix := AddressStoragePrefix(so.Address())
iterator := sdk.KVStorePrefixIterator(store, prefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {