Skip to content

Commit e4267c4

Browse files
Refactor/fix state sync logs (#515)
## Description Improves logs for the state sync submodule. ## Issue Fixes #514 ## Type of change Please mark the relevant option(s): - [ ] New feature, functionality or library - [ ] Bug fix - [X] Code health or cleanup - [ ] Major breaking change - [ ] Documentation - [ ] Other <!-- add details here if it a different type of change --> ## List of changes - Add constant state sync message types to the consensus module types - Update logs and error messages of the state sync - Delete unnecessary server mode enabling op in app/pocket/main.go ## Testing - [X] `make develop_test` - [X] [LocalNet](https://github.com/pokt-network/pocket/blob/main/docs/development/README.md) w/ all of the steps outlined in the `README` ## Required Checklist - [X] I have performed a self-review of my own code - [X] I have commented my code, particularly in hard-to-understand areas - [X] I have tested my changes using the available tooling - [X] I have updated the corresponding CHANGELOG ### If Applicable Checklist - [ ] I have updated the corresponding README(s); local and/or global - [ ] I have added tests that prove my fix is effective or that my feature works - [ ] I have added, or updated, [mermaid.js](https://mermaid-js.github.io) diagrams in the corresponding README(s) - [ ] I have added, or updated, documentation and [mermaid.js](https://mermaid-js.github.io) diagrams in `shared/docs/*` if I updated `shared/*`README(s) --------- Co-authored-by: Daniel Olshansky <[email protected]>
1 parent a90d766 commit e4267c4

File tree

8 files changed

+92
-24
lines changed

8 files changed

+92
-24
lines changed

app/pocket/doc/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.0.0.5] - 2023-02-17
11+
12+
- Removed unnecessary server mode enabling call via `EnableServerMode()` function
13+
1014
## [0.0.0.4] - 2023-02-07
1115

1216
- Added GITHUB_WIKI tags where it was missing

app/pocket/main.go

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ func main() {
3131
if err != nil {
3232
logger.Global.Fatal().Err(err).Msg("Failed to create pocket node")
3333
}
34-
pocketNode.GetBus().GetConsensusModule().EnableServerMode()
3534

3635
if err = pocketNode.Start(); err != nil {
3736
logger.Global.Fatal().Err(err).Msg("Failed to start pocket node")

consensus/debugging.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,12 @@ func (m *consensusModule) sendGetBlockStateSyncMessage(_ *messaging.DebugMessage
124124
}
125125

126126
for _, val := range validators {
127+
if m.GetNodeAddress() == val.GetAddress() {
128+
continue
129+
}
127130
valAddress := cryptoPocket.AddressFromString(val.GetAddress())
128131
if err := m.stateSync.SendStateSyncMessage(stateSyncGetBlockMessage, valAddress, requestHeight); err != nil {
129-
m.logger.Debug().Msgf(typesCons.SendingStateSyncMessage(valAddress, requestHeight), err)
132+
m.logger.Error().Err(err).Str("proto_type", "GetBlockRequest").Msg("failed to send StateSyncMessage")
130133
}
131134
}
132135
}
@@ -151,9 +154,12 @@ func (m *consensusModule) sendGetMetadataStateSyncMessage(_ *messaging.DebugMess
151154
}
152155

153156
for _, val := range validators {
157+
if m.GetNodeAddress() != val.GetAddress() {
158+
continue
159+
}
154160
valAddress := cryptoPocket.AddressFromString(val.GetAddress())
155161
if err := m.stateSync.SendStateSyncMessage(stateSyncMetaDataReqMessage, valAddress, requestHeight); err != nil {
156-
m.logger.Debug().Msgf(typesCons.SendingStateSyncMessage(valAddress, requestHeight), err)
162+
m.logger.Error().Err(err).Str("proto_type", "GetMetaDataRequest").Msg("failed to send StateSyncMessage")
157163
}
158164
}
159165

consensus/doc/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.0.0.30] - 2023-02-17
11+
12+
- Updated log messages in the state sync submodule with consistent style and add height information
13+
- Added state sync message types to the types package
14+
1015
## [0.0.0.29] - 2023-02-17
1116

1217
- Modules embed `base_modules.IntegratableModule` and `base_modules.InterruptableModule` for DRYness

consensus/state_sync/helpers.go

+24-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@ import (
66
"google.golang.org/protobuf/types/known/anypb"
77
)
88

