Skip to content

Commit

Permalink
all tests passed
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Dec 17, 2024
1 parent 2f2050a commit ad6a3ba
Show file tree
Hide file tree
Showing 15 changed files with 290 additions and 620 deletions.
9 changes: 5 additions & 4 deletions core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,11 @@ func (s *State) globalTrie(bucket db.Bucket, newTrie trie.NewTrieFunc) (*trie.Tr

// fetch root key
rootKeyDBKey := dbPrefix
var rootKey *trie.Key
var rootKey *trie.BitArray
err := s.txn.Get(rootKeyDBKey, func(val []byte) error {
rootKey = new(trie.Key)
return rootKey.UnmarshalBinary(val)
rootKey = new(trie.BitArray)
rootKey.UnmarshalBinary(val)
return nil
})

// if some error other than "not found"
Expand All @@ -169,7 +170,7 @@ func (s *State) globalTrie(bucket db.Bucket, newTrie trie.NewTrieFunc) (*trie.Tr

if resultingRootKey != nil {
var rootKeyBytes bytes.Buffer
_, marshalErr := resultingRootKey.WriteTo(&rootKeyBytes)
_, marshalErr := resultingRootKey.Write(&rootKeyBytes)
if marshalErr != nil {
return marshalErr
}
Expand Down
21 changes: 13 additions & 8 deletions core/trie/bitarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,13 @@ func (b *BitArray) EqualMSBs(x *BitArray) bool {
return true
}

var long, short *BitArray

long, short = b, x
if b.len < x.len {
long, short = x, b
// Compare only the first min(b.len, x.len) bits
minLen := b.len
if x.len < minLen {
minLen = x.len
}

return long.Rsh(long, long.len-short.len).Equal(short)
return new(BitArray).MSBs(b, minLen).Equal(new(BitArray).MSBs(x, minLen))
}

// LSBs sets b to the least significant 'n' bits of x.
Expand Down Expand Up @@ -229,8 +228,7 @@ func (b *BitArray) Rsh(x *BitArray, n uint8) *BitArray {
}

if n >= x.len {
x.clear()
return b.Set(x)
return b.clear()
}

switch {
Expand Down Expand Up @@ -277,6 +275,13 @@ func (b *BitArray) Xor(x, y *BitArray) *BitArray {

// Eq checks if two bit arrays are equal
func (b *BitArray) Equal(x *BitArray) bool {
// TODO(weiihann): this is really not a good thing to do...
if b == nil && x == nil {
return true
} else if b == nil || x == nil {
return false
}

return b.len == x.len &&
b.words[0] == x.words[0] &&
b.words[1] == x.words[1] &&
Expand Down
186 changes: 0 additions & 186 deletions core/trie/key.go

This file was deleted.

Loading

0 comments on commit ad6a3ba

Please sign in to comment.