Skip to content

Commit

Permalink
Bytes() return fixed array
Browse files Browse the repository at this point in the history
minor

minor

fix
  • Loading branch information
weiihann committed Jan 17, 2025
1 parent 70bef42 commit b67ff0a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
3 changes: 1 addition & 2 deletions core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 8 additions & 5 deletions core/trie/bitarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -47,15 +48,15 @@ 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])
binary.BigEndian.PutUint64(res[8:16], b.words[2])
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.
Expand Down Expand Up @@ -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)

Check warning on line 519 in core/trie/bitarray.go

View check run for this annotation

Codecov / codecov/patch

core/trie/bitarray.go#L514-L519

Added lines #L514 - L519 were not covered by tests
}

// 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[:]))

Check warning on line 526 in core/trie/bitarray.go

View check run for this annotation

Codecov / codecov/patch

core/trie/bitarray.go#L524-L526

Added lines #L524 - L526 were not covered by tests
}

func (b *BitArray) setFelt(f *felt.Felt) {
Expand Down
2 changes: 1 addition & 1 deletion core/trie/bitarray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
5 changes: 1 addition & 4 deletions migration/migration_pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit b67ff0a

Please sign in to comment.