9-
func (m *stateSync) SendStateSyncMessage(stateSyncMsg *typesCons.StateSyncMessage, peerId cryptoPocket.Address, blockHeight uint64) error {
9+
func (m *stateSync) SendStateSyncMessage(stateSyncMsg *typesCons.StateSyncMessage, peerId cryptoPocket.Address, height uint64) error {
1010
anyMsg, err := anypb.New(stateSyncMsg)
1111
if err != nil {
1212
return err
1313
}
14-
m.logger.Info().Uint64("height", blockHeight).Msg(typesCons.SendingStateSyncMessage(peerId, blockHeight))
14+
15+
fields := map[string]any{
16+
"height": height,
17+
"peerId": peerId,
18+
"proto_type": getMessageType(stateSyncMsg),
19+
}
20+
21+
m.logger.Info().Fields(fields).Msg("Sending StateSync Message")
1522
return m.sendToPeer(anyMsg, peerId)
1623
}
1724

@@ -23,3 +30,18 @@ func (m *stateSync) sendToPeer(msg *anypb.Any, peerId cryptoPocket.Address) erro
2330
}
2431
return nil
2532
}
33+
34+
func getMessageType(msg *typesCons.StateSyncMessage) string {
35+
switch msg.Message.(type) {
36+
case *typesCons.StateSyncMessage_MetadataReq:
37+
return "MetadataRequest"
38+
case *typesCons.StateSyncMessage_MetadataRes:
39+
return "MetadataResponse"
40+
case *typesCons.StateSyncMessage_GetBlockReq:
41+
return "GetBlockRequest"
42+
case *typesCons.StateSyncMessage_GetBlockRes:
43+
return "GetBlockResponse"
44+
default:
45+
return "Unknown"
46+
}
47+
}

consensus/state_sync/module.go

+27-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type StateSyncModule interface {
3636
IsServerModEnabled() bool
3737
EnableServerMode()
3838

39-
SendStateSyncMessage(*typesCons.StateSyncMessage, cryptoPocket.Address, uint64) error
39+
SendStateSyncMessage(msg *typesCons.StateSyncMessage, nodeAddress cryptoPocket.Address, height uint64) error
4040
}
4141

