From 94b1cc5c34651a0ee0b3944af966daff6298b4c6 Mon Sep 17 00:00:00 2001 From: Jenita Date: Mon, 29 Sep 2025 22:43:30 -0500 Subject: [PATCH] feat: added error handling for NewMsgRollForwardNtC Signed-off-by: Jenita --- protocol/chainsync/client_test.go | 16 +++++++---- protocol/chainsync/messages.go | 7 +++-- protocol/chainsync/messages_test.go | 42 +++++++++++++---------------- protocol/chainsync/server.go | 8 +++--- 4 files changed, 36 insertions(+), 37 deletions(-) diff --git a/protocol/chainsync/client_test.go b/protocol/chainsync/client_test.go index 575ef920..5805ca63 100644 --- a/protocol/chainsync/client_test.go +++ b/protocol/chainsync/client_test.go @@ -207,6 +207,16 @@ func TestGetAvailableBlockRange(t *testing.T) { testBlock.SlotNumber(), testBlock.Hash().Bytes(), ) + + rollForwardMsg, err := chainsync.NewMsgRollForwardNtC( + ledger.BlockTypeBabbage, + blockCbor, + expectedTip, + ) + if err != nil { + t.Fatalf("failed to create RollForward message: %s", err) + } + conversation := append( conversationHandshakeFindIntersect, ouroboros_mock.ConversationEntryOutput{ @@ -235,11 +245,7 @@ func TestGetAvailableBlockRange(t *testing.T) { ProtocolId: chainsync.ProtocolIdNtC, IsResponse: true, Messages: []protocol.Message{ - chainsync.NewMsgRollForwardNtC( - ledger.BlockTypeBabbage, - blockCbor, - expectedTip, - ), + rollForwardMsg, }, }, ) diff --git a/protocol/chainsync/messages.go b/protocol/chainsync/messages.go index 650d654d..708a8365 100644 --- a/protocol/chainsync/messages.go +++ b/protocol/chainsync/messages.go @@ -123,7 +123,7 @@ func NewMsgRollForwardNtC( blockType uint, blockCbor []byte, tip Tip, -) *MsgRollForwardNtC { +) (*MsgRollForwardNtC, error) { m := &MsgRollForwardNtC{ MessageBase: protocol.MessageBase{ MessageType: MessageTypeRollForward, @@ -135,12 +135,11 @@ func NewMsgRollForwardNtC( copy(m.blockCbor, blockCbor) wb := NewWrappedBlock(blockType, blockCbor) content, err := cbor.Encode(wb) - // TODO: figure out better way to handle error (#855) if err != nil { - return nil + return nil, fmt.Errorf("failed to encode wrapped block: %w", err) } m.WrappedBlock = cbor.Tag{Number: 24, Content: content} - return m + return m, nil } func (m *MsgRollForwardNtC) UnmarshalCBOR(data []byte) error { diff --git a/protocol/chainsync/messages_test.go b/protocol/chainsync/messages_test.go index c8cea138..680e0c32 100644 --- a/protocol/chainsync/messages_test.go +++ b/protocol/chainsync/messages_test.go @@ -212,6 +212,15 @@ func TestMsgRollForwardNodeToNode_CorruptedCBOR(t *testing.T) { } } func TestMsgRollForwardNodeToClient(t *testing.T) { + createMsg := func(t *testing.T, blockType uint, filePath string, tip Tip) *MsgRollForwardNtC { + blockData := hexDecode(string(readFile(filePath))) + msg, err := NewMsgRollForwardNtC(blockType, blockData, tip) + if err != nil { + t.Fatalf("failed to create NewMsgRollForwardNtC: %v", err) + } + return msg + } + tests := []testDefinition{ // Byron EBB (NtC) { @@ -220,15 +229,10 @@ func TestMsgRollForwardNodeToClient(t *testing.T) { "testdata/rollforward_ntc_byron_ebb_testnet_8f8602837f7c6f8b8867dd1cbc1842cf51a27eaed2c70ef48325d00f8efb320f.hex", ), ), - Message: NewMsgRollForwardNtC( + Message: createMsg( + t, 0, - hexDecode( - string( - readFile( - "testdata/byron_ebb_testnet_8f8602837f7c6f8b8867dd1cbc1842cf51a27eaed2c70ef48325d00f8efb320f.hex", - ), - ), - ), + "testdata/byron_ebb_testnet_8f8602837f7c6f8b8867dd1cbc1842cf51a27eaed2c70ef48325d00f8efb320f.hex", Tip{ Point: common.Point{ Slot: 49055, @@ -249,15 +253,10 @@ func TestMsgRollForwardNodeToClient(t *testing.T) { "testdata/rollforward_ntc_byron_main_block_testnet_f38aa5e8cf0b47d1ffa8b2385aa2d43882282db2ffd5ac0e3dadec1a6f2ecf08.hex", ), ), - Message: NewMsgRollForwardNtC( + Message: createMsg( + t, 1, - hexDecode( - string( - readFile( - "testdata/byron_main_block_testnet_f38aa5e8cf0b47d1ffa8b2385aa2d43882282db2ffd5ac0e3dadec1a6f2ecf08.hex", - ), - ), - ), + "testdata/byron_main_block_testnet_f38aa5e8cf0b47d1ffa8b2385aa2d43882282db2ffd5ac0e3dadec1a6f2ecf08.hex", Tip{ Point: common.Point{ Slot: 49055, @@ -278,15 +277,10 @@ func TestMsgRollForwardNodeToClient(t *testing.T) { "testdata/rollforward_ntc_shelley_block_testnet_02b1c561715da9e540411123a6135ee319b02f60b9a11a603d3305556c04329f.hex", ), ), - Message: NewMsgRollForwardNtC( + Message: createMsg( + t, 2, - hexDecode( - string( - readFile( - "testdata/shelley_block_testnet_02b1c561715da9e540411123a6135ee319b02f60b9a11a603d3305556c04329f.hex", - ), - ), - ), + "testdata/shelley_block_testnet_02b1c561715da9e540411123a6135ee319b02f60b9a11a603d3305556c04329f.hex", Tip{ Point: common.Point{ Slot: 55829927, diff --git a/protocol/chainsync/server.go b/protocol/chainsync/server.go index fb964bfc..f7ed2880 100644 --- a/protocol/chainsync/server.go +++ b/protocol/chainsync/server.go @@ -137,21 +137,21 @@ func (s *Server) RollForward(blockType uint, blockData []byte, tip Tip) error { } return s.SendMessage(msg) } else { - msg := NewMsgRollForwardNtC( + msg, err := NewMsgRollForwardNtC( blockType, blockData, tip, ) - if msg == nil { + if err != nil { s.Protocol.Logger(). Error( - "failed to create roll forward message", + fmt.Sprintf("failed to create roll forward message: %s", err), "component", "network", "protocol", ProtocolName, "role", "server", "connection_id", s.callbackContext.ConnectionId.String(), ) - return errors.New("failed to create roll forward message") + return fmt.Errorf("failed to create roll forward message: %w", err) } return s.SendMessage(msg) }