Skip to main content

Raw bytes and machines

The @ethdebug/pointers package uses two abstractions for low-level concerns. Both come from the @ethdebug/common package, which holds the runtime code that the ethdebug packages share:

  • Raw represents a sequence of bytes. It is a plain Uint8Array type, together with a namespace of functions that convert to/from other formats (like hex strings and bigints), for example Raw.toHex(raw) and Raw.fromUint(value).
  • interface Machine defines an asynchronous API to model a running EVM. It includes the sole method trace(): AsyncIterable<Machine.State>, where the Machine.State type represents point-in-time access to a running EVM at a particular program counter in a particular execution context, etc.

Raw​

tip

Note how this implementation handles the conversion to/from JavaScript's number and bigint types. The EVM orders numerical values' bytes by starting with the most significant byte first (i.e., they are big-endian).

When implementing this functionality, be careful not to get this wrong.

Code listing for src/raw.ts
src/raw.ts
import { toHex as hexOf } from "ethereum-cryptography/utils";

/**
* A byte string, most significant byte first.
*/
export type Raw = Uint8Array;

export namespace Raw {
export function zero(): Raw {
return new Uint8Array([]);
}

export function fromUint(value: bigint): Raw {
if (value === 0n) {
return zero();
}

const byteCount = Math.ceil(Number(value.toString(2).length) / 8);
const bytes = new Uint8Array(byteCount);
for (let i = byteCount - 1; i >= 0; i--) {
bytes[i] = Number(value & 0xffn);
value >>= 8n;
}
return bytes;
}

export function fromNumber(value: number): Raw {
const byteCount = Math.ceil(Math.log2(value + 1) / 8);
const bytes = new Uint8Array(byteCount);
for (let i = byteCount - 1; i >= 0; i--) {
bytes[i] = value & 0xff;
value >>= 8;
}
return bytes;
}

export function fromHex(hex: string): Raw {
if (!hex.startsWith("0x")) {
throw new Error('Invalid hex string format. Expected "0x" prefix.');
}
const bytes = new Uint8Array((hex.length - 2) / 2 + 0.5);
for (let i = 2; i < hex.length; i += 2) {
bytes[i / 2 - 1] = parseInt(hex.slice(i, i + 2), 16);
}
return bytes;
}

export function asUint(raw: Raw): bigint {
const bits = 8n;

let value = 0n;
for (const byte of raw.values()) {
const byteValue = BigInt(byte);
value = (value << bits) + byteValue;
}
return value;
}

export function toHex(raw: Raw): string {
return `0x${hexOf(raw)}`;
}

export function padUntilAtLeast(raw: Raw, length: number): Raw {
if (raw.length >= length) {
return raw;
}

const padded = new Uint8Array(length);
padded.set(raw, length - raw.length);
return padded;
}

export function resizeTo(raw: Raw, length: number): Raw {
if (raw.length === length) {
return raw;
}

const resized = new Uint8Array(length);

if (raw.length < length) {
resized.set(raw, length - raw.length);
} else {
resized.set(raw.slice(raw.length - length));
}

return resized;
}

export function concat(...raws: Raw[]): Raw {
const length = raws.reduce((total, raw) => total + raw.length, 0);
const joined = new Uint8Array(length);
let offset = 0;
for (const raw of raws) {
joined.set(raw, offset);
offset += raw.length;
}
return joined;
}
}

Machine​

Code listing for src/machine.ts
src/machine.ts
import type { Raw } from "#raw";

export interface Machine {
trace(): AsyncIterable<Machine.State>;
}

export namespace Machine {
export interface State {
get traceIndex(): Promise<bigint>;
get programCounter(): Promise<bigint>;
get opcode(): Promise<string>;

get stack(): State.Stack;
get memory(): State.Bytes;
get storage(): State.Words;
get calldata(): State.Bytes;
get returndata(): State.Bytes;
get transient(): State.Words;
get code(): State.Bytes;
}

export namespace State {
export interface Slice {
offset: bigint;
length: bigint;
}

export interface Stack {
get length(): Promise<bigint>;

/** read element at position from top of stack */
peek(options: { depth: bigint; slice?: Slice }): Promise<Raw>;
}

export interface Bytes {
get length(): Promise<bigint>;

read(options: { slice: Slice }): Promise<Raw>;
}

export interface Words {
read(options: { slot: Raw; slice?: Slice }): Promise<Raw>;
}
}
}