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

make MinGasPrice consensus param #163

Merged
merged 3 commits into from
Jan 31, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ async function run() {
epochLength: null,
epochLengthIndex: -1,
},
gas: {
minPrice: 0,
minPriceIndex: -1,
},
},
networkId: `${config.network}-${config.networkId}`,
genesis: config.genesis,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"js-abci": "leapdao/js-abci#34538e252811610b23340a9f5314be4d0a44a501",
"jsbi-utils": "^1.0.0",
"jsondiffpatch": "^0.2.5",
"leap-core": "^0.22.0",
"leap-core": "^0.23.1",
"level": "^4.0.0",
"lodash": "^4.17.10",
"lodash.get": "^4.4.2",
Expand Down
12 changes: 12 additions & 0 deletions src/abis/exitHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@ module.exports = [
name: 'NewToken',
type: 'event',
},
{
anonymous: false,
inputs: [
{
indexed: false,
name: 'minGasPrice',
type: 'uint256',
},
],
name: 'MinGasPrice',
type: 'event',
},
{
constant: false,
inputs: [
Expand Down
4 changes: 4 additions & 0 deletions src/bridgeState.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ module.exports = class BridgeState {
this.deposits = {};
this.exits = {};
this.epochLengths = [];
this.minGasPrices = [];

this.onNewBlock = this.onNewBlock.bind(this);
this.eventsBuffer = new TinyQueue([], (a, b) => {
Expand Down Expand Up @@ -135,6 +136,9 @@ module.exports = class BridgeState {
EpochLength: ({ returnValues: event }) => {
this.epochLengths.push(Number(event.epochLength));
},
MinGasPrice: ({ returnValues: event }) => {
this.minGasPrices.push(Number(event.minGasPrice));
},
});

await handler(events);
Expand Down
5 changes: 3 additions & 2 deletions src/bridgeState.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ const ContractsEventsSubscription = require('./utils/ContractsEventsSubscription

/* eslint-disable */
ContractsEventsSubscription.__setEventBatches([
[{ event: 'EpochLength', returnValues: { epochLength: 4 } }],
[{ event: 'EpochLength', returnValues: { epochLength: '4' } }],
[{ event: 'MinGasPrice', returnValues: { minGasPrice: '1000000' } }],
[
{
event: 'NewDeposit',
returnValues: {
depositId: 0,
depositId: '0',
depositor: '0xB8205608d54cb81f44F263bE086027D8610F3C94',
color: '0',
amount: '100',
Expand Down
7 changes: 7 additions & 0 deletions src/eventsRelay.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ module.exports = class EventsRelay {
sendTx(this.txServerPort, tx.hex());
}, minDelay);
},
MinGasPrice: async event => {
const { minGasPrice } = event.returnValues;
const tx = Tx.minGasPrice(BigInt(minGasPrice));
setTimeout(() => {
sendTx(this.txServerPort, tx.hex());
}, minDelay);
},
ExitStarted: async event => {
const { txHash, outIndex } = event.returnValues;
const tx = Tx.exit(new Input(new Outpoint(txHash, Number(outIndex))));
Expand Down
11 changes: 10 additions & 1 deletion src/tx/applyTx/checkConsolidate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const ADDR_1 = '0x4436373705394267350db2c06613990d34621d69';

describe('checkConsolidate', () => {
test('wrong type', () => {
const tx = Tx.deposit(0, 0, ADDR_1);
const tx = Tx.deposit(1, '123345', ADDR_1);
expect(() => checkConsolidate({}, tx)).toThrow('Consolidate tx expected');
});

Expand All @@ -17,6 +17,9 @@ describe('checkConsolidate', () => {
[new Outpoint(deposit1.hash(), 0).hex()]: deposit1.outputs[0].toJSON(),
[new Outpoint(deposit2.hash(), 0).hex()]: deposit2.outputs[0].toJSON(),
},
gas: {
minPrice: 0,
},
};
const consolidate = Tx.consolidate(
[
Expand All @@ -34,6 +37,9 @@ describe('checkConsolidate', () => {
unspent: {
[new Outpoint(deposit.hash(), 0).hex()]: deposit.outputs[0].toJSON(),
},
gas: {
minPrice: 0,
},
};

expect(() => {
Expand All @@ -55,6 +61,9 @@ describe('checkConsolidate', () => {
[new Outpoint(deposit1.hash(), 0).hex()]: deposit1.outputs[0].toJSON(),
[new Outpoint(deposit2.hash(), 0).hex()]: deposit2.outputs[0].toJSON(),
},
gas: {
minPrice: 0,
},
};
const consolidate = Tx.consolidate(
[
Expand Down
19 changes: 3 additions & 16 deletions src/tx/applyTx/checkDeposit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const makeDepositMock = (depositor, amount, color) => {

const getInitialState = () => ({
processedDeposit: 0,
gas: {
minPrice: 0,
},
});

const defaultDepositMock = makeDepositMock(ADDR_1, '500', 0);
Expand All @@ -41,22 +44,6 @@ describe('checkDeposit', () => {
expect(state.processedDeposit).toBe(1);
});

test('deposit with size 0', () => {
const state = getInitialState();
const tx = Tx.deposit(1, 0, ADDR_1, 1);
expect(() => {
checkDeposit(state, tx, makeDepositMock(ADDR_1, '0', '1'));
}).toThrow('Deposit out has value < 1');
});

test('deposit with negative size', () => {
const state = getInitialState();
const tx = Tx.deposit(1, -1, ADDR_1, 1);
expect(() => {
checkDeposit(state, tx, makeDepositMock(ADDR_1, '-1', '1'));
}).toThrow('Deposit out has value < 1');
});

test('valid tx (nft)', () => {
const state = getInitialState();
const color = 2 ** 15 + 1;
Expand Down
31 changes: 31 additions & 0 deletions src/tx/applyTx/checkMinGasPrice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (c) 2018-present, Leap DAO (leapdao.org)
*
* This source code is licensed under the Mozilla Public License Version 2.0
* found in the LICENSE file in the root directory of this source tree.
*/

const { Type } = require('leap-core');
const { BigInt, equal } = require('jsbi-utils');

module.exports = (state, tx, bridgeState) => {
if (tx.type !== Type.MIN_GAS_PRICE) {
throw new Error('minGasPrice tx expected');
}

if (state.gas.minPriceIndex + 1 !== bridgeState.minGasPrices.length - 1) {
throw new Error('Unknown minGasPrice change');
}

if (
!equal(
BigInt(bridgeState.minGasPrices[state.gas.minPriceIndex + 1]),
BigInt(tx.options.minGasPrice)
)
) {
throw new Error('Wrong minGasPrice');
}

state.gas.minPriceIndex += 1;
state.gas.minPrice = BigInt(tx.options.minGasPrice).toString();
};
50 changes: 50 additions & 0 deletions src/tx/applyTx/checkMinGasPrice.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const { Tx } = require('leap-core');

const checkMinGasPrice = require('./checkMinGasPrice');

const getInitialState = () => ({
gas: {
minPrice: 0,
minPriceIndex: -1,
},
});

describe('checkMinGasPrice', () => {
test('wrong type', () => {
const tx = Tx.transfer([], []);
expect(() => checkMinGasPrice({}, tx)).toThrow('minGasPrice tx expected');
});

test('successful tx', () => {
const state = getInitialState();
const minGasTx = Tx.minGasPrice('40000000000000');

checkMinGasPrice(state, minGasTx, {
minGasPrices: ['40000000000000'],
});

expect(state.gas.minPrice).toBe('40000000000000');
});

test('tx without corresponding event', () => {
const state = getInitialState();
const minGasTx = Tx.minGasPrice('40000000000000');

expect(() => {
checkMinGasPrice(state, minGasTx, {
minGasPrices: [],
});
}).toThrow('Unknown minGasPrice change');
});

test('epoch length mismatch', () => {
const state = getInitialState();
const minGasTx = Tx.minGasPrice('40000000000000');

expect(() => {
checkMinGasPrice(state, minGasTx, {
minGasPrices: ['55555555555555'],
});
}).toThrow('Wrong minGasPrice');
});
});
66 changes: 25 additions & 41 deletions src/tx/applyTx/checkTransfer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const PRIV_3 =

describe('checkTransfer', () => {
test('wrong type', () => {
const tx = Tx.deposit(0, 0, ADDR_1);
const tx = Tx.deposit(1, '234345', ADDR_1);
expect(() => checkTransfer({}, tx, {})).toThrow('Transfer tx expected');
});

Expand All @@ -22,6 +22,9 @@ describe('checkTransfer', () => {
unspent: {
[new Outpoint(deposit.hash(), 0).hex()]: deposit.outputs[0].toJSON(),
},
gas: {
minPrice: 0,
},
};

const transfer = Tx.transfer(
Expand All @@ -40,6 +43,9 @@ describe('checkTransfer', () => {
[new Outpoint(deposit1.hash(), 0).hex()]: deposit1.outputs[0].toJSON(),
[new Outpoint(deposit2.hash(), 0).hex()]: deposit2.outputs[0].toJSON(),
},
gas: {
minPrice: 0,
},
};

const transfer = Tx.transfer(
Expand All @@ -58,6 +64,9 @@ describe('checkTransfer', () => {
unspent: {
[new Outpoint(deposit.hash(), 0).hex()]: deposit.outputs[0].toJSON(),
},
gas: {
minPrice: 0,
},
};

const transfer = Tx.transfer(
Expand All @@ -75,6 +84,9 @@ describe('checkTransfer', () => {
unspent: {
[new Outpoint(deposit.hash(), 0).hex()]: deposit.outputs[0].toJSON(),
},
gas: {
minPrice: 0,
},
};

const transfer = Tx.transfer(
Expand All @@ -99,6 +111,9 @@ describe('checkTransfer', () => {
[new Outpoint(transfer.hash(), 0).hex()]: transfer.outputs[0].toJSON(),
[new Outpoint(transfer.hash(), 1).hex()]: transfer.outputs[1].toJSON(),
},
gas: {
minPrice: 0,
},
};

const transfer2 = Tx.transfer(
Expand All @@ -116,6 +131,9 @@ describe('checkTransfer', () => {
[new Outpoint(deposit1.hash(), 0).hex()]: deposit1.outputs[0].toJSON(),
[new Outpoint(deposit2.hash(), 0).hex()]: deposit2.outputs[0].toJSON(),
},
gas: {
minPrice: 0,
},
};

const transfer = Tx.transfer(
Expand All @@ -136,6 +154,9 @@ describe('checkTransfer', () => {
[new Outpoint(deposit1.hash(), 0).hex()]: deposit1.outputs[0].toJSON(),
[new Outpoint(deposit2.hash(), 0).hex()]: deposit2.outputs[0].toJSON(),
},
gas: {
minPrice: 0,
},
};

const transfer = Tx.transfer(
Expand All @@ -159,6 +180,9 @@ describe('checkTransfer', () => {
unspent: {
[new Outpoint(deposit.hash(), 0).hex()]: deposit.outputs[0].toJSON(),
},
gas: {
minPrice: 0,
},
};

const transfer = Tx.transfer(
Expand All @@ -173,44 +197,4 @@ describe('checkTransfer', () => {
checkTransfer(state, transfer, {});
}).toThrow('Ins and outs values are mismatch');
});

test('tx with 0 value out', () => {
const deposit = Tx.deposit(12, '1000', ADDR_1, 0);
const state = {
unspent: {
[new Outpoint(deposit.hash(), 0).hex()]: deposit.outputs[0].toJSON(),
},
};

const transfer = Tx.transfer(
[new Input(new Outpoint(deposit.hash(), 0))],
[new Output('1000', ADDR_1, 0), new Output('0', ADDR_2, 0)]
).signAll(PRIV_1);

console.log(transfer);

expect(() => {
checkTransfer(state, transfer, {});
}).toThrow('One of the outs has value < 1');
});

test('try to send tx with negative value out', () => {
const deposit = Tx.deposit(12, 1000, ADDR_1, 0);
const state = {
unspent: {
[new Outpoint(deposit.hash(), 0).hex()]: deposit.outputs[0].toJSON(),
},
};

const transfer = Tx.transfer(
[new Input(new Outpoint(deposit.hash(), 0))],
[new Output('1001', ADDR_1, 0), new Output(-1, ADDR_2, 0)]
).signAll(PRIV_1);

console.log(transfer);

expect(() => {
checkTransfer(state, transfer, {});
}).toThrow('One of the outs has value < 1');
});
});
2 changes: 1 addition & 1 deletion src/tx/applyTx/checkValidatorJoin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const getInitialState = () => ({

describe('checkValidatorJoin', () => {
test('wrong type', () => {
const tx = Tx.deposit(0, 0, ADDR_1);
const tx = Tx.deposit(1, '234345', ADDR_1);
expect(() => checkValidatorJoin({}, tx)).toThrow(
'validatorJoin tx expected'
);
Expand Down
2 changes: 1 addition & 1 deletion src/tx/applyTx/checkValidatorLogout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const TENDER_KEY_2 = '0x0000069D9EDB21592CBDF4CC49956EA53E59656FC2D8BBD1AE3F427B

describe('checkValidatorLogout', () => {
test('wrong type', () => {
const tx = Tx.deposit(0, 0, ADDR_1);
const tx = Tx.deposit(1, '23445', ADDR_1);
expect(() => checkValidatorLogout({}, tx)).toThrow(
'validatorJoin tx expected'
);
Expand Down
Loading