-
Notifications
You must be signed in to change notification settings - Fork 20
/
l2_verifiers.go
140 lines (117 loc) · 3.62 KB
/
l2_verifiers.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package ethverifier
import (
"context"
"errors"
"fmt"
"sort"
"time"
"code.vegaprotocol.io/vega/core/client/eth"
"code.vegaprotocol.io/vega/core/datasource/external/ethcall"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/logging"
"golang.org/x/exp/maps"
)
type L2Clients interface {
Get(chainID string) (*eth.L2Client, *eth.EthereumConfirmations, bool)
}
type EthL2CallEngines interface {
GetOrInstantiate(chainID string) (EthCallEngine, error)
}
type L2Verifiers struct {
log *logging.Logger
witness Witness
ts TimeService
broker Broker
oracleBroadcaster OracleDataBroadcaster
isValidator bool
clients L2Clients
// chain id -> Verifier
verifiers map[string]*Verifier
// eth L2 call engines
ethL2CallEngine EthL2CallEngines
}
func NewL2Verifiers(
log *logging.Logger,
witness Witness,
ts TimeService,
broker Broker,
oracleBroadcaster OracleDataBroadcaster,
clients L2Clients,
callEngines EthL2CallEngines,
isValidator bool,
) (sv *L2Verifiers) {
return &L2Verifiers{
log: log,
witness: witness,
ts: ts,
broker: broker,
oracleBroadcaster: oracleBroadcaster,
clients: clients,
verifiers: map[string]*Verifier{},
ethL2CallEngine: callEngines,
isValidator: isValidator,
}
}
func (v *L2Verifiers) OnEthereumL2ConfigsUpdated(
ctx context.Context, ethCfg *types.EthereumL2Configs,
) error {
// new L2 configured, instatiate the verifier for it.
for _, c := range ethCfg.Configs {
// if already exists, do nothing
if _, ok := v.verifiers[c.ChainID]; ok {
continue
}
v.verifiers[c.ChainID] = v.instantiate(c.ChainID)
}
return nil
}
func (v *L2Verifiers) instantiate(chainID string) *Verifier {
var confs *eth.EthereumConfirmations
if v.isValidator {
var ok bool
_, confs, ok = v.clients.Get(chainID)
if !ok {
v.log.Panic("ethereum client not configured for L2",
logging.String("chain-id", chainID),
)
}
}
ethCallEngine, err := v.ethL2CallEngine.GetOrInstantiate(chainID)
if err != nil {
v.log.Panic("could not get call engine for L2", logging.String("chain-id", chainID))
}
return New(v.log, v.witness, v.ts, v.broker, v.oracleBroadcaster, ethCallEngine, confs, v.isValidator)
}
func (v *L2Verifiers) OnTick(ctx context.Context, t time.Time) {
ids := maps.Keys(v.verifiers)
sort.Strings(ids)
for _, id := range ids {
v.verifiers[id].OnTick(ctx, t)
}
}
func (v *L2Verifiers) ProcessEthereumContractCallResult(callEvent ethcall.ContractCallEvent) error {
if callEvent.SourceChainID == nil {
return errors.New("invalid non l2 event")
}
verifier, ok := v.verifiers[fmt.Sprintf("%v", *callEvent.SourceChainID)]
if !ok {
return errors.New("unsupported l2 chain")
}
return verifier.ProcessEthereumContractCallResult(callEvent)
}
func (v *L2Verifiers) FromProtoSnapshot() {}
func (v *L2Verifiers) ToProtoSnapshot() []byte { return nil }