4242
var (
@@ -71,6 +71,7 @@ func (*stateSync) Create(bus modules.Bus, options ...modules.ModuleOption) (modu
7171
bus.RegisterModule(m)
7272

7373
// when node is starting, it is in sync mode, as it might need to bootstrap to the latest state
74+
// TODO: change this to to reflect the state in the fsm once merged
7475
m.currentMode = Sync
7576
m.serverMode = false
7677

@@ -118,13 +119,36 @@ func (m *stateSync) EnableServerMode() {
118119
// TODO(#352): Implement this function
119120
// Placeholder function
120121
func (m *stateSync) HandleGetBlockResponse(blockRes *typesCons.GetBlockResponse) error {
121-
m.logger.Debug().Msgf("Received get block response: %s", blockRes.Block.String())
122+
consensusMod := m.GetBus().GetConsensusModule()
123+
serverNodePeerId := consensusMod.GetNodeAddress()
124+
clientPeerId := blockRes.PeerAddress
125+
126+
fields := map[string]any{
127+
"currentHeight": blockRes.Block.BlockHeader.Height,
128+
"sender": serverNodePeerId,
129+
"receiver": clientPeerId,
130+
}
131+
132+
m.logger.Info().Fields(fields).Msgf("Received StateSync GetBlockResponse: %s", blockRes)
133+
122134
return nil
123135
}
124136

125137
// TODO(#352): Implement the business to handle these correctly
126138
// Placeholder function
127139
func (m *stateSync) HandleStateSyncMetadataResponse(metaDataRes *typesCons.StateSyncMetadataResponse) error {
128-
m.logger.Debug().Msgf("Received get metadata response: %s", metaDataRes.String())
140+
consensusMod := m.GetBus().GetConsensusModule()
141+
serverNodePeerId := consensusMod.GetNodeAddress()
142+
clientPeerId := metaDataRes.PeerAddress
143+
currentHeight := consensusMod.CurrentHeight()
144+
145+
fields := map[string]any{
146+
"currentHeight": currentHeight,
147+
"sender": serverNodePeerId,
148+
"receiver": clientPeerId,
149+
}
150+
151+
m.logger.Info().Fields(fields).Msgf("Received StateSync MetadataResponse: %s", metaDataRes)
152+
129153
return nil
130154
}

consensus/state_sync/server.go

+24-11
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,20 @@ type StateSyncServerModule interface {
2222

2323
func (m *stateSync) HandleStateSyncMetadataRequest(metadataReq *typesCons.StateSyncMetadataRequest) error {
2424
consensusMod := m.GetBus().GetConsensusModule()
25-
serverNodePeerId := m.GetBus().GetConsensusModule().GetNodeAddress()
26-
25+
serverNodePeerAddress := consensusMod.GetNodeAddress()
2726
clientPeerAddress := metadataReq.PeerAddress
28-
m.logger.Info().Msgf("%s received state sync metadata request from: %s", serverNodePeerId, clientPeerAddress)
27+
// current height is the height of the block that is being processed, so we need to subtract 1 for the last finalized block
28+
lastPersistedBlockHeight := consensusMod.CurrentHeight() - 1
29+
30+
fields := map[string]any{
31+
"height": lastPersistedBlockHeight,
32+
"sender": serverNodePeerAddress,
33+
"receiver": clientPeerAddress,
34+
}
35+
36+
m.logger.Info().Fields(fields).Msgf("Received StateSync Metadata %s", metadataReq)
2937

30-
// last finalized block
31-
persistenceContext, err := m.GetBus().GetPersistenceModule().NewReadContext(int64(consensusMod.CurrentHeight()) - 1)
38+
persistenceContext, err := m.GetBus().GetPersistenceModule().NewReadContext(int64(lastPersistedBlockHeight))
3239
if err != nil {
3340
return nil
3441
}
@@ -47,7 +54,7 @@ func (m *stateSync) HandleStateSyncMetadataRequest(metadataReq *typesCons.StateS
4754
stateSyncMessage := typesCons.StateSyncMessage{
4855
Message: &typesCons.StateSyncMessage_MetadataRes{
4956
MetadataRes: &typesCons.StateSyncMetadataResponse{
50-
PeerAddress: serverNodePeerId,
57+
PeerAddress: serverNodePeerAddress,
5158
MinHeight: minHeight,
5259
MaxHeight: uint64(maxHeight),
5360
},
@@ -60,14 +67,20 @@ func (m *stateSync) HandleStateSyncMetadataRequest(metadataReq *typesCons.StateS
6067
func (m *stateSync) HandleGetBlockRequest(blockReq *typesCons.GetBlockRequest) error {
6168
consensusMod := m.GetBus().GetConsensusModule()
6269
serverNodePeerAddress := consensusMod.GetNodeAddress()
63-
6470
clientPeerAddress := blockReq.PeerAddress
65-
m.logger.Info().Msgf("%s received state sync Get Block Req from: %s", serverNodePeerAddress, clientPeerAddress)
71+
// current height is the height of the block that is being processed, so we need to subtract 1 for the last finalized block
72+
lastPersistedBlockHeight := consensusMod.CurrentHeight() - 1
73+
74+
fields := map[string]any{
75+
"height": lastPersistedBlockHeight,
76+
"sender": serverNodePeerAddress,
77+
"receiver": clientPeerAddress,
78+
}
6679

67-
currentHeight := m.GetBus().GetConsensusModule().CurrentHeight()
80+
m.logger.Info().Fields(fields).Msgf("Received StateSync GetBlockRequest: %s", blockReq)
6881

69-
if currentHeight < blockReq.Height {
70-
return fmt.Errorf("requested block height: %d is higher than node's block height: %d", blockReq.Height, consensusMod.CurrentHeight())
82+
if lastPersistedBlockHeight < blockReq.Height {
83+
return fmt.Errorf("requested block height: %d is higher than current persisted block height: %d", blockReq.Height, lastPersistedBlockHeight)
7184
}
7285

7386
// get block from the persistence module

consensus/types/errors.go consensus/types/messages.go

-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/pokt-network/pocket/logger"
1111
"github.com/pokt-network/pocket/shared/codec"
12-
cryptoPocket "github.com/pokt-network/pocket/shared/crypto"
1312
"google.golang.org/protobuf/proto"
1413
)
1514

@@ -83,10 +82,6 @@ func SendingMessage(msg *HotstuffMessage, nodeId NodeId) string {
8382
return fmt.Sprintf("✉️ Sending message ✉️ to %d at (height, step, round) (%d, %d, %d)", nodeId, msg.Height, msg.Step, msg.Round)
8483
}
8584

86-
func SendingStateSyncMessage(nodeId cryptoPocket.Address, height uint64) string {
87-
return fmt.Sprintf("🔄 Sending State sync message ✉️ to node %s at height: (%d) 🔄", nodeId, height)
88-
}
89-
9085
func BroadcastingMessage(msg *HotstuffMessage) string {
9186
return fmt.Sprintf("📣 Broadcasting message 📣 (height, step, round): (%d, %d, %d)", msg.GetHeight(), msg.GetStep(), msg.GetRound())
9287
}

0 commit comments

Comments
 (0)