Skip to content

Instantly share code, notes, and snippets.

@masonforest masonforest/run.js
Created Sep 11, 2018

Embed
What would you like to do?
Run transaction at block #177610 in ethereumjs-vm
var VM = require('./lib');
const SecureTrie = require('merkle-patricia-tree/secure')
var Account = require('ethereumjs-account');
const StateManager = require('./lib/stateManager.js');
const utils = require('ethereumjs-util')
const rlp = utils.rlp
const ethUtil = require('ethereumjs-util')
async function run() {
const newAddress = ethUtil.generateAddress(new Buffer('0f572e5295c57f15886f9b263e2f6d2d6c7b5ec6', 'hex'), [0]);
// console.log(newAddress.toString('hex'));
//create a new VM instance
var code = '6060604052361561007f576000357c0100000000000000000000000000000000000000000000000000000000900480630efafd011461008157806325d8dcf2146100a257806341c0e1b5146100af5780635acce36b146100bc5780636c6f1d93146100dd578063cee6f93c146100fe578063efa6358f146101775761007f565b005b61008c6004506107d8565b6040518082815260200191505060405180910390f35b6100ad6004506101ce565b005b6100ba6004506107ea565b005b6100c76004506101aa565b6040518082815260200191505060405180910390f35b6100e8600450610198565b6040518082815260200191505060405180910390f35b61010960045061075a565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600302600f01f150905090810190601f1680156101695780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610182600450610748565b6040518082815260200191505060405180910390f35b600060016000505490506101a7565b90565b60003073ffffffffffffffffffffffffffffffffffffffff163190506101cb565b90565b6000600060023073ffffffffffffffffffffffffffffffffffffffff1631026002600050819055506fffffffffffffffffffffffffffffffff34111561030c57604060405190810160405280600f81526020017f776167657220746f6f206c6172676500000000000000000000000000000000008152602001506004600050908051906020019082805482825590600052602060002090601f01602090048101928215610298579182015b82811115610297578251826000505591602001919060010190610279565b5b5090506102c391906102a5565b808211156102bf57600081815060009055506001016102a5565b5090565b505060006003600050819055503373ffffffffffffffffffffffffffffffffffffffff16600034604051809050600060405180830381858888f19350505050506107445661050f565b60026000505434111561043d57606060405190810160405280602b81526020017f7761676572206c6172676572207468616e20636f6e747261637427732061626981526020017f6c69747920746f207061790000000000000000000000000000000000000000008152602001506004600050908051906020019082805482825590600052602060002090601f016020900481019282156103c9579182015b828111156103c85782518260005055916020019190600101906103aa565b5b5090506103f491906103d6565b808211156103f057600081815060009055506001016103d6565b5090565b505060006003600050819055503373ffffffffffffffffffffffffffffffffffffffff16600034604051809050600060405180830381858888f19350505050506107445661050e565b600034141561050d57604060405190810160405280600e81526020017f776167657220776173207a65726f0000000000000000000000000000000000008152602001506004600050908051906020019082805482825590600052602060002090601f016020900481019282156104d0579182015b828111156104cf5782518260005055916020019190600101906104b1565b5b5090506104fb91906104dd565b808211156104f757600081815060009055506001016104dd565b5090565b50506000600360005081905550610744565b5b5b34915061051b8261087e565b90506000600282061415610627577fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff826fffffffffffffffffffffffffffffffff1602600360005081905550604060405190810160405280600481526020017f6c6f7373000000000000000000000000000000000000000000000000000000008152602001506004600050908051906020019082805482825590600052602060002090601f016020900481019282156105f1579182015b828111156105f05782518260005055916020019190600101906105d2565b5b50905061061c91906105fe565b8082111561061857600081815060009055506001016105fe565b5090565b505061074456610743565b816fffffffffffffffffffffffffffffffff16600360005081905550604060405190810160405280600381526020017f77696e00000000000000000000000000000000000000000000000000000000008152602001506004600050908051906020019082805482825590600052602060002090601f016020900481019282156106cd579182015b828111156106cc5782518260005055916020019190600101906106ae565b5b5090506106f891906106da565b808211156106f457600081815060009055506001016106da565b5090565b50503373ffffffffffffffffffffffffffffffffffffffff166000600284026fffffffffffffffffffffffffffffffff16604051809050600060405180830381858888f19350505050505b5b5050565b60006002600050549050610757565b90565b60206040519081016040528060008152602001506004600050805480601f016020809104026020016040519081016040528092919081815260200182805480156107c957820191906000526020600020905b8154815290600101906020018083116107ac57829003601f168201915b505050505090506107d5565b90565b600060036000505490506107e7565b90565b600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff16141561087b57600060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b5b565b600044414284604051808581526020018473ffffffffffffffffffffffffffffffffffffffff166c01000000000000000000000000028152601401838152602001826fffffffffffffffffffffffffffffffff167001000000000000000000000000000000000281526010019450505050506040518091039020600190049050610903565b91905056'
//
//code needs to be a buffer
code = new Buffer(code, 'hex')
const util = require('util');
var trie = new SecureTrie();
// var stateManager = new StateManager({});
// stateManager.trie.checkpoint()
var account = new Account();
var originAccount = new Account();
const address = Buffer.from("00bca629f698d95a3ab6a2b379cac78c952eb75c", "hex");
const origin = Buffer.from("cf684dfb8304729355b58315e8019b1aa2ad1bac", "hex");
const key1 = Buffer.from("8a35acfbc15ff81a39ae7d344fd709f28e8600b4aa8c65c6b64bfe7fe36bd19b", "hex");
const value1 = rlp.encode(Buffer.from("6e6f207761676572732079657400000000000000000000000000000000000000", "hex"));
const key2 = Buffer.from("0000000000000000000000000000000000000000000000000000000000000004", "hex");
const value2 = rlp.encode(Buffer.from("000000000000000000000000000000000000000000000000000000000000000d", "hex"));
// const setStorage = util.promisify(account.setStorage);
// const stateTriePut = util.promisify(trie.put);
originAccount.balance = '0x3782dace9d900000'
// originAccount.balance = '0x3782dace9d900000'
account.setStorage(trie, key1, value1, function (err, value) {
account.setStorage(trie, key2, value2, function (err, value) {
// await setStorage(trie, key1, value1);
// await setStorage(trie, key2, value2);
trie.put(address, account.serialize(), () => {
trie.put(origin, originAccount.serialize(), () => {
trie.get(address, function (err, val) {
var account = new Account(val)
account.getStorage(trie, key2, function (err, value) {
console.log(`Value ${value.toString('hex')} set and retrieved from trie.`)
})
var vm = new VM({
state: trie,
})
vm.runCode({
code: code,
origin: Buffer.from("cf684dfb8304729355b58315e8019b1aa2ad1bac", "hex"),
address: Buffer.from("00bca629f698d95a3ab6a2b379cac78c952eb75c", "hex"),
caller: Buffer.from("cf684dfb8304729355b58315e8019b1aa2ad1bac", "hex"),
value: 3000000000000000000,
data: Buffer.from("25d8dcf2","hex"),
gasLimit: 68728,
}, (error, results) => {
console.log(error)
console.log(results)
})
storageRoot = account.stateRoot // used later! :)
console.log('------results------')
console.log('nonce: ' + account.nonce.toString('hex'))
console.log('balance in wei: ' + account.balance.toString('hex'))
console.log('stateRoot: ' + storageRoot.toString('hex'))
console.log('codeHash:' + account.codeHash.toString('hex'))
console.log('-------------------')
})
})
})
})
})
//
// await putContractStorage(address, key1, value1);
// await putContractStorage(address, key2, value2);
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.