Skip to content

Commit

Permalink
Add: new storage, epoch height, nonce logic
Browse files Browse the repository at this point in the history
  • Loading branch information
yqrashawn committed Apr 16, 2020
1 parent 0036f0f commit 4574f96
Show file tree
Hide file tree
Showing 38 changed files with 405 additions and 226 deletions.
1 change: 1 addition & 0 deletions app/scripts/controllers/network/createCfxMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function alterRpcMethodAndParams ({ method = '', params = {} } = {}) {
if (method) {
method = method.replace('eth_', 'cfx_')
method = method.replace('getTransactionCount', 'getNextNonce')
method = method.replace(/estimateGas$/, 'estimateGasAndCollateral')
method = method.replace('getBlockByNumber', 'getBlockByEpochNumber')
method = method.replace('cfx_blockNumber', 'cfx_epochNumber')
}
Expand Down
4 changes: 4 additions & 0 deletions app/scripts/controllers/permissions/enums.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const SAFE_METHODS = [
'eth_chainId',
'eth_coinbase',
'eth_estimateGas',
'eth_estimateGasAndCollateral',
'eth_gasPrice',
'eth_getBalance',
'eth_getBlockByHash',
Expand All @@ -41,6 +42,7 @@ export const SAFE_METHODS = [
'eth_getTransactionByBlockNumberAndIndex',
'eth_getTransactionByHash',
'eth_getTransactionCount',
'eth_getNextNonce',
'eth_getTransactionReceipt',
'eth_getUncleByBlockHashAndIndex',
'eth_getUncleByBlockNumberAndIndex',
Expand Down Expand Up @@ -73,6 +75,7 @@ export const SAFE_METHODS = [
'cfx_chainId',
'cfx_coinbase',
'cfx_estimateGas',
'cfx_estimateGasAndCollateral',
'cfx_gasPrice',
'cfx_getBalance',
'cfx_getEpochByHash',
Expand All @@ -88,6 +91,7 @@ export const SAFE_METHODS = [
'cfx_getTransactionByEpochNumberAndIndex',
'cfx_getTransactionByHash',
'cfx_getTransactionCount',
'cfx_getNextNonce',
'cfx_getTransactionReceipt',
'cfx_getUncleByEpochHashAndIndex',
'cfx_getUncleByEpochNumberAndIndex',
Expand Down
3 changes: 2 additions & 1 deletion app/scripts/controllers/transactions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ class TransactionController extends EventEmitter {
this.txStateManager.setTxStatusApproved(txId)
// get next nonce
const txMeta = this.txStateManager.getTx(txId)
txMeta.txParams.epochHeight = await this.blockTracker.getLatestBlock()
const fromAddress = txMeta.txParams.from
// wait for a nonce
let { customNonceValue = null } = txMeta
Expand Down Expand Up @@ -772,7 +773,7 @@ class TransactionController extends EventEmitter {
let code
if (!result) {
try {
code = await this.query.getCode(to)
code = to.startsWith('0x1') ? null : await this.query.getCode(to)
} catch (e) {
code = null
// conflux fullnode will return a error here if it's not a contract addr
Expand Down
20 changes: 10 additions & 10 deletions app/scripts/controllers/transactions/lib/recipient-blacklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ export default {
// IDEX phisher
'0x9bcb0A9d99d815Bb87ee3191b1399b1Bcc46dc77',
// Ganache default seed phrases
"0x19c2b5c29e4e3ba1ce804885644316ce7f8607a2",
"0x1d97c764d79f14172d0163438aa4df97aa6ddaf7",
"0x15b539d2f3ef6aba2e78ba48cf8f29fad40cd87b",
"0x1e9fda4236acff0b561a05f8ab03919e5c5abba8",
"0x19396f8115517e26fa78ec7f1ee73681dd3d1e54",
"0x1fc6b6c2c0bebae12a91e3665813ac9e5018589b",
"0x1de6cda2345564a14e78c8404882757c052adbad",
"0x1c6f7bc52b671c036a5a64318046276e2ece8177",
"0x122c470c08a1944a527a4bddd263121a43fed76f",
"0x18a577c7a551818c8e73f0e4c08c90e3b5c861c9"
'0x102b422446a6786e166e7591ed0fad8a272f6f49',
'0x15a17ec00f5a77213b9aa398d80732e98379658c',
'0x1d16aa6ade9ef09b56891407ac40be2d28e64ea0',
'0x101b532ac74669d54016b6350df21403241bee32',
'0x1cdc8dccfb521bd409e6c5326848cb128b223a81',
'0x187c61604d245eaad2909bbd3de34249177bcdce',
'0x1cec65a4e1d77a5f25b88c75e9a11a900121f7a8',
'0x1c1223fdb4334194269429d3d9537acfb9be7b91',
'0x1c9ffa3c7735df2832a417956c61fc48354583b3',
'0x19893da7563781460bdceb60552a6e39b9fbb133',
],
}
2 changes: 2 additions & 0 deletions app/scripts/controllers/transactions/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const normalizers = {
data: (data) => addHexPrefix(data),
gas: (gas) => addHexPrefix(gas),
gasPrice: (gasPrice) => addHexPrefix(gasPrice),
storageLimit: (storageLimit) => addHexPrefix(storageLimit),
epochHeight: (epochHeight) => addHexPrefix(epochHeight.toString(16)),
}

/**
Expand Down
43 changes: 37 additions & 6 deletions app/scripts/controllers/transactions/tx-gas-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { addHexPrefix } from 'cfx-util'
import { SEND_ETHER_ACTION_KEY } from '../../../../ui/app/helpers/constants/transactions.js'

const SIMPLE_GAS_COST = '0x5208' // Hex for 21000, cost of a simple send.
const SIMPLE_STORAGE_COST = '0x0' // Hex for 0, cost of a simple send.

import { TRANSACTION_NO_CONTRACT_ERROR_KEY } from '../../../../ui/app/helpers/constants/error-keys'

Expand All @@ -27,12 +28,18 @@ class TxGasUtil {
async analyzeGasUsage (txMeta, getCodeResponse) {
const block = await this.query.getBlockByNumber('latest', false)
let estimatedGasHex
let estimatedStorageHex
try {
estimatedGasHex = await this.estimateTxGas(
const {
gasUsed,
storageCollateralized,
} = await this.estimateTxGas(
txMeta,
block.gasLimit,
getCodeResponse
)
estimatedGasHex = gasUsed
estimatedStorageHex = storageCollateralized
} catch (err) {
log.warn(err)
txMeta.simulationFails = {
Expand All @@ -47,12 +54,15 @@ class TxGasUtil {

return txMeta
}
this.setTxGas(txMeta, block.gasLimit, estimatedGasHex)
this.setTxGas(txMeta, block.gasLimit, {
estimatedGasHex,
estimatedStorageHex,
})
return txMeta
}

/**
Estimates the tx's gas usage
Estimates the tx's gas/storage usage
@param {Object} txMeta - the txMeta object
@param {string} blockGasLimitHex - hex string of the block's gas limit
@returns {string} - the estimated gas limit as a hex string
Expand All @@ -62,6 +72,7 @@ class TxGasUtil {

// check if gasLimit is already specified
txMeta.gasLimitSpecified = Boolean(txParams.gas)
txMeta.storageLimitSpecified = Boolean(txParams.storageLimit)

// if it is, use that value
if (txMeta.gasLimitSpecified) {
Expand Down Expand Up @@ -109,28 +120,48 @@ class TxGasUtil {
}

/**
Writes the gas on the txParams in the txMeta
Writes the gas/stroage on the txParams in the txMeta
@param {Object} txMeta - the txMeta object to write to
@param {string} blockGasLimitHex - the block gas limit hex
@param {string} estimatedGasHex - the estimated gas hex
*/
setTxGas (txMeta, blockGasLimitHex, estimatedGasHex) {
setTxGas (
txMeta,
blockGasLimitHex,
{ estimatedGasHex, estimatedStorageHex }
) {
txMeta.estimatedGas = addHexPrefix(estimatedGasHex)
txMeta.estimatedStroage = addHexPrefix(estimatedStorageHex)
const txParams = txMeta.txParams

if (txMeta.simpleSend) {
txMeta.estimatedGas = txParams.gas
txMeta.estimatedStorage = SIMPLE_STORAGE_COST
return
}

if (txMeta.storageLimitSpecified) {
txMeta.estimatedStorage = txParams.storageLimit
}

// if gasLimit was specified and doesnt OOG,
// use original specified amount
if (txMeta.gasLimitSpecified || txMeta.simpleSend) {
if (txMeta.gasLimitSpecified) {
txMeta.estimatedGas = txParams.gas
return
}

// if gasLimit not originally specified,
// try adding an additional gas buffer to our estimation for safety
const recommendedGasHex = this.addGasBuffer(
txMeta.estimatedGas,
blockGasLimitHex
)
txParams.gas = recommendedGasHex

if (!txMeta.storageLimitSpecified) {
txParams.storageLimit = txMeta.estimatedStroage
}
return
}

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"bip39": "^2.2.0",
"bn.js": "^4.11.7",
"c3": "^0.7.10",
"cfx-keyring-controller": "^5.6.3",
"cfx-keyring-controller": "^5.6.4",
"cfx-sig-util": "^0.0.1-a",
"cfx-util": "^0.0.1-c",
"cfx-wallet": "^0.0.1-c",
Expand Down Expand Up @@ -129,7 +129,7 @@
"gaba": "^1.10.0",
"human-standard-token-abi": "^2.0.0",
"jazzicon": "^2.0.0",
"js-conflux-sdk": "^0.8.0-alpha",
"js-conflux-sdk": "^0.9.0-alpha",
"json-rpc-engine": "^5.1.6",
"json-rpc-middleware-stream": "^2.1.1",
"jsonschema": "^1.2.4",
Expand Down Expand Up @@ -303,7 +303,7 @@
},
"husky": {
"hooks": {
"pre-commit": "lint-staged"
"pre-commit": "yarn lint:changed:fix"
}
}
}
12 changes: 6 additions & 6 deletions test/unit/app/controllers/metamask-controller-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ const DEFAULT_LABEL = 'Account 1'
const DEFAULT_LABEL_2 = 'Account 2'
const TEST_SEED =
'debris dizzy just program just float decrease vacant alarm reduce speak stadium'
const TEST_ADDRESS = '0x1b398b5288001b79275ef9293835aa1a21caccfc'
const TEST_ADDRESS_2 = '0x15f81a1b497e8ba4bfa766916e9b0a56008e902a'
const TEST_ADDRESS_3 = '0x1237787c844f8ec2df6bafb3870d1aee4ffd670e'
const TEST_ADDRESS = '0x17c03dfb6cc3ceb6fc2634b906fb7abe99f254c4'
const TEST_ADDRESS_2 = '0x1e1aca25be19d60bc6e59e8e162273183dd1de0a'
const TEST_ADDRESS_3 = '0x126dcc25e6ea588971631c15512821fd5f26fd4a'
const TEST_SEED_ALT =
'setup olympic issue mobile velvet surge alcohol burger horse view reopen gentle'
const TEST_ADDRESS_ALT = '0x1cd356635e3139dfa7e7762f8306165ce978203d'
const TEST_ADDRESS_ALT = '0x1aac926ed958373188fddcaf959d492a8bddfb5d'
const CUSTOM_RPC_URL = 'http://localhost:8545'

describe('MetaMaskController', function () {
Expand Down Expand Up @@ -833,7 +833,7 @@ describe('MetaMaskController', function () {
describe('#newUnsignedPersonalMessage', function () {
let msgParams, metamaskPersonalMsgs, personalMessages, msgId

const address = '0x1cd356635e3139dfa7e7762f8306165ce978203d'
const address = '0x1aac926ed958373188fddcaf959d492a8bddfb5d'
const data = '0x43727970746f6b697474696573'

beforeEach(async function () {
Expand Down Expand Up @@ -906,7 +906,7 @@ describe('MetaMaskController', function () {
assert.equal(metamaskPersonalMsgs[msgId].status, 'signed')
assert.equal(
metamaskPersonalMsgs[msgId].rawSig,
'0xf8ef0b4b2e47c4f2bce9fe8b21d2a58ba107d6b4fcaced3f6b52c255b5d954e67873b8f5969a0e125c2b56a8bd2917f12186c43ed0471a36172744a7b7d923b600'
'0x9c6c6f9d4da11e9fde35668c9c1af13d6609bfd8b327ab59b252d3f2ce3be2c87010917270b9fc88086c415209fa8b4a9168258bb40f65fc22fe88ec6800a21401'
)
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('Recipient Blacklist Checker', function () {

it('fails for public account - uppercase', function () {
const mainnetId = 1
const publicAccount = '0X19396F8115517E26FA78EC7F1EE73681DD3D1E54'
const publicAccount = '0X1CDC8DCCFB521BD409E6C5326848CB128B223A81'
try {
recipientBlackListChecker.checkAccount(mainnetId, publicAccount)
assert.fail('function should have thrown an error')
Expand All @@ -65,7 +65,7 @@ describe('Recipient Blacklist Checker', function () {

it('fails for public account - lowercase', async function () {
const mainnetId = 1
const publicAccount = '0x19396f8115517e26fa78ec7f1ee73681dd3d1e54'
const publicAccount = '0x1cdc8dccfb521bd409e6c5326848cb128b223a81'
try {
await recipientBlackListChecker.checkAccount(mainnetId, publicAccount)
assert.fail('function should have thrown an error')
Expand Down
Loading

0 comments on commit 4574f96

Please sign in to comment.