github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/network/codec/cbor/decoder.go (about)

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