Skip to content

Commit

Permalink
Merge pull request #98 from cloudstruct/feature/localtxsubmission-fai…
Browse files Browse the repository at this point in the history
…l-reason

fix: decode message more generically to help avoid known parsing problems
  • Loading branch information
agaffney authored Sep 8, 2022
2 parents 537bbcc + bd07714 commit fb76df7
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/cloudstruct/go-ouroboros-network/muxer"
"github.com/cloudstruct/go-ouroboros-network/utils"
"github.com/fxamacker/cbor/v2"
"io"
"sync"
)
Expand Down Expand Up @@ -242,9 +243,10 @@ func (p *Protocol) recvLoop() {
leftoverData = false
// Wait until ready to receive based on state map
<-p.recvReadyChan
// Decode message into generic list until we can determine what type of message it is
// This also lets us determine how many bytes the message is
var tmpMsg []interface{}
// Decode message into generic list until we can determine what type of message it is.
// This also lets us determine how many bytes the message is. We use RawMessage here to
// avoid parsing things that we may not be able to parse
var tmpMsg []cbor.RawMessage
numBytesRead, err := utils.CborDecode(p.recvBuffer.Bytes(), &tmpMsg)
if err != nil {
if err == io.EOF && p.recvBuffer.Len() > 0 {
Expand All @@ -256,8 +258,12 @@ func (p *Protocol) recvLoop() {
p.SendError(fmt.Errorf("%s: decode error: %s", p.config.Name, err))
return
}
// Decode first list item to determine message type
var msgType uint
if _, err := utils.CborDecode(tmpMsg[0], &msgType); err != nil {
p.SendError(fmt.Errorf("%s: decode error: %s", p.config.Name, err))
}
// Create Message object from CBOR
msgType := uint(tmpMsg[0].(uint64))
msgData := p.recvBuffer.Bytes()[:numBytesRead]
msg, err := p.config.MessageFromCborFunc(msgType, msgData)
if err != nil {
Expand Down

0 comments on commit fb76df7

Please sign in to comment.