Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GatewayRequest.drain() to copy stack to output for better testing #5

Merged
merged 4 commits into from
Sep 30, 2024
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
2 changes: 1 addition & 1 deletion contracts/GatewayProver.sol
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ library GatewayProver {
vm.target = address(0);
vm.storageRoot = NOT_A_CONTRACT;
vm.slot = 0;
outputs = new bytes[](vm.readByte());
outputs = new bytes[](vm.readByte()); // NOTE: implies maximum outputs is 255
exitCode = evalCommand(vm, outputs);
}

Expand Down
4 changes: 2 additions & 2 deletions src/eth/merkle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { HexString, BigNumberish } from '../types.js';
import type { EthProof } from './types.js';
import { ZeroHash } from 'ethers/constants';
import { keccak256 } from 'ethers/crypto';
import { decodeRlp, zeroPadValue } from 'ethers/utils';
import { decodeRlp } from 'ethers/utils';
import { toPaddedHex } from '../utils.js';

// https://github.com/ethereum/consensus-specs/blob/dev/ssz/merkle-proofs.md
Expand Down Expand Up @@ -53,7 +53,7 @@ export function proveStorageValue(
if (!rlp) return ZeroHash;
const decoded = decodeRlp(rlp);
if (typeof decoded !== 'string') throw new Error('invalid storage value');
return zeroPadValue(decoded, 32);
return toPaddedHex(decoded);
}

