-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
set up proof collection coordinator. closes #10
- Loading branch information
1 parent
eca2c65
commit 9c33314
Showing
15 changed files
with
1,765 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
"proversidecar/config" | ||
) | ||
|
||
const ( | ||
configCommandName = "config" // so we can reference it in the root command pre-run hook | ||
|
||
flagForceInitConfig = "force" | ||
) | ||
|
||
func ConfigCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: configCommandName, | ||
Short: "config subcommands", | ||
} | ||
|
||
cmd.AddCommand(InitConfigCmd()) | ||
|
||
return cmd | ||
} | ||
|
||
func InitConfigCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "init", | ||
Short: "init config file", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
force, _ := cmd.Flags().GetBool(flagForceInitConfig) | ||
|
||
homedir := GetHomedir(cmd) | ||
logger := GetLogger(cmd) | ||
|
||
return config.InitConfig(logger, homedir, force) | ||
}, | ||
} | ||
|
||
cmd.Flags().Bool(flagForceInitConfig, false, "force overwrite of existing config file") | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package coordinator | ||
|
||
import ( | ||
"context" | ||
"go.uber.org/zap" | ||
"golang.org/x/sync/errgroup" | ||
"proversidecar/config" | ||
"proversidecar/provers" | ||
"proversidecar/provers/cosmos" | ||
"time" | ||
) | ||
|
||
const ( | ||
defaultMinQueryLoopDuration = 1 * time.Second | ||
) | ||
|
||
type Coordinator struct { | ||
logger *zap.Logger | ||
|
||
chainProvers []provers.ChainProver | ||
} | ||
|
||
func NewCoordinator(logger *zap.Logger, sidecarConfig config.Config) (*Coordinator, error) { | ||
var chainProvers []provers.ChainProver | ||
for _, cosmosConfig := range sidecarConfig.CosmosChains { | ||
prover, err := cosmos.NewCosmosProver(logger, cosmosConfig.ChainID, cosmosConfig.RPC, cosmosConfig.ClientID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
chainProvers = append(chainProvers, prover) | ||
} | ||
|
||
return &Coordinator{ | ||
logger: logger, | ||
chainProvers: chainProvers, | ||
}, nil | ||
} | ||
|
||
func (c *Coordinator) Run(ctx context.Context) error { | ||
c.logger.Debug("Coordinator.Run") | ||
|
||
var eg errgroup.Group | ||
runCtx, runCtxCancel := context.WithCancel(ctx) | ||
for _, chainProver := range c.chainProvers { | ||
c.logger.Info("Starting chain prover loop", zap.String("chain_id", chainProver.ChainID())) | ||
|
||
chainProver := chainProver | ||
eg.Go(func() error { | ||
err := c.chainProverLoop(runCtx, chainProver) | ||
runCtxCancel() // Signal the other chain processors to exit. | ||
return err | ||
}) | ||
} | ||
|
||
err := eg.Wait() | ||
runCtxCancel() | ||
return err | ||
} | ||
|
||
func (c *Coordinator) chainProverLoop(ctx context.Context, chainProver provers.ChainProver) error { | ||
ticker := time.NewTicker(defaultMinQueryLoopDuration) // TODO: Make this configurable per chain | ||
defer ticker.Stop() | ||
|
||
for { | ||
// TODO: Add retry logic | ||
c.logger.Info("Collecting proofs", zap.String("chain_id", chainProver.ChainID())) | ||
if err := chainProver.CollectProofs(ctx); err != nil { | ||
return err | ||
} | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case <-ticker.C: | ||
ticker.Reset(defaultMinQueryLoopDuration) | ||
} | ||
} | ||
} |
Oops, something went wrong.