-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
292 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package atxsync | ||
|
||
import ( | ||
"context" | ||
"math/rand" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
|
||
"github.com/spacemeshos/go-spacemesh/common/types" | ||
"github.com/spacemeshos/go-spacemesh/sql" | ||
"github.com/spacemeshos/go-spacemesh/sql/atxs" | ||
) | ||
|
||
//go:generate mockgen -typed -package=mocks -destination=./mocks/mocks.go -source=./atxsync.go | ||
type atxFetcher interface { | ||
GetAtxs(context.Context, []types.ATXID) error | ||
} | ||
|
||
func getMissing(db *sql.Database, set []types.ATXID) ([]types.ATXID, error) { | ||
missing := []types.ATXID{} | ||
for _, atx := range set { | ||
exist, err := atxs.Has(db, atx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if !exist { | ||
missing = append(missing, atx) | ||
} | ||
} | ||
return missing, nil | ||
} | ||
|
||
// Download specified set of atxs from peers in the network. | ||
// | ||
// actual retry interval will be between [retryInterval, 2*retryInterval] | ||
func Download( | ||
ctx context.Context, | ||
retryInterval time.Duration, | ||
logger *zap.Logger, | ||
db *sql.Database, | ||
fetcher atxFetcher, | ||
set []types.ATXID, | ||
) error { | ||
total := len(set) | ||
for { | ||
missing, err := getMissing(db, set) | ||
if err != nil { | ||
return err | ||
} | ||
set = missing | ||
downloaded := total - len(missing) | ||
logger.Info("downloaded atxs", | ||
zap.Int("total", total), | ||
zap.Int("downloaded", downloaded), | ||
zap.Array("missing", zapcore.ArrayMarshalerFunc(func(enc zapcore.ArrayEncoder) error { | ||
for _, atx := range missing { | ||
enc.AppendString(atx.ShortString()) | ||
} | ||
return nil | ||
}))) | ||
if downloaded == total { | ||
return nil | ||
} | ||
if err := fetcher.GetAtxs(ctx, missing); err != nil { | ||
logger.Debug("failed to fetch atxs", zap.Error(err)) | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case <-time.After(retryInterval + time.Duration(rand.Int63n(int64(retryInterval)))): | ||
} | ||
} | ||
} | ||
} |
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,122 @@ | ||
package atxsync | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/spacemeshos/go-spacemesh/common/types" | ||
"github.com/spacemeshos/go-spacemesh/log/logtest" | ||
"github.com/spacemeshos/go-spacemesh/sql" | ||
"github.com/spacemeshos/go-spacemesh/sql/atxs" | ||
"github.com/spacemeshos/go-spacemesh/syncer/atxsync/mocks" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
func atx(id types.ATXID) *types.VerifiedActivationTx { | ||
atx := &types.ActivationTx{InnerActivationTx: types.InnerActivationTx{ | ||
NIPostChallenge: types.NIPostChallenge{ | ||
PublishEpoch: 1, | ||
}, | ||
NumUnits: 1, | ||
}} | ||
atx.SetID(id) | ||
atx.SetEffectiveNumUnits(1) | ||
atx.SetReceived(time.Now()) | ||
copy(atx.SmesherID[:], id[:]) | ||
vatx, err := atx.Verify(0, 1) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return vatx | ||
} | ||
|
||
func id(id ...byte) types.ATXID { | ||
var atxid types.ATXID | ||
copy(atxid[:], id) | ||
return atxid | ||
} | ||
|
||
type fetchRequest struct { | ||
request []types.ATXID | ||
result []*types.VerifiedActivationTx | ||
error error | ||
} | ||
|
||
func TestDownload(t *testing.T) { | ||
canceled, cancel := context.WithCancel(context.Background()) | ||
cancel() | ||
for _, tc := range []struct { | ||
desc string | ||
ctx context.Context | ||
retry time.Duration | ||
existing []*types.VerifiedActivationTx | ||
set []types.ATXID | ||
fetched []fetchRequest | ||
rst error | ||
}{ | ||
{ | ||
desc: "all existing", | ||
ctx: context.Background(), | ||
existing: []*types.VerifiedActivationTx{atx(id(1)), atx(id(2)), atx(id(3))}, | ||
set: []types.ATXID{id(1), id(2), id(3)}, | ||
}, | ||
{ | ||
desc: "with multiple requests", | ||
ctx: context.Background(), | ||
existing: []*types.VerifiedActivationTx{atx(id(1))}, | ||
fetched: []fetchRequest{ | ||
{request: []types.ATXID{id(2), id(3)}, result: []*types.VerifiedActivationTx{atx(id(2))}}, | ||
{request: []types.ATXID{id(3)}, result: []*types.VerifiedActivationTx{atx(id(3))}}, | ||
}, | ||
set: []types.ATXID{id(1), id(2), id(3)}, | ||
}, | ||
{ | ||
desc: "continue on error", | ||
ctx: context.Background(), | ||
retry: 1, | ||
existing: []*types.VerifiedActivationTx{atx(id(1))}, | ||
fetched: []fetchRequest{ | ||
{request: []types.ATXID{id(2)}, error: errors.New("test")}, | ||
{request: []types.ATXID{id(2)}, result: []*types.VerifiedActivationTx{atx(id(2))}}, | ||
}, | ||
set: []types.ATXID{id(1), id(2)}, | ||
}, | ||
{ | ||
desc: "exit on context", | ||
ctx: canceled, | ||
retry: 1, | ||
existing: []*types.VerifiedActivationTx{atx(id(1))}, | ||
fetched: []fetchRequest{ | ||
{request: []types.ATXID{id(2)}, error: errors.New("test")}, | ||
}, | ||
set: []types.ATXID{id(1), id(2)}, | ||
rst: context.Canceled, | ||
}, | ||
} { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
logger := logtest.New(t) | ||
db := sql.InMemory() | ||
ctrl := gomock.NewController(t) | ||
fetcher := mocks.NewMockatxFetcher(ctrl) | ||
for _, atx := range tc.existing { | ||
require.NoError(t, atxs.Add(db, atx)) | ||
} | ||
for i := range tc.fetched { | ||
req := tc.fetched[i] | ||
fetcher.EXPECT(). | ||
GetAtxs(tc.ctx, req.request). | ||
Times(1). | ||
DoAndReturn(func(_ context.Context, _ []types.ATXID) error { | ||
for _, atx := range req.result { | ||
require.NoError(t, atxs.Add(db, atx)) | ||
} | ||
return req.error | ||
}) | ||
} | ||
require.Equal(t, tc.rst, Download(tc.ctx, tc.retry, logger.Zap(), db, fetcher, tc.set)) | ||
}) | ||
} | ||
} |
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