Skip to content

Commit

Permalink
Merge pull request #19 from jsign/jsign-txn-rename
Browse files Browse the repository at this point in the history
Rename Txn* to Tx*
  • Loading branch information
jsign authored Feb 7, 2024
2 parents 2a4bcb1 + 44ef147 commit 5ee25fb
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 211 deletions.
36 changes: 18 additions & 18 deletions src/blockchain/blockchain.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const StateDB = @import("../state/state.zig").StateDB;
const Hash32 = types.Hash32;
const Bytes32 = types.Bytes32;
const Address = types.Address;
const TxnSigner = signer.TxnSigner;
const TxSigner = signer.TxSigner;
const VM = vm.VM;
const Keccak256 = std.crypto.hash.sha3.Keccak256;

Expand All @@ -32,7 +32,7 @@ pub const Blockchain = struct {
state: *StateDB,
prev_block: BlockHeader,
last_256_blocks_hashes: [256]Hash32, // ordered in asc order
txn_signer: TxnSigner,
tx_signer: TxSigner,

// init initializes a blockchain.
// The caller **does not** transfer ownership of prev_block.
Expand All @@ -49,7 +49,7 @@ pub const Blockchain = struct {
.state = state,
.prev_block = try prev_block.clone(allocator),
.last_256_blocks_hashes = last_256_blocks_hashes,
.txn_signer = try signer.TxnSigner.init(@intFromEnum(chain_id)),
.tx_signer = try signer.TxSigner.init(@intFromEnum(chain_id)),
};
}

