Skip to content

Commit

Permalink
Bug (Fixed) (#631)
Browse files Browse the repository at this point in the history
* wip - bug - passing some cases

* wip

* fix(bug): logic matching as closely the zkevm bug

---------

Co-authored-by: Kamen Stoykov <k.stoykov@razorlabs.com>
  • Loading branch information
revitteth and kstoykov authored Jun 20, 2024
1 parent c164869 commit 85bbd67
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 86 deletions.
6 changes: 3 additions & 3 deletions cmd/hack/rpc_checker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ func compareReceipts(nodeURL1, nodeURL2 string, txHashes []string) (bool, error)
go run cmd/hack/rpc_checker/main.go -node1=http://your-node1-url -node2=http://your-node2-url -fromBlock=3000000 -step=1000 -compare-receipts=true
*/
func main() {
nodeURL1 := flag.String("node1", "http://0.0.0.0:8545", "First node URL")
nodeURL1 := flag.String("node1", "http://0.0.0.0:8123", "First node URL")
nodeURL2 := flag.String("node2", "https://rpc.cardona.zkevm-rpc.com", "Second node URL")
compareReceiptsFlag := flag.Bool("compare-receipts", false, "Compare receipts for transactions in the block")
fromBlock := flag.Int("fromBlock", 1, "Starting block number")
step := flag.Int("step", 1000, "Block number increment")
fromBlock := flag.Int("fromBlock", 3816916, "Starting block number")
step := flag.Int("step", 1, "Block number increment")
flag.Parse()

blockNumber := *fromBlock
Expand Down
116 changes: 77 additions & 39 deletions core/vm/instructions_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/log/v3"
"strings"
)

func opCallDataLoad_zkevmIncompatible(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
Expand Down Expand Up @@ -198,7 +196,8 @@ func makeLog_zkevm(size int, logIndexPerTx bool) executionFunc {
\/
*/
var err error
d, _, err = applyHexPadBug(d, mSize.Uint64(), blockNo)

d, err = applyHexPadBug(d, int(mSize.Uint64()), blockNo)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -231,45 +230,84 @@ func makeLog_zkevm(size int, logIndexPerTx bool) executionFunc {
}
}

func applyHexPadBug(d []byte, msInt, blockNo uint64) ([]byte, bool, error) {
dataHex := hex.EncodeToString(d)
bug := false
l := len(dataHex)

// for the bug to be present the data must have length, be indivisible by the mSize, or shorter/longer than 64
if l > 0 && (uint64(l)%(msInt*2) != 0 || l != 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 applyHexPadBug(d []byte, msInt int, blockNo uint64) ([]byte, error) {
fullMs := msInt

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)
}
var dLastWord []byte
if len(d) <= 32 {
dLastWord = append(d, make([]byte, 32-len(d))...)
d = []byte{}
} else {
dLastWord, msInt = getLastWordBytes(d, fullMs)
d = d[:len(d)-len(dLastWord)]
}

if len(lastWord) > 0 && lastWord[0] == '0' && lastWord[1] != '0' && len(lastWord) != 64 {
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, "")
dataHex := hex.EncodeToString(dLastWord)

dataHex = appendZeros(dataHex, 64)

for len(dataHex) > 0 && dataHex[0] == '0' {
dataHex = dataHex[1:]
}

if len(dataHex) < msInt*2 {
dataHex = prependZeros(dataHex, msInt*2)
}
outputStr := takeFirstN(dataHex, msInt*2)

op, err := hex.DecodeString(outputStr)
if err != nil {
return nil, err
}
d = append(d, op...)

d = d[:fullMs]

return d, nil
}

func min(a, b uint64) uint64 {
if a < b {
return a
}
return b
}

func getLastWordBytes(data []byte, originalMsInt int) ([]byte, int) {
wordLength := 32
dataLength := len(data)

remainderLength := dataLength % wordLength
if remainderLength == 0 {
return data[dataLength-wordLength:], 32
}

toRemove := dataLength / wordLength

msInt := originalMsInt - (toRemove * wordLength)

return data[dataLength-remainderLength:], msInt
}

func prependZeros(data string, size int) string {
for len(data) < size {
data = "0" + data
}
return data
}

func takeFirstN(data string, n int) string {
if len(data) < n {
return data
}
return data[:n]
}

func appendZeros(dataHex string, targetLength int) string {
for len(dataHex) < targetLength {
dataHex += "0"
}
d, err := hex.DecodeString(dataHex)
return d, bug, err
return dataHex
}

func opCreate_zkevm(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) {
Expand Down
68 changes: 24 additions & 44 deletions core/vm/instructions_zkevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,80 +20,60 @@ func TestApplyHexPadBug(t *testing.T) {
type testScenario struct {
data string
mSize int
bug bool
expected string
}

scenarios := map[string]testScenario{
// expected verified via zkevm node rpc
"cardona-block-3177498": {
"cardona-bug-block-3177498": {
data: "0x010203",
mSize: 3,
bug: true,
expected: "102030",
},
"longHexWithBug": {
data: "0x0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f",
mSize: 64,
bug: true,
expected: "0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f010230405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0",
},
"singleByteHexWithoutBug": {
data: "0x11",
mSize: 1,
bug: false,
expected: "11",
},
"longHexWithoutBug1": {
data: "0x00000000000000000000000000000000000000000000000000000000000001ff",
mSize: 32,
bug: false,
expected: "00000000000000000000000000000000000000000000000000000000000001ff",
"another": {
data: "0x100000000000000000000000000000000000000000000000000000000000000",
mSize: 1,
expected: "10",
},
"longHexWithoutBug2": {
data: "0x00000000000000000000000000000000000000000000000000005af3107a4000",
mSize: 32,
bug: false,
expected: "00000000000000000000000000000000000000000000000000005af3107a4000",
"another1": {
data: "0x1020000000000000000000000000000000000000000000000000000000000",
mSize: 3,
expected: "102000",
},
"randomHexWithoutBug": {
data: "0x060303e606c27f9cddd90a7f129f525c83a0be7108fd5209174a77ffa7809e1c",
"cardona-897048": {
data: "0x0000000000000000000000000000000000000000000000000000000000000001",
mSize: 32,
bug: false,
expected: "060303e606c27f9cddd90a7f129f525c83a0be7108fd5209174a77ffa7809e1c",
expected: "0000000000000000000000000000000000000000000000000000000000000001",
},
// expected verified via zkevm node rpc
"cardona-901721": {
data: "0x000c0d08908319bb1f124d95d1e890847956013e989b3a0d8cbcb3a923dc12010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008908319bb1f124d95d1e890847956013e989b3a0d8cbcb3a923dc81f23aa91d",
mSize: 128,
bug: false,
expected: "000c0d08908319bb1f124d95d1e890847956013e989b3a0d8cbcb3a923dc12010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008908319bb1f124d95d1e890847956013e989b3a0d8cbcb3a923dc81f23aa91d",
"cardona-896194": {
data: "0x0000000000000000000000000000000000000000000000010000000000001e44000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009af3049dd15616fd627a35563b5282bea5c32e2000000000000000000000000000000000000000000000000000005af3107a4000",
mSize: 160,
expected: "0000000000000000000000000000000000000000000000010000000000001e44000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009af3049dd15616fd627a35563b5282bea5c32e2000000000000000000000000000000000000000000000000000005af3107a4000",
},
// expected verified via zkevm node rpc
"cardona-1498495": {
data: "0x0000000000000000000000000000000000000000000000000000000005f7cf60000000000000000000000000e6386158ecc340d79439f0398a1085502c13993b00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000140000000000000000000000067a061fb72554db38869a048db9d915600000bc70200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000005f5ca880000000000000000000000000000000000000000000000000000000005f5d7820000000000000000000000000000000000000000000000000000000005f7cf600000000000000000000000000000000000000000000000000000000005f7cf6000000000000000000000000000000000000000000000000000000000000000040300010200000000000000000000000000000000000000000000000000000000",
mSize: 384,
bug: false,
expected: "0000000000000000000000000000000000000000000000000000000005f7cf60000000000000000000000000e6386158ecc340d79439f0398a1085502c13993b00000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000140000000000000000000000067a061fb72554db38869a048db9d915600000bc70200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000005f5ca880000000000000000000000000000000000000000000000000000000005f5d7820000000000000000000000000000000000000000000000000000000005f7cf600000000000000000000000000000000000000000000000000000000005f7cf6000000000000000000000000000000000000000000000000000000000000000040300010200000000000000000000000000000000000000000000000000000000",
"cardona-3816917": {
data: "0x0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f",
mSize: 60,
expected: "0102030405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f010230405060708090a0b0c0d0e0f0102030405060708090a0b0c0d0e0f0",
},
}

for name, scenario := range scenarios {
t.Run(name, func(t *testing.T) {
d := common.FromHex(scenario.data)
result, bug, err := applyHexPadBug(d, uint64(scenario.mSize), 0)

result, err := applyHexPadBug(d, 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)
}
if resultHex != scenario.expected {
t.Errorf("Expected %s but got %s", scenario.expected, resultHex)
}
})
}
Expand Down

0 comments on commit 85bbd67

Please sign in to comment.