Skip to content

Commit

Permalink
Use zap in beacon
Browse files Browse the repository at this point in the history
  • Loading branch information
fasmat committed May 23, 2024
1 parent a503c3d commit 0420f5c
Show file tree
Hide file tree
Showing 13 changed files with 454 additions and 353 deletions.
311 changes: 190 additions & 121 deletions beacon/beacon.go

Large diffs are not rendered by default.

66 changes: 33 additions & 33 deletions beacon/beacon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import (
"github.com/spacemeshos/fixed"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"go.uber.org/zap/zaptest"
"golang.org/x/sync/errgroup"

"github.com/spacemeshos/go-spacemesh/beacon/metrics"
"github.com/spacemeshos/go-spacemesh/beacon/weakcoin"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/common/types/result"
"github.com/spacemeshos/go-spacemesh/datastore"
"github.com/spacemeshos/go-spacemesh/log/logtest"
"github.com/spacemeshos/go-spacemesh/p2p"
"github.com/spacemeshos/go-spacemesh/p2p/pubsub"
pubsubmocks "github.com/spacemeshos/go-spacemesh/p2p/pubsub/mocks"
Expand Down Expand Up @@ -87,11 +87,11 @@ func newTestDriver(tb testing.TB, cfg Config, p pubsub.Publisher, miners int, id
mSync: mocks.NewMockSyncStateProvider(ctrl),
mVerifier: NewMockvrfVerifier(ctrl),
}
lg := logtest.New(tb).Named(id)
lg := zaptest.NewLogger(tb).Named(id)

tpd.mVerifier.EXPECT().Verify(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(true)

tpd.cdb = datastore.NewCachedDB(sql.InMemory(), lg.Zap())
tpd.cdb = datastore.NewCachedDB(sql.InMemory(), lg)
tpd.ProtocolDriver = New(p, signing.NewEdVerifier(), tpd.mVerifier, tpd.cdb, tpd.mClock,
WithConfig(cfg),
WithLogger(lg),
Expand Down Expand Up @@ -490,15 +490,15 @@ func TestBeaconWithMetrics(t *testing.T) {

func TestBeacon_NoRaceOnClose(t *testing.T) {
mclock := NewMocklayerClock(gomock.NewController(t))
lg := logtest.New(t)
lg := zaptest.NewLogger(t)
pd := &ProtocolDriver{
logger: lg.WithName("Beacon"),
logger: lg.Named("Beacon"),
beacons: make(map[types.EpochID]types.Beacon),
cdb: datastore.NewCachedDB(sql.InMemory(), lg.Zap()),
cdb: datastore.NewCachedDB(sql.InMemory(), lg),
clock: mclock,
closed: make(chan struct{}),
results: make(chan result.Beacon, 100),
metricsCollector: metrics.NewBeaconMetricsCollector(nil, lg.WithName("metrics")),
metricsCollector: metrics.NewBeaconMetricsCollector(nil, lg.Named("metrics")),
}
// check for a race between onResult and Close
var eg errgroup.Group
Expand All @@ -524,12 +524,12 @@ func TestBeacon_NoRaceOnClose(t *testing.T) {
func TestBeacon_BeaconsWithDatabase(t *testing.T) {
t.Parallel()

lg := logtest.New(t)
lg := zaptest.NewLogger(t)
mclock := NewMocklayerClock(gomock.NewController(t))
pd := &ProtocolDriver{
logger: lg.WithName("Beacon"),
logger: lg.Named("Beacon"),
beacons: make(map[types.EpochID]types.Beacon),
cdb: datastore.NewCachedDB(sql.InMemory(), lg.Zap()),
cdb: datastore.NewCachedDB(sql.InMemory(), lg),
clock: mclock,
}
epoch3 := types.EpochID(3)
Expand Down Expand Up @@ -577,12 +577,12 @@ func TestBeacon_BeaconsWithDatabase(t *testing.T) {
func TestBeacon_BeaconsWithDatabaseFailure(t *testing.T) {
t.Parallel()

lg := logtest.New(t)
lg := zaptest.NewLogger(t)
mclock := NewMocklayerClock(gomock.NewController(t))
pd := &ProtocolDriver{
logger: lg.WithName("Beacon"),
logger: lg.Named("Beacon"),
beacons: make(map[types.EpochID]types.Beacon),
cdb: datastore.NewCachedDB(sql.InMemory(), lg.Zap()),
cdb: datastore.NewCachedDB(sql.InMemory(), lg),
clock: mclock,
}
epoch := types.EpochID(3)
Expand All @@ -596,11 +596,11 @@ func TestBeacon_BeaconsWithDatabaseFailure(t *testing.T) {
func TestBeacon_BeaconsCleanupOldEpoch(t *testing.T) {
t.Parallel()

lg := logtest.New(t)
lg := zaptest.NewLogger(t)
mclock := NewMocklayerClock(gomock.NewController(t))
pd := &ProtocolDriver{
logger: lg.WithName("Beacon"),
cdb: datastore.NewCachedDB(sql.InMemory(), lg.Zap()),
logger: lg.Named("Beacon"),
cdb: datastore.NewCachedDB(sql.InMemory(), lg),
beacons: make(map[types.EpochID]types.Beacon),
ballotsBeacons: make(map[types.EpochID]map[types.Beacon]*beaconWeight),
clock: mclock,
Expand Down Expand Up @@ -700,12 +700,12 @@ func TestBeacon_ReportBeaconFromBallot(t *testing.T) {
require.Greater(t, maxWeight.Float(), 0.0)
}

lg := logtest.New(t)
lg := zaptest.NewLogger(t)
mclock := NewMocklayerClock(gomock.NewController(t))
pd := &ProtocolDriver{
logger: lg.WithName("Beacon"),
logger: lg.Named("Beacon"),
config: UnitTestConfig(),
cdb: datastore.NewCachedDB(sql.InMemory(), lg.Zap()),
cdb: datastore.NewCachedDB(sql.InMemory(), lg),
beacons: make(map[types.EpochID]types.Beacon),
ballotsBeacons: make(map[types.EpochID]map[types.Beacon]*beaconWeight),
clock: mclock,
Expand Down Expand Up @@ -736,12 +736,12 @@ func TestBeacon_ReportBeaconFromBallot(t *testing.T) {
func TestBeacon_ReportBeaconFromBallot_SameBallot(t *testing.T) {
t.Parallel()

lg := logtest.New(t)
lg := zaptest.NewLogger(t)
mclock := NewMocklayerClock(gomock.NewController(t))
pd := &ProtocolDriver{
logger: lg.WithName("Beacon"),
logger: lg.Named("Beacon"),
config: UnitTestConfig(),
cdb: datastore.NewCachedDB(sql.InMemory(), lg.Zap()),
cdb: datastore.NewCachedDB(sql.InMemory(), lg),
beacons: make(map[types.EpochID]types.Beacon),
ballotsBeacons: make(map[types.EpochID]map[types.Beacon]*beaconWeight),
clock: mclock,
Expand Down Expand Up @@ -777,7 +777,7 @@ func TestBeacon_ensureEpochHasBeacon_BeaconAlreadyCalculated(t *testing.T) {
beacon := types.RandomBeacon()
beaconFromBallots := types.RandomBeacon()
pd := &ProtocolDriver{
logger: logtest.New(t).WithName("Beacon"),
logger: zaptest.NewLogger(t).Named("Beacon"),
config: UnitTestConfig(),
beacons: map[types.EpochID]types.Beacon{
epoch: beacon,
Expand Down Expand Up @@ -829,7 +829,7 @@ func TestBeacon_findMajorityBeacon(t *testing.T) {
}
epoch := types.EpochID(3)
pd := &ProtocolDriver{
logger: logtest.New(t).WithName("Beacon"),
logger: zaptest.NewLogger(t).Named("Beacon"),
config: UnitTestConfig(),
beacons: make(map[types.EpochID]types.Beacon),
ballotsBeacons: map[types.EpochID]map[types.Beacon]*beaconWeight{epoch: beaconFromBallots},
Expand Down Expand Up @@ -865,7 +865,7 @@ func TestBeacon_findMajorityBeacon_plurality(t *testing.T) {
}
epoch := types.EpochID(3)
pd := &ProtocolDriver{
logger: logtest.New(t).WithName("Beacon"),
logger: zaptest.NewLogger(t).Named("Beacon"),
config: UnitTestConfig(),
beacons: make(map[types.EpochID]types.Beacon),
ballotsBeacons: map[types.EpochID]map[types.Beacon]*beaconWeight{epoch: beaconFromBallots},
Expand Down Expand Up @@ -901,7 +901,7 @@ func TestBeacon_findMajorityBeacon_NotEnoughBallots(t *testing.T) {
}
epoch := types.EpochID(3)
pd := &ProtocolDriver{
logger: logtest.New(t).WithName("Beacon"),
logger: zaptest.NewLogger(t).Named("Beacon"),
config: UnitTestConfig(),
beacons: make(map[types.EpochID]types.Beacon),
ballotsBeacons: map[types.EpochID]map[types.Beacon]*beaconWeight{epoch: beaconFromBallots},
Expand All @@ -915,7 +915,7 @@ func TestBeacon_findMajorityBeacon_NoBeacon(t *testing.T) {
t.Parallel()

pd := &ProtocolDriver{
logger: logtest.New(t).WithName("Beacon"),
logger: zaptest.NewLogger(t).Named("Beacon"),
config: UnitTestConfig(),
beacons: make(map[types.EpochID]types.Beacon),
ballotsBeacons: make(map[types.EpochID]map[types.Beacon]*beaconWeight),
Expand Down Expand Up @@ -1049,15 +1049,15 @@ func TestBeacon_proposalPassesEligibilityThreshold(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

logger := logtest.New(t).WithName("proposal checker")
checker := createProposalChecker(logger, cfg, tc.wEarly, tc.wOntime)
logger := zaptest.NewLogger(t)
checker := createProposalChecker(logger.Named("proposal checker"), cfg, tc.wEarly, tc.wOntime)
var numEligible, numEligibleStrict int
for i := 0; i < tc.wEarly; i++ {
signer, err := signing.NewEdSigner()
require.NoError(t, err)
proposal := buildSignedProposal(
context.Background(),
logtest.New(t),
logger,
signer.VRFSigner(),
3,
types.VRFPostIndex(1),
Expand Down Expand Up @@ -1095,7 +1095,7 @@ func TestBeacon_buildProposal(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

result := buildProposal(logtest.New(t), tc.epoch, types.VRFPostIndex(1))
result := buildProposal(tc.epoch, types.VRFPostIndex(1))
require.Equal(t, tc.result, string(result))
})
}
Expand Down Expand Up @@ -1130,7 +1130,7 @@ func TestBeacon_getSignedProposal(t *testing.T) {

result := buildSignedProposal(
context.Background(),
logtest.New(t),
zaptest.NewLogger(t),
edSgn.VRFSigner(),
tc.epoch,
types.VRFPostIndex(1),
Expand All @@ -1148,7 +1148,7 @@ func TestBeacon_calcBeacon(t *testing.T) {
Proposal{0x05}: {},
}

beacon := calcBeacon(logtest.New(t), set)
beacon := calcBeacon(zaptest.NewLogger(t), set)
expected := types.HexToBeacon("0xe69fd154")
require.EqualValues(t, expected, beacon)
}
Loading

0 comments on commit 0420f5c

Please sign in to comment.