-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sync, blocks: request blocks when tortoise crossed local threshold (#…
…4826) closes: #4824 - first improvement is to register peers that supported blocks, otherwise we will be guessing from whom to request the block - and when mesh is called by sync or block builder it may send request to fetch blocks from registered peers. previously it would do so only in sync code path, which is less robust and might be delayed by fork finder
- Loading branch information
Showing
11 changed files
with
219 additions
and
95 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,58 @@ | ||
package blockssync | ||
|
||
import ( | ||
"context" | ||
|
||
"go.uber.org/zap" | ||
"golang.org/x/sync/errgroup" | ||
|
||
"github.com/spacemeshos/go-spacemesh/common/types" | ||
) | ||
|
||
//go:generate mockgen -package=blockssync -destination=./mocks.go -source=./blocks.go | ||
|
||
type blockFetcher interface { | ||
GetBlocks(context.Context, []types.BlockID) error | ||
} | ||
|
||
// Sync requests last specified blocks in background. | ||
func Sync(ctx context.Context, logger *zap.Logger, requests <-chan []types.BlockID, fetcher blockFetcher) error { | ||
var ( | ||
eg errgroup.Group | ||
lastch = make(chan map[types.BlockID]struct{}) | ||
) | ||
eg.Go(func() error { | ||
var ( | ||
send chan map[types.BlockID]struct{} | ||
last map[types.BlockID]struct{} | ||
) | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
close(lastch) | ||
return ctx.Err() | ||
case req := <-requests: | ||
if last == nil { | ||
last = map[types.BlockID]struct{}{} | ||
send = lastch | ||
} | ||
for _, id := range req { | ||
last[id] = struct{}{} | ||
} | ||
case send <- last: | ||
last = nil | ||
send = nil | ||
} | ||
} | ||
}) | ||
for batch := range lastch { | ||
blocks := make([]types.BlockID, 0, len(batch)) | ||
for id := range batch { | ||
blocks = append(blocks, id) | ||
} | ||
if err := fetcher.GetBlocks(ctx, blocks); err != nil { | ||
logger.Warn("failed to fetch blocks", zap.Error(err)) | ||
} | ||
} | ||
return eg.Wait() | ||
} |
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,84 @@ | ||
package blockssync | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/sync/errgroup" | ||
|
||
"github.com/spacemeshos/go-spacemesh/common/types" | ||
"github.com/spacemeshos/go-spacemesh/log/logtest" | ||
) | ||
|
||
func TestCanBeAggregated(t *testing.T) { | ||
var ( | ||
ctrl = gomock.NewController(t) | ||
fetch = NewMockblockFetcher(ctrl) | ||
req = make(chan []types.BlockID, 10) | ||
out = make(chan []types.BlockID, 10) | ||
ctx, cancel = context.WithCancel(context.Background()) | ||
eg errgroup.Group | ||
) | ||
t.Cleanup(func() { | ||
cancel() | ||
eg.Wait() | ||
}) | ||
eg.Go(func() error { | ||
return Sync(ctx, logtest.New(t).Zap(), req, fetch) | ||
}) | ||
fetch.EXPECT().GetBlocks(gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, blocks []types.BlockID) error { | ||
out <- blocks | ||
return nil | ||
}).AnyTimes() | ||
first := []types.BlockID{{1}, {2}, {3}} | ||
second := []types.BlockID{{2}, {3}, {4}} | ||
req <- first | ||
req <- second | ||
rst1 := timedRead(t, out) | ||
require.Subset(t, rst1, first) | ||
if len(rst1) == len(first) { | ||
require.Subset(t, timedRead(t, out), second) | ||
} | ||
} | ||
|
||
func TestErrorDoesntExit(t *testing.T) { | ||
var ( | ||
ctrl = gomock.NewController(t) | ||
fetch = NewMockblockFetcher(ctrl) | ||
req = make(chan []types.BlockID, 10) | ||
out = make(chan []types.BlockID, 10) | ||
ctx, cancel = context.WithCancel(context.Background()) | ||
eg errgroup.Group | ||
) | ||
t.Cleanup(func() { | ||
cancel() | ||
eg.Wait() | ||
}) | ||
eg.Go(func() error { | ||
return Sync(ctx, logtest.New(t).Zap(), req, fetch) | ||
}) | ||
fetch.EXPECT().GetBlocks(gomock.Any(), gomock.Any()).DoAndReturn(func(ctx context.Context, blocks []types.BlockID) error { | ||
out <- blocks | ||
return errors.New("test") | ||
}).AnyTimes() | ||
first := []types.BlockID{{1}, {2}, {3}} | ||
req <- first | ||
require.Subset(t, timedRead(t, out), first) | ||
req <- first | ||
require.Subset(t, timedRead(t, out), first) | ||
} | ||
|
||
func timedRead(tb testing.TB, blocks chan []types.BlockID) []types.BlockID { | ||
delay := time.Second | ||
select { | ||
case v := <-blocks: | ||
return v | ||
case <-time.After(delay): | ||
require.FailNow(tb, "timed out after", delay) | ||
} | ||
return nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Oops, something went wrong.