Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

fix: eth_getTransactionByBlockNumberAndIndex and eth_getTransactionByBlockHashAndIndex to respect non-zero transaction index #3119

Merged
merged 1 commit into from
May 26, 2022
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
4 changes: 2 additions & 2 deletions src/chains/ethereum/ethereum/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,7 @@ export default class EthereumApi implements Api {
.catch<Block>(_ => null);
if (!block) return null;
const transactions = block.getTransactions();
return transactions[parseInt(Quantity.from(index).toString(), 10)].toJSON(
return transactions[Quantity.from(index).toNumber()].toJSON(
blockchain.common
);
}
Expand Down Expand Up @@ -1289,7 +1289,7 @@ export default class EthereumApi implements Api {
const block = await blockchain.blocks.get(number).catch<Block>(_ => null);
if (!block) return null;
const transactions = block.getTransactions();
return transactions[parseInt(Quantity.from(index).toString(), 10)].toJSON(
return transactions[Quantity.from(index).toNumber()].toJSON(
blockchain.common
);
}
Expand Down
101 changes: 71 additions & 30 deletions src/chains/ethereum/ethereum/tests/api/eth/eth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,59 +429,100 @@ describe("api", () => {
it("eth_getTransactionByBlockNumberAndIndex", async () => {
await provider.send("eth_subscribe", ["newHeads"]);
const gasPrice = await provider.send("eth_gasPrice", []);
const txHash = await provider.send("eth_sendTransaction", [
{
from: accounts[0],
to: accounts[1],
value: "0x1",
gasPrice
}
]);

await provider.send("miner_stop");
const txArgs = {
from: accounts[0],
to: accounts[1],
value: "0x1",
gasPrice
};

const tx1Hash = await provider.send("eth_sendTransaction", [txArgs]);
const tx2Hash = await provider.send("eth_sendTransaction", [txArgs]);
await provider.send("miner_start");

await provider.once("message");

const tx = await provider.send(
const retrievedTx1 = await provider.send(
"eth_getTransactionByBlockNumberAndIndex",
["0x1", "0x0"]
);
assert.strictEqual(
tx.hash,
retrievedTx1.hash,
"0xab338178ffd130f1b7724a687ef20afcc75d44020184f82127ab1bc59f17d7e2",
"Unexpected transaction hash."
);
assert.strictEqual(
tx.hash,
txHash,
retrievedTx1.hash,
tx1Hash,
"eth_getTransactionByBlockNumberAndIndex transaction hash doesn't match tx hash"
);

const retrievedTx2 = await provider.send(
"eth_getTransactionByBlockNumberAndIndex",
["0x1", "0x1"]
);
assert.strictEqual(
retrievedTx2.hash,
"0x32b957c5669ff38b7baa8bfb9c4f84811f70ae3fb44fb2ad63e70ee959c88471",
"Unexpected transaction hash."
);
assert.strictEqual(
retrievedTx2.hash,
tx2Hash,
"eth_getTransactionByBlockNumberAndIndex transaction hash doesn't match tx hash"
);
});

it("eth_getTransactionByBlockHashAndIndex", async () => {
await provider.send("eth_subscribe", ["newHeads"]);
const gasPrice = await provider.send("eth_gasPrice", []);
const txHash = await provider.send("eth_sendTransaction", [
{
from: accounts[0],
to: accounts[1],
value: "0x1",
gasPrice
}
]);
const _message = await provider.once("message");
const block = await provider.send("eth_getBlockByNumber", ["0x1"]);

const tx = await provider.send("eth_getTransactionByBlockHashAndIndex", [
block.hash,
"0x0"
]);
await provider.send("miner_stop");
const txArgs = {
from: accounts[0],
to: accounts[1],
value: "0x1",
gasPrice
};

const tx1Hash = await provider.send("eth_sendTransaction", [txArgs]);
const tx2Hash = await provider.send("eth_sendTransaction", [txArgs]);
await provider.send("miner_start");

await provider.once("message");

const {hash: blockHash} = await provider.send("eth_getBlockByNumber", ["0x1"]);
const retrievedTx1 = await provider.send(
"eth_getTransactionByBlockHashAndIndex",
[blockHash, "0x0"]
);

assert.strictEqual(
tx.hash,
retrievedTx1.hash,
"0xab338178ffd130f1b7724a687ef20afcc75d44020184f82127ab1bc59f17d7e2",
"Unexpected transaction hash."
);
assert.strictEqual(
tx.hash,
txHash,
"eth_getTransactionByBlockNumberAndIndex transaction hash doesn't match tx hash"
retrievedTx1.hash,
tx1Hash,
"eth_getTransactionByBlockHashAndIndex transaction hash doesn't match tx hash"
);

const retrievedTx2 = await provider.send(
"eth_getTransactionByBlockHashAndIndex",
[blockHash, "0x1"]
);
assert.strictEqual(
retrievedTx2.hash,
"0x32b957c5669ff38b7baa8bfb9c4f84811f70ae3fb44fb2ad63e70ee959c88471",
"Unexpected transaction hash."
);
assert.strictEqual(
retrievedTx2.hash,
tx2Hash,
"eth_getTransactionByBlockHashAndIndex transaction hash doesn't match tx hash"
);
});

Expand Down