-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathtypes.go
162 lines (145 loc) · 4.9 KB
/
types.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package ethrpc
import (
"bytes"
"math/big"
"unsafe"
)
// Transaction - transaction object
type Transaction struct {
Hash string
Nonce int
BlockHash string
BlockNumber *int
TransactionIndex *int
From string
To string
Value big.Int
Gas int
GasPrice big.Int
Input string
}
// Block - block object
type Block struct {
Number uint64
Hash string
ParentHash string
Nonce string
Sha3Uncles string
LogsBloom string
TransactionsRoot string
StateRoot string
Miner string
Difficulty big.Int
TotalDifficulty big.Int
ExtraData string
Size int
GasLimit int
GasUsed int
Timestamp int
Uncles []string
Transactions []Transaction
}
type hexInt uint64
func (i *hexInt) UnmarshalJSON(data []byte) error {
result, err := ParseInt(string(bytes.Trim(data, `"`)))
*i = hexInt(result)
return err
}
type hexBig big.Int
func (i *hexBig) UnmarshalJSON(data []byte) error {
result, err := ParseBigInt(string(bytes.Trim(data, `"`)))
*i = hexBig(result)
return err
}
type proxyBlock interface {
toBlock() Block
}
type proxyBlockWithTransactions struct {
Number hexInt `json:"number"`
Hash string `json:"hash"`
ParentHash string `json:"parentHash"`
Nonce string `json:"nonce"`
Sha3Uncles string `json:"sha3Uncles"`
LogsBloom string `json:"logsBloom"`
TransactionsRoot string `json:"transactionsRoot"`
StateRoot string `json:"stateRoot"`
Miner string `json:"miner"`
Difficulty hexBig `json:"difficulty"`
TotalDifficulty hexBig `json:"totalDifficulty"`
ExtraData string `json:"extraData"`
Size hexInt `json:"size"`
GasLimit hexInt `json:"gasLimit"`
GasUsed hexInt `json:"gasUsed"`
Timestamp hexInt `json:"timestamp"`
Uncles []string `json:"uncles"`
Transactions []proxyTransaction `json:"transactions"`
}
type proxyBlockWithoutTransactions struct {
Number hexInt `json:"number"`
Hash string `json:"hash"`
ParentHash string `json:"parentHash"`
Nonce string `json:"nonce"`
Sha3Uncles string `json:"sha3Uncles"`
LogsBloom string `json:"logsBloom"`
TransactionsRoot string `json:"transactionsRoot"`
StateRoot string `json:"stateRoot"`
Miner string `json:"miner"`
Difficulty hexBig `json:"difficulty"`
TotalDifficulty hexBig `json:"totalDifficulty"`
ExtraData string `json:"extraData"`
Size hexInt `json:"size"`
GasLimit hexInt `json:"gasLimit"`
GasUsed hexInt `json:"gasUsed"`
Timestamp hexInt `json:"timestamp"`
Uncles []string `json:"uncles"`
Transactions []string `json:"transactions"`
}
func (proxy *proxyBlockWithoutTransactions) toBlock() Block {
block := Block{
Number: uint64(proxy.Number),
Hash: proxy.Hash,
ParentHash: proxy.ParentHash,
Nonce: proxy.Nonce,
Sha3Uncles: proxy.Sha3Uncles,
LogsBloom: proxy.LogsBloom,
TransactionsRoot: proxy.TransactionsRoot,
StateRoot: proxy.StateRoot,
Miner: proxy.Miner,
Difficulty: big.Int(proxy.Difficulty),
TotalDifficulty: big.Int(proxy.TotalDifficulty),
ExtraData: proxy.ExtraData,
// #nosec G701 - copied file from 3rd library, always in range
Size: int(proxy.Size),
// #nosec G701 - copied file from 3rd library, always in range
GasLimit: int(proxy.GasLimit),
// #nosec G701 - copied file from 3rd library, always in range
GasUsed: int(proxy.GasUsed),
// #nosec G701 - copied file from 3rd library, always in range
Timestamp: int(proxy.Timestamp),
Uncles: proxy.Uncles,
}
block.Transactions = make([]Transaction, len(proxy.Transactions))
for i := range proxy.Transactions {
block.Transactions[i] = Transaction{
Hash: proxy.Transactions[i],
}
}
return block
}
type proxyTransaction struct {
Hash string `json:"hash"`
Nonce hexInt `json:"nonce"`
BlockHash string `json:"blockHash"`
BlockNumber *hexInt `json:"blockNumber"`
TransactionIndex *hexInt `json:"transactionIndex"`
From string `json:"from"`
To string `json:"to"`
Value hexBig `json:"value"`
Gas hexInt `json:"gas"`
GasPrice hexBig `json:"gasPrice"`
Input string `json:"input"`
}
func (proxy *proxyBlockWithTransactions) toBlock() Block {
// #nosec G103 - copied file from 3rd library, should be safe enough
return *(*Block)(unsafe.Pointer(proxy))
}