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

Dynamic gas limit for the deployment/configuration transactions #178

Merged
merged 4 commits into from
May 3, 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
2 changes: 1 addition & 1 deletion deploy/.env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#BRIDGE_MODE=ERC_TO_ERC
BRIDGE_MODE=NATIVE_TO_ERC
DEPLOYMENT_ACCOUNT_PRIVATE_KEY=67..14
DEPLOYMENT_GAS_LIMIT=5000000
DEPLOYMENT_GAS_LIMIT_EXTRA=0.2
HOME_DEPLOYMENT_GAS_PRICE=10000000000
FOREIGN_DEPLOYMENT_GAS_PRICE=10000000000
GET_RECEIPT_INTERVAL_IN_MILLISECONDS=3000
Expand Down
18 changes: 12 additions & 6 deletions deploy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ BRIDGE_MODE=NATIVE_TO_ERC
# deployments and initial configuration. The account's balance must contain
# funds from both networks.
DEPLOYMENT_ACCOUNT_PRIVATE_KEY=67..14
# The "gas" parameter set in every deployment/configuration transaction.
DEPLOYMENT_GAS_LIMIT=4000000
# Extra gas added to the estimated gas of a particular deployment/configuration transaction
# E.g. if estimated gas returns 100000 and the parameter is 0.2,
# the transaction gas limit will be (100000 + 100000 * 0.2) = 120000
DEPLOYMENT_GAS_LIMIT_EXTRA=0.2
# The "gasPrice" parameter set in every deployment/configuration transaction on
# Home network (in Wei).
HOME_DEPLOYMENT_GAS_PRICE=10000000000
Expand Down Expand Up @@ -152,8 +154,10 @@ BRIDGE_MODE=ERC_TO_ERC
# deployments and initial configuration. The account's balance must contain
# funds from both networks.
DEPLOYMENT_ACCOUNT_PRIVATE_KEY=67..14
# The "gas" parameter set in every deployment/configuration transaction.
DEPLOYMENT_GAS_LIMIT=4000000
# Extra gas added to the estimated gas of a particular deployment/configuration transaction
# E.g. if estimated gas returns 100000 and the parameter is 0.2,
# the transaction gas limit will be (100000 + 100000 * 0.2) = 120000
DEPLOYMENT_GAS_LIMIT_EXTRA=0.2
# The "gasPrice" parameter set in every deployment/configuration transaction on
# Home network (in Wei).
HOME_DEPLOYMENT_GAS_PRICE=10000000000
Expand Down Expand Up @@ -266,8 +270,10 @@ BRIDGE_MODE=ERC_TO_NATIVE
# deployments and initial configuration. The account's balance must contain
# funds from both networks.
DEPLOYMENT_ACCOUNT_PRIVATE_KEY=67..14
# The "gas" parameter set in every deployment/configuration transaction.
DEPLOYMENT_GAS_LIMIT=4000000
# Extra gas added to the estimated gas of a particular deployment/configuration transaction
# E.g. if estimated gas returns 100000 and the parameter is 0.2,
# the transaction gas limit will be (100000 + 100000 * 0.2) = 120000
DEPLOYMENT_GAS_LIMIT_EXTRA=0.2
# The "gasPrice" parameter set in every deployment/configuration transaction on
# Home network (in Wei).
HOME_DEPLOYMENT_GAS_PRICE=10000000000
Expand Down
1 change: 1 addition & 0 deletions deploy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"bignumber.js": "^7.2.1",
"dotenv": "^5.0.1",
"envalid": "^4.1.4",
"ethereumjs-tx": "^1.3.4",
Expand Down
34 changes: 31 additions & 3 deletions deploy/src/deploymentUtils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const BigNumber = require('bignumber.js')
const Web3 = require('web3')
const Tx = require('ethereumjs-tx')
const Web3Utils = require('web3-utils')
Expand All @@ -9,7 +10,7 @@ const {
deploymentPrivateKey,
FOREIGN_RPC_URL,
HOME_RPC_URL,
GAS_LIMIT,
GAS_LIMIT_EXTRA,
HOME_DEPLOYMENT_GAS_PRICE,
FOREIGN_DEPLOYMENT_GAS_PRICE,
GET_RECEIPT_INTERVAL_IN_MILLISECONDS
Expand Down Expand Up @@ -70,10 +71,30 @@ async function sendRawTxForeign(options) {

async function sendRawTx({ data, nonce, to, privateKey, url, gasPrice, value }) {
try {
const txToEstimateGas = {
from: privateKeyToAddress(Web3Utils.bytesToHex(privateKey)),
value: value,
to,
data
}
const estimatedGas = BigNumber(await sendNodeRequest(url, 'eth_estimateGas', txToEstimateGas))

const blockData = await sendNodeRequest(url, 'eth_getBlockByNumber', ["latest", false])
const blockGasLimit = BigNumber(blockData.gasLimit)
if (estimatedGas.isGreaterThan(blockGasLimit)) {
throw new Error(`estimated gas greater (${estimatedGas.toString()}) than the block gas limit (${blockGasLimit.toString()})`)
}
let gas = estimatedGas.multipliedBy(BigNumber(1 + GAS_LIMIT_EXTRA))
if (gas.isGreaterThan(blockGasLimit)) {
gas = blockGasLimit
} else {
gas = gas.toFixed(0)
}

const rawTx = {
nonce,
gasPrice: Web3Utils.toHex(gasPrice),
gasLimit: Web3Utils.toHex(GAS_LIMIT),
gasLimit: Web3Utils.toHex(gas),
to,
data,
value
Expand All @@ -96,6 +117,9 @@ async function sendRawTx({ data, nonce, to, privateKey, url, gasPrice, value })
}

async function sendNodeRequest(url, method, signedData) {
if (! Array.isArray(signedData)) {
signedData = [signedData]
}
const request = await fetch(url, {
headers: {
'Content-type': 'application/json'
Expand All @@ -104,15 +128,19 @@ async function sendNodeRequest(url, method, signedData) {
body: JSON.stringify({
jsonrpc: '2.0',
method,
params: [signedData],
params: signedData,
id: 1
})
})
const json = await request.json()
if (typeof json.error === 'undefined' || json.error === null) {
if (method === 'eth_sendRawTransaction') {
assert.strictEqual(json.result.length, 66, `Tx wasn't sent ${json}`)
}
return json.result
} else {
throw new Error(`web3 RPC failed: ${JSON.stringify(json.error)}`)
}
}

function timeout(ms) {
Expand Down
2 changes: 1 addition & 1 deletion deploy/src/loadEnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if (!validBridgeModes.includes(BRIDGE_MODE)) {

let validations = {
DEPLOYMENT_ACCOUNT_PRIVATE_KEY: envalid.str(),
DEPLOYMENT_GAS_LIMIT: bigNumValidator(),
DEPLOYMENT_GAS_LIMIT_EXTRA: envalid.num(),
HOME_DEPLOYMENT_GAS_PRICE: bigNumValidator(),
FOREIGN_DEPLOYMENT_GAS_PRICE: bigNumValidator(),
GET_RECEIPT_INTERVAL_IN_MILLISECONDS: bigNumValidator(),
Expand Down
4 changes: 2 additions & 2 deletions deploy/src/web3.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const foreignProvider = new Web3.providers.HttpProvider(FOREIGN_RPC_URL)
const web3Foreign = new Web3(foreignProvider)

const { HOME_DEPLOYMENT_GAS_PRICE, FOREIGN_DEPLOYMENT_GAS_PRICE } = env
const GAS_LIMIT = env.DEPLOYMENT_GAS_LIMIT
const GAS_LIMIT_EXTRA = env.DEPLOYMENT_GAS_LIMIT_EXTRA

const deploymentPrivateKey = Buffer.from(DEPLOYMENT_ACCOUNT_PRIVATE_KEY, 'hex')

Expand All @@ -25,7 +25,7 @@ module.exports = {
deploymentPrivateKey,
HOME_RPC_URL,
FOREIGN_RPC_URL,
GAS_LIMIT,
GAS_LIMIT_EXTRA,
HOME_DEPLOYMENT_GAS_PRICE,
FOREIGN_DEPLOYMENT_GAS_PRICE,
GET_RECEIPT_INTERVAL_IN_MILLISECONDS
Expand Down