Ethereumjs

我簽署的交易似乎失敗了,並且沒有廣播。為什麼?

  • December 24, 2020

我想做一個非常簡單的操作:將 eth 發送到另一個地址。

這是我所做的:

方法 1。使用personal.send_transaction JSON-RPC 介面。(似乎已棄用)

我跑:

curl --request POST   --data '{
   "jsonrpc":"2.0",
   "method":"personal_sendTransaction",
   "params":[
     {
       "gas": "0x5208",
       "gasPrice": "0x178411b200",
       "from": "0x7CCfaF74ADBA37b2eF11B4caa3ce3759xxxxxxxx",
       "to": "0x3Ae7a18407B17037B2ECC4901c1b77Dbxxxxxxxx",
       "value": "0x4f94ae6af8000"
     },
     "my_pass_word"
   ],
   "id": 4004
 }'   --header 'Content-Type: application/json' http://172.16.1.53:8800

回复:

{
   "jsonrpc":"2.0",
   "result":"0x76af9bc26ac39f6011c76c3adf2914ab8b677ad95a8687ed0b3de058d4866bb6",
   "id":4004
}

但是,在 etherscan 中找不到此 tx。

並得到了這個日誌:

2020-12-20 07:45:03 UTC http.worker10 WARN rpc  personal_sendTransaction is deprecated and will be removed in future versions: Account management is being phased out see #9997 for alternatives.
2020-12-20 07:45:03 UTC http.worker10 DEBUG own_tx  Imported to the pool (hash 0x76af9bc26ac39f6011c76c3adf2914ab8b677ad95a8687ed0b3de058d4866bb6)
2020-12-20 07:45:03 UTC http.worker10 DEBUG txqueue  [0x76af9bc26ac39f6011c76c3adf2914ab8b677ad95a8687ed0b3de058d4866bb6] Added to the pool.
2020-12-20 07:45:03 UTC http.worker10 DEBUG txqueue  [0x76af9bc26ac39f6011c76c3adf2914ab8b677ad95a8687ed0b3de058d4866bb6] Sender: 0x7ccf…xxxx, nonce: 887, gasPrice: 101000000000, gas: 21000, value: 1400000000000000, dataLen: 0))
2020-12-20 07:45:03 UTC IO Worker #1 DEBUG sync  Finished transaction propagation, took 0ms
2020-12-20 07:45:03 UTC http.worker10 DEBUG txqueue  Re-computing pending set for block: 11475896
2020-12-20 07:45:03 UTC http.worker10 DEBUG miner  Attempting to push 1 transactions.
2020-12-20 07:45:03 UTC http.worker10 DEBUG miner  Adding tx 0x76af9bc26ac39f6011c76c3adf2914ab8b677ad95a8687ed0b3de058d4866bb6 took 3 ms
2020-12-20 07:45:03 UTC http.worker10 DEBUG miner  Pushed 1 transactions in 3 ms
2020-12-20 07:45:03 UTC http.worker10 DEBUG rpc  [Some(Num(4004))] Took 42ms
2020-12-20 07:45:03 UTC http.worker10 DEBUG rpc  Response: {"jsonrpc":"2.0","result":"0x76af9bc26ac39f6011c76c3adf2914ab8b677ad95a8687ed0b3de058d4866bb6","id":4004}.

方法 2. 呼叫 eth_sendRawTransaction 到我的本地 ETH 節點。

我按照以下步驟執行此操作:

步驟 2.1 得到了私鑰(真的正確,我保證,我是通過 2 語言 API 得到的,它們是一樣的!)

<my_private_key>

步驟 2.2 通過我的程式碼獲得了簽名的 tx:

const get_raw_tx = function(params){

 console.info("== params:")
 console.info(params)
 const privateKey = Buffer.from(
   params.private_key,
   'hex',
 )

 const txParams = { 
   nonce: params.nonce,
   gasPrice: params.gas_price,
   gasLimit: params.gas_limit,
   to: params.to_address,
   value: params.crypto_amount,
   data: params.data,
 }

 const tx = new EthereumTx(txParams, { chain: 'mainnet', hardfork: 'petersburg' })
 tx.sign(privateKey)
 const serializedTx = tx.serialize()
 const rawTx = '0x' + serializedTx.toString('hex');
 return rawTx
}

和 HTTP GET 方法:

  • 加密金額:0.0013 ETH
  • gas_limit:21000,十六進制:0x5208
  • gas_price:121000000000(121 Gwei),十六進制:0x1c2c297a00
$ curl http://localhost:8000/get_tx?private_key=MY_PRIVATE_KEY&nonce=0x01&gas_price=0x1c2c297a00&gas_limit=0x2710&to_address=0xC6d64494D2042B69aceAd368395f8e4Fxxxxxxxx&crypto_amount=1300000000000000

