Skip to content

Commit 5cf1829

Browse files
authored
Merge pull request #74 from cloudstruct/feature/chain-sync-message-tests
More unit tests for chain-sync messages
2 parents b56559d + 5955ff9 commit 5cf1829

14 files changed

+488
-92
lines changed

cmd/go-ouroboros-network/chainsync.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,15 @@ func testChainSync(f *globalFlags) {
121121
syncState.oConn = o
122122
syncState.readyForNextBlockChan = make(chan bool)
123123
syncState.nodeToNode = f.ntnProto
124-
intersect := []interface{}{}
124+
var point chainsync.Point
125125
if len(eraIntersect[f.networkMagic][chainSyncFlags.startEra]) > 0 {
126126
// Slot
127-
intersect = append(intersect, eraIntersect[f.networkMagic][chainSyncFlags.startEra][0])
127+
point.Slot = uint64(eraIntersect[f.networkMagic][chainSyncFlags.startEra][0].(int))
128128
// Block hash
129129
hash, _ := hex.DecodeString(eraIntersect[f.networkMagic][chainSyncFlags.startEra][1].(string))
130-
intersect = append(intersect, hash)
130+
point.Hash = hash
131131
}
132-
if err := o.ChainSync.FindIntersect([]interface{}{intersect}); err != nil {
132+
if err := o.ChainSync.FindIntersect([]chainsync.Point{point}); err != nil {
133133
fmt.Printf("ERROR: FindIntersect: %s\n", err)
134134
os.Exit(1)
135135
}

protocol/chainsync/chainsync.go

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"fmt"
55
"github.com/cloudstruct/go-ouroboros-network/block"
66
"github.com/cloudstruct/go-ouroboros-network/protocol"
7-
"github.com/cloudstruct/go-ouroboros-network/utils"
87
)
98

109
const (
@@ -163,7 +162,7 @@ func (c *ChainSync) RequestNext() error {
163162
return c.SendMessage(msg)
164163
}
165164

166-
func (c *ChainSync) FindIntersect(points []interface{}) error {
165+
func (c *ChainSync) FindIntersect(points []Point) error {
167166
msg := NewMsgFindIntersect(points)
168167
return c.SendMessage(msg)
169168
}
@@ -184,16 +183,12 @@ func (c *ChainSync) handleRollForward(msgGeneric protocol.Message) error {
184183
msg := msgGeneric.(*MsgRollForwardNtN)
185184
var blockHeader interface{}
186185
var blockType uint
187-
blockHeaderType := msg.WrappedHeader.Type
188-
switch blockHeaderType {
186+
blockEra := msg.WrappedHeader.Era
187+
switch blockEra {
189188
case block.BLOCK_HEADER_TYPE_BYRON:
190-
var wrappedHeaderByron WrappedHeaderByron
191-
if _, err := utils.CborDecode(msg.WrappedHeader.RawData, &wrappedHeaderByron); err != nil {
192-
return fmt.Errorf("%s: decode error: %s", PROTOCOL_NAME, err)
193-
}
194-
blockType = wrappedHeaderByron.Unknown.Type
189+
blockType = msg.WrappedHeader.ByronType()
195190
var err error
196-
blockHeader, err = block.NewBlockHeaderFromCbor(blockType, wrappedHeaderByron.RawHeader)
191+
blockHeader, err = block.NewBlockHeaderFromCbor(blockType, msg.WrappedHeader.HeaderCbor())
197192
if err != nil {
198193
return err
199194
}
@@ -205,14 +200,9 @@ func (c *ChainSync) handleRollForward(msgGeneric protocol.Message) error {
205200
block.BLOCK_HEADER_TYPE_MARY: block.BLOCK_TYPE_MARY,
206201
block.BLOCK_HEADER_TYPE_ALONZO: block.BLOCK_TYPE_ALONZO,
207202
}
208-
blockType = blockTypeMap[blockHeaderType]
209-
// We decode into a byte array to implicitly unwrap the CBOR tag object
210-
var payload []byte
211-
if _, err := utils.CborDecode(msg.WrappedHeader.RawData, &payload); err != nil {
212-
return fmt.Errorf("%s: decode error: %s", PROTOCOL_NAME, err)
213-
}
203+
blockType = blockTypeMap[blockEra]
214204
var err error
215-
blockHeader, err = block.NewBlockHeaderFromCbor(blockType, payload)
205+
blockHeader, err = block.NewBlockHeaderFromCbor(blockType, msg.WrappedHeader.HeaderCbor())
216206
if err != nil {
217207
return err
218208
}
@@ -221,17 +211,12 @@ func (c *ChainSync) handleRollForward(msgGeneric protocol.Message) error {
221211
return c.callbackConfig.RollForwardFunc(blockType, blockHeader)
222212
} else {
223213
msg := msgGeneric.(*MsgRollForwardNtC)
224-
// Decode only enough to get the block type value
225-
var wrappedBlock WrappedBlock
226-
if _, err := utils.CborDecode(msg.WrappedData, &wrappedBlock); err != nil {
227-
return fmt.Errorf("%s: decode error: %s", PROTOCOL_NAME, err)
228-
}
229-
blk, err := block.NewBlockFromCbor(wrappedBlock.Type, wrappedBlock.RawBlock)
214+
blk, err := block.NewBlockFromCbor(msg.BlockType(), msg.BlockCbor())
230215
if err != nil {
231216
return err
232217
}
233218
// Call the user callback function
234-
return c.callbackConfig.RollForwardFunc(wrappedBlock.Type, blk)
219+
return c.callbackConfig.RollForwardFunc(msg.BlockType(), blk)
235220
}
236221
}
237222

protocol/chainsync/messages.go

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -88,35 +88,52 @@ func NewMsgAwaitReply() *MsgAwaitReply {
8888

8989
type MsgRollForwardNtC struct {
9090
protocol.MessageBase
91-
WrappedData []byte
92-
Tip Tip
91+
WrappedBlock cbor.Tag
92+
Tip Tip
93+
blockType uint
94+
blockCbor []byte
9395
}
9496

95-
func NewMsgRollForwardNtC(wrappedData []byte, tip Tip) *MsgRollForwardNtC {
97+
func NewMsgRollForwardNtC(blockType uint, blockCbor []byte, tip Tip) *MsgRollForwardNtC {
9698
m := &MsgRollForwardNtC{
9799
MessageBase: protocol.MessageBase{
98100
MessageType: MESSAGE_TYPE_ROLL_FORWARD,
99101
},
100-
WrappedData: wrappedData,
101-
Tip: tip,
102+
Tip: tip,
103+
}
104+
wb := NewWrappedBlock(blockType, blockCbor)
105+
content, err := cbor.Marshal(wb)
106+
// TODO: figure out better way to handle error
107+
if err != nil {
108+
return nil
102109
}
110+
m.WrappedBlock = cbor.Tag{Number: 24, Content: content}
103111
return m
104112
}
105113

114+
func (m *MsgRollForwardNtC) BlockType() uint {
115+
return m.blockType
116+
}
117+
118+
func (m *MsgRollForwardNtC) BlockCbor() []byte {
119+
return m.blockCbor
120+
}
121+
106122
type MsgRollForwardNtN struct {
107123
protocol.MessageBase
108124
WrappedHeader WrappedHeader
109125
Tip Tip
110126
}
111127

112-
func NewMsgRollForwardNtN(wrappedHeader WrappedHeader, tip Tip) *MsgRollForwardNtN {
128+
func NewMsgRollForwardNtN(era uint, byronType uint, blockCbor []byte, tip Tip) *MsgRollForwardNtN {
113129
m := &MsgRollForwardNtN{
114130
MessageBase: protocol.MessageBase{
115131
MessageType: MESSAGE_TYPE_ROLL_FORWARD,
116132
},
117-
WrappedHeader: wrappedHeader,
118-
Tip: tip,
133+
Tip: tip,
119134
}
135+
wrappedHeader := NewWrappedHeader(era, byronType, blockCbor)
136+
m.WrappedHeader = *wrappedHeader
120137
return m
121138
}
122139

@@ -139,10 +156,10 @@ func NewMsgRollBackward(point Point, tip Tip) *MsgRollBackward {
139156

140157
type MsgFindIntersect struct {
141158
protocol.MessageBase
142-
Points []interface{}
159+
Points []Point
143160
}
144161

145-
func NewMsgFindIntersect(points []interface{}) *MsgFindIntersect {
162+
func NewMsgFindIntersect(points []Point) *MsgFindIntersect {
146163
m := &MsgFindIntersect{
147164
MessageBase: protocol.MessageBase{
148165
MessageType: MESSAGE_TYPE_FIND_INTERSECT,
@@ -205,6 +222,8 @@ type Tip struct {
205222
}
206223

207224
type Point struct {
225+
// Tells the CBOR decoder to convert to/from a struct and a CBOR array
226+
_ struct{} `cbor:",toarray"`
208227
Slot uint64
209228
Hash []byte
210229
}
@@ -223,28 +242,13 @@ func (p *Point) UnmarshalCBOR(data []byte) error {
223242
return nil
224243
}
225244

226-
type WrappedBlock struct {
227-
// Tells the CBOR decoder to convert to/from a struct and a CBOR array
228-
_ struct{} `cbor:",toarray"`
229-
Type uint
230-
RawBlock cbor.RawMessage
231-
}
232-
233-
type WrappedHeader struct {
234-
// Tells the CBOR decoder to convert to/from a struct and a CBOR array
235-
_ struct{} `cbor:",toarray"`
236-
Type uint
237-
RawData cbor.RawMessage
238-
}
239-
240-
type WrappedHeaderByron struct {
241-
// Tells the CBOR decoder to convert to/from a struct and a CBOR array
242-
_ struct{} `cbor:",toarray"`
243-
Unknown struct {
244-
// Tells the CBOR decoder to convert to/from a struct and a CBOR array
245-
_ struct{} `cbor:",toarray"`
246-
Type uint
247-
Unknown uint64
248-
}
249-
RawHeader []byte
245+
func (p *Point) MarshalCBOR() ([]byte, error) {
246+
var data []interface{}
247+
if p.Slot == 0 && p.Hash == nil {
248+
// Return an empty list if values are zero
249+
data = make([]interface{}, 0)
250+
} else {
251+
data = []interface{}{p.Slot, p.Hash}
252+
}
253+
return utils.CborEncode(data)
250254
}

0 commit comments

Comments
 (0)