1919package geth
2020
2121import (
22+ "encoding/json"
2223 "errors"
2324 "fmt"
2425
2526 "github.com/ethereum/go-ethereum/core/types"
27+ "github.com/ethereum/go-ethereum/rlp"
2628)
2729
2830// A Nonce is a 64-bit hash which proves (combined with the mix-hash) that
@@ -61,6 +63,45 @@ type Header struct {
6163 header * types.Header
6264}
6365
66+ // NewHeaderFromRLP parses a header from an RLP data dump.
67+ func NewHeaderFromRLP (data []byte ) (* Header , error ) {
68+ h := & Header {
69+ header : new (types.Header ),
70+ }
71+ if err := rlp .DecodeBytes (data , h .header ); err != nil {
72+ return nil , err
73+ }
74+ return h , nil
75+ }
76+
77+ // EncodeRLP encodes a header into an RLP data dump.
78+ func (h * Header ) EncodeRLP () ([]byte , error ) {
79+ return rlp .EncodeToBytes (h .header )
80+ }
81+
82+ // NewHeaderFromJSON parses a header from an JSON data dump.
83+ func NewHeaderFromJSON (data string ) (* Header , error ) {
84+ h := & Header {
85+ header : new (types.Header ),
86+ }
87+ if err := json .Unmarshal ([]byte (data ), h .header ); err != nil {
88+ return nil , err
89+ }
90+ return h , nil
91+ }
92+
93+ // EncodeJSON encodes a header into an JSON data dump.
94+ func (h * Header ) EncodeJSON () (string , error ) {
95+ data , err := json .Marshal (h .header )
96+ return string (data ), err
97+ }
98+
99+ // String implements the fmt.Stringer interface to print some semi-meaningful
100+ // data dump of the header for debugging purposes.
101+ func (h * Header ) String () string {
102+ return h .header .String ()
103+ }
104+
64105func (h * Header ) GetParentHash () * Hash { return & Hash {h .header .ParentHash } }
65106func (h * Header ) GetUncleHash () * Hash { return & Hash {h .header .UncleHash } }
66107func (h * Header ) GetCoinbase () * Address { return & Address {h .header .Coinbase } }
@@ -76,9 +117,7 @@ func (h *Header) GetTime() int64 { return h.header.Time.Int64() }
76117func (h * Header ) GetExtra () []byte { return h .header .Extra }
77118func (h * Header ) GetMixDigest () * Hash { return & Hash {h .header .MixDigest } }
78119func (h * Header ) GetNonce () * Nonce { return & Nonce {h .header .Nonce } }
79-
80- func (h * Header ) GetHash () * Hash { return & Hash {h .header .Hash ()} }
81- func (h * Header ) GetHashNoNonce () * Hash { return & Hash {h .header .HashNoNonce ()} }
120+ func (h * Header ) GetHash () * Hash { return & Hash {h .header .Hash ()} }
82121
83122// Headers represents a slice of headers.
84123type Headers struct { headers []* types.Header }
@@ -101,6 +140,45 @@ type Block struct {
101140 block * types.Block
102141}
103142
143+ // NewBlockFromRLP parses a block from an RLP data dump.
144+ func NewBlockFromRLP (data []byte ) (* Block , error ) {
145+ b := & Block {
146+ block : new (types.Block ),
147+ }
148+ if err := rlp .DecodeBytes (data , b .block ); err != nil {
149+ return nil , err
150+ }
151+ return b , nil
152+ }
153+
154+ // EncodeRLP encodes a block into an RLP data dump.
155+ func (b * Block ) EncodeRLP () ([]byte , error ) {
156+ return rlp .EncodeToBytes (b .block )
157+ }
158+
159+ // NewBlockFromJSON parses a block from an JSON data dump.
160+ func NewBlockFromJSON (data string ) (* Block , error ) {
161+ b := & Block {
162+ block : new (types.Block ),
163+ }
164+ if err := json .Unmarshal ([]byte (data ), b .block ); err != nil {
165+ return nil , err
166+ }
167+ return b , nil
168+ }
169+
170+ // EncodeJSON encodes a block into an JSON data dump.
171+ func (b * Block ) EncodeJSON () (string , error ) {
172+ data , err := json .Marshal (b .block )
173+ return string (data ), err
174+ }
175+
176+ // String implements the fmt.Stringer interface to print some semi-meaningful
177+ // data dump of the block for debugging purposes.
178+ func (b * Block ) String () string {
179+ return b .block .String ()
180+ }
181+
104182func (b * Block ) GetParentHash () * Hash { return & Hash {b .block .ParentHash ()} }
105183func (b * Block ) GetUncleHash () * Hash { return & Hash {b .block .UncleHash ()} }
106184func (b * Block ) GetCoinbase () * Address { return & Address {b .block .Coinbase ()} }
@@ -137,6 +215,45 @@ func NewTransaction(nonce int64, to *Address, amount, gasLimit, gasPrice *BigInt
137215 return & Transaction {types .NewTransaction (uint64 (nonce ), to .address , amount .bigint , gasLimit .bigint , gasPrice .bigint , data )}
138216}
139217
218+ // NewTransactionFromRLP parses a transaction from an RLP data dump.
219+ func NewTransactionFromRLP (data []byte ) (* Transaction , error ) {
220+ tx := & Transaction {
221+ tx : new (types.Transaction ),
222+ }
223+ if err := rlp .DecodeBytes (data , tx .tx ); err != nil {
224+ return nil , err
225+ }
226+ return tx , nil
227+ }
228+
229+ // EncodeRLP encodes a transaction into an RLP data dump.
230+ func (tx * Transaction ) EncodeRLP () ([]byte , error ) {
231+ return rlp .EncodeToBytes (tx .tx )
232+ }
233+
234+ // NewTransactionFromJSON parses a transaction from an JSON data dump.
235+ func NewTransactionFromJSON (data string ) (* Transaction , error ) {
236+ tx := & Transaction {
237+ tx : new (types.Transaction ),
238+ }
239+ if err := json .Unmarshal ([]byte (data ), tx .tx ); err != nil {
240+ return nil , err
241+ }
242+ return tx , nil
243+ }
244+
245+ // EncodeJSON encodes a transaction into an JSON data dump.
246+ func (tx * Transaction ) EncodeJSON () (string , error ) {
247+ data , err := json .Marshal (tx .tx )
248+ return string (data ), err
249+ }
250+
251+ // String implements the fmt.Stringer interface to print some semi-meaningful
252+ // data dump of the transaction for debugging purposes.
253+ func (tx * Transaction ) String () string {
254+ return tx .tx .String ()
255+ }
256+
140257func (tx * Transaction ) GetData () []byte { return tx .tx .Data () }
141258func (tx * Transaction ) GetGas () int64 { return tx .tx .Gas ().Int64 () }
142259func (tx * Transaction ) GetGasPrice () * BigInt { return & BigInt {tx .tx .GasPrice ()} }
@@ -185,6 +302,45 @@ type Receipt struct {
185302 receipt * types.Receipt
186303}
187304
305+ // NewReceiptFromRLP parses a transaction receipt from an RLP data dump.
306+ func NewReceiptFromRLP (data []byte ) (* Receipt , error ) {
307+ r := & Receipt {
308+ receipt : new (types.Receipt ),
309+ }
310+ if err := rlp .DecodeBytes (data , r .receipt ); err != nil {
311+ return nil , err
312+ }
313+ return r , nil
314+ }
315+
316+ // EncodeRLP encodes a transaction receipt into an RLP data dump.
317+ func (r * Receipt ) EncodeRLP () ([]byte , error ) {
318+ return rlp .EncodeToBytes (r .receipt )
319+ }
320+
321+ // NewReceiptFromJSON parses a transaction receipt from an JSON data dump.
322+ func NewReceiptFromJSON (data string ) (* Receipt , error ) {
323+ r := & Receipt {
324+ receipt : new (types.Receipt ),
325+ }
326+ if err := json .Unmarshal ([]byte (data ), r .receipt ); err != nil {
327+ return nil , err
328+ }
329+ return r , nil
330+ }
331+
332+ // EncodeJSON encodes a transaction receipt into an JSON data dump.
333+ func (r * Receipt ) EncodeJSON () (string , error ) {
334+ data , err := rlp .EncodeToBytes (r .receipt )
335+ return string (data ), err
336+ }
337+
338+ // String implements the fmt.Stringer interface to print some semi-meaningful
339+ // data dump of the transaction receipt for debugging purposes.
340+ func (r * Receipt ) String () string {
341+ return r .receipt .String ()
342+ }
343+
188344func (r * Receipt ) GetPostState () []byte { return r .receipt .PostState }
189345func (r * Receipt ) GetCumulativeGasUsed () * BigInt { return & BigInt {r .receipt .CumulativeGasUsed } }
190346func (r * Receipt ) GetBloom () * Bloom { return & Bloom {r .receipt .Bloom } }
0 commit comments