stargate: migrate types (#670)

* changelog v0.4.0

* stargate: types changes

* msg and handler changes

* validation

* fixes

* more fixes

* more test fixes

* changelog

* changelog

* lint

* rm comment

* lint

* redundant if condition
This commit is contained in:
Federico Kunze
2021-01-06 17:56:40 -03:00
committed by GitHub
parent 64ada18b01
commit 9cbb4dcf6d
33 changed files with 540 additions and 586 deletions
+6 -6
View File
@@ -151,6 +151,7 @@ func (k Keeper) GetAllTxLogs(ctx sdk.Context) []types.TransactionLogs {
// GetAccountStorage return state storage associated with an account
func (k Keeper) GetAccountStorage(ctx sdk.Context, address common.Address) (types.Storage, error) {
storage := types.Storage{}
err := k.ForEachStorage(ctx, address, func(key, value common.Hash) bool {
storage = append(storage, types.NewState(key, value))
return false
@@ -164,9 +165,9 @@ func (k Keeper) GetAccountStorage(ctx sdk.Context, address common.Address) (type
// GetChainConfig gets block height from block consensus hash
func (k Keeper) GetChainConfig(ctx sdk.Context) (types.ChainConfig, bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixChainConfig)
// get from an empty key that's already prefixed by KeyPrefixChainConfig
bz := store.Get([]byte{})
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.KeyPrefixChainConfig)
if len(bz) == 0 {
return types.ChainConfig{}, false
}
@@ -178,8 +179,7 @@ func (k Keeper) GetChainConfig(ctx sdk.Context) (types.ChainConfig, bool) {
// SetChainConfig sets the mapping from block consensus hash to block height
func (k Keeper) SetChainConfig(ctx sdk.Context, config types.ChainConfig) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixChainConfig)
store := ctx.KVStore(k.storeKey)
bz := k.cdc.MustMarshalBinaryBare(config)
// get to an empty key that's already prefixed by KeyPrefixChainConfig
store.Set([]byte{}, bz)
store.Set(types.KeyPrefixChainConfig, bz)
}
+2 -2
View File
@@ -98,10 +98,10 @@ func (suite *KeeperTestSuite) TestTransactionLogs() {
txLogs := suite.app.EvmKeeper.GetAllTxLogs(suite.ctx)
suite.Require().Equal(2, len(txLogs))
suite.Require().Equal(ethcmn.Hash{}.String(), txLogs[0].Hash.String())
suite.Require().Equal(ethcmn.Hash{}.String(), txLogs[0].Hash)
suite.Require().Equal([]*ethtypes.Log{log2, log3}, txLogs[0].Logs)
suite.Require().Equal(ethHash.String(), txLogs[1].Hash.String())
suite.Require().Equal(ethHash.String(), txLogs[1].Hash)
suite.Require().Equal([]*ethtypes.Log{log}, txLogs[1].Logs)
}
+104
View File
@@ -0,0 +1,104 @@
package keeper
import (
"github.com/ethereum/go-ethereum/common"
tmtypes "github.com/tendermint/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
ethermint "github.com/cosmos/ethermint/types"
"github.com/cosmos/ethermint/x/evm/types"
)
// EthereumTx implements the Msg/EthereumTx gRPC method.
func (k Keeper) EthereumTx(ctx sdk.Context, msg types.MsgEthereumTx) (*sdk.Result, error) {
// parse the chainID from a string to a base-10 integer
chainIDEpoch, err := ethermint.ParseChainID(ctx.ChainID())
if err != nil {
return nil, err
}
// Verify signature and retrieve sender address
sender, err := msg.VerifySig(chainIDEpoch)
if err != nil {
return nil, err
}
var recipient *common.Address
if msg.Data.Recipient != nil {
addr := common.HexToAddress(msg.Data.Recipient.Address)
recipient = &addr
}
txHash := tmtypes.Tx(ctx.TxBytes()).Hash()
ethHash := common.BytesToHash(txHash)
st := types.StateTransition{
AccountNonce: msg.Data.AccountNonce,
Price: msg.Data.Price.BigInt(),
GasLimit: msg.Data.GasLimit,
Recipient: recipient,
Amount: msg.Data.Amount.BigInt(),
Payload: msg.Data.Payload,
Csdb: k.CommitStateDB.WithContext(ctx),
ChainID: chainIDEpoch,
TxHash: &ethHash,
Sender: sender,
Simulate: ctx.IsCheckTx(),
}
// since the txCount is used by the stateDB, and a simulated tx is run only on the node it's submitted to,
// then this will cause the txCount/stateDB of the node that ran the simulated tx to be different than the
// other nodes, causing a consensus error
if !st.Simulate {
// Prepare db for logs
blockHash := types.HashFromContext(ctx)
k.CommitStateDB.Prepare(ethHash, blockHash, k.TxCount)
k.TxCount++
}
config, found := k.GetChainConfig(ctx)
if !found {
return nil, types.ErrChainConfigNotFound
}
executionResult, err := st.TransitionDb(ctx, config)
if err != nil {
return nil, err
}
if !st.Simulate {
// update block bloom filter
k.Bloom.Or(k.Bloom, executionResult.Bloom)
// update transaction logs in KVStore
err = k.SetLogs(ctx, common.BytesToHash(txHash), executionResult.Logs)
if err != nil {
panic(err)
}
}
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeEthereumTx,
sdk.NewAttribute(sdk.AttributeKeyAmount, msg.Data.Amount.String()),
),
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
sdk.NewAttribute(sdk.AttributeKeySender, sender.String()),
),
})
if msg.Data.Recipient != nil {
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeEthereumTx,
sdk.NewAttribute(types.AttributeKeyRecipient, msg.Data.Recipient.Address),
),
)
}
executionResult.Result.Events = ctx.EventManager().Events()
return executionResult.Result, nil
}
-31
View File
@@ -1,7 +1,6 @@
package keeper
import (
"encoding/json"
"fmt"
"strconv"
@@ -39,8 +38,6 @@ 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")
}
@@ -183,31 +180,3 @@ 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) {
hexAddress := path[1]
addr := ethcmn.HexToAddress(hexAddress)
var storage types.Storage
err := keeper.ForEachStorage(ctx, addr, func(key, value ethcmn.Hash) bool {
storage = append(storage, types.NewState(key, value))
return false
})
if err != nil {
return nil, err
}
res := types.GenesisAccount{
Address: hexAddress,
Code: keeper.GetCode(ctx, addr),
Storage: storage,
}
// TODO: codec.MarshalJSONIndent doesn't call the String() method of types properly
bz, err := json.MarshalIndent(res, "", "\t")
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
-1
View File
@@ -34,7 +34,6 @@ func (suite *KeeperTestSuite) TestQuerier() {
}, 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},
}
+10 -10
View File
@@ -582,7 +582,7 @@ func (suite *KeeperTestSuite) TestCommitStateDB_ForEachStorage() {
name string
malleate func()
callback func(key, value ethcmn.Hash) (stop bool)
expValues []ethcmn.Hash
expValues []string
}{
{
"aggregate state",
@@ -595,12 +595,12 @@ func (suite *KeeperTestSuite) TestCommitStateDB_ForEachStorage() {
storage = append(storage, types.NewState(key, value))
return false
},
[]ethcmn.Hash{
ethcmn.BytesToHash([]byte("value0")),
ethcmn.BytesToHash([]byte("value1")),
ethcmn.BytesToHash([]byte("value2")),
ethcmn.BytesToHash([]byte("value3")),
ethcmn.BytesToHash([]byte("value4")),
[]string{
ethcmn.BytesToHash([]byte("value0")).String(),
ethcmn.BytesToHash([]byte("value1")).String(),
ethcmn.BytesToHash([]byte("value2")).String(),
ethcmn.BytesToHash([]byte("value3")).String(),
ethcmn.BytesToHash([]byte("value4")).String(),
},
},
{
@@ -616,8 +616,8 @@ func (suite *KeeperTestSuite) TestCommitStateDB_ForEachStorage() {
}
return false
},
[]ethcmn.Hash{
ethcmn.BytesToHash([]byte("filtervalue")),
[]string{
ethcmn.BytesToHash([]byte("filtervalue")).String(),
},
},
}
@@ -632,7 +632,7 @@ func (suite *KeeperTestSuite) TestCommitStateDB_ForEachStorage() {
suite.Require().NoError(err)
suite.Require().Equal(len(tc.expValues), len(storage), fmt.Sprintf("Expected values:\n%v\nStorage Values\n%v", tc.expValues, storage))
vals := make([]ethcmn.Hash, len(storage))
vals := make([]string, len(storage))
for i := range storage {
vals[i] = storage[i].Value
}