From e6a9f3b386436483775c21ecf3e0f88ec6b8216b Mon Sep 17 00:00:00 2001 From: Westlad Date: Tue, 15 Feb 2022 14:04:21 +0000 Subject: [PATCH] fix: bug - proposers can register twice --- nightfall-deployer/contracts/Proposers.sol | 1 + test/http.mjs | 52 ++++++++++++++++------ test/neg-http.mjs | 13 ++++-- 3 files changed, 49 insertions(+), 17 deletions(-) diff --git a/nightfall-deployer/contracts/Proposers.sol b/nightfall-deployer/contracts/Proposers.sol index 6ebf50ad1..6d7f1ff48 100644 --- a/nightfall-deployer/contracts/Proposers.sol +++ b/nightfall-deployer/contracts/Proposers.sol @@ -31,6 +31,7 @@ contract Proposers is Stateful, Structures, Config { //add the proposer to the circular linked list function registerProposer() external payable { require(REGISTRATION_BOND <= msg.value, 'The registration payment is incorrect'); + require(state.getProposer(msg.sender).thisAddress == address(0), 'This proposer is already registered'); payable(address(state)).transfer(REGISTRATION_BOND); state.setBondAccount(msg.sender,REGISTRATION_BOND); LinkedAddress memory currentProposer = state.getCurrentProposer(); diff --git a/test/http.mjs b/test/http.mjs index 596be4f2f..8386d4760 100644 --- a/test/http.mjs +++ b/test/http.mjs @@ -257,19 +257,25 @@ describe('Testing the http API', () => { describe('Basic Proposer tests', () => { after(async () => { - // After the proposer tests, re-register proposers - const myAddress = (await getAccounts())[0]; - const res = await chai - .request(optimistUrl) - .post('/proposer/register') - .send({ address: myAddress }); - const { txDataToSign } = res.body; - expect(txDataToSign).to.be.a('string'); - const bond = 10; - const count = logCounts.registerProposer; - await submitTransaction(txDataToSign, privateKey, proposersAddress, gas, bond); - await waitForTxExecution(count, 'registerProposer'); - stateBalance += bond; + // After the proposer tests, re-register proposers, if needed + try { + const myAddress = (await getAccounts())[0]; + const res = await chai + .request(optimistUrl) + .post('/proposer/register') + .send({ address: myAddress }); + const { txDataToSign } = res.body; + expect(txDataToSign).to.be.a('string'); + const bond = 10; + const count = logCounts.registerProposer; + await submitTransaction(txDataToSign, privateKey, proposersAddress, gas, bond); + await waitForTxExecution(count, 'registerProposer'); + stateBalance += bond; + } catch (err) { + // an EVM revert almost certainly indicates that the proposer is already registered. That's + // fine, it's ok to continue + if (!err.message.includes('Transaction has been reverted by the EVM')) throw new Error(err); + } }); it('should register a proposer', async () => { @@ -306,6 +312,26 @@ describe('Testing the http API', () => { }); }); + it('should fail to register a proposer twice', async () => { + const myAddress = (await getAccounts())[0]; + const res = await chai + .request(optimistUrl) + .post('/proposer/register') + .send({ address: myAddress }); + const { txDataToSign } = res.body; + expect(txDataToSign).to.be.a('string'); + // we have to pay 10 ETH to be registered + const bond = 10; + // now we need to sign the transaction and send it to the blockchain + console.log('submitting tx'); + try { + await submitTransaction(txDataToSign, privateKey, proposersAddress, gas, bond); + expect.fail('Submitting the same proposer registration should have caused an EVM revert'); + } catch (err) { + expect(err.message).to.include('Transaction has been reverted by the EVM'); + } + }); + it('should de-register a proposer', async () => { const myAddress = (await getAccounts())[0]; const res = await chai.request(optimistUrl).post('/proposer/de-register'); diff --git a/test/neg-http.mjs b/test/neg-http.mjs index 6bb27f222..13c4bf65a 100644 --- a/test/neg-http.mjs +++ b/test/neg-http.mjs @@ -192,10 +192,15 @@ describe('Testing the challenge http API', () => { // should register a proposer const myAddress = (await getAccounts())[0]; const bond = 10; - res = await chai.request(optimistUrl).post('/proposer/register').send({ address: myAddress }); - txToSign = res.body.txDataToSign; - await submitTransaction(txToSign, privateKey, proposersAddress, gas, bond); - + try { + res = await chai.request(optimistUrl).post('/proposer/register').send({ address: myAddress }); + txToSign = res.body.txDataToSign; + await submitTransaction(txToSign, privateKey, proposersAddress, gas, bond); + } catch (err) { + // an EVM revert almost certainly indicates that the proposer is already registered. That's + // fine, it's ok to continue + if (!err.message.includes('Transaction has been reverted by the EVM')) throw new Error(err); + } // should subscribe to block proposed event with the provided incoming viewing key await chai .request(url)