Skip to content

Commit

Permalink
Merge pull request #385 from blinklabs-io/chore/update-fxamacker-cbor
Browse files Browse the repository at this point in the history
chore: update fxamacker/cbor to 2.5.0
  • Loading branch information
agaffney authored Sep 30, 2023
2 parents 51a51bf + ff82a8b commit 4ed605c
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 32 deletions.
31 changes: 10 additions & 21 deletions cbor/bytestring.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,23 @@ package cbor

import (
"encoding/hex"

_cbor "github.com/fxamacker/cbor/v2"
)

// Wrapper for bytestrings that allows them to be used as keys for a map
// We use a string because []byte isn't comparable, which means it can't be used as a map key
type ByteString string
// This was originally a full implementation, but now it just extends the upstream
// type
type ByteString struct {
_cbor.ByteString
}

func NewByteString(data []byte) ByteString {
bs := ByteString(data)
bs := ByteString{ByteString: _cbor.ByteString(data)}
return bs
}

func (bs *ByteString) UnmarshalCBOR(data []byte) error {
tmpValue := []byte{}
if _, err := Decode(data, &tmpValue); err != nil {
return err
}
*bs = ByteString(tmpValue)
return nil
}

func (bs ByteString) MarshalCBOR() ([]byte, error) {
return Encode([]byte(bs))
}

func (bs ByteString) Bytes() []byte {
return []byte(bs)
}

// String returns a hex-encoded representation of the bytestring
func (bs ByteString) String() string {
return hex.EncodeToString([]byte(bs))
return hex.EncodeToString(bs.Bytes())
}
3 changes: 2 additions & 1 deletion cbor/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"io"
"math/big"
"reflect"
"strings"
Expand All @@ -43,7 +44,7 @@ var testDefs = []struct {
{
cborHex: "81",
expectedObject: nil,
expectedDecodeError: fmt.Errorf("EOF"),
expectedDecodeError: io.ErrUnexpectedEOF,
},
// Invalid map key type
{
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/blinklabs-io/gouroboros
go 1.20

require (
github.com/fxamacker/cbor/v2 v2.4.0
github.com/fxamacker/cbor/v2 v2.5.0
github.com/jinzhu/copier v0.4.0
golang.org/x/crypto v0.13.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/fxamacker/cbor/v2 v2.4.0 h1:ri0ArlOR+5XunOP8CRUowT0pSJOwhW098ZCUyskZD88=
github.com/fxamacker/cbor/v2 v2.4.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo=
github.com/fxamacker/cbor/v2 v2.5.0 h1:oHsG0V/Q6E/wqTS2O1Cozzsy69nqCiguo5Q1a1ADivE=
github.com/fxamacker/cbor/v2 v2.5.0/go.mod h1:TA1xS00nchWmaBnEIxPSE5oHLuJBAVvqrtAnWBwBCVo=
github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8=
github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
Expand Down
4 changes: 2 additions & 2 deletions ledger/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (m MultiAsset[T]) MarshalJSON() ([]byte, error) {
for policyId, policyData := range m.data {
for assetName, amount := range policyData {
tmpObj := multiAssetJson[T]{
Name: string(assetName),
Name: string(assetName.Bytes()),
NameHex: hex.EncodeToString(assetName.Bytes()),
Amount: amount,
PolicyId: policyId.String(),
Expand Down Expand Up @@ -142,7 +142,7 @@ func (m *MultiAsset[T]) Asset(policyId Blake2b224, assetName []byte) T {
if !ok {
return 0
}
return policy[cbor.ByteString(assetName)]
return policy[cbor.NewByteString(assetName)]
}

type AssetFingerprint struct {
Expand Down
6 changes: 3 additions & 3 deletions ledger/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestMultiAssetJson(t *testing.T) {
multiAssetObj: MultiAsset[MultiAssetTypeOutput]{
data: map[Blake2b224]map[cbor.ByteString]MultiAssetTypeOutput{
NewBlake2b224(test.DecodeHexString("29a8fb8318718bd756124f0c144f56d4b4579dc5edf2dd42d669ac61")): {
cbor.ByteString(test.DecodeHexString("6675726e697368613239686e")): 123456,
cbor.NewByteString(test.DecodeHexString("6675726e697368613239686e")): 123456,
},
},
},
Expand All @@ -67,7 +67,7 @@ func TestMultiAssetJson(t *testing.T) {
multiAssetObj: MultiAsset[MultiAssetTypeOutput]{
data: map[Blake2b224]map[cbor.ByteString]MultiAssetTypeOutput{
NewBlake2b224(test.DecodeHexString("eaf8042c1d8203b1c585822f54ec32c4c1bb4d3914603e2cca20bbd5")): {
cbor.ByteString(test.DecodeHexString("426f7764757261436f6e63657074733638")): 234567,
cbor.NewByteString(test.DecodeHexString("426f7764757261436f6e63657074733638")): 234567,
},
},
},
Expand All @@ -77,7 +77,7 @@ func TestMultiAssetJson(t *testing.T) {
multiAssetObj: MultiAsset[MultiAssetTypeOutput]{
data: map[Blake2b224]map[cbor.ByteString]MultiAssetTypeOutput{
NewBlake2b224(test.DecodeHexString("cf78aeb9736e8aa94ce8fab44da86b522fa9b1c56336b92a28420525")): {
cbor.ByteString(test.DecodeHexString("363438346330393264363164373033656236333233346461")): 12345678,
cbor.NewByteString(test.DecodeHexString("363438346330393264363164373033656236333233346461")): 12345678,
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion ledger/mary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func createMaryTransactionOutputValueAssets(policyId []byte, assetName []byte, a
data := map[Blake2b224]map[cbor.ByteString]uint64{}
policyIdKey := Blake2b224{}
copy(policyIdKey[:], policyId)
assetKey := cbor.ByteString(assetName)
assetKey := cbor.NewByteString(assetName)
data[policyIdKey] = map[cbor.ByteString]uint64{
assetKey: amount,
}
Expand Down
2 changes: 1 addition & 1 deletion protocol/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func (p *Protocol) recvLoop() {
var tmpMsg []cbor.RawMessage
numBytesRead, err := cbor.Decode(p.recvBuffer.Bytes(), &tmpMsg)
if err != nil {
if err == io.EOF && p.recvBuffer.Len() > 0 {
if err == io.ErrUnexpectedEOF && p.recvBuffer.Len() > 0 {
// This is probably a multi-part message, so we wait until we get more of the message
// before trying to process it
p.recvReadyChan <- true
Expand Down

0 comments on commit 4ed605c

Please sign in to comment.