Skip to content

Commit

Permalink
Bytes() return fixed array
Browse files Browse the repository at this point in the history
minor
  • Loading branch information
weiihann committed Jan 17, 2025
1 parent 70bef42 commit 8c495e6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 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)
}

// 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) {
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

0 comments on commit 8c495e6

Please sign in to comment.