From 6c8ec3a1ef294115a6c5f6d56b2b1fa8eefe34d5 Mon Sep 17 00:00:00 2001 From: Tomasz Slabon Date: Mon, 21 Aug 2023 14:16:31 +0200 Subject: [PATCH 1/6] Moved setting unmarshaller for announcement message to separate function --- pkg/protocol/announcer/announcer.go | 10 ++++++---- pkg/tbtc/dkg.go | 2 ++ pkg/tbtc/node.go | 2 ++ 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/pkg/protocol/announcer/announcer.go b/pkg/protocol/announcer/announcer.go index 47cac0c178..01a3d443df 100644 --- a/pkg/protocol/announcer/announcer.go +++ b/pkg/protocol/announcer/announcer.go @@ -71,6 +71,12 @@ type Announcer struct { membershipValidator *group.MembershipValidator } +func RegisterUnmarshaller(channel net.BroadcastChannel) { + channel.SetUnmarshaler(func() net.TaggedUnmarshaler { + return &announcementMessage{} + }) +} + // New creates a new instance of the Announcer. It expects a unique protocol // identifier, a broadcast channel configured to mediate between group members, // and a membership validator configured to validate the group membership of @@ -80,10 +86,6 @@ func New( broadcastChannel net.BroadcastChannel, membershipValidator *group.MembershipValidator, ) *Announcer { - broadcastChannel.SetUnmarshaler(func() net.TaggedUnmarshaler { - return &announcementMessage{} - }) - return &Announcer{ protocolID: protocolID, broadcastChannel: broadcastChannel, diff --git a/pkg/tbtc/dkg.go b/pkg/tbtc/dkg.go index 3c33876776..7df0b46a25 100644 --- a/pkg/tbtc/dkg.go +++ b/pkg/tbtc/dkg.go @@ -259,6 +259,8 @@ func (de *dkgExecutor) generateSigningGroup( return } + announcer.RegisterUnmarshaller(broadcastChannel) + dkgParameters, err := de.chain.DKGParameters() if err != nil { dkgLogger.Errorf("cannot get DKG parameters: [%v]", err) diff --git a/pkg/tbtc/node.go b/pkg/tbtc/node.go index 8c21e04195..77f7bc3173 100644 --- a/pkg/tbtc/node.go +++ b/pkg/tbtc/node.go @@ -18,6 +18,7 @@ import ( "github.com/keep-network/keep-common/pkg/persistence" "github.com/keep-network/keep-core/pkg/generator" "github.com/keep-network/keep-core/pkg/net" + "github.com/keep-network/keep-core/pkg/protocol/announcer" "github.com/keep-network/keep-core/pkg/protocol/group" "github.com/keep-network/keep-core/pkg/tecdsa/signing" ) @@ -241,6 +242,7 @@ func (n *node) getSigningExecutor( } signing.RegisterUnmarshallers(broadcastChannel) + announcer.RegisterUnmarshaller(broadcastChannel) membershipValidator := group.NewMembershipValidator( executorLogger, From 3e589c30013d4e636d944924383e88b2a7040904 Mon Sep 17 00:00:00 2001 From: Tomasz Slabon Date: Mon, 21 Aug 2023 14:46:33 +0200 Subject: [PATCH 2/6] Moved setting unmarshaller for signing done message to separate function --- pkg/protocol/announcer/announcer.go | 2 ++ pkg/tbtc/node.go | 14 ++++++++++++-- pkg/tbtc/signing_done.go | 4 ---- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/pkg/protocol/announcer/announcer.go b/pkg/protocol/announcer/announcer.go index 01a3d443df..c69904492f 100644 --- a/pkg/protocol/announcer/announcer.go +++ b/pkg/protocol/announcer/announcer.go @@ -71,6 +71,8 @@ type Announcer struct { membershipValidator *group.MembershipValidator } +// RegisterUnmarshaller initializes the given broadcast channel to be able to +// handle announcement messages by registering the required unmarshaller. func RegisterUnmarshaller(channel net.BroadcastChannel) { channel.SetUnmarshaler(func() net.TaggedUnmarshaler { return &announcementMessage{} diff --git a/pkg/tbtc/node.go b/pkg/tbtc/node.go index 77f7bc3173..2ca42e3522 100644 --- a/pkg/tbtc/node.go +++ b/pkg/tbtc/node.go @@ -241,8 +241,7 @@ func (n *node) getSigningExecutor( return nil, false, fmt.Errorf("failed to get broadcast channel: [%v]", err) } - signing.RegisterUnmarshallers(broadcastChannel) - announcer.RegisterUnmarshaller(broadcastChannel) + registerSigningUnmarshallers(broadcastChannel) membershipValidator := group.NewMembershipValidator( executorLogger, @@ -601,3 +600,14 @@ func withCancelOnBlock( return blockCtx, cancelBlockCtx } + +// registerSigningUnmarshallers initializes the given broadcast channel to be +// able to perform the signing protocol interactions by registering all the +// required protocol unmarshallers. +func registerSigningUnmarshallers(channel net.BroadcastChannel) { + signing.RegisterUnmarshallers(channel) + announcer.RegisterUnmarshaller(channel) + channel.SetUnmarshaler(func() net.TaggedUnmarshaler { + return &signingDoneMessage{} + }) +} diff --git a/pkg/tbtc/signing_done.go b/pkg/tbtc/signing_done.go index 76c6a67f6e..58dfeccc83 100644 --- a/pkg/tbtc/signing_done.go +++ b/pkg/tbtc/signing_done.go @@ -62,10 +62,6 @@ func newSigningDoneCheck( broadcastChannel net.BroadcastChannel, membershipValidator *group.MembershipValidator, ) *signingDoneCheck { - broadcastChannel.SetUnmarshaler(func() net.TaggedUnmarshaler { - return &signingDoneMessage{} - }) - return &signingDoneCheck{ groupSize: groupSize, broadcastChannel: broadcastChannel, From 8800eacf3d858a620260635e314b093bae6063a2 Mon Sep 17 00:00:00 2001 From: Tomasz Slabon Date: Mon, 21 Aug 2023 15:04:24 +0200 Subject: [PATCH 3/6] Updated unit tests --- pkg/protocol/announcer/announcer_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/protocol/announcer/announcer_test.go b/pkg/protocol/announcer/announcer_test.go index d3ade84fdf..2472a14db0 100644 --- a/pkg/protocol/announcer/announcer_test.go +++ b/pkg/protocol/announcer/announcer_test.go @@ -150,6 +150,8 @@ func TestAnnouncer(t *testing.T) { localChain.Signing(), ) + RegisterUnmarshaller(broadcastChannel) + announcer := New( protocolID, broadcastChannel, From 244a76916919ef5a9b5ec6816c0d5225c0450524 Mon Sep 17 00:00:00 2001 From: Tomasz Slabon Date: Thu, 24 Aug 2023 17:13:11 +0200 Subject: [PATCH 4/6] Moved setting unmarshaller just after the channel creation --- pkg/tbtc/dkg.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/pkg/tbtc/dkg.go b/pkg/tbtc/dkg.go index 7df0b46a25..162a01269e 100644 --- a/pkg/tbtc/dkg.go +++ b/pkg/tbtc/dkg.go @@ -111,7 +111,7 @@ func (de *dkgExecutor) preParamsCount() int { // the result to the chain. The execution can be delayed by an arbitrary number // of blocks using the delayBlocks argument. This allows confirming the state // on-chain - e.g. wait for the required number of confirming blocks - before -//executing the off-chain action. +// executing the off-chain action. func (de *dkgExecutor) executeDkgIfEligible( seed *big.Int, startBlock uint64, @@ -162,12 +162,12 @@ func (de *dkgExecutor) executeDkgIfEligible( // checkEligibility performs on-chain group selection and returns two pieces // of information: -// - Indexes of members selected to the signing group and controlled by this -// operator. The indexes are in range [1, `groupSize`]. The slice is nil if -// none of the selected signing group members is controlled by this operator. -// - Group selection result holding chain.OperatorID and chain.Address for -// operators selected to the signing group. There are always `groupSize` -// selected operators. +// - Indexes of members selected to the signing group and controlled by this +// operator. The indexes are in range [1, `groupSize`]. The slice is nil if +// none of the selected signing group members is controlled by this operator. +// - Group selection result holding chain.OperatorID and chain.Address for +// operators selected to the signing group. There are always `groupSize` +// selected operators. func (de *dkgExecutor) checkEligibility( dkgLogger log.StandardLogger, ) ([]uint8, *GroupSelectionResult, error) { @@ -218,6 +218,7 @@ func (de *dkgExecutor) setupBroadcastChannel( } dkg.RegisterUnmarshallers(broadcastChannel) + announcer.RegisterUnmarshaller(broadcastChannel) err = broadcastChannel.SetFilter(membershipValidator.IsInGroup) if err != nil { @@ -259,8 +260,6 @@ func (de *dkgExecutor) generateSigningGroup( return } - announcer.RegisterUnmarshaller(broadcastChannel) - dkgParameters, err := de.chain.DKGParameters() if err != nil { dkgLogger.Errorf("cannot get DKG parameters: [%v]", err) From 874c32033121796f7ee74d321449ef5faa204baa Mon Sep 17 00:00:00 2001 From: Tomasz Slabon Date: Thu, 24 Aug 2023 17:40:07 +0200 Subject: [PATCH 5/6] Updated signing done check related unit tests --- pkg/tbtc/signing_done_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/tbtc/signing_done_test.go b/pkg/tbtc/signing_done_test.go index 9dc00f2a3f..792edd6b68 100644 --- a/pkg/tbtc/signing_done_test.go +++ b/pkg/tbtc/signing_done_test.go @@ -14,6 +14,7 @@ import ( "github.com/keep-network/keep-core/internal/testutils" "github.com/keep-network/keep-core/pkg/chain" "github.com/keep-network/keep-core/pkg/chain/local_v1" + "github.com/keep-network/keep-core/pkg/net" "github.com/keep-network/keep-core/pkg/net/local" "github.com/keep-network/keep-core/pkg/operator" "github.com/keep-network/keep-core/pkg/protocol/group" @@ -326,6 +327,10 @@ func setupSigningDoneCheck( t.Fatal(err) } + broadcastChannel.SetUnmarshaler(func() net.TaggedUnmarshaler { + return &signingDoneMessage{} + }) + membershipValidator := group.NewMembershipValidator( &testutils.MockLogger{}, operators, From b6d65d93590dc154c78935260a76d225460eed99 Mon Sep 17 00:00:00 2001 From: Tomasz Slabon Date: Thu, 31 Aug 2023 15:24:03 +0200 Subject: [PATCH 6/6] Fix for failing unit tests --- pkg/tbtc/node.go | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/pkg/tbtc/node.go b/pkg/tbtc/node.go index 2ca42e3522..fe7b92d3d9 100644 --- a/pkg/tbtc/node.go +++ b/pkg/tbtc/node.go @@ -241,7 +241,11 @@ func (n *node) getSigningExecutor( return nil, false, fmt.Errorf("failed to get broadcast channel: [%v]", err) } - registerSigningUnmarshallers(broadcastChannel) + signing.RegisterUnmarshallers(broadcastChannel) + announcer.RegisterUnmarshaller(broadcastChannel) + broadcastChannel.SetUnmarshaler(func() net.TaggedUnmarshaler { + return &signingDoneMessage{} + }) membershipValidator := group.NewMembershipValidator( executorLogger, @@ -600,14 +604,3 @@ func withCancelOnBlock( return blockCtx, cancelBlockCtx } - -// registerSigningUnmarshallers initializes the given broadcast channel to be -// able to perform the signing protocol interactions by registering all the -// required protocol unmarshallers. -func registerSigningUnmarshallers(channel net.BroadcastChannel) { - signing.RegisterUnmarshallers(channel) - announcer.RegisterUnmarshaller(channel) - channel.SetUnmarshaler(func() net.TaggedUnmarshaler { - return &signingDoneMessage{} - }) -}