Skip to content

Commit

Permalink
fix: logs queries after last mined block should return nothing (#1224)
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhani-cw authored Jun 24, 2024
1 parent 4e8ddb9 commit de13d8e
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 133 deletions.
28 changes: 12 additions & 16 deletions e2e/cloudwalk-contracts/integration/test/relayer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,12 @@ describe("Relayer integration test", function () {
const amount = Math.floor(Math.random() * 10) + 1;

try {
const tx = await brlcToken
.connect(sender)
.transfer(receiver.address, amount, {
gasPrice: 0,
gasLimit: GAS_LIMIT_OVERRIDE,
type: 0,
nonce: nonces[senderIndex],
});
const tx = await brlcToken.connect(sender).transfer(receiver.address, amount, {
gasPrice: 0,
gasLimit: GAS_LIMIT_OVERRIDE,
type: 0,
nonce: nonces[senderIndex],
});
txHashList.push(tx.hash);
} catch (error) {}

Expand Down Expand Up @@ -210,14 +208,12 @@ describe("Relayer integration test", function () {
let sender = wallets[i % 2];
let receiver = wallets[(i + 1) % 2];

const tx = await brlcToken
.connect(sender)
.transfer(receiver.address, 10, {
gasPrice: 0,
gasLimit: GAS_LIMIT_OVERRIDE,
type: 0,
nonce: nonces[i % 2],
});
const tx = await brlcToken.connect(sender).transfer(receiver.address, 10, {
gasPrice: 0,
gasLimit: GAS_LIMIT_OVERRIDE,
type: 0,
nonce: nonces[i % 2],
});

txHashList.push(tx.transactionHash);

Expand Down
25 changes: 24 additions & 1 deletion e2e/test/automine/e2e-json-rpc.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import { expect } from "chai";
import { Block, Bytes } from "web3-types";
import { Block, Bytes, TransactionReceipt } from "web3-types";

import { ALICE, BOB } from "../helpers/account";
import { isStratus } from "../helpers/network";
import {
CHAIN_ID,
CHAIN_ID_DEC,
ETHERJS,
HASH_ZERO,
HEX_PATTERN,
ONE,
TEST_BALANCE,
ZERO,
deployTestContractBalances,
send,
sendAndGetError,
sendEvmMine,
sendExpect,
sendReset,
subscribeAndGetEvent,
subscribeAndGetEventWithContract,
toHex,
} from "../helpers/rpc";

describe("JSON-RPC", () => {
Expand Down Expand Up @@ -123,6 +126,26 @@ describe("JSON-RPC", () => {
});
});

describe("Logs", () => {
describe("eth_getLogs", () => {
it("return no logs for queries after the last mined block", async () => {
// mine a test transaction
const contract = await deployTestContractBalances();
const txResponse = await contract.connect(ALICE.signer()).add(ALICE.address, 10);
const txReceipt = await ETHERJS.getTransactionReceipt(txResponse.hash);
expect(txReceipt).exist;
expect(txReceipt?.status).eq(1);

// check log queries starting at the last mined block and starting after it
const txBlockNumber = txReceipt?.blockNumber ?? 0;
const filter = { address: contract.target };
expect(await send("eth_getLogs", [{ ...filter, fromBlock: toHex(txBlockNumber) }])).length(1); // last mined block
expect(await send("eth_getLogs", [{ ...filter, fromBlock: toHex(txBlockNumber + 1) }])).length(0); // 1 after mined block
expect(await send("eth_getLogs", [{ ...filter, fromBlock: toHex(txBlockNumber + 2) }])).length(0); // 2 after mined block
});
});
});

describe("Evm", () => {
async function latest(): Promise<{ timestamp: number; block_number: number }> {
const block = await send("eth_getBlockByNumber", ["latest", false]);
Expand Down
2 changes: 1 addition & 1 deletion e2e/test/automine/e2e-tx-serial-contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ describe("Transaction: serial TestContractBalances", () => {
(await sendExpect("eth_getLogs", [{ ...filter, fromBlock: toHex(_block + 1) }])).length(3);
(await sendExpect("eth_getLogs", [{ ...filter, fromBlock: toHex(_block + 2) }])).length(2);
(await sendExpect("eth_getLogs", [{ ...filter, fromBlock: toHex(_block + 3) }])).length(1);
(await sendExpect("eth_getLogs", [{ ...filter, fromBlock: toHex(_block + 4) }])).length(1);
(await sendExpect("eth_getLogs", [{ ...filter, fromBlock: toHex(_block + 4) }])).length(0);

// filter topics
(await sendExpect("eth_getLogs", [{ ...filter, topics: [CONTRACT_TOPIC_ADD] }])).length(2);
Expand Down
30 changes: 0 additions & 30 deletions e2e/test/issues/automine/e2e-abm-logs.ts

This file was deleted.

32 changes: 0 additions & 32 deletions e2e/test/issues/external/e2e-ebm-logs.ts

This file was deleted.

39 changes: 0 additions & 39 deletions e2e/test/issues/interval/e2e-ibm-logs.ts

This file was deleted.

15 changes: 9 additions & 6 deletions src/eth/primitives/block_selection.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use crate::eth::primitives::BlockNumber;
use crate::eth::primitives::Hash;

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, serde::Serialize)]
pub enum BlockSelection {
/// Retrieve the most recent block.
#[default]
Latest,

/// Retrieve the most early block.
Expand All @@ -16,11 +17,9 @@ pub enum BlockSelection {
Number(BlockNumber),
}

impl Default for BlockSelection {
fn default() -> Self {
Self::Latest
}
}
// -----------------------------------------------------------------------------
// Serialization / Deserilization
// -----------------------------------------------------------------------------

impl<'de> serde::Deserialize<'de> for BlockSelection {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
Expand Down Expand Up @@ -48,6 +47,10 @@ impl<'de> serde::Deserialize<'de> for BlockSelection {
}
}

// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------

#[cfg(test)]
mod tests {
use serde_json::json;
Expand Down
9 changes: 1 addition & 8 deletions src/eth/storage/stratus_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,14 +523,7 @@ impl StratusStorage {
pub fn translate_to_point_in_time(&self, block_selection: &BlockSelection) -> anyhow::Result<StoragePointInTime> {
match block_selection {
BlockSelection::Latest => Ok(StoragePointInTime::Present),
BlockSelection::Number(number) => {
let current_block = self.perm.read_mined_block_number()?;
if number <= &current_block {
Ok(StoragePointInTime::Past(*number))
} else {
Ok(StoragePointInTime::Past(current_block))
}
}
BlockSelection::Number(number) => Ok(StoragePointInTime::Past(*number)),
BlockSelection::Earliest | BlockSelection::Hash(_) => match self.read_block(block_selection)? {
Some(block) => Ok(StoragePointInTime::Past(block.header.number)),
None => Err(anyhow!(
Expand Down

0 comments on commit de13d8e

Please sign in to comment.