|
9 | 9 | "errors" |
10 | 10 | "fmt" |
11 | 11 | "io" |
| 12 | + "math" |
12 | 13 | "math/rand" |
13 | 14 | "net/http" |
14 | 15 | "os" |
@@ -69,6 +70,19 @@ type waspCmd struct { |
69 | 70 | logScanner sync.WaitGroup |
70 | 71 | } |
71 | 72 |
|
| 73 | +func Retry(fn func() error, retries int) error { |
| 74 | + var err error |
| 75 | + for i := 0; i < retries; i++ { |
| 76 | + err = fn() |
| 77 | + if err != nil { |
| 78 | + time.Sleep(time.Duration(math.Pow(2, float64(i))) * 500 * time.Millisecond) // Exponential backoff |
| 79 | + continue |
| 80 | + } |
| 81 | + break |
| 82 | + } |
| 83 | + return err |
| 84 | +} |
| 85 | + |
72 | 86 | func New(name string, config *ClusterConfig, dataPath string, t *testing.T, log log.Logger, l1PacakgeID *iotago.PackageID) *Cluster { |
73 | 87 | if log == nil { |
74 | 88 | if t == nil { |
@@ -209,37 +223,48 @@ func (clu *Cluster) InitDistributedKeyGeneration(committeeNodeCount int) ([]int, |
209 | 223 | } |
210 | 224 |
|
211 | 225 | func (clu *Cluster) RunDistributedKeyGeneration(committeeNodes []int, threshold uint16, timeout ...time.Duration) (*cryptolib.Address, error) { |
212 | | - if threshold == 0 { |
213 | | - threshold = (uint16(len(committeeNodes))*2)/3 + 1 |
214 | | - } |
215 | | - apiHosts := clu.Config.APIHosts(committeeNodes) |
| 226 | + var addr *cryptolib.Address |
| 227 | + var err error |
| 228 | + err = Retry(func() error { |
| 229 | + if threshold == 0 { |
| 230 | + threshold = (uint16(len(committeeNodes))*2)/3 + 1 |
| 231 | + } |
| 232 | + apiHosts := clu.Config.APIHosts(committeeNodes) |
216 | 233 |
|
217 | | - peerPubKeys := make([]string, 0) |
218 | | - for _, i := range committeeNodes { |
219 | | - //nolint:bodyclose // false positive |
220 | | - peeringNodeInfo, _, err := clu.WaspClient(i).NodeAPI.GetPeeringIdentity(context.Background()).Execute() |
221 | | - if err != nil { |
222 | | - return nil, err |
| 234 | + peerPubKeys := make([]string, 0) |
| 235 | + for _, i := range committeeNodes { |
| 236 | + //nolint:bodyclose // false positive |
| 237 | + peeringNodeInfo, _, err := clu.WaspClient(i).NodeAPI.GetPeeringIdentity(context.Background()).Execute() |
| 238 | + if err != nil { |
| 239 | + return err |
| 240 | + } |
| 241 | + |
| 242 | + peerPubKeys = append(peerPubKeys, peeringNodeInfo.PublicKey) |
223 | 243 | } |
224 | 244 |
|
225 | | - peerPubKeys = append(peerPubKeys, peeringNodeInfo.PublicKey) |
226 | | - } |
| 245 | + distKeyGenInitiatorIndex := rand.Intn(len(apiHosts)) |
| 246 | + client := clu.WaspClientFromHostName(apiHosts[distKeyGenInitiatorIndex]) |
227 | 247 |
|
228 | | - distKeyGenInitiatorIndex := rand.Intn(len(apiHosts)) |
229 | | - client := clu.WaspClientFromHostName(apiHosts[distKeyGenInitiatorIndex]) |
| 248 | + addr, err = apilib.RunDistributedKeyGeneration(context.Background(), client, peerPubKeys, threshold, timeout...) |
| 249 | + return err |
| 250 | + }, 5) |
| 251 | + |
| 252 | + if err != nil { |
| 253 | + return nil, err |
| 254 | + } |
230 | 255 |
|
231 | | - return apilib.RunDistributedKeyGeneration(context.Background(), client, peerPubKeys, threshold, timeout...) |
| 256 | + return addr, nil |
232 | 257 | } |
233 | 258 |
|
234 | 259 | func (clu *Cluster) DeployChainWithDistKeyGen(allPeers, committeeNodes []int, quorum uint16, blockKeepAmount ...int32) (*Chain, error) { |
235 | 260 | stateAddr, err := clu.RunDistributedKeyGeneration(committeeNodes, quorum) |
236 | 261 | if err != nil { |
237 | 262 | return nil, err |
238 | 263 | } |
239 | | - return clu.DeployChain(allPeers, committeeNodes, quorum, stateAddr, blockKeepAmount...) |
| 264 | + return clu.DeployChain(allPeers, committeeNodes, quorum, stateAddr, false, blockKeepAmount...) |
240 | 265 | } |
241 | 266 |
|
242 | | -func (clu *Cluster) DeployChain(allPeers, committeeNodes []int, quorum uint16, stateAddr *cryptolib.Address, blockKeepAmount ...int32) (*Chain, error) { |
| 267 | +func (clu *Cluster) DeployChain(allPeers, committeeNodes []int, quorum uint16, stateAddr *cryptolib.Address, deployTestContracts bool, blockKeepAmount ...int32) (*Chain, error) { |
243 | 268 | if len(allPeers) == 0 { |
244 | 269 | allPeers = clu.Config.AllNodes() |
245 | 270 | } |
@@ -279,7 +304,7 @@ func (clu *Cluster) DeployChain(allPeers, committeeNodes []int, quorum uint16, s |
279 | 304 | isc.NewAddressAgentID(chain.OriginatorAddress()), |
280 | 305 | 1074, |
281 | 306 | blockKeepAmountVal, |
282 | | - false, |
| 307 | + deployTestContracts, |
283 | 308 | ).Encode() |
284 | 309 |
|
285 | 310 | getCoinsRes, err := l1Client.GetCoins( |
|
0 commit comments