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

fix: assume unknown tx type is TransactionType.Legacy #3523

Merged
merged 2 commits into from
Aug 17, 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
10 changes: 1 addition & 9 deletions src/chains/ethereum/transaction/src/transaction-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ function assertValidTransactionSValue(common: Common, tx: TypedTransaction) {
}
}

const UNTYPED_TX_START_BYTE = 0xc0; // all txs with first byte >= 0xc0 are untyped

export enum TransactionType {
Legacy = 0x0,
EIP2930AccessList = 0x1,
Expand Down Expand Up @@ -265,14 +263,8 @@ export class TransactionFactory {
type === TransactionType.EIP2930AccessList
) {
return type;
} else if (
type >= UNTYPED_TX_START_BYTE ||
type === TransactionType.Legacy ||
type === undefined
) {
return TransactionType.Legacy;
} else {
throw new Error(`Invalid transaction type: ${type}`);
return TransactionType.Legacy;
}
}

Expand Down
55 changes: 38 additions & 17 deletions src/chains/ethereum/transaction/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ describe("@ganache/ethereum-transaction", async () => {
type: "0x0",
gasPrice: "0xffff"
};

const UNTYPED_TX_START_BYTE = 0xc0; // all txs with first byte >= 0xc0 are untyped

const rawLegacyStrTx =
"0xf8618082ffff80945a17650be84f28ed583e93e6ed0c99b1d1fc1b348080820a95a0d9c2d3cb65d7079f528d28d782fe752ed698381481f7b91790e067ea335c1dc0a0731131778ae061aa29567cc6b36fe3528092b687fb68c3f529493fca200c711d";
Expand Down Expand Up @@ -144,6 +146,42 @@ describe("@ganache/ethereum-transaction", async () => {
);
assert.strictEqual(txFromRpc.type.toString(), "0x0");
});
it("assumes legacy transaction if type unknown", () => {
const unknownTypeTx: Transaction = {
from: from,
to: to,
type: "0x55",
gasPrice: "0xffff"
};
txFromRpc = <LegacyTransaction>(
TransactionFactory.fromRpc(unknownTypeTx, common)
);
assert.strictEqual(txFromRpc.type.toString(), "0x0");
});
it(`assumes legacy transaction if type ${UNTYPED_TX_START_BYTE}`, () => {
const unknownTypeTx = {
from: from,
to: to,
type: "0x" + UNTYPED_TX_START_BYTE.toString(16),
gasPrice: "0xffff"
};
txFromRpc = <LegacyTransaction>(
TransactionFactory.fromRpc(unknownTypeTx, common)
);
assert.strictEqual(txFromRpc.type.toString(), "0x0");
});
it(`assumes legacy transaction if type > ${UNTYPED_TX_START_BYTE}`, () => {
const unknownTypeTx = {
from: from,
to: to,
type: "0x" + (UNTYPED_TX_START_BYTE + 1).toString(16),
gasPrice: "0xffff"
};
txFromRpc = <LegacyTransaction>(
TransactionFactory.fromRpc(unknownTypeTx, common)
);
assert.strictEqual(txFromRpc.type.toString(), "0x0");
davidmurdoch marked this conversation as resolved.
Show resolved Hide resolved
});
it("generates legacy transactions from rpc data", async () => {
txFromRpc = <LegacyTransaction>(
TransactionFactory.fromRpc(typedLegacyTx, common)
Expand Down Expand Up @@ -557,17 +595,6 @@ describe("@ganache/ethereum-transaction", async () => {
});

describe("Error and helper cases", () => {
it("does not allow unsupported tx types from rpc data", async () => {
const rpc: Transaction = {
from: from,
to: to,
type: "0x55",
gasPrice: "0xffff"
};
assert.throws(() => {
TransactionFactory.fromRpc(rpc, common);
});
});
it("does not allow unsupported tx types from raw buffer data", async () => {
const db = [
Buffer.from("0x55"),
Expand All @@ -590,12 +617,6 @@ describe("@ganache/ethereum-transaction", async () => {
);
});
});
it("does not allow unsupported tx types from raw string data", async () => {
const str: string = "0x55";
assert.throws(() => {
TransactionFactory.fromString(str, common);
});
});
it("gets tx type from raw data", async () => {
const db = [
BUFFER_EMPTY,
Expand Down