Skip to content

Commit

Permalink
Add Receipt to GetTransaction() RPC response
Browse files Browse the repository at this point in the history
  • Loading branch information
weiribao committed May 29, 2020
1 parent 7c6bb47 commit 7746b5d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
24 changes: 19 additions & 5 deletions blockchain/tx.go
Expand Up @@ -80,23 +80,33 @@ type TxReceiptEntry struct {
EvmRet common.Bytes
ContractAddress common.Address
GasUsed uint64
EvmErr error
EvmErr string
}

// AddTxReceipt adds transaction receipt.
func (ch *Chain) AddTxReceipt(txHash common.Hash, logs []*types.Log, evmRet common.Bytes,
func (ch *Chain) AddTxReceipt(tx types.Tx, logs []*types.Log, evmRet common.Bytes,
contractAddr common.Address, gasUsed uint64, evmErr error) {
raw, err := types.TxToBytes(tx)
if err != nil {
// Should never happen
logger.Panic(err)
}
txHash := crypto.Keccak256Hash(raw)
errStr := ""
if evmErr != nil {
errStr = evmErr.Error()
}
txReceiptEntry := TxReceiptEntry{
TxHash: txHash,
Logs: logs,
EvmRet: evmRet,
ContractAddress: contractAddr,
GasUsed: gasUsed,
EvmErr: evmErr,
EvmErr: errStr,
}
key := txReceiptKey(txHash)

err := ch.store.Put(key, txReceiptEntry)
err = ch.store.Put(key, txReceiptEntry)
if err != nil {
logger.Panic(err)
}
Expand All @@ -105,7 +115,11 @@ func (ch *Chain) AddTxReceipt(txHash common.Hash, logs []*types.Log, evmRet comm
// FindTxReceiptByHash looks up transaction receipt by hash.
func (ch *Chain) FindTxReceiptByHash(hash common.Hash) (*TxReceiptEntry, bool) {
txReceiptEntry := &TxReceiptEntry{}
err := ch.store.Get(txReceiptKey(hash), txReceiptEntry)

key := txReceiptKey(hash)

err := ch.store.Get(key, txReceiptEntry)

if err != nil {
if err != store.ErrKeyNotFound {
logger.Error(err)
Expand Down
6 changes: 5 additions & 1 deletion ledger/execution/tx_smart_contract.go
Expand Up @@ -128,7 +128,11 @@ func (exec *SmartContractTxExecutor) process(chainID string, view *st.StoreView,

// TODO: Add tx receipt: status and events
logs := view.PopLogs()
exec.chain.AddTxReceipt(txHash, logs, evmRet, contractAddr, gasUsed, evmErr)
if evmErr != nil {
// Do not record events if transaction is reverted
logs = nil
}
exec.chain.AddTxReceipt(tx, logs, evmRet, contractAddr, gasUsed, evmErr)

return txHash, result.OK
}
Expand Down
20 changes: 14 additions & 6 deletions rpc/query.go
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/thetatoken/theta/blockchain"
"github.com/thetatoken/theta/crypto/bls"

"github.com/thetatoken/theta/common"
Expand Down Expand Up @@ -107,12 +108,13 @@ type GetTransactionArgs struct {
}

type GetTransactionResult struct {
BlockHash common.Hash `json:"block_hash"`
BlockHeight common.JSONUint64 `json:"block_height"`
Status TxStatus `json:"status"`
TxHash common.Hash `json:"hash"`
Type byte `json:"type"`
Tx types.Tx `json:"transaction"`
BlockHash common.Hash `json:"block_hash"`
BlockHeight common.JSONUint64 `json:"block_height"`
Status TxStatus `json:"status"`
TxHash common.Hash `json:"hash"`
Type byte `json:"type"`
Tx types.Tx `json:"transaction"`
Receipt *blockchain.TxReceiptEntry `json:"receipt"`
}

type TxStatus string
Expand Down Expand Up @@ -161,6 +163,12 @@ func (t *ThetaRPCService) GetTransaction(args *GetTransactionArgs, result *GetTr
result.Tx = tx
result.Type = getTxType(tx)

// Add receipt
receipt, found := t.chain.FindTxReceiptByHash(hash)
if found {
result.Receipt = receipt
}

return nil
}

Expand Down

0 comments on commit 7746b5d

Please sign in to comment.