Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
8 changes: 8 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ packages:
config:
dir: "{{ .InterfaceDir }}/mocks"
all: true
# Ports of the tracker's reorg re-evaluation (see bridgetracker/domain/reorg.go), mocked for the engine tests
github.com/agglayer/aggkit/bridgetracker/domain:
config:
dir: "{{ .InterfaceDir }}/mocks"
interfaces:
OriginBlockSource:
InjectedGERChecker:
SettlementHistorySource:
# Resolver is mocked for its consumers (activity scan, tracker); ClientResolver for the tests of the package
github.com/agglayer/aggkit/bridgetracker/finality:
config:
Expand Down
290 changes: 86 additions & 204 deletions bridgetracker/activity.go

Large diffs are not rendered by default.

61 changes: 59 additions & 2 deletions bridgetracker/activity_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ type ActivityEngine struct {
// ticking is true while a tick is in flight; guards against Start's loop stacking overlapping
// ticks (see startTick)
ticking atomic.Bool

// addrLocksMu guards addrLocks, the per-address locks that serialize RefreshAddress calls of
// one address (see lockAddress)
addrLocksMu sync.Mutex
addrLocks map[common.Address]*addressLock
}

// addressLock is the lock of one address (a 1-slot channel, so waiting on it can be cancelled),
// with the number of refreshes holding or waiting for it: the entry is dropped when it reaches 0
type addressLock struct {
slot chan struct{}
refs int
}

// NewActivityEngine returns an activity engine over the given store
Expand All @@ -98,7 +110,8 @@ func NewActivityEngine(
cfg = cfg.withDefaults()
return &ActivityEngine{
logger: logger, cfg: cfg, store: store, now: time.Now,
sem: make(chan struct{}, cfg.MaxConcurrentRefreshes),
sem: make(chan struct{}, cfg.MaxConcurrentRefreshes),
addrLocks: make(map[common.Address]*addressLock),
}, nil
}

