Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[EVM] Fix timestamp in evm read calls #1486

Merged
merged 5 commits into from
Mar 27, 2024
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
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,7 @@ func (app *App) RegisterTendermintService(clientCtx client.Context) {
}
ctx, err := app.CreateQueryContext(i, false)
if err != nil {
app.Logger().Error("failed to create query context for EVM; using latest context instead")
app.Logger().Error(fmt.Sprintf("failed to create query context for EVM; using latest context instead: %v+", err.Error()))
return app.GetCheckCtx()
}
return ctx
Expand Down
4 changes: 4 additions & 0 deletions contracts/src/EVMCompatibilityTester.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ contract EVMCompatibilityTester {
bool public boolVar;
uint256 public uint256Var;
string public stringVar;
uint256 public lastTimestamp;

// State variable to store the details
MsgDetails public lastMsgDetails;
Expand Down Expand Up @@ -123,6 +124,9 @@ contract EVMCompatibilityTester {
return (blockHash, coinbase, prevrandao, gaslimit, number, timestamp);
}

function setTimestamp() public {
lastTimestamp = block.timestamp;
}

function revertIfFalse(bool value) public {
boolVar = value;
Expand Down
62 changes: 57 additions & 5 deletions contracts/test/EVMCompatabilityTester.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,27 @@ describe("EVM Test", function () {


describe("Block Properties", function () {
it("Should get and print the block properties", async function () {
await delay()
const blockProperties = await evmTester.getBlockProperties();
debug(blockProperties)
it("Should have consistent block properties for a block", async function () {
const currentBlockNumber = await ethers.provider.getBlockNumber();
const iface = new ethers.Interface(["function getBlockProperties() view returns (bytes32 blockHash, address coinbase, uint256 prevrandao, uint256 gaslimit, uint256 number, uint256 timestamp)"]);
const addr = await evmTester.getAddress()
const tx = {
to: addr,
data: iface.encodeFunctionData("getBlockProperties", []),
blockTag: currentBlockNumber-2
};
const result = await ethers.provider.call(tx);

// wait for block to change
while(true){
const bn = await ethers.provider.getBlockNumber();
if(bn !== currentBlockNumber){
break
}
await sleep(100)
}
const result2 = await ethers.provider.call(tx);
expect(result).to.equal(result2)
});
});

Expand Down Expand Up @@ -217,6 +234,41 @@ describe("EVM Test", function () {
expect(await evmTester.uint256Var()).to.equal(12345);
});

it("Should trace a call with timestamp", async function () {
await delay()
const txResponse = await evmTester.setTimestamp();
const receipt = await txResponse.wait(); // Wait for the transaction to be mined

// get the timestamp that was saved off during setTimestamp()
const lastTimestamp = await evmTester.lastTimestamp();

// perform two trace calls with a small delay in between
const trace1 = await hre.network.provider.request({
method: "debug_traceTransaction",
params: [receipt.hash],
});
await sleep(500)
const trace2 = await hre.network.provider.request({
method: "debug_traceTransaction",
params: [receipt.hash],
});

// expect consistency in the trace calls (timestamp should be fixed to block)
expect(JSON.stringify(trace1)).to.equal(JSON.stringify(trace2))

// expect timestamp in the actual trace to match the timestamp seen at the time of invocation
let found = false
for(let log of trace1.structLogs) {
if(log.op === "SSTORE" && log.stack.length >= 3) {
const ts = log.stack[2]
expect(ts).to.equal(lastTimestamp)
found = true
break;
}
}
expect(found).to.be.true;
});


it("Should set the string correctly and emit an event", async function () {
// Call setBoolVar
Expand Down Expand Up @@ -291,7 +343,7 @@ describe("EVM Test", function () {
})

describe("Historical query test", function() {
it.only("Should be able to get historical block data", async function() {
it("Should be able to get historical block data", async function() {
const feeData = await ethers.provider.getFeeData();
const gasPrice = Number(feeData.gasPrice);
const zero = ethers.parseUnits('0', 'ether')
Expand Down
13 changes: 10 additions & 3 deletions evmrpc/simulate.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ func (b *Backend) StateAtTransaction(ctx context.Context, block *ethtypes.Block,
if err != nil {
return nil, vm.BlockContext{}, nil, nil, err
}
// set block context time as of the block time (block time is the time of the CURRENT block)
blockContext.Time = block.Time()

if idx == txIndex {
return msg, *blockContext, statedb, emptyRelease, nil
}
Expand All @@ -307,10 +310,12 @@ func (b *Backend) StateAtBlock(ctx context.Context, block *ethtypes.Block, reexe
return statedb, emptyRelease, nil
}

func (b *Backend) GetEVM(_ context.Context, msg *core.Message, stateDB vm.StateDB, _ *ethtypes.Header, vmConfig *vm.Config, _ *vm.BlockContext) *vm.EVM {
func (b *Backend) GetEVM(_ context.Context, msg *core.Message, stateDB vm.StateDB, _ *ethtypes.Header, vmConfig *vm.Config, blockCtx *vm.BlockContext) *vm.EVM {
txContext := core.NewEVMTxContext(msg)
context, _ := b.keeper.GetVMBlockContext(b.ctxProvider(LatestCtxHeight), core.GasPool(b.RPCGasCap()))
evm := vm.NewEVM(*context, txContext, stateDB, b.ChainConfig(), *vmConfig)
if blockCtx == nil {
blockCtx, _ = b.keeper.GetVMBlockContext(b.ctxProvider(LatestCtxHeight), core.GasPool(b.RPCGasCap()))
}
evm := vm.NewEVM(*blockCtx, txContext, stateDB, b.ChainConfig(), *vmConfig)
if dbImpl, ok := stateDB.(*state.DBImpl); ok {
dbImpl.SetEVM(evm)
}
Expand Down Expand Up @@ -360,8 +365,10 @@ func (b *Backend) getHeader(blockNumber *big.Int) *ethtypes.Header {
}
number := blockNumber.Int64()
block, err := blockByNumber(context.Background(), b.tmClient, &number)
//TODO: what should happen if an err occurs here?
if err == nil {
header.ParentHash = common.BytesToHash(block.BlockID.Hash)
header.Time = uint64(block.Block.Header.Time.Unix())
}
return header
}
Expand Down
Loading