Skip to content

Commit

Permalink
Rename Battleships smart contract to BattleshipsZkApp and adapt imports
Browse files Browse the repository at this point in the history
  • Loading branch information
Shigoto-dev19 committed Mar 1, 2024
1 parent 41d8a26 commit 161a127
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 22 deletions.
14 changes: 7 additions & 7 deletions src/Battleships.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
Battleships,
BattleshipsZkApp,
TargetMerkleWitness,
HitMerkleWitness,
EMPTY_TREE8_ROOT,
Expand All @@ -18,7 +18,7 @@ import { AttackUtils, BoardUtils } from './provableUtils';

const proofsEnabled = false;

async function localDeploy(zkapp: Battleships, deployerKey: PrivateKey, zkappPrivateKey: PrivateKey) {
async function localDeploy(zkapp: BattleshipsZkApp, deployerKey: PrivateKey, zkappPrivateKey: PrivateKey) {
const deployerAccount = deployerKey.toPublicKey();
const tx = await Mina.transaction(deployerAccount, () => {
AccountUpdate.fundNewAccount(deployerAccount);
Expand All @@ -29,7 +29,7 @@ async function localDeploy(zkapp: Battleships, deployerKey: PrivateKey, zkappPri
await tx.sign([deployerKey, zkappPrivateKey]).send();
}

async function initializeGame(zkapp: Battleships, deployerKey: PrivateKey) {
async function initializeGame(zkapp: BattleshipsZkApp, deployerKey: PrivateKey) {
const deployerAccount = deployerKey.toPublicKey();

// deployer initializes zkapp
Expand All @@ -47,7 +47,7 @@ describe('Battleships Game Tests', () => {
intruderKey: PrivateKey,
zkappAddress: PublicKey,
zkappPrivateKey: PrivateKey,
zkapp: Battleships,
zkapp: BattleshipsZkApp,
targetTree: MerkleTree,
hitTree: MerkleTree,
hostBoard: number[][],
Expand All @@ -57,7 +57,7 @@ describe('Battleships Game Tests', () => {
intruderBoard: number[][];

beforeAll(async () => {
if (proofsEnabled) await Battleships.compile();
if (proofsEnabled) await BattleshipsZkApp.compile();

// setup local blockchain
const Local = Mina.LocalBlockchain({ proofsEnabled });
Expand All @@ -71,7 +71,7 @@ describe('Battleships Game Tests', () => {
// zkapp account
zkappPrivateKey = PrivateKey.random();
zkappAddress = zkappPrivateKey.toPublicKey();
zkapp = new Battleships(zkappAddress);
zkapp = new BattleshipsZkApp(zkappAddress);

// initialize local Merkle Tree for target & hit storage
targetTree = new MerkleTree(8);
Expand Down Expand Up @@ -571,7 +571,7 @@ describe('Battleships Game Tests', () => {
});

// player2 turn --> turn = 7 --> report player1 Miss
it('should accept player2 sending a valid attack TX choosing a previous target that was a MISS: 6th check', async () => {
it('should accept player2 sending a valid attack TX choosing a previous target that was a MISS: 7th check', async () => {
await testValidAttack(joinerKey, joinerBoard, joinerSalt, [9, 0], false, [1, 2]);
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/Battleships.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from './provableUtils.js';

export {
Battleships,
BattleshipsZkApp,
TargetMerkleWitness,
HitMerkleWitness,
EMPTY_TREE8_ROOT,
Expand All @@ -30,7 +30,7 @@ const EMPTY_TREE8_ROOT = Field(1447284246012508664544490936857120907919499162790
class TargetMerkleWitness extends MerkleWitness(8) {}
class HitMerkleWitness extends MerkleWitness(8) {}

class Battleships extends SmartContract {
class BattleshipsZkApp extends SmartContract {
@state(Field) player1Id = State<Field>();
@state(Field) player2Id = State<Field>();
@state(UInt8) turns = State<UInt8>();
Expand Down
8 changes: 4 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Mina,
} from 'o1js';
import {
Battleships,
BattleshipsZkApp,
HitMerkleWitness,
TargetMerkleWitness
} from './Battleships.js';
Expand All @@ -31,7 +31,7 @@ export { BattleShipsClient }

//TODO Point to redundancy of game data storage and that the merkle tree are sustainble in case of error
class BattleShipsClient {
public zkapp: Battleships;
public zkapp: BattleshipsZkApp;

private playerKey: PrivateKey;
public playerAddress: PublicKey;
Expand All @@ -57,7 +57,7 @@ class BattleShipsClient {
public playerHits: number[];

constructor(
zkapp: Battleships,
zkapp: BattleshipsZkApp,
playerKey: PrivateKey,
board: number[][],
) {
Expand Down Expand Up @@ -86,7 +86,7 @@ class BattleShipsClient {
this.playerHits = [];
}

static initialize(zkapp: Battleships, playerKey: PrivateKey, board: number[][]) {
static initialize(zkapp: BattleshipsZkApp, playerKey: PrivateKey, board: number[][]) {
return new BattleShipsClient(
zkapp,
playerKey,
Expand Down
2 changes: 1 addition & 1 deletion src/displayUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function generateEmptyGameGrid(): string[][] {
/**
* Positions ships on a 10x10 grid representing a Battleships board.
*
* @param ships An array of ship configurations, where each ship is represented as [x, y, z] coordinates:
* @param board An array of ship configurations, where each ship is represented as [x, y, z] coordinates:
* x = x coordinate on the board
* y = y coordinate on the board
* z = orientation (0=horizontal, 1=vertical)
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { Battleships } from './Battleships.js';
import { BattleshipsZkApp } from './Battleships.js';

export { Battleships };
export { BattleshipsZkApp };
13 changes: 7 additions & 6 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
*/

import { Battleships } from './Battleships.js';
import { BattleshipsZkApp } from './Battleships.js';
import { BattleShipsClient } from './client.js';
import { printLog } from './displayUtils.js';
import {
Expand All @@ -26,9 +26,10 @@ import {
PublicKey,
} from 'o1js';

//TODO Add checkmarks and log after Tx is valid
const proofsEnabled = process.argv[2] ? true : false;

async function localDeploy(zkapp: Battleships, deployerKey: PrivateKey, zkappPrivateKey: PrivateKey) {
async function localDeploy(zkapp: BattleshipsZkApp, deployerKey: PrivateKey, zkappPrivateKey: PrivateKey) {
const deployerAccount = deployerKey.toPublicKey();
const tx = await Mina.transaction(deployerAccount, () => {
AccountUpdate.fundNewAccount(deployerAccount);
Expand All @@ -39,7 +40,7 @@ async function localDeploy(zkapp: Battleships, deployerKey: PrivateKey, zkappPri
await tx.sign([deployerKey, zkappPrivateKey]).send();
}

async function initializeGame(zkapp: Battleships, deployerKey: PrivateKey) {
async function initializeGame(zkapp: BattleshipsZkApp, deployerKey: PrivateKey) {
const deployerAccount = deployerKey.toPublicKey();

// deployer initializes zkapp
Expand All @@ -58,11 +59,11 @@ async function runGame(gameBoard: typeof game1Boards, gameShots: typeof game1Sho
joinerKey: PrivateKey,
zkappAddress: PublicKey,
zkappPrivateKey: PrivateKey,
zkapp: Battleships,
zkapp: BattleshipsZkApp,
joiner: BattleShipsClient,
host: BattleShipsClient;

if (proofsEnabled) await Battleships.compile();
if (proofsEnabled) await BattleshipsZkApp.compile();

// setup local blockchain
const Local = Mina.LocalBlockchain({ proofsEnabled });
Expand All @@ -75,7 +76,7 @@ async function runGame(gameBoard: typeof game1Boards, gameShots: typeof game1Sho
// zkapp account
zkappPrivateKey = PrivateKey.random();
zkappAddress = zkappPrivateKey.toPublicKey();
zkapp = new Battleships(zkappAddress);
zkapp = new BattleshipsZkApp(zkappAddress);

// initialize client for each player
host = BattleShipsClient.initialize(zkapp, hostKey, gameBoard.host);
Expand Down

0 comments on commit 161a127

Please sign in to comment.