Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorporate Uri's feedback #6

Merged
merged 4 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Mandatory
AVS_SYNC_PRIVATE_KEY=
AVS_SYNC_REGISTRY_COORDINATOR_ADDR=0xd19d750531c5e95CcC5C9610bF119dDe3008A16D
AVS_SYNC_OPERATOR_STATE_RETRIEVER_ADDR=0x1b41CA79b86295e77Dd49f28DbB000286c022dfd
AVS_SYNC_ETH_HTTP_URL=http://localhost:8545
AVS_SYNC_SYNC_INTERVAL=24h

# Optional
AVS_SYNC_FIRST_SYNC_TIME=00:00:00 # this will make it run at midnight
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ start-anvil-goerli-fork: ##
anvil --fork-url https://goerli.gateway.tenderly.co

run-avs-sync: ##
./start.sh
./run.sh

test: ##
go test ./...
Expand Down
21 changes: 16 additions & 5 deletions avssync.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,17 @@ func NewAvsSync(
}

func (a *AvsSync) Start() {
a.logger.Infof("Starting avs sync with sleepBeforeFirstSyncDuration=%s, syncInterval=%s, operators=%v, quorums=%v, fetchQuorumsDynamically=%v, readerTimeoutDuration=%s, writerTimeoutDuration=%s",
a.sleepBeforeFirstSyncDuration, a.syncInterval, a.operators, convertQuorumsBytesToInts(a.quorums), a.fetchQuorumsDynamically, a.readerTimeoutDuration, a.writerTimeoutDuration)
// TODO: should prob put all of these in a config struct, to make sure we don't forget to print any of them
// when we add new ones.
a.logger.Info("Avssync config",
"sleepBeforeFirstSyncDuration", a.sleepBeforeFirstSyncDuration,
"syncInterval", a.syncInterval,
"operators", a.operators,
"quorums", a.quorums,
"fetchQuorumsDynamically", a.fetchQuorumsDynamically,
"readerTimeoutDuration", a.readerTimeoutDuration,
"writerTimeoutDuration", a.writerTimeoutDuration,
)

// ticker doesn't tick immediately, so we send a first updateStakes here
// see https://github.com/golang/go/issues/17601
Expand All @@ -83,7 +92,7 @@ func (a *AvsSync) Start() {
if err != nil {
a.logger.Error("Error updating stakes", err)
}
a.logger.Infof("Sleeping for %s\n", a.syncInterval)
a.logger.Infof("Sleeping for %s", a.syncInterval)
}
}

