以太坊研究系列【交易结果判断】

in #ethereum6 years ago

还有心情来写文章和看文章的,也真是佛系了!

对以太坊交易结果检测一直存有疑问,再来从交易数据理一下。

转帐

未打包

> eth.getTransaction("0x9b03576991948f3e129651f07aef16d510e12d471b836ab7ecf5d5bfd49657f5")
{
  blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  blockNumber: null,
  from: "0x54b865714068f5f03574ace39a1f3279c4e83e2c",
  gas: 90000,
  gasPrice: 10000000000,
  hash: "0x9b03576991948f3e129651f07aef16d510e12d471b836ab7ecf5d5bfd49657f5",
  input: "0x",
  nonce: 297,
  r: "0xba32b89aefe894dc994e57bebaafba69da17f43484176f8b85e6db5f50bae40e",
  s: "0x7f3119a44dec4365a92f0824eddde1415ee21f6f7f2b3b40368ee9ed94b239e4",
  to: "0x469f07278600b1cb031448fd25f187f24a89ecb6",
  transactionIndex: 0,
  v: "0x42",
  value: 100
}

> eth.getTransactionReceipt("0x9b03576991948f3e129651f07aef16d510e12d471b836ab7ecf5d5bfd49657f5")
null

已打包,交易成功

> eth.getTransaction("0x9b03576991948f3e129651f07aef16d510e12d471b836ab7ecf5d5bfd49657f5")
{
  blockHash: "0x3d2eb9089222e86e2ad7c07e26e63b7e03687b2895456351895895e0c2e0abfb",
  blockNumber: 21202,
  from: "0x54b865714068f5f03574ace39a1f3279c4e83e2c",
  gas: 90000,
  gasPrice: 10000000000,
  hash: "0x9b03576991948f3e129651f07aef16d510e12d471b836ab7ecf5d5bfd49657f5",
  input: "0x",
  nonce: 297,
  r: "0xba32b89aefe894dc994e57bebaafba69da17f43484176f8b85e6db5f50bae40e",
  s: "0x7f3119a44dec4365a92f0824eddde1415ee21f6f7f2b3b40368ee9ed94b239e4",
  to: "0x469f07278600b1cb031448fd25f187f24a89ecb6",
  transactionIndex: 0,
  v: "0x42",
  value: 100
}

> eth.getTransactionReceipt("0x9b03576991948f3e129651f07aef16d510e12d471b836ab7ecf5d5bfd49657f5")
{
  blockHash: "0x3d2eb9089222e86e2ad7c07e26e63b7e03687b2895456351895895e0c2e0abfb",
  blockNumber: 21202,
  contractAddress: null,
  cumulativeGasUsed: 21000,
  from: "0x54b865714068f5f03574ace39a1f3279c4e83e2c",
  gasUsed: 21000,
  logs: [],
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  root: "0x19d7db804506eaa7af7c9e4303361aa7fad5027ec09a66f13745f1e9df185e1b",
  to: "0x469f07278600b1cb031448fd25f187f24a89ecb6",
  transactionHash: "0x9b03576991948f3e129651f07aef16d510e12d471b836ab7ecf5d5bfd49657f5",
  transactionIndex: 0
}

合约函数调用

调用合约

> contract_print_custodian.requestUnlock.sendTransaction("0x52e19166f9481e51d02b183db5d9ee83e951597375fa9b005c992b45e7851e82", "0x651a2fc0b59a8478c92fe088e012ffce665ba965", "0x8cbf4145", "0x54b865714068f5f03574ace39a1f3279c4e83e2c", {from:a0, value:web3.toWei(0,"ether"), gas:210000, gasPrice: web3.toWei(7,'gwei')})
"0x6e46e4436146bf31510c568338dc9a63ea03ca7c23429b818d54b8867f34be3e"

未打包

函数已调用,未打包,取交易数据

> eth.getTransaction("0x6e46e4436146bf31510c568338dc9a63ea03ca7c23429b818d54b8867f34be3e")
{
  blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  blockNumber: null,
  from: "0x54b865714068f5f03574ace39a1f3279c4e83e2c",
  gas: 210000,
  gasPrice: 7000000000,
  hash: "0x6e46e4436146bf31510c568338dc9a63ea03ca7c23429b818d54b8867f34be3e",
  input: "0xa706a2e252e19166f9481e51d02b183db5d9ee83e951597375fa9b005c992b45e7851e82000000000000000000000000651a2fc0b59a8478c92fe088e012ffce665ba9658cbf41450000000000000000000000000000000000000000000000000000000000000000000000000000000054b865714068f5f03574ace39a1f3279c4e83e2c",
  nonce: 295,
  r: "0x9073f55a09da42ec5380188fe9f7a621b7961c5e63591192152a42e292c14aba",
  s: "0x24819c0e4aeefd7538e054a9bf30305b8bd6bb4b0d1f52cb8cb577aa898cf130",
  to: "0xb6b5821a592dd7e4bd46ebbc406d223db6ba849b",
  transactionIndex: 0,
  v: "0x41",
  value: 0
}

