New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

fmg-simple-adjudicator

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fmg-simple-adjudicator - npm Package Compare versions

Comparing version 0.0.10 to 0.1.4

11

package.json
{
"name": "fmg-simple-adjudicator",
"version": "0.0.10",
"version": "0.1.4",
"description": "Simple adjudicator for force-move games",

@@ -16,11 +16,12 @@ "directories": {

"dependencies": {
"fmg-core": "^0.0.6",
"fmg-core": "^0.1.4",
"sha3": "^1.2.2",
"web3-utils": "^1.0.0-beta.30"
"web3": "^1.0.0-beta.36",
"web3-utils": "^1.0.0-beta.36"
},
"devDependencies": {
"esm": "^3.0.52",
"truffle-assertions": "^0.4.0",
"truffle-core": "^4.1.11"
"truffle": "^5.0.0-beta.1",
"truffle-assertions": "^0.4.0"
}
}

@@ -1,2 +0,4 @@

import { Channel, State, assertRevert, increaseTime, duration, CountingGame } from 'fmg-core';
import { toWei } from 'web3-utils';
import { Channel, State, assertRevert, increaseTime, DURATION, CountingGame, sign, } from 'fmg-core';
import BN from 'bn.js';

@@ -13,4 +15,4 @@ var SimpleAdjudicator = artifacts.require("./SimpleAdjudicator.sol");

const B_IDX = 2;
const aBal = Number(web3.toWei('6', "ether"));
const bBal = Number(web3.toWei('4', "ether"));
const aBal = Number(toWei('6', "wei"));
const bBal = Number(toWei('4', "wei"));
const resolution = [aBal, bBal];

@@ -20,9 +22,18 @@ const differentResolution = [bBal, aBal];

contract('SimpleAdjudicator', (accounts) => {
let simpleAdj, countingGame;
let simpleAdj;
let channel;
let state0, state1, state2, state3, state1alt, state2alt;
let alice, aliceState, aliceBal, r0, v0, s0;
let bob, bobState, bobBal, r1, v1, s1;
// private keys are from the accounts generated by `ganache-cli -d`
const alice = web3.eth.accounts.privateKeyToAccount('6cbed15c793ce57650b9877cf6fa156fbef513c4e6134f022a85b1ffdd59b2a1');
const bob = web3.eth.accounts.privateKeyToAccount('6370fd033278c143179d81c5526140625662b8daa446c22ee2d73db3707e620c');
let aliceDestination, bobDestination;
const challenger = bob;
const challengee = alice;
let aliceState, aliceBal, r0, v0, s0;
let bobState, bobBal, r1, v1, s1;
let aliceEthAccount, bobEthAccount;
before(async () => {

@@ -33,3 +44,7 @@ CountingStateContract.link(StateLib);

let countingGameContract = await CountingGameContract.new();
channel = new Channel(countingGameContract.address, 0, [accounts[A_IDX], accounts[B_IDX]]);
channel = new Channel(
countingGameContract.address.toLowerCase(),
0,
[alice.address.toLowerCase(), bob.address.toLowerCase()]
);

@@ -52,8 +67,5 @@ state0 = CountingGame.gameState({channel, resolution, turnNum: 6, gameCounter: 1});

let challenger = accounts[B_IDX];
let challengee = accounts[A_IDX];
let { r: r0, s: s0, v: v0 } = sign(agreedState.toHex(), challengee.privateKey);
let { r: r1, s: s1, v: v1 } = sign(challengeState.toHex(), challenger.privateKey);
let [r0, s0, v0] = agreedState.sign(challengee);
let [r1, s1, v1] = challengeState.sign(challenger);
assert.equal(await simpleAdj.currentChallengePresent(), false);

@@ -70,3 +82,3 @@

// Have to cancel the challenge as to not muck up further tests...
let [r2, s2, v2] = responseState.sign(challengee);
const { r: r2, s: s2, v: v2 } = sign(responseState.toHex(), challengee.privateKey);
await simpleAdj.respondWithMove(responseState.toHex(), v2, r2, s2);

@@ -80,8 +92,5 @@ });

let challenger = accounts[B_IDX];
let challengee = accounts[A_IDX];
const { r: r0, s: s0, v: v0 } = sign(agreedState.toHex(), challengee.privateKey)
const { r: r1, s: s1, v: v1 } = sign(challengeState.toHex(), challenger.privateKey);
let [r0, s0, v0] = agreedState.sign(challengee);
let [r1, s1, v1] = challengeState.sign(challenger);
assert.equal(await simpleAdj.currentChallengePresent(), false);

@@ -94,3 +103,3 @@

let [r2, s2, v2] = responseState.sign(challengee);
let { r: r2, s: s2, v: v2 } = sign(responseState.toHex(), challengee.privateKey);
let tx = await simpleAdj.respondWithMove(responseState.toHex(), v2, r2, s2);

@@ -109,8 +118,5 @@

let challenger = accounts[B_IDX];
let challengee = accounts[A_IDX];
let { r: r0, s: s0, v: v0 } = sign(agreedState.toHex(), challengee.privateKey);
let { r: r1, s: s1, v: v1 } = sign(challengeState.toHex(), challenger.privateKey);
let [r0, s0, v0] = agreedState.sign(challengee);
let [r1, s1, v1] = challengeState.sign(challenger);
assert.equal(await simpleAdj.currentChallengePresent(), false, "challenge exists at start of game");

@@ -122,3 +128,3 @@

// refute
let [r2, s2, v2] = refutationState.sign(challenger);
let { r: r2, s: s2, v: v2 } = sign(refutationState.toHex(), challenger.privateKey);

@@ -138,8 +144,5 @@ let tx = await simpleAdj.refute(refutationState.toHex(), v2, r2, s2);

let challenger = accounts[B_IDX];
let challengee = accounts[A_IDX];
let { r: r0, s: s0, v: v0 } = sign(agreedState.toHex(), challengee.privateKey);
let { r: r1, s: s1, v: v1 } = sign(challengeState.toHex(), challenger.privateKey);
let [r0, s0, v0] = agreedState.sign(challengee);
let [r1, s1, v1] = challengeState.sign(challenger);
assert.equal(await simpleAdj.currentChallengePresent(), false, "challenge exists at start of game");

@@ -150,4 +153,4 @@

let [r2, s2, v2] = alternativeState.sign(challenger);
let [r3, s3, v3] = responseState.sign(challengee);
let { r: r2, s: s2, v: v2 } = sign(alternativeState.toHex(), challenger.privateKey);
let { r: r3, s: s3, v: v3 } = sign(responseState.toHex(), challengee.privateKey);

@@ -169,13 +172,7 @@ let tx = await simpleAdj.alternativeRespondWithMove(

bobState = state1;
alice = accounts[A_IDX];
bob = accounts[B_IDX];
aliceState.stateType = State.StateType.Conclude;
aliceState.stateType = State.StateType.Conclude;
bobState.stateType = State.StateType.Conclude;
[r0, s0, v0] = aliceState.sign(alice);
[r1, s1, v1] = bobState.sign(bob);
await simpleAdj.conclude(aliceState.toHex(), bobState.toHex(), [v0, v1], [r0, r1], [s0, s1] );
const { r: r0, s: s0, v: v0 } = sign(aliceState.toHex(), alice.privateKey);
const { r: r1, s: s1, v: v1 } = sign(bobState.toHex(), bob.privateKey);
await simpleAdj.conclude(aliceState.toHex(), bobState.toHex(), [v0, v1], [r0, r1], [s0, s1] );
assertRevert(

@@ -185,4 +182,23 @@ simpleAdj.conclude(aliceState.toHex(), bobState.toHex(), [v0, v1], [r0, r1], [s0, s1])

});
describe("withdrawals", () => {
let aliceStart, bobStart;
async function withdrawHelper(account, destination) {
const { v, r, s } = sign(
[
{ type: 'address', value: account.address },
{ type: 'address', value: destination },
{ type: 'bytes32', value: channel.id },
],
account.privateKey
);
describe("withdrawals", () => {
await simpleAdj.withdraw(
account.address,
destination,
channel.id,
v, r, s,
);
}
beforeEach(async () => {

@@ -192,15 +208,22 @@ aliceState = state0;

alice = accounts[A_IDX];
bob = accounts[B_IDX];
aliceBal = aBal;
bobBal = bBal;
aliceEthAccount = accounts[A_IDX];
bobEthAccount = accounts[B_IDX];
aliceStart = await Number(await web3.eth.getBalance(aliceEthAccount));
bobStart = await Number(await web3.eth.getBalance(bobEthAccount));
// Generate random destination addresses for more deterministic tests.
// How ironic.
aliceDestination = web3.eth.accounts.create().address.toLowerCase();
bobDestination = web3.eth.accounts.create().address.toLowerCase();
simpleAdj = await SimpleAdjudicator.new(channel.id);
});
it("forceMove -> timeout -> withdraw", async () => {
// fund the contract
it("conclude -> withdraw", async () => {
await web3.eth.sendTransaction({
from: alice, // challengee
from: aliceEthAccount,
to: simpleAdj.address,

@@ -212,3 +235,3 @@ value: aliceBal,

await web3.eth.sendTransaction({
from: bob, // challenger
from: bobEthAccount,
to: simpleAdj.address,

@@ -220,3 +243,3 @@ value: bobBal,

assert.equal(
web3.eth.getBalance(simpleAdj.address),
await web3.eth.getBalance(simpleAdj.address),
aliceBal + bobBal,

@@ -226,41 +249,62 @@ "Funds were not deposited in the SimpleAdjudicator"

assert.equal(
Number(web3.eth.getBalance(bob)),
START_BALANCE - bobBal,
Number(await web3.eth.getBalance(bobEthAccount)),
bobStart - bobBal,
"Funds were not deposited from bob"
);
assert.equal(
Number(web3.eth.getBalance(alice)),
START_BALANCE - aliceBal,
Number(await web3.eth.getBalance(aliceEthAccount)),
aliceStart - aliceBal,
"Funds were not deposited from alice"
);
// create a valid conclusion proof
aliceState.stateType = State.StateType.Conclude;
bobState.stateType = State.StateType.Conclude;
[r0, s0, v0] = aliceState.sign(alice);
[r1, s1, v1] = bobState.sign(bob);
const { r: r0, s: s0, v: v0 } = sign(aliceState.toHex(), alice.privateKey);
const { r: r1, s: s1, v: v1 } = sign(bobState.toHex(), bob.privateKey);
await simpleAdj.forceMove(aliceState.toHex(), bobState.toHex(), [v0, v1], [r0, r1], [s0, s1] );
await increaseTime(duration.days(2));
await simpleAdj.withdraw(alice);
await simpleAdj.withdraw(bob);
// pass the conclusion proof
await simpleAdj.conclude(aliceState.toHex(), bobState.toHex(), [v0, v1], [r0, r1], [s0, s1] );
await withdrawHelper(bob, bobDestination);
assert.equal(
web3.eth.getBalance(simpleAdj.address),
Number(await web3.eth.getBalance(bobDestination)),
bBal,
"Bob's balance resolved incorrectly after his withdrawal."
);
assert.equal(
Number(await web3.eth.getBalance(aliceDestination)),
0,
"SimpleAdjudicator wasn't emptied"
"Alice's balance resolved incorrectly before her withdrawal."
);
await withdrawHelper(alice, aliceDestination);
assert.equal(
Number(web3.eth.getBalance(bob)),
START_BALANCE,
"Resolved balances incorrectly."
Number(await web3.eth.getBalance(aliceDestination)),
aliceBal,
"Alice's balance resolved incorrectly after her withdrawal."
);
assert.equal(
Number(web3.eth.getBalance(alice)),
START_BALANCE,
"Resolved balances incorrectly."
await web3.eth.getBalance(simpleAdj.address),
0,
"SimpleAdjudicator wasn't emptied"
);
await withdrawHelper(bob, bobDestination);
assert.equal(
Number(await web3.eth.getBalance(bobDestination)),
bBal,
"Bob withdrew multiple times."
);
});
it("conclude -> withdraw", async () => {
it("forceMove -> timeout -> withdraw", async () => {
// fund the contract
await web3.eth.sendTransaction({
from: alice,
from: aliceEthAccount, // challengee
to: simpleAdj.address,

@@ -272,3 +316,3 @@ value: aliceBal,

await web3.eth.sendTransaction({
from: bob,
from: bobEthAccount, // challenger
to: simpleAdj.address,

@@ -280,3 +324,3 @@ value: bobBal,

assert.equal(
web3.eth.getBalance(simpleAdj.address),
await web3.eth.getBalance(simpleAdj.address),
aliceBal + bobBal,

@@ -286,52 +330,37 @@ "Funds were not deposited in the SimpleAdjudicator"

assert.equal(
Number(web3.eth.getBalance(bob)),
START_BALANCE - bobBal,
Number(await web3.eth.getBalance(bobEthAccount)),
bobStart - bobBal,
"Funds were not deposited from bob"
);
assert.equal(
Number(web3.eth.getBalance(alice)),
START_BALANCE - aliceBal,
Number(await web3.eth.getBalance(aliceEthAccount)),
aliceStart - aliceBal,
"Funds were not deposited from alice"
);
aliceState.stateType = State.StateType.Conclude;
bobState.stateType = State.StateType.Conclude;
[r0, s0, v0] = aliceState.sign(alice);
[r1, s1, v1] = bobState.sign(bob);
const { r: r0, s: s0, v: v0 } = sign(aliceState.toHex(), alice.privateKey);
const { r: r1, s: s1, v: v1 } = sign(bobState.toHex(), bob.privateKey)
await simpleAdj.conclude(aliceState.toHex(), bobState.toHex(), [v0, v1], [r0, r1], [s0, s1] );
await simpleAdj.forceMove(aliceState.toHex(), bobState.toHex(), [v0, v1], [r0, r1], [s0, s1] );
await increaseTime(DURATION.days(2));
await simpleAdj.withdraw(bob);
assert.equal(
Number(web3.eth.getBalance(bob)),
START_BALANCE,
"Bob's balance resolved incorrectly after his withdrawal."
);
await withdrawHelper(alice, aliceDestination);
await withdrawHelper(bob, bobDestination);
assert.equal(
Number(web3.eth.getBalance(alice)),
START_BALANCE - aliceBal,
"Alice's balance resolved incorrectly before her withdrawal."
await web3.eth.getBalance(simpleAdj.address),
0,
"SimpleAdjudicator wasn't emptied"
);
await simpleAdj.withdraw(alice);
assert.equal(
Number(web3.eth.getBalance(alice)),
START_BALANCE,
"Alice's balance resolved incorrectly after her withdrawal."
Number(await web3.eth.getBalance(bobDestination)),
bobBal,
"Resolved Bob's balances incorrectly."
);
assert.equal(
web3.eth.getBalance(simpleAdj.address),
0,
"SimpleAdjudicator wasn't emptied"
Number(await web3.eth.getBalance(aliceDestination)),
aliceBal,
"Resolved Alice's balances incorrectly."
);
await simpleAdj.withdraw(bob);
assert.equal(
Number(web3.eth.getBalance(bob)),
START_BALANCE,
"Bob withdrew multiple times."
);
});

@@ -341,3 +370,3 @@

await web3.eth.sendTransaction({
from: bob,
from: bobEthAccount,
to: simpleAdj.address,

@@ -349,3 +378,3 @@ value: bobBal,

assert.equal(
Number(web3.eth.getBalance(simpleAdj.address)),
Number(await web3.eth.getBalance(simpleAdj.address)),
bobBal,

@@ -355,9 +384,9 @@ "Funds were not deposited in the SimpleAdjudicator"

assert.equal(
Number(web3.eth.getBalance(bob)),
START_BALANCE - bobBal,
Number(await web3.eth.getBalance(bobEthAccount)),
bobStart - bobBal,
"Funds were not deposited from bob"
);
assert.equal(
Number(web3.eth.getBalance(alice)),
START_BALANCE,
Number(await web3.eth.getBalance(aliceEthAccount)),
aliceStart,
"Funds were deposited from alice"

@@ -369,18 +398,18 @@ );

[r0, s0, v0] = aliceState.sign(alice);
[r1, s1, v1] = bobState.sign(bob);
const { r: r0, s: s0, v: v0 } = sign(aliceState.toHex(), alice.privateKey)
const { r: r1, s: s1, v: v1 } = sign(bobState.toHex(), bob.privateKey);
await simpleAdj.conclude(aliceState.toHex(), bobState.toHex(), [v0, v1], [r0, r1], [s0, s1] );
await simpleAdj.withdraw(bob);
await withdrawHelper(bob, bobDestination);
assert.equal(
Number(web3.eth.getBalance(bob)),
START_BALANCE - bobBal,
Number(await web3.eth.getBalance(bobDestination)),
0,
"Bob took alice's money."
);
await simpleAdj.withdraw(alice);
await withdrawHelper(alice, aliceDestination);
assert.equal(
Number(web3.eth.getBalance(alice)),
START_BALANCE + bobBal,
Number(await web3.eth.getBalance(aliceDestination)),
bobBal,
"Alice's balance resolved incorrectly after her withdrawal."

@@ -390,3 +419,3 @@ );

assert.equal(
web3.eth.getBalance(simpleAdj.address),
await web3.eth.getBalance(simpleAdj.address),
0,

@@ -399,3 +428,3 @@ "SimpleAdjudicator wasn't emptied"

describe('events', async () => {
it('emits fundsReceived upon contract creation', async () => {
it.skip('emits fundsReceived upon contract creation', async () => {
CountingStateContract.link(StateLib);

@@ -405,3 +434,3 @@ let stateContract = await CountingStateContract.new();

let countingGameContract = await CountingGameContract.new();
channel = new Channel(countingGameContract.address, 0, [accounts[A_IDX], accounts[B_IDX]]);
channel = new Channel(countingGameContract.address, 0, [alice.address, bob.address]);

@@ -414,3 +443,3 @@ simpleAdj = await SimpleAdjudicator.new(channel.id);

return (
event.adjudicatorBalance.toPrecision(1) === '0'
event.adjudicatorBalance.eq(new BN(0))
);

@@ -421,8 +450,9 @@ });

it('emits fundsReceived upon being sent funds', async () => {
const address = accounts[9];
await simpleAdj.sendTransaction({
from: accounts[9],
from: address,
value: 50
});
let result = await simpleAdj.sendTransaction({
from: accounts[9],
from: address,
value: 100

@@ -433,5 +463,5 @@ });

return (
event.adjudicatorBalance.toPrecision(3) === '150' &&
event.sender === accounts[9] &&
event.amountReceived.toPrecision(3) === '100'
event.adjudicatorBalance.eq(new BN(150)) &&
event.sender === address &&
event.amountReceived.eq(new BN(100))
);

@@ -438,0 +468,0 @@ });

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc