Skip to content
This repository has been archived by the owner on Dec 13, 2019. It is now read-only.

Commit

Permalink
Merge branch 'liam/appInstanceAdjudicator' of github.com:counterfactu…
Browse files Browse the repository at this point in the history
…al/monorepo into liam/appInstanceAdjudicator
  • Loading branch information
snario committed Nov 5, 2018
2 parents 367d30b + 40d6697 commit 69ef295
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 62 deletions.
2 changes: 1 addition & 1 deletion packages/contracts/contracts/AppInstanceAdjudicator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ contract AppInstanceAdjudicator {
c.state.status = Status.ON;
c.state.latestSubmitter = msg.sender;

emit DisputeCancelled(_id,msg.sender);
emit DisputeCancelled(_id, msg.sender);
}

/// @notice Fetch and store the resolution of a state channel application
Expand Down
20 changes: 2 additions & 18 deletions packages/contracts/contracts/NonceRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ pragma experimental "ABIEncoderV2";
/// @title NonceRegistry - A global nonce time-lock registry. Maps nonce keys to nonce values.
/// @author Liam Horne - <liam@l4v.io>
/// @notice Supports a global mapping of sender, timeout and salt based keys to sequential nonces
/// A nonce (aka "dependency nonce") is a mapping from a nonce key to a nonce value which can be set
/// under certain circumstances (to be defined later). A nonce is parametrized by the sender, the salt,
/// A nonce is a mapping from a nonce key to a nonce value which can be set
/// if certain conditions (to be defined later) are satisfied. A nonce is parametrized by the sender, the salt,
/// and the timeout. These parameters determine the nonce key. A nonce can only be set by its sender.
/// When a nonce is first set, a timer of length `timeout` starts. During this timeout period, it may
/// only be set to higher values. When the timer expires, the nonce may no longer be set.
Expand Down Expand Up @@ -57,22 +57,6 @@ contract NonceRegistry {
emit NonceSet(key, nonceValue);
}

/// @notice Return the N highest-order bits from the input.
/// @param input A uint256 treated as a bitfield from which to get the high-order bits
/// @param N the number of bits to get from input
/// @return A uint256 where the N highest-order bits are the same as in input, and
/// the other bits are 0
function getFirstNBits(uint256 input, uint8 N) public pure returns (uint256) {
uint256 nOnes = uint256(2) ** N - 1;
uint256 mask = nOnes << (uint8(256) - N); // uint8(256) == uint8(0)
return uint256(bytes32(input) & bytes32(mask));
}

/// @return Whether the Nth highest-order bit in input is set
function bitSet(uint256 self, uint8 index) public pure returns (bool) {
return self >> index & 1 == 1;
}

/// @notice Computes a unique key for the particular salt and msg.sender
/// @param salt A salt used to generate the nonce key
/// @return A unique nonce key derived from the salt and msg.sender
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"build": "truffle compile --all && tsc -b",
"migrate": "truffle migrate --reset --network ganache",
"test": "tsc -b && truffle test --network ganache $npm_package_config_testFiles",
"clean": "rm -rf dist build",
"clean": "rm -rf dist build networks/7777777.json",
"lint:fix": "yarn lint:ts:fix && yarn lint:sol:fix",
"lint": "yarn lint:ts && yarn lint:sol",
"lint:sol:fix": "solium -d contracts/ --fix",
Expand Down
29 changes: 16 additions & 13 deletions packages/contracts/test/integration/commitReveal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ enum Player {
}

const { parseEther } = ethers.utils;
const commitRevealApp = AbstractContract.loadBuildArtifact("CommitRevealApp", {
StaticCall
});
const commitRevealAppDefinition = AbstractContract.loadBuildArtifact(
"CommitRevealApp",
{
StaticCall
}
);

