Skip to content
This repository has been archived by the owner on Apr 20, 2022. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
attente committed Mar 6, 2018
1 parent 82d6743 commit 03aeef6
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 64 deletions.
19 changes: 12 additions & 7 deletions client/dgame/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ export class DGame {
players: [
{
seedRating: 0,
publicSeed: new Uint8Array(0)
publicSeed: [ethers.utils.bigNumberify(0)]
},
{
seedRating: 0,
publicSeed: new Uint8Array(0)
publicSeed: [ethers.utils.bigNumberify(0)]
}
],
matchSignature: new Signature(),
Expand Down Expand Up @@ -286,11 +286,13 @@ export class State {
}

private tag: number
private data: Uint8Array
// XXX: https://github.com/ethereum/solidity/issues/3270
private data: [ethers.utils.BigNumber]
private metadata: {
nonce: number
tag: number
data: Uint8Array
// XXX: https://github.com/ethereum/solidity/issues/3270
data: [ethers.utils.BigNumber, ethers.utils.BigNumber, ethers.utils.BigNumber]
}
}

Expand Down Expand Up @@ -349,16 +351,19 @@ interface MatchInterface {

interface PlayerInterface {
readonly seedRating: number
readonly publicSeed: Uint8Array
// XXX: https://github.com/ethereum/solidity/issues/3270
readonly publicSeed: [ethers.utils.BigNumber]
}

interface StateInterface {
readonly nonce: number
readonly tag: number
readonly data: Uint8Array
// XXX: https://github.com/ethereum/solidity/issues/3270
readonly data: [ethers.utils.BigNumber, ethers.utils.BigNumber, ethers.utils.BigNumber]
readonly state: {
readonly tag: number
readonly data: Uint8Array
// XXX: https://github.com/ethereum/solidity/issues/3270
readonly data: [ethers.utils.BigNumber]
}
}

Expand Down
15 changes: 12 additions & 3 deletions contracts/Arcadeum.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ contract Arcadeum {
string private constant MESSAGE_PREFIX = 'Sign to play! This won\'t cost anything.\n';
string private constant PLAYER_PREFIX = '\nPlayer: 0x';

// XXX: https://github.com/ethereum/solidity/issues/3270
// *** THIS MUST MATCH DGame.sol ***
uint private constant PUBLIC_SEED_LENGTH = 1;
uint private constant META_STATE_DATA_LENGTH = 3;
uint private constant STATE_DATA_LENGTH = 1;

struct Match {
DGame game;
uint32 matchID;
Expand All @@ -27,7 +33,8 @@ contract Arcadeum {

struct Player {
uint32 seedRating;
bytes publicSeed;
// XXX: https://github.com/ethereum/solidity/issues/3270
bytes32[PUBLIC_SEED_LENGTH] publicSeed;
}

struct Move {
Expand Down Expand Up @@ -427,14 +434,16 @@ contract Arcadeum {
return subkeyParent(subkey, subkeySignature);
}

function matchHash(DGame game, uint32 matchID, uint timestamp, address[2] accounts, uint32[2] seedRatings, bytes[2] publicSeeds) public pure returns (bytes32) {
// XXX: https://github.com/ethereum/solidity/issues/3270
function matchHash(DGame game, uint32 matchID, uint timestamp, address[2] accounts, uint32[2] seedRatings, bytes32[PUBLIC_SEED_LENGTH][2] publicSeeds) public pure returns (bytes32) {
return keccak256(game, matchID, timestamp, accounts[0], accounts[1], seedRatings[0], seedRatings[1], publicSeeds[0], publicSeeds[1]);
}

function matchMaker(Match aMatch, address sender) private pure returns (address) {
address[2] memory accounts;
uint32[2] memory seedRatings;
bytes[2] memory publicSeeds;
// XXX: https://github.com/ethereum/solidity/issues/3270
bytes32[PUBLIC_SEED_LENGTH][2] memory publicSeeds;
bytes32 hash;

accounts[aMatch.playerID] = sender;
Expand Down
51 changes: 27 additions & 24 deletions contracts/DGame.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ pragma solidity ^0.4.19;
pragma experimental ABIEncoderV2;

contract DGame {
// XXX: https://github.com/ethereum/solidity/issues/3270
// *** THIS MUST MATCH Arcadeum.sol ***
uint private constant PUBLIC_SEED_LENGTH = 1;
uint private constant META_STATE_DATA_LENGTH = 3;
uint private constant STATE_DATA_LENGTH = 1;

enum Winner {
NONE,
PLAYER_0,
Expand Down Expand Up @@ -32,13 +38,15 @@ contract DGame {
struct MetaState {
uint32 nonce;
MetaTag tag;
bytes data;
// XXX: https://github.com/ethereum/solidity/issues/3270
bytes32[META_STATE_DATA_LENGTH] data;
State state;
}

struct State {
uint32 tag;
bytes data;
// XXX: https://github.com/ethereum/solidity/issues/3270
bytes32[STATE_DATA_LENGTH] data;
}

struct Move {
Expand All @@ -62,8 +70,9 @@ contract DGame {
return 0;
}

function publicSeed(bytes /* secretSeed */) public pure returns (bytes) {
return new bytes(0);
// XXX: https://github.com/ethereum/solidity/issues/3270
function publicSeed(bytes /* secretSeed */) public pure returns (bytes32[PUBLIC_SEED_LENGTH]) {
return [bytes32(0)];
}

function initialState(bytes publicSeed0, bytes publicSeed1) public pure returns (MetaState);
Expand Down Expand Up @@ -111,8 +120,6 @@ contract DGame {
function isMoveLegal(MetaState metaState, Move move) public pure returns (bool, uint32) {
NextPlayers next;
bytes32 hash;
uint offset;
uint i;

next = nextPlayers(metaState);

Expand Down Expand Up @@ -147,12 +154,10 @@ contract DGame {
}

hash = keccak256(move.data);
offset = 1 + 32 * move.playerID;

for (i = 0; i < 32; i++) {
if (hash[i] != metaState.data[offset + i]) {
return (false, REASON_WRONG_HASH);
}
// XXX: https://github.com/ethereum/solidity/issues/3270
if (hash != metaState.data[1 + move.playerID]) {
return (false, REASON_WRONG_HASH);
}

return (true, REASON_NONE);
Expand All @@ -167,12 +172,10 @@ contract DGame {

} else if (metaState.tag == MetaTag.REVEALING_SECRET) {
hash = keccak256(move.data);
offset = 32 * move.playerID;

for (i = 0; i < 32; i++) {
if (hash[i] != metaState.data[offset + i]) {
return (false, REASON_WRONG_HASH);
}
// XXX: https://github.com/ethereum/solidity/issues/3270
if (hash != metaState.data[move.playerID]) {
return (false, REASON_WRONG_HASH);
}

return (true, REASON_NONE);
Expand Down Expand Up @@ -229,12 +232,12 @@ contract DGame {

} else {
next.tag = MetaTag.REVEALING_RANDOM;
next.data = new bytes(65);
next.data[0] = metaState.data[0];

// XXX: https://github.com/ethereum/solidity/issues/3270
for (i = 0; i < 32; i++) {
next.data[1 + i] = moves[0].data[i];
next.data[33 + i] = moves[1].data[i];
next.data[1] |= bytes32(moves[0].data[i]) << ((31 - i) * 8);
next.data[2] |= bytes32(moves[1].data[i]) << ((31 - i) * 8);
}

next.state = metaState.state;
Expand All @@ -261,11 +264,11 @@ contract DGame {

} else {
next.tag = MetaTag.REVEALING_SECRET;
next.data = new bytes(64);

// XXX: https://github.com/ethereum/solidity/issues/3270
for (i = 0; i < 32; i++) {
next.data[0 + i] = moves[0].data[i];
next.data[32 + i] = moves[1].data[i];
next.data[0] |= bytes32(moves[0].data[i]) << ((31 - i) * 8);
next.data[1] |= bytes32(moves[1].data[i]) << ((31 - i) * 8);
}

next.state = metaState.state;
Expand Down Expand Up @@ -323,8 +326,8 @@ contract DGame {
require(nBytes >= 8);

metaState.tag = MetaTag.COMMITTING_RANDOM;
metaState.data = new bytes(1);
metaState.data[0] = byte(nBytes);
// XXX: https://github.com/ethereum/solidity/issues/3270
metaState.data[0] = bytes32(nBytes);
metaState.state = state;

return metaState;
Expand Down
58 changes: 28 additions & 30 deletions contracts/TTT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,41 @@ contract TTT is DGame {
function initialState(bytes, bytes) public pure returns (MetaState) {
State memory state;

state.data = new bytes(9);

return meta(state);
}

function winnerImplementation(State state) internal pure returns (Winner) {
if (byte(0) != state.data[0] && state.data[0] == state.data[1] && state.data[1] == state.data[2]) {
return Winner(uint8(state.data[0]));
// XXX: https://github.com/ethereum/solidity/issues/3270
if (byte(0) != state.data[0][0] && state.data[0][0] == state.data[0][1] && state.data[0][1] == state.data[0][2]) {
return Winner(uint8(state.data[0][0]));

} else if (byte(0) != state.data[3] && state.data[3] == state.data[4] && state.data[4] == state.data[5]) {
return Winner(uint8(state.data[3]));
// XXX: https://github.com/ethereum/solidity/issues/3270
} else if (byte(0) != state.data[0][3] && state.data[0][3] == state.data[0][4] && state.data[0][4] == state.data[0][5]) {
return Winner(uint8(state.data[0][3]));

} else if (byte(0) != state.data[6] && state.data[6] == state.data[7] && state.data[7] == state.data[8]) {
return Winner(uint8(state.data[6]));
// XXX: https://github.com/ethereum/solidity/issues/3270
} else if (byte(0) != state.data[0][6] && state.data[0][6] == state.data[0][7] && state.data[0][7] == state.data[0][8]) {
return Winner(uint8(state.data[0][6]));

} else if (byte(0) != state.data[0] && state.data[0] == state.data[3] && state.data[3] == state.data[6]) {
return Winner(uint8(state.data[0]));
// XXX: https://github.com/ethereum/solidity/issues/3270
} else if (byte(0) != state.data[0][0] && state.data[0][0] == state.data[0][3] && state.data[0][3] == state.data[0][6]) {
return Winner(uint8(state.data[0][0]));

} else if (byte(0) != state.data[1] && state.data[1] == state.data[4] && state.data[4] == state.data[7]) {
return Winner(uint8(state.data[1]));
// XXX: https://github.com/ethereum/solidity/issues/3270
} else if (byte(0) != state.data[0][1] && state.data[0][1] == state.data[0][4] && state.data[0][4] == state.data[0][7]) {
return Winner(uint8(state.data[0][1]));

} else if (byte(0) != state.data[2] && state.data[2] == state.data[5] && state.data[5] == state.data[8]) {
return Winner(uint8(state.data[2]));
// XXX: https://github.com/ethereum/solidity/issues/3270
} else if (byte(0) != state.data[0][2] && state.data[0][2] == state.data[0][5] && state.data[0][5] == state.data[0][8]) {
return Winner(uint8(state.data[0][2]));

} else if (byte(0) != state.data[0] && state.data[0] == state.data[4] && state.data[4] == state.data[8]) {
return Winner(uint8(state.data[0]));
// XXX: https://github.com/ethereum/solidity/issues/3270
} else if (byte(0) != state.data[0][0] && state.data[0][0] == state.data[0][4] && state.data[0][4] == state.data[0][8]) {
return Winner(uint8(state.data[0][0]));

} else if (byte(0) != state.data[2] && state.data[2] == state.data[4] && state.data[4] == state.data[6]) {
return Winner(uint8(state.data[2]));
// XXX: https://github.com/ethereum/solidity/issues/3270
} else if (byte(0) != state.data[0][2] && state.data[0][2] == state.data[0][4] && state.data[0][4] == state.data[0][6]) {
return Winner(uint8(state.data[0][2]));

} else {
return Winner.NONE;
Expand All @@ -70,7 +76,8 @@ contract TTT is DGame {
return (false, REASON_NOT_A_SQUARE);
}

if (state.data[uint(move.data[0])] != 0) {
// XXX: https://github.com/ethereum/solidity/issues/3270
if (state.data[0][uint(move.data[0])] != 0) {
return (false, REASON_ALREADY_PLAYED);
}

Expand All @@ -81,17 +88,8 @@ contract TTT is DGame {
State memory next;

next.tag = state.tag + 1;
next.data = new bytes(9);
next.data[0] = state.data[0];
next.data[1] = state.data[1];
next.data[2] = state.data[2];
next.data[3] = state.data[3];
next.data[4] = state.data[4];
next.data[5] = state.data[5];
next.data[6] = state.data[6];
next.data[7] = state.data[7];
next.data[8] = state.data[8];
next.data[uint(move.data[0])] = byte(1 + move.playerID);
// XXX: https://github.com/ethereum/solidity/issues/3270
next.data[0] = state.data[0] | (bytes32(1 + move.playerID) << ((31 - uint(move.data[0])) * 8));

return meta(next);
}
Expand Down

0 comments on commit 03aeef6

Please sign in to comment.