ethereumjs / ethereumjs-lib

[DEPRECATED] A JavaScript library of core Ethereum functions
442 stars 102 forks source link

Couldn't create a contract in VM #108

Closed asinyagin closed 9 years ago

asinyagin commented 9 years ago

Hey,

I've reproduced the problem with a contract in VM. The code below creates a contract with ethereumjs-lib 0.1.10 and doesn't create it with 0.3.1 (the full source code is here https://github.com/asinyagin/ethereumjs-test):

...
var accountKey = 'cow';

// Works good with this code:
//var code = '60606040526001600060005055600a8060196000396000f30060606040526008565b00';
// and doesn't work with this one:
var code = '60606040527f526f6d616e00000000000000000000000000000000000000000000000000000060006000505573cd2a3d9f938e13cd947ec05abc7fe734df8dd826600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff02191690830217905550620f4240600260005055608d806100826000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900480638a19e3551460415780639e80f2ca14605257603f565b005b60506004803590602001506063565b005b60616004803590602001506078565b005b80600260005054016002600050819055505b50565b80600260005054036002600050819055505b5056';

async.waterfall([
  createVM,
  createAccount,
  //watchLog,
  createContract,
  printVM
], function(err) {
  if (err) console.error(err);
  else console.log('done!');
});
...
function createContract(vm, cb) {
  var tx = new Transaction();
  tx.nonce = 0;
  tx.to = null;
  tx.gasPrice = 1000;
  tx.gasLimit = 100000;
  tx.data = new Buffer(code, 'hex');
  tx.sign(new Buffer(sha3(accountKey), 'hex'));
  vm.runTx({ tx: tx }, function(err, results) {
    if (err) return cb(err);
    //console.log(results);
    cb(null, vm);
  });
}

function printVM(vm, cb) {
  var stream = vm.trie.createReadStream();
  stream.on('data', function(data) {
    var account = new Account(data.value);
    account.getCode(vm.trie, function(err, code) {
      console.log(data.key.toString('hex') + ' => ' + code.toString('hex'));
    });
  });
  stream.on('end', cb.bind(null, null, vm));
}
...

Output:

alexander@mercury:~/Documents/ethergit/ethereumjs-test$ node app.js
77045e71a7a2c50903d88e564cd72fab11e82051 => 
cd2a3d9f938e13cd947ec05abc7fe734df8dd826 => 
done!

With var code = '60606040526001600060005055600a8060196000396000f30060606040526008565b00':

alexander@mercury:~/Documents/ethergit/ethereumjs-test$ node app.js
77045e71a7a2c50903d88e564cd72fab11e82051 => 60606040526008565b00
cd2a3d9f938e13cd947ec05abc7fe734df8dd826 => 
done!
wanderer commented 9 years ago

that is because the second code exmple does return any code. There no contract is created.

wanderer commented 9 years ago

I created a PR on your repo. explaining more. But feel free to ask question or ask for features here.

romanman commented 9 years ago

@wanderer : I have tried to run that contract in javaand it really return something to create here is the trace:

http://pastebin.com/raw.php?i=LeJTabSh

would you like to double check it