const { provider, unlockedAccount: masterAccount } = setupTestEnv(web3);
const appStateEncoding = abiEncodingForStruct(`
Expand All @@ -73,29 +76,29 @@ async function createMultisig(
return multisig;
}

async function deployApp(): Promise<ethers.Contract> {
return (await commitRevealApp).deploy(masterAccount);
async function deployAppDefinition(): Promise<ethers.Contract> {
return (await commitRevealAppDefinition).deploy(masterAccount);
}

async function deployStateChannel(
async function deployAppInstance(
multisig: Multisig,
appContract: ethers.Contract,
terms: TransferTerms
) {
const registry = await (await Registry).getDeployed(masterAccount);
const signers = multisig.owners; // TODO: generate new signing keys for each state channel
const stateChannel = new AppInstance(
const appInstance = new AppInstance(
signers,
multisig,
appContract,
appStateEncoding,
terms
);
await stateChannel.deploy(masterAccount, registry);
if (!stateChannel.contract) {
await appInstance.deploy(masterAccount, registry);
if (!appInstance.contract) {
throw new Error("Deploy failed");
}
return stateChannel;
return appInstance;
}

async function executeStateChannelTransaction(
Expand Down Expand Up @@ -140,15 +143,15 @@ describe("CommitReveal", async () => {
bob
]);

// 2. Deploy CommitRevealApp app
const appContract = await deployApp();
// 2. Deploy CommitRevealApp AppDefinition
const appContract = await deployAppDefinition();

// 3. Deploy StateChannel
const terms = {
assetType: AssetType.ETH,
limit: parseEther("2")
};
const stateChannel = await deployStateChannel(multisig, appContract, terms);
const stateChannel = await deployAppInstance(multisig, appContract, terms);

// 4. Call setState(claimFinal=true) on StateChannel with a final state
const numberSalt =
Expand Down
25 changes: 0 additions & 25 deletions packages/contracts/test/unit/nonceRegistry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,6 @@ contract("NonceRegistry", accounts => {
registry = await nonceRegistry.deploy(unlockedAccount);
});

it("getFirstNBits works for 8", async () => {
const ret = await registry.functions.getFirstNBits(
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
8
);
expect(ret).to.be.eql(
new ethers.utils.BigNumber(
"0xff00000000000000000000000000000000000000000000000000000000000000"
)
);
});

it("getFirstNBits works for 9", async () => {
const ret = await registry.functions.getFirstNBits(
"0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
9
);
// 0x8 == 0b1000
expect(ret).to.be.eql(
new ethers.utils.BigNumber(
"0xff80000000000000000000000000000000000000000000000000000000000000"
)
);
});

it("can set nonces", async () => {
const timeout = new ethers.utils.BigNumber(10);
await registry.functions.setNonce(timeout, ethers.constants.HashZero, 1);
Expand Down
4 changes: 2 additions & 2 deletions packages/machine/src/middleware/cf-operation/cf-op-install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class CfOpInstall extends CfMultiSendOp {
this.app.terms.limit,
this.app.terms.token
];
const depNonceKey = keccak256(
const uninstallKey = keccak256(
abi.encodePacked(
["address", "uint256", "uint256"],
[this.multisig, 0, this.dependencyNonce.salt]
Expand All @@ -54,7 +54,7 @@ export class CfOpInstall extends CfMultiSendOp {
).functions.executeAppConditionalTransaction.encode([
this.networkContext.registryAddr,
this.networkContext.nonceRegistryAddr,
depNonceKey,
uninstallKey,
this.appCfAddress,
terms
]);
Expand Down
4 changes: 2 additions & 2 deletions packages/machine/src/middleware/cf-operation/cf-op-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class CfOpSetup extends CfMultiSendOp {
public conditionalTransactionInput(): MultisigInput {
const terms = CfFreeBalance.terms();

const depNonceKey = keccak256(
const uninstallKey = keccak256(
abi.encodePacked(
["address", "uint256", "uint256"],
[this.multisig, 0, this.dependencyNonce.salt]
Expand All @@ -59,7 +59,7 @@ export class CfOpSetup extends CfMultiSendOp {
).functions.executeAppConditionalTransaction.encode([
this.networkContext.registryAddr,
this.networkContext.nonceRegistryAddr,
depNonceKey,
uninstallKey,
this.freeBalanceStateChannel.cfAddress(),
[terms.assetType, terms.limit, terms.token]
]);
Expand Down

0 comments on commit 69ef295

Please sign in to comment.