Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
### FEATURES

- [\#346](https://github.com/cosmos/evm/pull/346) Add eth_createAccessList method and implementation
- [\#502](https://github.com/cosmos/evm/pull/502) Add block time in derived logs.

### STATE BREAKING

Expand Down
257 changes: 159 additions & 98 deletions api/cosmos/evm/vm/v1/evm.pulsar.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion mempool/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (b Blockchain) StateAt(hash common.Hash) (vm.StateDB, error) {
}

appHash := ctx.BlockHeader().AppHash
stateDB := statedb.New(ctx, b.vmKeeper, statedb.NewEmptyTxConfig(common.Hash(appHash)))
stateDB := statedb.New(ctx, b.vmKeeper, statedb.NewEmptyTxConfig())

b.logger.Debug("StateDB created successfully", "app_hash", common.Hash(appHash).Hex())
return stateDB, nil
Expand Down
4 changes: 2 additions & 2 deletions precompiles/common/balance_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func TestAfterBalanceChange(t *testing.T) {
tKey := storetypes.NewTransientStoreKey("test_t")
ctx := sdktestutil.DefaultContext(storeKey, tKey)

stateDB := statedb.New(ctx, mocks.NewEVMKeeper(), statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash())))
stateDB := statedb.New(ctx, mocks.NewEVMKeeper(), statedb.NewEmptyTxConfig())

_, addrs, err := testutil.GeneratePrivKeyAddressPairs(2)
require.NoError(t, err)
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestAfterBalanceChangeErrors(t *testing.T) {
storeKey := storetypes.NewKVStoreKey("test")
tKey := storetypes.NewTransientStoreKey("test_t")
ctx := sdktestutil.DefaultContext(storeKey, tKey)
stateDB := statedb.New(ctx, mocks.NewEVMKeeper(), statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash())))
stateDB := statedb.New(ctx, mocks.NewEVMKeeper(), statedb.NewEmptyTxConfig())

_, addrs, err := testutil.GeneratePrivKeyAddressPairs(1)
require.NoError(t, err)
Expand Down
3 changes: 3 additions & 0 deletions proto/cosmos/evm/vm/v1/evm.proto
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ message Log {
// reorganisation. You must pay attention to this field if you receive logs
// through a filter query.
bool removed = 9;

// block_timestamp is the timestamp of the block in which the transaction was
uint64 block_timestamp = 10 [ (gogoproto.jsontag) = "blockTimestamp" ];
}

