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

perf: use address.buf instead of toBuffer to prevent copy #4395

Draft
wants to merge 3 commits into
base: poc/transaction_simulation
Choose a base branch
from
Draft
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
8 changes: 4 additions & 4 deletions src/chains/ethereum/block/src/runtime-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Common } from "@ethereumjs/common";
export type BlockHeader = {
parentHash: Data;
sha3Uncles: Data;
miner: Data;
miner: Address;
stateRoot: Data;
transactionsRoot: Data;
receiptsRoot: Data;
Expand Down Expand Up @@ -56,7 +56,7 @@ export function makeHeader(
return {
parentHash: Data.from(raw[0], 32),
sha3Uncles: Data.from(raw[1], 32),
miner: Data.from(raw[2], 20),
miner: Address.from(raw[2]),
stateRoot: Data.from(raw[3], 32),
transactionsRoot: Data.from(raw[4], 32),
receiptsRoot: Data.from(raw[5], 32),
Expand Down Expand Up @@ -113,7 +113,7 @@ export class RuntimeBlock {
withdrawalsRoot?: Buffer
) {
this._common = common;
const coinbaseBuffer = coinbase.toBuffer();
const coinbaseBuffer = coinbase.buf;
this.header = {
parentHash: parentHash.toBuffer(),
coinbase: new Address(coinbaseBuffer),
Expand Down Expand Up @@ -183,7 +183,7 @@ export class RuntimeBlock {
tx.raw.length === 9
? tx.raw // legacy transactions don't have their own encoding
: tx.serialized ?? encodeWithPrefix(tx.type.toNumber(), tx.raw);
extraTxs[i] = [tx.from.toBuffer(), tx.hash.toBuffer()];
extraTxs[i] = [tx.from.buf, tx.hash.toBuffer()];
}
const rawBlock: EthereumRawBlock = isEip4895
? [rawHeader, txs, [], []]
Expand Down
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 @@ -1117,7 +1117,7 @@ export default class EthereumApi implements Api {
blockchain.common,
Quantity.from((parentHeader.number.toBigInt() || 0n) + 1n),
parentHeader.parentHash,
new Address(parentHeader.miner.toBuffer()),
new Address(parentHeader.miner.buf),
tx.gas,
parentHeader.gasUsed,
parentHeader.timestamp,
Expand Down Expand Up @@ -1971,7 +1971,7 @@ export default class EthereumApi implements Api {
paddedPosBuff = posBuff.slice(-32);
}

const addressBuf = Address.from(address).toBuffer();
const addressBuf = Address.toBuffer(address);
const addressData = await trie.get(addressBuf);
// An address's stateRoot is stored in the 3rd rlp entry
const addressStateRoot = decode<EthereumRawAccount>(addressData)[2];
Expand Down
6 changes: 3 additions & 3 deletions src/chains/ethereum/ethereum/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -734,7 +734,7 @@ export default class Blockchain extends Emittery<BlockchainTypedEvents> {
#commitAccounts = (accounts: Account[]) => {
return Promise.all<void>(
accounts.map(account =>
this.trie.put(account.address.toBuffer(), account.serialize())
this.trie.put(account.address.buf, account.serialize())
)
);
};
Expand Down Expand Up @@ -1124,7 +1124,7 @@ export default class Blockchain extends Emittery<BlockchainTypedEvents> {
// subtract out the transaction's base fee from the gas limit before
// simulating the tx, because `runCall` doesn't account for raw gas costs.
const hasToAddress = transaction.to != null;
const to = hasToAddress ? new Address(transaction.to.toBuffer()) : null;
const to = hasToAddress ? new Address(transaction.to.buf) : null;

//todo: getCommonForBlockNumber doesn't presently respect shanghai, so we just assume it's the same common as the fork
// this won't work as expected if simulating on blocks before shanghai.
Expand Down Expand Up @@ -1209,7 +1209,7 @@ export default class Blockchain extends Emittery<BlockchainTypedEvents> {
}
});

const caller = transaction.from.toBuffer();
const caller = transaction.from.buf;
const callerAddress = new Address(caller);

if (common.isActivatedEIP(2929)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class AccountManager {
trieCopy.setContext(stateRoot.toBuffer(), null, number);

// get the account from the trie
return await trieCopy.get(address.toBuffer());
return await trieCopy.get(address.buf);
}

public async getNonce(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default class BlockManager extends Manager<Block> {
const header: EthereumRawBlockHeader = [
Data.toBuffer(json.parentHash),
Data.toBuffer(json.sha3Uncles),
Address.from(json.miner).toBuffer(),
Address.toBuffer(json.miner),
Data.toBuffer(json.stateRoot),
Data.toBuffer(json.transactionsRoot),
Data.toBuffer(json.receiptsRoot),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default class TransactionReceiptManager extends Manager<InternalTransacti
const cumulativeGasUsed = Quantity.toBuffer(res.cumulativeGasUsed);
const logsBloom = Data.toBuffer(res.logsBloom, 256);
const logs = res.logs.map(log => [
Address.from(log.address).toBuffer(),
Address.toBuffer(log.address),
log.topics.map(topic => Data.toBuffer(topic)),
Array.isArray(log.data)
? log.data.map(data => Data.toBuffer(data))
Expand All @@ -38,7 +38,7 @@ export default class TransactionReceiptManager extends Manager<InternalTransacti
const contractAddress =
res.contractAddress == null
? BUFFER_EMPTY
: Address.from(res.contractAddress).toBuffer();
: Address.toBuffer(res.contractAddress);
return InternalTransactionReceipt.fromValues(
status,
cumulativeGasUsed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function parseFilterDetails(
// `filter.address` may be a single address or an array
const addresses = filter.address
? (Array.isArray(filter.address) ? filter.address : [filter.address]).map(
a => Address.from(a.toLowerCase()).toBuffer()
a => Address.toBuffer(a.toLowerCase())
)
: [];
const topics = filter.topics ? filter.topics : [];
Expand Down
2 changes: 1 addition & 1 deletion src/chains/ethereum/ethereum/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ export default class Wallet {

public createFakePrivateKey(address: string) {
let fakePrivateKey: Buffer;
const addressBuf = Address.from(address).toBuffer();
const addressBuf = Address.toBuffer(address);
if (addressBuf.equals(ACCOUNT_ZERO)) {
// allow signing with the 0x0 address...
// always sign with the same fake key, a 31 `0`s followed by a single
Expand Down
3 changes: 1 addition & 2 deletions src/chains/ethereum/transaction/src/base-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ export class BaseTransaction {
}

public calculateIntrinsicGas() {
const hasToAddress =
this.to != null && !this.to.toBuffer().equals(BUFFER_EMPTY);
const hasToAddress = this.to != null && !this.to.buf.equals(BUFFER_EMPTY);
return calculateIntrinsicGas(this.data, hasToAddress, this.common);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ export class EIP1559FeeMarketTransaction extends RuntimeTransaction {
this.maxPriorityFeePerGas.toBuffer(),
this.maxFeePerGas.toBuffer(),
this.gas.toBuffer(),
this.to ? this.to.toBuffer() : BUFFER_EMPTY,
this.to ? this.to.buf : BUFFER_EMPTY,
this.value.toBuffer(),
this.data.toBuffer(),
this.accessList,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export class EIP2930AccessListTransaction extends RuntimeTransaction {
this.nonce.toBuffer(),
this.gasPrice.toBuffer(),
this.gas.toBuffer(),
this.to ? this.to.toBuffer() : BUFFER_EMPTY,
this.to ? this.to.buf : BUFFER_EMPTY,
this.value.toBuffer(),
this.data.toBuffer(),
this.accessList,
Expand Down
2 changes: 1 addition & 1 deletion src/chains/ethereum/transaction/src/legacy-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ export class LegacyTransaction extends RuntimeTransaction {
this.nonce.toBuffer(),
this.gasPrice.toBuffer(),
this.gas.toBuffer(),
this.to ? this.to.toBuffer() : BUFFER_EMPTY,
this.to ? this.to.buf : BUFFER_EMPTY,
this.value.toBuffer(),
this.data.toBuffer(),
v,
Expand Down
4 changes: 2 additions & 2 deletions src/chains/ethereum/transaction/src/runtime-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export abstract class RuntimeTransaction extends BaseTransaction {
// block it twice for each block save step.
legacy ? this.raw : ([this.type.toBuffer(), ...this.raw] as any),
[
this.from.toBuffer(),
this.from.buf,
this.hash.toBuffer(),
blockHash.toBuffer(),
blockNumber.toBuffer(),
Expand Down Expand Up @@ -201,7 +201,7 @@ export abstract class RuntimeTransaction extends BaseTransaction {
// and `s` values, make sure the `from` address matches
if (data.from !== null) {
const userFrom = toValidLengthAddress(data.from, "from");
if (!from.toBuffer().equals(userFrom.toBuffer())) {
if (!from.buf.equals(userFrom.buf)) {
throw new Error(
"Transaction is signed and contains a `from` field, but the signature doesn't match."
);
Expand Down
2 changes: 1 addition & 1 deletion src/chains/ethereum/utils/src/things/blocklogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class BlockLogs {
const transactionHash = Data.from(log.transactionHash, 32);
const transactionIndex = Quantity.from(log.transactionIndex);
blockLogs.append(transactionIndex, transactionHash, [
address.toBuffer(), // `address`
address.buf,
topics,
data
]);
Expand Down