-
Notifications
You must be signed in to change notification settings - Fork 26
/
russian-roulette.ts
156 lines (135 loc) · 4.74 KB
/
russian-roulette.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import assert from "assert";
import * as anchor from "@coral-xyz/anchor";
import { Program, BN } from "@coral-xyz/anchor";
import {
Keypair,
PublicKey,
SystemProgram,
LAMPORTS_PER_SOL,
} from "@solana/web3.js";
import {
Orao,
networkStateAccountAddress,
randomnessAccountAddress,
FulfillBuilder,
InitBuilder,
} from "@orao-network/solana-vrf";
import { RussianRoulette } from "../target/types/russian_roulette";
import nacl from "tweetnacl";
describe("russian-roulette", () => {
const provider = anchor.AnchorProvider.env();
anchor.setProvider(provider);
const program = anchor.workspace
.RussianRoulette as Program<RussianRoulette>;
const vrf = new Orao(provider);
// This accounts are for test VRF.
const treasury = Keypair.generate();
const fulfillmentAuthority = Keypair.generate();
// Initial force for russian-roulette
let force = Keypair.generate().publicKey;
// Player state account address won't change during the tests.
const [playerState] = PublicKey.findProgramAddressSync(
[
Buffer.from("russian-roulette-player-state"),
provider.wallet.publicKey.toBuffer(),
],
program.programId
);
// This helper will play a single round of russian-roulette.
async function spinAndPullTheTrigger(prevForce: Buffer, force: Buffer) {
const prevRound = randomnessAccountAddress(prevForce);
const random = randomnessAccountAddress(force);
await program.methods
.spinAndPullTheTrigger([...force])
.accounts({
player: provider.wallet.publicKey,
playerState,
prevRound,
vrf: vrf.programId,
config: networkStateAccountAddress(),
treasury: treasury.publicKey,
random,
systemProgram: SystemProgram.programId,
})
.rpc();
}
// This helper will fulfill randomness for our test VRF.
async function emulateFulfill(seed: Buffer) {
let signature = nacl.sign.detached(
seed,
fulfillmentAuthority.secretKey
);
await new FulfillBuilder(vrf, seed).rpc(
fulfillmentAuthority.publicKey,
signature
);
}
before(async () => {
// Initialize test VRF
const fee = 2 * LAMPORTS_PER_SOL;
const fulfillmentAuthorities = [fulfillmentAuthority.publicKey];
const configAuthority = Keypair.generate();
await new InitBuilder(
vrf,
configAuthority.publicKey,
treasury.publicKey,
fulfillmentAuthorities,
new BN(fee)
).rpc();
});
it("spin and pull the trigger", async () => {
await spinAndPullTheTrigger(Buffer.alloc(32), force.toBuffer());
const playerStateAcc = await program.account.playerState.fetch(
playerState
);
assert.ok(Buffer.from(playerStateAcc.force).equals(force.toBuffer()));
assert.ok(playerStateAcc.rounds.eq(new BN(1)));
});
it("play until dead", async () => {
let currentNumberOfRounds = 1;
let prevForce = force;
while (true) {
let [randomness, _] = await Promise.all([
vrf.waitFulfilled(force.toBuffer()),
emulateFulfill(force.toBuffer()),
]);
assert.ok(
!Buffer.from(randomness.randomness).equals(Buffer.alloc(64))
);
if (
Buffer.from(randomness.randomness).readBigUInt64LE() %
BigInt(6) ===
BigInt(0)
) {
console.log("The player is dead");
break;
} else {
console.log("The player is alive");
}
// Run another round
prevForce = force;
force = Keypair.generate().publicKey;
await spinAndPullTheTrigger(prevForce.toBuffer(), force.toBuffer());
const playerStateAcc = await program.account.playerState.fetch(
playerState
);
assert.ok(
Buffer.from(playerStateAcc.force).equals(force.toBuffer())
);
assert.ok(
playerStateAcc.rounds.eq(new BN(++currentNumberOfRounds))
);
}
});
it("can't play anymore", async () => {
const prevForce = force;
force = Keypair.generate().publicKey;
try {
await spinAndPullTheTrigger(prevForce.toBuffer(), force.toBuffer());
} catch (e) {
assert.equal(e.error.errorCode.code, "PlayerDead");
return;
}
assert.ok(false, "Instruction invocation should fail");
});
});