Expand Down Expand Up @@ -157,8 +170,20 @@ func (e *ActivityEngine) startTick(ctx context.Context) {
// ActivityEngineConfig.MaxConcurrentRefreshes) and runs one refresh for addr, bounded by
// ActivityEngineConfig.RefreshTimeout so one hung network scan or RPC client cannot occupy that
// slot indefinitely. Shared by both tick's regular sweep and the trigger fast-path. Errors are
// logged, never fatal to the loop
// logged, never fatal to the loop.
//
// Refreshes of the same address never overlap: a trigger can arrive while a tick is refreshing
// that address, and a refresh applies a plan computed from the entries it read before scanning
// (see domain.PlanActivityRefresh), so an older plan applied after a newer one could overwrite or
// remove what the newer one just settled. The second refresh waits for the first (before taking a
// concurrency slot) and then reads the entries afresh
func (e *ActivityEngine) refreshOne(ctx context.Context, fromAddress common.Address) {
unlock, ok := e.lockAddress(ctx, fromAddress)
if !ok {
return
}
defer unlock()

select {
case e.sem <- struct{}{}:
case <-ctx.Done():
Expand All @@ -176,6 +201,38 @@ func (e *ActivityEngine) refreshOne(ctx context.Context, fromAddress common.Addr
}
}

// lockAddress waits until no other refresh of fromAddress is running and returns the function
// that releases it; ok is false when ctx ends first
func (e *ActivityEngine) lockAddress(ctx context.Context, fromAddress common.Address) (unlock func(), ok bool) {
e.addrLocksMu.Lock()
lock, exists := e.addrLocks[fromAddress]
if !exists {
lock = &addressLock{slot: make(chan struct{}, 1)}
e.addrLocks[fromAddress] = lock
}
lock.refs++
e.addrLocksMu.Unlock()

release := func() {
e.addrLocksMu.Lock()
lock.refs--
if lock.refs == 0 {
delete(e.addrLocks, fromAddress)
}
e.addrLocksMu.Unlock()
}
select {
case lock.slot <- struct{}{}:
return func() {
<-lock.slot
release()
}, true
case <-ctx.Done():
release()
return nil, false
}
}

// tick forgets the addresses whose idle timeout has elapsed (see ActivityEngineConfig.
// IdleTimeout) before enumerating who's left, then refreshes every remaining supervised address
// concurrently, since each is independent and may hit a different set of networks β€” bounded by
Expand Down
81 changes: 81 additions & 0 deletions bridgetracker/activity_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,87 @@ func TestActivityEngineTickBoundsConcurrentRefreshes(t *testing.T) {
<-done
}

// TestActivityEngineSerializesRefreshesOfOneAddress verifies two refreshes of the same address
// (a trigger arriving while a tick refreshes it) never overlap, while refreshes of different
// addresses still run concurrently
func TestActivityEngineSerializesRefreshesOfOneAddress(t *testing.T) {
t.Parallel()

var (
mu sync.Mutex
inFlight int
maxObserved int
calls int
)
release := make(chan struct{})
scanner := &hookedActivityScanner{hook: func() {
mu.Lock()
inFlight++
calls++
if inFlight > maxObserved {
maxObserved = inFlight
}
mu.Unlock()

<-release

mu.Lock()
inFlight--
mu.Unlock()
}}
cache := newTestActivityCache(scanner, &fakeActivityClaims{})
engine, err := NewActivityEngine(ActivityEngineConfig{MaxConcurrentRefreshes: 4},
log.WithFields("module", "activity_engine_test"), cache)
require.NoError(t, err)
other := common.HexToAddress("0x2222222222222222222222222222222222222222")
mustRegisterActivity(t, cache, testFromAddress, 0)
mustRegisterActivity(t, cache, other, 0)

var wg sync.WaitGroup
for _, addr := range []common.Address{testFromAddress, testFromAddress, other} {
wg.Add(1)
go func(addr common.Address) {
defer wg.Done()
engine.refreshOne(t.Context(), addr)
}(addr)
}

require.Eventually(t, func() bool {
mu.Lock()
defer mu.Unlock()
return inFlight == 2
}, time.Second, time.Millisecond, "one refresh per address in flight")
time.Sleep(20 * time.Millisecond) // give an overlapping refresh of the same address a chance to start
mu.Lock()
require.Equal(t, 2, maxObserved, "the second refresh of the same address waits for the first")
mu.Unlock()

close(release)
wg.Wait()
require.Equal(t, 3, calls, "the waiting refresh runs once the first one is done")
engine.addrLocksMu.Lock()
require.Empty(t, engine.addrLocks, "per-address locks are dropped once released")
engine.addrLocksMu.Unlock()
}

// TestActivityEngineRefreshOneGivesUpWaitingWhenCancelled verifies a refresh waiting for another
// refresh of the same address returns when its context ends, without refreshing
func TestActivityEngineRefreshOneGivesUpWaitingWhenCancelled(t *testing.T) {
engine, _ := newTestActivityEngine(t, ActivityEngineConfig{}, &fakeActivityScanner{}, &fakeActivityClaims{})
unlock, ok := engine.lockAddress(t.Context(), testFromAddress)
require.True(t, ok)

ctx, cancel := context.WithCancel(t.Context())
cancel()
_, ok = engine.lockAddress(ctx, testFromAddress)
require.False(t, ok)

unlock()
engine.addrLocksMu.Lock()
require.Empty(t, engine.addrLocks)
engine.addrLocksMu.Unlock()
}

// TestActivityEnginePruneIdleCalledEachTick verifies tick forgets addresses idle past
// IdleTimeout, proven indirectly by observing isClaimed() being asked again for a bridge that
// had already settled β€” which would not happen if its cached state had survived
Expand Down
12 changes: 6 additions & 6 deletions bridgetracker/activity_finality_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// the empty-activity value for an address with nothing cached, and whatever the address's cache
// holds otherwise (the follow-up issues are the ones that maintain it)
func TestActivityCache_GetActivityRevision(t *testing.T) {
cache := NewActivityCache(nil, nil, nil, nil)
cache := NewActivityCache(nil, nil, nil, nil, nil)

revision, err := cache.GetActivityRevision(t.Context(), testFromAddress)
require.NoError(t, err)
Expand Down Expand Up @@ -47,7 +47,7 @@ func TestActivityCache_GetActivityRevision(t *testing.T) {
// TestActivityCache_GetActivityRevisionIsNotAnAccess verifies reading the revision does not
// refresh the address's lastAccess: only the GetActivity read that follows it counts
func TestActivityCache_GetActivityRevisionIsNotAnAccess(t *testing.T) {
cache := NewActivityCache(nil, nil, nil, nil)
cache := NewActivityCache(nil, nil, nil, nil, nil)
start := time.Unix(1_700_000_000, 0)
cache.now = func() time.Time { return start }
_, err := cache.RegisterAndAwait(testFromAddress, false, 0)
Expand All @@ -69,12 +69,12 @@ func TestMatchesFilter_ClaimedIncludesClaimedFinalized(t *testing.T) {
return &domain.ActivityEntry{TrackerClaimStatus: status}
}

require.True(t, matchesFilter(entry(types.TrackerClaimStatusClaimed), types.ActivityFilterClaimed))
require.True(t, matchesFilter(entry(types.TrackerClaimStatusClaimedFinalized), types.ActivityFilterClaimed))
require.True(t, matchesFilter(entry(types.TrackerClaimStatusClaimedFinalized), types.ActivityFilterAll))
require.True(t, MatchesActivityFilter(entry(types.TrackerClaimStatusClaimed), types.ActivityFilterClaimed))
require.True(t, MatchesActivityFilter(entry(types.TrackerClaimStatusClaimedFinalized), types.ActivityFilterClaimed))
require.True(t, MatchesActivityFilter(entry(types.TrackerClaimStatusClaimedFinalized), types.ActivityFilterAll))
for _, filter := range []types.ActivityFilter{
types.ActivityFilterPending, types.ActivityFilterReadyToClaim, types.ActivityFilterError,
} {
require.False(t, matchesFilter(entry(types.TrackerClaimStatusClaimedFinalized), filter), filter.String())
require.False(t, MatchesActivityFilter(entry(types.TrackerClaimStatusClaimedFinalized), filter), filter.String())
}
}
Loading
Loading