Skip to content

Commit

Permalink
refactor: make IsL1MessageSkipped public (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo authored Oct 21, 2024
1 parent e971fc9 commit 8c5d2f0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
10 changes: 10 additions & 0 deletions encoding/bitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,13 @@ func DecodeBitmap(skippedL1MessageBitmap []byte, totalL1MessagePopped int) ([]*b
}
return skippedBitmap, nil
}

// IsL1MessageSkipped checks if the L1 message at the given index is skipped.
func IsL1MessageSkipped(skippedBitmap []*big.Int, index uint64) bool {
if index >= uint64(len(skippedBitmap))*256 {
return false
}
quo := index / 256
rem := index % 256
return skippedBitmap[quo].Bit(int(rem)) == 1
}
30 changes: 10 additions & 20 deletions encoding/bitmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package encoding

import (
"encoding/hex"
"math/big"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -16,25 +15,16 @@ func TestDecodeBitmap(t *testing.T) {
decodedBitmap, err := DecodeBitmap(skippedL1MessageBitmap, 42)
assert.NoError(t, err)

isL1MessageSkipped := func(skippedBitmap []*big.Int, index uint64) bool {
if index >= uint64(len(skippedBitmap))*256 {
return false
}
quo := index / 256
rem := index % 256
return skippedBitmap[quo].Bit(int(rem)) == 1
}

assert.True(t, isL1MessageSkipped(decodedBitmap, 0))
assert.True(t, isL1MessageSkipped(decodedBitmap, 9))
assert.False(t, isL1MessageSkipped(decodedBitmap, 10))
assert.True(t, isL1MessageSkipped(decodedBitmap, 11))
assert.True(t, isL1MessageSkipped(decodedBitmap, 36))
assert.False(t, isL1MessageSkipped(decodedBitmap, 37))
assert.False(t, isL1MessageSkipped(decodedBitmap, 38))
assert.False(t, isL1MessageSkipped(decodedBitmap, 39))
assert.False(t, isL1MessageSkipped(decodedBitmap, 40))
assert.False(t, isL1MessageSkipped(decodedBitmap, 41))
assert.True(t, IsL1MessageSkipped(decodedBitmap, 0))
assert.True(t, IsL1MessageSkipped(decodedBitmap, 9))
assert.False(t, IsL1MessageSkipped(decodedBitmap, 10))
assert.True(t, IsL1MessageSkipped(decodedBitmap, 11))
assert.True(t, IsL1MessageSkipped(decodedBitmap, 36))
assert.False(t, IsL1MessageSkipped(decodedBitmap, 37))
assert.False(t, IsL1MessageSkipped(decodedBitmap, 38))
assert.False(t, IsL1MessageSkipped(decodedBitmap, 39))
assert.False(t, IsL1MessageSkipped(decodedBitmap, 40))
assert.False(t, IsL1MessageSkipped(decodedBitmap, 41))

_, err = DecodeBitmap([]byte{0x00}, 8)
assert.Error(t, err)
Expand Down

0 comments on commit 8c5d2f0

Please sign in to comment.