Skip to content

Commit f86c417

Browse files
authored
Merge pull request #15042 from rjl493456442/receipt_rpc
internal/ethapi: add status code to receipt rpc return
2 parents d78ad22 + 7e9e3a1 commit f86c417

File tree

4 files changed

+41
-21
lines changed

4 files changed

+41
-21
lines changed

core/database_util_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ func TestBlockReceiptStorage(t *testing.T) {
336336
db, _ := ethdb.NewMemDatabase()
337337

338338
receipt1 := &types.Receipt{
339-
Failed: true,
339+
Status: types.ReceiptStatusFailed,
340340
CumulativeGasUsed: big.NewInt(1),
341341
Logs: []*types.Log{
342342
{Address: common.BytesToAddress([]byte{0x11})},

core/types/gen_receipt_json.go

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/types/receipt.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,23 @@ import (
3030
//go:generate gencodec -type Receipt -field-override receiptMarshaling -out gen_receipt_json.go
3131

3232
var (
33-
receiptStatusFailed = []byte{}
34-
receiptStatusSuccessful = []byte{0x01}
33+
receiptStatusFailedRLP = []byte{}
34+
receiptStatusSuccessfulRLP = []byte{0x01}
35+
)
36+
37+
const (
38+
// ReceiptStatusFailed is the status code of a transaction if execution failed.
39+
ReceiptStatusFailed = uint(0)
40+
41+
// ReceiptStatusSuccessful is the status code of a transaction if execution succeeded.
42+
ReceiptStatusSuccessful = uint(1)
3543
)
3644

3745
// Receipt represents the results of a transaction.
3846
type Receipt struct {
3947
// Consensus fields
4048
PostState []byte `json:"root"`
41-
Failed bool `json:"failed"`
49+
Status uint `json:"status"`
4250
CumulativeGasUsed *big.Int `json:"cumulativeGasUsed" gencodec:"required"`
4351
Bloom Bloom `json:"logsBloom" gencodec:"required"`
4452
Logs []*Log `json:"logs" gencodec:"required"`
@@ -51,6 +59,7 @@ type Receipt struct {
5159

5260
type receiptMarshaling struct {
5361
PostState hexutil.Bytes
62+
Status hexutil.Uint
5463
CumulativeGasUsed *hexutil.Big
5564
GasUsed *hexutil.Big
5665
}
@@ -75,7 +84,13 @@ type receiptStorageRLP struct {
7584

7685
// NewReceipt creates a barebone transaction receipt, copying the init fields.
7786
func NewReceipt(root []byte, failed bool, cumulativeGasUsed *big.Int) *Receipt {
78-
return &Receipt{PostState: common.CopyBytes(root), Failed: failed, CumulativeGasUsed: new(big.Int).Set(cumulativeGasUsed)}
87+
r := &Receipt{PostState: common.CopyBytes(root), CumulativeGasUsed: new(big.Int).Set(cumulativeGasUsed)}
88+
if failed {
89+
r.Status = ReceiptStatusFailed
90+
} else {
91+
r.Status = ReceiptStatusSuccessful
92+
}
93+
return r
7994
}
8095

8196
// EncodeRLP implements rlp.Encoder, and flattens the consensus fields of a receipt
@@ -100,10 +115,10 @@ func (r *Receipt) DecodeRLP(s *rlp.Stream) error {
100115

101116
func (r *Receipt) setStatus(postStateOrStatus []byte) error {
102117
switch {
103-
case bytes.Equal(postStateOrStatus, receiptStatusSuccessful):
104-
r.Failed = false
105-
case bytes.Equal(postStateOrStatus, receiptStatusFailed):
106-
r.Failed = true
118+
case bytes.Equal(postStateOrStatus, receiptStatusSuccessfulRLP):
119+
r.Status = ReceiptStatusSuccessful
120+
case bytes.Equal(postStateOrStatus, receiptStatusFailedRLP):
121+
r.Status = ReceiptStatusFailed
107122
case len(postStateOrStatus) == len(common.Hash{}):
108123
r.PostState = postStateOrStatus
109124
default:
@@ -114,19 +129,18 @@ func (r *Receipt) setStatus(postStateOrStatus []byte) error {
114129

115130
func (r *Receipt) statusEncoding() []byte {
116131
if len(r.PostState) == 0 {
117-
if r.Failed {
118-
return receiptStatusFailed
119-
} else {
120-
return receiptStatusSuccessful
132+
if r.Status == ReceiptStatusFailed {
133+
return receiptStatusFailedRLP
121134
}
135+
return receiptStatusSuccessfulRLP
122136
}
123137
return r.PostState
124138
}
125139

126140
// String implements the Stringer interface.
127141
func (r *Receipt) String() string {
128-
if r.PostState == nil {
129-
return fmt.Sprintf("receipt{failed=%t cgas=%v bloom=%x logs=%v}", r.Failed, r.CumulativeGasUsed, r.Bloom, r.Logs)
142+
if len(r.PostState) == 0 {
143+
return fmt.Sprintf("receipt{status=%d cgas=%v bloom=%x logs=%v}", r.Status, r.CumulativeGasUsed, r.Bloom, r.Logs)
130144
}
131145
return fmt.Sprintf("receipt{med=%x cgas=%v bloom=%x logs=%v}", r.PostState, r.CumulativeGasUsed, r.Bloom, r.Logs)
132146
}

internal/ethapi/api.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,6 @@ func (s *PublicTransactionPoolAPI) GetTransactionReceipt(hash common.Hash) (map[
991991
from, _ := types.Sender(signer, tx)
992992

993993
fields := map[string]interface{}{
994-
"root": hexutil.Bytes(receipt.PostState),
995994
"blockHash": blockHash,
996995
"blockNumber": hexutil.Uint64(blockNumber),
997996
"transactionHash": hash,
@@ -1004,6 +1003,13 @@ func (s *PublicTransactionPoolAPI) GetTransactionReceipt(hash common.Hash) (map[
10041003
"logs": receipt.Logs,
10051004
"logsBloom": receipt.Bloom,
10061005
}
1006+
1007+
// Assign receipt status or post state.
1008+
if len(receipt.PostState) > 0 {
1009+
fields["root"] = hexutil.Bytes(receipt.PostState)
1010+
} else {
1011+
fields["status"] = hexutil.Uint(receipt.Status)
1012+
}
10071013
if receipt.Logs == nil {
10081014
fields["logs"] = [][]*types.Log{}
10091015
}

0 commit comments

Comments
 (0)