Expand All @@ -98,7 +107,7 @@ func (a *AvsSync) updateStakes() error {
for _, quorum := range a.quorums {
a.tryNTimesUpdateStakesOfEntireOperatorSetForQuorum(quorum, a.retrySyncNTimes)
}
a.logger.Info("Completed stake update successfully")
a.logger.Info("Completed stake update. Check logs to make sure every quorum update succeeded successfully.")
return nil
} else {
a.logger.Infof("Updating stakes of operators: %v", a.operators)
Expand Down Expand Up @@ -139,9 +148,11 @@ func (a *AvsSync) tryNTimesUpdateStakesOfEntireOperatorSetForQuorum(quorum byte,

timeoutCtx, cancel := context.WithTimeout(context.Background(), a.readerTimeoutDuration)
defer cancel()
// we need to refetch the operator set because one reason for update stakes failing is that the operator set has changed
// in between us fetching it and trying to update it (the contract makes sure the entire operator set is updated and reverts if not)
operatorAddrsPerQuorum, err := a.avsReader.GetOperatorAddrsInQuorumsAtCurrentBlock(&bind.CallOpts{Context: timeoutCtx}, []byte{quorum})
if err != nil {
a.logger.Error("Error fetching operator addresses in quorums", "err", err)
a.logger.Error("Error fetching operator addresses in quorums", "err", err, "quorum", quorum, "retryNTimes", retryNTimes, "try", i+1)
continue
}
var operators []common.Address
Expand Down
15 changes: 7 additions & 8 deletions flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ var (
EnvVar: envVarPrefix + "SYNC_INTERVAL",
}
/* Optional Flags */
SleepBeforeFirstSyncDurationFlag = cli.DurationFlag{
Name: "sleep-before-first-sync-duration",
FirstSyncTimeFlag = cli.StringFlag{
Name: "first-sync-time",
Required: false,
Value: 0,
Usage: "sleep for `SECONDS` before first sync (default=0)",
EnvVar: envVarPrefix + "SLEEP_BEFORE_FIRST_SYNC_DURATION",
Usage: "Set the HH:MI:SS time at which to run the first sync update",
EnvVar: envVarPrefix + "FIRST_SYNC_TIME",
}
OperatorListFlag = cli.StringSliceFlag{
Name: "operators",
Expand All @@ -59,9 +58,9 @@ var (
Usage: "List of quorums to update stakes for (only needs to be present if operators list not present and fetch-quorums-dynamically is false)",
EnvVar: envVarPrefix + "QUORUMS",
}
FetchQuorumDynamicallyFlag = cli.BoolFlag{
FetchQuorumDynamicallyFlag = cli.BoolTFlag{
Name: "fetch-quorums-dynamically",
Usage: "If set to true, will fetch the list of quorums registered in the contract and update all of them",
Usage: "If set to true (default), will fetch the list of quorums registered in the contract and update all of them",
EnvVar: envVarPrefix + "FETCH_QUORUMS_DYNAMICALLY",
}
ReaderTimeoutDurationFlag = cli.DurationFlag{
Expand Down Expand Up @@ -93,7 +92,7 @@ var RequiredFlags = []cli.Flag{
}

var OptionalFlags = []cli.Flag{
SleepBeforeFirstSyncDurationFlag,
FirstSyncTimeFlag,
OperatorListFlag,
QuorumListFlag,
FetchQuorumDynamicallyFlag,
Expand Down
21 changes: 20 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,30 @@ func avsSyncMain(cliCtx *cli.Context) error {
for _, quorum := range cliCtx.IntSlice(QuorumListFlag.Name) {
quorums = append(quorums, byte(quorum))
}

firstSyncTimeStr := cliCtx.String(FirstSyncTimeFlag.Name)
var sleepBeforeFirstSyncDuration time.Duration
if firstSyncTimeStr == "" {
sleepBeforeFirstSyncDuration = 0 * time.Second
} else {
now := time.Now()
firstSyncTime, err := time.Parse("15:04:05", firstSyncTimeStr)
samlaf marked this conversation as resolved.
Show resolved Hide resolved
firstSyncTime = time.Date(now.Year(), now.Month(), now.Day(), firstSyncTime.Hour(), firstSyncTime.Minute(), firstSyncTime.Second(), 0, now.Location())
if err != nil {
return err
}
if now.After(firstSyncTime) {
// If the set time is before the current time, add a day to the set time
firstSyncTime = firstSyncTime.Add(24 * time.Hour)
}
sleepBeforeFirstSyncDuration = firstSyncTime.Sub(now)
}
logger.Infof("Sleeping for %v before first sync, so that it happens at %v", sleepBeforeFirstSyncDuration, time.Now().Add(sleepBeforeFirstSyncDuration))
avsSync := NewAvsSync(
logger,
avsReader,
avsWriter,
cliCtx.Duration(SleepBeforeFirstSyncDurationFlag.Name),
sleepBeforeFirstSyncDuration,
cliCtx.Duration(SyncIntervalFlag.Name),
operators,
quorums,
Expand Down
6 changes: 6 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
samlaf marked this conversation as resolved.
Show resolved Hide resolved
set -a # export all variables from .env
source .env
set +a

go run .
9 changes: 0 additions & 9 deletions start.sh

This file was deleted.

Loading