Skip to content

Commit

Permalink
more wip
Browse files Browse the repository at this point in the history
  • Loading branch information
acud committed Aug 15, 2024
1 parent 3e3263b commit d4445fd
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 46 deletions.
1 change: 0 additions & 1 deletion atxsdata/warmup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ func TestWarmup(t *testing.T) {
})
t.Run("db failures", func(t *testing.T) {
db := sql.InMemory()
nonce := types.VRFPostIndex(1)
data := gatx(types.ATXID{1, 1}, 1, types.NodeID{1})
require.NoError(t, atxs.Add(db, &data, types.AtxBlob{}))

Expand Down
30 changes: 15 additions & 15 deletions beacon/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ import (
"github.com/spacemeshos/go-spacemesh/signing"
)

const epochWeight = uint64(100)
const (
epochWeight = uint64(100)
epoch = types.EpochID(10)
round = types.RoundID(5)
)

func createProtocolDriverWithFirstRoundVotes(
t *testing.T,
signer *signing.EdSigner,
malicious bool,
epoch types.EpochID,
round types.RoundID,
) (*testProtocolDriver, proposalList) {
tpd := setUpProtocolDriver(t)
tpd.setBeginProtocol(context.Background())
Expand Down Expand Up @@ -1168,11 +1170,9 @@ func Test_HandleFirstVotes_MinerMissingATX(t *testing.T) {
func Test_HandleFollowingVotes_Success(t *testing.T) {
t.Parallel()

const epoch = types.EpochID(10)
const round = types.RoundID(5)
signer, err := signing.NewEdSigner()
require.NoError(t, err)
tpd, plist := createProtocolDriverWithFirstRoundVotes(t, signer, false, epoch, round)
tpd, plist := createProtocolDriverWithFirstRoundVotes(t, signer, false)

// this msg will contain a bit vector that set bit 0 and 2
msg := createFollowingVote(t, signer, epoch, round, []byte{0b101}, false)
Expand Down Expand Up @@ -1202,7 +1202,7 @@ func Test_HandleFollowingVotes_Malicious(t *testing.T) {
const round = types.RoundID(5)
signer, err := signing.NewEdSigner()
require.NoError(t, err)
tpd, plist := createProtocolDriverWithFirstRoundVotes(t, signer, true, epoch, round)
tpd, plist := createProtocolDriverWithFirstRoundVotes(t, signer, true)

// this msg will contain a bit vector that set bit 0 and 2
msg := createFollowingVote(t, signer, epoch, round, []byte{0b101}, false)
Expand All @@ -1228,7 +1228,7 @@ func Test_HandleFollowingVotes_Shutdown(t *testing.T) {
const round = types.RoundID(5)
signer, err := signing.NewEdSigner()
require.NoError(t, err)
tpd, plist := createProtocolDriverWithFirstRoundVotes(t, signer, false, epoch, round)
tpd, plist := createProtocolDriverWithFirstRoundVotes(t, signer, false)
tpd.Close()

// this msg will contain a bit vector that set bit 0 and 2
Expand All @@ -1249,7 +1249,7 @@ func Test_HandleFollowingVotes_NotInProtocol(t *testing.T) {
const round = types.RoundID(5)
signer, err := signing.NewEdSigner()
require.NoError(t, err)
tpd, plist := createProtocolDriverWithFirstRoundVotes(t, signer, false, epoch, round)
tpd, plist := createProtocolDriverWithFirstRoundVotes(t, signer, false)

// this msg will contain a bit vector that set bit 0 and 2
msg := createFollowingVote(t, signer, epoch, round, []byte{0b101}, false)
Expand All @@ -1270,7 +1270,7 @@ func Test_handleFollowingVotes_CorruptMsg(t *testing.T) {
const round = types.RoundID(5)
signer, err := signing.NewEdSigner()
require.NoError(t, err)
tpd, plist := createProtocolDriverWithFirstRoundVotes(t, signer, false, epoch, round)
tpd, plist := createProtocolDriverWithFirstRoundVotes(t, signer, false)

// this msg will contain a bit vector that set bit 0 and 2
msg := createFollowingVote(t, signer, epoch, round, []byte{0b101}, false)
Expand All @@ -1290,7 +1290,7 @@ func Test_handleFollowingVotes_WrongEpoch(t *testing.T) {
const round = types.RoundID(5)
signer, err := signing.NewEdSigner()
require.NoError(t, err)
tpd, plist := createProtocolDriverWithFirstRoundVotes(t, signer, false, epoch, round)
tpd, plist := createProtocolDriverWithFirstRoundVotes(t, signer, false)
minerAtxs := map[types.NodeID]*minerInfo{
signer.NodeID(): {atxid: createATX(t, tpd.cdb, (epoch - 1).FirstLayer().Sub(1), signer, 10, time.Now())},
}
Expand Down Expand Up @@ -1333,7 +1333,7 @@ func Test_handleFollowingVotes_TooEarly(t *testing.T) {
const round = types.RoundID(5)
signer, err := signing.NewEdSigner()
require.NoError(t, err)
tpd, plist := createProtocolDriverWithFirstRoundVotes(t, signer, false, epoch, round)
tpd, plist := createProtocolDriverWithFirstRoundVotes(t, signer, false)

// this msg will contain a bit vector that set bit 0 and 2
msg := createFollowingVote(t, signer, epoch, round, []byte{0b101}, false)
Expand All @@ -1356,7 +1356,7 @@ func Test_handleFollowingVotes_FailedToVerifySig(t *testing.T) {
const round = types.RoundID(5)
signer, err := signing.NewEdSigner()
require.NoError(t, err)
tpd, plist := createProtocolDriverWithFirstRoundVotes(t, signer, false, epoch, round)
tpd, plist := createProtocolDriverWithFirstRoundVotes(t, signer, false)

// this msg will contain a bit vector that set bit 0 and 2
msg := createFollowingVote(t, signer, epoch, round, []byte{0b101}, true)
Expand All @@ -1378,7 +1378,7 @@ func Test_handleFollowingVotes_AlreadyVoted(t *testing.T) {
const round = types.RoundID(5)
signer, err := signing.NewEdSigner()
require.NoError(t, err)
tpd, plist := createProtocolDriverWithFirstRoundVotes(t, signer, false, epoch, round)
tpd, plist := createProtocolDriverWithFirstRoundVotes(t, signer, false)

// this msg will contain a bit vector that set bit 0 and 2
msg := createFollowingVote(t, signer, epoch, round, []byte{0b101}, false)
Expand Down Expand Up @@ -1420,7 +1420,7 @@ func Test_handleFollowingVotes_MinerMissingATX(t *testing.T) {
const round = types.RoundID(5)
signer, err := signing.NewEdSigner()
require.NoError(t, err)
tpd, plist := createProtocolDriverWithFirstRoundVotes(t, signer, false, epoch, round)
tpd, plist := createProtocolDriverWithFirstRoundVotes(t, signer, false)

miner, err := signing.NewEdSigner()
require.NoError(t, err)
Expand Down
3 changes: 1 addition & 2 deletions blocks/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func getProposalMetadata(
meshHashes = make(map[types.Hash32]*meshState)
err error
)
md.tickHeight, md.rewards, err = rewardInfoAndHeight(cfg, db, atxs, proposals)
md.tickHeight, md.rewards, err = rewardInfoAndHeight(db, atxs, proposals)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -231,7 +231,6 @@ func toUint64Slice(b []byte) []uint64 {
}

func rewardInfoAndHeight(
cfg Config,
db *sql.Database,
atxs *atxsdata.Data,
props []*types.Proposal,
Expand Down
6 changes: 3 additions & 3 deletions cmd/merge-nodes/internal/merge_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func MergeDBs(ctx context.Context, dbLog *zap.Logger, from, to string) error {
defer dstDB.Close()
// target database exists, check if there is at least one key in the target key directory
// not named supervisedIDKeyFileName
if err := checkIdentities(dbLog, to); err != nil {
if err := checkIdentities(to); err != nil {
switch {
case errors.Is(err, ErrSupervisedNode):
dbLog.Sugar().Errorf(
Expand All @@ -76,7 +76,7 @@ func MergeDBs(ctx context.Context, dbLog *zap.Logger, from, to string) error {
return fmt.Errorf("close source database: %w", err)
}

if err := checkIdentities(dbLog, from); err != nil {
if err := checkIdentities(from); err != nil {
switch {
case errors.Is(err, ErrSupervisedNode):
dbLog.Sugar().Errorf(
Expand Down Expand Up @@ -226,7 +226,7 @@ func openDB(dbLog *zap.Logger, path string) (*localsql.Database, error) {
return db, nil
}

func checkIdentities(dbLog *zap.Logger, path string) error {
func checkIdentities(path string) error {
dir := filepath.Join(path, keyDir)
if err := os.MkdirAll(dir, 0o700); err != nil {
return err
Expand Down
17 changes: 9 additions & 8 deletions fetch/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import (
)

// getCachedEntry is a thread-safe cache get helper.
func getCachedEntry(cache *HashPeersCache, hash types.Hash32) (HashPeers, bool) {
func getCachedEntry(cache *HashPeersCache, hash types.Hash32) HashPeers {
cache.mu.Lock()
defer cache.mu.Unlock()

return cache.get(hash)
hp, _ := cache.get(hash)
return hp
}

func TestAdd(t *testing.T) {
Expand All @@ -45,7 +46,7 @@ func TestAdd(t *testing.T) {
cache.Add(hash, peer3)
}()
wg.Wait()
hashPeers, _ := getCachedEntry(cache, hash)
hashPeers := getCachedEntry(cache, hash)
require.Len(t, hashPeers, 3)
})
t.Run("2Hashes1Peer", func(t *testing.T) {
Expand All @@ -64,9 +65,9 @@ func TestAdd(t *testing.T) {
cache.Add(hash2, peer)
}()
wg.Wait()
hash1Peers, _ := getCachedEntry(cache, hash1)
hash1Peers := getCachedEntry(cache, hash1)
require.Len(t, hash1Peers, 1)
hash2Peers, _ := getCachedEntry(cache, hash2)
hash2Peers := getCachedEntry(cache, hash2)
require.Len(t, hash2Peers, 1)
})
}
Expand Down Expand Up @@ -144,11 +145,11 @@ func TestRegisterPeerHashes(t *testing.T) {
hash3 := types.RandomHash()
peer1 := p2p.Peer("test_peer_1")
cache.RegisterPeerHashes(peer1, []types.Hash32{hash1, hash2, hash3})
hash1Peers, _ := getCachedEntry(cache, hash1)
hash1Peers := getCachedEntry(cache, hash1)
require.Len(t, hash1Peers, 1)
hash2Peers, _ := getCachedEntry(cache, hash2)
hash2Peers := getCachedEntry(cache, hash2)
require.Len(t, hash2Peers, 1)
hash3Peers, _ := getCachedEntry(cache, hash3)
hash3Peers := getCachedEntry(cache, hash3)
require.Len(t, hash3Peers, 1)
})
}
Expand Down
12 changes: 6 additions & 6 deletions hare3/eligibility/oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ func TestBuildVRFMessage_BeaconError(t *testing.T) {
o := defaultOracle(t)
errUnknown := errors.New("unknown")
o.mBeacon.EXPECT().GetBeacon(gomock.Any()).Return(types.EmptyBeacon, errUnknown).Times(1)
msg, err := o.buildVRFMessage(context.Background(), types.LayerID(1), 1)
msg, err := o.buildVRFMessage(types.LayerID(1), 1)
require.ErrorIs(t, err, errUnknown)
require.Nil(t, msg)
}
Expand All @@ -462,24 +462,24 @@ func TestBuildVRFMessage(t *testing.T) {
secondLayer := firstLayer.Add(1)
beacon := types.RandomBeacon()
o.mBeacon.EXPECT().GetBeacon(firstLayer.GetEpoch()).Return(beacon, nil).Times(1)
m1, err := o.buildVRFMessage(context.Background(), firstLayer, 2)
m1, err := o.buildVRFMessage(firstLayer, 2)
require.NoError(t, err)

// check not same for different round
o.mBeacon.EXPECT().GetBeacon(firstLayer.GetEpoch()).Return(beacon, nil).Times(1)
m3, err := o.buildVRFMessage(context.Background(), firstLayer, 3)
m3, err := o.buildVRFMessage(firstLayer, 3)
require.NoError(t, err)
require.NotEqual(t, m1, m3)

// check not same for different layer
o.mBeacon.EXPECT().GetBeacon(firstLayer.GetEpoch()).Return(beacon, nil).Times(1)
m4, err := o.buildVRFMessage(context.Background(), secondLayer, 2)
m4, err := o.buildVRFMessage(secondLayer, 2)
require.NoError(t, err)
require.NotEqual(t, m1, m4)

// check same call returns same result
o.mBeacon.EXPECT().GetBeacon(firstLayer.GetEpoch()).Return(beacon, nil).Times(1)
m5, err := o.buildVRFMessage(context.Background(), firstLayer, 2)
m5, err := o.buildVRFMessage(firstLayer, 2)
require.NoError(t, err)
require.Equal(t, m1, m5) // check same result
}
Expand All @@ -495,7 +495,7 @@ func TestBuildVRFMessage_Concurrency(t *testing.T) {
for i := 0; i < total; i++ {
wg.Add(1)
go func(x int) {
_, err := o.buildVRFMessage(context.Background(), firstLayer, uint32(x%expectAdd))
_, err := o.buildVRFMessage(firstLayer, uint32(x%expectAdd))
assert.NoError(t, err)
wg.Done()
}(i)
Expand Down
2 changes: 1 addition & 1 deletion hare4/eligibility/oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (o *Oracle) prepareEligibilityCheck(
return 0, fixed.Fixed{}, fixed.Fixed{}, true, err
}

msg, err := o.buildVRFMessage(ctx, layer, round)
msg, err := o.buildVRFMessage(layer, round)
if err != nil {
logger.Warn("could not build vrf message", zap.Error(err))
return 0, fixed.Fixed{}, fixed.Fixed{}, true, err
Expand Down
12 changes: 6 additions & 6 deletions hare4/eligibility/oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ func TestBuildVRFMessage_BeaconError(t *testing.T) {
o := defaultOracle(t)
errUnknown := errors.New("unknown")
o.mBeacon.EXPECT().GetBeacon(gomock.Any()).Return(types.EmptyBeacon, errUnknown).Times(1)
msg, err := o.buildVRFMessage(context.Background(), types.LayerID(1), 1)
msg, err := o.buildVRFMessage(types.LayerID(1), 1)
require.ErrorIs(t, err, errUnknown)
require.Nil(t, msg)
}
Expand All @@ -462,24 +462,24 @@ func TestBuildVRFMessage(t *testing.T) {
secondLayer := firstLayer.Add(1)
beacon := types.RandomBeacon()
o.mBeacon.EXPECT().GetBeacon(firstLayer.GetEpoch()).Return(beacon, nil).Times(1)
m1, err := o.buildVRFMessage(context.Background(), firstLayer, 2)
m1, err := o.buildVRFMessage(firstLayer, 2)
require.NoError(t, err)

// check not same for different round
o.mBeacon.EXPECT().GetBeacon(firstLayer.GetEpoch()).Return(beacon, nil).Times(1)
m3, err := o.buildVRFMessage(context.Background(), firstLayer, 3)
m3, err := o.buildVRFMessage(firstLayer, 3)
require.NoError(t, err)
require.NotEqual(t, m1, m3)

// check not same for different layer
o.mBeacon.EXPECT().GetBeacon(firstLayer.GetEpoch()).Return(beacon, nil).Times(1)
m4, err := o.buildVRFMessage(context.Background(), secondLayer, 2)
m4, err := o.buildVRFMessage(secondLayer, 2)
require.NoError(t, err)
require.NotEqual(t, m1, m4)

// check same call returns same result
o.mBeacon.EXPECT().GetBeacon(firstLayer.GetEpoch()).Return(beacon, nil).Times(1)
m5, err := o.buildVRFMessage(context.Background(), firstLayer, 2)
m5, err := o.buildVRFMessage(firstLayer, 2)
require.NoError(t, err)
require.Equal(t, m1, m5) // check same result
}
Expand All @@ -495,7 +495,7 @@ func TestBuildVRFMessage_Concurrency(t *testing.T) {
for i := 0; i < total; i++ {
wg.Add(1)
go func(x int) {
_, err := o.buildVRFMessage(context.Background(), firstLayer, uint32(x%expectAdd))
_, err := o.buildVRFMessage(firstLayer, uint32(x%expectAdd))
assert.NoError(t, err)
wg.Done()
}(i)
Expand Down
6 changes: 3 additions & 3 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func TestSpacemeshApp_GrpcService(t *testing.T) {
require.NoError(t, err)

run := func(c *cobra.Command, args []string) error {
return app.startAPIServices(context.Background())
return app.startAPIServices()
}
defer app.stopServices(context.Background())

Expand Down Expand Up @@ -277,7 +277,7 @@ func TestSpacemeshApp_JsonServiceNotRunning(t *testing.T) {

// Make sure the service is not running by default
run := func(c *cobra.Command, args []string) error {
return app.startAPIServices(context.Background())
return app.startAPIServices()
}

str, err := testArgs(context.Background(), cmdWithRun(run))
Expand Down Expand Up @@ -312,7 +312,7 @@ func TestSpacemeshApp_JsonService(t *testing.T) {

// Make sure the service is not running by default
run := func(c *cobra.Command, args []string) error {
return app.startAPIServices(context.Background())
return app.startAPIServices()
}

// Test starting the JSON server from the command line
Expand Down
2 changes: 1 addition & 1 deletion tortoise/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ func TestStateDecodeVotes(t *testing.T) {
lid := base.layer.Add(1)
encoded := opinion.encodedVotes()
var rst votes
rst, _, err = decodeVotes(genesis.Sub(1), lid, base, encoded)
rst, _, err = decodeVotes(lid, base, encoded)
if err != nil {
break
}
Expand Down

0 comments on commit d4445fd

Please sign in to comment.