From 3a983d2419cdd053f5e03193794d1663c841f4b2 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Fri, 3 Jul 2015 11:20:07 -0500 Subject: [PATCH 1/9] Initial getTransactionReceipt support --- rpc/api/eth.go | 20 ++++++++++++++++++++ rpc/api/parsing.go | 23 +++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/rpc/api/eth.go b/rpc/api/eth.go index db0b4b024..c556c739f 100644 --- a/rpc/api/eth.go +++ b/rpc/api/eth.go @@ -77,6 +77,7 @@ var ( "eth_submitWork": (*ethApi).SubmitWork, "eth_resend": (*ethApi).Resend, "eth_pendingTransactions": (*ethApi).PendingTransactions, + "eth_getTransactionReceipt": (*ethApi).GetTransactionReceipt, } ) @@ -596,3 +597,22 @@ func (self *ethApi) PendingTransactions(req *shared.Request) (interface{}, error return ltxs, nil } + +func (self *ethApi) GetTransactionReceipt(req *shared.Request) (interface{}, error) { + args := new(HashArgs) + if err := self.codec.Decode(req.Params, &args); err != nil { + return nil, shared.NewDecodeParamError(err.Error()) + } + + rec, _ := self.xeth.GetTxReceipt(common.StringToHash(args.Hash)) + // We could have an error of "not found". Should disambiguate + // if err != nil { + // return err, nil + // } + if rec != nil { + v := NewReceiptRes(rec) + return v, nil + } + + return nil, nil +} diff --git a/rpc/api/parsing.go b/rpc/api/parsing.go index 632462c31..d1f9ccac2 100644 --- a/rpc/api/parsing.go +++ b/rpc/api/parsing.go @@ -402,6 +402,29 @@ func NewUncleRes(h *types.Header) *UncleRes { // WorkProved string `json:"workProved"` // } +type ReceiptRes struct { + TransactionHash *hexdata `json:transactionHash` + TransactionIndex *hexnum `json:transactionIndex` + BlockNumber *hexnum `json:blockNumber` + BlockHash *hexdata `json:blockHash` + CumulativeGasUsed *hexnum `json:cumulativeGasUsed` + GasUsed *hexnum `json:gasUsed` + ContractAddress *hexdata `json:contractAddress` + Logs *[]interface{} `json:logs` +} + +func NewReceiptRes(rec *types.Receipt) *ReceiptRes { + if rec == nil { + return nil + } + + var v = new(ReceiptRes) + // TODO fill out rest of object + v.CumulativeGasUsed = newHexNum(rec.CumulativeGasUsed) + + return v +} + func numString(raw interface{}) (*big.Int, error) { var number *big.Int // Parse as integer From 80eb8f46b7991b80dffe00e52d9fb00a90531bc0 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Fri, 3 Jul 2015 23:46:59 -0500 Subject: [PATCH 2/9] Fix hex conversion --- rpc/api/eth.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rpc/api/eth.go b/rpc/api/eth.go index c556c739f..9d78538fe 100644 --- a/rpc/api/eth.go +++ b/rpc/api/eth.go @@ -604,7 +604,8 @@ func (self *ethApi) GetTransactionReceipt(req *shared.Request) (interface{}, err return nil, shared.NewDecodeParamError(err.Error()) } - rec, _ := self.xeth.GetTxReceipt(common.StringToHash(args.Hash)) + v := common.BytesToHash(common.FromHex(args.Hash)) + rec := self.xeth.GetTxReceipt(v) // We could have an error of "not found". Should disambiguate // if err != nil { // return err, nil From 481b221279be1673832f96e35e3fdc0f82e178bc Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Sat, 4 Jul 2015 00:00:23 -0500 Subject: [PATCH 3/9] Decode full receipt storage --- core/transaction_util.go | 15 +++++++++++++++ rpc/api/parsing.go | 6 ++++-- xeth/xeth.go | 4 ++-- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/core/transaction_util.go b/core/transaction_util.go index cb5d6c7f7..61bf023a6 100644 --- a/core/transaction_util.go +++ b/core/transaction_util.go @@ -54,6 +54,21 @@ func PutReceipts(db common.Database, receipts types.Receipts) error { return nil } +// GetReceipt returns a receipt by hash +func GetFullReceipt(db common.Database, txHash common.Hash) *types.ReceiptForStorage { + data, _ := db.Get(append(receiptsPre, txHash[:]...)) + if len(data) == 0 { + return nil + } + + var receipt types.ReceiptForStorage + err := rlp.DecodeBytes(data, &receipt) + if err != nil { + glog.V(logger.Error).Infoln("GetReceipt err:", err) + } + return &receipt +} + // GetReceipt returns a receipt by hash func GetReceipt(db common.Database, txHash common.Hash) *types.Receipt { data, _ := db.Get(append(receiptsPre, txHash[:]...)) diff --git a/rpc/api/parsing.go b/rpc/api/parsing.go index d1f9ccac2..c7edf4325 100644 --- a/rpc/api/parsing.go +++ b/rpc/api/parsing.go @@ -413,15 +413,17 @@ type ReceiptRes struct { Logs *[]interface{} `json:logs` } -func NewReceiptRes(rec *types.Receipt) *ReceiptRes { +func NewReceiptRes(rec *types.ReceiptForStorage) *ReceiptRes { if rec == nil { return nil } var v = new(ReceiptRes) // TODO fill out rest of object + // ContractAddress is all 0 when not a creation tx + v.ContractAddress = newHexData(rec.ContractAddress) v.CumulativeGasUsed = newHexNum(rec.CumulativeGasUsed) - + v.TransactionHash = newHexData(rec.TxHash) return v } diff --git a/xeth/xeth.go b/xeth/xeth.go index cbc8dbbde..1b7f06821 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -368,8 +368,8 @@ func (self *XEth) GetBlockReceipts(bhash common.Hash) types.Receipts { return self.backend.BlockProcessor().GetBlockReceipts(bhash) } -func (self *XEth) GetTxReceipt(txhash common.Hash) *types.Receipt { - return core.GetReceipt(self.backend.ExtraDb(), txhash) +func (self *XEth) GetTxReceipt(txhash common.Hash) *types.ReceiptForStorage { + return core.GetFullReceipt(self.backend.ExtraDb(), txhash) } func (self *XEth) GasLimit() *big.Int { From 3be9046c21920abffa3c5ec81d5aabea8e3bab73 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Sat, 4 Jul 2015 10:24:52 -0500 Subject: [PATCH 4/9] Rename local variable for clarity --- rpc/api/eth.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rpc/api/eth.go b/rpc/api/eth.go index 9d78538fe..0f3b14525 100644 --- a/rpc/api/eth.go +++ b/rpc/api/eth.go @@ -604,8 +604,8 @@ func (self *ethApi) GetTransactionReceipt(req *shared.Request) (interface{}, err return nil, shared.NewDecodeParamError(err.Error()) } - v := common.BytesToHash(common.FromHex(args.Hash)) - rec := self.xeth.GetTxReceipt(v) + txhash := common.BytesToHash(common.FromHex(args.Hash)) + rec := self.xeth.GetTxReceipt(txhash) // We could have an error of "not found". Should disambiguate // if err != nil { // return err, nil From cd4cc309ae4ead756cbe58ad564b029e874d9832 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Sat, 4 Jul 2015 11:28:30 -0500 Subject: [PATCH 5/9] Remove redundant function --- core/transaction_util.go | 15 --------------- rpc/api/parsing.go | 2 +- xeth/xeth.go | 4 ++-- 3 files changed, 3 insertions(+), 18 deletions(-) diff --git a/core/transaction_util.go b/core/transaction_util.go index 61bf023a6..cb5d6c7f7 100644 --- a/core/transaction_util.go +++ b/core/transaction_util.go @@ -54,21 +54,6 @@ func PutReceipts(db common.Database, receipts types.Receipts) error { return nil } -// GetReceipt returns a receipt by hash -func GetFullReceipt(db common.Database, txHash common.Hash) *types.ReceiptForStorage { - data, _ := db.Get(append(receiptsPre, txHash[:]...)) - if len(data) == 0 { - return nil - } - - var receipt types.ReceiptForStorage - err := rlp.DecodeBytes(data, &receipt) - if err != nil { - glog.V(logger.Error).Infoln("GetReceipt err:", err) - } - return &receipt -} - // GetReceipt returns a receipt by hash func GetReceipt(db common.Database, txHash common.Hash) *types.Receipt { data, _ := db.Get(append(receiptsPre, txHash[:]...)) diff --git a/rpc/api/parsing.go b/rpc/api/parsing.go index c7edf4325..70e9bf942 100644 --- a/rpc/api/parsing.go +++ b/rpc/api/parsing.go @@ -413,7 +413,7 @@ type ReceiptRes struct { Logs *[]interface{} `json:logs` } -func NewReceiptRes(rec *types.ReceiptForStorage) *ReceiptRes { +func NewReceiptRes(rec *types.Receipt) *ReceiptRes { if rec == nil { return nil } diff --git a/xeth/xeth.go b/xeth/xeth.go index 1b7f06821..cbc8dbbde 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -368,8 +368,8 @@ func (self *XEth) GetBlockReceipts(bhash common.Hash) types.Receipts { return self.backend.BlockProcessor().GetBlockReceipts(bhash) } -func (self *XEth) GetTxReceipt(txhash common.Hash) *types.ReceiptForStorage { - return core.GetFullReceipt(self.backend.ExtraDb(), txhash) +func (self *XEth) GetTxReceipt(txhash common.Hash) *types.Receipt { + return core.GetReceipt(self.backend.ExtraDb(), txhash) } func (self *XEth) GasLimit() *big.Int { From 30afd37604da40416b0dd4fdc8cad322c12651cf Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Sat, 4 Jul 2015 12:03:37 -0500 Subject: [PATCH 6/9] Compose additional fields --- rpc/api/eth.go | 7 ++++++- rpc/api/parsing.go | 16 ++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/rpc/api/eth.go b/rpc/api/eth.go index 0f3b14525..6d759a087 100644 --- a/rpc/api/eth.go +++ b/rpc/api/eth.go @@ -605,13 +605,18 @@ func (self *ethApi) GetTransactionReceipt(req *shared.Request) (interface{}, err } txhash := common.BytesToHash(common.FromHex(args.Hash)) + tx, bhash, bnum, txi := self.xeth.EthTransactionByHash(args.Hash) rec := self.xeth.GetTxReceipt(txhash) // We could have an error of "not found". Should disambiguate // if err != nil { // return err, nil // } - if rec != nil { + if rec != nil && tx != nil { v := NewReceiptRes(rec) + v.BlockHash = newHexData(bhash) + v.BlockNumber = newHexNum(bnum) + v.GasUsed = newHexNum(tx.Gas().Bytes()) + v.TransactionIndex = newHexNum(txi) return v, nil } diff --git a/rpc/api/parsing.go b/rpc/api/parsing.go index 70e9bf942..cc7f60cad 100644 --- a/rpc/api/parsing.go +++ b/rpc/api/parsing.go @@ -1,6 +1,7 @@ package api import ( + "bytes" "encoding/binary" "encoding/hex" "encoding/json" @@ -419,11 +420,18 @@ func NewReceiptRes(rec *types.Receipt) *ReceiptRes { } var v = new(ReceiptRes) - // TODO fill out rest of object - // ContractAddress is all 0 when not a creation tx - v.ContractAddress = newHexData(rec.ContractAddress) - v.CumulativeGasUsed = newHexNum(rec.CumulativeGasUsed) v.TransactionHash = newHexData(rec.TxHash) + // v.TransactionIndex = newHexNum(input) // transaction + // v.BlockNumber = newHexNum(input) // transaction + // v.BlockHash = newHexData(input) //transaction + v.CumulativeGasUsed = newHexNum(rec.CumulativeGasUsed) + // v.GasUsed = newHexNum(input) // CumulativeGasUsed (blocknum-1) + // If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation + if bytes.Compare(rec.ContractAddress.Bytes(), bytes.Repeat([]byte{0}, 20)) != 0 { + v.ContractAddress = newHexData(rec.ContractAddress) + } + // v.Logs = rec.Logs() + return v } From 62559ac3304ff582028a20771c8ef870f24685f5 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Sat, 4 Jul 2015 12:14:06 -0500 Subject: [PATCH 7/9] Cleanup --- rpc/api/parsing.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/rpc/api/parsing.go b/rpc/api/parsing.go index cc7f60cad..4209ea7e3 100644 --- a/rpc/api/parsing.go +++ b/rpc/api/parsing.go @@ -421,11 +421,11 @@ func NewReceiptRes(rec *types.Receipt) *ReceiptRes { var v = new(ReceiptRes) v.TransactionHash = newHexData(rec.TxHash) - // v.TransactionIndex = newHexNum(input) // transaction - // v.BlockNumber = newHexNum(input) // transaction - // v.BlockHash = newHexData(input) //transaction + // v.TransactionIndex = newHexNum(input) + // v.BlockNumber = newHexNum(input) + // v.BlockHash = newHexData(input) v.CumulativeGasUsed = newHexNum(rec.CumulativeGasUsed) - // v.GasUsed = newHexNum(input) // CumulativeGasUsed (blocknum-1) + // v.GasUsed = newHexNum(input) // If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation if bytes.Compare(rec.ContractAddress.Bytes(), bytes.Repeat([]byte{0}, 20)) != 0 { v.ContractAddress = newHexData(rec.ContractAddress) From dd521ece3ff6163c891d2f945903533079413903 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Sun, 5 Jul 2015 12:25:44 -0500 Subject: [PATCH 8/9] Always return transaction hash --- xeth/xeth.go | 1 - 1 file changed, 1 deletion(-) diff --git a/xeth/xeth.go b/xeth/xeth.go index cbc8dbbde..1e87b738d 100644 --- a/xeth/xeth.go +++ b/xeth/xeth.go @@ -966,7 +966,6 @@ func (self *XEth) Transact(fromStr, toStr, nonceStr, valueStr, gasStr, gasPriceS if contractCreation { addr := crypto.CreateAddress(from, nonce) glog.V(logger.Info).Infof("Tx(%x) created: %x\n", tx.Hash(), addr) - return addr.Hex(), nil } else { glog.V(logger.Info).Infof("Tx(%x) to: %x\n", tx.Hash(), tx.To()) } From 6c7f5e3d0e6f4166f161278fdced3d90dfd6f9cc Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Sun, 5 Jul 2015 15:42:04 -0500 Subject: [PATCH 9/9] Add autocomplete support for console --- rpc/api/utils.go | 1 + 1 file changed, 1 insertion(+) diff --git a/rpc/api/utils.go b/rpc/api/utils.go index e6a01d3d6..54ca28774 100644 --- a/rpc/api/utils.go +++ b/rpc/api/utils.go @@ -86,6 +86,7 @@ var ( "submitWork", "pendingTransactions", "resend", + "getTransactionReceipt", }, "miner": []string{ "hashrate",