// TxResult stores results of Tx execution.
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/precompiles/gov/test_gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,10 @@ func (s *PrecompileTestSuite) TestRun() {
s.Require().NoError(err, "failed to instantiate EVM config")

// Instantiate EVM
headerHash := ctx.HeaderHash()
stDB := statedb.New(
ctx,
s.network.App.GetEVMKeeper(),
statedb.NewEmptyTxConfig(common.BytesToHash(headerHash)),
statedb.NewEmptyTxConfig(),
)
evm := s.network.App.GetEVMKeeper().NewEVM(
ctx, *msg, cfg, nil, stDB,
Expand Down
3 changes: 1 addition & 2 deletions tests/integration/precompiles/staking/test_staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,10 @@ func (s *PrecompileTestSuite) TestRun() {
s.Require().NoError(err, "failed to instantiate EVM config")

// Instantiate EVM
headerHash := ctx.HeaderHash()
stDB := statedb.New(
ctx,
s.network.App.GetEVMKeeper(),
statedb.NewEmptyTxConfig(common.BytesToHash(headerHash)),
statedb.NewEmptyTxConfig(),
)
evm := s.network.App.GetEVMKeeper().NewEVM(
ctx, *msg, cfg, nil, stDB,
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/x/vm/test_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (s *GenesisTestSuite) TestInitGenesis() {
ctx = s.network.GetContext()
vmdb = statedb.New(
ctx, s.network.App.GetEVMKeeper(),
statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash())))
statedb.NewEmptyTxConfig())

tc.malleate(s.network)
err := vmdb.Commit()
Expand Down
1 change: 0 additions & 1 deletion tests/integration/x/vm/test_grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ func (s *KeeperTestSuite) TestQueryTxLogs() {
for _, tc := range testCases {
s.Run(fmt.Sprintf("Case %s", tc.msg), func() {
txCfg := statedb.NewTxConfig(
common.BytesToHash(s.Network.GetContext().HeaderHash()),
txHash,
txIndex,
logIndex,
Expand Down
1 change: 0 additions & 1 deletion tests/integration/x/vm/test_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ func (s *KeeperTestSuite) TestEvmHooks() {
ctx := s.Network.GetContext()
txHash := common.BigToHash(big.NewInt(1))
vmdb := statedb.New(ctx, k, statedb.NewTxConfig(
common.BytesToHash(ctx.HeaderHash()),
txHash,
0,
0,
Expand Down
1 change: 0 additions & 1 deletion tests/integration/x/vm/test_statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,6 @@ func (s *KeeperTestSuite) TestAddLog() {
s.Run(tc.name, func() {
s.SetupTest()
vmdb := statedb.New(s.Network.GetContext(), s.Network.App.GetEVMKeeper(), statedb.NewTxConfig(
common.BytesToHash(s.Network.GetContext().HeaderHash()),
tc.hash,
0, 0,
))
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/x/vm/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (s *KeeperTestSuite) EvmDenom() string {
}

func (s *KeeperTestSuite) StateDB() *statedb.StateDB {
return statedb.New(s.Network.GetContext(), s.Network.App.GetEVMKeeper(), statedb.NewEmptyTxConfig(common.BytesToHash(s.Network.GetContext().HeaderHash())))
return statedb.New(s.Network.GetContext(), s.Network.App.GetEVMKeeper(), statedb.NewEmptyTxConfig())
}

// DeployTestContract deploy a test erc20 contract and returns the contract address
Expand Down
5 changes: 1 addition & 4 deletions testutil/integration/evm/network/unit_network.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package network

import (
"github.com/ethereum/go-ethereum/common"

"github.com/cosmos/evm"
"github.com/cosmos/evm/x/vm/statedb"

Expand Down Expand Up @@ -36,11 +34,10 @@ func NewUnitTestNetwork(createEvmApp CreateEvmApp, opts ...ConfigOption) *UnitTe

// GetStateDB returns the state database for the current block.
func (n *UnitTestNetwork) GetStateDB() *statedb.StateDB {
headerHash := n.GetContext().HeaderHash()
return statedb.New(
n.GetContext(),
n.app.GetEVMKeeper(),
statedb.NewEmptyTxConfig(common.BytesToHash(headerHash)),
statedb.NewEmptyTxConfig(),
)
}

Expand Down
4 changes: 1 addition & 3 deletions testutil/statedb.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package testutil

import (
"github.com/ethereum/go-ethereum/common"

anteinterfaces "github.com/cosmos/evm/ante/interfaces"
"github.com/cosmos/evm/x/vm/statedb"

Expand All @@ -11,5 +9,5 @@ import (

// NewStateDB returns a new StateDB for testing purposes.
func NewStateDB(ctx sdk.Context, evmKeeper anteinterfaces.EVMKeeper) *statedb.StateDB {
return statedb.New(ctx, evmKeeper, statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash())))
return statedb.New(ctx, evmKeeper, statedb.NewEmptyTxConfig())
}
7 changes: 3 additions & 4 deletions x/vm/keeper/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ func (k *Keeper) EVMConfig(ctx sdk.Context, proposerAddress sdk.ConsAddress) (*s
// TxConfig loads `TxConfig` from current transient storage
func (k *Keeper) TxConfig(ctx sdk.Context, txHash common.Hash) statedb.TxConfig {
return statedb.NewTxConfig(
common.BytesToHash(ctx.HeaderHash()), // BlockHash
txHash, // TxHash
uint(k.GetTxIndexTransient(ctx)), // TxIndex
uint(k.GetLogSizeTransient(ctx)), // LogIndex
txHash, // TxHash
uint(k.GetTxIndexTransient(ctx)), // TxIndex
uint(k.GetLogSizeTransient(ctx)), // LogIndex
)
}

Expand Down
10 changes: 5 additions & 5 deletions x/vm/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func (k Keeper) EthCall(c context.Context, req *types.EthCallRequest) (*types.Ms
}

msg := args.ToMessage(cfg.BaseFee, false, false)
txConfig := statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash()))
txConfig := statedb.NewEmptyTxConfig()

// pass false to not commit StateDB
res, err := k.ApplyMessageWithConfig(ctx, *msg, nil, false, cfg, txConfig, false)
Expand Down Expand Up @@ -324,7 +324,7 @@ func (k Keeper) EstimateGasInternal(c context.Context, req *types.EthCallRequest
nonce := k.GetNonce(ctx, args.GetFrom())
args.Nonce = (*hexutil.Uint64)(&nonce)

txConfig := statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash()))
txConfig := statedb.NewEmptyTxConfig()

if args.Gas == nil {
args.Gas = new(hexutil.Uint64)
Expand Down Expand Up @@ -524,7 +524,7 @@ func (k Keeper) TraceTx(c context.Context, req *types.QueryTraceTxRequest) (*typ
}

signer := ethtypes.MakeSigner(types.GetEthChainConfig(), big.NewInt(ctx.BlockHeight()), uint64(ctx.BlockTime().Unix())) //#nosec G115 -- int overflow is not a concern here
txConfig := statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash()))
txConfig := statedb.NewEmptyTxConfig()

// gas used at this point corresponds to GetProposerAddress & CalculateBaseFee
// need to reset gas meter per transaction to be consistent with tx execution
Expand Down Expand Up @@ -618,7 +618,7 @@ func (k Keeper) TraceBlock(c context.Context, req *types.QueryTraceBlockRequest)
txsLength := len(req.Txs)
results := make([]*types.TxTraceResult, 0, txsLength)

txConfig := statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash()))
txConfig := statedb.NewEmptyTxConfig()

for i, tx := range req.Txs {
result := types.TxTraceResult{}
Expand Down Expand Up @@ -698,7 +698,7 @@ func (k *Keeper) traceTx(
}

tCtx := &tracers.Context{
BlockHash: txConfig.BlockHash,
BlockHash: common.BytesToHash(ctx.HeaderHash()),
TxIndex: int(txConfig.TxIndex), //#nosec G115 -- int overflow is not a concern here
TxHash: txConfig.TxHash,
}
Expand Down
9 changes: 5 additions & 4 deletions x/vm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (k *Keeper) ApplyTransaction(ctx sdk.Context, tx *ethtypes.Transaction) (*t
TxHash: txConfig.TxHash,
ContractAddress: contractAddr,
GasUsed: res.GasUsed,
BlockHash: txConfig.BlockHash,
BlockHash: common.BytesToHash(ctx.HeaderHash()),
BlockNumber: big.NewInt(ctx.BlockHeight()),
TransactionIndex: txConfig.TxIndex,
}
Expand Down Expand Up @@ -308,7 +308,7 @@ func (k *Keeper) ApplyMessage(ctx sdk.Context, msg core.Message, tracer *tracing
return nil, errorsmod.Wrap(err, "failed to load evm config")
}

txConfig := statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash()))
txConfig := statedb.NewEmptyTxConfig()
return k.ApplyMessageWithConfig(ctx, msg, tracer, commit, cfg, txConfig, internal)
}

Expand Down Expand Up @@ -394,7 +394,7 @@ func (k *Keeper) ApplyMessageWithConfig(ctx sdk.Context, msg core.Message, trace
return nil, errorsmod.Wrap(core.ErrIntrinsicGas, "apply message")
}
// Gas limit suffices for the floor data cost (EIP-7623)
rules := ethCfg.Rules(big.NewInt(ctx.BlockHeight()), true, uint64(ctx.BlockTime().Unix())) //#nosec G115 -- int overflow is not a concern here
rules := ethCfg.Rules(evm.Context.BlockNumber, true, evm.Context.Time)
if rules.IsPrague {
floorDataGas, err := core.FloorDataGas(msg.Data)
if err != nil {
Expand Down Expand Up @@ -507,12 +507,13 @@ func (k *Keeper) ApplyMessageWithConfig(ctx sdk.Context, msg core.Message, trace
ret = evm.Interpreter().ReturnData()
}

logs := stateDB.GetLogs(uint64(ctx.BlockHeight()), common.BytesToHash(ctx.HeaderHash()), evm.Context.Time) //#nosec G115 -- int overflow is not a concern here
return &types.MsgEthereumTxResponse{
GasUsed: gasUsed.TruncateInt().Uint64(),
MaxUsedGas: maxUsedGas,
VmError: vmError,
Ret: ret,
Logs: types.NewLogsFromEth(stateDB.Logs()),
Logs: types.NewLogsFromEth(logs),
Hash: txConfig.TxHash.Hex(),
}, nil
}
Expand Down
25 changes: 11 additions & 14 deletions x/vm/statedb/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,27 @@ import (

// TxConfig encapulates the readonly information of current tx for `StateDB`.
type TxConfig struct {
BlockHash common.Hash // hash of current block
TxHash common.Hash // hash of current tx
TxIndex uint // the index of current transaction
LogIndex uint // the index of next log within current block
TxHash common.Hash // hash of current tx
TxIndex uint // the index of current transaction
LogIndex uint // the index of next log within current block
}

// NewTxConfig returns a TxConfig
func NewTxConfig(bhash, thash common.Hash, txIndex, logIndex uint) TxConfig {
func NewTxConfig(thash common.Hash, txIndex, logIndex uint) TxConfig {
return TxConfig{
BlockHash: bhash,
TxHash: thash,
TxIndex: txIndex,
LogIndex: logIndex,
TxHash: thash,
TxIndex: txIndex,
LogIndex: logIndex,
}
}

// NewEmptyTxConfig construct an empty TxConfig,
// used in context where there's no transaction, e.g. `eth_call`/`eth_estimateGas`.
func NewEmptyTxConfig(bhash common.Hash) TxConfig {
func NewEmptyTxConfig() TxConfig {
return TxConfig{
BlockHash: bhash,
TxHash: common.Hash{},
TxIndex: 0,
LogIndex: 0,
TxHash: common.Hash{},
TxIndex: 0,
LogIndex: 0,
}
}

Expand Down
12 changes: 11 additions & 1 deletion x/vm/statedb/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,22 @@ func (s *StateDB) AddLog(log *ethtypes.Log) {
s.journal.append(addLogChange{})

log.TxHash = s.txConfig.TxHash
log.BlockHash = s.txConfig.BlockHash
log.TxIndex = s.txConfig.TxIndex
log.Index = s.txConfig.LogIndex + uint(len(s.logs))
s.logs = append(s.logs, log)
}

// GetLogs returns the logs matching the specified transaction hash, and annotates
// them with the given blockNumber and blockHash.
func (s *StateDB) GetLogs(blockNumber uint64, blockHash common.Hash, blockTime uint64) []*ethtypes.Log {
for _, l := range s.logs {
l.BlockNumber = blockNumber
l.BlockHash = blockHash
l.BlockTimestamp = blockTime
}
return s.logs
}

// Logs returns the logs of current transaction.
func (s *StateDB) Logs() []*ethtypes.Log {
return s.logs
Expand Down
24 changes: 12 additions & 12 deletions x/vm/statedb/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var (
address2 common.Address = common.BigToAddress(big.NewInt(102))
address3 common.Address = common.BigToAddress(big.NewInt(103))
blockHash common.Hash = common.BigToHash(big.NewInt(9999))
emptyTxConfig statedb.TxConfig = statedb.NewEmptyTxConfig(blockHash)
emptyTxConfig statedb.TxConfig = statedb.NewEmptyTxConfig()
)

type StateDBTestSuite struct {
Expand Down Expand Up @@ -587,7 +587,6 @@ func (suite *StateDBTestSuite) TestLog() {
txHash := common.BytesToHash([]byte("tx"))
// use a non-default tx config
txConfig := statedb.NewTxConfig(
blockHash,
txHash,
1, 1,
)
Expand All @@ -601,16 +600,17 @@ func (suite *StateDBTestSuite) TestLog() {
})
suite.Require().Equal(1, len(db.Logs()))
expecedLog := &ethtypes.Log{
Address: address,
Topics: []common.Hash{},
Data: data,
BlockNumber: 1,
BlockHash: blockHash,
TxHash: txHash,
TxIndex: 1,
Index: 1,
Address: address,
Topics: []common.Hash{},
Data: data,
BlockNumber: 1,
BlockHash: blockHash,
BlockTimestamp: 1,
TxHash: txHash,
TxIndex: 1,
Index: 1,
}
suite.Require().Equal(expecedLog, db.Logs()[0])
suite.Require().Equal(expecedLog, db.GetLogs(1, blockHash, 1)[0])

db.AddLog(&ethtypes.Log{
Address: address,
Expand All @@ -620,7 +620,7 @@ func (suite *StateDBTestSuite) TestLog() {
})
suite.Require().Equal(2, len(db.Logs()))
expecedLog.Index++
suite.Require().Equal(expecedLog, db.Logs()[1])
suite.Require().Equal(expecedLog, db.GetLogs(1, blockHash, 1)[1])
}

func (suite *StateDBTestSuite) TestRefund() {
Expand Down
Loading
Loading