33package cli
44
55import (
6- "fmt"
7- // "math/big"
86 "encoding/hex"
7+ "fmt"
8+ "reflect"
99 "strconv"
1010
1111 "github.com/ethereum/go-ethereum/rlp"
1212 "github.com/ethereum/go-ethereum/rpc"
13- // "github.com/ethereum/go-ethereum/common"
1413)
1514
1615type Header struct {
@@ -21,7 +20,7 @@ type Header struct {
2120 TxHash string `json:"transactionsRoot" gencodec:"required"`
2221 ReceiptHash string `json:"receiptsRoot" gencodec:"required"`
2322 Bloom string `json:"logsBloom" gencodec:"required"`
24- Difficulty string `json:"difficulty" gencodec:"required"`
23+ Difficulty string `json:"difficulty" gencodec:"required"`
2524 Number string `json:"number" gencodec:"required"`
2625 GasLimit string `json:"gasLimit" gencodec:"required"`
2726 GasUsed string `json:"gasUsed" gencodec:"required"`
@@ -62,49 +61,42 @@ func rlpEncodeBlock(client *rpc.Client, block string) {
6261 fmt .Println ("can't get requested block:" , err )
6362 return
6463 } else {
64+ blockInterface := generateInterface (blockHeader )
65+ encodedBlock := EncodeBlock (blockInterface )
66+ fmt .Printf ("%+x\n " , encodedBlock )
67+ }
68+ }
6569
66- encodedBlock := EncodeBlock (blockHeader )
67- fmt .Printf ("%+v\n " , encodedBlock )
70+ func calculateRlpEncoding (client * rpc.Client , block string ) {
71+ var blockHeader Header
72+ err := client .Call (& blockHeader , "eth_getBlockByNumber" , block , true )
73+ if err != nil {
74+ fmt .Println ("can't get requested block:" , err )
75+ return
76+ } else {
77+ blockInterface := generateInterface (blockHeader )
78+ encodedBlock := EncodeBlock (blockInterface )
79+ fmt .Printf ("%+x\n " , encodedBlock )
6880 }
6981}
7082
71- func EncodeBlock (header Header ) (h []byte ) {
72- // Annoyingly we need to funk around with the fields ugly for the time being
73- encParentHash , _ := hex .DecodeString (header .ParentHash [2 :])
74- encUncleHash , _ := hex .DecodeString (header .UncleHash [2 :])
75- encCoinbase , _ := hex .DecodeString (header .Coinbase [2 :])
76- encRoot , _ := hex .DecodeString (header .Root [2 :])
77- encTxHash , _ := hex .DecodeString (header .TxHash [2 :])
78- encReceiptHash , _ := hex .DecodeString (header .ReceiptHash [2 :])
79- encBloom , _ := hex .DecodeString (header .Bloom [2 :])
80- encDifficulty , _ := hex .DecodeString (header .Difficulty [2 :])
81- encNumber , _ := hex .DecodeString (header .Number [2 :])
82- encGasLimit , _ := hex .DecodeString (header .GasLimit [2 :])
83- encGasUsed , _ := hex .DecodeString (header .GasUsed [2 :])
84- encTime , _ := hex .DecodeString (header .Time [2 :])
85- encExtra , _ := hex .DecodeString (header .Extra [2 :])
86- encMixDigest , _ := hex .DecodeString (header .MixDigest [2 :])
87- encNonce , _ := hex .DecodeString (header .Nonce [2 :])
83+ func generateInterface (blockHeader Header ) (rest interface {}) {
84+ blockInterface := []interface {}{}
85+ s := reflect .ValueOf (& blockHeader ).Elem ()
8886
89- x := []interface {}{
90- encParentHash ,
91- encUncleHash ,
92- encCoinbase ,
93- encRoot ,
94- encTxHash ,
95- encReceiptHash ,
96- encBloom ,
97- encDifficulty ,
98- encNumber ,
99- encGasLimit ,
100- encGasUsed ,
101- encTime ,
102- encExtra ,
103- encMixDigest ,
104- encNonce ,
87+ // Append items into the interface
88+ for i := 0 ; i < s .NumField (); i ++ {
89+ f := s .Field (i ).String ()
90+ element , _ := hex .DecodeString (f [2 :])
91+ blockInterface = append (blockInterface , element )
10592 }
10693
107- h , _ = rlp .EncodeToBytes (x )
94+ return blockInterface
95+ }
96+
97+ func EncodeBlock (blockInterface interface {}) (h []byte ) {
98+ // Encode the block
99+ h , _ = rlp .EncodeToBytes (blockInterface )
108100
109101 return h
110102}
0 commit comments