Expand All @@ -63,7 +63,7 @@ pub const Blockchain = struct {
const allocator = arena.allocator();

// Execute block.
var result = try applyBody(allocator, self, self.state, block, self.txn_signer);
var result = try applyBody(allocator, self, self.state, block, self.tx_signer);

// Post execution checks.
if (result.gas_used != block.header.gas_used)
Expand Down Expand Up @@ -153,19 +153,19 @@ pub const Blockchain = struct {
withdrawals_root: Hash32,
};

fn applyBody(allocator: Allocator, chain: *Blockchain, state: *StateDB, block: Block, txn_signer: TxnSigner) !BlockExecutionResult {
fn applyBody(allocator: Allocator, chain: *Blockchain, state: *StateDB, block: Block, tx_signer: TxSigner) !BlockExecutionResult {
var gas_available = block.header.gas_limit;
for (block.transactions) |tx| {
const txn_info = try checkTransaction(allocator, tx, block.header.base_fee_per_gas, gas_available, txn_signer);
const tx_info = try checkTransaction(allocator, tx, block.header.base_fee_per_gas, gas_available, tx_signer);

const env: Environment = .{
.origin = txn_info.sender_address,
.origin = tx_info.sender_address,
.block_hashes = chain.last_256_blocks_hashes,
.coinbase = block.header.fee_recipient,
.number = block.header.block_number,
.gas_limit = block.header.gas_limit,
.base_fee_per_gas = block.header.base_fee_per_gas,
.gas_price = txn_info.effective_gas_price,
.gas_price = tx_info.effective_gas_price,
.time = block.header.timestamp,
.prev_randao = block.header.prev_randao,
.state = state,
Expand Down Expand Up @@ -195,14 +195,14 @@ pub const Blockchain = struct {
};
}

fn checkTransaction(allocator: Allocator, tx: transaction.Txn, base_fee_per_gas: u256, gas_available: u64, txn_signer: TxnSigner) !struct { sender_address: Address, effective_gas_price: u256 } {
fn checkTransaction(allocator: Allocator, tx: transaction.Tx, base_fee_per_gas: u256, gas_available: u64, tx_signer: TxSigner) !struct { sender_address: Address, effective_gas_price: u256 } {
if (tx.getGasLimit() > gas_available)
return error.InsufficientGas;

const sender_address = try txn_signer.get_sender(allocator, tx);
const sender_address = try tx_signer.get_sender(allocator, tx);

const effective_gas_price = switch (tx) {
.FeeMarketTxn => |fm_tx| blk: {
.FeeMarketTx => |fm_tx| blk: {
if (fm_tx.max_fee_per_gas < fm_tx.max_priority_fee_per_gas)
return error.InvalidMaxFeePerGas;
if (fm_tx.max_fee_per_gas < base_fee_per_gas)
Expand All @@ -211,7 +211,7 @@ pub const Blockchain = struct {
const priority_fee_per_gas = @min(fm_tx.max_priority_fee_per_gas, fm_tx.max_fee_per_gas - base_fee_per_gas);
break :blk priority_fee_per_gas + base_fee_per_gas;
},
.LegacyTxn, .AccessListTxn => blk: {
.LegacyTx, .AccessListTx => blk: {
if (tx.getGasPrice() < base_fee_per_gas)
return error.GasPriceLowerThanBaseFee;
break :blk tx.getGasPrice();
Expand All @@ -220,7 +220,7 @@ pub const Blockchain = struct {
return .{ .sender_address = sender_address, .effective_gas_price = effective_gas_price };
}

fn processTransaction(allocator: Allocator, env: Environment, tx: transaction.Txn) !struct { gas_used: u64 } {
fn processTransaction(allocator: Allocator, env: Environment, tx: transaction.Tx) !struct { gas_used: u64 } {
if (!validateTransaction(tx))
return error.InvalidTransaction;

Expand All @@ -230,7 +230,7 @@ pub const Blockchain = struct {

var sender_account = env.state.getAccount(sender);
if (sender_account.nonce != tx.getNonce())
return error.InvalidTxnNonce;
return error.InvalidTxNonce;
if (sender_account.balance < gas_fee + tx.getValue())
return error.NotEnoughBalance;
if (sender_account.code.len > 0)
Expand All @@ -245,7 +245,7 @@ pub const Blockchain = struct {

try env.state.putAccessedAccount(env.coinbase);
switch (tx) {
.LegacyTxn => {},
.LegacyTx => {},
inline else => |al_tx| {
for (al_tx.access_list) |al| {
try env.state.putAccessedAccount(al.address);
Expand Down Expand Up @@ -300,7 +300,7 @@ pub const Blockchain = struct {
return .{ .gas_used = total_gas_used };
}

fn validateTransaction(tx: transaction.Txn) bool {
fn validateTransaction(tx: transaction.Tx) bool {
if (calculateIntrinsicCost(tx) > tx.getGasLimit())
return false;
if (tx.getNonce() >= (2 << 64) - 1)
Expand All @@ -310,7 +310,7 @@ pub const Blockchain = struct {
return true;
}

fn calculateIntrinsicCost(tx: transaction.Txn) u64 {
fn calculateIntrinsicCost(tx: transaction.Tx) u64 {
var data_cost: u64 = 0;
const data = tx.getData();
for (data) |byte| {
Expand All @@ -320,7 +320,7 @@ pub const Blockchain = struct {
const create_cost = if (tx.getTo() == null) params.tx_create_cost + initCodeCost(data.len) else 0;

const access_list_cost = switch (tx) {
.LegacyTxn => 0,
.LegacyTx => 0,
inline else => |al_tx| blk: {
var sum: u64 = 0;
for (al_tx.access_list) |al| {
Expand Down
6 changes: 3 additions & 3 deletions src/blockchain/vm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ pub const VM = struct {
.input_data = msg.data.ptr,
.input_size = msg.data.len,
.value = blk: {
var txn_value: [32]u8 = undefined;
std.mem.writeIntSliceBig(u256, &txn_value, msg.value);
break :blk .{ .bytes = txn_value };
var tx_value: [32]u8 = undefined;
std.mem.writeIntSliceBig(u256, &tx_value, msg.value);
break :blk .{ .bytes = tx_value };
},
.create2_salt = undefined, // EVMC docs: field only mandatory for CREATE2 kind which doesn't apply at depth 0.
.code_address = toEVMCAddress(msg.code_address),
Expand Down
10 changes: 5 additions & 5 deletions src/engine_api/engine_api.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const types = @import("../types/types.zig");
const common = @import("../common/common.zig");
const Allocator = std.mem.Allocator;
const Withdrawal = types.Withdrawal;
const Txn = types.Txn;
const Tx = types.Tx;
const ExecutionPayload = execution_payload.ExecutionPayload;

pub const execution_payload = @import("execution_payload.zig");
Expand Down Expand Up @@ -34,11 +34,11 @@ const AllPossibleExecutionParams = struct {
transactions: [][]const u8,

pub fn to_execution_payload(self: *const AllPossibleExecutionParams, allocator: Allocator) !ExecutionPayload {
var txns: []Txn = &[0]Txn{};
var txs: []Tx = &[0]Tx{};
if (self.transactions.len > 0) {
txns = try allocator.alloc(Txn, self.transactions.len);
txs = try allocator.alloc(Tx, self.transactions.len);
for (self.transactions, 0..) |tx, i| {
txns[i] = try Txn.decode(allocator, tx);
txs[i] = try Tx.decode(allocator, tx);
}
}

Expand All @@ -56,7 +56,7 @@ const AllPossibleExecutionParams = struct {
.gasUsed = try common.prefixedhex2u64(self.gasUsed),
.timestamp = try common.prefixedhex2u64(self.timestamp),
.baseFeePerGas = try common.prefixedhex2u64(self.baseFeePerGas),
.transactions = txns,
.transactions = txs,
.withdrawals = &[0]Withdrawal{},
.blobGasUsed = null,
.excessBlobGas = null,
Expand Down
4 changes: 2 additions & 2 deletions src/engine_api/execution_payload.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const types = @import("../types/types.zig");
const Allocator = std.mem.Allocator;
const BlockHeader = types.BlockHeader;
const Withdrawal = types.Withdrawal;
const Txn = types.Txn;
const Tx = types.Tx;

pub const ExecutionPayload = struct {
parentHash: types.Hash32,
Expand All @@ -19,7 +19,7 @@ pub const ExecutionPayload = struct {
extraData: []const u8,
baseFeePerGas: u256,
blockHash: types.Hash32,
transactions: []Txn,
transactions: []Tx,

withdrawals: []types.Withdrawal,
blobGasUsed: ?u64,
Expand Down
16 changes: 8 additions & 8 deletions src/exec-spec-tests/execspectests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ const blockchain = @import("../blockchain/blockchain.zig");
const vm = @import("../blockchain/vm.zig");
const ecdsa = @import("../crypto/crypto.zig").ecdsa;
const state = @import("../state/state.zig");
const TxnSigner = @import("../signer/signer.zig").TxnSigner;
const TxSigner = @import("../signer/signer.zig").TxSigner;
const Allocator = std.mem.Allocator;
const Address = types.Address;
const Block = types.Block;
const BlockHeader = types.BlockHeader;
const Txn = types.Txn;
const Tx = types.Tx;
const Hash32 = types.Hash32;
const Bytes32 = types.Bytes32;
const VM = vm.VM;
Expand Down Expand Up @@ -153,11 +153,11 @@ pub const TransactionHex = struct {
data: HexString,
gasLimit: HexString,

pub fn toTx(self: TransactionHex, allocator: Allocator, txn_signer: TxnSigner) !Txn {
pub fn toTx(self: TransactionHex, allocator: Allocator, tx_signer: TxSigner) !Tx {
const type_ = try std.fmt.parseInt(u8, self.type[2..], 16);
std.debug.assert(type_ == 0);
const chain_id = try std.fmt.parseInt(u64, self.chainId[2..], 16);
if (chain_id != txn_signer.chain_id) {
if (chain_id != tx_signer.chain_id) {
return error.InvalidChainId;
}
const nonce = try std.fmt.parseUnsigned(u64, self.nonce[2..], 16);
Expand All @@ -172,13 +172,13 @@ pub const TransactionHex = struct {
_ = try std.fmt.hexToBytes(data, self.data[2..]);
const gas_limit = try std.fmt.parseUnsigned(u64, self.gasLimit[2..], 16);

var txn = Txn.initLegacyTxn(nonce, gas_price, value, to, data, gas_limit);
var tx = Tx.initLegacyTx(nonce, gas_price, value, to, data, gas_limit);
var privkey: ecdsa.PrivateKey = undefined;
_ = try std.fmt.hexToBytes(&privkey, self.secretKey[2..]);
const sig = try txn_signer.sign(allocator, txn, privkey);
txn.setSignature(sig.v, sig.r, sig.s);
const sig = try tx_signer.sign(allocator, tx, privkey);
tx.setSignature(sig.v, sig.r, sig.s);

return txn;
return tx;
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const Address = types.Address;
const VM = @import("blockchain/vm.zig").VM;
const StateDB = @import("state/state.zig").StateDB;
const Block = types.Block;
const Txn = types.Txn;
const TxnSigner = @import("signer/signer.zig").TxnSigner;
const Tx = types.Tx;
const TxSigner = @import("signer/signer.zig").TxSigner;
const httpz = @import("httpz");
const engine_api = @import("engine_api/engine_api.zig");
const json = std.json;
Expand Down
Loading

0 comments on commit 5ee25fb

Please sign in to comment.