/
panarchy.go
331 lines (271 loc) · 8.73 KB
/
panarchy.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package panarchy
import (
"math/big"
"sync"
"fmt"
"encoding/binary"
"os"
"encoding/json"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/params/types/ctypes"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie/trienode"
"github.com/ethereum/go-ethereum/trie/triestate"
"github.com/ethereum/go-ethereum/log"
)
const (
compensateForPossibleReorg = 20
)
type StorageSlots struct {
election []byte
hashOnion []byte
validSince []byte
}
type Schedule struct {
genesis uint64
period uint64
}
type ValidatorContract struct {
slots StorageSlots
addr common.Address
schedule Schedule
}
type HashOnion struct {
Root common.Hash `json:"root"`
Layers int `json:"layers"`
}
type Panarchy struct {
config *ctypes.PanarchyConfig
contract ValidatorContract
hashOnion HashOnion
lock sync.RWMutex
signer common.Address
signFn SignerFn
state *state.StateDB
}
type OnionStored struct {
Hash common.Hash
ValidSince *big.Int
}
func pad(val []byte) []byte {
return common.LeftPadBytes(val, 32)
}
func weeksToSeconds(weeks uint64) uint64 {
return weeks*7*24*60*60
}
type SignerFn func(signer accounts.Account, mimeType string, message []byte) ([]byte, error)
func New(config *ctypes.PanarchyConfig, db ethdb.Database) *Panarchy {
return &Panarchy{
config: config,
contract: ValidatorContract{
slots: StorageSlots{
election: pad([]byte{2}),
hashOnion: pad([]byte{3}),
validSince: pad([]byte{4}),
},
addr: common.HexToAddress("0x0000000000000000000000000000000000000020"),
schedule: Schedule {
genesis: 1709960400,
period: weeksToSeconds(4),
},
},
}
}
func (p *Panarchy) blockValidator(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB) error {
p.state = state
signer, err := p.Author(header)
if err != nil {
return err
}
parentHeader := chain.GetHeaderByHash(header.ParentHash)
if len(header.Extra) != 129 {
return errMissingExtraData
}
parentTrieRoot := common.BytesToHash(parentHeader.Extra[: 32])
trie, err := p.state.Database().OpenTrie(parentTrieRoot)
if err != nil {
return fmt.Errorf("Failed to open trie: %v", err)
}
onion, err := p.hashOnionFromStorageOrNew(signer, header.Number, trie)
if err != nil {
return err
}
preimage := common.BytesToHash(header.Extra[32: 64])
hash := crypto.Keccak256Hash(preimage.Bytes())
if hash != onion.Hash {
return fmt.Errorf("block rng hash not valid")
}
onion.Hash = preimage
newTrieRoot, err := p.updateAndCloseTrie(signer, onion, header.Number.Uint64(), parentTrieRoot, trie)
if err != nil {
return err
}
signedTrieRoot := common.BytesToHash(header.Extra[: 32])
if signedTrieRoot != newTrieRoot {
return fmt.Errorf("The RNG trie root is not valid")
}
skipped := new(big.Int).Sub(common.Big1, parentHeader.Difficulty)
if signer != p.getValidator(header, skipped) {
return fmt.Errorf("Validator is not elected to sign the block")
}
return nil
}
func (p *Panarchy) blockProducer(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB) error {
p.state = state
signer := p.signer
parentHeader := chain.GetHeaderByHash(header.ParentHash)
if len(parentHeader.Extra) != 129 {
return errMissingExtraData
}
trieRoot := common.BytesToHash(parentHeader.Extra[: 32])
trie, err := state.Database().OpenTrie(trieRoot)
if err != nil {
return fmt.Errorf("Failed to open trie: %v", err)
}
onion, err := p.hashOnionFromStorageOrNew(signer, header.Number, trie)
if err != nil {
return err
}
if err := p.getHashOnionPreimage(onion); err != nil {
return err
}
newTrieRoot, err := p.updateAndCloseTrie(signer, onion, header.Number.Uint64(), trieRoot, trie)
if err != nil {
return err
}
header.Extra = append(newTrieRoot.Bytes(), onion.Hash.Bytes()...)
return nil
}
func (p *Panarchy) hashOnionFromStorageOrNew(signer common.Address, blockNumber *big.Int, trie state.Trie) (*OnionStored, error) {
addrAndSlot := append(pad(signer.Bytes()), p.contract.slots.validSince...)
key := crypto.Keccak256Hash(addrAndSlot)
data := p.state.GetState(p.contract.addr, key)
validSince := new(big.Int).SetBytes(data.Bytes())
if validSince == common.Big0 {
return nil, fmt.Errorf("no registered hash onion in validator contract")
}
value, err := trie.GetStorage(common.Address{}, signer.Bytes())
if err != nil {
return nil, fmt.Errorf("get hash onion failed: %w", err)
}
var onion OnionStored
if len(value) != 0 {
if err := rlp.DecodeBytes(value, &onion); err != nil {
return nil, fmt.Errorf("decode hash onion failed: %w", err)
}
} else {
onion = OnionStored{}
}
if blockNumber.Cmp(validSince) >= 0 {
if onion.ValidSince == nil || onion.ValidSince.Cmp(validSince) < 0 {
addrAndSlot := append(pad(signer.Bytes()), p.contract.slots.hashOnion...)
key := crypto.Keccak256Hash(addrAndSlot)
onion.Hash = p.state.GetState(p.contract.addr, key)
onion.ValidSince = validSince
}
}
return &onion, nil
}
func (p *Panarchy) getHashOnionPreimage(onion *OnionStored) error {
preimage := p.hashOnion.Root.Bytes()
for i := 0; i < p.hashOnion.Layers-1; i++ {
preimage = crypto.Keccak256(preimage)
}
var hash []byte
for i := 0; i < compensateForPossibleReorg; i++ {
hash = crypto.Keccak256(preimage)
if onion.Hash == common.BytesToHash(hash) {
onion.Hash = common.BytesToHash(preimage)
if i == 0 {
p.hashOnion.Layers--
if err := p.WriteHashOnion(); err != nil {
return fmt.Errorf("Unable to update %s", p.config.HashOnionFilePath)
}
}
return nil
}
preimage = hash
}
return fmt.Errorf("Validator hash onion cannot be verified")
}
func (p *Panarchy) updateAndCloseTrie(signer common.Address, onion *OnionStored, blockNumber uint64, trieRoot common.Hash, trie state.Trie) (common.Hash, error) {
encoded, err := rlp.EncodeToBytes(onion)
if err != nil {
return common.Hash{}, fmt.Errorf("Error encoding hashOnion:", err)
}
if err := trie.UpdateStorage(common.Address{}, signer.Bytes(), encoded); err != nil {
return common.Hash{}, fmt.Errorf("update hash onion storage failed: %w", err)
}
newTrieRoot, nodes, err := trie.Commit(false)
if err != nil {
return common.Hash{}, fmt.Errorf("Commit trie failed", err)
}
triedb := p.state.Database().TrieDB()
mergedNodes := trienode.NewWithNodeSet(nodes)
if err := triedb.Update(newTrieRoot, trieRoot, blockNumber, mergedNodes, &triestate.Set{}); err != nil {
return common.Hash{}, fmt.Errorf("Failed to store tree to database", err)
}
if err := triedb.Commit(newTrieRoot, false); err != nil {
return common.Hash{}, fmt.Errorf("Failed to commit tree to database", err)
}
return newTrieRoot, nil
}
func (p *Panarchy) getValidator(header *types.Header, skipped *big.Int) common.Address {
schedule := ((header.Time - p.contract.schedule.genesis) / p.contract.schedule.period)
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, schedule)
index := pad(buf)
trieRoot := common.BytesToHash(header.Extra[: 32])
random := new(big.Int).SetBytes(trieRoot.Bytes())
random.Add(random, skipped)
slot := p.contract.slots.election
lengthKey := crypto.Keccak256Hash(append(index, slot...))
electionLength := p.state.GetState(p.contract.addr, lengthKey)
modulus := new(big.Int).SetBytes(electionLength.Bytes())
random.Mod(random, modulus)
key := new(big.Int).SetBytes(crypto.Keccak256(crypto.Keccak256(append(index, slot...))))
key.Add(key, random)
validator := p.state.GetState(p.contract.addr, common.BytesToHash(key.Bytes()))
return common.BytesToAddress(validator.Bytes())
}
func (p *Panarchy) LoadHashOnion() error {
filePath := p.config.HashOnionFilePath
file, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("error opening file: %v, hashOnionFilePath: %v", err, filePath)
}
defer file.Close()
err = json.NewDecoder(file).Decode(&p.hashOnion)
if err != nil {
return fmt.Errorf("error decoding JSON: %v", err)
}
return nil
}
func (p *Panarchy) WriteHashOnion() error {
filePath := p.config.HashOnionFilePath
file, err := os.Create(filePath)
if err != nil {
return fmt.Errorf("error creating file: %v, hashOnionFilePath: %v", err, filePath)
}
defer file.Close()
err = json.NewEncoder(file).Encode(p.hashOnion)
if err != nil {
return fmt.Errorf("error encoding hashOnion to JSON: %v", err)
}
return nil
}
func (p *Panarchy) Authorize(signer common.Address, signFn SignerFn) {
p.lock.Lock()
defer p.lock.Unlock()
p.signer = signer
p.signFn = signFn
if err := p.LoadHashOnion(); err != nil {
log.Error("LoadHashOnion error:", err)
}
}