-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpc.go
112 lines (96 loc) · 3.19 KB
/
rpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (c) 2018 Clearmatics Technologies Ltd
package cli
import (
"encoding/hex"
"fmt"
"reflect"
"strconv"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
)
type Header struct {
ParentHash string `json:"parentHash"`
UncleHash string `json:"sha3Uncles"`
Coinbase string `json:"miner"`
Root string `json:"stateRoot"`
TxHash string `json:"transactionsRoot"`
ReceiptHash string `json:"receiptsRoot"`
Bloom string `json:"logsBloom"`
Difficulty string `json:"difficulty"`
Number string `json:"number"`
GasLimit string `json:"gasLimit"`
GasUsed string `json:"gasUsed"`
Time string `json:"timestamp"`
Extra string `json:"extraData"`
MixDigest string `json:"mixHash"`
Nonce string `json:"nonce"`
}
func latestBlock(client *rpc.Client) {
var lastBlock Block
err := client.Call(&lastBlock, "eth_getBlockByNumber", "latest", true)
if err != nil {
fmt.Println("can't get latest block:", err)
return
}
// Print events from the subscription as they arrive.
k, _ := strconv.ParseInt(lastBlock.Number, 0, 64)
fmt.Printf("latest block: %v\n", k)
}
func getBlock(client *rpc.Client, block string) {
var blockHeader Header
err := client.Call(&blockHeader, "eth_getBlockByNumber", block, true)
if err != nil {
fmt.Println("can't get requested block:", err)
return
}
fmt.Printf("%+v\n", blockHeader)
}
func rlpEncodeBlock(client *rpc.Client, block string) {
var blockHeader Header
err := client.Call(&blockHeader, "eth_getBlockByNumber", block, true)
if err != nil {
fmt.Println("can't get requested block:", err)
return
}
blockInterface := GenerateInterface(blockHeader)
encodedBlock := EncodeBlock(blockInterface)
fmt.Printf("%+x\n", encodedBlock)
}
func calculateRlpEncoding(client *rpc.Client, block string) {
var blockHeader Header
err := client.Call(&blockHeader, "eth_getBlockByNumber", block, true)
if err != nil {
fmt.Println("can't get requested block:", err)
return
}
// Generate an interface to encode the standard block header
blockInterface := GenerateInterface(blockHeader)
encodedBlock := EncodeBlock(blockInterface)
fmt.Printf("%+x\n", encodedBlock)
// Generate an interface to encode the blockheader without the signature in the extraData
blockHeader.Extra = blockHeader.Extra[:len(blockHeader.Extra)-130]
blockInterface = GenerateInterface(blockHeader)
encodedBlock = EncodeBlock(blockInterface)
fmt.Printf("\n%+x\n", encodedBlock[1:3])
// Generate an interface to encode the blockheader without the signature in the extraData
encExtra, _ := hex.DecodeString(blockHeader.Extra[2:])
encodedBlock = EncodeBlock(encExtra)
fmt.Printf("\n%+x\n", encodedBlock[0:1])
}
// Creates an interface for a block
func GenerateInterface(blockHeader Header) (rest interface{}) {
blockInterface := []interface{}{}
s := reflect.ValueOf(&blockHeader).Elem()
// Append items into the interface
for i := 0; i < s.NumField(); i++ {
f := s.Field(i).String()
element, _ := hex.DecodeString(f[2:])
blockInterface = append(blockInterface, element)
}
return blockInterface
}
// Encodes a block
func EncodeBlock(blockInterface interface{}) (h []byte) {
h, _ = rlp.EncodeToBytes(blockInterface)
return h
}