Skip to content

Commit

Permalink
fix globalIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
ARR552 committed Dec 20, 2024
1 parent 6489a6b commit 3794525
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
2 changes: 1 addition & 1 deletion etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ func DecodeGlobalIndex(globalIndex *big.Int) (bool, uint32, uint32, error) {
}
mainnetFlag := big.NewInt(0).SetBytes([]byte{gIBytes[23]}).Uint64() == 1
rollupIndex := big.NewInt(0).SetBytes(gIBytes[24:28])
localRootIndex := big.NewInt(0).SetBytes(gIBytes[29:32])
localRootIndex := big.NewInt(0).SetBytes(gIBytes[28:32])
if rollupIndex.Uint64() > math.MaxUint32 {
return false, 0, 0, fmt.Errorf("invalid rollupIndex length. Should be fit into uint32 type")
}
Expand Down
50 changes: 47 additions & 3 deletions etherman/etherman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package etherman

import (
"context"
"math"
"math/big"
"testing"

Expand Down Expand Up @@ -181,6 +182,18 @@ func TestDecodeGlobalIndex(t *testing.T) {
assert.Equal(t, true, mainnetFlag)
assert.Equal(t, uint32(0), rollupIndex)
assert.Equal(t, uint32(0), localExitRootIndex)

globalIndex, _ = big.NewInt(0).SetString("18446744073709551615", 0)

gi = globalIndex.FillBytes(buf[:])
for _, n := range gi {
t.Logf("%08b ", n)
}
mainnetFlag, rollupIndex, localExitRootIndex, err = DecodeGlobalIndex(globalIndex)
require.NoError(t, err)
assert.Equal(t, false, mainnetFlag)
assert.Equal(t, uint32(math.MaxUint32), rollupIndex)
assert.Equal(t, uint32(math.MaxUint32), localExitRootIndex)
}

func TestVerifyBatchEvent(t *testing.T) {
Expand Down Expand Up @@ -238,7 +251,9 @@ func TestGenerateGlobalIndex(t *testing.T) {
mainnetFlag, rollupIndex, localExitRootIndex := false, uint32(1), uint32(11)
globalIndexGenerated := GenerateGlobalIndex(mainnetFlag, rollupIndex, localExitRootIndex)
t.Log("First test number:")
for _, n := range globalIndexGenerated.Bytes() {
var buf [32]byte
gIBytes := globalIndexGenerated.FillBytes(buf[:])
for _, n := range gIBytes {
t.Logf("%08b ", n)
}
assert.Equal(t, globalIndex, globalIndexGenerated)
Expand All @@ -247,7 +262,8 @@ func TestGenerateGlobalIndex(t *testing.T) {
mainnetFlag, rollupIndex, localExitRootIndex = false, uint32(2), uint32(12)
globalIndexGenerated = GenerateGlobalIndex(mainnetFlag, rollupIndex, localExitRootIndex)
t.Log("Second test number:")
for _, n := range globalIndexGenerated.Bytes() {
gIBytes = globalIndexGenerated.FillBytes(buf[:])
for _, n := range gIBytes {
t.Logf("%08b ", n)
}
assert.Equal(t, globalIndex, globalIndexGenerated)
Expand All @@ -256,7 +272,35 @@ func TestGenerateGlobalIndex(t *testing.T) {
mainnetFlag, rollupIndex, localExitRootIndex = true, uint32(0), uint32(11)
globalIndexGenerated = GenerateGlobalIndex(mainnetFlag, rollupIndex, localExitRootIndex)
t.Log("Third test number:")
for _, n := range globalIndexGenerated.Bytes() {
gIBytes = globalIndexGenerated.FillBytes(buf[:])
for _, n := range gIBytes {
t.Logf("%08b ", n)
}
assert.Equal(t, globalIndex, globalIndexGenerated)

globalIndex, _ = big.NewInt(0).SetString("18446744073709551615", 0)
globalIndexGenerated = GenerateGlobalIndex(false, math.MaxUint32, math.MaxUint32)
t.Log("Fourth test number:")
gIBytes = globalIndexGenerated.FillBytes(buf[:])
for _, n := range gIBytes {
t.Logf("%08b ", n)
}
assert.Equal(t, globalIndex, globalIndexGenerated)

globalIndex = big.NewInt(0)
globalIndexGenerated = GenerateGlobalIndex(false, uint32(0), uint32(0))
t.Log("Fourth test number:")
gIBytes = globalIndexGenerated.FillBytes(buf[:])
for _, n := range gIBytes {
t.Logf("%08b ", n)
}
assert.Equal(t, globalIndex.String(), globalIndexGenerated.String())

globalIndex, _ = big.NewInt(0).SetString("18446744073709551616", 0)
globalIndexGenerated = GenerateGlobalIndex(true, uint32(0), uint32(0))
t.Log("Fourth test number:")
gIBytes = globalIndexGenerated.FillBytes(buf[:])
for _, n := range gIBytes {
t.Logf("%08b ", n)
}
assert.Equal(t, globalIndex, globalIndexGenerated)
Expand Down

0 comments on commit 3794525

Please sign in to comment.