取交易凭据

> eth.getTransactionReceipt("0x6e46e4436146bf31510c568338dc9a63ea03ca7c23429b818d54b8867f34be3e")
null

已打包,调用成功

取交易数据

> eth.getTransaction("0x6e46e4436146bf31510c568338dc9a63ea03ca7c23429b818d54b8867f34be3e")
{
  blockHash: "0xf649b43869074be46fb4dc8306af73689cd1890bc94b5cac2c570fdb95ddded0",
  blockNumber: 21195,
  from: "0x54b865714068f5f03574ace39a1f3279c4e83e2c",
  gas: 210000,
  gasPrice: 7000000000,
  hash: "0x6e46e4436146bf31510c568338dc9a63ea03ca7c23429b818d54b8867f34be3e",
  input: "0xa706a2e252e19166f9481e51d02b183db5d9ee83e951597375fa9b005c992b45e7851e82000000000000000000000000651a2fc0b59a8478c92fe088e012ffce665ba9658cbf41450000000000000000000000000000000000000000000000000000000000000000000000000000000054b865714068f5f03574ace39a1f3279c4e83e2c",
  nonce: 295,
  r: "0x9073f55a09da42ec5380188fe9f7a621b7961c5e63591192152a42e292c14aba",
  s: "0x24819c0e4aeefd7538e054a9bf30305b8bd6bb4b0d1f52cb8cb577aa898cf130",
  to: "0xb6b5821a592dd7e4bd46ebbc406d223db6ba849b",
  transactionIndex: 0,
  v: "0x41",
  value: 0
}

取交易凭据

> eth.getTransactionReceipt("0x6e46e4436146bf31510c568338dc9a63ea03ca7c23429b818d54b8867f34be3e")
{
  blockHash: "0xf649b43869074be46fb4dc8306af73689cd1890bc94b5cac2c570fdb95ddded0",
  blockNumber: 21195,
  contractAddress: null,
  cumulativeGasUsed: 128181,
  from: "0x54b865714068f5f03574ace39a1f3279c4e83e2c",
  gasUsed: 128181,
  logs: [{
      address: "0xb6b5821a592dd7e4bd46ebbc406d223db6ba849b",
      blockHash: "0xf649b43869074be46fb4dc8306af73689cd1890bc94b5cac2c570fdb95ddded0",
      blockNumber: 21195,
      data: "0x52e19166f9481e51d02b183db5d9ee83e951597375fa9b005c992b45e7851e82000000000000000000000000651a2fc0b59a8478c92fe088e012ffce665ba9658cbf41450000000000000000000000000000000000000000000000000000000047718117c471a08733f7849bcfef6765519bdd3097a71ef9ae6d1368e886684600000000000000000000000054b865714068f5f03574ace39a1f3279c4e83e2cee90af9d5aafb41e842dc5e48553221934e6ea89347f1eb94ef381dfe8b4ccb8000000000000000000000000000000000000000000000000000000005bf3a8ca",
      logIndex: 0,
      removed: false,
      topics: ["0xdb7266ce4b13a685194a86c2cb74e34d93b88ba3a4966cf3efdf1abc8ad1cbec"],
      transactionHash: "0x6e46e4436146bf31510c568338dc9a63ea03ca7c23429b818d54b8867f34be3e",
      transactionIndex: 0
  }],
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000001000000000000000000000000000000000000010000000000000000000000000000000",
  root: "0x9e5f8466790ad9c2e993dccbc8aaf4cfb419d43ee7691043fda38634b95805b4",
  to: "0xb6b5821a592dd7e4bd46ebbc406d223db6ba849b",
  transactionHash: "0x6e46e4436146bf31510c568338dc9a63ea03ca7c23429b818d54b8867f34be3e",
  transactionIndex: 0
}

已打包,gas不足

取交易数据

