Guest User

Untitled

a guest
Feb 21st, 2018
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. var currentState = web3.eth.getFullState([Contract address]);
  2.  
  3. function getContractState(abi_object, contract_instance)
  4. {
  5. "use strict";
  6. for (let i=0; i<abi_object.length;i++)
  7. {
  8. if (abi_object[i].constant==true &&
  9. abi_object[i].inputs.length==0&&
  10. abi_object[i].payable==false &&
  11. abi_object[i].type=="function")
  12. {
  13. console.log(abi_object[i].name+"="+contract_instance[abi_object[i].name].call());
  14. }
  15. }
  16. }
  17.  
  18. contract StateTest
  19. {
  20. uint public u;
  21. bool public b;
  22. int public i;
  23. string public s;
  24. byte public by;
  25. bytes public bs;
  26. address public a;
  27.  
  28. function StateTest()
  29. {
  30. u=1;
  31. b=true;
  32. i=-1;
  33. s="abc";
  34. by = 0x13;
  35.  
  36. bs=new bytes(2);
  37. bs[0]=0x11;
  38. bs[1]=0xff;
  39.  
  40. a = msg.sender;
  41. }
  42. }
  43.  
  44. a=0xa90fa13d754131a31cde816fa8ce7ad222d4a246
  45. b=true
  46. bs=0x11ff
  47. by=0x13
  48. s=abc
  49. u=1
  50. i=-1
  51.  
  52. var Trie = require('merkle-patricia-tree');
  53. var rlp = require('rlp');
  54. var levelup = require('levelup');
  55. var leveldown = require('leveldown');
  56. var db = levelup(leveldown('/your_home_dir/Library/Ethereum/rinkeby/geth/chaindata'));
  57. var keccak256 = require('js-sha3').keccak256;
  58.  
  59. // the block state root, rinkeby, block number 1775804
  60. // the block state root can be obtained by invoking web3.eth.getBlock(<blockNumber>) in `stateRoot` field
  61. var root = '0xe4a6ff741ec2e0d0cd274a745756028df27312161bdb4557b6da434349f716a9';
  62. var trie = new Trie(db, root);
  63.  
  64. trie.checkRoot(root, function (err, val) {
  65. console.log('Root exists:', val);
  66. });
  67.  
  68. var address = '398A7A69f3c59181A1ffe34bed11DCb5DF863A8a';
  69. var addressHash = keccak256(new Buffer(address, 'hex'));
  70.  
  71. trie.get('0x' + addressHash, function (err, val) {
  72. var decodedVal = rlp.decode(val);
  73. console.log(decodedVal);
  74.  
  75. if (!decodedVal || decodedVal.length < 4) {
  76. console.log('The value for the address must be an array of 4 elements');
  77. return;
  78. }
  79.  
  80. // 3rd element in the array is storage root, 1st - nonce, 2nd - balance, 4th - codeHash
  81. var storageRoot = decodedVal[2];
  82. console.log('storageRoot', storageRoot);
  83.  
  84. trie.root = storageRoot;
  85.  
  86. trie.checkRoot(storageRoot, function (err, val) {
  87. console.log('Storage root exists:', val);
  88. });
  89.  
  90. // Read storage slot with index 0
  91.  
  92. var slotZeroHash = keccak256(new Buffer('0000000000000000000000000000000000000000000000000000000000000000', 'hex'));
  93. trie.get('0x' + slotZeroHash, function (err, val) {
  94. var decodedVal = rlp.decode(val);
  95. console.log('Value at slot 0: ', decodedVal);
  96. });
  97.  
  98. // Read all entries from contract storage
  99.  
  100. var stream = trie.createReadStream();
  101.  
  102. stream.on('data', function (data) {
  103. console.log('key:' + data.key.toString('hex'));
  104.  
  105. // values are rlp encoded
  106. var decodedVal = rlp.decode(data.value);
  107. console.log(decodedVal);
  108. });
  109.  
  110. stream.on('end', function (val) {
  111. console.log('done reading!');
  112. });
  113. });
  114.  
  115. Root exists: true
  116. Account data: 398A7A69f3c59181A1ffe34bed11DCb5DF863A8a [ <Buffer 01>,
  117. <Buffer >,
  118. <Buffer 24 21 83 63 90 de b4 32 ce 0e ac fe 5f 49 be 88 99 17 bf 8a fa 07 72 24 1c 30 9e 61 e0 4a 0d 42>,
  119. <Buffer 61 60 55 49 c9 7c 3e 7a d6 68 63 3b 72 b2 60 d3 00 5e ab be f3 22 a2 d6 33 2a 49 76 80 89 77 7a> ]
  120. Storage root: <Buffer 24 21 83 63 90 de b4 32 ce 0e ac fe 5f 49 be 88 99 17 bf 8a fa 07 72 24 1c 30 9e 61 e0 4a 0d 42>
  121. Storage root exists: true
  122. Value at slot 0 - key: 290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563
  123. <Buffer 40 9f 9c d8 52 a3 ba e9 52 5d 2a aa>
  124. key:0205d9ce8b4a26409d40486b0ac7b8dc356714e840016b19cc5c0f2c8adbcd74
  125. <Buffer 36 35 c9 ad c5 de a0 00 00>
  126. key:0249d346d51fad5ef0b6fae89b4907e63c831f4f8af088d602baef47cda4eab7
  127. <Buffer 0a 07 64 07 d3 f7 44 00 00>
Add Comment
Please, Sign In to add comment