-
Notifications
You must be signed in to change notification settings - Fork 33
/
provider.go
153 lines (132 loc) · 4.43 KB
/
provider.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
package netconf
import (
"context"
"fmt"
"strconv"
"time"
"github.com/omni-network/omni/contracts/bindings"
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/evmchain"
"github.com/omni-network/omni/lib/expbackoff"
"github.com/omni-network/omni/lib/log"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
)
// AwaitOnChain blocks and returns network configuration as soon as it can be loaded from the on-chain registry.
// It only returns an error if the context is canceled.
func AwaitOnChain(ctx context.Context, netID ID, portalRegistry *bindings.PortalRegistry, expected []string) (Network, error) {
if netID == Simnet {
// Create a simnet netID (single binary with mocked clients).
dummyAddr := common.HexToAddress("0x000000000000000000000000000000000000dead")
return Network{
ID: netID,
Chains: []Chain{
{
ID: netID.Static().OmniExecutionChainID,
Name: "omni_evm",
BlockPeriod: time.Millisecond * 500, // Speed up block times for testing
PortalAddress: dummyAddr,
},
{
ID: netID.Static().OmniConsensusChainIDUint64(),
Name: "omni_consensus",
DeployHeight: 1, // Validator sets start at height 1, not 0.
BlockPeriod: time.Millisecond * 500, // Speed up block times for testing
PortalAddress: dummyAddr,
},
{
ID: 100, // todo(Lazar): make it dynamic. this is coming from lib/xchain/provider/mock.go
Name: "mock_l1",
PortalAddress: dummyAddr,
},
{
ID: 200, // todo(Lazar): make it dynamic. this is coming from lib/xchain/provider/mock.go
Name: "mock_l2",
PortalAddress: dummyAddr,
},
},
}, nil
}
backoff := expbackoff.New(ctx)
for {
if ctx.Err() != nil {
return Network{}, errors.Wrap(ctx.Err(), "provider timeout")
}
portals, err := portalRegistry.List(&bind.CallOpts{Context: ctx})
if err != nil {
log.Warn(ctx, "Failed fetching xchain registry from omni_evm (will retry)", err)
backoff()
continue
}
network := networkFromPortals(netID, portals)
if !containsAll(network, expected) {
log.Info(ctx, "XChain registry doesn't contain all expected chains (will retry)", ""+
"expected", expected, "actual", network.ChainNamesByIDs())
backoff()
continue
}
log.Info(ctx, "XChain network configuration initialized from on-chain registry", "chains", network.ChainNamesByIDs())
return network, nil
}
}
// containsAll returns true if the network contains the all expected chains (by name or ID).
func containsAll(network Network, expected []string) bool {
want := make(map[string]struct{}, len(expected))
for _, name := range expected {
want[name] = struct{}{}
}
for _, chain := range network.Chains {
delete(want, chain.Name)
delete(want, strconv.FormatUint(chain.ID, 10))
}
return len(want) == 0
}
func networkFromPortals(network ID, portals []bindings.PortalRegistryDeployment) Network {
var chains []Chain
for _, portal := range portals {
metadata := MetadataByID(network, portal.ChainId)
chains = append(chains, Chain{
ID: portal.ChainId,
Name: metadata.Name,
PortalAddress: portal.Addr,
DeployHeight: portal.DeployHeight,
BlockPeriod: metadata.BlockPeriod,
FinalizationStrat: FinalizationStrat(portal.FinalizationStrat),
})
}
// Add omni consensus chain
consensusMeta := MetadataByID(network, network.Static().OmniConsensusChainIDUint64())
chains = append(chains, Chain{
ID: consensusMeta.ChainID,
Name: consensusMeta.Name,
BlockPeriod: consensusMeta.BlockPeriod,
FinalizationStrat: StratFinalized,
DeployHeight: 1, // ValidatorSets start at 1, not 0.
})
return Network{
ID: network,
Chains: chains,
}
}
func MetadataByID(network ID, chainID uint64) evmchain.Metadata {
if IsOmniConsensus(network, chainID) {
return evmchain.Metadata{
ChainID: chainID,
Name: "omni_consensus",
BlockPeriod: time.Second * 2,
}
}
if meta, ok := evmchain.MetadataByID(chainID); ok { // If well-known chain, use it.
return meta
}
return evmchain.Metadata{
ChainID: chainID,
Name: fmt.Sprintf("unknown_%d", chainID),
BlockPeriod: time.Second * 2,
}
}
func ChainNamer(network ID) func(uint64) string {
return func(chainID uint64) string {
return MetadataByID(network, chainID).Name
}
}