// same arg order as MerkleTrie.get()
Expand Down
34 changes: 24 additions & 10 deletions src/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
ProofSequenceV1,
Provider,
} from './types.js';
import { ZeroAddress, ZeroHash, MaxUint256 } from 'ethers/constants';
import { ZeroAddress, ZeroHash } from 'ethers/constants';
import { Contract } from 'ethers/contract';
import { Interface } from 'ethers/abi';
import { keccak256 } from 'ethers/crypto';
Expand Down Expand Up @@ -256,8 +256,11 @@ export class GatewayProgram {
plus() {
return this.addByte(OP.PLUS);
}
complement() {
return this.not().push(1).plus(); // twos complement
}
subtract() {
return this.push(MaxUint256).plus().not().plus(); // TODO: add op?
return this.complement().plus();
}
times() {
return this.addByte(OP.TIMES);
Expand Down Expand Up @@ -318,24 +321,35 @@ export class GatewayRequest extends GatewayProgram {
super();
this.addByte(outputCount);
}
get outputCount() {
return this.ops[0];
}
override clone() {
const temp = new GatewayRequest();
temp.ops.length = 0;
temp.ops.push(...this.ops);
temp.inputs.push(...this.inputs);
return temp;
}
// convenience for writing JS-based requests
// (this functionality is not available in solidity)
get outputCount() {
return this.ops[0];
}
// the following functionality is not available in solidity!
private ensureCapacity(n: number) {
if (n < this.outputCount) throw new Error('invalid capacity');
if (n > 0xff) throw new Error('output overflow');
this.ops[0] = n;
}
// convenience for writing requests
addOutput() {
const i = this.ops[0];
if (i == 0xff) throw new Error('output overflow');
this.ops[0] = i + 1;
const i = this.outputCount;
this.ensureCapacity(i + 1);
return this.setOutput(i);
}
// convenience for draining stack into outputs
drain(count: number) {
const offset = this.outputCount;
this.ensureCapacity(offset + count);
while (count > 0) this.setOutput(offset + --count);
return this;
}
}

export type TargetNeed = { target: HexAddress; required: boolean };
Expand Down
91 changes: 52 additions & 39 deletions test/components/ops.test.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import {
GatewayProgram,
GatewayRequest,
solidityArraySlots,
solidityFollowSlot,
} from '../../src/vm.js';
import { GatewayProgram, GatewayRequest } from '../../src/vm.js';
import type { BigNumberish } from '../../src/types.js';
import { EthProver } from '../../src/eth/EthProver.js';
import { Foundry } from '@adraffy/blocksmith';
import { ZeroAddress } from 'ethers/constants';
import { id as keccakStr } from 'ethers/hash';
import { randomBytes } from 'ethers/crypto';
import { hexlify, dataSlice, toUtf8Bytes, concat } from 'ethers/utils';
import { toPaddedHex } from '../../src/utils.js';
import { afterAll, expect, test } from 'bun:test';
import { describe } from '../bun-describe-fix.js';
import { randomBytes } from 'ethers/crypto';

function rngUint(n = 32) {
return BigInt(hexlify(randomBytes(n)));
Expand Down Expand Up @@ -113,10 +108,15 @@ describe('ops', async () => {
});

test('setSlot', async () => {
const slot = 1337n;
const req = new GatewayRequest().push(slot).slot();
const state = await verify(req);
expect(state.slot).toEqual(slot);
const req = new GatewayRequest().push(123).slot().pushSlot().addOutput();
const { values } = await verify(req);
expect(values[0]).toEqual(toPaddedHex(123));
});

test('addSlot', async () => {
const req = new GatewayRequest().push(123).addSlot().pushSlot().addOutput();
const { values } = await verify(req);
expect(values[0]).toEqual(toPaddedHex(123));
});

test('keccak', async () => {
Expand Down Expand Up @@ -231,15 +231,21 @@ describe('ops', async () => {
expect(state.values[0]).toEqual(toPaddedHex(sum));
});

test('subtract', async () => {
test('plus wraps', async () => {
const req = new GatewayRequest().push(3).push(-1).plus().addOutput();
const { values } = await verify(req);
expect(values[0]).toEqual(toPaddedHex(2));
});

test('times wraps', async () => {
const req = new GatewayRequest().push(2).push(-1).times().addOutput();
const { values } = await verify(req);
expect(values[0]).toEqual(toPaddedHex(-2));
});

test('divide by zero', async () => {
const req = new GatewayRequest().push(1).push(0).divide().addOutput();
expect(verify(req)).rejects.toThrow('divi');
expect(verify(req)).rejects.toThrow('divi'); // NOTE: node/bun use diff message
});

test('not', async () => {
Expand All @@ -250,59 +256,61 @@ describe('ops', async () => {

testRepeat('shift left', async () => {
const x = rngUint();
const shift = rngUint(2);
const shift = rngUint(1);
const req = new GatewayRequest().push(x).shl(shift).addOutput();
const { values } = await verify(req);
expect(values[0]).toEqual(toPaddedHex(x << shift));
});

test('shift right', async () => {
const req = new GatewayRequest().push(256).shr(8).addOutput();
testRepeat('shift right', async () => {
const x = rngUint();
const shift = rngUint(1);
const req = new GatewayRequest().push(x).shr(shift).addOutput();
const { values } = await verify(req);
expect(values[0]).toEqual(toPaddedHex(1));
expect(values[0]).toEqual(toPaddedHex(x >> shift));
});

test('cast to uint256', async () => {
const value = '0x88';
const req = new GatewayRequest()
.pushBytes(value)
.pushBytes(value) // NOTE: not push()
.dup()
.addOutput()
.pushBytes(value) // note: not push()
.push(0)
.or()
.or() // turns bytes(0x88) into uint256(0x88)
.addOutput();
const { values } = await verify(req);
expect(values[0]).toEqual(value);
expect(values[1]).toEqual(toPaddedHex(value));
});

test('dup last', async () => {
const req = new GatewayRequest().push(1).dup();
const { stack } = await verify(req);
expect(stack).toEqual(toPaddedArray([1, 1]));
const req = new GatewayRequest().push(1).dup().drain(2);
const { values } = await verify(req);
expect(values).toEqual(toPaddedArray([1, 1]));
});

test('dup deep', async () => {
const req = new GatewayRequest().push(1).push(2).push(3).dup(2);
const { stack } = await verify(req);
expect(stack).toEqual(toPaddedArray([1, 2, 3, 1]));
const req = new GatewayRequest().push(1).push(2).push(3).dup(2).drain(4);
const { values } = await verify(req);
expect(values).toEqual(toPaddedArray([1, 2, 3, 1]));
});

test('dup nothing', async () => {
const req = new GatewayRequest().dup().addOutput();
const req = new GatewayRequest().dup();
expect(verify(req)).rejects.toThrow('back overflow');
});

test('dup2', async () => {
const req = new GatewayRequest().push(1).push(2).dup(1).dup(1);
const { stack } = await verify(req);
expect(stack).toEqual(toPaddedArray([1, 2, 1, 2]));
const req = new GatewayRequest().push(1).push(2).dup(1).dup(1).drain(4);
const { values } = await verify(req);
expect(values).toEqual(toPaddedArray([1, 2, 1, 2]));
});

test('swap', async () => {
const req = new GatewayRequest().push(1).push(2).swap();
const { stack } = await verify(req);
expect(stack).toEqual(toPaddedArray([2, 1]));
const req = new GatewayRequest().push(1).push(2).swap().drain(2);
const { values } = await verify(req);
expect(values).toEqual(toPaddedArray([2, 1]));
});

test('swap mixed', async () => {
Expand Down Expand Up @@ -358,9 +366,13 @@ describe('ops', async () => {
});

test('follow slot', async () => {
const req = new GatewayRequest().setSlot(6).pushStr('raffy').follow();
const { slot } = await verify(req);
expect(slot).toEqual(solidityFollowSlot(6, utf8Hex('raffy')));
const req = new GatewayRequest()
.setSlot(6)
.pushStr('raffy')
.follow()
.pushSlot()
.addOutput();
await verify(req);
});

test('follow value', async () => {
Expand All @@ -380,9 +392,10 @@ describe('ops', async () => {
.setTarget(contract.target)
.setSlot(5)
.push(1) // names[1]
.followIndex();
const { slot } = await verify(req);
expect(slot).toEqual(solidityArraySlots(5, 2)[1]);
.followIndex()
.pushSlot()
.addOutput();
await verify(req);
});

test('followIndex value', async () => {
Expand Down