Skip to content

Commit 44dd693

Browse files
committed
Problem: no block time in derived logs
Closes: cosmos#501 ref: * ethereum/go-ethereum#31887 * ethereum/execution-apis#639 fix test
1 parent 03083a8 commit 44dd693

File tree

7 files changed

+48
-41
lines changed

7 files changed

+48
-41
lines changed

mempool/blockchain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func (b Blockchain) StateAt(hash common.Hash) (vm.StateDB, error) {
199199
}
200200

201201
appHash := ctx.BlockHeader().AppHash
202-
stateDB := statedb.New(ctx, b.vmKeeper, statedb.NewEmptyTxConfig(common.Hash(appHash)))
202+
stateDB := statedb.New(ctx, b.vmKeeper, statedb.NewEmptyTxConfig())
203203

204204
b.logger.Debug("StateDB created successfully", "app_hash", common.Hash(appHash).Hex())
205205
return stateDB, nil

x/vm/keeper/config.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ func (k *Keeper) EVMConfig(ctx sdk.Context, proposerAddress sdk.ConsAddress) (*s
3535
// TxConfig loads `TxConfig` from current transient storage
3636
func (k *Keeper) TxConfig(ctx sdk.Context, txHash common.Hash) statedb.TxConfig {
3737
return statedb.NewTxConfig(
38-
common.BytesToHash(ctx.HeaderHash()), // BlockHash
39-
txHash, // TxHash
40-
uint(k.GetTxIndexTransient(ctx)), // TxIndex
41-
uint(k.GetLogSizeTransient(ctx)), // LogIndex
38+
txHash, // TxHash
39+
uint(k.GetTxIndexTransient(ctx)), // TxIndex
40+
uint(k.GetLogSizeTransient(ctx)), // LogIndex
4241
)
4342
}
4443

x/vm/keeper/grpc_query.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ func (k Keeper) EthCall(c context.Context, req *types.EthCallRequest) (*types.Ms
251251
}
252252

253253
msg := args.ToMessage(cfg.BaseFee, false, false)
254-
txConfig := statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash()))
254+
txConfig := statedb.NewEmptyTxConfig()
255255

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

327-
txConfig := statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash()))
327+
txConfig := statedb.NewEmptyTxConfig()
328328

329329
if args.Gas == nil {
330330
args.Gas = new(hexutil.Uint64)
@@ -488,7 +488,7 @@ func (k Keeper) TraceTx(c context.Context, req *types.QueryTraceTxRequest) (*typ
488488
}
489489

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

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

585-
txConfig := statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash()))
585+
txConfig := statedb.NewEmptyTxConfig()
586586

587587
for i, tx := range req.Txs {
588588
result := types.TxTraceResult{}
@@ -662,7 +662,7 @@ func (k *Keeper) traceTx(
662662
}
663663

664664
tCtx := &tracers.Context{
665-
BlockHash: txConfig.BlockHash,
665+
BlockHash: common.BytesToHash(ctx.HeaderHash()),
666666
TxIndex: int(txConfig.TxIndex), //#nosec G115 -- int overflow is not a concern here
667667
TxHash: txConfig.TxHash,
668668
}

x/vm/keeper/state_transition.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func (k *Keeper) ApplyTransaction(ctx sdk.Context, tx *ethtypes.Transaction) (*t
207207
TxHash: txConfig.TxHash,
208208
ContractAddress: contractAddr,
209209
GasUsed: res.GasUsed,
210-
BlockHash: txConfig.BlockHash,
210+
BlockHash: common.BytesToHash(ctx.HeaderHash()),
211211
BlockNumber: big.NewInt(ctx.BlockHeight()),
212212
TransactionIndex: txConfig.TxIndex,
213213
}
@@ -308,7 +308,7 @@ func (k *Keeper) ApplyMessage(ctx sdk.Context, msg core.Message, tracer *tracing
308308
return nil, errorsmod.Wrap(err, "failed to load evm config")
309309
}
310310

311-
txConfig := statedb.NewEmptyTxConfig(common.BytesToHash(ctx.HeaderHash()))
311+
txConfig := statedb.NewEmptyTxConfig()
312312
return k.ApplyMessageWithConfig(ctx, msg, tracer, commit, cfg, txConfig, internal)
313313
}
314314

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

510+
logs := stateDB.GetLogs(uint64(ctx.BlockHeight()), common.BytesToHash(ctx.HeaderHash()), evm.Context.Time)
510511
return &types.MsgEthereumTxResponse{
511512
GasUsed: gasUsed.TruncateInt().Uint64(),
512513
VmError: vmError,
513514
Ret: ret,
514-
Logs: types.NewLogsFromEth(stateDB.Logs()),
515+
Logs: types.NewLogsFromEth(logs),
515516
Hash: txConfig.TxHash.Hex(),
516517
}, nil
517518
}

