github.com/onflow/flow-go@v0.33.17/network/codec/cbor/decoder.go (about) 1 // (c) 2019 Dapper Labs - ALL RIGHTS RESERVED 2 3 package cbor 4 5 import ( 6 "github.com/fxamacker/cbor/v2" 7 8 "github.com/onflow/flow-go/network/codec" 9 _ "github.com/onflow/flow-go/utils/binstat" 10 ) 11 12 // Decoder implements a stream decoder for CBOR. 13 type Decoder struct { 14 dec *cbor.Decoder 15 } 16 17 // Decode will decode the next CBOR value from the stream. 18 // Expected error returns during normal operations: 19 // - codec.ErrInvalidEncoding if message encoding is invalid. 20 // - codec.ErrUnknownMsgCode if message code byte does not match any of the configured message codes. 21 // - codec.ErrMsgUnmarshal if the codec fails to unmarshal the data to the message type denoted by the message code. 22 func (d *Decoder) Decode() (interface{}, error) { 23 24 // read from stream and extract code 25 var data []byte 26 //bs1 := binstat.EnterTime(binstat.BinNet + ":strm>1(cbor)iowriter2payload2envelope") 27 err := d.dec.Decode(&data) 28 //binstat.LeaveVal(bs1, int64(len(data))) 29 if err != nil { 30 return nil, codec.NewInvalidEncodingErr(err) 31 } 32 33 msgCode, err := codec.MessageCodeFromPayload(data) 34 if err != nil { 35 return nil, err 36 } 37 38 msgInterface, what, err := codec.InterfaceFromMessageCode(msgCode) 39 if err != nil { 40 return nil, err 41 } 42 43 // unmarshal the payload 44 //bs2 := binstat.EnterTimeVal(fmt.Sprintf("%s%s%s:%d", binstat.BinNet, ":strm>2(cbor)", what, code), int64(len(data))) // e.g. ~3net:strm>2(cbor)CodeEntityRequest:23 45 err = defaultDecMode.Unmarshal(data[1:], msgInterface) // all but first byte 46 //binstat.Leave(bs2) 47 if err != nil { 48 return nil, codec.NewMsgUnmarshalErr(data[0], what, err) 49 } 50 51 return msgInterface, nil 52 }