node/pkg/watchers: add RunWithScissors

This commit is contained in:
Paul Noel 2023-03-29 22:56:50 +00:00 committed by Evan Gray
parent a846036b6e
commit 9b8bed4dbf
1 changed files with 101 additions and 56 deletions

View File

@ -154,9 +154,7 @@ func (e *Watcher) Run(ctx context.Context) error {
logger.Info("Algorand watcher connecting to indexer ", zap.String("url", e.indexerRPC)) logger.Info("Algorand watcher connecting to indexer ", zap.String("url", e.indexerRPC))
logger.Info("Algorand watcher connecting to RPC node ", zap.String("url", e.algodRPC)) logger.Info("Algorand watcher connecting to RPC node ", zap.String("url", e.algodRPC))
timer := time.NewTicker(time.Second * 1) // The indexerClient is used to get the transaction by hash for a re-observation
defer timer.Stop()
indexerClient, err := indexer.MakeClient(e.indexerRPC, e.indexerToken) indexerClient, err := indexer.MakeClient(e.indexerRPC, e.indexerToken)
if err != nil { if err != nil {
logger.Error("indexer make client", zap.Error(err)) logger.Error("indexer make client", zap.Error(err))
@ -182,10 +180,89 @@ func (e *Watcher) Run(ctx context.Context) error {
logger.Info(fmt.Sprintf("first block %d", e.next_round)) logger.Info(fmt.Sprintf("first block %d", e.next_round))
// Create the timer for the getting the block height
timer := time.NewTicker(time.Second * 1)
defer timer.Stop()
// Create an error channel
errC := make(chan error)
defer close(errC)
// Signal that basic initialization is complete
readiness.SetReady(e.readinessSync)
// Signal to the supervisor that this runnable has finished initialization
supervisor.Signal(ctx, supervisor.SignalHealthy)
// Create the go routine to handle events from core contract
common.RunWithScissors(ctx, errC, "core_events_and_block_height", func(ctx context.Context) error {
logger.Info("Entering core_events_and_block_height...")
for { for {
select { select {
case err := <-errC:
logger.Error("core_events_and_block_height died", zap.Error(err))
return fmt.Errorf("core_events_and_block_height died: %w", err)
case <-ctx.Done(): case <-ctx.Done():
return nil logger.Error("core_events_and_block_height context done")
return ctx.Err()
case <-timer.C:
status, err := algodClient.Status().Do(context.Background())
if err != nil {
logger.Error("algodClient.Status", zap.Error(err))
p2p.DefaultRegistry.AddErrorCount(vaa.ChainIDAlgorand, 1)
continue
}
if e.next_round <= status.LastRound {
for {
block, err := algodClient.Block(e.next_round).Do(context.Background())
if err != nil {
logger.Error("algodClient.Block %d: %s", zap.Uint64("next_round", e.next_round), zap.Error(err))
p2p.DefaultRegistry.AddErrorCount(vaa.ChainIDAlgorand, 1)
break
}
if block.Round == 0 {
break
}
for _, element := range block.Payset {
lookAtTxn(e, element, block, logger)
}
e.next_round = e.next_round + 1
if e.next_round > status.LastRound {
break
}
}
}
currentAlgorandHeight.Set(float64(status.LastRound))
p2p.DefaultRegistry.SetNetworkStats(vaa.ChainIDAlgorand, &gossipv1.Heartbeat_Network{
Height: int64(status.LastRound),
ContractAddress: fmt.Sprintf("%d", e.appid),
})
readiness.SetReady(e.readinessSync)
}
}
})
// Create the go routine to listen for re-observation requests
common.RunWithScissors(ctx, errC, "fetch_obvs_req", func(ctx context.Context) error {
logger.Info("Entering fetch_obvs_req...")
for {
select {
case err := <-errC:
logger.Error("fetch_obvs_req died", zap.Error(err))
return fmt.Errorf("fetch_obvs_req died: %w", err)
case <-ctx.Done():
logger.Error("fetch_obvs_req context done")
return ctx.Err()
case r := <-e.obsvReqC: case r := <-e.obsvReqC:
if vaa.ChainID(r.ChainId) != vaa.ChainIDAlgorand { if vaa.ChainID(r.ChainId) != vaa.ChainIDAlgorand {
panic("invalid chain ID") panic("invalid chain ID")
@ -215,46 +292,14 @@ func (e *Watcher) Run(ctx context.Context) error {
lookAtTxn(e, element, block, logger) lookAtTxn(e, element, block, logger)
} }
} }
case <-timer.C:
status, err := algodClient.Status().Do(context.Background())
if err != nil {
logger.Error(fmt.Sprintf("algodClient.Status: %s", err.Error()))
p2p.DefaultRegistry.AddErrorCount(vaa.ChainIDAlgorand, 1)
continue
}
if e.next_round <= status.LastRound {
for {
block, err := algodClient.Block(e.next_round).Do(context.Background())
if err != nil {
logger.Error(fmt.Sprintf("algodClient.Block %d: %s", e.next_round, err.Error()))
p2p.DefaultRegistry.AddErrorCount(vaa.ChainIDAlgorand, 1)
break
}
if block.Round == 0 {
break
}
for _, element := range block.Payset {
lookAtTxn(e, element, block, logger)
}
e.next_round = e.next_round + 1
if e.next_round > status.LastRound {
break
} }
} }
}
currentAlgorandHeight.Set(float64(status.LastRound))
p2p.DefaultRegistry.SetNetworkStats(vaa.ChainIDAlgorand, &gossipv1.Heartbeat_Network{
Height: int64(status.LastRound),
ContractAddress: fmt.Sprintf("%d", e.appid),
}) })
readiness.SetReady(e.readinessSync) select {
} case <-ctx.Done():
return ctx.Err()
case err := <-errC:
return err
} }
} }