並得到了這個簽名的 TX 字元串:

0xf86d820d1b851c2c297a00825208943ae7a18407b17037b2ecc4901c1b77db98367cda87049e57d63540008026a07931b78f0f596f49655d2c0418156f742ffab64cb2c545616d1f3843e4f4f778a026d63f88d6166ec32223c1e850fb9245e4b5df6d50237080684fc933e7ac0e4b

步驟 2.3 我像這樣進行 JSON-PRC 呼叫:

curl --location --request POST 'http://my-server-ip:port'   --header 'Content-Type: application/json'   --data-raw '{
   "jsonrpc":"2.0",
   "method":"eth_sendRawTransaction",
   "params":["0xf86d820d1b851c2c297a00825208943ae7a18407b17037b2ecc4901c1b77db98367cda87049e57d63540008026a07931b78f0f596f49655d2c0418156f742ffab64cb2c545616d1f3843e4f4f778a026d63f88d6166ec32223c1e850fb9245e4b5df6d50237080684fc933e7ac0e4b"],
   "id": 4004
 }'

這是回應:

{
   "jsonrpc":"2.0",
   "result":"0x06a1958e25ef269346cc79043c4c4343b9df86bf0b135c257b9b7077e7d83bc7",
   "id":4004
}

在 ( http://etherscan.io/tx/0x06a1958e25ef269346cc79043c4c4343b9df86bf0b135c257b9b7077e7d83bc7 )上仍然找不到此 tx

日誌中沒有錯誤或警告。

方法3.呼叫infura API

curl https://mainnet.infura.io/v3/MY-PROJECT-ID     
   -X POST
   -H "Content-Type: application/json"
   -d '{
        "jsonrpc":"2.0",
        "method":"eth_sendRawTransaction",
        "params":["0xf86d820d1b851c2c297a00825208943ae7a18407b17037b2ecc4901c1b77db98367cda87049e57d63540008026a07931b78f0f596f49655d2c0418156f742ffab64cb2c545616d1f3843e4f4f778a026d63f88d6166ec32223c1e850fb9245e4b5df6d50237080684fc933e7ac0e4b"],
        "id":1
      }'

並得到了infura的回复:

{
   "jsonrpc":"2.0",
   "id":1,
   "result":"0x5809db5cdbdab054ca7f5f383e59ba6df74e3f5e87a33b90df1b1c6e7d0ae587"
}

仍然,在 etherscan.io 上找不到這個 TX

所以,有人可以幫助我,為什麼所有這些方法都失敗了。

多謝!

ps

我的環境

Ubuntu 18.04 伺服器

客戶端:openethereum (OpenEthereum/v3.0.1-stable-8ca8089-20200601/x86_64-unknown-linux-gnu/rustc1.43.1)

命令啟動它:nohup ./openethereum &

配置文件:(在.local/share/openethereum/config.toml

[parity]
mode = "active"
identity = "lueluelue"
[rpc]
apis = ["web3", "eth", "pubsub", "net", "parity", "parity_pubsub", "traces", "rpc", "shh", "shh_pubsub", "personal", "parity_accounts", "secretstore"]
server_threads = 2

port = 8800
interface = "172.my.i.p"

[websockets]
apis = ["web3", "eth", "pubsub", "net", "parity", "parity_pubsub", "traces", "rpc", "shh", "shh_pubsub", "personal"]

[misc]
log_file = "/home/ubuntu/eth.log"
logging = "debug"

[mining]
min_gas_price = 1 

當塊未完成同步時,我執行了 JSON-RPC 呼叫。(同步到 1 天前)

好的,我明白了:參數nonce很重要!

我的錯:我隨機給出了nonce參數,這在某種程度上是無效的。( 我不知道 )

搜尋了很多文章後,我發現這nonce很重要!(根據https://nonseodion.medium.com/7-easy-steps-to-resolve-cancel-pending-ethereum-transactions-88d7d8bce053,這篇文章討論了nonce和pendingtransactions之間的關係)

還有這篇文章:https ://info.etherscan.com/how-to-cancel-ethereum-pending-transactions 提到了 nonce。

所以,為了弄清楚哪個號碼應該是我的地址,我使用 MetaMask 創建了一個事務,很快,我得到了它的狀態:

0.204465275198051046 Eth
Nonce: 912

0.202643275198051046 Eth
Nonce: 913     <!---  this should be your next transaction's nonce! 

在此處輸入圖像描述

所以我更改了我的程式碼,指定了正確的nonce,然後我的新簽名交易在 3 秒內廣播!

^_^

引用自:https://ethereum.stackexchange.com/questions/91324