開發以太坊遇到的幾個問題

changliangwl發表於2018-03-02

1.資料同步(根據帳號查詢歷史交易資訊)

https://ethereum.stackexchange.com/questions/2531/common-useful-javascript-snippets-for-geth/3478#3478

 

function getTransactionsByAccount(myaccount, startBlockNumber, endBlockNumber) {
  if (endBlockNumber == null) {
    endBlockNumber = eth.blockNumber;
    console.log("Using endBlockNumber: " + endBlockNumber);
  }
  if (startBlockNumber == null) {
    startBlockNumber = endBlockNumber - 1000;
    console.log("Using startBlockNumber: " + startBlockNumber);
  }
  console.log("Searching for transactions to/from account \"" + myaccount + "\" within blocks "  + startBlockNumber + " and " + endBlockNumber);

  for (var i = startBlockNumber; i <= endBlockNumber; i++) {
    if (i % 1000 == 0) {
      console.log("Searching block " + i);
    }
    var block = eth.getBlock(i, true);
    if (block != null && block.transactions != null) {
      block.transactions.forEach( function(e) {
        if (myaccount == "*" || myaccount == e.from || myaccount == e.to) {
          console.log("  tx hash          : " + e.hash + "\n"
            + "   nonce           : " + e.nonce + "\n"
            + "   blockHash       : " + e.blockHash + "\n"
            + "   blockNumber     : " + e.blockNumber + "\n"
            + "   transactionIndex: " + e.transactionIndex + "\n"
            + "   from            : " + e.from + "\n" 
            + "   to              : " + e.to + "\n"
            + "   value           : " + e.value + "\n"
            + "   time            : " + block.timestamp + " " + new Date(block.timestamp * 1000).toGMTString() + "\n"
            + "   gasPrice        : " + e.gasPrice + "\n"
            + "   gas             : " + e.gas + "\n"
            + "   input           : " + e.input);
        }
      })
    }
  }
}

 

Example

Find transactions to/from eth.accounts[0] address:

> getTransactionsByAccount(eth.accounts[0])
Using endBlockNumber: 1864
Using startBlockNumber: 864
Searching for transactions to/from account "0xa7857047907d53a2e494d5f311b4b586dc6a96d2" within blocks 864 and 1864
Searching block 1000
  tx hash          : 0x3c3bc3c456a84e20cf0077f9aa5ce363d3b12bca18d01000a750288c2e76401e
   nonce           : 44
   blockHash       : 0xef2d15775908951fc61f9a83b53c00cf2cde4e0def93e20544f784441c6178db
   blockNumber     : 1582
   transactionIndex: 0
   from            : 0xa7857047907d53a2e494d5f311b4b586dc6a96d2
   to              : null
   value           : 0
   time            : 1470459255 Sat, 06 Aug 2016 04:54:15 GMT
   gasPrice        : 20000000000
   gas             : 24615
   input           : 0x6060604052600a8060106000396000f360606040526008565b00
  tx hash          : 0xc255cdbf477452eb8922d8230889f7cc08b9deed4695378aba3d97906071ce5f
   nonce           : 45
   blockHash       : 0x987a8214af96bb1530b97fe09da8f8168679e42c9efb4defee50800f2067d6d8
   blockNumber     : 1587
   transactionIndex: 0
   from            : 0xa7857047907d53a2e494d5f311b4b586dc6a96d2
   to              : null
   value           : 0
   time            : 1470459409 Sat, 06 Aug 2016 04:56:49 GMT
   gasPrice        : 20000000000
   gas             : 24615
   input           : 0x6060604052600a8060106000396000f360606040526008565b00
...

 

 

2.根據交易hash查詢raw資料

https://ethereum.stackexchange.com/questions/7473/get-raw-transaction-from-hash

 

There is an "undocumented" method eth_getRawTransactionByHash from JSON-RPC

curl -H "Content-Type: application/json" -X POST --data \
'{"jsonrpc":"2.0","method":"eth_getRawTransactionByHash","params":["<TX_HASH>"],"id":1}' http://localhost:8545

<TX_HASH> - transaction id

curl -H "Content-Type: application/json" -X POST --data \'{"jsonrpc":"2.0","method":"eth_getRawTransactionByHash","params":["<TX_HASH>"],"id":1}' http://localhost:8545

請檢視我的另一篇文章

http://mp.blog.csdn.net/postedit/79428665

3.交易狀態的確定  

out of gas

https://etherscan.io/tx/0xaee761811a9d4957faca2e01eaea806e0de9c738f91b1fb8c9e1f963a7c3a506

解決辦法

https://ethereum.stackexchange.com/questions/23204/how-can-i-tell-from-raw-transaction-data-if-it-succeeded-or-failed

主要思路是

web3.eth.getTransactionReceipt 返回的

status 

如果沒有就比較

web3.eth.getTransactionReceipt

gasUsed

web3.eth.getTransaction

 

gas

4.geth 節點同步總追不上

尤其到2400000後,追的這個費勁

電腦效能硬傷,這個不考慮

星火計劃

https://ethfans.org/wikis/Home

加入個static-nodes.json 檔案到

--datadir "/data/geth/" /data/geth/ 裡

1.6以上版本geth預設 fast 模式同步 啟動需加

--syncmode fast --maxpeers 100

--cache 2048

都是扯淡(阿里租個伺服器SSD,幾天就同步完成())

 5.ERC20 tx

ERC20代幣及屬性

https://github.com/kvhnuke/etherwallet/blob/mercury/app/scripts/tokens/ethTokens.json

交易特殊to 的地址是合約地址,需另解析to ,input 

6.gas price

https://ethgasstation.info

 

 

相關文章