Skip to content

Commit

Permalink
chore tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
acud committed Jul 11, 2024
1 parent ba42f60 commit a931632
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
4 changes: 3 additions & 1 deletion hare4/hare.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
"github.com/spacemeshos/go-spacemesh/system"
)

const PROTOCOL_NAME = "hare4_compacts"
const PROTOCOL_NAME = "hare4/full_exchange"

var (
errNoLayerProposals = errors.New("no proposals for layer")
Expand Down Expand Up @@ -333,6 +333,8 @@ func (h *Hare) fetchFull(ctx context.Context, peer p2p.Peer, msgId types.Hash32)
return nil, fmt.Errorf("stream request: %w", err)
}

h.tracer.OnCompactIdResponse(resp)

return resp.Ids, nil
}

Expand Down
47 changes: 39 additions & 8 deletions hare4/hare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import (

const layersPerEpoch = 4

var wait = 10 * time.Second

func TestMain(m *testing.M) {
types.SetLayersPerEpoch(layersPerEpoch)
res := m.Run()
Expand Down Expand Up @@ -589,12 +591,29 @@ func (cl *lockstepCluster) waitStopped() {
}
}

// drainInteractiveMessages will make sure that the channels that signal
// that interactive messages came in on the tracer are read from.
func (cl *lockstepCluster) drainInteractiveMessages() {
for _, n := range cl.nodes {
go func() {
for {
select {
case <-n.tracer.compactReq:
case <-n.tracer.compactResp:
}
}
}()
}
}

func newTestTracer(tb testing.TB) *testTracer {
return &testTracer{
TB: tb,
stopped: make(chan types.LayerID, 100),
eligibility: make(chan []*types.HareEligibility),
sent: make(chan *Message),
compactReq: make(chan struct{}),
compactResp: make(chan struct{}),
}
}

Expand All @@ -603,10 +622,11 @@ type testTracer struct {
stopped chan types.LayerID
eligibility chan []*types.HareEligibility
sent chan *Message
compactReq chan struct{}
compactResp chan struct{}
}

func (t *testTracer) waitStopped() types.LayerID {
wait := 10 * time.Second
select {
case <-time.After(wait):
require.FailNow(t, "didn't stop", "wait %v", wait)
Expand All @@ -617,7 +637,6 @@ func (t *testTracer) waitStopped() types.LayerID {
}

func (t *testTracer) waitEligibility() []*types.HareEligibility {
wait := 10 * time.Second
select {
case <-time.After(wait):
require.FailNow(t, "no eligibility", "wait %v", wait)
Expand All @@ -628,7 +647,6 @@ func (t *testTracer) waitEligibility() []*types.HareEligibility {
}

func (t *testTracer) waitSent() *Message {
wait := 10 * time.Second
select {
case <-time.After(wait):
require.FailNow(t, "no message", "wait %v", wait)
Expand All @@ -648,7 +666,6 @@ func (t *testTracer) OnStop(lid types.LayerID) {
}

func (t *testTracer) OnActive(el []*types.HareEligibility) {
wait := 10 * time.Second
select {
case <-time.After(wait):
require.FailNow(t, "eligibility can't be sent", "wait %v", wait)
Expand All @@ -657,7 +674,6 @@ func (t *testTracer) OnActive(el []*types.HareEligibility) {
}

func (t *testTracer) OnMessageSent(m *Message) {
wait := 10 * time.Second
select {
case <-time.After(wait):
require.FailNow(t, "message can't be sent", "wait %v", wait)
Expand All @@ -667,9 +683,21 @@ func (t *testTracer) OnMessageSent(m *Message) {

func (*testTracer) OnMessageReceived(*Message) {}

func (t *testTracer) OnCompactIdRequest(*CompactIdRequest) {}
func (t *testTracer) OnCompactIdRequest(*CompactIdRequest) {
select {
case <-time.After(wait):
require.FailNow(t, "compact req can't be sent", "wait %v", wait)
case t.compactReq <- struct{}{}:
}
}

func (t *testTracer) OnCompactIdResponse(*CompactIdResponse) {}
func (t *testTracer) OnCompactIdResponse(*CompactIdResponse) {
select {
case <-time.After(wait):
require.FailNow(t, "compact resp can't be sent", "wait %v", wait)
case t.compactResp <- struct{}{}:
}
}

func testHare(t *testing.T, active, inactive, equivocators int, opts ...clusterOpt) {
t.Helper()
Expand All @@ -692,6 +720,7 @@ func testHare(t *testing.T, active, inactive, equivocators int, opts ...clusterO
cluster = cluster.addSigner(cluster.signersCount)
cluster.partitionSigners()
}
cluster.drainInteractiveMessages()

layer := tst.genesis + 1
cluster.setup()
Expand Down Expand Up @@ -1131,7 +1160,7 @@ func TestHare_ReconstructForward(t *testing.T) {
cluster = cluster.addSigner(cluster.signersCount)
cluster.partitionSigners()
}

cluster.drainInteractiveMessages()
layer := tst.genesis + 1

// cluster setup
Expand Down Expand Up @@ -1263,6 +1292,7 @@ func TestHare_ReconstructAll(t *testing.T) {
}
layer := tst.genesis + 1

cluster.drainInteractiveMessages()
// cluster setup
calls := [3]int{}
for i, n := range cluster.nodes {
Expand Down Expand Up @@ -1329,6 +1359,7 @@ func TestHare_ReconstructCollision(t *testing.T) {
cluster.partitionSigners()
}
layer := tst.genesis + 1
cluster.drainInteractiveMessages()
cluster.setup()
// scenario:
// node 1 has generated 1 proposal that (mocked) hash into 0xab as prefix - both nodes know the proposal
Expand Down

0 comments on commit a931632

Please sign in to comment.