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

Sequencing relay requests #902

Merged
merged 15 commits into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions contracts/solidity/contracts/KeepRandomBeaconOperator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ contract KeepRandomBeaconOperator is Ownable {

uint256[] public tickets;
bytes[] public submissions;
uint256 internal currentEntryStartBlock;
bool internal entryInProgress;

bool public groupSelectionInProgress;

Expand All @@ -83,7 +85,7 @@ contract KeepRandomBeaconOperator is Ownable {

// Timeout in blocks for a relay entry to appear on the chain. Blocks are
// counted from the moment relay request occur.
uint256 public relayRequestTimeout;
uint256 public relayEntryTimeout;

// expiredGroupOffset is pointing to the first active group, it is also the
// expired groups counter
Expand Down Expand Up @@ -151,7 +153,7 @@ contract KeepRandomBeaconOperator is Ownable {
* @param _activeGroupsThreshold is the minimal number of groups that cannot be marked as expired and
* needs to be greater than 0.
* @param _groupActiveTime is the time in block after which a group expires.
* @param _relayRequestTimeout Timeout in blocks for a relay entry to appear on the chain.
* @param _relayEntryTimeout Timeout in blocks for a relay entry to appear on the chain.
* Blocks are counted from the moment relay request occur.
*/
function initialize(
Expand All @@ -167,7 +169,7 @@ contract KeepRandomBeaconOperator is Ownable {
uint256 _resultPublicationBlockStep,
uint256 _activeGroupsThreshold,
uint256 _groupActiveTime,
uint256 _relayRequestTimeout,
uint256 _relayEntryTimeout,
uint256 _genesisEntry,
bytes memory _genesisGroupPubKey
) public onlyOwner {
Expand All @@ -186,7 +188,7 @@ contract KeepRandomBeaconOperator is Ownable {
resultPublicationBlockStep = _resultPublicationBlockStep;
activeGroupsThreshold = _activeGroupsThreshold;
groupActiveTime = _groupActiveTime;
relayRequestTimeout = _relayRequestTimeout;
relayEntryTimeout = _relayEntryTimeout;
groupSelectionSeed = _genesisEntry;

// Create initial relay entry request. This will allow relayEntry to be called once
Expand Down Expand Up @@ -552,7 +554,7 @@ contract KeepRandomBeaconOperator is Ownable {
* performing any operations.
*/
function groupStaleTime(Group memory group) internal view returns(uint256) {
return groupActiveTimeOf(group) + relayRequestTimeout;
return groupActiveTimeOf(group) + relayEntryTimeout;
pdyraga marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -672,6 +674,12 @@ contract KeepRandomBeaconOperator is Ownable {
"At least one group needed to serve the request."
);

uint256 entryTimeout = currentEntryStartBlock + relayEntryTimeout;
require(!entryInProgress || block.number > entryTimeout, "Relay entry is in progress.");

currentEntryStartBlock = block.number;
entryInProgress = true;

bytes memory groupPubKey = selectGroup(previousEntry);

signingRequestCounter++;
Expand Down Expand Up @@ -701,5 +709,7 @@ contract KeepRandomBeaconOperator is Ownable {

ServiceContract(serviceContract).entryCreated(requestId, _groupSignature);
createGroup(_groupSignature, _seed);

entryInProgress = false;
}
}
18 changes: 16 additions & 2 deletions contracts/solidity/migrations/2_deploy_contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,21 @@ const timeoutChallenge = 4;
const resultPublicationBlockStep = 3;
const activeGroupsThreshold = 5;
const groupActiveTime = 300;
const relayRequestTimeout = 10;
// Time in blocks it takes to execute relay entry signing.
// 1 state with state.MessagingStateDelayBlocks which is set to 1
// 1 state with state.MessagingStateActiveBlocks which is set to 3
const relayEntrySigningTime = 4

// Deadline in blocks for relay entry publication after the first
// group member becomes eligible to submit the result.
// Deadline should not be shorter than the time it takes for the
// last group member to become eligible plus at least one block
// to submit.
const relayEntryPublicationDeadline = 20

// The maximum time it may take for relay entry to appear on
// chain after relay request has been published
const relayEntryTimeout = relayEntrySigningTime + relayEntryPublicationDeadline

// timeDKG - Timeout in blocks after DKG result is complete and ready to be published.
// 7 states with state.MessagingStateActiveBlocks which is set to 3
Expand Down Expand Up @@ -55,7 +69,7 @@ module.exports = async function(deployer) {
keepRandomBeaconOperator.initialize(
StakingProxy.address, KeepRandomBeaconService.address, minStake, groupThreshold, groupSize,
timeoutInitial, timeoutSubmission, timeoutChallenge, timeDKG, resultPublicationBlockStep,
activeGroupsThreshold, groupActiveTime, relayRequestTimeout,
activeGroupsThreshold, groupActiveTime, relayEntryTimeout,
web3.utils.toBN('31415926535897932384626433832795028841971693993751058209749445923078164062862'),
"0x1f1954b33144db2b5c90da089e8bde287ec7089d5d6433f3b6becaefdb678b1b2a9de38d14bef2cf9afc3c698a4211fa7ada7b4f036a2dfef0dc122b423259d0",
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import expectThrowWithMessage from './helpers/expectThrowWithMessage';
pdyraga marked this conversation as resolved.
Show resolved Hide resolved
import {bls} from './helpers/data';
import {initContracts} from './helpers/initContracts';
import mineBlocks from './helpers/mineBlocks';

contract('TestKeepRandomBeaconOperatorRelayRequestTimeout', function(accounts) {
pdyraga marked this conversation as resolved.
Show resolved Hide resolved
pdyraga marked this conversation as resolved.
Show resolved Hide resolved
let serviceContract, operatorContract;
const blocksForward = 20;

describe("RelayRequestTimeout", function() {

beforeEach(async () => {

let contracts = await initContracts(
accounts,
artifacts.require('./KeepToken.sol'),
artifacts.require('./StakingProxy.sol'),
artifacts.require('./TokenStaking.sol'),
artifacts.require('./KeepRandomBeaconService.sol'),
artifacts.require('./KeepRandomBeaconServiceImplV1.sol'),
artifacts.require('./KeepRandomBeaconOperatorStub.sol')
);

operatorContract = contracts.operatorContract;
serviceContract = contracts.serviceContract;

// Using stub method to add first group to help testing.
await operatorContract.registerNewGroup(bls.groupPubKey);
});

it("should not throw an error when sigining is in progress and the block number > relay entry timeout", async function() {
await serviceContract.requestRelayEntry(bls.seed, {value: 10});
mineBlocks(blocksForward)
await serviceContract.requestRelayEntry(bls.seed, {value: 10});

assert.equal((await operatorContract.getPastEvents())[0].event, 'SignatureRequested', "SignatureRequested event should occur on operator contract.");
})

it("should throw an error when sigining is in progress and the block number <= relay entry timeout", async function() {
await serviceContract.requestRelayEntry(bls.seed, {value: 10});

await expectThrowWithMessage(serviceContract.requestRelayEntry(bls.seed, {value: 10}), 'Relay entry request is in progress.');
})

it("should not throw an error when sigining is not in progress and the block number > relay entry timeout", async function() {
await serviceContract.requestRelayEntry(bls.seed, {value: 10});

assert.equal((await operatorContract.getPastEvents())[0].event, 'SignatureRequested', "SignatureRequested event should occur on operator contract.");
})

})
});
10 changes: 7 additions & 3 deletions contracts/solidity/test/TestKeepRandomBeaconServiceViaProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import latestTime from './helpers/latestTime';
import exceptThrow from './helpers/expectThrow';
import encodeCall from './helpers/encodeCall';
import {initContracts} from './helpers/initContracts';
import mineBlocks from './helpers/mineBlocks';
pdyraga marked this conversation as resolved.
Show resolved Hide resolved
const ServiceContractProxy = artifacts.require('./KeepRandomBeaconService.sol')

contract('TestKeepRandomBeaconServiceViaProxy', function(accounts) {
Expand All @@ -13,7 +14,9 @@ contract('TestKeepRandomBeaconServiceViaProxy', function(accounts) {
account_two = accounts[1],
account_three = accounts[2];

before(async () => {
const blocksForward = 20;
pdyraga marked this conversation as resolved.
Show resolved Hide resolved

beforeEach(async () => {
let contracts = await initContracts(
accounts,
artifacts.require('./KeepToken.sol'),
Expand Down Expand Up @@ -51,10 +54,12 @@ contract('TestKeepRandomBeaconServiceViaProxy', function(accounts) {

let contractBalanceViaProxy = await web3.eth.getBalance(serviceContractProxy.address);
assert.equal(contractBalanceViaProxy, 100, "Keep Random Beacon service contract new balance should be visible via serviceContractProxy.");

});

it("should be able to request relay entry via serviceContractProxy contract with enough ether", async function() {
await serviceContract.requestRelayEntry(0, {from: account_two, value: 100})

mineBlocks(blocksForward)
pdyraga marked this conversation as resolved.
Show resolved Hide resolved
await exceptThrow(serviceContractProxy.sendTransaction({from: account_two, value: 1000}));

await web3.eth.sendTransaction({
Expand All @@ -72,7 +77,6 @@ contract('TestKeepRandomBeaconServiceViaProxy', function(accounts) {
});

it("owner should be able to withdraw ether from random beacon service contract", async function() {

await serviceContract.requestRelayEntry(0, {from: account_one, value: 100})

// should fail to withdraw if not owner
Expand Down
10 changes: 10 additions & 0 deletions contracts/solidity/test/helpers/expectThrowWithMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default async (promise, message) => {
try {
await promise;
} catch (error) {
assert.include(error.message, message);

return;
}
assert.fail('Expected throw not received');
};