Skip to content

Commit

Permalink
feat(bug): add test scenarios for the bug
Browse files Browse the repository at this point in the history
  • Loading branch information
revitteth committed Jun 14, 2024
1 parent b2775fb commit 8a8b3ce
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 57 deletions.
4 changes: 2 additions & 2 deletions core/vm/gas_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestEIP2200(t *testing.T) {
}
vmenv := NewEVM(vmctx, evmtypes.TxContext{}, s, params.AllProtocolChanges, Config{ExtraEips: []int{2200}})

_, gas, err := vmenv.Call(AccountRef(libcommon.Address{}), address, nil, tt.gaspool, new(uint256.Int), false /* bailout */)
_, gas, err := vmenv.Call(AccountRef(libcommon.Address{}), address, nil, tt.gaspool, new(uint256.Int), false /* bailout */, 0)
if !errors.Is(err, tt.failure) {
t.Errorf("test %d: failure mismatch: have %v, want %v", i, err, tt.failure)
}
Expand Down Expand Up @@ -157,7 +157,7 @@ func TestCreateGas(t *testing.T) {
vmenv := NewEVM(vmctx, evmtypes.TxContext{}, s, params.TestChainConfig, config)

var startGas uint64 = math.MaxUint64
_, gas, err := vmenv.Call(AccountRef(libcommon.Address{}), address, nil, startGas, new(uint256.Int), false /* bailout */)
_, gas, err := vmenv.Call(AccountRef(libcommon.Address{}), address, nil, startGas, new(uint256.Int), false /* bailout */, 0)
if err != nil {
t.Errorf("test %d execution failed: %v", i, err)
}
Expand Down
91 changes: 39 additions & 52 deletions core/vm/instructions_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,44 +197,10 @@ func makeLog_zkevm(size int, logIndexPerTx bool) executionFunc {
\ /
\/
*/
dataHex := hex.EncodeToString(d)

if len(dataHex) > 0 && uint64(len(dataHex))%(mSize.Uint64()*2) != 0 {
msInt := mSize.Uint64()

words := []string{}
for i := 0; i < len(dataHex); i += 64 {
end := i + 64
if end > len(dataHex) {
end = len(dataHex)
}
word := dataHex[i:end]
words = append(words, word)
}

var lastWord string
lastWordIndex := 0
if len(words) > 0 {
lastWordIndex = len(words) - 1
lastWord = words[lastWordIndex]
} else {
log.Warn("Words is empty", "block", blockNo, "data", dataHex, "size", msInt)
}

if len(lastWord) > 0 && lastWord[0] == '0' && lastWord[1] != '0' {
tempLastWord := lastWord[1:]
if uint64(len(tempLastWord)) < msInt*2 {
log.Warn("Possible bug detected in log data", "block", blockNo, "data", dataHex, "size", msInt)
lastWord = tempLastWord + "0"
}
}
words[lastWordIndex] = lastWord
dataHex = strings.Join(words, "")
var err error
d, err = hex.DecodeString(dataHex)
if err != nil {
return nil, err
}
var err error
d, _, err = applyHexPadBug(d, mSize.Uint64(), blockNo)
if err != nil {
return nil, err
}
/*
\ /
Expand Down Expand Up @@ -265,22 +231,43 @@ func makeLog_zkevm(size int, logIndexPerTx bool) executionFunc {
}
}

func prependZerosHex(s string, length int) string {
for len(s) < length {
s = "0" + s
}
return s
}
func applyHexPadBug(d []byte, msInt, blockNo uint64) ([]byte, bool, error) {
dataHex := hex.EncodeToString(d)
bug := false
l := len(dataHex)
if l > 0 && (uint64(len(dataHex))%(msInt*2) != 0 || len(dataHex) != 64) {
words := []string{}
for i := 0; i < l; i += 64 {
end := i + 64
if end > l {
end = l
}
word := dataHex[i:end]
words = append(words, word)
}

func appendZerosHex(s string, length int) string {
for len(s) < length {
s = s + "0"
}
return s
}
var lastWord string
lastWordIndex := 0
if len(words) > 0 {
lastWordIndex = len(words) - 1
lastWord = words[lastWordIndex]
} else {
log.Warn("Words is empty", "block", blockNo, "data", dataHex, "size", msInt)
}

func appendOneZero(s string) string {
return s + "0"
if len(lastWord) > 0 && lastWord[0] == '0' && lastWord[1] != '0' {
tempLastWord := lastWord[1:]
if uint64(len(tempLastWord)) < msInt*2 {
log.Warn("Possible bug detected in log data", "block", blockNo, "data", dataHex, "size", msInt)
lastWord = tempLastWord + "0"
bug = true
}
}
words[lastWordIndex] = lastWord
dataHex = strings.Join(words, "")
}
d, err := hex.DecodeString(dataHex)
return d, bug, err
}

func opCreate_zkevm(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
Expand Down
107 changes: 104 additions & 3 deletions core/vm/instructions_zkevm_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
//go:build notzkevm
// +build notzkevm

package vm

import (
Expand All @@ -16,8 +13,83 @@ import (
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
"github.com/ledgerwatch/erigon/core/vm/stack"
"github.com/ledgerwatch/erigon/params"
"encoding/hex"
)

func TestApplyHexPadBug(t *testing.T) {
type testScenario struct {
hexString string
mSize int
bug bool
expected string
}

scenarios := map[string]testScenario{
"cardona-block-3177498": {
hexString: "0x010203",
mSize: 3,
bug: true,
expected: "102030",
},
"longHexWithBug": {
hexString: "0x0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f",
mSize: 64,
bug: true,
expected: "0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f010230405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0",
},
"singleByteHexWithoutBug": {
hexString: "0x11",
mSize: 1,
bug: false,
expected: "11",
},
"longHexWithoutBug1": {
hexString: "0x00000000000000000000000000000000000000000000000000000000000001ff",
mSize: 32,
bug: false,
expected: "00000000000000000000000000000000000000000000000000000000000001ff",
},
"longHexWithoutBug2": {
hexString: "0x00000000000000000000000000000000000000000000000000005af3107a4000",
mSize: 32,
bug: false,
expected: "00000000000000000000000000000000000000000000000000005af3107a4000",
},
"randomHexWithoutBug": {
hexString: "0x060303e606c27f9cddd90a7f129f525c83a0be7108fd5209174a77ffa7809e1c",
mSize: 32,
bug: false,
expected: "060303e606c27f9cddd90a7f129f525c83a0be7108fd5209174a77ffa7809e1c",
},
"veryLongHexWithBug": {
hexString: "0000000000000000000000000000000000000000000000000000000000009ce10000000000000000000000005071d96f0251884debe6f2e02fa610df183859e3000000000000000000000000000000000000000000000000000000000000000200000000000000000000000061d79bc5dc25e6c4aee44b34cfcdfb47f0d984100d1dcde1acde7f8bf8e5c1a9f9a3f2394500fa3fcf2620acee012d87fa860745",
mSize: 160,
bug: true,
expected: "0000000000000000000000000000000000000000000000000000000000009ce10000000000000000000000005071d96f0251884debe6f2e02fa610df183859e3000000000000000000000000000000000000000000000000000000000000000200000000000000000000000061d79bc5dc25e6c4aee44b34cfcdfb47f0d98410d1dcde1acde7f8bf8e5c1a9f9a3f2394500fa3fcf2620acee012d87fa8607450",
},
}

for name, scenario := range scenarios {
t.Run(name, func(t *testing.T) {
inputBytes := common.FromHex(scenario.hexString)
result, bug, err := applyHexPadBug(inputBytes, uint64(scenario.mSize), 0)
if err != nil {
t.Fatalf("Error in applyHexPadBug: %v", err)
}

resultHex := hex.EncodeToString(result)

if scenario.bug != bug {
t.Errorf("Expected %t but got %t", scenario.bug, bug)
} else {
if resultHex != scenario.expected {
t.Errorf("Expected %s but got %s", scenario.expected, resultHex)
}
}
})
}
}

func TestBlockhashV2(t *testing.T) {

gethashFn := func(bn uint64) libcommon.Hash {
Expand Down Expand Up @@ -126,6 +198,35 @@ func TestExtCodeHashV2(t *testing.T) {

type TestIntraBlockState struct{}

func (ibs TestIntraBlockState) HasLiveAccount(addr libcommon.Address) bool {
//TODO implement me
panic("implement me")
}

func (ibs TestIntraBlockState) HasLiveState(addr libcommon.Address, key *libcommon.Hash) bool {
//TODO implement me
panic("implement me")
}

func (ibs TestIntraBlockState) AddLog(log *types.Log) {
//TODO implement me
panic("implement me")
}

func (ibs TestIntraBlockState) GetLogs(hash libcommon.Hash) []*types.Log {
//TODO implement me
panic("implement me")
}

func (ibs TestIntraBlockState) GetBlockStateRoot(blockNum uint64) libcommon.Hash {
return libcommon.BigToHash(new(big.Int).SetUint64(blockNum))
}

func (ibs TestIntraBlockState) GetBlockNumber() *uint256.Int {
//TODO implement me
panic("implement me")
}

func (ibs TestIntraBlockState) CreateAccount(libcommon.Address, bool) {}
func (ibs TestIntraBlockState) GetTxCount() (uint64, error) { return 0, nil }

Expand Down

0 comments on commit 8a8b3ce

Please sign in to comment.