-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: add state sync integration test
- Loading branch information
1 parent
7f7afd2
commit 78079a2
Showing
16 changed files
with
737 additions
and
575 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
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
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,62 @@ | ||
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package statesync | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
) | ||
|
||
var _ Syncer[interface{}] = (*BlockWindowSyncer[interface{}])(nil) | ||
|
||
type BlockSyncer[T any] interface { | ||
Accept(ctx context.Context, block T) (bool, error) | ||
} | ||
|
||
type BlockWindowSyncer[T any] struct { | ||
syncer BlockSyncer[T] | ||
doneOnce sync.Once | ||
done chan struct{} | ||
} | ||
|
||
func NewBlockWindowSyncer[T any](syncer BlockSyncer[T]) *BlockWindowSyncer[T] { | ||
return &BlockWindowSyncer[T]{ | ||
syncer: syncer, | ||
done: make(chan struct{}), | ||
} | ||
} | ||
|
||
func (b *BlockWindowSyncer[T]) Start(ctx context.Context, target T) error { | ||
done, err := b.syncer.Accept(ctx, target) | ||
if done { | ||
b.doneOnce.Do(func() { | ||
close(b.done) | ||
}) | ||
} | ||
return err | ||
} | ||
|
||
func (b *BlockWindowSyncer[T]) Wait(ctx context.Context) error { | ||
select { | ||
case <-b.done: | ||
return nil | ||
case <-ctx.Done(): | ||
return fmt.Errorf("failed to await full block window: %w", ctx.Err()) | ||
} | ||
} | ||
|
||
func (b *BlockWindowSyncer[T]) Close() error { | ||
return nil | ||
} | ||
|
||
func (b *BlockWindowSyncer[T]) UpdateSyncTarget(ctx context.Context, target T) error { | ||
done, err := b.syncer.Accept(ctx, target) | ||
if done { | ||
b.doneOnce.Do(func() { | ||
close(b.done) | ||
}) | ||
} | ||
return err | ||
} |
Oops, something went wrong.