-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathparameters.test.ts
More file actions
151 lines (126 loc) · 3.55 KB
/
parameters.test.ts
File metadata and controls
151 lines (126 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import "reflect-metadata";
import {
PrivateKey,
UInt64,
Provable,
Field,
Struct,
Signature,
MerkleMap,
MerkleMapWitness,
PublicKey,
ZkProgram,
} from "o1js";
import { runtimeMethod, RuntimeModule, runtimeModule } from "@proto-kit/module";
import { assert, State, StateMap, state } from "@proto-kit/protocol";
import { expectDefined } from "@proto-kit/common";
import { TestingAppChain } from "../src/index";
class TestStruct extends Struct({
foo: Field,
bar: Provable.Array(Field, 2),
}) {}
const BALLOT_LENGTH = 10;
class Ballot extends Struct({
ballot: Provable.Array(UInt64, BALLOT_LENGTH),
}) {
public static empty(): Ballot {
const ballot = Array<UInt64>(10).fill(UInt64.from(0));
return new Ballot({ ballot });
}
}
const map = new MerkleMap();
const witness = map.getWitness(Field(0));
async function foo(publicInput: Field) {
return Field(0);
}
const program = ZkProgram({
name: "parametersTestProgram",
publicOutput: Field,
publicInput: Field,
methods: {
foo: {
privateInputs: [],
method: foo,
},
},
});
class ProgramProof extends ZkProgram.Proof(program) {}
const proof = await ProgramProof.dummy(Field(0), Field(0), 2);
@runtimeModule()
class TestRuntime extends RuntimeModule<unknown> {
@state() public test1 = State.from(Field);
@state() public ballots = StateMap.from(Field, Ballot);
@runtimeMethod()
public async test(
field: Field,
uInt64: UInt64,
struct: TestStruct,
signature: Signature,
ballot: Ballot,
// eslint-disable-next-line @typescript-eslint/no-shadow
witness: MerkleMapWitness,
// eslint-disable-next-line @typescript-eslint/no-shadow
proof: ProgramProof,
address: PublicKey
) {
const valid = signature.verify(
this.transaction.sender.value,
TestStruct.toFields(struct)
);
assert(valid, "Signature invalid");
await this.test1.set(field);
await this.ballots.get(Field(1));
await this.ballots.set(Field(1), ballot);
const [root, key] = witness.computeRootAndKey(Field(0));
const knownRoot = Provable.witness(Field, () => map.getRoot());
assert(root.equals(knownRoot), "Root missmatch");
assert(key.equals(Field(0)), "Key missmatch");
proof.verify();
Provable.log("address", address);
}
}
describe("parameters", () => {
it("should accept various provable transaction arguments", async () => {
expect.assertions(2);
const signer = PrivateKey.random();
const sender = signer.toPublicKey();
/**
* Setup the app chain for testing purposes,
* using the provided runtime modules
*/
const appChain = TestingAppChain.fromRuntime({
TestRuntime,
});
appChain.configurePartial({
Runtime: {
TestRuntime: {},
Balances: {},
},
});
await appChain.start();
appChain.setSigner(signer);
const runtime = appChain.runtime.resolve("TestRuntime");
const struct = new TestStruct({
foo: Field(1),
bar: [Field(1), Field(1)],
});
const signature = Signature.create(signer, TestStruct.toFields(struct));
const transaction = await appChain.transaction(sender, async () => {
await runtime.test(
Field(0),
UInt64.from(0),
struct,
signature,
Ballot.empty(),
witness,
proof,
PrivateKey.random().toPublicKey()
);
});
await transaction.sign();
await transaction.send();
const block = await appChain.produceBlock();
expectDefined(block);
expect(block.transactions[0].status.toBoolean()).toBe(true);
}, 60_000);
});