Skip to content

Commit b6b5ec8

Browse files
committed
core/types: add core type marshal methods too
1 parent ca37730 commit b6b5ec8

4 files changed

Lines changed: 131 additions & 0 deletions

File tree

‎core/types/json.go‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ import (
2626

2727
type hexBytes []byte
2828

29+
func (b *hexBytes) MarshalJSON() ([]byte, error) {
30+
if b != nil {
31+
return []byte(fmt.Sprintf(`"0x%x"`, []byte(*b))), nil
32+
}
33+
return nil, nil
34+
}
35+
2936
func (b *hexBytes) UnmarshalJSON(input []byte) error {
3037
if len(input) < 2 || input[0] != '"' || input[len(input)-1] != '"' {
3138
return fmt.Errorf("cannot unmarshal non-string into hexBytes")
@@ -44,6 +51,13 @@ func (b *hexBytes) UnmarshalJSON(input []byte) error {
4451

4552
type hexBig big.Int
4653

54+
func (b *hexBig) MarshalJSON() ([]byte, error) {
55+
if b != nil {
56+
return []byte(fmt.Sprintf(`"0x%x"`, (*big.Int)(b))), nil
57+
}
58+
return nil, nil
59+
}
60+
4761
func (b *hexBig) UnmarshalJSON(input []byte) error {
4862
raw, err := checkHexNumber(input)
4963
if err != nil {
@@ -59,6 +73,13 @@ func (b *hexBig) UnmarshalJSON(input []byte) error {
5973

6074
type hexUint64 uint64
6175

76+
func (b *hexUint64) MarshalJSON() ([]byte, error) {
77+
if b != nil {
78+
return []byte(fmt.Sprintf(`"0x%x"`, *(*uint64)(b))), nil
79+
}
80+
return nil, nil
81+
}
82+
6283
func (b *hexUint64) UnmarshalJSON(input []byte) error {
6384
raw, err := checkHexNumber(input)
6485
if err != nil {

‎core/types/json_test.go‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package types
22

33
import (
44
"encoding/json"
5+
"reflect"
56
"testing"
67

78
"github.com/ethereum/go-ethereum/common"
@@ -44,6 +45,31 @@ func TestUnmarshalHeader(t *testing.T) {
4445
}
4546
}
4647

48+
func TestMarshalHeader(t *testing.T) {
49+
for name, test := range unmarshalHeaderTests {
50+
if test.wantError != nil {
51+
continue
52+
}
53+
var original *Header
54+
json.Unmarshal([]byte(test.input), &original)
55+
56+
blob, err := json.Marshal(original)
57+
if err != nil {
58+
t.Errorf("test %q: failed to marshal header: %v", name, err)
59+
continue
60+
}
61+
var proced *Header
62+
if err := json.Unmarshal(blob, &proced); err != nil {
63+
t.Errorf("Test %q: failed to unmarshal marhsalled header: %v", name, err)
64+
continue
65+
}
66+
if !reflect.DeepEqual(original, proced) {
67+
t.Errorf("test %q: header mismatch: have %+v, want %+v", name, proced, original)
68+
continue
69+
}
70+
}
71+
}
72+
4773
var unmarshalTransactionTests = map[string]struct {
4874
input string
4975
wantHash common.Hash
@@ -94,6 +120,32 @@ func TestUnmarshalTransaction(t *testing.T) {
94120
}
95121
}
96122

123+
func TestMarshalTransaction(t *testing.T) {
124+
for name, test := range unmarshalTransactionTests {
125+
if test.wantError != nil {
126+
continue
127+
}
128+
var original *Transaction
129+
json.Unmarshal([]byte(test.input), &original)
130+
131+
blob, err := json.Marshal(original)
132+
if err != nil {
133+
t.Errorf("test %q: failed to marshal transaction: %v", name, err)
134+
continue
135+
}
136+
var proced *Transaction
137+
if err := json.Unmarshal(blob, &proced); err != nil {
138+
t.Errorf("Test %q: failed to unmarshal marhsalled transaction: %v", name, err)
139+
continue
140+
}
141+
proced.Hash() // hack private fields to pass deep equal
142+
if !reflect.DeepEqual(original, proced) {
143+
t.Errorf("test %q: transaction mismatch: have %+v, want %+v", name, proced, original)
144+
continue
145+
}
146+
}
147+
}
148+
97149
var unmarshalReceiptTests = map[string]struct {
98150
input string
99151
wantError error
@@ -119,6 +171,31 @@ func TestUnmarshalReceipt(t *testing.T) {
119171
}
120172
}
121173

174+
func TestMarshalReceipt(t *testing.T) {
175+
for name, test := range unmarshalReceiptTests {
176+
if test.wantError != nil {
177+
continue
178+
}
179+
var original *Receipt
180+
json.Unmarshal([]byte(test.input), &original)
181+
182+
blob, err := json.Marshal(original)
183+
if err != nil {
184+
t.Errorf("test %q: failed to marshal receipt: %v", name, err)
185+
continue
186+
}
187+
var proced *Receipt
188+
if err := json.Unmarshal(blob, &proced); err != nil {
189+
t.Errorf("Test %q: failed to unmarshal marhsalled receipt: %v", name, err)
190+
continue
191+
}
192+
if !reflect.DeepEqual(original, proced) {
193+
t.Errorf("test %q: receipt mismatch: have %+v, want %+v", name, proced, original)
194+
continue
195+
}
196+
}
197+
}
198+
122199
func checkError(t *testing.T, testname string, got, want error) bool {
123200
if got == nil {
124201
if want != nil {

‎core/types/receipt.go‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ func (r *Receipt) DecodeRLP(s *rlp.Stream) error {
8484
return nil
8585
}
8686

87+
// MarshalJSON encodes receipts into the web3 RPC response block format.
88+
func (r *Receipt) MarshalJSON() ([]byte, error) {
89+
root := common.BytesToHash(r.PostState)
90+
91+
return json.Marshal(&jsonReceipt{
92+
PostState: &root,
93+
CumulativeGasUsed: (*hexBig)(r.CumulativeGasUsed),
94+
Bloom: &r.Bloom,
95+
Logs: &r.Logs,
96+
TxHash: &r.TxHash,
97+
ContractAddress: &r.ContractAddress,
98+
GasUsed: (*hexBig)(r.GasUsed),
99+
})
100+
}
101+
87102
// UnmarshalJSON decodes the web3 RPC receipt format.
88103
func (r *Receipt) UnmarshalJSON(input []byte) error {
89104
var dec jsonReceipt

‎core/types/transaction.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,24 @@ func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
128128
return err
129129
}
130130

131+
// MarshalJSON encodes transactions into the web3 RPC response block format.
132+
func (tx *Transaction) MarshalJSON() ([]byte, error) {
133+
hash, v := tx.Hash(), uint64(tx.data.V)
134+
135+
return json.Marshal(&jsonTransaction{
136+
Hash: &hash,
137+
AccountNonce: (*hexUint64)(&tx.data.AccountNonce),
138+
Price: (*hexBig)(tx.data.Price),
139+
GasLimit: (*hexBig)(tx.data.GasLimit),
140+
Recipient: tx.data.Recipient,
141+
Amount: (*hexBig)(tx.data.Amount),
142+
Payload: (*hexBytes)(&tx.data.Payload),
143+
V: (*hexUint64)(&v),
144+
R: (*hexBig)(tx.data.R),
145+
S: (*hexBig)(tx.data.S),
146+
})
147+
}
148+
131149
// UnmarshalJSON decodes the web3 RPC transaction format.
132150
func (tx *Transaction) UnmarshalJSON(input []byte) error {
133151
var dec jsonTransaction

0 commit comments

Comments
 (0)