-
Notifications
You must be signed in to change notification settings - Fork 0
/
header.go
160 lines (129 loc) · 3.02 KB
/
header.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
package types
import (
"encoding/binary"
"fmt"
"sync/atomic"
"github.com/esportzvio/frietorchain/helper/hex"
)
// Header represents a block header in the Ethereum blockchain.
type Header struct {
ParentHash Hash
Sha3Uncles Hash
Miner []byte
StateRoot Hash
TxRoot Hash
ReceiptsRoot Hash
LogsBloom Bloom
Difficulty uint64
Number uint64
GasLimit uint64
GasUsed uint64
Timestamp uint64
ExtraData []byte
MixHash Hash
Nonce Nonce
Hash Hash
// BaseFee was added by EIP-1559 and is ignored in legacy headers.
BaseFee uint64 `json:"baseFeePerGas"`
}
func (h *Header) Equal(hh *Header) bool {
return h.Hash == hh.Hash
}
func (h *Header) HasBody() bool {
return h.TxRoot != EmptyRootHash || h.Sha3Uncles != EmptyUncleHash
}
func (h *Header) HasReceipts() bool {
return h.ReceiptsRoot != EmptyRootHash
}
func (h *Header) SetNonce(i uint64) {
binary.BigEndian.PutUint64(h.Nonce[:], i)
}
func (h *Header) IsGenesis() bool {
return h.Hash != ZeroHash && h.Number == 0
}
type Nonce [8]byte
func (n Nonce) String() string {
return hex.EncodeToHex(n[:])
}
// MarshalText implements encoding.TextMarshaler
func (n Nonce) MarshalText() ([]byte, error) {
return []byte(n.String()), nil
}
func (h *Header) Copy() *Header {
newHeader := &Header{
ParentHash: h.ParentHash,
Sha3Uncles: h.Sha3Uncles,
StateRoot: h.StateRoot,
TxRoot: h.TxRoot,
ReceiptsRoot: h.ReceiptsRoot,
MixHash: h.MixHash,
Hash: h.Hash,
LogsBloom: h.LogsBloom,
Nonce: h.Nonce,
Difficulty: h.Difficulty,
Number: h.Number,
GasLimit: h.GasLimit,
GasUsed: h.GasUsed,
Timestamp: h.Timestamp,
BaseFee: h.BaseFee,
}
newHeader.Miner = make([]byte, len(h.Miner))
copy(newHeader.Miner[:], h.Miner[:])
newHeader.ExtraData = make([]byte, len(h.ExtraData))
copy(newHeader.ExtraData[:], h.ExtraData[:])
return newHeader
}
type Body struct {
Transactions []*Transaction
Uncles []*Header
}
type FullBlock struct {
Block *Block
Receipts []*Receipt
}
type Block struct {
Header *Header
Transactions []*Transaction
Uncles []*Header
// Cache
size atomic.Pointer[uint64]
}
func (b *Block) Hash() Hash {
return b.Header.Hash
}
func (b *Block) Number() uint64 {
return b.Header.Number
}
func (b *Block) ParentHash() Hash {
return b.Header.ParentHash
}
func (b *Block) Body() *Body {
return &Body{
Transactions: b.Transactions,
Uncles: b.Uncles,
}
}
func (b *Block) Size() uint64 {
sizePtr := b.size.Load()
if sizePtr == nil {
bytes := b.MarshalRLP()
size := uint64(len(bytes))
b.size.Store(&size)
return size
}
return *sizePtr
}
func (b *Block) String() string {
str := fmt.Sprintf(`Block(#%v):`, b.Number())
return str
}
// WithSeal returns a new block with the data from b but the header replaced with
// the sealed one.
func (b *Block) WithSeal(header *Header) *Block {
cpy := *header
return &Block{
Header: &cpy,
Transactions: b.Transactions,
Uncles: b.Uncles,
}
}