Skip to content

Commit ed29620

Browse files
committed
enable single update
1 parent 56f8a4c commit ed29620

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

avssync.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,6 @@ func (a *AvsSync) Start() {
5959
a.logger.Infof("Starting avs sync with sleepBeforeFirstSyncDuration=%s, syncInterval=%s, operators=%v, quorums=%v, fetchQuorumsDynamically=%v, readerTimeoutDuration=%s, writerTimeoutDuration=%s",
6060
a.sleepBeforeFirstSyncDuration, a.syncInterval, a.operators, a.quorums, a.fetchQuorumsDynamically, a.readerTimeoutDuration, a.writerTimeoutDuration)
6161

62-
// run something every syncInterval
63-
ticker := time.NewTicker(a.syncInterval)
64-
defer ticker.Stop()
65-
6662
// ticker doesn't tick immediately, so we send a first updateStakes here
6763
// see https://github.com/golang/go/issues/17601
6864
// we first sleep some amount of time before the first sync, which allows the syncs to happen at some preferred time
@@ -73,6 +69,15 @@ func (a *AvsSync) Start() {
7369
a.logger.Error("Error updating stakes", err)
7470
}
7571

72+
if a.syncInterval == 0 {
73+
a.logger.Infof("Sync interval is 0, running updateStakes once and exiting")
74+
return // only run once
75+
}
76+
77+
// update stakes every syncInterval
78+
ticker := time.NewTicker(a.syncInterval)
79+
defer ticker.Stop()
80+
7681
for range ticker.C {
7782
err := a.updateStakes()
7883
if err != nil {

flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ var (
3737
SyncIntervalFlag = cli.DurationFlag{
3838
Name: "sync-interval",
3939
Required: true,
40-
Usage: "call updateStakes function at every `TIME` interval",
40+
Usage: "Interval at which to sync with the chain (e.g. 24h). If set to 0, will only sync once and then exit.",
4141
Value: 24 * time.Hour,
4242
EnvVar: envVarPrefix + "SYNC_INTERVAL",
4343
}

integration_test.go

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func TestIntegrationUpdateSingleOperatorPath(t *testing.T) {
6161
}
6262
operatorAddr := crypto.PubkeyToAddress(operatorEcdsaPrivKey.PublicKey)
6363
operatorBlsPrivKey := "0x1"
64-
avsSync := NewTestAvsSync(anvilHttpEndpoint, contractAddresses, []common.Address{operatorAddr})
64+
avsSync := NewTestAvsSync(anvilHttpEndpoint, contractAddresses, []common.Address{operatorAddr}, 30*time.Second)
6565

6666
// first register operator into avs. at this point, the operator will have whatever stake it had registered in eigenlayer in the avs
6767
registerOperatorWithAvs(anvilHttpEndpoint, contractAddresses, operatorEcdsaPrivKeyHex, operatorBlsPrivKey)
@@ -117,7 +117,7 @@ func TestIntegrationFullOperatorSet(t *testing.T) {
117117
}
118118
operatorAddr := crypto.PubkeyToAddress(operatorEcdsaPrivKey.PublicKey)
119119
operatorBlsPrivKey := "0x1"
120-
avsSync := NewTestAvsSync(anvilHttpEndpoint, contractAddresses, []common.Address{})
120+
avsSync := NewTestAvsSync(anvilHttpEndpoint, contractAddresses, []common.Address{}, 30*time.Second)
121121

122122
// first register operator into avs. at this point, the operator will have whatever stake it had registered in eigenlayer in the avs
123123
registerOperatorWithAvs(anvilHttpEndpoint, contractAddresses, operatorEcdsaPrivKeyHex, operatorBlsPrivKey)
@@ -179,7 +179,7 @@ func TestIntegrationFullOperatorSetWithRetry(t *testing.T) {
179179

180180
// we create avs sync and replace its avsWriter with a mock that will fail the first 2 times we call UpdateStakesOfEntireOperatorSetForQuorums
181181
// and succeed on the third time
182-
avsSync := NewTestAvsSync(anvilHttpEndpoint, contractAddresses, []common.Address{})
182+
avsSync := NewTestAvsSync(anvilHttpEndpoint, contractAddresses, []common.Address{}, 30*time.Second)
183183
mockCtrl := gomock.NewController(t)
184184
mockAvsRegistryWriter := chainiomocks.NewMockAvsRegistryWriter(mockCtrl)
185185
// this is the test. we just make sure this is called 3 times
@@ -194,7 +194,59 @@ func TestIntegrationFullOperatorSetWithRetry(t *testing.T) {
194194

195195
}
196196

197-
func NewTestAvsSync(anvilHttpEndpoint string, contractAddresses ContractAddresses, operators []common.Address) *AvsSync {
197+
func TestSingleRun(t *testing.T) {
198+
/* Start the anvil chain */
199+
anvilC := startAnvilTestContainer()
200+
// Not sure why but deferring anvilC.Terminate() causes a panic when the test finishes...
201+
// so letting it terminate silently for now
202+
anvilHttpEndpoint, err := anvilC.Endpoint(context.Background(), "http")
203+
if err != nil {
204+
t.Fatal(err)
205+
}
206+
207+
contractAddresses := getContractAddressesFromContractRegistry(anvilHttpEndpoint)
208+
operatorEcdsaPrivKeyHex := "ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"
209+
operatorEcdsaPrivKey, err := crypto.HexToECDSA(operatorEcdsaPrivKeyHex)
210+
if err != nil {
211+
t.Fatal(err)
212+
}
213+
operatorAddr := crypto.PubkeyToAddress(operatorEcdsaPrivKey.PublicKey)
214+
operatorBlsPrivKey := "0x1"
215+
// set sync interval to 0 so that we only run once
216+
avsSync := NewTestAvsSync(anvilHttpEndpoint, contractAddresses, []common.Address{}, 0)
217+
218+
// first register operator into avs. at this point, the operator will have whatever stake it had registered in eigenlayer in the avs
219+
registerOperatorWithAvs(anvilHttpEndpoint, contractAddresses, operatorEcdsaPrivKeyHex, operatorBlsPrivKey)
220+
221+
// get stake of operator before sync
222+
operatorsPerQuorumBeforeSync, err := avsSync.avsReader.GetOperatorsStakeInQuorumsAtCurrentBlock(&bind.CallOpts{}, []byte{0})
223+
if err != nil {
224+
t.Fatal(err)
225+
}
226+
// TODO: should be checking all operators, not just the first one
227+
operatorStakeBeforeSync := operatorsPerQuorumBeforeSync[0][0].Stake
228+
229+
// deposit into strategy to create a diff between eigenlayer and avs stakes
230+
depositAmount := big.NewInt(100)
231+
depositErc20IntoStrategyForOperator(anvilHttpEndpoint, contractAddresses.DelegationManager, contractAddresses.Erc20MockStrategy, operatorEcdsaPrivKeyHex, operatorAddr.Hex(), depositAmount)
232+
233+
avsSync.Start()
234+
235+
// get stake of operator after sync
236+
operatorsPerQuorumAfterSync, err := avsSync.avsReader.GetOperatorsStakeInQuorumsAtCurrentBlock(&bind.CallOpts{}, []byte{0})
237+
if err != nil {
238+
t.Fatal(err)
239+
}
240+
operatorStakeAfterSync := operatorsPerQuorumAfterSync[0][0].Stake
241+
operatorStakeDiff := new(big.Int).Sub(operatorStakeAfterSync, operatorStakeBeforeSync)
242+
243+
// we just check that the diff is equal to the deposited amount
244+
if operatorStakeDiff.Cmp(depositAmount) != 0 {
245+
t.Errorf("expected operator stake diff to be equal to deposit amount, got %v", operatorStakeDiff)
246+
}
247+
}
248+
249+
func NewTestAvsSync(anvilHttpEndpoint string, contractAddresses ContractAddresses, operators []common.Address, syncInterval time.Duration) *AvsSync {
198250
logger, err := logging.NewZapLogger(logging.Development)
199251
if err != nil {
200252
panic(err)
@@ -252,7 +304,7 @@ func NewTestAvsSync(anvilHttpEndpoint string, contractAddresses ContractAddresse
252304
avsReader,
253305
avsWriter,
254306
0*time.Second,
255-
30*time.Second,
307+
syncInterval,
256308
operators,
257309
// we only test with one quorum
258310
[]byte{0},

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func avsSyncMain(cliCtx *cli.Context) error {
111111
cliCtx.Duration(ReaderTimeoutDurationFlag.Name),
112112
cliCtx.Duration(WriterTimeoutDurationFlag.Name),
113113
)
114-
avsSync.Start()
115114

115+
avsSync.Start()
116116
return nil
117117
}

0 commit comments

Comments
 (0)