-
Notifications
You must be signed in to change notification settings - Fork 1
/
header.go
185 lines (159 loc) · 4.85 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2020 Snowfork
// SPDX-License-Identifier: LGPL-3.0-only
package ethereum
import (
"fmt"
"math/big"
etypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"github.com/sirupsen/logrus"
"github.com/snowfork/ethashproof"
"github.com/snowfork/ethashproof/ethash"
"github.com/snowfork/go-substrate-rpc-client/v2/scale"
types "github.com/snowfork/go-substrate-rpc-client/v2/types"
"github.com/Polkadex-Substrate/eth-relayer/chain"
)
type HeaderID struct {
Number types.U64
Hash types.H256
}
type headerSCALE struct {
ParentHash types.H256
Timestamp types.U64
Number types.U64
Author types.H160
TransactionsRoot types.H256
OmmersHash types.H256
ExtraData types.Bytes
StateRoot types.H256
ReceiptsRoot types.H256
LogsBloom types.Bytes256
GasUsed types.U256
GasLimit types.U256
Difficulty types.U256
Seal []types.Bytes
}
type Header struct {
Fields headerSCALE
header *etypes.Header
}
func (h *Header) Decode(decoder scale.Decoder) error {
var fields headerSCALE
err := decoder.Decode(&fields)
if err != nil {
return err
}
h.Fields = fields
return nil
}
func (h Header) Encode(encoder scale.Encoder) error {
return encoder.Encode(h.Fields)
}
func (h *Header) ID() HeaderID {
return HeaderID{
Number: h.Fields.Number,
Hash: types.NewH256(h.header.Hash().Bytes()),
}
}
type DoubleNodeWithMerkleProof struct {
DagNodes [2]types.H512
Proof [][16]byte
}
func MakeHeaderFromEthHeader(
gethheader *etypes.Header,
proofcache *ethashproof.DatasetMerkleTreeCache,
log *logrus.Entry,
) (*chain.Header, error) {
headerData, err := MakeHeaderData(gethheader)
if err != nil {
return nil, err
}
proofData, err := MakeProofData(gethheader, proofcache)
if err != nil {
return nil, err
}
log.WithFields(logrus.Fields{
"blockHash": gethheader.Hash().Hex(),
"blockNumber": gethheader.Number,
}).Debug("Generated header from Ethereum header")
return &chain.Header{HeaderData: *headerData, ProofData: proofData}, nil
}
func MakeHeaderData(gethheader *etypes.Header) (*Header, error) {
// Convert Geth types to their Substrate Go client counterparts that match our node
var blockNumber uint64
if !gethheader.Number.IsUint64() {
return nil, fmt.Errorf("gethheader.Number is not uint64")
}
blockNumber = gethheader.Number.Uint64()
var gasUsed, gasLimit big.Int
gasUsed.SetUint64(gethheader.GasUsed)
gasLimit.SetUint64(gethheader.GasLimit)
var bloomBytes [256]byte
copy(bloomBytes[:], gethheader.Bloom.Bytes())
mixHashRLP, err := rlp.EncodeToBytes(gethheader.MixDigest)
if err != nil {
return nil, err
}
nonceRLP, err := rlp.EncodeToBytes(gethheader.Nonce)
if err != nil {
return nil, err
}
return &Header{
Fields: headerSCALE{
ParentHash: types.NewH256(gethheader.ParentHash.Bytes()),
Timestamp: types.NewU64(gethheader.Time),
Number: types.NewU64(blockNumber),
Author: types.NewH160(gethheader.Coinbase.Bytes()),
TransactionsRoot: types.NewH256(gethheader.TxHash.Bytes()),
OmmersHash: types.NewH256(gethheader.UncleHash.Bytes()),
ExtraData: types.NewBytes(gethheader.Extra),
StateRoot: types.NewH256(gethheader.Root.Bytes()),
ReceiptsRoot: types.NewH256(gethheader.ReceiptHash.Bytes()),
LogsBloom: types.NewBytes256(bloomBytes),
GasUsed: types.NewU256(gasUsed),
GasLimit: types.NewU256(gasLimit),
Difficulty: types.NewU256(*gethheader.Difficulty),
Seal: []types.Bytes{mixHashRLP, nonceRLP},
},
header: gethheader,
}, nil
}
func MakeProofData(
gethheader *etypes.Header,
proofcache *ethashproof.DatasetMerkleTreeCache,
) ([]DoubleNodeWithMerkleProof, error) {
// Generate merkle proofs for Ethash
blockNumber := gethheader.Number.Uint64()
indices := ethash.Instance.GetVerificationIndices(
blockNumber,
ethash.Instance.SealHash(gethheader),
gethheader.Nonce.Uint64(),
)
proofData := make([]DoubleNodeWithMerkleProof, len(indices))
for i, index := range indices {
element, proof, err := ethashproof.CalculateProof(blockNumber, index, proofcache)
if err != nil {
return nil, err
}
es := element.ToUint256Array()
node1Bytes := make([]byte, 64)
node2Bytes := make([]byte, 64)
// Each 32 byte sequence is left-padded with 0
copy(node1Bytes[32-len(es[0].Bytes()):32], es[0].Bytes())
copy(node1Bytes[64-len(es[1].Bytes()):], es[1].Bytes())
copy(node2Bytes[32-len(es[2].Bytes()):32], es[2].Bytes())
copy(node2Bytes[64-len(es[3].Bytes()):], es[3].Bytes())
proofH128 := make([][16]byte, len(proof))
for j, pr := range proof {
proofH128[j] = [16]byte(pr)
}
proofData[i] = DoubleNodeWithMerkleProof{
DagNodes: [2]types.H512{
types.NewH512(node1Bytes),
types.NewH512(node2Bytes),
},
Proof: proofH128,
}
}
return proofData, nil
}