/
e2e.ts
131 lines (119 loc) · 4.28 KB
/
e2e.ts
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
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { ethers } from "hardhat";
import { exit } from "process";
import { GameTurn, ZKShuffle } from "@zk-shuffle/jssdk/src/shuffle/zkShuffle";
import { Hilo, Hilo__factory, ShuffleManager } from "../types";
import { deploy_shuffle_manager } from "../helper/deploy";
import { dnld_aws, P0X_DIR, sleep } from "@zk-shuffle/jssdk/src/shuffle/utility";
import { resolve } from "path";
async function player_run(SM: ShuffleManager, owner: SignerWithAddress, gameId: number) {
console.log("Player ", owner.address.slice(0, 6).concat("..."), "init shuffle context!");
const numCards = (await SM.getNumCards(gameId)).toNumber();
let encrypt_wasm;
let encrypt_zkey;
if (numCards == 5) {
(encrypt_wasm = resolve(P0X_DIR, "./wasm/encrypt.wasm.5")),
(encrypt_zkey = resolve(P0X_DIR, "./zkey/encrypt.zkey.5"));
} else if (numCards == 30) {
(encrypt_wasm = resolve(P0X_DIR, "./wasm/encrypt.wasm.30")),
(encrypt_zkey = resolve(P0X_DIR, "./zkey/encrypt.zkey.30"));
} else if (numCards == 52) {
(encrypt_wasm = resolve(P0X_DIR, "./wasm/encrypt.wasm")),
(encrypt_zkey = resolve(P0X_DIR, "./zkey/encrypt.zkey"));
}
const player = await ZKShuffle.create(
SM.address,
owner,
await ZKShuffle.generateShuffleSecret(),
resolve(P0X_DIR, "./wasm/decrypt.wasm"),
resolve(P0X_DIR, "./zkey/decrypt.zkey"),
encrypt_wasm,
encrypt_zkey,
);
// join Game
let playerIdx = await player.joinGame(gameId);
console.log(
"Player ",
owner.address.slice(0, 6).concat("..."),
"Join Game ",
gameId,
" asigned playerId ",
playerIdx,
);
// play game
let turn = GameTurn.NOP;
while (turn != GameTurn.Complete) {
turn = await player.checkTurn(gameId);
//console.log("player ", playerIdx, " state : ", state, " nextBlock ", nextBlock)
if (turn != GameTurn.NOP) {
switch (turn) {
case GameTurn.Shuffle:
console.log("Player ", playerIdx, " 's Shuffle turn!");
await player.shuffle(gameId);
break;
case GameTurn.Deal:
console.log("Player ", playerIdx, " 's Deal Decrypt turn!");
await player.draw(gameId);
break;
case GameTurn.Open:
console.log("Player ", playerIdx, " 's Open Decrypt turn!");
let cards = await player.openOffchain(gameId, [playerIdx]);
console.log("Player ", playerIdx, " open offchain hand card ", cards[0]);
cards = await player.open(gameId, [playerIdx]);
console.log("Player ", playerIdx, " open onchain hand card ", cards[0]);
break;
case GameTurn.Complete:
console.log("Player ", playerIdx, " 's Game End!");
break;
default:
console.log("err turn ", turn);
exit(-1);
}
}
await sleep(1000);
}
}
async function fullprocess(numCard: number) {
const signers = await ethers.getSigners();
const sm_owner = signers[10];
const hilo_owner = signers[11];
const players = signers;
// deploy shuffleManager
const SM: ShuffleManager = await deploy_shuffle_manager(sm_owner);
// deploy gameContract
const game: Hilo = await new Hilo__factory(hilo_owner).deploy(SM.address, numCard);
// Hilo.newGame
await (await game.connect(players[0]).newGame()).wait();
const gameId = (await game.largestGameId()).toNumber(); // TODO : get gameId from reciept.
console.log("Player ", players[0].address.slice(0, 6).concat("..."), "Create Game ", gameId);
// allow Join Game
await (await game.connect(players[0]).allowJoinGame(gameId)).wait();
await Promise.all([player_run(SM, players[0], gameId), player_run(SM, players[1], gameId)]);
}
describe("ZKShuffle E2E test", function () {
before(async () => {
await Promise.all(
[
"wasm/decrypt.wasm",
"zkey/decrypt.zkey",
"wasm/encrypt.wasm.5",
"zkey/encrypt.zkey.5",
"wasm/encrypt.wasm.30",
"zkey/encrypt.zkey.30",
"wasm/encrypt.wasm",
"zkey/encrypt.zkey",
].map(async (e) => {
await dnld_aws(e);
}),
);
});
it("Hilo E2E 5 card", async () => {
await fullprocess(5);
});
it("Hilo E2E 30 card", async () => {
await fullprocess(30);
});
it("Hilo E2E 52 card", async () => {
await fullprocess(52);
});
});