Skip to content

Commit d755cac

Browse files
authored
feat: added error handling for NewMsgRollForwardNtC (#1197)
Signed-off-by: Jenita <[email protected]>
1 parent 8ae44fe commit d755cac

File tree

4 files changed

+36
-37
lines changed

4 files changed

+36
-37
lines changed

protocol/chainsync/client_test.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ func TestGetAvailableBlockRange(t *testing.T) {
207207
testBlock.SlotNumber(),
208208
testBlock.Hash().Bytes(),
209209
)
210+
211+
rollForwardMsg, err := chainsync.NewMsgRollForwardNtC(
212+
ledger.BlockTypeBabbage,
213+
blockCbor,
214+
expectedTip,
215+
)
216+
if err != nil {
217+
t.Fatalf("failed to create RollForward message: %s", err)
218+
}
219+
210220
conversation := append(
211221
conversationHandshakeFindIntersect,
212222
ouroboros_mock.ConversationEntryOutput{
@@ -235,11 +245,7 @@ func TestGetAvailableBlockRange(t *testing.T) {
235245
ProtocolId: chainsync.ProtocolIdNtC,
236246
IsResponse: true,
237247
Messages: []protocol.Message{
238-
chainsync.NewMsgRollForwardNtC(
239-
ledger.BlockTypeBabbage,
240-
blockCbor,
241-
expectedTip,
242-
),
248+
rollForwardMsg,
243249
},
244250
},
245251
)

protocol/chainsync/messages.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func NewMsgRollForwardNtC(
123123
blockType uint,
124124
blockCbor []byte,
125125
tip Tip,
126-
) *MsgRollForwardNtC {
126+
) (*MsgRollForwardNtC, error) {
127127
m := &MsgRollForwardNtC{
128128
MessageBase: protocol.MessageBase{
129129
MessageType: MessageTypeRollForward,
@@ -135,12 +135,11 @@ func NewMsgRollForwardNtC(
135135
copy(m.blockCbor, blockCbor)
136136
wb := NewWrappedBlock(blockType, blockCbor)
137137
content, err := cbor.Encode(wb)
138-
// TODO: figure out better way to handle error (#855)
139138
if err != nil {
140-
return nil
139+
return nil, fmt.Errorf("failed to encode wrapped block: %w", err)
141140
}
142141
m.WrappedBlock = cbor.Tag{Number: 24, Content: content}
143-
return m
142+
return m, nil
144143
}
145144

146145
func (m *MsgRollForwardNtC) UnmarshalCBOR(data []byte) error {

protocol/chainsync/messages_test.go

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,15 @@ func TestMsgRollForwardNodeToNode_CorruptedCBOR(t *testing.T) {
212212
}
213213
}
214214
func TestMsgRollForwardNodeToClient(t *testing.T) {
215+
createMsg := func(t *testing.T, blockType uint, filePath string, tip Tip) *MsgRollForwardNtC {
216+
blockData := hexDecode(string(readFile(filePath)))
217+
msg, err := NewMsgRollForwardNtC(blockType, blockData, tip)
218+
if err != nil {
219+
t.Fatalf("failed to create NewMsgRollForwardNtC: %v", err)
220+
}
221+
return msg
222+
}
223+
215224
tests := []testDefinition{
216225
// Byron EBB (NtC)
217226
{
@@ -220,15 +229,10 @@ func TestMsgRollForwardNodeToClient(t *testing.T) {
220229
"testdata/rollforward_ntc_byron_ebb_testnet_8f8602837f7c6f8b8867dd1cbc1842cf51a27eaed2c70ef48325d00f8efb320f.hex",
221230
),
222231
),
223-
Message: NewMsgRollForwardNtC(
232+
Message: createMsg(
233+
t,
224234
0,
225-
hexDecode(
226-
string(
227-
readFile(
228-
"testdata/byron_ebb_testnet_8f8602837f7c6f8b8867dd1cbc1842cf51a27eaed2c70ef48325d00f8efb320f.hex",
229-
),
230-
),
231-
),
235+
"testdata/byron_ebb_testnet_8f8602837f7c6f8b8867dd1cbc1842cf51a27eaed2c70ef48325d00f8efb320f.hex",
232236
Tip{
233237
Point: common.Point{
234238
Slot: 49055,
@@ -249,15 +253,10 @@ func TestMsgRollForwardNodeToClient(t *testing.T) {
249253
"testdata/rollforward_ntc_byron_main_block_testnet_f38aa5e8cf0b47d1ffa8b2385aa2d43882282db2ffd5ac0e3dadec1a6f2ecf08.hex",
250254
),
251255
),
252-
Message: NewMsgRollForwardNtC(
256+
Message: createMsg(
257+
t,
253258
1,
254-
hexDecode(
255-
string(
256-
readFile(
257-
"testdata/byron_main_block_testnet_f38aa5e8cf0b47d1ffa8b2385aa2d43882282db2ffd5ac0e3dadec1a6f2ecf08.hex",
258-
),
259-
),
260-
),
259+
"testdata/byron_main_block_testnet_f38aa5e8cf0b47d1ffa8b2385aa2d43882282db2ffd5ac0e3dadec1a6f2ecf08.hex",
261260
Tip{
262261
Point: common.Point{
263262
Slot: 49055,
@@ -278,15 +277,10 @@ func TestMsgRollForwardNodeToClient(t *testing.T) {
278277
"testdata/rollforward_ntc_shelley_block_testnet_02b1c561715da9e540411123a6135ee319b02f60b9a11a603d3305556c04329f.hex",
279278
),
280279
),
281-
Message: NewMsgRollForwardNtC(
280+
Message: createMsg(
281+
t,
282282
2,
283-
hexDecode(
284-
string(
285-
readFile(
286-
"testdata/shelley_block_testnet_02b1c561715da9e540411123a6135ee319b02f60b9a11a603d3305556c04329f.hex",
287-
),
288-
),
289-
),
283+
"testdata/shelley_block_testnet_02b1c561715da9e540411123a6135ee319b02f60b9a11a603d3305556c04329f.hex",
290284
Tip{
291285
Point: common.Point{
292286
Slot: 55829927,

protocol/chainsync/server.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,21 @@ func (s *Server) RollForward(blockType uint, blockData []byte, tip Tip) error {
137137
}
138138
return s.SendMessage(msg)
139139
} else {
140-
msg := NewMsgRollForwardNtC(
140+
msg, err := NewMsgRollForwardNtC(
141141
blockType,
142142
blockData,
143143
tip,
144144
)
145-
if msg == nil {
145+
if err != nil {
146146
s.Protocol.Logger().
147147
Error(
148-
"failed to create roll forward message",
148+
fmt.Sprintf("failed to create roll forward message: %s", err),
149149
"component", "network",
150150
"protocol", ProtocolName,
151151
"role", "server",
152152
"connection_id", s.callbackContext.ConnectionId.String(),
153153
)
154-
return errors.New("failed to create roll forward message")
154+
return fmt.Errorf("failed to create roll forward message: %w", err)
155155
}
156156
return s.SendMessage(msg)
157157
}

0 commit comments

Comments
 (0)