diff --git a/.golangci.yaml b/.golangci.yaml index 6c365f3a20..e8c45f6c46 100644 --- a/.golangci.yaml +++ b/.golangci.yaml @@ -46,10 +46,10 @@ linters: desc: use github.com/NethermindEth/juno/utils for logging cbor: files: - - "!**/encoder/*.go" + - "!**/utils/cbor/*/*.go" deny: - pkg: github.com/fxamacker/cbor/v2 - desc: use github.com/NethermindEth/juno/encoder, do not import any other CBOR library directly + desc: use github.com/NethermindEth/juno/utils/cbor, do not import any other CBOR library directly funlen: lines: 120 statements: 50 diff --git a/blockchain/init_test.go b/blockchain/init_test.go index b0639842c0..914c238d7d 100644 --- a/blockchain/init_test.go +++ b/blockchain/init_test.go @@ -1,5 +1,5 @@ package blockchain_test import ( - _ "github.com/NethermindEth/juno/encoder/registry" + _ "github.com/NethermindEth/juno/utils/cbor/registry" ) diff --git a/cmd/juno/init_test.go b/cmd/juno/init_test.go index ea0e5b1416..8296b63c41 100644 --- a/cmd/juno/init_test.go +++ b/cmd/juno/init_test.go @@ -1,5 +1,5 @@ package main_test import ( - _ "github.com/NethermindEth/juno/encoder/registry" + _ "github.com/NethermindEth/juno/utils/cbor/registry" ) diff --git a/cmd/juno/juno.go b/cmd/juno/juno.go index 98727cc95f..cd3cfa0aab 100644 --- a/cmd/juno/juno.go +++ b/cmd/juno/juno.go @@ -16,11 +16,11 @@ import ( "time" "github.com/NethermindEth/juno/blockchain/networks" - _ "github.com/NethermindEth/juno/encoder/registry" _ "github.com/NethermindEth/juno/jemalloc" "github.com/NethermindEth/juno/l1/eth" "github.com/NethermindEth/juno/node" "github.com/NethermindEth/juno/utils" + _ "github.com/NethermindEth/juno/utils/cbor/registry" "github.com/NethermindEth/juno/utils/log" "github.com/NethermindEth/juno/vm" "github.com/mitchellh/mapstructure" diff --git a/consensus/p2p/validator/init_test.go b/consensus/p2p/validator/init_test.go index 462e8695ce..099ebcecd0 100644 --- a/consensus/p2p/validator/init_test.go +++ b/consensus/p2p/validator/init_test.go @@ -1,5 +1,5 @@ package validator import ( - _ "github.com/NethermindEth/juno/encoder/registry" + _ "github.com/NethermindEth/juno/utils/cbor/registry" ) diff --git a/consensus/proposer/init_test.go b/consensus/proposer/init_test.go index e52eda1d73..394b8cc20b 100644 --- a/consensus/proposer/init_test.go +++ b/consensus/proposer/init_test.go @@ -1,5 +1,5 @@ package proposer_test import ( - _ "github.com/NethermindEth/juno/encoder/registry" + _ "github.com/NethermindEth/juno/utils/cbor/registry" ) diff --git a/consensus/tendermint/init_test.go b/consensus/tendermint/init_test.go index d630e667db..40aa3d2573 100644 --- a/consensus/tendermint/init_test.go +++ b/consensus/tendermint/init_test.go @@ -1,5 +1,5 @@ package tendermint import ( - _ "github.com/NethermindEth/juno/encoder/registry" + _ "github.com/NethermindEth/juno/utils/cbor/registry" ) diff --git a/consensus/walstore/init_test.go b/consensus/walstore/init_test.go index e5049bfc61..57269ab1d5 100644 --- a/consensus/walstore/init_test.go +++ b/consensus/walstore/init_test.go @@ -1,5 +1,5 @@ package walstore_test import ( - _ "github.com/NethermindEth/juno/encoder/registry" + _ "github.com/NethermindEth/juno/utils/cbor/registry" ) diff --git a/core/accessors.go b/core/accessors.go index c5e5a59f71..23dd32eba0 100644 --- a/core/accessors.go +++ b/core/accessors.go @@ -7,7 +7,7 @@ import ( "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/db" - "github.com/NethermindEth/juno/encoder" + "github.com/NethermindEth/juno/utils/cbor/v1" "github.com/bits-and-blooms/bloom/v3" ) @@ -56,13 +56,13 @@ func GetClass(r db.KeyValueReader, classHash *felt.Felt) (*DeclaredClassDefiniti var class *DeclaredClassDefinition err := r.Get(db.ClassKey(classHash), func(data []byte) error { - return encoder.Unmarshal(data, &class) + return cbor.Unmarshal(data, &class) }) return class, err } func WriteClass(w db.KeyValueWriter, classHash *felt.Felt, class *DeclaredClassDefinition) error { - data, err := encoder.Marshal(class) + data, err := cbor.Marshal(class) if err != nil { return err } @@ -94,7 +94,7 @@ func DeleteContractDeploymentHeight(w db.KeyValueWriter, addr *felt.Felt) error func GetStateUpdateByBlockNum(r db.KeyValueReader, blockNum uint64) (*StateUpdate, error) { var stateUpdate *StateUpdate err := r.Get(db.StateUpdateByBlockNumKey(blockNum), func(data []byte) error { - return encoder.Unmarshal(data, &stateUpdate) + return cbor.Unmarshal(data, &stateUpdate) }) if err != nil { return nil, err @@ -103,7 +103,7 @@ func GetStateUpdateByBlockNum(r db.KeyValueReader, blockNum uint64) (*StateUpdat } func WriteStateUpdateByBlockNum(w db.KeyValueWriter, blockNum uint64, stateUpdate *StateUpdate) error { - data, err := encoder.Marshal(stateUpdate) + data, err := cbor.Marshal(stateUpdate) if err != nil { return err } @@ -206,13 +206,13 @@ func DeleteDeprecatedContractClassHashHistory( func GetL1Head(r db.KeyValueReader) (L1Head, error) { var l1Head L1Head err := r.Get(db.L1Height.Key(), func(data []byte) error { - return encoder.Unmarshal(data, &l1Head) + return cbor.Unmarshal(data, &l1Head) }) return l1Head, err } func WriteL1Head(w db.KeyValueWriter, l1Head *L1Head) error { - data, err := encoder.Marshal(l1Head) + data, err := cbor.Marshal(l1Head) if err != nil { return err } @@ -238,7 +238,7 @@ func DeleteBlockHeaderNumberByHash(w db.KeyValueWriter, hash *felt.Felt) error { } func WriteBlockHeaderByNumber(w db.KeyValueWriter, header *Header) error { - data, err := encoder.Marshal(header) + data, err := cbor.Marshal(header) if err != nil { return err } @@ -252,13 +252,13 @@ func DeleteBlockHeaderByNumber(w db.KeyValueWriter, number uint64) error { func GetBlockCommitmentByBlockNum(r db.KeyValueReader, blockNum uint64) (*BlockCommitments, error) { var commitment *BlockCommitments err := r.Get(db.BlockCommitmentsKey(blockNum), func(data []byte) error { - return encoder.Unmarshal(data, &commitment) + return cbor.Unmarshal(data, &commitment) }) return commitment, err } func WriteBlockCommitment(w db.KeyValueWriter, blockNum uint64, commitment *BlockCommitments) error { - data, err := encoder.Marshal(commitment) + data, err := cbor.Marshal(commitment) if err != nil { return err } @@ -322,7 +322,7 @@ func DeleteChainHeight(w db.KeyValueWriter) error { func GetBlockHeaderByNumber(r db.KeyValueReader, blockNum uint64) (*Header, error) { var header *Header err := r.Get(db.BlockHeaderByNumberKey(blockNum), func(data []byte) error { - return encoder.Unmarshal(data, &header) + return cbor.Unmarshal(data, &header) }) return header, err } @@ -332,7 +332,7 @@ func GetBlockHeaderByNumber(r db.KeyValueReader, blockNum uint64) (*Header, erro func GetGlobalStateRootByBlockNumber(r db.KeyValueReader, blockNum uint64) (*felt.Felt, error) { var header headerGlobalStateRootProjection err := r.Get(db.BlockHeaderByNumberKey(blockNum), func(data []byte) error { - return encoder.Unmarshal(data, &header) + return cbor.Unmarshal(data, &header) }) if err != nil { return nil, err @@ -348,7 +348,7 @@ func GetGlobalStateRootByBlockNumber(r db.KeyValueReader, blockNum uint64) (*fel func GetBlockHeaderHashByNumber(r db.KeyValueReader, blockNum uint64) (*felt.Felt, error) { var header headerHashProjection err := r.Get(db.BlockHeaderByNumberKey(blockNum), func(data []byte) error { - return encoder.Unmarshal(data, &header) + return cbor.Unmarshal(data, &header) }) if err != nil { return nil, err @@ -364,7 +364,7 @@ func GetBlockHeaderHashByNumber(r db.KeyValueReader, blockNum uint64) (*felt.Fel func GetBlockTransactionCountByNumber(r db.KeyValueReader, blockNum uint64) (uint64, error) { var header headerTransactionCountProjection err := r.Get(db.BlockHeaderByNumberKey(blockNum), func(data []byte) error { - return encoder.Unmarshal(data, &header) + return cbor.Unmarshal(data, &header) }) if err != nil { return 0, err @@ -375,7 +375,7 @@ func GetBlockTransactionCountByNumber(r db.KeyValueReader, blockNum uint64) (uin func GetBlockHeaderTimestampByNumber(r db.KeyValueReader, blockNum uint64) (uint64, error) { var header headerTimestampProjection err := r.Get(db.BlockHeaderByNumberKey(blockNum), func(data []byte) error { - return encoder.Unmarshal(data, &header) + return cbor.Unmarshal(data, &header) }) if err != nil { return 0, err @@ -392,7 +392,7 @@ func GetBlockHeaderEventsBloomByNumber( ) (*bloom.BloomFilter, error) { var header headerEventsBloomProjection err := r.Get(db.BlockHeaderByNumberKey(blockNum), func(data []byte) error { - return encoder.Unmarshal(data, &header) + return cbor.Unmarshal(data, &header) }) if err != nil { return nil, err @@ -409,7 +409,7 @@ func GetBlockHeaderHashAndStateRootByNumber( ) (hash, stateRoot *felt.Felt, err error) { var header headerHashAndStateRootProjection err = r.Get(db.BlockHeaderByNumberKey(blockNum), func(data []byte) error { - return encoder.Unmarshal(data, &header) + return cbor.Unmarshal(data, &header) }) if err != nil { return nil, nil, err @@ -646,7 +646,7 @@ func DeleteTransactionsAndReceipts( func GetAggregatedBloomFilter(r db.KeyValueReader, fromBlock, toBLock uint64) (AggregatedBloomFilter, error) { var filter AggregatedBloomFilter err := r.Get(db.AggregatedBloomFilterKey(fromBlock, toBLock), func(data []byte) error { - err := encoder.Unmarshal(data, &filter) + err := cbor.Unmarshal(data, &filter) return err }) if err != nil { @@ -657,7 +657,7 @@ func GetAggregatedBloomFilter(r db.KeyValueReader, fromBlock, toBLock uint64) (A } func WriteAggregatedBloomFilter(w db.KeyValueWriter, filter *AggregatedBloomFilter) error { - enc, err := encoder.Marshal(filter) + enc, err := cbor.Marshal(filter) if err != nil { return err } @@ -671,7 +671,7 @@ func DeleteAggregatedBloomFilter(w db.KeyValueWriter, fromBlock, toBlock uint64) func GetRunningEventFilter(r db.KeyValueReader) (*RunningEventFilter, error) { var filter RunningEventFilter err := r.Get(db.RunningEventFilter.Key(), func(data []byte) error { - err := encoder.Unmarshal(data, &filter) + err := cbor.Unmarshal(data, &filter) return err }) if err != nil { @@ -682,7 +682,7 @@ func GetRunningEventFilter(r db.KeyValueReader) (*RunningEventFilter, error) { } func WriteRunningEventFilter(w db.KeyValueWriter, filter *RunningEventFilter) error { - enc, err := encoder.Marshal(filter) + enc, err := cbor.Marshal(filter) if err != nil { return err } diff --git a/core/accessors_benchmark_test.go b/core/accessors_benchmark_test.go index 48d1f0487b..633fb23ab5 100644 --- a/core/accessors_benchmark_test.go +++ b/core/accessors_benchmark_test.go @@ -11,8 +11,8 @@ import ( "github.com/NethermindEth/juno/core" "github.com/NethermindEth/juno/db" "github.com/NethermindEth/juno/db/pebblev2" - _ "github.com/NethermindEth/juno/encoder/registry" "github.com/NethermindEth/juno/starknet" + _ "github.com/NethermindEth/juno/utils/cbor/registry" "github.com/stretchr/testify/require" ) diff --git a/core/accessors_test.go b/core/accessors_test.go index ed2ac46e4e..cc4f9b7dde 100644 --- a/core/accessors_test.go +++ b/core/accessors_test.go @@ -10,8 +10,8 @@ import ( "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/db" "github.com/NethermindEth/juno/db/memory" - "github.com/NethermindEth/juno/encoder" adaptfeeder "github.com/NethermindEth/juno/starknetdata/feeder" + "github.com/NethermindEth/juno/utils/cbor/v1" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -466,7 +466,7 @@ func TestPartialBlockHeaderAccessorsByNumber(t *testing.T) { t.Run("missing field returns error", func(t *testing.T) { t.Parallel() partialHeaderDB := memory.New() - data, err := encoder.Marshal(tt.headerWithoutField) + data, err := cbor.Marshal(tt.headerWithoutField) require.NoError(t, err) require.NoError(t, partialHeaderDB.Put(db.BlockHeaderByNumberKey(block.Number), data)) @@ -503,7 +503,7 @@ func TestGetBlockTransactionCountByNumber(t *testing.T) { t.Run("header without transaction count returns zero", func(t *testing.T) { t.Parallel() partialHeaderDB := memory.New() - data, err := encoder.Marshal(struct { + data, err := cbor.Marshal(struct { Hash *felt.Felt }{Hash: block.Hash}) require.NoError(t, err) @@ -537,7 +537,7 @@ func TestGetBlockHeaderTimestampByNumber(t *testing.T) { t.Run("missing field returns error", func(t *testing.T) { t.Parallel() partialHeaderDB := memory.New() - data, err := encoder.Marshal(struct{ Hash *felt.Felt }{Hash: block.Hash}) + data, err := cbor.Marshal(struct{ Hash *felt.Felt }{Hash: block.Hash}) require.NoError(t, err) require.NoError(t, partialHeaderDB.Put(db.BlockHeaderByNumberKey(block.Number), data)) @@ -568,7 +568,7 @@ func TestGetBlockHeaderEventsBloomByNumber(t *testing.T) { t.Run("missing field returns error", func(t *testing.T) { t.Parallel() partialHeaderDB := memory.New() - data, err := encoder.Marshal(struct{ Hash *felt.Felt }{Hash: block.Hash}) + data, err := cbor.Marshal(struct{ Hash *felt.Felt }{Hash: block.Hash}) require.NoError(t, err) require.NoError(t, partialHeaderDB.Put(db.BlockHeaderByNumberKey(block.Number), data)) @@ -599,7 +599,7 @@ func TestGetBlockHeaderHashAndStateRootByNumber(t *testing.T) { t.Run("missing hash returns error", func(t *testing.T) { t.Parallel() partialHeaderDB := memory.New() - data, err := encoder.Marshal( + data, err := cbor.Marshal( struct{ GlobalStateRoot *felt.Felt }{GlobalStateRoot: block.GlobalStateRoot}, ) require.NoError(t, err) @@ -612,7 +612,7 @@ func TestGetBlockHeaderHashAndStateRootByNumber(t *testing.T) { t.Run("missing state root returns error", func(t *testing.T) { t.Parallel() partialHeaderDB := memory.New() - data, err := encoder.Marshal(struct{ Hash *felt.Felt }{Hash: block.Hash}) + data, err := cbor.Marshal(struct{ Hash *felt.Felt }{Hash: block.Hash}) require.NoError(t, err) require.NoError(t, partialHeaderDB.Put(db.BlockHeaderByNumberKey(block.Number), data)) diff --git a/core/block_transaction_serializer.go b/core/block_transaction_serializer.go index 8b3961ab4d..d625c9e558 100644 --- a/core/block_transaction_serializer.go +++ b/core/block_transaction_serializer.go @@ -5,13 +5,13 @@ import ( "slices" "github.com/NethermindEth/juno/core/felt" - "github.com/NethermindEth/juno/encoder" + "github.com/NethermindEth/juno/utils/cbor/v1" ) type BlockTransactionsSerializer struct{} func (BlockTransactionsSerializer) Marshal(value *BlockTransactions) ([]byte, error) { - indexes, err := encoder.Marshal(value.Indexes) + indexes, err := cbor.Marshal(value.Indexes) if err != nil { return nil, err } @@ -165,7 +165,7 @@ func (blockTransactionsPartialSerializer[E, S, T]) UnmarshalPartial( value *T, ) error { var blockTransactions BlockTransactions - remaining, err := encoder.UnmarshalFirst(data, &blockTransactions.Indexes) + remaining, err := cbor.UnmarshalFirst(data, &blockTransactions.Indexes) if err != nil { return err } diff --git a/core/block_transaction_test.go b/core/block_transaction_test.go index 88e5c68785..b426eeba4e 100644 --- a/core/block_transaction_test.go +++ b/core/block_transaction_test.go @@ -8,17 +8,17 @@ import ( "github.com/NethermindEth/juno/core" "github.com/NethermindEth/juno/core/indexed" "github.com/NethermindEth/juno/db/typed/partial" - "github.com/NethermindEth/juno/encoder" - _ "github.com/NethermindEth/juno/encoder/registry" + _ "github.com/NethermindEth/juno/utils/cbor/registry" + "github.com/NethermindEth/juno/utils/cbor/v1" "github.com/stretchr/testify/require" ) const transactionCount = 100 -func toCborSeq[T any](items []T) iter.Seq2[encoder.RawMessage, error] { - return func(yield func(encoder.RawMessage, error) bool) { +func toCborSeq[T any](items []T) iter.Seq2[cbor.RawMessage, error] { + return func(yield func(cbor.RawMessage, error) bool) { for _, item := range items { - cbor, err := encoder.Marshal(item) + cbor, err := cbor.Marshal(item) if err != nil { yield(nil, err) return diff --git a/core/block_transaction_txhash_test.go b/core/block_transaction_txhash_test.go index fff949bb96..283c3b9d4a 100644 --- a/core/block_transaction_txhash_test.go +++ b/core/block_transaction_txhash_test.go @@ -5,7 +5,7 @@ import ( "github.com/NethermindEth/juno/core" "github.com/NethermindEth/juno/core/felt" - _ "github.com/NethermindEth/juno/encoder/registry" // register transaction CBOR tags + _ "github.com/NethermindEth/juno/utils/cbor/registry" // register transaction CBOR tags "github.com/stretchr/testify/require" ) diff --git a/core/class.go b/core/class.go index 617565f6cc..4e0ec2f38e 100644 --- a/core/class.go +++ b/core/class.go @@ -12,7 +12,7 @@ import ( "github.com/NethermindEth/juno/core/crypto/blake2s" "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/db" - "github.com/NethermindEth/juno/encoder" + "github.com/NethermindEth/juno/utils/cbor/v1" ) var ( @@ -338,7 +338,7 @@ type DeclaredClassDefinition struct { } func (d *DeclaredClassDefinition) MarshalBinary() ([]byte, error) { - classEnc, err := encoder.Marshal(d.Class) + classEnc, err := cbor.Marshal(d.Class) if err != nil { return nil, err } @@ -357,7 +357,7 @@ func (d *DeclaredClassDefinition) UnmarshalBinary(data []byte) error { } d.At = binary.BigEndian.Uint64(data[:8]) - return encoder.Unmarshal(data[8:], &d.Class) + return cbor.Unmarshal(data[8:], &d.Class) } // ClassCasmHashMetadata tracks the CASM (Compiled Sierra) hash metadata for a class. diff --git a/core/class_test.go b/core/class_test.go index aa70e52a92..e0453560dc 100644 --- a/core/class_test.go +++ b/core/class_test.go @@ -13,8 +13,8 @@ import ( "github.com/NethermindEth/juno/core" "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/db" - "github.com/NethermindEth/juno/encoder" adaptfeeder "github.com/NethermindEth/juno/starknetdata/feeder" + "github.com/NethermindEth/juno/utils/cbor/v1" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -289,11 +289,11 @@ func TestClassEncoding(t *testing.T) { func checkClassSymmetry(t *testing.T, input core.ClassDefinition) { t.Helper() - data, err := encoder.Marshal(input) + data, err := cbor.Marshal(input) require.NoError(t, err) var class core.ClassDefinition - require.NoError(t, encoder.Unmarshal(data, &class)) + require.NoError(t, cbor.Unmarshal(data, &class)) switch v := class.(type) { case *core.DeprecatedCairoClass: diff --git a/core/deprecatedstate/history_test.go b/core/deprecatedstate/history_test.go index 515b392075..17bdbc4b3e 100644 --- a/core/deprecatedstate/history_test.go +++ b/core/deprecatedstate/history_test.go @@ -9,7 +9,7 @@ import ( "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/db" "github.com/NethermindEth/juno/db/memory" - _ "github.com/NethermindEth/juno/encoder/registry" + _ "github.com/NethermindEth/juno/utils/cbor/registry" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/core/felt/cbor.go b/core/felt/cbor.go index 9463876a3c..4c3942654e 100644 --- a/core/felt/cbor.go +++ b/core/felt/cbor.go @@ -4,7 +4,7 @@ import ( "encoding/binary" "math" - "github.com/NethermindEth/juno/encoder" + "github.com/NethermindEth/juno/utils/cbor/v1" "github.com/consensys/gnark-crypto/ecc/stark-curve/fp" ) @@ -21,7 +21,7 @@ func (z *Felt) UnmarshalCBOR(data []byte) error { if decodeFelt(data, z) { return nil } - return encoder.Unmarshal(data, (*fp.Element)(z)) + return cbor.Unmarshal(data, (*fp.Element)(z)) } const ( diff --git a/core/felt/cbor_fastpath_test.go b/core/felt/cbor_fastpath_test.go index 751c471d0a..c0fb599433 100644 --- a/core/felt/cbor_fastpath_test.go +++ b/core/felt/cbor_fastpath_test.go @@ -10,7 +10,7 @@ import ( "testing" "github.com/NethermindEth/juno/core/felt" - "github.com/NethermindEth/juno/encoder" + "github.com/NethermindEth/juno/utils/cbor/v1" "github.com/consensys/gnark-crypto/ecc/stark-curve/fp" "github.com/stretchr/testify/require" ) @@ -50,13 +50,13 @@ func encodeBoth(value *felt.Felt) (fast, generic []byte, err error) { if err != nil { return nil, nil, err } - generic, err = encoder.Marshal((*fp.Element)(value)) + generic, err = cbor.Marshal((*fp.Element)(value)) return fast, generic, err } func decodeBoth(data []byte) (fast, generic felt.Felt, errFast, errGeneric error) { errFast = fast.UnmarshalCBOR(data) - errGeneric = encoder.Unmarshal(data, (*fp.Element)(&generic)) + errGeneric = cbor.Unmarshal(data, (*fp.Element)(&generic)) return fast, generic, errFast, errGeneric } diff --git a/core/felt/felt_test.go b/core/felt/felt_test.go index 8407b81151..2cafa9d508 100644 --- a/core/felt/felt_test.go +++ b/core/felt/felt_test.go @@ -7,7 +7,7 @@ import ( "testing" "github.com/NethermindEth/juno/core/felt" - "github.com/NethermindEth/juno/encoder" + "github.com/NethermindEth/juno/utils/cbor/v1" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -15,11 +15,11 @@ import ( func TestFeltCbor(t *testing.T) { val := felt.NewRandom[felt.Felt]() - encoded, err := encoder.Marshal(val) + encoded, err := cbor.Marshal(val) require.NoError(t, err) var decoded felt.Felt - require.NoError(t, encoder.Unmarshal(encoded, &decoded)) + require.NoError(t, cbor.Unmarshal(encoded, &decoded)) assert.Equal(t, val, &decoded) } diff --git a/core/felt/slice.go b/core/felt/slice.go index 9513bd1665..52ca1766cf 100644 --- a/core/felt/slice.go +++ b/core/felt/slice.go @@ -10,7 +10,7 @@ import ( "math" "slices" - "github.com/NethermindEth/juno/encoder" + "github.com/NethermindEth/juno/utils/cbor/v1" ) type Slice[F FeltLike] []F @@ -18,8 +18,8 @@ type Slice[F FeltLike] []F var ( _ json.MarshalerTo = Slice[Felt]{} _ json.UnmarshalerFrom = (*Slice[Felt])(nil) - _ encoder.SelfEncoder = Slice[Felt]{} - _ encoder.SelfDecoder = (*Slice[Felt])(nil) + _ cbor.SelfEncoder = Slice[Felt]{} + _ cbor.SelfDecoder = (*Slice[Felt])(nil) ) const ( @@ -80,7 +80,7 @@ func (s *Slice[F]) UnmarshalCBOR(data []byte) error { // unmarshalGeneric handles any shape the fast path does not recognise. func (s *Slice[F]) unmarshalGeneric(data []byte) error { var buffer []F - if err := encoder.Unmarshal(data, &buffer); err != nil { + if err := cbor.Unmarshal(data, &buffer); err != nil { return err } diff --git a/core/felt/slice_bench_test.go b/core/felt/slice_bench_test.go index d54d6bbe77..b696232c7c 100644 --- a/core/felt/slice_bench_test.go +++ b/core/felt/slice_bench_test.go @@ -6,14 +6,14 @@ import ( "testing" "github.com/NethermindEth/juno/core/felt" - "github.com/NethermindEth/juno/encoder" + "github.com/NethermindEth/juno/utils/cbor/v1" ) func BenchmarkSliceVsFeltArrayCBOR(b *testing.B) { for _, n := range []int{1000, 5000} { slice := randomSlice[feltoid](n) feltArray := []feltoid(slice) - encoded, err := encoder.Marshal(slice) + encoded, err := cbor.Marshal(slice) if err != nil { b.Fatal(err) } @@ -21,27 +21,27 @@ func BenchmarkSliceVsFeltArrayCBOR(b *testing.B) { b.Run(fmt.Sprintf("marshal/Slice/n=%d", n), func(b *testing.B) { b.ReportAllocs() for b.Loop() { - _, _ = encoder.Marshal(slice) + _, _ = cbor.Marshal(slice) } }) b.Run(fmt.Sprintf("marshal/FeltArray/n=%d", n), func(b *testing.B) { b.ReportAllocs() for b.Loop() { - _, _ = encoder.Marshal(feltArray) + _, _ = cbor.Marshal(feltArray) } }) b.Run(fmt.Sprintf("unmarshal/Slice/n=%d", n), func(b *testing.B) { b.ReportAllocs() for b.Loop() { var out felt.Slice[feltoid] - _ = encoder.Unmarshal(encoded, &out) + _ = cbor.Unmarshal(encoded, &out) } }) b.Run(fmt.Sprintf("unmarshal/FeltArray/n=%d", n), func(b *testing.B) { b.ReportAllocs() for b.Loop() { var out []felt.Felt - _ = encoder.Unmarshal(encoded, &out) + _ = cbor.Unmarshal(encoded, &out) } }) } diff --git a/core/felt/slice_test.go b/core/felt/slice_test.go index 8217f68104..03288825a3 100644 --- a/core/felt/slice_test.go +++ b/core/felt/slice_test.go @@ -12,7 +12,7 @@ import ( "testing" "github.com/NethermindEth/juno/core/felt" - "github.com/NethermindEth/juno/encoder" + "github.com/NethermindEth/juno/utils/cbor/v1" "github.com/stretchr/testify/require" ) @@ -55,7 +55,7 @@ func requireSliceDecodeCBOREquivalent[F felt.FeltLike](t *testing.T, data []byte errFast := fast.UnmarshalCBOR(data) var generic []F - errGeneric := encoder.Unmarshal(data, &generic) + errGeneric := cbor.Unmarshal(data, &generic) if errGeneric != nil { require.Equal( @@ -109,7 +109,7 @@ func TestSliceRoundTripCBORBoundarySizes(t *testing.T) { fast, err := s.MarshalCBOR() require.NoError(t, err) - generic, err := encoder.Marshal([]felt.Felt(s)) + generic, err := cbor.Marshal([]felt.Felt(s)) require.NoError(t, err) require.Equal( t, @@ -132,7 +132,7 @@ func TestSliceMarshalCBORNil(t *testing.T) { fast, err := s.MarshalCBOR() require.NoError(t, err) - generic, err := encoder.Marshal([]felt.Felt(s)) + generic, err := cbor.Marshal([]felt.Felt(s)) require.NoError(t, err) require.Equal(t, generic, fast, "nil slice must marshal like the generic encoder") diff --git a/core/indexed/indexed.go b/core/indexed/indexed.go index 126c446858..61a5366f49 100644 --- a/core/indexed/indexed.go +++ b/core/indexed/indexed.go @@ -4,19 +4,19 @@ import ( "bytes" "iter" - "github.com/NethermindEth/juno/encoder" + "github.com/NethermindEth/juno/utils/cbor/v1" ) type BufferedEncoder struct { *bytes.Buffer - encoder.Encoder + cbor.Encoder } func NewBufferedEncoder() BufferedEncoder { var buf bytes.Buffer return BufferedEncoder{ Buffer: &buf, - Encoder: encoder.NewEncoder(&buf), + Encoder: cbor.NewEncoder(&buf), } } diff --git a/core/indexed/lazy_slice.go b/core/indexed/lazy_slice.go index 00df2bd0c1..bc4591e303 100644 --- a/core/indexed/lazy_slice.go +++ b/core/indexed/lazy_slice.go @@ -5,7 +5,7 @@ import ( "iter" "github.com/NethermindEth/juno/db" - "github.com/NethermindEth/juno/encoder" + "github.com/NethermindEth/juno/utils/cbor/v1" ) // Items are stored contiguously in a data slice, with an indexes slice marking each @@ -35,7 +35,7 @@ func (l LazySlice[T]) getInto(index int, value *T) error { if index < len(l.indexes)-1 { end = l.indexes[index+1] } - return encoder.Unmarshal(l.data[start:end], value) + return cbor.Unmarshal(l.data[start:end], value) } func (l LazySlice[T]) Get(index int) (T, error) { diff --git a/core/indexed/lazy_slice_bench_test.go b/core/indexed/lazy_slice_bench_test.go index efef97afbe..81a53b7bfb 100644 --- a/core/indexed/lazy_slice_bench_test.go +++ b/core/indexed/lazy_slice_bench_test.go @@ -4,7 +4,7 @@ import ( "testing" "github.com/NethermindEth/juno/core/indexed" - "github.com/NethermindEth/juno/encoder" + "github.com/NethermindEth/juno/utils/cbor/v1" ) type benchItem struct { @@ -20,7 +20,7 @@ func newBenchLazySlice(tb testing.TB) indexed.LazySlice[benchItem] { indexes := make([]int, 0, benchItemsCount) var data []byte for i := range benchItemsCount { - encoded, err := encoder.Marshal(benchItem{ + encoded, err := cbor.Marshal(benchItem{ Hash: [4]uint64{uint64(i), uint64(i + 1), uint64(i + 2), uint64(i + 3)}, Nonce: uint64(i), Extra: uint64(i * 2), diff --git a/core/indexed/lazy_slice_test.go b/core/indexed/lazy_slice_test.go index 9c2c63b742..bfd68c5b18 100644 --- a/core/indexed/lazy_slice_test.go +++ b/core/indexed/lazy_slice_test.go @@ -5,7 +5,7 @@ import ( "testing" "github.com/NethermindEth/juno/core/indexed" - "github.com/NethermindEth/juno/encoder" + "github.com/NethermindEth/juno/utils/cbor/v1" "github.com/stretchr/testify/require" ) @@ -34,7 +34,7 @@ func rawLazySlice[T any](t *testing.T, items ...any) indexed.LazySlice[T] { indexes := make([]int, len(items)) var data []byte for i, item := range items { - encoded, err := encoder.Marshal(item) + encoded, err := cbor.Marshal(item) require.NoError(t, err) indexes[i] = len(data) data = append(data, encoded...) diff --git a/core/init_test.go b/core/init_test.go index ccc2768178..7a1d5d7839 100644 --- a/core/init_test.go +++ b/core/init_test.go @@ -1,5 +1,5 @@ package core_test import ( - _ "github.com/NethermindEth/juno/encoder/registry" + _ "github.com/NethermindEth/juno/utils/cbor/registry" ) diff --git a/core/partial_cbor.go b/core/partial_cbor.go index ffea9bee19..bf0543633e 100644 --- a/core/partial_cbor.go +++ b/core/partial_cbor.go @@ -4,7 +4,7 @@ import ( "errors" "github.com/NethermindEth/juno/core/felt" - "github.com/NethermindEth/juno/encoder" + "github.com/NethermindEth/juno/utils/cbor/v1" bloom "github.com/bits-and-blooms/bloom/v3" ) @@ -23,8 +23,8 @@ var errDiscardedCBORMarshal = errors.New( ) var ( - _ encoder.SelfEncoder = discardedCBOR{} - _ encoder.SelfDecoder = discardedCBOR{} + _ cbor.SelfEncoder = discardedCBOR{} + _ cbor.SelfDecoder = discardedCBOR{} ) func (discardedCBOR) UnmarshalCBOR([]byte) error { return nil } diff --git a/core/partial_cbor_test.go b/core/partial_cbor_test.go index 4d74ebef34..3208af1597 100644 --- a/core/partial_cbor_test.go +++ b/core/partial_cbor_test.go @@ -7,7 +7,7 @@ import ( "testing" "github.com/NethermindEth/juno/core/felt" - "github.com/NethermindEth/juno/encoder" + "github.com/NethermindEth/juno/utils/cbor/v1" bloom "github.com/bits-and-blooms/bloom/v3" "github.com/stretchr/testify/require" ) @@ -68,7 +68,7 @@ func assertProjectionsCoverSource(t *testing.T, source reflect.Type, projections func assertProjectionsCoverEveryWireKey(t *testing.T, data []byte, projections ...any) { t.Helper() for _, projection := range projections { - require.NoErrorf(t, encoder.UnmarshalStrict(data, projection), + require.NoErrorf(t, cbor.UnmarshalStrict(data, projection), "%T leaves a wire key unmatched (field added, or a tag name/option changed)", projection) } } @@ -161,13 +161,13 @@ func TestStrictGuardCatchesKeyAsIntDrift(t *testing.T) { "cborKeys cannot see the keyasint option — this is its documented blind spot") // Ground truth: encode with the integer key, then strict-decode. - data, err := encoder.Marshal(&driftSourceKeyAsInt{A: new(felt.Felt).SetUint64(7)}) + data, err := cbor.Marshal(&driftSourceKeyAsInt{A: new(felt.Felt).SetUint64(7)}) require.NoError(t, err) // The string-key projection does not match the integer wire key → strict decode errors. - require.Error(t, encoder.UnmarshalStrict(data, &driftProjectionStringKey{}), + require.Error(t, cbor.UnmarshalStrict(data, &driftProjectionStringKey{}), "strict decode must reject an integer wire key that the projection expects as a string") // The matching keyasint projection decodes cleanly. - require.NoError(t, encoder.UnmarshalStrict(data, &driftProjectionIntKey{}), + require.NoError(t, cbor.UnmarshalStrict(data, &driftProjectionIntKey{}), "a projection whose keyasint matches the source must decode without error") } @@ -182,31 +182,31 @@ func headerProjectionCases() []headerProjectionCase { return []headerProjectionCase{ { "hash", - func(d []byte) { var h struct{ Hash *felt.Felt }; _ = encoder.Unmarshal(d, &h) }, - func(d []byte) { var h headerHashProjection; _ = encoder.Unmarshal(d, &h) }, + func(d []byte) { var h struct{ Hash *felt.Felt }; _ = cbor.Unmarshal(d, &h) }, + func(d []byte) { var h headerHashProjection; _ = cbor.Unmarshal(d, &h) }, }, { "global_state_root", - func(d []byte) { var h struct{ GlobalStateRoot *felt.Felt }; _ = encoder.Unmarshal(d, &h) }, - func(d []byte) { var h headerGlobalStateRootProjection; _ = encoder.Unmarshal(d, &h) }, + func(d []byte) { var h struct{ GlobalStateRoot *felt.Felt }; _ = cbor.Unmarshal(d, &h) }, + func(d []byte) { var h headerGlobalStateRootProjection; _ = cbor.Unmarshal(d, &h) }, }, { "transaction_count", - func(d []byte) { var h struct{ TransactionCount uint64 }; _ = encoder.Unmarshal(d, &h) }, - func(d []byte) { var h headerTransactionCountProjection; _ = encoder.Unmarshal(d, &h) }, + func(d []byte) { var h struct{ TransactionCount uint64 }; _ = cbor.Unmarshal(d, &h) }, + func(d []byte) { var h headerTransactionCountProjection; _ = cbor.Unmarshal(d, &h) }, }, { "timestamp", - func(d []byte) { var h struct{ Timestamp *uint64 }; _ = encoder.Unmarshal(d, &h) }, - func(d []byte) { var h headerTimestampProjection; _ = encoder.Unmarshal(d, &h) }, + func(d []byte) { var h struct{ Timestamp *uint64 }; _ = cbor.Unmarshal(d, &h) }, + func(d []byte) { var h headerTimestampProjection; _ = cbor.Unmarshal(d, &h) }, }, { "events_bloom", func(d []byte) { var h struct{ EventsBloom *bloom.BloomFilter } - _ = encoder.Unmarshal(d, &h) + _ = cbor.Unmarshal(d, &h) }, - func(d []byte) { var h headerEventsBloomProjection; _ = encoder.Unmarshal(d, &h) }, + func(d []byte) { var h headerEventsBloomProjection; _ = cbor.Unmarshal(d, &h) }, }, { "hash_and_state_root", @@ -215,9 +215,9 @@ func headerProjectionCases() []headerProjectionCase { Hash *felt.Felt GlobalStateRoot *felt.Felt } - _ = encoder.Unmarshal(d, &h) + _ = cbor.Unmarshal(d, &h) }, - func(d []byte) { var h headerHashAndStateRootProjection; _ = encoder.Unmarshal(d, &h) }, + func(d []byte) { var h headerHashAndStateRootProjection; _ = cbor.Unmarshal(d, &h) }, }, } } @@ -273,7 +273,7 @@ func sampleHeader() *Header { // appears here automatically. func sampleHeaderBytes(tb testing.TB) []byte { tb.Helper() - data, err := encoder.Marshal(sampleHeader()) + data, err := cbor.Marshal(sampleHeader()) require.NoError(tb, err) return data } @@ -287,30 +287,30 @@ func TestProjectionsDecodeShadowedField(t *testing.T) { const shadowMsg = "shadowing field must receive the wire value, not discardedCBOR" var hash headerHashProjection - require.NoError(t, encoder.Unmarshal(data, &hash)) + require.NoError(t, cbor.Unmarshal(data, &hash)) require.Equal(t, header.Hash, hash.Hash, shadowMsg) var stateRoot headerGlobalStateRootProjection - require.NoError(t, encoder.Unmarshal(data, &stateRoot)) + require.NoError(t, cbor.Unmarshal(data, &stateRoot)) require.Equal(t, header.GlobalStateRoot, stateRoot.GlobalStateRoot, shadowMsg) var txCount headerTransactionCountProjection - require.NoError(t, encoder.Unmarshal(data, &txCount)) + require.NoError(t, cbor.Unmarshal(data, &txCount)) require.Equal(t, header.TransactionCount, txCount.TransactionCount, shadowMsg) var timestamp headerTimestampProjection - require.NoError(t, encoder.Unmarshal(data, ×tamp)) + require.NoError(t, cbor.Unmarshal(data, ×tamp)) require.NotNil(t, timestamp.Timestamp, shadowMsg) require.Equal(t, header.Timestamp, *timestamp.Timestamp, shadowMsg) var eventsBloom headerEventsBloomProjection - require.NoError(t, encoder.Unmarshal(data, &eventsBloom)) + require.NoError(t, cbor.Unmarshal(data, &eventsBloom)) require.NotNil(t, eventsBloom.EventsBloom, shadowMsg) require.True(t, eventsBloom.EventsBloom.Test([]byte("sample-event")), "decoded bloom must carry the added element, not be a fresh empty filter") var hashAndRoot headerHashAndStateRootProjection - require.NoError(t, encoder.Unmarshal(data, &hashAndRoot)) + require.NoError(t, cbor.Unmarshal(data, &hashAndRoot)) require.Equal(t, header.Hash, hashAndRoot.Hash, shadowMsg) require.Equal(t, header.GlobalStateRoot, hashAndRoot.GlobalStateRoot, shadowMsg) } @@ -318,7 +318,7 @@ func TestProjectionsDecodeShadowedField(t *testing.T) { // TestProjectionsAreDecodeOnly proves marshaling a projection fails loudly rather than emitting a // corrupt record from its discarded fields. func TestProjectionsAreDecodeOnly(t *testing.T) { - _, err := encoder.Marshal(&headerHashProjection{Hash: felt.NewFromUint64[felt.Felt](1)}) + _, err := cbor.Marshal(&headerHashProjection{Hash: felt.NewFromUint64[felt.Felt](1)}) require.ErrorIs(t, err, errDiscardedCBORMarshal) } @@ -354,7 +354,7 @@ func sampleReceipt() *TransactionReceipt { // sampleReceiptBytes marshals a live TransactionReceipt, so the wire key set tracks the struct. func sampleReceiptBytes(tb testing.TB) []byte { tb.Helper() - data, err := encoder.Marshal(sampleReceipt()) + data, err := cbor.Marshal(sampleReceipt()) require.NoError(tb, err) return data } @@ -392,7 +392,7 @@ func TestReceiptProjectionCoversEveryWireKey(t *testing.T) { func TestExecutionStatusProjectionDecodesShadowedFields(t *testing.T) { receipt := sampleReceipt() var projection receiptExecutionStatusProjection - require.NoError(t, encoder.Unmarshal(sampleReceiptBytes(t), &projection)) + require.NoError(t, cbor.Unmarshal(sampleReceiptBytes(t), &projection)) require.Equal(t, receipt.Reverted, projection.Reverted, "Reverted must receive the wire value, not discardedCBOR") require.Equal(t, receipt.RevertReason, projection.RevertReason, @@ -404,7 +404,7 @@ func TestExecutionStatusProjectionDecodesShadowedFields(t *testing.T) { func TestEventsProjectionDecodesShadowedFields(t *testing.T) { receipt := sampleReceipt() var projection receiptEventsProjection - require.NoError(t, encoder.Unmarshal(sampleReceiptBytes(t), &projection)) + require.NoError(t, cbor.Unmarshal(sampleReceiptBytes(t), &projection)) require.Equal(t, receipt.Events, projection.Events, "Events must receive the wire value, not discardedCBOR") require.Equal(t, receipt.TransactionHash, projection.TransactionHash, @@ -440,14 +440,14 @@ func BenchmarkTransactionEventsProjection(b *testing.B) { b.ReportAllocs() for b.Loop() { var r TransactionReceipt - _ = encoder.Unmarshal(data, &r) + _ = cbor.Unmarshal(data, &r) } }) b.Run("events_projection", func(b *testing.B) { b.ReportAllocs() for b.Loop() { var r receiptEventsProjection - _ = encoder.Unmarshal(data, &r) + _ = cbor.Unmarshal(data, &r) } }) } @@ -464,14 +464,14 @@ func BenchmarkExecutionStatusProjection(b *testing.B) { Reverted bool RevertReason string } - _ = encoder.Unmarshal(data, &r) + _ = cbor.Unmarshal(data, &r) } }) b.Run("discard", func(b *testing.B) { b.ReportAllocs() for b.Loop() { var r receiptExecutionStatusProjection - _ = encoder.Unmarshal(data, &r) + _ = cbor.Unmarshal(data, &r) } }) } @@ -574,7 +574,7 @@ const cborMajorTypeTag = 6 // arrived untagged here would silently stop covering the case the production reader hits. func sampleTransactionBytes(tb testing.TB, transaction Transaction) []byte { tb.Helper() - data, err := encoder.Marshal(transaction) + data, err := cbor.Marshal(transaction) require.NoError(tb, err) require.EqualValues(tb, cborMajorTypeTag, data[0]>>5, "transaction records must be tag-wrapped (needs encoder/registry linked into this binary)") @@ -600,7 +600,7 @@ func TestTransactionHashProjectionDecodesHash(t *testing.T) { for _, transaction := range sampleTransactions() { t.Run(fmt.Sprintf("%T", transaction), func(t *testing.T) { var projection transactionHashProjection - require.NoError(t, encoder.Unmarshal(sampleTransactionBytes(t, transaction), &projection)) + require.NoError(t, cbor.Unmarshal(sampleTransactionBytes(t, transaction), &projection)) require.Equal(t, *transaction.Hash(), projection.TransactionHash, "TransactionHash must receive the wire value, not discardedCBOR") }) @@ -617,14 +617,14 @@ func BenchmarkTransactionHashProjection(b *testing.B) { b.ReportAllocs() for b.Loop() { var decoded Transaction - _ = encoder.Unmarshal(data, &decoded) + _ = cbor.Unmarshal(data, &decoded) } }) b.Run("hash_projection", func(b *testing.B) { b.ReportAllocs() for b.Loop() { var projection transactionHashProjection - _ = encoder.Unmarshal(data, &projection) + _ = cbor.Unmarshal(data, &projection) } }) }) diff --git a/core/running_event_filter_test.go b/core/running_event_filter_test.go index 173b6dd8ec..e21d03e46a 100644 --- a/core/running_event_filter_test.go +++ b/core/running_event_filter_test.go @@ -12,8 +12,8 @@ import ( "github.com/NethermindEth/juno/db" "github.com/NethermindEth/juno/db/memory" "github.com/NethermindEth/juno/db/pebblev2" - "github.com/NethermindEth/juno/encoder" adaptfeeder "github.com/NethermindEth/juno/starknetdata/feeder" + "github.com/NethermindEth/juno/utils/cbor/v1" "github.com/bits-and-blooms/bitset" "github.com/bits-and-blooms/bloom/v3" "github.com/stretchr/testify/require" @@ -525,11 +525,11 @@ func TestMarshalling(t *testing.T) { err := rf.Insert(testBloomWithRandomKeys(t, 1), core.NumBlocksPerFilter) require.NoError(t, err) - rfBytes, err := encoder.Marshal(rf) + rfBytes, err := cbor.Marshal(rf) require.NoError(t, err) var decoded core.RunningEventFilter - require.NoError(t, encoder.Unmarshal(rfBytes, &decoded)) + require.NoError(t, cbor.Unmarshal(rfBytes, &decoded)) rfInner, err := rf.InnerFilter() require.NoError(t, err) diff --git a/core/state/accessors.go b/core/state/accessors.go index dde9c64035..15571219bb 100644 --- a/core/state/accessors.go +++ b/core/state/accessors.go @@ -6,7 +6,7 @@ import ( "github.com/NethermindEth/juno/core" "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/db" - "github.com/NethermindEth/juno/encoder" + "github.com/NethermindEth/juno/utils/cbor/v1" ) func GetStateObject(r db.KeyValueReader, state *State, addr *felt.Felt) (*stateObject, error) { @@ -106,7 +106,7 @@ func WriteClass( classHash *felt.Felt, class *core.DeclaredClassDefinition, ) error { - enc, err := encoder.Marshal(class) + enc, err := cbor.Marshal(class) if err != nil { return err } @@ -123,7 +123,7 @@ func GetClass(r db.KeyValueReader, classHash *felt.Felt) (*core.DeclaredClassDef var class core.DeclaredClassDefinition if err := r.Get(key, func(data []byte) error { - return encoder.Unmarshal(data, &class) + return cbor.Unmarshal(data, &class) }); err != nil { return nil, err } diff --git a/core/state/accessors_test.go b/core/state/accessors_test.go index b869695b7b..b4f4dcd350 100644 --- a/core/state/accessors_test.go +++ b/core/state/accessors_test.go @@ -10,7 +10,7 @@ import ( "github.com/NethermindEth/juno/core/trie2/triedb" "github.com/NethermindEth/juno/db" "github.com/NethermindEth/juno/db/memory" - _ "github.com/NethermindEth/juno/encoder/registry" + _ "github.com/NethermindEth/juno/utils/cbor/registry" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/core/state/state_test.go b/core/state/state_test.go index ec4d897592..cbbe4c0cea 100644 --- a/core/state/state_test.go +++ b/core/state/state_test.go @@ -12,8 +12,8 @@ import ( "github.com/NethermindEth/juno/core/trie2/triedb" "github.com/NethermindEth/juno/db" "github.com/NethermindEth/juno/db/memory" - _ "github.com/NethermindEth/juno/encoder/registry" adaptfeeder "github.com/NethermindEth/juno/starknetdata/feeder" + _ "github.com/NethermindEth/juno/utils/cbor/registry" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/core/transaction.go b/core/transaction.go index a9e8c2df89..1bc4daa2a2 100644 --- a/core/transaction.go +++ b/core/transaction.go @@ -11,8 +11,8 @@ import ( "github.com/NethermindEth/juno/blockchain/networks" "github.com/NethermindEth/juno/core/crypto" "github.com/NethermindEth/juno/core/felt" - "github.com/NethermindEth/juno/encoder" "github.com/NethermindEth/juno/l1/eth" + "github.com/NethermindEth/juno/utils/cbor/v1" "github.com/bits-and-blooms/bloom/v3" "golang.org/x/crypto/sha3" ) @@ -197,8 +197,8 @@ func (v *TransactionVersion) AsFelt() *felt.Felt { } var ( - _ encoder.SelfEncoder = (*TransactionVersion)(nil) - _ encoder.SelfDecoder = (*TransactionVersion)(nil) + _ cbor.SelfEncoder = (*TransactionVersion)(nil) + _ cbor.SelfDecoder = (*TransactionVersion)(nil) ) func (v *TransactionVersion) MarshalCBOR() ([]byte, error) { diff --git a/core/transaction_test.go b/core/transaction_test.go index 10f74b3810..f52805acf0 100644 --- a/core/transaction_test.go +++ b/core/transaction_test.go @@ -11,8 +11,8 @@ import ( "github.com/NethermindEth/juno/clients/feeder" "github.com/NethermindEth/juno/core" "github.com/NethermindEth/juno/core/felt" - "github.com/NethermindEth/juno/encoder" adaptfeeder "github.com/NethermindEth/juno/starknetdata/feeder" + "github.com/NethermindEth/juno/utils/cbor/v1" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -126,11 +126,11 @@ func TestTransactionEncoding(t *testing.T) { func checkTransactionSymmetry(t *testing.T, input core.Transaction) { t.Helper() - data, err := encoder.Marshal(input) + data, err := cbor.Marshal(input) require.NoError(t, err) var txn core.Transaction - require.NoError(t, encoder.Unmarshal(data, &txn)) + require.NoError(t, cbor.Unmarshal(data, &txn)) switch v := txn.(type) { case *core.DeclareTransaction: diff --git a/core/trie2/triedb/pathdb/journal.go b/core/trie2/triedb/pathdb/journal.go index fcdfcc8a0b..8306c3d9f3 100644 --- a/core/trie2/triedb/pathdb/journal.go +++ b/core/trie2/triedb/pathdb/journal.go @@ -12,7 +12,7 @@ import ( "github.com/NethermindEth/juno/core/trie2/trienode" "github.com/NethermindEth/juno/core/trie2/trieutils" "github.com/NethermindEth/juno/db" - "github.com/NethermindEth/juno/encoder" + "github.com/NethermindEth/juno/utils/cbor/v1" ) const ( @@ -75,7 +75,7 @@ func (dl *diffLayer) journal(w io.Writer) error { EncNodeset: buf.Bytes(), } - enc, err := encoder.Marshal(diffJn) + enc, err := cbor.Marshal(diffJn) if err != nil { return err } @@ -118,7 +118,7 @@ func (dl *diskLayer) journal(w io.Writer) error { EncNodeset: buf.Bytes(), } - enc, err := encoder.Marshal(diskJn) + enc, err := cbor.Marshal(diskJn) if err != nil { return err } @@ -159,7 +159,7 @@ func (d *Database) Journal(root *felt.StateRootHash) error { dbJn.EncLayers = buf.Bytes() - enc, err := encoder.Marshal(dbJn) + enc, err := cbor.Marshal(dbJn) if err != nil { return err } @@ -187,7 +187,7 @@ func (d *Database) loadJournal() (layer, error) { } var journal DBJournal - if err := encoder.Unmarshal(enc, &journal); err != nil { + if err := cbor.Unmarshal(enc, &journal); err != nil { return nil, err } @@ -223,7 +223,7 @@ func (d *Database) loadLayers(enc []byte) (layer, error) { switch layerType { case diffJournal: var diffJn DiffJournal - if err := encoder.Unmarshal(encLayer, &diffJn); err != nil { + if err := cbor.Unmarshal(encLayer, &diffJn); err != nil { return nil, err } nodes := new(nodeSet) @@ -240,7 +240,7 @@ func (d *Database) loadLayers(enc []byte) (layer, error) { parent = head case diskJournal: var diskJn DiskJournal - if err := encoder.Unmarshal(encLayer, &diskJn); err != nil { + if err := cbor.Unmarshal(encLayer, &diskJn); err != nil { return nil, err } diff --git a/core/trie2/triedb/pathdb/nodeset.go b/core/trie2/triedb/pathdb/nodeset.go index 3a290b1bc9..f12af349f4 100644 --- a/core/trie2/triedb/pathdb/nodeset.go +++ b/core/trie2/triedb/pathdb/nodeset.go @@ -9,7 +9,7 @@ import ( "github.com/NethermindEth/juno/core/trie2/trienode" "github.com/NethermindEth/juno/core/trie2/trieutils" "github.com/NethermindEth/juno/db" - "github.com/NethermindEth/juno/encoder" + "github.com/NethermindEth/juno/utils/cbor/v1" ) // Contains the set of trie nodes for all the trie types @@ -195,7 +195,7 @@ func (s *nodeSet) encode(w io.Writer) error { nodes = append(nodes, entry) } - enc, err := encoder.Marshal(&JournalNodeSet{Nodes: nodes}) + enc, err := cbor.Marshal(&JournalNodeSet{Nodes: nodes}) if err != nil { return err } @@ -207,7 +207,7 @@ func (s *nodeSet) encode(w io.Writer) error { // Decodes the journal nodeset from the encoded bytes func (s *nodeSet) decode(data []byte) error { var encoded JournalNodeSet - if err := encoder.Unmarshal(data, &encoded); err != nil { + if err := cbor.Unmarshal(data, &encoded); err != nil { return err } s.classNodes = make(classNodesMap) diff --git a/core/trie2/trienode/init_test.go b/core/trie2/trienode/init_test.go index 54b54c1372..18af7a0ec4 100644 --- a/core/trie2/trienode/init_test.go +++ b/core/trie2/trienode/init_test.go @@ -1,5 +1,5 @@ package trienode import ( - _ "github.com/NethermindEth/juno/encoder" + _ "github.com/NethermindEth/juno/utils/cbor/v1" ) diff --git a/db/pebblev2/upgrade_test.go b/db/pebblev2/upgrade_test.go index 5e3f9236f4..f6177132d1 100644 --- a/db/pebblev2/upgrade_test.go +++ b/db/pebblev2/upgrade_test.go @@ -9,8 +9,8 @@ import ( "github.com/NethermindEth/juno/db" "github.com/NethermindEth/juno/db/pebble" "github.com/NethermindEth/juno/db/pebblev2" - "github.com/NethermindEth/juno/encoder" - _ "github.com/NethermindEth/juno/encoder/registry" + _ "github.com/NethermindEth/juno/utils/cbor/registry" + "github.com/NethermindEth/juno/utils/cbor/v1" pebblev2lib "github.com/cockroachdb/pebble/v2" "github.com/cockroachdb/pebble/v2/vfs" "github.com/stretchr/testify/require" @@ -48,7 +48,7 @@ func buildTestValue(t *testing.T) []byte { data[key] = rand.Text() } - dataBytes, err := encoder.Marshal(data) + dataBytes, err := cbor.Marshal(data) require.NoError(t, err) return dataBytes } diff --git a/db/typed/key/cbor.go b/db/typed/key/cbor.go index 61958e06c7..9904f3573b 100644 --- a/db/typed/key/cbor.go +++ b/db/typed/key/cbor.go @@ -1,11 +1,11 @@ package key -import "github.com/NethermindEth/juno/encoder" +import "github.com/NethermindEth/juno/utils/cbor/v1" type cborSerializer[K any] struct{} func (cborSerializer[K]) Marshal(value K) []byte { - data, err := encoder.Marshal(value) + data, err := cbor.Marshal(value) if err != nil { panic(err) } diff --git a/db/typed/value/cbor.go b/db/typed/value/cbor.go index d1e04b1444..09e8364fa6 100644 --- a/db/typed/value/cbor.go +++ b/db/typed/value/cbor.go @@ -1,13 +1,13 @@ package value -import "github.com/NethermindEth/juno/encoder" +import "github.com/NethermindEth/juno/utils/cbor/v1" type cborSerializer[V any] struct{} func (cborSerializer[V]) Marshal(value *V) ([]byte, error) { - return encoder.Marshal(value) + return cbor.Marshal(value) } func (cborSerializer[V]) Unmarshal(data []byte, value *V) error { - return encoder.Unmarshal(data, value) + return cbor.Unmarshal(data, value) } diff --git a/encoder/raw_test.go b/encoder/raw_test.go deleted file mode 100644 index 1bedc7e33f..0000000000 --- a/encoder/raw_test.go +++ /dev/null @@ -1,54 +0,0 @@ -package encoder_test - -import ( - "testing" - - "github.com/NethermindEth/juno/encoder" - "github.com/stretchr/testify/require" -) - -// the array [1, 2, 3], already encoded -var cborArray123 = []byte{0x83, 0x01, 0x02, 0x03} - -// two cborArray123 inside an array of two -var cborTwoArrays = []byte{0x82, 0x83, 0x01, 0x02, 0x03, 0x83, 0x01, 0x02, 0x03} - -func TestRawMessageMarshalUnchanged(t *testing.T) { - out, err := encoder.Marshal([]encoder.RawMessage{cborArray123, cborArray123}) - require.NoError(t, err) - require.Equal(t, cborTwoArrays, out) -} - -func TestRawMessageMarshalEmptyAsNull(t *testing.T) { - out, err := encoder.Marshal(encoder.RawMessage(nil)) - require.NoError(t, err) - require.Equal(t, []byte{0xf6}, out) -} - -func TestRawMessageUnmarshalUnchanged(t *testing.T) { - var decoded []encoder.RawMessage - require.NoError(t, encoder.Unmarshal(cborTwoArrays, &decoded)) - require.Equal(t, []encoder.RawMessage{cborArray123, cborArray123}, decoded) -} - -func TestRawMessageUnmarshalNullAsNull(t *testing.T) { - var decoded encoder.RawMessage - require.NoError(t, encoder.Unmarshal([]byte{0xf6}, &decoded)) - require.Equal(t, encoder.RawMessage{0xf6}, decoded) -} - -func TestRawMessageUnmarshalReplacesTarget(t *testing.T) { - decoded := encoder.RawMessage{0xff, 0xff, 0xff, 0xff, 0xff, 0xff} - require.NoError(t, encoder.Unmarshal(cborArray123, &decoded)) - require.Equal(t, encoder.RawMessage(cborArray123), decoded) -} - -func TestRawMessageUnmarshalCopies(t *testing.T) { - data := append([]byte{}, cborArray123...) - - var decoded encoder.RawMessage - require.NoError(t, encoder.Unmarshal(data, &decoded)) - - data[1] = 0xff - require.Equal(t, encoder.RawMessage(cborArray123), decoded) -} diff --git a/mempool/db_utils.go b/mempool/db_utils.go index e4b3d0ccc4..3c387065f3 100644 --- a/mempool/db_utils.go +++ b/mempool/db_utils.go @@ -6,7 +6,7 @@ import ( "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/db" - "github.com/NethermindEth/juno/encoder" + "github.com/NethermindEth/juno/utils/cbor/v1" ) func GetHeadValue(r db.KeyValueReader) (felt.Felt, error) { @@ -38,13 +38,13 @@ func WriteTailValue(w db.KeyValueWriter, tail *felt.Felt) error { func GetTxn(r db.KeyValueReader, txnHash *felt.Felt) (dbPoolTxn, error) { var item dbPoolTxn err := r.Get(db.MempoolNodeKey(txnHash), func(data []byte) error { - return encoder.Unmarshal(data, &item) + return cbor.Unmarshal(data, &item) }) return item, err } func WriteTxn(w db.KeyValueWriter, item *dbPoolTxn) error { - itemBytes, err := encoder.Marshal(item) + itemBytes, err := cbor.Marshal(item) if err != nil { return err } diff --git a/mempool/mempool_test.go b/mempool/mempool_test.go index 0b3baaa267..c95dc1dd49 100644 --- a/mempool/mempool_test.go +++ b/mempool/mempool_test.go @@ -13,10 +13,10 @@ import ( statetestutils "github.com/NethermindEth/juno/core/state/testutils" "github.com/NethermindEth/juno/db" "github.com/NethermindEth/juno/db/pebblev2" - _ "github.com/NethermindEth/juno/encoder/registry" "github.com/NethermindEth/juno/mempool" "github.com/NethermindEth/juno/mocks" adaptfeeder "github.com/NethermindEth/juno/starknetdata/feeder" + _ "github.com/NethermindEth/juno/utils/cbor/registry" "github.com/NethermindEth/juno/utils/log" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/migration/blocktransactions/blocktransactions.go b/migration/blocktransactions/blocktransactions.go index d76e564759..fbe5a2319b 100644 --- a/migration/blocktransactions/blocktransactions.go +++ b/migration/blocktransactions/blocktransactions.go @@ -10,10 +10,10 @@ import ( "github.com/NethermindEth/juno/db" "github.com/NethermindEth/juno/db/typed/key" "github.com/NethermindEth/juno/db/typed/prefix" - _ "github.com/NethermindEth/juno/encoder/registry" "github.com/NethermindEth/juno/migration" "github.com/NethermindEth/juno/migration/pipeline" "github.com/NethermindEth/juno/migration/semaphore" + _ "github.com/NethermindEth/juno/utils/cbor/registry" "github.com/NethermindEth/juno/utils/log" ) diff --git a/migration/blocktransactions/ingestor.go b/migration/blocktransactions/ingestor.go index 0fe0c959d2..c530563a23 100644 --- a/migration/blocktransactions/ingestor.go +++ b/migration/blocktransactions/ingestor.go @@ -8,9 +8,9 @@ import ( "github.com/NethermindEth/juno/core" "github.com/NethermindEth/juno/db" "github.com/NethermindEth/juno/db/typed/prefix" - "github.com/NethermindEth/juno/encoder" "github.com/NethermindEth/juno/migration/pipeline" "github.com/NethermindEth/juno/migration/semaphore" + "github.com/NethermindEth/juno/utils/cbor/v1" "github.com/NethermindEth/juno/utils/log" "go.uber.org/zap" ) @@ -170,10 +170,10 @@ func (c *ingestor) validateCount( func extractValues( seq iter.Seq2[prefix.Entry[[]byte], error], -) iter.Seq2[encoder.RawMessage, error] { - return func(yield func(encoder.RawMessage, error) bool) { +) iter.Seq2[cbor.RawMessage, error] { + return func(yield func(cbor.RawMessage, error) bool) { for item, err := range seq { - if !yield(encoder.RawMessage(item.Value), err) { + if !yield(cbor.RawMessage(item.Value), err) { return } } diff --git a/migration/blocktransactions/txlayout/transaction_layout_test.go b/migration/blocktransactions/txlayout/transaction_layout_test.go index 34f7bbb7ba..1f49508c4e 100644 --- a/migration/blocktransactions/txlayout/transaction_layout_test.go +++ b/migration/blocktransactions/txlayout/transaction_layout_test.go @@ -8,8 +8,8 @@ import ( "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/db" "github.com/NethermindEth/juno/db/pebblev2" - _ "github.com/NethermindEth/juno/encoder/registry" "github.com/NethermindEth/juno/migration/blocktransactions/txlayout" + _ "github.com/NethermindEth/juno/utils/cbor/registry" "github.com/stretchr/testify/require" ) diff --git a/migration/deprecated/init_pkg_test.go b/migration/deprecated/init_pkg_test.go index b9ce9f562d..2ebf435838 100644 --- a/migration/deprecated/init_pkg_test.go +++ b/migration/deprecated/init_pkg_test.go @@ -1,5 +1,5 @@ package deprecated import ( - _ "github.com/NethermindEth/juno/encoder/registry" + _ "github.com/NethermindEth/juno/utils/cbor/registry" ) diff --git a/migration/deprecated/l1handlermapping/init_test.go b/migration/deprecated/l1handlermapping/init_test.go index 9baf56b501..a6697ad00c 100644 --- a/migration/deprecated/l1handlermapping/init_test.go +++ b/migration/deprecated/l1handlermapping/init_test.go @@ -1,5 +1,5 @@ package l1handlermapping_test import ( - _ "github.com/NethermindEth/juno/encoder/registry" + _ "github.com/NethermindEth/juno/utils/cbor/registry" ) diff --git a/migration/deprecated/migration.go b/migration/deprecated/migration.go index e77d13ffa4..136e0e8e05 100644 --- a/migration/deprecated/migration.go +++ b/migration/deprecated/migration.go @@ -25,11 +25,11 @@ import ( "github.com/NethermindEth/juno/db/typed" "github.com/NethermindEth/juno/db/typed/key" "github.com/NethermindEth/juno/db/typed/value" - "github.com/NethermindEth/juno/encoder" "github.com/NethermindEth/juno/migration/blocktransactions/txlayout" "github.com/NethermindEth/juno/migration/deprecated/casmhashmetadata" "github.com/NethermindEth/juno/migration/deprecated/l1handlermapping" "github.com/NethermindEth/juno/starknet" + "github.com/NethermindEth/juno/utils/cbor/v1" "github.com/NethermindEth/juno/utils/log" "github.com/bits-and-blooms/bitset" "github.com/sourcegraph/conc/pool" @@ -205,7 +205,7 @@ func SchemaMetadata(log log.StructuredLogger, targetDB db.KeyValueStore) (schema } err = txn.Get(db.DeprecatedSchemaIntermediateState.Key(), func(data []byte) error { - err := encoder.Unmarshal(data, &metadata.IntermediateState) + err := cbor.Unmarshal(data, &metadata.IntermediateState) if err != nil { // TODO: Instead of returning nil, we log the error for now to debug the issue log.Error( @@ -232,7 +232,7 @@ func updateSchemaMetadata(txn db.KeyValueWriter, schema schemaMetadata) error { err error ) binary.BigEndian.PutUint64(version[:], schema.Version) - state, err = encoder.Marshal(schema.IntermediateState) + state, err = cbor.Marshal(schema.IntermediateState) if err != nil { return err } @@ -481,7 +481,7 @@ func (m *changeTrieNodeEncoding) Migrate( return err } - if err = encoder.Unmarshal(v, &n); err != nil { + if err = cbor.Unmarshal(v, &n); err != nil { return err } @@ -715,7 +715,7 @@ type oldStateUpdate struct { func changeStateDiffStruct2(txn db.KeyValueWriter, key, value []byte, _ *networks.Network) error { old := new(oldStateUpdate) - if err := encoder.Unmarshal(value, old); err != nil { + if err := cbor.Unmarshal(value, old); err != nil { return fmt.Errorf("unmarshal: %v", err) } @@ -746,7 +746,7 @@ func changeStateDiffStruct2(txn db.KeyValueWriter, key, value []byte, _ *network replacedClasses[*replacedClass.Address] = replacedClass.ClassHash } - newValue, err := encoder.Marshal(&core.StateUpdate{ + newValue, err := cbor.Marshal(&core.StateUpdate{ BlockHash: old.BlockHash, NewRoot: old.NewRoot, OldRoot: old.OldRoot, @@ -799,11 +799,11 @@ func migrateCairo1CompiledClass2( _ *networks.Network, ) error { var class declaredClass - err := encoder.Unmarshal(value, &class) + err := cbor.Unmarshal(value, &class) if err != nil { // assumption that only Cairo0 class causes this error // TODO(granza): discriminate the record by its own shape, not by the error. - targetErr := new(encoder.UnmarshalTypeError) + targetErr := new(cbor.UnmarshalTypeError) if errors.As(err, &targetErr) { return nil } @@ -838,7 +838,7 @@ func migrateCairo1CompiledClass2( }, } - value, err = encoder.Marshal(declaredClass) + value, err = cbor.Marshal(declaredClass) if err != nil { return err } @@ -967,7 +967,7 @@ func calculateCasmClassHashesV2(txn db.IndexedBatch, network *networks.Network) workerPool.Go( func() error { var declaredClass core.DeclaredClassDefinition - if err := encoder.Unmarshal(value, &declaredClass); err != nil { + if err := cbor.Unmarshal(value, &declaredClass); err != nil { return err } diff --git a/migration/deprecated/migration_pkg_test.go b/migration/deprecated/migration_pkg_test.go index 483ceb8e0e..f7e74939b8 100644 --- a/migration/deprecated/migration_pkg_test.go +++ b/migration/deprecated/migration_pkg_test.go @@ -18,9 +18,9 @@ import ( "github.com/NethermindEth/juno/core/trie" "github.com/NethermindEth/juno/db" "github.com/NethermindEth/juno/db/memory" - "github.com/NethermindEth/juno/encoder" "github.com/NethermindEth/juno/l1/eth" adaptfeeder "github.com/NethermindEth/juno/starknetdata/feeder" + "github.com/NethermindEth/juno/utils/cbor/v1" "github.com/NethermindEth/juno/utils/log" "github.com/bits-and-blooms/bitset" "github.com/stretchr/testify/assert" @@ -162,7 +162,7 @@ func TestChangeTrieNodeEncoding(t *testing.T) { for i := range 5 { n.Value = new(felt.Felt).SetUint64(uint64(i)) - encodedNode, err := encoder.Marshal(n) + encodedNode, err := cbor.Marshal(n) if err != nil { return err } @@ -341,7 +341,7 @@ func TestMigrateCairo1CompiledClass(t *testing.T) { }, } { expectedDeclared.Class.Compiled = json.RawMessage(test.compiledJSON) - classBytes, err := encoder.Marshal(expectedDeclared) + classBytes, err := cbor.Marshal(expectedDeclared) require.NoError(t, err) err = txn.Put(key, classBytes) require.NoError(t, err) @@ -350,7 +350,7 @@ func TestMigrateCairo1CompiledClass(t *testing.T) { var actualDeclared core.DeclaredClassDefinition err = txn.Get(key, func(data []byte) error { - return encoder.Unmarshal(data, &actualDeclared) + return cbor.Unmarshal(data, &actualDeclared) }) require.NoError(t, err) @@ -678,7 +678,7 @@ func TestChangeStateDiffStruct(t *testing.T) { ReplacedClasses: []oldAddressClassHashPair{{Address: felt.NewUnsafeFromString[felt.Felt]("0x13"), ClassHash: felt.NewUnsafeFromString[felt.Felt]("0x14")}}, }, } - su0Bytes, err := encoder.Marshal(su0) + su0Bytes, err := cbor.Marshal(su0) require.NoError(t, err) require.NoError(t, txn.Put(su0Key, su0Bytes)) @@ -703,7 +703,7 @@ func TestChangeStateDiffStruct(t *testing.T) { ReplacedClasses: []oldAddressClassHashPair{{Address: felt.NewUnsafeFromString[felt.Felt]("0x28"), ClassHash: felt.NewUnsafeFromString[felt.Felt]("0x29")}}, }, } - su1Bytes, err := encoder.Marshal(su1) + su1Bytes, err := cbor.Marshal(su1) require.NoError(t, err) require.NoError(t, txn.Put(su1Key, su1Bytes)) return nil @@ -798,7 +798,7 @@ func TestChangeStateDiffStruct(t *testing.T) { value, err := iter.Value() require.NoError(t, err) got := new(core.StateUpdate) - require.NoError(t, encoder.Unmarshal(value, got)) + require.NoError(t, cbor.Unmarshal(value, got)) require.Equal(t, update.want, got) } require.False(t, iter.Next()) diff --git a/migration/historyprunner/migrator_test.go b/migration/historyprunner/migrator_test.go index e0da536060..d34e46acb7 100644 --- a/migration/historyprunner/migrator_test.go +++ b/migration/historyprunner/migrator_test.go @@ -10,9 +10,9 @@ import ( "github.com/NethermindEth/juno/core" "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/db" - _ "github.com/NethermindEth/juno/encoder/registry" "github.com/NethermindEth/juno/migration/historyprunner" "github.com/NethermindEth/juno/pruner/testutils" + _ "github.com/NethermindEth/juno/utils/cbor/registry" "github.com/NethermindEth/juno/utils/log" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/migration/statedifflength/migrator_test.go b/migration/statedifflength/migrator_test.go index df1118ccf4..a027517ab7 100644 --- a/migration/statedifflength/migrator_test.go +++ b/migration/statedifflength/migrator_test.go @@ -11,8 +11,8 @@ import ( "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/db" "github.com/NethermindEth/juno/db/memory" - _ "github.com/NethermindEth/juno/encoder/registry" "github.com/NethermindEth/juno/migration/statedifflength" + _ "github.com/NethermindEth/juno/utils/cbor/registry" "github.com/NethermindEth/juno/utils/log" "github.com/stretchr/testify/require" ) diff --git a/node/init_test.go b/node/init_test.go index cb01f2035a..c197986d61 100644 --- a/node/init_test.go +++ b/node/init_test.go @@ -1,5 +1,5 @@ package node_test import ( - _ "github.com/NethermindEth/juno/encoder/registry" + _ "github.com/NethermindEth/juno/utils/cbor/registry" ) diff --git a/node/migration_test.go b/node/migration_test.go index 8071a7a279..3aac27b72e 100644 --- a/node/migration_test.go +++ b/node/migration_test.go @@ -9,7 +9,7 @@ import ( "github.com/NethermindEth/juno/core" "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/db/memory" - _ "github.com/NethermindEth/juno/encoder/registry" + _ "github.com/NethermindEth/juno/utils/cbor/registry" "github.com/NethermindEth/juno/utils/log" "github.com/stretchr/testify/require" ) diff --git a/p2p/codec.go b/p2p/codec.go index cea5537447..bdadf4ee38 100644 --- a/p2p/codec.go +++ b/p2p/codec.go @@ -3,7 +3,7 @@ package p2p import ( "fmt" - "github.com/NethermindEth/juno/encoder" + "github.com/NethermindEth/juno/utils/cbor/v1" "github.com/multiformats/go-multiaddr" ) @@ -14,7 +14,7 @@ func EncodeAddrs(addrs []multiaddr.Multiaddr) ([]byte, error) { multiAddrBytes[i] = addr.Bytes() } - encoded, err := encoder.Marshal(multiAddrBytes) + encoded, err := cbor.Marshal(multiAddrBytes) if err != nil { return nil, fmt.Errorf("encode addresses: %w", err) } @@ -25,7 +25,7 @@ func EncodeAddrs(addrs []multiaddr.Multiaddr) ([]byte, error) { // decodeAddrs decodes a byte slice into a slice of multiaddrs func decodeAddrs(b []byte) ([]multiaddr.Multiaddr, error) { var multiAddrBytes [][]byte - if err := encoder.Unmarshal(b, &multiAddrBytes); err != nil { + if err := cbor.Unmarshal(b, &multiAddrBytes); err != nil { return nil, fmt.Errorf("decode addresses: %w", err) } diff --git a/plugin/init_test.go b/plugin/init_test.go index 7dfd97e7f6..6585f6f98d 100644 --- a/plugin/init_test.go +++ b/plugin/init_test.go @@ -1,5 +1,5 @@ package plugin_test import ( - _ "github.com/NethermindEth/juno/encoder/registry" + _ "github.com/NethermindEth/juno/utils/cbor/registry" ) diff --git a/pruner/accessors_internal_test.go b/pruner/accessors_internal_test.go index c1e489faec..6bc45d853d 100644 --- a/pruner/accessors_internal_test.go +++ b/pruner/accessors_internal_test.go @@ -6,8 +6,8 @@ import ( "github.com/NethermindEth/juno/core" "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/db" - _ "github.com/NethermindEth/juno/encoder/registry" "github.com/NethermindEth/juno/pruner/testutils" + _ "github.com/NethermindEth/juno/utils/cbor/registry" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/pruner/accessors_test.go b/pruner/accessors_test.go index 01a7f56f3d..bf7303e155 100644 --- a/pruner/accessors_test.go +++ b/pruner/accessors_test.go @@ -6,9 +6,9 @@ import ( "github.com/NethermindEth/juno/core" "github.com/NethermindEth/juno/db" - _ "github.com/NethermindEth/juno/encoder/registry" "github.com/NethermindEth/juno/pruner" "github.com/NethermindEth/juno/pruner/testutils" + _ "github.com/NethermindEth/juno/utils/cbor/registry" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/pruner/pruner_test.go b/pruner/pruner_test.go index 20609720bb..b5212e49cf 100644 --- a/pruner/pruner_test.go +++ b/pruner/pruner_test.go @@ -7,10 +7,10 @@ import ( "github.com/NethermindEth/juno/core" "github.com/NethermindEth/juno/db" - _ "github.com/NethermindEth/juno/encoder/registry" "github.com/NethermindEth/juno/feed" "github.com/NethermindEth/juno/pruner" "github.com/NethermindEth/juno/pruner/testutils" + _ "github.com/NethermindEth/juno/utils/cbor/registry" "github.com/NethermindEth/juno/utils/log" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/pruner/retention_test.go b/pruner/retention_test.go index 9690ef2964..bf0ba1e005 100644 --- a/pruner/retention_test.go +++ b/pruner/retention_test.go @@ -7,9 +7,9 @@ import ( "github.com/NethermindEth/juno/core" "github.com/NethermindEth/juno/db" "github.com/NethermindEth/juno/db/memory" - _ "github.com/NethermindEth/juno/encoder/registry" "github.com/NethermindEth/juno/pruner" "github.com/NethermindEth/juno/pruner/testutils" + _ "github.com/NethermindEth/juno/utils/cbor/registry" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) diff --git a/rpc/v10/init_test.go b/rpc/v10/init_test.go index f66001f9d8..11d17c2c8c 100644 --- a/rpc/v10/init_test.go +++ b/rpc/v10/init_test.go @@ -1,5 +1,5 @@ package rpcv10_test import ( - _ "github.com/NethermindEth/juno/encoder/registry" + _ "github.com/NethermindEth/juno/utils/cbor/registry" ) diff --git a/rpc/v8/init_test.go b/rpc/v8/init_test.go index 7b977cf0f5..c9f68fca87 100644 --- a/rpc/v8/init_test.go +++ b/rpc/v8/init_test.go @@ -1,5 +1,5 @@ package rpcv8_test import ( - _ "github.com/NethermindEth/juno/encoder/registry" + _ "github.com/NethermindEth/juno/utils/cbor/registry" ) diff --git a/rpc/v9/init_test.go b/rpc/v9/init_test.go index 743cac2e21..9a2591cf57 100644 --- a/rpc/v9/init_test.go +++ b/rpc/v9/init_test.go @@ -1,5 +1,5 @@ package rpcv9_test import ( - _ "github.com/NethermindEth/juno/encoder/registry" + _ "github.com/NethermindEth/juno/utils/cbor/registry" ) diff --git a/sequencer/init_test.go b/sequencer/init_test.go index 4f7f506887..b3e9485abe 100644 --- a/sequencer/init_test.go +++ b/sequencer/init_test.go @@ -1,5 +1,5 @@ package sequencer_test import ( - _ "github.com/NethermindEth/juno/encoder/registry" + _ "github.com/NethermindEth/juno/utils/cbor/registry" ) diff --git a/sync/init_test.go b/sync/init_test.go index 29deb6530c..4de694f716 100644 --- a/sync/init_test.go +++ b/sync/init_test.go @@ -1,5 +1,5 @@ package sync_test import ( - _ "github.com/NethermindEth/juno/encoder/registry" + _ "github.com/NethermindEth/juno/utils/cbor/registry" ) diff --git a/encoder/registry/registry.go b/utils/cbor/registry/registry.go similarity index 94% rename from encoder/registry/registry.go rename to utils/cbor/registry/registry.go index 4dbb813213..82535695e1 100644 --- a/encoder/registry/registry.go +++ b/utils/cbor/registry/registry.go @@ -8,7 +8,7 @@ import ( "github.com/NethermindEth/juno/core" "github.com/NethermindEth/juno/core/trie2/triedb/pathdb" "github.com/NethermindEth/juno/core/trie2/trienode" - "github.com/NethermindEth/juno/encoder" + "github.com/NethermindEth/juno/utils/cbor/v1" ) var once sync.Once @@ -39,7 +39,7 @@ func init() { } for _, t := range types { - err := encoder.RegisterType(t) + err := cbor.RegisterType(t) if err != nil { panic(err) } diff --git a/encoder/encoder.go b/utils/cbor/v1/cbor.go similarity index 77% rename from encoder/encoder.go rename to utils/cbor/v1/cbor.go index 6bb6c2e85c..7b3701de19 100644 --- a/encoder/encoder.go +++ b/utils/cbor/v1/cbor.go @@ -1,17 +1,17 @@ -package encoder +package cbor import ( "io" "reflect" - "github.com/fxamacker/cbor/v2" + fxcbor "github.com/fxamacker/cbor/v2" ) // UnmarshalTypeError is a wire item that does not fit the Go type, as opposed to malformed input. -type UnmarshalTypeError = cbor.UnmarshalTypeError +type UnmarshalTypeError = fxcbor.UnmarshalTypeError var ( - ts = cbor.NewTagSet() + ts = fxcbor.NewTagSet() // https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml // 65536-15309735 Unassigned tagNum uint64 = 65536 @@ -20,16 +20,16 @@ var ( strictMode = newStrictMode() ) -func newEncMode() cbor.EncMode { - mode, err := cbor.CanonicalEncOptions().EncModeWithTags(ts) +func newEncMode() fxcbor.EncMode { + mode, err := fxcbor.CanonicalEncOptions().EncModeWithTags(ts) if err != nil { panic(err) } return mode } -func newDecMode() cbor.DecMode { - mode, err := cbor.DecOptions{ +func newDecMode() fxcbor.DecMode { + mode, err := fxcbor.DecOptions{ MaxArrayElements: 10485760, // Set to a reasonably high value, 10MiB }.DecModeWithTags(ts) if err != nil { @@ -38,9 +38,9 @@ func newDecMode() cbor.DecMode { return mode } -func newStrictMode() cbor.DecMode { - mode, err := cbor.DecOptions{ - ExtraReturnErrors: cbor.ExtraDecErrorUnknownField, +func newStrictMode() fxcbor.DecMode { + mode, err := fxcbor.DecOptions{ + ExtraReturnErrors: fxcbor.ExtraDecErrorUnknownField, }.DecMode() if err != nil { panic(err) @@ -52,7 +52,7 @@ func newStrictMode() cbor.DecMode { // Only call this from encoder/registry's init(). func RegisterType(rType reflect.Type) error { if err := ts.Add( - cbor.TagOptions{EncTag: cbor.EncTagRequired, DecTag: cbor.DecTagRequired}, + fxcbor.TagOptions{EncTag: fxcbor.EncTagRequired, DecTag: fxcbor.DecTagRequired}, rType, tagNum, ); err != nil { diff --git a/encoder/golden_test.go b/utils/cbor/v1/golden_test.go similarity index 96% rename from encoder/golden_test.go rename to utils/cbor/v1/golden_test.go index 746ae22242..ab7473e068 100644 --- a/encoder/golden_test.go +++ b/utils/cbor/v1/golden_test.go @@ -1,4 +1,4 @@ -package encoder_test +package cbor_test import ( "encoding/hex" @@ -12,9 +12,9 @@ import ( "github.com/NethermindEth/juno/core/felt" "github.com/NethermindEth/juno/core/trie2/triedb/pathdb" "github.com/NethermindEth/juno/core/trie2/trienode" - "github.com/NethermindEth/juno/encoder" - _ "github.com/NethermindEth/juno/encoder/registry" "github.com/NethermindEth/juno/l1/eth" + _ "github.com/NethermindEth/juno/utils/cbor/registry" + "github.com/NethermindEth/juno/utils/cbor/v1" bloom "github.com/bits-and-blooms/bloom/v3" "github.com/stretchr/testify/require" ) @@ -70,7 +70,7 @@ func goldenCases() []struct { felt.FromUint64[felt.Felt](8), }}, {"felt.Slice nil", felt.Slice[felt.Felt](nil)}, - {"encoder.RawMessage", encoder.RawMessage{0x83, 0x01, 0x02, 0x03}}, + {"cbor.RawMessage", cbor.RawMessage{0x83, 0x01, 0x02, 0x03}}, {"DeclaredClassDefinition, a Sierra class", populatedDeclaredClassDefinition()}, {"Header, populated", populatedHeader()}, {"InvokeTransaction, populated", populatedInvokeTransaction()}, @@ -183,7 +183,7 @@ func TestGoldenBytes(t *testing.T) { for _, c := range goldenCases() { t.Run(c.name, func(t *testing.T) { - b, err := encoder.Marshal(c.value) + b, err := cbor.Marshal(c.value) require.NoError(t, err) want, ok := golden[c.name] @@ -194,7 +194,7 @@ func TestGoldenBytes(t *testing.T) { require.NoError(t, err) back := reflect.New(reflect.TypeOf(c.value)) - require.NoError(t, encoder.Unmarshal(stored, back.Interface())) + require.NoError(t, cbor.Unmarshal(stored, back.Interface())) require.Equal(t, c.value, back.Elem().Interface()) }) } diff --git a/encoder/hook.go b/utils/cbor/v1/hook.go similarity index 94% rename from encoder/hook.go rename to utils/cbor/v1/hook.go index 0a8a28e46d..a86e95c596 100644 --- a/encoder/hook.go +++ b/utils/cbor/v1/hook.go @@ -1,4 +1,4 @@ -package encoder +package cbor // SelfEncoder is the hook a type implements to write its own encoding. type SelfEncoder interface { diff --git a/encoder/raw.go b/utils/cbor/v1/raw.go similarity index 97% rename from encoder/raw.go rename to utils/cbor/v1/raw.go index bdfbb9e88b..7e524e36bb 100644 --- a/encoder/raw.go +++ b/utils/cbor/v1/raw.go @@ -1,4 +1,4 @@ -package encoder +package cbor // cborNull is what the generic encoder emits for a nil slice. const cborNull = 0xf6 diff --git a/utils/cbor/v1/raw_test.go b/utils/cbor/v1/raw_test.go new file mode 100644 index 0000000000..073bbeba58 --- /dev/null +++ b/utils/cbor/v1/raw_test.go @@ -0,0 +1,54 @@ +package cbor_test + +import ( + "testing" + + "github.com/NethermindEth/juno/utils/cbor/v1" + "github.com/stretchr/testify/require" +) + +// the array [1, 2, 3], already encoded +var cborArray123 = []byte{0x83, 0x01, 0x02, 0x03} + +// two cborArray123 inside an array of two +var cborTwoArrays = []byte{0x82, 0x83, 0x01, 0x02, 0x03, 0x83, 0x01, 0x02, 0x03} + +func TestRawMessageMarshalUnchanged(t *testing.T) { + out, err := cbor.Marshal([]cbor.RawMessage{cborArray123, cborArray123}) + require.NoError(t, err) + require.Equal(t, cborTwoArrays, out) +} + +func TestRawMessageMarshalEmptyAsNull(t *testing.T) { + out, err := cbor.Marshal(cbor.RawMessage(nil)) + require.NoError(t, err) + require.Equal(t, []byte{0xf6}, out) +} + +func TestRawMessageUnmarshalUnchanged(t *testing.T) { + var decoded []cbor.RawMessage + require.NoError(t, cbor.Unmarshal(cborTwoArrays, &decoded)) + require.Equal(t, []cbor.RawMessage{cborArray123, cborArray123}, decoded) +} + +func TestRawMessageUnmarshalNullAsNull(t *testing.T) { + var decoded cbor.RawMessage + require.NoError(t, cbor.Unmarshal([]byte{0xf6}, &decoded)) + require.Equal(t, cbor.RawMessage{0xf6}, decoded) +} + +func TestRawMessageUnmarshalReplacesTarget(t *testing.T) { + decoded := cbor.RawMessage{0xff, 0xff, 0xff, 0xff, 0xff, 0xff} + require.NoError(t, cbor.Unmarshal(cborArray123, &decoded)) + require.Equal(t, cbor.RawMessage(cborArray123), decoded) +} + +func TestRawMessageUnmarshalCopies(t *testing.T) { + data := append([]byte{}, cborArray123...) + + var decoded cbor.RawMessage + require.NoError(t, cbor.Unmarshal(data, &decoded)) + + data[1] = 0xff + require.Equal(t, cbor.RawMessage(cborArray123), decoded) +} diff --git a/encoder/testdata/on_disk_bytes.json b/utils/cbor/v1/testdata/on_disk_bytes.json similarity index 99% rename from encoder/testdata/on_disk_bytes.json rename to utils/cbor/v1/testdata/on_disk_bytes.json index 204fbc6ace..3c41e8a24c 100644 --- a/encoder/testdata/on_disk_bytes.json +++ b/utils/cbor/v1/testdata/on_disk_bytes.json @@ -27,7 +27,7 @@ "[][]byte nil": "f6", "[][]byte, the p2p shape": "834301020342040540", "[]byte, the schema state": "43090807", - "encoder.RawMessage": "83010203", + "cbor.RawMessage": "83010203", "felt.Slice nil": "f6", "felt.Slice, two felts": "82841bffffffffffffff211bffffffffffffffff1bffffffffffffffff1b07fffffffffff130841bffffffffffffff011bffffffffffffffff1bffffffffffffffff1b07ffffffffffef10" } diff --git a/encoder/unmarshal_first_test.go b/utils/cbor/v1/unmarshal_first_test.go similarity index 85% rename from encoder/unmarshal_first_test.go rename to utils/cbor/v1/unmarshal_first_test.go index 673109291d..def0b2621c 100644 --- a/encoder/unmarshal_first_test.go +++ b/utils/cbor/v1/unmarshal_first_test.go @@ -1,4 +1,4 @@ -package encoder_test +package cbor_test import ( "bytes" @@ -7,7 +7,7 @@ import ( "testing" "github.com/NethermindEth/juno/core/felt" - "github.com/NethermindEth/juno/encoder" + "github.com/NethermindEth/juno/utils/cbor/v1" "github.com/stretchr/testify/require" ) @@ -39,7 +39,7 @@ func generateTestStructSlice() []testStruct { } type testCase interface { - encode(*testing.T, encoder.Encoder) + encode(*testing.T, cbor.Encoder) assert(*testing.T, []byte) []byte } @@ -51,7 +51,7 @@ func testData[T any](expected T) testCase { return testCaseData[T]{expected: expected} } -func (c testCaseData[T]) encode(t *testing.T, encoder encoder.Encoder) { +func (c testCaseData[T]) encode(t *testing.T, encoder cbor.Encoder) { t.Helper() require.NoError(t, encoder.Encode(c.expected)) } @@ -59,7 +59,7 @@ func (c testCaseData[T]) encode(t *testing.T, encoder encoder.Encoder) { func (c testCaseData[T]) assert(t *testing.T, data []byte) []byte { t.Helper() var actual T - remaining, err := encoder.UnmarshalFirst(data, &actual) + remaining, err := cbor.UnmarshalFirst(data, &actual) require.NoError(t, err) require.Equal(t, c.expected, actual) return remaining @@ -67,7 +67,7 @@ func (c testCaseData[T]) assert(t *testing.T, data []byte) []byte { func TestUnmarshalFirst(t *testing.T) { var buf bytes.Buffer - encoder := encoder.NewEncoder(&buf) + encoder := cbor.NewEncoder(&buf) testCases := []testCase{ testData(felt.Random[felt.Felt]()), diff --git a/vm/init_test.go b/vm/init_test.go index e8dd094955..08849a57f9 100644 --- a/vm/init_test.go +++ b/vm/init_test.go @@ -1,5 +1,5 @@ package vm_test import ( - _ "github.com/NethermindEth/juno/encoder/registry" + _ "github.com/NethermindEth/juno/utils/cbor/registry" )