From 9fa943eecd53f33dd6eb3f4184f5a56e009fc233 Mon Sep 17 00:00:00 2001 From: Evan Gray Date: Tue, 6 Sep 2022 23:36:02 +0000 Subject: [PATCH] node: support for heartbeat spying fixes #1768 --- node/cmd/guardiand/node.go | 2 +- node/cmd/spy/spy.go | 2 +- node/pkg/common/guardianset.go | 11 ++++++++++- node/pkg/common/guardianset_test.go | 6 +++--- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/node/cmd/guardiand/node.go b/node/cmd/guardiand/node.go index 91b15cc50..c6912095d 100644 --- a/node/cmd/guardiand/node.go +++ b/node/cmd/guardiand/node.go @@ -782,7 +782,7 @@ func runNode(cmd *cobra.Command, args []string) { injectC := make(chan *vaa.VAA) // Guardian set state managed by processor - gst := common.NewGuardianSetState() + gst := common.NewGuardianSetState(nil) // Per-chain observation requests chainObsvReqC := make(map[vaa.ChainID]chan *gossipv1.ObservationRequest) diff --git a/node/cmd/spy/spy.go b/node/cmd/spy/spy.go index fb6409fe8..c0b72f187 100644 --- a/node/cmd/spy/spy.go +++ b/node/cmd/spy/spy.go @@ -257,7 +257,7 @@ func runSpy(cmd *cobra.Command, args []string) { signedInC := make(chan *gossipv1.SignedVAAWithQuorum, 50) // Guardian set state managed by processor - gst := common.NewGuardianSetState() + gst := common.NewGuardianSetState(nil) // RPC server s := newSpyServer(logger) diff --git a/node/pkg/common/guardianset.go b/node/pkg/common/guardianset.go index a85fd42e3..b1a06b3c2 100644 --- a/node/pkg/common/guardianset.go +++ b/node/pkg/common/guardianset.go @@ -82,11 +82,17 @@ type GuardianSetState struct { // Last heartbeat message received per guardian per p2p node. Maintained // across guardian set updates - these values don't change. lastHeartbeats map[common.Address]map[peer.ID]*gossipv1.Heartbeat + updateC chan *gossipv1.Heartbeat } -func NewGuardianSetState() *GuardianSetState { +// NewGuardianSetState returns a new GuardianSetState. +// +// The provided channel will be pushed heartbeat updates as they are set, +// but be aware that the channel will block guardian set updates if full. +func NewGuardianSetState(guardianSetStateUpdateC chan *gossipv1.Heartbeat) *GuardianSetState { return &GuardianSetState{ lastHeartbeats: map[common.Address]map[peer.ID]*gossipv1.Heartbeat{}, + updateC: guardianSetStateUpdateC, } } @@ -136,6 +142,9 @@ func (st *GuardianSetState) SetHeartbeat(addr common.Address, peerId peer.ID, hb } v[peerId] = hb + if st.updateC != nil { + st.updateC <- hb + } return nil } diff --git a/node/pkg/common/guardianset_test.go b/node/pkg/common/guardianset_test.go index 5ef2c4a39..95c9afbf9 100644 --- a/node/pkg/common/guardianset_test.go +++ b/node/pkg/common/guardianset_test.go @@ -57,7 +57,7 @@ func TestKeysAsHexStrings(t *testing.T) { } func TestNewGuardianSetState(t *testing.T) { - gss := NewGuardianSetState() + gss := NewGuardianSetState(nil) assert.NotNil(t, gss) assert.Nil(t, gss.current) assert.Nil(t, gss.Get()) @@ -72,7 +72,7 @@ func TestSet(t *testing.T) { Index: 1, } - gss := NewGuardianSetState() + gss := NewGuardianSetState(nil) assert.Nil(t, gss.current) gss.Set(&gs) assert.Equal(t, gss.current, &gs) @@ -87,7 +87,7 @@ func TestGet(t *testing.T) { Index: 1, } - gss := NewGuardianSetState() + gss := NewGuardianSetState(nil) assert.Nil(t, gss.Get()) gss.Set(&gs) assert.Equal(t, gss.Get(), &gs)