🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Sign inDemoInstall
Socket

merklux

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

merklux - npm Package Compare versions

Comparing version

to
0.1.0

.solcover.js

8

package.json
{
"name": "merklux",
"version": "0.0.3",
"version": "0.1.0",
"description": "A merkleized unidirectional data flow for state verification across multiple chains",

@@ -12,3 +12,5 @@ "directories": {

"ganache-cli": "^6.1.8",
"rlp": "^2.1.0",
"solidity-coverage": "^0.5.11",
"solidity-docgen": "^0.1.0",
"standard": "^12.0.1",

@@ -19,3 +21,5 @@ "truffle": "^4.1.14",

"dependencies": {
"openzeppelin-solidity": "^2.0.0-rc.2"
"openzeppelin-solidity": "^2.0.0-rc.2",
"solidity-patricia-tree": "^1.0.3",
"solidity-rlp": "^1.1.0"
},

@@ -22,0 +26,0 @@ "scripts": {

@@ -5,266 +5,6 @@ const chai = require('chai')

const should = chai.use(require('chai-bignumber')(BigNumber)).should()
const MerkluxTree = artifacts.require('MerkluxTree')
const MerkluxCase = artifacts.require('MerkluxCase')
const { progress } = require('./utils')
const Status = {
OPENED: 0,
ONGOING: 1,
SUCCESS: 2,
FAILURE: 3
}
contract('MerkluxCase', async ([_, primary, nonPrimary]) => {
let originalRootEdge
let targetRootEdge
let snapshotTree
let testNode1
let testNode2
before('Plasma manages its state with MerkluxTree', async () => {
// Get the original root edge
let plasmaTree = await MerkluxTree.new({ from: primary })
await plasmaTree.insert('key1', 'val1', { from: primary })
await plasmaTree.insert('key2', 'val2', { from: primary })
originalRootEdge = await plasmaTree.getRootEdge()
// Get the target root edge
await plasmaTree.insert('key3', 'val3', { from: primary })
await plasmaTree.insert('key4', 'val4', { from: primary })
targetRootEdge = await plasmaTree.getRootEdge()
// Get the target root edge
await plasmaTree.insert('key5', 'val5', { from: primary })
let testRootHash = (await plasmaTree.getRootEdge())[2]
testNode1 = [testRootHash, ...(await plasmaTree.getNode(testRootHash))]
await plasmaTree.insert('key6', 'val6', { from: primary })
testRootHash = (await plasmaTree.getRootEdge())[2]
testNode2 = [testRootHash, ...(await plasmaTree.getNode(testRootHash))]
// Init a test tree
snapshotTree = await MerkluxTree.new({ from: primary })
await snapshotTree.insert('key1', 'val1', { from: primary })
await snapshotTree.insert('key2', 'val2', { from: primary })
})
describe('constructor()', async () => {
let merkluxCase
it('should assign the original root edge and the target root edge', async () => {
merkluxCase = await MerkluxCase.new({ from: primary })
assert.ok('deployed successfully')
})
it('should set its initial status as OPENED', async () => {
merkluxCase = await MerkluxCase.new({ from: primary })
assert.equal((await merkluxCase.status()).toNumber(), Status.OPENED)
})
})
context('Once deployed successfully', async () => {
let merkluxCase
let dataToCommit
const commitNodes = async (nodes) => {
for (const node of nodes) {
await merkluxCase.commitNode(...node, { from: primary })
}
}
const commitValues = async (values) => {
for (const value of values) {
await merkluxCase.commitValue(value, { from: primary })
}
}
before('prepare', async () => {
let rootValueOfOriginalState = originalRootEdge[2]
dataToCommit = await getDataToCommit(snapshotTree, rootValueOfOriginalState)
})
beforeEach('Use a newly deployed MerkluxCase for every test', async () => {
merkluxCase = await MerkluxCase.new({ from: primary })
await merkluxCase.commitOriginalEdge(...originalRootEdge, { from: primary })
await merkluxCase.commitTargetEdge(...targetRootEdge, { from: primary })
})
describe('commitNode()', async () => {
it('should be called only when the case is in OPENED status', async () => {
await commitNodes(dataToCommit.nodes)
assert.ok('successfully committed nodes')
})
it('should revert when an invalid node is added', async () => {
await merkluxCase.commitNode(...testNode1, { from: primary })
assert.ok('Successfully passed because the passed node is valid')
try {
await merkluxCase.commitNode(0, ...originalRootEdge, ...originalRootEdge, { from: primary })
assert.fail('should revert')
} catch (e) {
assert.ok('reverted successfully')
}
})
it('should revert if same hash already exists', async () => {
await merkluxCase.commitNode(...testNode1, { from: primary })
assert.ok('Successfully passed because same hash does not exist')
try {
await merkluxCase.commitNode(...testNode1, { from: primary })
assert.fail('should revert because the same hash already exists')
} catch (e) {
assert.ok('reverted successfully')
}
})
it('should revert when it is not in OPENED status', async () => {
await commitNodes(dataToCommit.nodes)
await commitValues(dataToCommit.values)
await merkluxCase.seal({ from: primary })
try {
await merkluxCase.commitNode(testNode1, { from: primary })
assert.fail('it should revert')
} catch (e) {
assert.ok('successfully reverted')
}
})
})
describe('seal()', async () => {
it('should revert if it does not passes the merkle proof', async () => {
it('should have enough committed values which it refers', async () => {
await commitNodes(dataToCommit.nodes)
try {
await merkluxCase.seal({ from: primary })
assert.fail('should revert')
} catch (e) {
assert.ok('reverted successfully')
}
})
it('should have enough committed nodes for its merkle proof', async () => {
await commitValues(dataToCommit.values)
try {
await merkluxCase.seal({ from: primary })
assert.fail('should revert')
} catch (e) {
assert.ok('reverted successfully')
}
})
})
it('should change its status as ONGOING and emit an event for it', async () => {
// make the case have ONGOING status first
await commitNodes(dataToCommit.nodes)
await commitValues(dataToCommit.values)
let response = await merkluxCase.seal({ from: primary })
// check it emits an event
assert.equal(
web3.toDecimal(response.receipt.logs[0].data),
Status.ONGOING,
'event will be logged in the receipt'
)
// check it returns its status as ONGOING
assert.equal(
(await merkluxCase.status()).toNumber(),
Status.ONGOING,
'it should return its status as ONGOING'
)
})
})
describe('insert()', async () => {
it('should be called only when the case is in ONGOING status', async () => {
try {
await merkluxCase.insert('somekey', 'someval', { from: primary })
assert.fail('should revert')
} catch (e) {
assert.ok('reverted successfully')
}
})
it('should be called by only the primary account', async () => {
// make the case have ONGOING status first
await commitNodes(dataToCommit.nodes)
await commitValues(dataToCommit.values)
await merkluxCase.seal({ from: primary })
// insert item with primary account
await merkluxCase.insert('somekey', 'someval', { from: primary })
assert.ok('primary account can insert item')
try {
// insert item with non primary account
await merkluxCase.insert('somekey', 'someval', { from: nonPrimary })
assert.fail('should revert')
} catch (e) {
assert.ok('non primary account can not insert item')
}
})
})
describe('proof()', async () => {
it('should revert when the calculated root hash is not equal to the target hash', async () => {
// make the case have ONGOING status first
await commitNodes(dataToCommit.nodes)
await commitValues(dataToCommit.values)
await merkluxCase.seal({ from: primary })
// insert manipulated items
await merkluxCase.insert('key3', 'manipulatedval3', { from: primary }) // original value is 'val3'
await merkluxCase.insert('key4', 'manipulatedval4', { from: primary }) // original value is 'val4'
// try to proof
try {
await merkluxCase.proof({ from: nonPrimary })
assert.fail('should revert')
} catch (e) {
assert.ok('reverted because it was manipulated')
}
})
it('should change its state as SUCCESS when the calculated root hash is equal to the target hash', async () => {
// make the case have ONGOING status first
await commitNodes(dataToCommit.nodes)
await commitValues(dataToCommit.values)
await merkluxCase.seal({ from: primary })
// insert correct items
await merkluxCase.insert('key3', 'val3', { from: primary })
await merkluxCase.insert('key4', 'val4', { from: primary })
// try to proof
await merkluxCase.proof({ from: primary })
// check it changes its status as SUCCESS
assert.equal(
(await merkluxCase.status()).toNumber(),
Status.SUCCESS,
'it should return its status as SUCCESS'
)
})
})
})
})
const getDataToCommit = async function (tree, hash) {
let result = {
values: [],
nodes: []
}
let response = await tree.getNode(hash)
// response[2] means the first child's node value
// response[5] means the second child's node value
if (web3.toDecimal(response[2]) === 0 && web3.toDecimal(response[5]) === 0) {
// when if it is a leaf node, push the value to commit
let value = await tree.getValue(hash)
result.values.push(web3.toUtf8(value))
} else {
// when if it is a branch node, push the node and repeat recursively
result.nodes.push([hash, ...response])
let resultFromFirstChild = await getDataToCommit(tree, response[2])
result.values = [...result.values, ...resultFromFirstChild.values]
result.nodes = [...result.nodes, ...resultFromFirstChild.nodes]
if (response[5] != response[2]) {
let resultFromSecondChild = await getDataToCommit(tree, response[5])
result.values = [...result.values, ...resultFromSecondChild.values]
result.nodes = [...result.nodes, ...resultFromSecondChild.nodes]
}
}
return result
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet