Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
RootChain,
ERC20Validator
} from '../helpers/contracts'
let ChildChain = artifacts.require('../child/ChildChain.sol')
let ChildToken = artifacts.require('../child/ChildERC20.sol')
const web3Child = new web3.constructor(
new web3.providers.HttpProvider('http://localhost:8546')
)
ChildChain.web3 = web3Child
ChildToken.web3 = web3Child
const BN = utils.BN
const rlp = utils.rlp
// const printReceiptEvents = receipt => {
// receipt.logs.forEach(l => {
// console.log(l.event, JSON.stringify(l.args))
// })
// }
contract('ERC20Validator', async function(accounts) {
describe('initialization', async function() {
let stakeToken
let rootToken
let childToken
let rootChain
let stakeManager
let wallets
let childChain
import Trie from 'merkle-patricia-tree'
import utils from 'ethereumjs-util'
import EthereumTx from 'ethereumjs-tx'
import EthereumBlock from 'ethereumjs-block/from-rpc'
import MerkleTree from '../helpers/merkle-tree.js'
const rlp = utils.rlp
// raw header
function getRawHeader(_block) {
if (typeof _block.difficulty !== 'string') {
_block.difficulty = '0x' + _block.difficulty.toString(16)
}
const block = new EthereumBlock(_block)
return block.header
}
// squanch transaction
export function squanchTx(tx) {
tx.gasPrice = '0x' + parseInt(tx.gasPrice).toString(16)
tx.value = '0x' + parseInt(tx.value).toString(16) || '0'
tx.gas = '0x' + parseInt(tx.gas).toString(16)
const Buffer = require('safe-buffer').Buffer
const async = require('async')
const ethUtil = require('ethereumjs-util')
const Bloom = require('./bloom.js')
const common = require('ethereum-common')
const rlp = ethUtil.rlp
const Trie = require('merkle-patricia-tree')
const BN = ethUtil.BN
const minerReward = new BN(common.minerReward.v)
/**
* process the transaction in a block and pays the miners
* @param opts
* @param opts.block {Block} the block we are processing
* @param opts.generate {Boolean} [gen=false] whether to generate the stateRoot
* @param cb {Function} the callback which is given an error string
*/
module.exports = function (opts, cb) {
const self = this
// parse options
import utils from 'ethereumjs-util'
import {Buffer} from 'safe-buffer'
import chain from './index'
import Transaction from './transaction'
import config from '../config'
const rlp = utils.rlp
export async function getAllUTXOs(address) {
if (!address || !utils.isValidAddress(address)) {
return []
}
const from = Buffer.concat([
config.prefixes.utxo,
utils.toBuffer(address),
Buffer.alloc(32), // block number
Buffer.alloc(32), // tx index
Buffer.alloc(32) // output index
])
const to = Buffer.concat([
config.prefixes.utxo,
txNumberInBlock: 0,
outputNumberInTransaction: 0,
amount: 1
}],
[{
amount: 100,
to: alice
}],
operatorKey
)
let block = createBlock(1, 1, firstHash, [tx], operatorKey)
const reencodedTX = tx.serialize();
const proof = block.merkleTree.getProof(0, true);
let blockArray = block.serialize();
let blockHeader = Buffer.concat(blockArray).slice(0,137);
let deserialization = ethUtil.rlp.decode(blockArray[7]);
let lastBlockNumber = await plasma.lastBlockNumber()
assert(lastBlockNumber.toString() == "0");
let submissionReceipt = await plasma.submitBlockHeaders(ethUtil.bufferToHex(blockHeader));
lastBlockNumber = await plasma.lastBlockNumber();
assert(lastBlockNumber.toString() == "1");
let allEvents = storage.allEvents({fromBlock: submissionReceipt.receipt.blockNumber, toBlock: submissionReceipt.receipt.blockNumber});
let get = util.promisify(allEvents.get.bind(allEvents))
let evs = await get()
assert.web3Event({logs: evs}, {
event: 'BlockHeaderSubmitted',
args: {_blockNumber: 1,
_merkleRoot: ethUtil.bufferToHex(block.header.merkleRootHash)}
}, 'The event is emitted');
let bl = await storage.blocks(1);
assert(bl[2] == ethUtil.bufferToHex(block.header.merkleRootHash));
tape('should run valid block', async (t) => {
const vm = setupVM()
const suite = setup(vm)
const genesis = new Block(util.rlp.decode(suite.data.genesisRLP))
const block = new Block(util.rlp.decode(suite.data.blocks[0].rlp))
const setupPreP = promisify(setupPreConditions)
await setupPreP(suite.vm.stateManager._trie, suite.data)
t.equal(
suite.vm.stateManager._trie.root.toString('hex'),
genesis.header.stateRoot.toString('hex'),
'genesis state root should match calculated state root'
)
let res = await suite.p.runBlock({ block, root: suite.vm.stateManager._trie.root, skipBlockValidation: true })
t.error(res.error, 'runBlock shouldn\'t have returned error')
t.equal(res.results[0].gasUsed.toString('hex'), '5208', 'actual gas used should equal blockHeader gasUsed')
t.end()
return trie.findPath(path, function(error, accountNode, keyRemainder, rootToLeafPath) {
if (error || !accountNode || keyRemainder.length > 0) {
return reject({ error: 'account_node_not_found' });
}
let parentNodes = rootToLeafPath.map((node) => node.raw),
proof = {
address: address,
parentNodes: ethUtils.rlp.encode(parentNodes).toString('hex'),
value: accountNode.value.toString('hex')
};
return resolve(proof);
});
});
const encode = (input) => {
return input === "0x0" ? Util.rlp.encode(Buffer.from([])) : Util.rlp.encode(input)
}
const decode = Util.rlp.decode
export function signTxLedger(app, eTx, rawTx, txData, old, callback) {
eTx.raw[6] = Buffer.from([1]); //ETH chain id
eTx.raw[7] = eTx.raw[8] = 0;
var toHash = old ? eTx.raw.slice(0, 6) : eTx.raw;
var txToSign = ethUtil.rlp.encode(toHash);
var localCallback = function(result, error) {
if (typeof error !== "undefined") {
if (callback !== undefined) callback({
isError: true,
error: error
});
return;
}
rawTx.v = "0x" + result['v'];
rawTx.r = "0x" + result['r'];
rawTx.s = "0x" + result['s'];
eTx = new ethUtil.Tx(rawTx);
rawTx.rawTx = JSON.stringify(rawTx);
rawTx.signedTx = '0x' + eTx.serialize().toString('hex');
rawTx.isError = false;
if (callback !== undefined) callback(rawTx);
getTxProof,
getReceiptProof,
verifyTxProof,
verifyReceiptProof,
verifyHeaderProof,
} from './helpers/proofs'
import { getHeaders, getBlockHeader } from './helpers/blocks'
import MerkleTree from './helpers/merkle-tree'
import RootChainArtifacts from '../artifacts/RootChain'
import ChildERC20Artifacts from '../artifacts/ChildERC20'
import ChildERC721Artifacts from '../artifacts/ChildERC721'
import WithdrawManagerArtifacts from '../artifacts/WithdrawManager'
import DepositManagerArtifacts from '../artifacts/DepositManager'
const rlp = utils.rlp
export default class Matic {
constructor(options = {}) {
this._throwIfNull(options.maticProvider, 'maticProvider is required')
this._throwIfNull(options.parentProvider, 'parentProvider is required')
this._web3 = new Web3(options.maticProvider)
this._web3.matic = true
this._parentWeb3 = new Web3(options.parentProvider)
this._syncerUrl = options.syncerUrl
this._watcherUrl = options.watcherUrl
this._rootChainAddress = options.rootChainAddress
this._maticWethAddress = options.maticWethAddress
this._withdrawManagerAddress = options.withdrawManagerAddress
this._depositManagerAddress = options.depositManagerAddress