Skip to content

Commit

Permalink
Fix/null blockhash (#975)
Browse files Browse the repository at this point in the history
* feat: write zero block hash in accepted on l2 sn txs

* add one log

* fix comments

* fix format hex

* fix comments
  • Loading branch information
Eikix authored Apr 16, 2024
1 parent bf6a8c9 commit d382202
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 17 deletions.
8 changes: 5 additions & 3 deletions indexer/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Utils
import { padString, toHexString } from "./utils/hex.ts";
import { NULL_BLOCK_HASH, padString, toHexString } from "./utils/hex.ts";

// Types
import { toEthTx, toTypedEthTx } from "./types/transaction.ts";
Expand Down Expand Up @@ -95,8 +95,6 @@ const isKakarotTransaction = (transaction: Transaction) => {
return true;
};

const NULL_BLOCK_HASH = padString("0x", 32);

export default async function transform({
header,
events,
Expand All @@ -120,6 +118,10 @@ export default async function transform({
(events ?? []).map(async ({ transaction, receipt, event }) => {
// Can be false if the transaction is not related to a specific instance of the Kakarot contract.
// This is typically the case if there are multiple Kakarot contracts on the same chain.
console.log(
"🔍 Processing transaction with Starknet hash: ",
transaction.meta.hash,
);
const isKakarotTx = isKakarotTransaction(transaction);
if (!isKakarotTx) {
return null;
Expand Down
4 changes: 2 additions & 2 deletions indexer/src/types/log.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Utils
import { padBigint } from "../utils/hex.ts";
import { NULL_BLOCK_HASH, padBigint } from "../utils/hex.ts";

// Starknet
import { Event, hash } from "../deps.ts";
Expand Down Expand Up @@ -83,7 +83,7 @@ export function toEthLog({
logIndex: null,
transactionIndex: bigIntToHex(BigInt(transaction.transactionIndex ?? 0)),
transactionHash: transaction.hash,
blockHash: isPendingBlock ? null : blockHash,
blockHash: isPendingBlock ? NULL_BLOCK_HASH : blockHash,
blockNumber,
address,
data: `0x${paddedData}`,
Expand Down
4 changes: 2 additions & 2 deletions indexer/src/types/receipt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Utils
import { padBytes } from "../utils/hex.ts";
import { NULL_BLOCK_HASH, padBytes } from "../utils/hex.ts";

// Types
import { fromJsonRpcLog, JsonRpcLog } from "./log.ts";
Expand Down Expand Up @@ -68,7 +68,7 @@ export function toEthReceipt({
return {
transactionHash: transaction.hash,
transactionIndex: bigIntToHex(BigInt(transaction.transactionIndex ?? 0)),
blockHash: isPendingBlock ? null : blockHash,
blockHash: isPendingBlock ? NULL_BLOCK_HASH : blockHash,
blockNumber,
from: transaction.from,
to: transaction.to,
Expand Down
12 changes: 7 additions & 5 deletions indexer/src/types/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,11 @@ export function toEthTx({
}
// If the transaction is a legacy, we can calculate it from the v value.
// v = 35 + 2 * chainId + yParity -> chainId = (v - 35) / 2
const chainId = isLegacyTx(transaction) &&
transaction.supports(Capability.EIP155ReplayProtection)
? bigIntToHex((BigInt(txJSON.v) - 35n) / 2n)
: txJSON.chainId;
const chainId =
isLegacyTx(transaction) &&
transaction.supports(Capability.EIP155ReplayProtection)
? bigIntToHex((BigInt(txJSON.v) - 35n) / 2n)
: txJSON.chainId;

const result: JsonRpcTx & { yParity?: string } = {
blockHash: isPendingBlock ? null : blockHash,
Expand All @@ -93,7 +94,7 @@ export function toEthTx({
input: txJSON.data!,
nonce: txJSON.nonce!,
to: transaction.to?.toString() ?? null,
transactionIndex: isPendingBlock ? null : padBigint(BigInt(index ?? 0), 32),
transactionIndex: isPendingBlock ? null : padBigint(BigInt(index ?? 0), 8),
value: txJSON.value!,
v: txJSON.v,
r: txJSON.r,
Expand Down Expand Up @@ -166,6 +167,7 @@ export function toTypedEthTx({
throw e;
}
// TODO: Ping alert webhooks
console.error(e);
return null;
}
}
Expand Down
7 changes: 4 additions & 3 deletions indexer/src/utils/hex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { bigIntToHex, bytesToHex } from "../deps.ts";
import { PrefixedHexString, stripHexPrefix } from "../deps.ts";

export const NULL_BLOCK_HASH = padString("0x", 32);

/**
* @param hex - A decimal string.
*/
Expand All @@ -20,7 +22,7 @@ export function padString(
hex: PrefixedHexString | undefined,
length: number,
): PrefixedHexString {
return "0x" + (stripHexPrefix(hex ?? "0x").padStart(2 * length, "0"));
return "0x" + stripHexPrefix(hex ?? "0x").padStart(2 * length, "0");
}

/**
Expand All @@ -31,8 +33,7 @@ export function padBigint(
b: bigint | undefined,
length: number,
): PrefixedHexString {
return "0x" +
(stripHexPrefix(bigIntToHex(b ?? 0n)).padStart(2 * length, "0"));
return "0x" + stripHexPrefix(bigIntToHex(b ?? 0n)).padStart(2 * length, "0");
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/eth_provider/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ where
let mut filter = into_filter("tx.blockHash", &hash, HASH_PADDING);
let index: usize = index.into();

filter.insert("tx.transactionIndex", format_hex(index, 64));
filter.insert("tx.transactionIndex", format_hex(index, U64_PADDING));
Ok(self.database.get_one::<StoredTransaction>(filter, None).await?.map(Into::into))
}

Expand All @@ -289,7 +289,7 @@ where
let mut filter = into_filter("tx.blockNumber", &block_number, U64_PADDING);
let index: usize = index.into();

filter.insert("tx.transactionIndex", format_hex(index, 64));
filter.insert("tx.transactionIndex", format_hex(index, U64_PADDING));
Ok(self.database.get_one::<StoredTransaction>(filter, None).await?.map(Into::into))
}

Expand Down

0 comments on commit d382202

Please sign in to comment.