-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathsequencers.go
More file actions
188 lines (168 loc) · 5.44 KB
/
sequencers.go
File metadata and controls
188 lines (168 loc) · 5.44 KB
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
package node
import (
"bytes"
"errors"
"fmt"
"math/big"
"slices"
"time"
"github.com/morph-l2/go-ethereum/common"
"github.com/morph-l2/go-ethereum/common/hexutil"
"github.com/morph-l2/go-ethereum/crypto/bls12381"
"github.com/tendermint/tendermint/blssignatures"
"github.com/tendermint/tendermint/crypto/ed25519"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
)
const tmKeySize = ed25519.PubKeySize
type validatorInfo struct {
address common.Address
blsPubKey blssignatures.PublicKey
}
func (e *Executor) getBlsPubKeyByTmKey(tmPubKey []byte) (blssignatures.PublicKey, bool) {
var tmKey [32]byte
copy(tmKey[:], tmPubKey)
val, found := e.valsByTmKey[tmKey]
if !found {
return blssignatures.PublicKey{}, false
}
return val.blsPubKey, true
}
func (e *Executor) VerifySignature(tmPubKey []byte, messageHash []byte, blsSig []byte) (bool, error) {
if e.devSequencer {
e.logger.Info("we are in dev mode, do not verify the bls signature")
return true, nil
}
if len(e.valsByTmKey) == 0 {
return false, errors.New("no available sequencers found in layer2")
}
blsKey, found := e.getBlsPubKeyByTmKey(tmPubKey)
if !found {
return false, errors.New("it is not a valid sequencer")
}
sig, err := blssignatures.SignatureFromBytes(blsSig)
if err != nil {
e.logger.Error("failed to recover bytes to signature", "error", err)
return false, fmt.Errorf("failed to recover bytes to signature, error: %v", err)
}
return blssignatures.VerifySignature(sig, messageHash, blsKey)
}
func (e *Executor) sequencerSetUpdates() ([][]byte, error) {
seqHash, err := e.sequencerCaller.SequencerSetVerifyHash(nil)
if err != nil {
return nil, err
}
if e.currentSeqHash != nil && bytes.Equal(e.currentSeqHash[:], seqHash[:]) {
return e.nextValidators, nil
}
sequencerSet0, err := e.sequencerCaller.GetSequencerSet0(nil)
if err != nil {
return nil, err
}
sequencerSet1, err := e.sequencerCaller.GetSequencerSet1(nil)
if err != nil {
return nil, err
}
sequencerSet2, err := e.sequencerCaller.GetSequencerSet2(nil)
if err != nil {
return nil, err
}
cache := make(map[common.Address]struct{})
requestAddrs := make([]common.Address, 0)
for _, addr := range append(append(sequencerSet0, sequencerSet1...), sequencerSet2...) {
_, ok := cache[addr]
if !ok {
cache[addr] = struct{}{}
requestAddrs = append(requestAddrs, addr)
}
}
stakesInfo, err := e.l2StakingCaller.GetStakesInfo(nil, requestAddrs)
if err != nil {
e.logger.Error("failed to GetStakesInfo", "error", err)
return nil, err
}
valsByTmKey := make(map[[tmKeySize]byte]validatorInfo)
nextValidators := make([][]byte, 0)
for i := range stakesInfo {
blsPK, err := decodeBlsPubKey(stakesInfo[i].BlsKey)
if err != nil {
e.logger.Error("failed to decode bls key", "key bytes", hexutil.Encode(stakesInfo[i].BlsKey), "error", err)
return nil, err
}
// sequencerSet2 is the latest updated sequencer set which is considered as the next validator set for tendermint
if slices.Contains(sequencerSet2, stakesInfo[i].Addr) {
nextValidators = append(nextValidators, stakesInfo[i].TmKey[:])
}
valsByTmKey[stakesInfo[i].TmKey] = validatorInfo{
address: stakesInfo[i].Addr,
blsPubKey: blsPK,
}
}
e.logger.Info("sequencers updates, sequencer verified hash changed")
e.valsByTmKey = valsByTmKey
e.nextValidators = nextValidators
e.currentSeqHash = &seqHash
return nextValidators, nil
}
func (e *Executor) batchParamsUpdates(height uint64) (*tmproto.BatchParams, error) {
var (
batchBlockInterval, batchTimeout *big.Int
err error
)
if batchBlockInterval, err = e.govCaller.BatchBlockInterval(nil); err != nil {
return nil, err
}
if batchTimeout, err = e.govCaller.BatchTimeout(nil); err != nil {
return nil, err
}
changed := e.batchParams.BlocksInterval != batchBlockInterval.Int64() ||
int64(e.batchParams.Timeout.Seconds()) != batchTimeout.Int64()
if changed {
e.batchParams.BlocksInterval = batchBlockInterval.Int64()
e.batchParams.Timeout = time.Duration(batchTimeout.Int64() * int64(time.Second))
e.logger.Info("batch params changed", "height", height,
"batchBlockInterval", batchBlockInterval.Int64(),
"batchTimeout", batchTimeout.Int64())
return &tmproto.BatchParams{
BlocksInterval: batchBlockInterval.Int64(),
Timeout: time.Duration(batchTimeout.Int64() * int64(time.Second)),
}, nil
}
return nil, nil
}
func (e *Executor) updateSequencerSet() ([][]byte, error) {
validatorUpdates, err := e.sequencerSetUpdates()
if err != nil {
e.logger.Error("failed to get sequencer set from geth", "err", err)
return nil, err
}
var tmPKBz [tmKeySize]byte
copy(tmPKBz[:], e.tmPubKey)
_, isSequencer := e.valsByTmKey[tmPKBz]
if !e.isSequencer && isSequencer {
e.logger.Info("I am a sequencer, start to launch syncer")
if e.syncer == nil {
syncer, err := e.newSyncerFunc()
if err != nil {
e.logger.Error("failed to create syncer", "error", err)
return nil, err
}
e.syncer = syncer
e.l1MsgReader = syncer // syncer works as l1MsgReader
e.syncer.Start()
} else {
go e.syncer.Start()
}
} else if e.isSequencer && !isSequencer {
e.logger.Info("I am not a sequencer, stop syncing")
e.syncer.Stop()
}
e.isSequencer = isSequencer
return validatorUpdates, nil
}
func decodeBlsPubKey(in []byte) (blssignatures.PublicKey, error) {
g2P, err := bls12381.NewG2().DecodePoint(in)
if err != nil {
return blssignatures.PublicKey{}, err
}
return blssignatures.NewTrustedPublicKey(g2P), nil
}