Skip to content

Commit

Permalink
Merge branch 'develop' into sync/rate-limit-server
Browse files Browse the repository at this point in the history
  • Loading branch information
dshulyak committed Oct 17, 2023
2 parents 3517793 + 62e62a1 commit 679ee80
Show file tree
Hide file tree
Showing 48 changed files with 1,497 additions and 812 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ jobs:
rm -f $OUTNAME/post.h
zip -r $OUTNAME.zip $OUTNAME
- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v0
uses: google-github-actions/setup-gcloud@v1
with:
version: "450.0.0"
project_id: ${{ secrets.GCP_PROJECT_ID }}
service_account_key: ${{ secrets.GCP_SA_KEY }}
export_default_credentials: true
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/systest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ jobs:

- name: Configure gcloud
uses: "google-github-actions/setup-gcloud@v1"
with:
version: "450.0.0"

- name: Configure gke authentication plugin
run: gcloud components install gke-gcloud-auth-plugin --quiet
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ See [RELEASE](./RELEASE.md) for workflow instructions.

The change improves initial sync speed and any sync protocol requests required during consensus.

* [#5109](https://github.com/spacemeshos/go-spacemesh/pull/5109) Limit number of layers that tortoise needs to read on startup.

Bounds the time required to restart a node.

## v1.2.0

### Upgrade information
Expand Down
5 changes: 1 addition & 4 deletions activation/activation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,10 +928,7 @@ func TestBuilder_SignAtx(t *testing.T) {
atx := newAtx(t, tab.sig, challenge, nipost, 100, types.Address{})
require.NoError(t, SignAndFinalizeAtx(tab.signer, atx))

verifier, err := signing.NewEdVerifier()
require.NoError(t, err)

ok := verifier.Verify(signing.ATX, tab.nodeID, atx.SignedBytes(), atx.Signature)
ok := signing.NewEdVerifier().Verify(signing.ATX, tab.nodeID, atx.SignedBytes(), atx.Signature)
require.True(t, ok)
require.Equal(t, tab.nodeID, atx.SmesherID)
}
Expand Down
6 changes: 1 addition & 5 deletions activation/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ type testHandler struct {
func newTestHandler(tb testing.TB, goldenATXID types.ATXID) *testHandler {
lg := logtest.New(tb)
cdb := datastore.NewCachedDB(sql.InMemory(), lg)

verifier, err := signing.NewEdVerifier()
require.NoError(tb, err)

ctrl := gomock.NewController(tb)
mclock := NewMocklayerClock(ctrl)
mpub := pubsubmocks.NewMockPublisher(ctrl)
Expand All @@ -104,7 +100,7 @@ func newTestHandler(tb testing.TB, goldenATXID types.ATXID) *testHandler {
mbeacon := NewMockAtxReceiver(ctrl)
mtortoise := mocks.NewMockTortoise(ctrl)

atxHdlr := NewHandler(localID, cdb, verifier, mclock, mpub, mockFetch, 1, goldenATXID, mValidator, mbeacon, mtortoise, lg, PoetConfig{})
atxHdlr := NewHandler(localID, cdb, signing.NewEdVerifier(), mclock, mpub, mockFetch, 1, goldenATXID, mValidator, mbeacon, mtortoise, lg, PoetConfig{})
return &testHandler{
Handler: atxHdlr,

Expand Down
7 changes: 2 additions & 5 deletions beacon/beacon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,13 @@ func newTestDriver(tb testing.TB, cfg Config, p pubsub.Publisher) *testProtocolD
}
edSgn, err := signing.NewEdSigner()
require.NoError(tb, err)
edVerify, err := signing.NewEdVerifier()
require.NoError(tb, err)
minerID := edSgn.NodeID()
lg := logtest.New(tb).WithName(minerID.ShortString())
lg := logtest.New(tb)

tpd.mVerifier.EXPECT().Verify(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(true)
tpd.mNonceFetcher.EXPECT().VRFNonce(gomock.Any(), gomock.Any()).AnyTimes().Return(types.VRFPostIndex(1), nil)

tpd.cdb = datastore.NewCachedDB(sql.InMemory(), lg)
tpd.ProtocolDriver = New(p, edSgn, edVerify, tpd.mVerifier, tpd.cdb, tpd.mClock,
tpd.ProtocolDriver = New(p, edSgn, signing.NewEdVerifier(), tpd.mVerifier, tpd.cdb, tpd.mClock,
WithConfig(cfg),
WithLogger(lg),
withWeakCoin(coinValueMock(tb, true)),
Expand Down
9 changes: 2 additions & 7 deletions blocks/certifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,14 @@ func newTestCertifier(t *testing.T) *testCertifier {
db := datastore.NewCachedDB(sql.InMemory(), logtest.New(t))
signer, err := signing.NewEdSigner()
require.NoError(t, err)
edVerifier, err := signing.NewEdVerifier()
require.NoError(t, err)
nid := signer.NodeID()
ctrl := gomock.NewController(t)
mo := hmocks.NewMockRolacle(ctrl)
mp := pubsubmock.NewMockPublisher(ctrl)
mc := mocks.NewMocklayerClock(ctrl)
mb := smocks.NewMockBeaconGetter(ctrl)
mtortoise := smocks.NewMockTortoise(ctrl)
c := NewCertifier(db, mo, nid, signer, edVerifier, mp, mc, mb, mtortoise,
c := NewCertifier(db, mo, nid, signer, signing.NewEdVerifier(), mp, mc, mb, mtortoise,
WithCertifierLogger(logtest.New(t)),
)
return &testCertifier{
Expand Down Expand Up @@ -607,17 +605,14 @@ func Test_CertifyIfEligible(t *testing.T) {
tc.mb.EXPECT().GetBeacon(b.LayerIndex.GetEpoch()).Return(types.RandomBeacon(), nil)
proof := types.RandomVrfSignature()

edVerifier, err := signing.NewEdVerifier()
require.NoError(t, err)

tc.mOracle.EXPECT().Proof(gomock.Any(), b.LayerIndex, eligibility.CertifyRound).Return(proof, nil)
tc.mOracle.EXPECT().CalcEligibility(gomock.Any(), b.LayerIndex, eligibility.CertifyRound, tc.cfg.CommitteeSize, tc.nodeID, proof).Return(defaultCnt, nil)
tc.mPub.EXPECT().Publish(gomock.Any(), pubsub.BlockCertify, gomock.Any()).DoAndReturn(
func(_ context.Context, _ string, got []byte) error {
var msg types.CertifyMessage
require.NoError(t, codec.Decode(got, &msg))

ok := edVerifier.Verify(signing.HARE, msg.SmesherID, msg.Bytes(), msg.Signature)
ok := signing.NewEdVerifier().Verify(signing.HARE, msg.SmesherID, msg.Bytes(), msg.Signature)
require.True(t, ok)
require.Equal(t, b.LayerIndex, msg.LayerID)
require.Equal(t, b.ID(), msg.BlockID)
Expand Down
4 changes: 1 addition & 3 deletions checkpoint/recovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,6 @@ func TestRecover_SameRecoveryInfo(t *testing.T) {

func validateAndPreserveData(tb testing.TB, db *sql.Database, deps []*types.VerifiedActivationTx, proofs []*types.PoetProofMessage) {
lg := logtest.New(tb)
edVerifier, err := signing.NewEdVerifier()
require.NoError(tb, err)
poetDb := activation.NewPoetDb(db, lg)
ctrl := gomock.NewController(tb)
mclock := activation.NewMocklayerClock(ctrl)
Expand All @@ -230,7 +228,7 @@ func validateAndPreserveData(tb testing.TB, db *sql.Database, deps []*types.Veri
atxHandler := activation.NewHandler(
"",
cdb,
edVerifier,
signing.NewEdVerifier(),
mclock,
nil,
mfetch,
Expand Down
4 changes: 1 addition & 3 deletions common/types/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ func Test_CertifyMessage(t *testing.T) {
require.NoError(t, codec.Decode(data, &decoded))
require.Equal(t, msg, decoded)

pke, err := signing.NewEdVerifier()
require.NoError(t, err)
ok := pke.Verify(signing.HARE, decoded.SmesherID, msg.Bytes(), decoded.Signature)
ok := signing.NewEdVerifier().Verify(signing.HARE, decoded.SmesherID, msg.Bytes(), decoded.Signature)
require.True(t, ok)
}

Expand Down
4 changes: 4 additions & 0 deletions config/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type LoggerConfig struct {
PostLoggerLevel string `mapstructure:"post"`
StateDbLoggerLevel string `mapstructure:"stateDb"`
StateLoggerLevel string `mapstructure:"state"`
AtxHandlerLevel string `mapstructure:"atxHandler"`
AtxDbStoreLoggerLevel string `mapstructure:"atxDbStore"`
BeaconLoggerLevel string `mapstructure:"beacon"`
WeakCoinLoggerLevel string `mapstructure:"weakCoin"`
Expand All @@ -45,6 +46,7 @@ type LoggerConfig struct {
HareBeaconLoggerLevel string `mapstructure:"hare-beacon"`
TimeSyncLoggerLevel string `mapstructure:"timesync"`
VMLogLevel string `mapstructure:"vm"`
ProposalListenerLevel string `mapstructure:"proposalListener"`
}

func DefaultLoggingConfig() LoggerConfig {
Expand All @@ -57,6 +59,7 @@ func DefaultLoggingConfig() LoggerConfig {
StateDbLoggerLevel: defaultLoggingLevel.String(),
StateLoggerLevel: defaultLoggingLevel.String(),
AtxDbStoreLoggerLevel: defaultLoggingLevel.String(),
AtxHandlerLevel: defaultLoggingLevel.String(),
BeaconLoggerLevel: defaultLoggingLevel.String(),
WeakCoinLoggerLevel: defaultLoggingLevel.String(),
PoetDbStoreLoggerLevel: defaultLoggingLevel.String(),
Expand All @@ -79,5 +82,6 @@ func DefaultLoggingConfig() LoggerConfig {
HareBeaconLoggerLevel: defaultLoggingLevel.String(),
TimeSyncLoggerLevel: defaultLoggingLevel.String(),
VMLogLevel: defaultLoggingLevel.String(),
ProposalListenerLevel: defaultLoggingLevel.String(),
}
}
2 changes: 2 additions & 0 deletions config/mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func MainnetConfig() Config {
}
logging := DefaultLoggingConfig()
logging.TrtlLoggerLevel = zapcore.WarnLevel.String()
logging.AtxHandlerLevel = zapcore.WarnLevel.String()
logging.ProposalListenerLevel = zapcore.WarnLevel.String()
return Config{
BaseConfig: BaseConfig{
DataDirParent: defaultDataDir,
Expand Down
9 changes: 8 additions & 1 deletion fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ type Config struct {
MaxRetriesForRequest int
EnableServesMetrics bool `mapstructure:"servers-metrics"`
ServersConfig map[string]ServerConfig `mapstructure:"servers"`
PeersRateThreshold float64 `mapstructure:"peers-rate-threshold"`
}

func (c Config) getServerConfig(protocol string) ServerConfig {
Expand Down Expand Up @@ -142,6 +143,7 @@ func DefaultConfig() Config {
// 64 bytes
OpnProtocol: ServerConfig{Queue: 10000, Requests: 1000, Interval: time.Second},
},
PeersRateThreshold: 0.02,
}
}

Expand Down Expand Up @@ -222,12 +224,12 @@ func NewFetch(
opts ...Option,
) *Fetch {
bs := datastore.NewBlobStore(cdb.Database)

f := &Fetch{
cfg: DefaultConfig(),
logger: log.NewNop(),
bs: bs,
host: host,
peers: peers.New(),
servers: map[string]requester{},
unprocessed: make(map[types.Hash32]*request),
ongoing: make(map[types.Hash32]*request),
Expand All @@ -237,6 +239,11 @@ func NewFetch(
for _, opt := range opts {
opt(f)
}
popts := []peers.Opt{}
if f.cfg.PeersRateThreshold != 0 {
popts = append(popts, peers.WithRateThreshold(f.cfg.PeersRateThreshold))
}
f.peers = peers.New(popts...)
// NOTE(dshulyak) this is to avoid tests refactoring.
// there is one test that covers this part.
if host != nil {
Expand Down
28 changes: 22 additions & 6 deletions fetch/peers/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ func (p *data) successRate() float64 {
return float64(p.success) / float64(p.success+p.failures)
}

func (p *data) cmp(other *data) int {
func (p *data) cmp(other *data, rateThreshold float64) int {
if p == nil && other != nil {
return -1
}
const rateThreshold = 0.1
switch {
case p.rate-other.rate > rateThreshold:
return 1
Expand All @@ -41,13 +40,30 @@ func (p *data) cmp(other *data) int {
return strings.Compare(string(p.id), string(other.id))
}

func New() *Peers {
return &Peers{peers: map[peer.ID]*data{}}
type Opt func(*Peers)

func WithRateThreshold(rate float64) Opt {
return func(p *Peers) {
p.rateThreshold = rate
}
}

func New(opts ...Opt) *Peers {
p := &Peers{
peers: map[peer.ID]*data{},
rateThreshold: 0.1,
}
for _, opt := range opts {
opt(p)
}
return p
}

type Peers struct {
mu sync.Mutex
peers map[peer.ID]*data

rateThreshold float64
}

func (p *Peers) Add(id peer.ID) {
Expand Down Expand Up @@ -107,7 +123,7 @@ func (p *Peers) SelectBestFrom(peers []peer.ID) peer.ID {
if !exist {
continue
}
if best.cmp(pdata) == -1 {
if best.cmp(pdata, p.rateThreshold) == -1 {
best = pdata
}
}
Expand All @@ -134,7 +150,7 @@ func (p *Peers) SelectBest(n int) []peer.ID {
for _, peer := range p.peers {
worst := peer
for i := range cache {
if cache[i].cmp(worst) == -1 {
if cache[i].cmp(worst, p.rateThreshold) == -1 {
cache[i], worst = worst, cache[i]
}
}
Expand Down
2 changes: 1 addition & 1 deletion fetch/peers/peers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type event struct {
}

func withEvents(events []event) *Peers {
tracker := New()
tracker := New(WithRateThreshold(0.1))
for _, ev := range events {
if ev.delete {
tracker.Delete(ev.id)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require (
github.com/libp2p/go-libp2p-pubsub v0.9.3
github.com/libp2p/go-libp2p-record v0.2.0
github.com/mitchellh/mapstructure v1.5.0
github.com/multiformats/go-multiaddr v0.11.0
github.com/multiformats/go-multiaddr v0.12.0
github.com/multiformats/go-varint v0.0.7
github.com/natefinch/atomic v1.0.1
github.com/oasisprotocol/curve25519-voi v0.0.0-20230904125328-1f23a7beb09a
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -500,8 +500,8 @@ github.com/multiformats/go-base36 v0.2.0 h1:lFsAbNOGeKtuKozrtBsAkSVhv1p9D0/qedU9
github.com/multiformats/go-base36 v0.2.0/go.mod h1:qvnKE++v+2MWCfePClUEjE78Z7P2a1UV0xHgWc0hkp4=
github.com/multiformats/go-multiaddr v0.1.1/go.mod h1:aMKBKNEYmzmDmxfX88/vz+J5IU55txyt0p4aiWVohjo=
github.com/multiformats/go-multiaddr v0.2.0/go.mod h1:0nO36NvPpyV4QzvTLi/lafl2y95ncPj0vFwVF6k6wJ4=
github.com/multiformats/go-multiaddr v0.11.0 h1:XqGyJ8ufbCE0HmTDwx2kPdsrQ36AGPZNZX6s6xfJH10=
github.com/multiformats/go-multiaddr v0.11.0/go.mod h1:gWUm0QLR4thQ6+ZF6SXUw8YjtwQSPapICM+NmCkxHSM=
github.com/multiformats/go-multiaddr v0.12.0 h1:1QlibTFkoXJuDjjYsMHhE73TnzJQl8FSWatk/0gxGzE=
github.com/multiformats/go-multiaddr v0.12.0/go.mod h1:WmZXgObOQOYp9r3cslLlppkrz1FYSHmE834dfz/lWu8=
github.com/multiformats/go-multiaddr-dns v0.3.1 h1:QgQgR+LQVt3NPTjbrLLpsaT2ufAA2y0Mkk+QRVJbW3A=
github.com/multiformats/go-multiaddr-dns v0.3.1/go.mod h1:G/245BRQ6FJGmryJCrOuTdB37AMA5AMOVuO6NY3JwTk=
github.com/multiformats/go-multiaddr-fmt v0.1.0 h1:WLEFClPycPkp4fnIzoFoV9FVd49/eQsuaL3/CWe167E=
Expand Down
8 changes: 2 additions & 6 deletions hare/algorithm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,9 @@ func buildBrokerWithLimit(tb testing.TB, testName string, limit int) *testBroker
mockStateQ := mocks.NewMockstateQuerier(ctrl)
mockSyncS := smocks.NewMockSyncStateProvider(ctrl)
mockMesh := mocks.NewMockmesh(ctrl)
edVerifier, err := signing.NewEdVerifier()
require.NoError(tb, err)
mpub := pubsubmocks.NewMockPublisher(ctrl)
return &testBroker{
Broker: newBroker(config.DefaultConfig(), mockMesh, edVerifier, &mockEligibilityValidator{valid: 1}, mockStateQ, mockSyncS,
Broker: newBroker(config.DefaultConfig(), mockMesh, signing.NewEdVerifier(), &mockEligibilityValidator{valid: 1}, mockStateQ, mockSyncS,
mpub, limit, logtest.New(tb).WithName(testName)),
mockMesh: mockMesh,
mockSyncS: mockSyncS,
Expand Down Expand Up @@ -348,8 +346,6 @@ func generateConsensusProcessWithConfig(tb testing.TB, cfg config.Config, inbox
oracle := eligibility.New(logger)
edSigner, err := signing.NewEdSigner()
require.NoError(tb, err)
edVerifier, err := signing.NewEdVerifier()
require.NoError(tb, err)
edPubkey := edSigner.PublicKey()
nid := types.BytesToNodeID(edPubkey.Bytes())
oracle.Register(true, nid)
Expand All @@ -372,7 +368,7 @@ func generateConsensusProcessWithConfig(tb testing.TB, cfg config.Config, inbox
oracle,
sq,
edSigner,
edVerifier,
signing.NewEdVerifier(),
NewEligibilityTracker(cfg.N),
types.BytesToNodeID(edPubkey.Bytes()),
noopPubSub(tb),
Expand Down
4 changes: 1 addition & 3 deletions hare/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,6 @@ func createConsensusProcess(
output := make(chan report, 1)
wc := make(chan wcReport, 1)
oracle.Register(isHonest, sig.NodeID())
edVerifier, err := signing.NewEdVerifier()
require.NoError(tb, err)
c, et, err := broker.Register(ctx, layer)
require.NoError(tb, err)
mch := make(chan *types.MalfeasanceGossip, cfg.N)
Expand All @@ -204,7 +202,7 @@ func createConsensusProcess(
oracle,
broker.mockStateQ,
sig,
edVerifier,
signing.NewEdVerifier(),
et,
sig.NodeID(),
network,
Expand Down
4 changes: 1 addition & 3 deletions hare/flows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ func createTestHare(tb testing.TB, msh mesh, tcfg config.Config, clock *mockCloc
tb.Helper()
signer, err := signing.NewEdSigner()
require.NoError(tb, err)
edVerifier, err := signing.NewEdVerifier()
require.NoError(tb, err)

ctrl := gomock.NewController(tb)
patrol := mocks.NewMocklayerPatrol(ctrl)
Expand All @@ -153,7 +151,7 @@ func createTestHare(tb testing.TB, msh mesh, tcfg config.Config, clock *mockCloc
tcfg,
p2p,
signer,
edVerifier,
signing.NewEdVerifier(),
signer.NodeID(),
make(chan LayerOutput, 100),
mockSyncS,
Expand Down
Loading

0 comments on commit 679ee80

Please sign in to comment.