x/vm/statedb/config.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,27 @@ import (
1010

1111
// TxConfig encapulates the readonly information of current tx for `StateDB`.
1212
type TxConfig struct {
13-
BlockHash common.Hash // hash of current block
14-
TxHash common.Hash // hash of current tx
15-
TxIndex uint // the index of current transaction
16-
LogIndex uint // the index of next log within current block
13+
TxHash common.Hash // hash of current tx
14+
TxIndex uint // the index of current transaction
15+
LogIndex uint // the index of next log within current block
1716
}
1817

1918
// NewTxConfig returns a TxConfig
20-
func NewTxConfig(bhash, thash common.Hash, txIndex, logIndex uint) TxConfig {
19+
func NewTxConfig(thash common.Hash, txIndex, logIndex uint) TxConfig {
2120
return TxConfig{
22-
BlockHash: bhash,
23-
TxHash: thash,
24-
TxIndex: txIndex,
25-
LogIndex: logIndex,
21+
TxHash: thash,
22+
TxIndex: txIndex,
23+
LogIndex: logIndex,
2624
}
2725
}
2826

2927
// NewEmptyTxConfig construct an empty TxConfig,
3028
// used in context where there's no transaction, e.g. `eth_call`/`eth_estimateGas`.
31-
func NewEmptyTxConfig(bhash common.Hash) TxConfig {
29+
func NewEmptyTxConfig() TxConfig {
3230
return TxConfig{
33-
BlockHash: bhash,
34-
TxHash: common.Hash{},
35-
TxIndex: 0,
36-
LogIndex: 0,
31+
TxHash: common.Hash{},
32+
TxIndex: 0,
33+
LogIndex: 0,
3734
}
3835
}
3936

x/vm/statedb/statedb.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,22 @@ func (s *StateDB) AddLog(log *ethtypes.Log) {
219219
s.journal.append(addLogChange{})
220220

221221
log.TxHash = s.txConfig.TxHash
222-
log.BlockHash = s.txConfig.BlockHash
223222
log.TxIndex = s.txConfig.TxIndex
224223
log.Index = s.txConfig.LogIndex + uint(len(s.logs))
225224
s.logs = append(s.logs, log)
226225
}
227226

227+
// GetLogs returns the logs matching the specified transaction hash, and annotates
228+
// them with the given blockNumber and blockHash.
229+
func (s *StateDB) GetLogs(blockNumber uint64, blockHash common.Hash, blockTime uint64) []*ethtypes.Log {
230+
for _, l := range s.logs {
231+
l.BlockNumber = blockNumber
232+
l.BlockHash = blockHash
233+
l.BlockTimestamp = blockTime
234+
}
235+
return s.logs
236+
}
237+
228238
// Logs returns the logs of current transaction.
229239
func (s *StateDB) Logs() []*ethtypes.Log {
230240
return s.logs

x/vm/statedb/statedb_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var (
2525
address2 common.Address = common.BigToAddress(big.NewInt(102))
2626
address3 common.Address = common.BigToAddress(big.NewInt(103))
2727
blockHash common.Hash = common.BigToHash(big.NewInt(9999))
28-
emptyTxConfig statedb.TxConfig = statedb.NewEmptyTxConfig(blockHash)
28+
emptyTxConfig statedb.TxConfig = statedb.NewEmptyTxConfig()
2929
)
3030

3131
type StateDBTestSuite struct {
@@ -587,7 +587,6 @@ func (suite *StateDBTestSuite) TestLog() {
587587
txHash := common.BytesToHash([]byte("tx"))
588588
// use a non-default tx config
589589
txConfig := statedb.NewTxConfig(
590-
blockHash,
591590
txHash,
592591
1, 1,
593592
)
@@ -601,16 +600,17 @@ func (suite *StateDBTestSuite) TestLog() {
601600
})
602601
suite.Require().Equal(1, len(db.Logs()))
603602
expecedLog := &ethtypes.Log{
604-
Address: address,
605-
Topics: []common.Hash{},
606-
Data: data,
607-
BlockNumber: 1,
608-
BlockHash: blockHash,
609-
TxHash: txHash,
610-
TxIndex: 1,
611-
Index: 1,
603+
Address: address,
604+
Topics: []common.Hash{},
605+
Data: data,
606+
BlockNumber: 1,
607+
BlockHash: blockHash,
608+
BlockTimestamp: 1,
609+
TxHash: txHash,
610+
TxIndex: 1,
611+
Index: 1,
612612
}
613-
suite.Require().Equal(expecedLog, db.Logs()[0])
613+
suite.Require().Equal(expecedLog, db.GetLogs(1, blockHash, 1)[0])
614614

615615
db.AddLog(&ethtypes.Log{
616616
Address: address,
@@ -620,7 +620,7 @@ func (suite *StateDBTestSuite) TestLog() {
620620
})
621621
suite.Require().Equal(2, len(db.Logs()))
622622
expecedLog.Index++
623-
suite.Require().Equal(expecedLog, db.Logs()[1])
623+
suite.Require().Equal(expecedLog, db.GetLogs(1, blockHash, 1)[1])
624624
}
625625

626626
func (suite *StateDBTestSuite) TestRefund() {

0 commit comments

Comments
 (0)