Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only organization should anchor state roots #560

2 changes: 1 addition & 1 deletion contracts/gateway/SafeCore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ contract SafeCore is StateRootInterface, Organized, CircularBufferUint {
bytes32 _stateRoot
)
external
onlyWorker
onlyOrganization
returns (bool success_)
{
// State root should be valid
Expand Down
155 changes: 86 additions & 69 deletions test/gateway/safe_core/commit_state_root.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const zeroBytes =
"0x0000000000000000000000000000000000000000000000000000000000000000";

contract('SafeCore.commitStateRoot()', function (accounts) {

schemar marked this conversation as resolved.
Show resolved Hide resolved
let remoteChainId,
blockHeight,
stateRoot,
Expand All @@ -38,161 +38,178 @@ contract('SafeCore.commitStateRoot()', function (accounts) {
safeCore,
owner,
worker;

beforeEach(async function () {

owner = accounts[2];
worker = accounts[3];
remoteChainId = new BN(1410);
blockHeight = new BN(5);
stateRoot = web3.utils.sha3("dummy_state_root");
maxNumberOfStateRoots = new BN(10);
membersManager = await MockMembersManager.new(owner, worker);

safeCore = await SafeCore.new(
remoteChainId,
blockHeight,
stateRoot,
maxNumberOfStateRoots,
membersManager.address,
);

stateRoot = web3.utils.sha3("dummy_state_root_1");

});

it('should fail when state root is zero', async () => {

stateRoot = zeroBytes;
blockHeight = blockHeight.addn(1);

await Utils.expectRevert(
safeCore.commitStateRoot(
blockHeight,
stateRoot,
{ from: worker },
{from: owner},
),
'State root must not be zero.',
);

});

it('should fail when block height is less than the latest committed ' +
'state root\'s block height', async () => {

blockHeight = blockHeight.subn(1);

await Utils.expectRevert(
safeCore.commitStateRoot(
blockHeight,
stateRoot,
{ from: worker },
),
'Given block height is lower or equal to highest committed state root block height.',
);

});

blockHeight = blockHeight.subn(1);
await Utils.expectRevert(
safeCore.commitStateRoot(
blockHeight,
stateRoot,
{from: owner},
),
'Given block height is lower or equal to highest committed state root block height.',
);
});
it('should fail when block height is equal to the latest committed ' +
'state root\'s block height', async () => {

await Utils.expectRevert(
safeCore.commitStateRoot(
blockHeight,
stateRoot,
{ from: worker },
),
'Given block height is lower or equal to highest committed state root block height.',
);

});

it('should fail when caller is not worker address', async () => {

await Utils.expectRevert(
safeCore.commitStateRoot(
blockHeight,
stateRoot,
{from: owner},
),
'Given block height is lower or equal to highest committed state root block height.',
);
});
it('should fail when caller is not owner address', async () => {
blockHeight = blockHeight.addn(1);

let nonOwner = accounts[6];

await Utils.expectRevert(
safeCore.commitStateRoot(
blockHeight,
stateRoot,
{ from: accounts[6] },
{from: nonOwner},
),
'Only whitelisted workers are allowed to call this method.',
'Only the organization is allowed to call this method.',
);

});


it('should fail when caller is not admin address', async () => {
gulshanvasnani marked this conversation as resolved.
Show resolved Hide resolved

blockHeight = blockHeight.addn(1);
let nonOrganization = accounts[6];

await Utils.expectRevert(
safeCore.commitStateRoot(
blockHeight,
stateRoot,
{from: nonOrganization},
),
'Only the organization is allowed to call this method.',
);

});

it('should pass with correct params', async () => {

blockHeight = blockHeight.addn(1);

let result = await safeCore.commitStateRoot.call(
blockHeight,
stateRoot,
{ from: worker },
{from: owner},
);

assert.strictEqual(
result,
true,
'Return value of commitStateRoot must be true.',
);

await safeCore.commitStateRoot(
blockHeight,
stateRoot,
{ from: worker },
{from: owner},
);

let latestBlockHeight = await safeCore.getLatestStateRootBlockHeight.call();
assert.strictEqual(
blockHeight.eq(latestBlockHeight),
true,
`Latest block height from the contract must be ${blockHeight}.`,
);

let latestStateRoot = await safeCore.getStateRoot.call(blockHeight);
assert.strictEqual(
latestStateRoot,
stateRoot,
`Latest state root from the contract must be ${stateRoot}.`,
);

});

it('should emit `StateRootAvailable` event', async () => {

blockHeight = blockHeight.addn(1);

let tx = await safeCore.commitStateRoot(
blockHeight,
stateRoot,
{ from: worker },
{from: owner},
);

let event = EventDecoder.getEvents(tx, safeCore);

assert.isDefined(
event.StateRootAvailable,
'Event `StateRootAvailable` must be emitted.',
);

let eventData = event.StateRootAvailable;

assert.strictEqual(
eventData._stateRoot,
stateRoot,
`The _stateRoot value in the event should be equal to ${stateRoot}`
);

assert.strictEqual(
blockHeight.eq(eventData._blockHeight),
true,
`The _blockHeight in the event should be equal to ${blockHeight}`
);

});

it('should store only the given number of max store roots', async () => {
/*
* It should store the given state roots and they should be
Expand All @@ -206,9 +223,9 @@ contract('SafeCore.commitStateRoot()', function (accounts) {
await safeCore.commitStateRoot(
blockHeight,
stateRoot,
{ from: worker },
{from: owner},
);

// Check that the older state root has been deleted when i > max state roots.
if (maxNumberOfStateRoots.ltn(i)) {
let prunedBlockHeight = blockHeight.sub(maxNumberOfStateRoots);
Expand All @@ -221,7 +238,7 @@ contract('SafeCore.commitStateRoot()', function (accounts) {
'There should not be any state root stored at a ' +
'pruned height. It should have been reset by now.',
);

/*
* The state root that is one block younger than the pruned
* one should still be available.
Expand All @@ -238,5 +255,5 @@ contract('SafeCore.commitStateRoot()', function (accounts) {
}
}
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ contract('SafeCore.getLatestStateRootBlockHeight()', function (accounts) {
await safeCore.commitStateRoot(
blockHeight,
stateRoot,
{ from: worker },
{ from: owner },
);

let latestBlockHeight = await safeCore.getLatestStateRootBlockHeight.call();
Expand Down
2 changes: 1 addition & 1 deletion test/gateway/safe_core/get_state_root.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ contract('SafeCore.getStateRoot()', function (accounts) {
await safeCore.commitStateRoot(
blockHeight,
stateRoot,
{ from: worker },
{ from: owner },
);

let latestStateRoot = await safeCore.getStateRoot.call(blockHeight);
Expand Down