diff --git a/core/state.go b/core/state.go index 27c20f0572..3b9d080ec5 100644 --- a/core/state.go +++ b/core/state.go @@ -142,8 +142,7 @@ func (s *State) globalTrie(bucket db.Bucket, newTrie trie.NewTrieFunc) (*trie.Tr var rootKey *trie.BitArray // TODO: use value instead of pointer err := s.txn.Get(rootKeyDBKey, func(val []byte) error { rootKey = new(trie.BitArray) - rootKey.UnmarshalBinary(val) - return nil + return rootKey.UnmarshalBinary(val) }) // if some error other than "not found" diff --git a/core/trie/bitarray.go b/core/trie/bitarray.go index b2bf952693..d1d28eab73 100644 --- a/core/trie/bitarray.go +++ b/core/trie/bitarray.go @@ -38,7 +38,8 @@ func NewBitArray(length uint8, val uint64) BitArray { // Returns the felt representation of the bit array. func (b *BitArray) Felt() felt.Felt { var f felt.Felt - f.SetBytes(b.Bytes()) + bt := b.Bytes() + f.SetBytes(bt[:]) return f } @@ -47,7 +48,7 @@ func (b *BitArray) Len() uint8 { } // Returns the bytes representation of the bit array in big endian format -func (b *BitArray) Bytes() []byte { +func (b *BitArray) Bytes() [32]byte { var res [32]byte binary.BigEndian.PutUint64(res[0:8], b.words[3]) @@ -55,7 +56,7 @@ func (b *BitArray) Bytes() []byte { binary.BigEndian.PutUint64(res[16:24], b.words[1]) binary.BigEndian.PutUint64(res[24:32], b.words[0]) - return res[:] + return res } // Sets the bit array to the least significant 'n' bits of x. @@ -512,15 +513,17 @@ func (b *BitArray) Copy() BitArray { // Returns the encoded string representation of the bit array. func (b *BitArray) EncodedString() string { var res []byte + bt := b.Bytes() res = append(res, b.len) - res = append(res, b.Bytes()...) + res = append(res, bt[:]...) return string(res) } // Returns a string representation of the bit array. // This is typically used for logging or debugging. func (b *BitArray) String() string { - return fmt.Sprintf("(%d) %s", b.len, hex.EncodeToString(b.Bytes())) + bt := b.Bytes() + return fmt.Sprintf("(%d) %s", b.len, hex.EncodeToString(bt[:])) } func (b *BitArray) setFelt(f *felt.Felt) { diff --git a/core/trie/bitarray_test.go b/core/trie/bitarray_test.go index 123ed52441..4786eee2e6 100644 --- a/core/trie/bitarray_test.go +++ b/core/trie/bitarray_test.go @@ -97,7 +97,7 @@ func TestBytes(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := tt.ba.Bytes() - if !bytes.Equal(got, tt.want[:]) { + if !bytes.Equal(got[:], tt.want[:]) { t.Errorf("BitArray.Bytes() = %v, want %v", got, tt.want) } diff --git a/migration/migration_pkg_test.go b/migration/migration_pkg_test.go index 9618ce4d6f..2b632d607a 100644 --- a/migration/migration_pkg_test.go +++ b/migration/migration_pkg_test.go @@ -261,10 +261,7 @@ func TestMigrateTrieRootKeysFromBitsetToTrieKeys(t *testing.T) { require.NoError(t, migrateTrieRootKeysFromBitsetToTrieKeys(memTxn, key, bsBytes, &utils.Mainnet)) var trieKey trie.BitArray - err = memTxn.Get(key, func(data []byte) error { - trieKey.UnmarshalBinary(data) - return nil - }) + err = memTxn.Get(key, trieKey.UnmarshalBinary) require.NoError(t, err) require.Equal(t, bs.Len(), uint(trieKey.Len())) require.Equal(t, felt.Zero, trieKey.Felt())