Skip to content

Commit 957c3ee

Browse files
committed
Cleaned up the generation of RLP encoded blocks
1 parent c10346b commit 957c3ee

File tree

4 files changed

+61
-50
lines changed

4 files changed

+61
-50
lines changed

src/block.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
{
22
"parentHash": "0xad34f0f919e4b06b18b0c674b8b9f6738a4878c76e837c8f31a2079f21dced1c",
3-
"difficulty": 2,
3+
"difficulty": "2",
44
"extraData": "0xd78301080a846765746887676f312e392e33856c696e75780000000000000000e0ac79c5577889dfb5745ace9c5dfebe1a11bb19ced9b98b427e7bd4c85765ce17154e658440915743ec442fb64756483bc592616754d13a3c62fce5a56ac9f501",
5-
"gasLimit": 16614106,
6-
"gasUsed": 0,
5+
"gasLimit": "16614106",
6+
"gasUsed": "0",
77
"hash": "0xd1d7abeb1345861e0103a24a239c178ccf930b069e50f2697d82fdd3496746ab",
88
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
99
"miner": "0x0000000000000000000000000000000000000000",
1010
"mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
1111
"nonce": "0x0000000000000000",
12-
"number": 10,
12+
"number": "10",
1313
"receiptsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
1414
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
15-
"size": 606,
15+
"size": "606",
1616
"stateRoot": "0xdb37435caa1fca7e1aa5b4da1c69fdf1d127232519eb3b1b5069825e6c62f5dc",
17-
"timestamp": 1529396972,
18-
"totalDifficulty": 21,
17+
"timestamp": "1529396972",
18+
"totalDifficulty": "21",
1919
"transactions": [],
2020
"transactionsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
2121
"uncles": []

src/cli/cli.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ func Launch(setup config.Setup) {
8080
},
8181
})
8282

83-
// Get block N
83+
// Get block N and spew out the RLP encoded block
8484
shell.AddCmd(&ishell.Cmd{
8585
Name: "rlpEncodeBlock",
86-
Help: "Gets specific block of Ethereum instance specified as from",
86+
Help: "Request RLP encoded block [N] of chain [from]",
8787
Func: func(c *ishell.Context) {
8888
c.Println("===============================================================")
8989
if len(c.Args) == 0 {
@@ -99,6 +99,25 @@ func Launch(setup config.Setup) {
9999
},
100100
})
101101

102+
// Get block N and spew out the RLP encoded block
103+
shell.AddCmd(&ishell.Cmd{
104+
Name: "getValidBlock",
105+
Help: "Request block [N] from chain [from], calculates the prefixes required for submission to chain [to]",
106+
Func: func(c *ishell.Context) {
107+
c.Println("===============================================================")
108+
if len(c.Args) == 0 {
109+
c.Println("Choose a block.")
110+
} else if len(c.Args) > 1 {
111+
c.Println("Too many arguments entered.")
112+
} else {
113+
block := strToHex(c.Args[0])
114+
c.Println("RLP encode block: " + c.Args[0])
115+
calculateRlpEncoding(client, block)
116+
}
117+
c.Println("===============================================================")
118+
},
119+
})
120+
102121
// run shell
103122
shell.Run()
104123
}

src/cli/rpc.go

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
package cli
44

55
import (
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

1615
type 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
}

src/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
package main
44

55
import (
6+
"flag"
67
"fmt"
78
"os"
8-
"flag"
99

1010
"github.com/validation/src/cli"
1111
"github.com/validation/src/config"

0 commit comments

Comments
 (0)