> eth.getTransaction("0xa9d1a487cc14c1e597831bba6e028cd4a895ea879844a962afcd8dc4ad54b488")
{
  blockHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
  blockNumber: null,
  from: "0x54b865714068f5f03574ace39a1f3279c4e83e2c",
  gas: 100000,
  gasPrice: 7000000000,
  hash: "0xa9d1a487cc14c1e597831bba6e028cd4a895ea879844a962afcd8dc4ad54b488",
  input: "0xa706a2e252e19166f9481e51d02b183db5d9ee83e951597375fa9b005c992b45e7851e82000000000000000000000000651a2fc0b59a8478c92fe088e012ffce665ba9658cbf41450000000000000000000000000000000000000000000000000000000000000000000000000000000054b865714068f5f03574ace39a1f3279c4e83e2c",
  nonce: 296,
  r: "0x7c2242ba3f694496b2ffc05f10817a51f90e5a5db62561c46cbf0d1aafa12a94",
  s: "0x2dab4b37bc5525a420a2f7fe46bf2226207341a3690f8083639f8814ebf9f07e",
  to: "0xb6b5821a592dd7e4bd46ebbc406d223db6ba849b",
  transactionIndex: 0,
  v: "0x41",
  value: 0
}

取交易凭据

> eth.getTransactionReceipt("0xa9d1a487cc14c1e597831bba6e028cd4a895ea879844a962afcd8dc4ad54b488")
{
  blockHash: "0x07ee5bb986f44abbc290d3e8450f57cf9c886cff3724e31e4f78d4bbcc3c1bd7",
  blockNumber: 21200,
  contractAddress: null,
  cumulativeGasUsed: 100000,
  from: "0x54b865714068f5f03574ace39a1f3279c4e83e2c",
  gasUsed: 100000,
  logs: [],
  logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
  root: "0x25e2ceebab1e6cee9a879b13ef9fb93e01f22308d0b1d16b1ed5c8e6e62f21d4",
  to: "0xb6b5821a592dd7e4bd46ebbc406d223db6ba849b",
  transactionHash: "0xa9d1a487cc14c1e597831bba6e028cd4a895ea879844a962afcd8dc4ad54b488",
  transactionIndex: 0
}

数据比较

  1. 未打包前blockHash为0,blockNumber为null
  2. 打包后blockHash块哈希,blockNumber有块号
  3. 未打包无交易凭据,打包后有交易凭据(不管调用是否成功)
  4. 合约调用成功后交易凭据中logs会有数据(可能是事件数据,未再确定),但普通转帐交易凭据中logs无数据
  5. 合约调用成功后交易凭据中gasUsed可能小于gas,如果gas指定不足调用后gasUsed等于gas
  6. 合约调用input为调用数据,而普通转帐交易input为"0x"

交易结果检测

可以看出即使打包了,交易也未必成功。那怎么来判断结果呢?从网上找了有几种方法:

1、判断gas == gasUsed,如果相等则“Out of Gas”,但是好像有可能正常交易也会相等;
2、使用debug.traceTransaction判断

> var status = debug.traceTransaction("0x9ee86a200528de32a695f1e2dd0d94a3871fefc7e49c5fd24a4a37eab1b99f7a")
undefined
> status.structLogs[status.structLogs.length-1].error

3、从块4370000 (Byzantium)后可以通过"status"进行判断,为0失败,为1成功

{
  blockHash: '0xb1fcff633029ee18ab6482b58ff8b6e95dd7c82a954c852157152a7a6d32785e',
  blockNumber: 4370000,
  contractAddress: null,
  cumulativeGasUsed: 21000,
  gasUsed: 21000,
  logs: [],
  logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
  root: null,
  status: 1, // **** HERE IS THE STATUS FIELD *****
  transactionHash: '0x1421a887a02301ae127bf2cd4c006116053c9dc4a255e69ea403a2d77c346cf5',
  transactionIndex: 0 
}

4、调用合约函数如果有事件,可使用logs数据,如果成功logs中有事件记录,如果失败为空。


参考

https://ethereum.stackexchange.com/questions/6007/how-can-the-transaction-status-from-a-thrown-error-be-detected-when-gas-can-be-e/6010#6010

https://ethereum.stackexchange.com/questions/6002/transaction-status

Sort:  

你今天过的开心吗?还在发愁自己的好文没人发现,收益惨淡吗?记得加上cn-curation标签,让飞鸽传书 帮你走出困境吧!假如我的留言打扰到你,请回复“取消”。

Congratulations @chaimyu! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made more than 3000 upvotes. Your next target is to reach 4000 upvotes.

Click here to view your Board of Honor
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

Meet the Steemians Contest - The results, the winners and the prizes
Meet the Steemians Contest - Special attendees revealed
Meet the Steemians Contest - Intermediate results

Support SteemitBoard's project! Vote for its witness and get one more award!

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.030
BTC 57606.89
ETH 3164.26
USDT 1.00
SBD 2.28