From 5695609f8be475d2c3dc7322fa241e3d5fe53831 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Mon, 13 Jan 2025 12:24:04 +0100 Subject: [PATCH] container: the writer should not ask for a CID, as its discarded --- go.mod | 6 +++--- go.sum | 12 ++++++------ pkg/container/serial_test.go | 10 +++++----- pkg/container/writer.go | 11 +++++------ pkg/container/writer_test.go | 18 ++++++++++++++++++ token/invocation/schema_test.go | 6 ------ 6 files changed, 37 insertions(+), 26 deletions(-) create mode 100644 pkg/container/writer_test.go diff --git a/go.mod b/go.mod index 2193ff4..c485298 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,8 @@ require ( github.com/multiformats/go-multicodec v0.9.0 github.com/multiformats/go-multihash v0.2.3 github.com/multiformats/go-varint v0.0.7 - github.com/stretchr/testify v1.9.0 + github.com/stretchr/testify v1.10.0 + golang.org/x/crypto v0.32.0 gotest.tools/v3 v3.5.1 ) @@ -34,8 +35,7 @@ require ( github.com/polydawn/refmt v0.89.0 // indirect github.com/segmentio/asm v1.2.0 // indirect github.com/spaolacci/murmur3 v1.1.0 // indirect - golang.org/x/crypto v0.25.0 // indirect - golang.org/x/sys v0.22.0 // indirect + golang.org/x/sys v0.29.0 // indirect google.golang.org/protobuf v1.34.2 // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 82932d5..6a7eb95 100644 --- a/go.sum +++ b/go.sum @@ -81,21 +81,21 @@ github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2 github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= -github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/urfave/cli v1.22.10/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= github.com/warpfork/go-wish v0.0.0-20220906213052-39a1cc7a02d0 h1:GDDkbFiaK8jsSDJfjId/PEGEShv6ugrt4kYsC5UIDaQ= github.com/warpfork/go-wish v0.0.0-20220906213052-39a1cc7a02d0/go.mod h1:x6AKhvSSexNrVSrViXSHUEbICjmGXhtgABaHIySUSGw= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30= -golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M= +golang.org/x/crypto v0.32.0 h1:euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc= +golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc= golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8= golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= -golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU= +golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= diff --git a/pkg/container/serial_test.go b/pkg/container/serial_test.go index 37ed3f7..b7eb14f 100644 --- a/pkg/container/serial_test.go +++ b/pkg/container/serial_test.go @@ -47,7 +47,7 @@ func TestContainerRoundTrip(t *testing.T) { for i := 0; i < 10; i++ { dlg, c, data := randToken() - writer.AddSealed(c, data) + writer.AddSealed(data) tokens[c] = dlg dataSize += len(data) } @@ -148,8 +148,8 @@ func BenchmarkContainerSerialisation(b *testing.B) { writer := NewWriter() for i := 0; i < 10; i++ { - _, c, data := randToken() - writer.AddSealed(c, data) + _, _, data := randToken() + writer.AddSealed(data) } buf := bytes.NewBuffer(nil) @@ -223,8 +223,8 @@ func FuzzContainerRead(f *testing.F) { for tokenCount := 0; tokenCount < 10; tokenCount++ { writer := NewWriter() for i := 0; i < tokenCount; i++ { - _, c, data := randToken() - writer.AddSealed(c, data) + _, _, data := randToken() + writer.AddSealed(data) } data, err := writer.ToBytes() require.NoError(f, err) diff --git a/pkg/container/writer.go b/pkg/container/writer.go index 6f366b8..1b5a885 100644 --- a/pkg/container/writer.go +++ b/pkg/container/writer.go @@ -4,7 +4,6 @@ import ( "bytes" "io" - "github.com/ipfs/go-cid" "github.com/ipld/go-ipld-prime" "github.com/ipld/go-ipld-prime/codec/cbor" "github.com/ipld/go-ipld-prime/datamodel" @@ -13,15 +12,15 @@ import ( ) // Writer is a token container writer. It provides a convenient way to aggregate and serialize tokens together. -type Writer map[cid.Cid][]byte +type Writer map[string]struct{} func NewWriter() Writer { return make(Writer) } // AddSealed includes a "sealed" token (serialized with a ToSealed* function) in the container. -func (ctn Writer) AddSealed(cid cid.Cid, data []byte) { - ctn[cid] = data +func (ctn Writer) AddSealed(data []byte) { + ctn[string(data)] = struct{}{} } // ToBytes encode the container into raw bytes. @@ -110,8 +109,8 @@ func (ctn Writer) toWriter(header header, w io.Writer) (err error) { }() node, err := qp.BuildMap(basicnode.Prototype.Any, 1, func(ma datamodel.MapAssembler) { qp.MapEntry(ma, containerVersionTag, qp.List(int64(len(ctn)), func(la datamodel.ListAssembler) { - for _, data := range ctn { - qp.ListEntry(la, qp.Bytes(data)) + for data, _ := range ctn { + qp.ListEntry(la, qp.Bytes([]byte(data))) } })) }) diff --git a/pkg/container/writer_test.go b/pkg/container/writer_test.go new file mode 100644 index 0000000..fcde1d5 --- /dev/null +++ b/pkg/container/writer_test.go @@ -0,0 +1,18 @@ +package container + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestWriterDedup(t *testing.T) { + ctn := NewWriter() + + _, _, sealed := randToken() + ctn.AddSealed(sealed) + require.Len(t, ctn, 1) + + ctn.AddSealed(sealed) + require.Len(t, ctn, 1) +} diff --git a/token/invocation/schema_test.go b/token/invocation/schema_test.go index f6cb5fb..581979f 100644 --- a/token/invocation/schema_test.go +++ b/token/invocation/schema_test.go @@ -2,7 +2,6 @@ package invocation_test import ( "bytes" - "fmt" "testing" "github.com/libp2p/go-libp2p/core/crypto" @@ -38,18 +37,13 @@ func TestSchemaRoundTrip(t *testing.T) { cborBytes, id, err := p1.ToSealed(privKey) require.NoError(t, err) assert.Equal(t, newCID, envelope.CIDToBase58BTC(id)) - fmt.Println("cborBytes length", len(cborBytes)) - fmt.Println("cbor", string(cborBytes)) p2, c2, err := invocation.FromSealed(cborBytes) require.NoError(t, err) assert.Equal(t, id, c2) - fmt.Println("read Cbor", p2) readJson, err := p2.ToDagJson(privKey) require.NoError(t, err) - fmt.Println("readJson length", len(readJson)) - fmt.Println("json: ", string(readJson)) assert.JSONEq(t, string(invocationJson), string(readJson)) })