forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* padding-in-log-bug * IF -> FOR
- Loading branch information
Showing
6 changed files
with
238 additions
and
190 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package types | ||
|
||
import ( | ||
"encoding/hex" | ||
|
||
libcommon "github.com/gateway-fm/cdk-erigon-lib/common" | ||
) | ||
|
||
func (_this *Log) Clone() *Log { | ||
address := libcommon.Address{} | ||
copy(address[:], _this.Address[:]) | ||
|
||
topics := make([]libcommon.Hash, len(_this.Topics)) | ||
for i, t := range _this.Topics { | ||
copy(topics[i][:], t[:]) | ||
} | ||
|
||
data := make([]byte, len(_this.Data)) | ||
copy(data, _this.Data) | ||
|
||
txHash := libcommon.Hash{} | ||
copy(txHash[:], _this.TxHash[:]) | ||
|
||
blockHash := libcommon.Hash{} | ||
copy(blockHash[:], _this.BlockHash[:]) | ||
|
||
return &Log{ | ||
Address: address, | ||
Topics: topics, | ||
Data: data, | ||
BlockNumber: _this.BlockNumber, | ||
TxHash: txHash, | ||
TxIndex: _this.TxIndex, | ||
BlockHash: blockHash, | ||
Index: _this.Index, | ||
Removed: _this.Removed, | ||
} | ||
} | ||
|
||
func (_this *Log) ApplyPaddingToLogsData(isForkId8, isForkId9 bool) { | ||
d := _this.Data | ||
mSize := len(d) | ||
|
||
if isForkId8 || isForkId9 { | ||
d = applyHexPadBug(d, mSize) | ||
} else { | ||
// [zkEvm] fill 0 at the end | ||
lenMod32 := mSize & 31 | ||
if lenMod32 != 0 { | ||
d = append(d, make([]byte, 32-lenMod32)...) | ||
} | ||
} | ||
|
||
_this.Data = d | ||
} | ||
|
||
func applyHexPadBug(d []byte, msInt int) []byte { | ||
fullMs := 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)] | ||
} | ||
|
||
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, _ := hex.DecodeString(outputStr) // there error is always nil here, because we prepare outputStr to have even length | ||
d = append(d, op...) | ||
d = d[:fullMs] | ||
|
||
return d | ||
} | ||
|
||
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" | ||
} | ||
return dataHex | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package types | ||
|
||
import ( | ||
"testing" | ||
|
||
"encoding/hex" | ||
|
||
"github.com/ledgerwatch/erigon/common" | ||
) | ||
|
||
func TestApplyHexPadBug(t *testing.T) { | ||
type testScenario struct { | ||
data string | ||
mSize int | ||
expected string | ||
} | ||
|
||
scenarios := map[string]testScenario{ | ||
"cardona-bug-block-3177498": { | ||
data: "0x010203", | ||
mSize: 3, | ||
expected: "102030", | ||
}, | ||
"singleByteHexWithoutBug": { | ||
data: "0x11", | ||
mSize: 1, | ||
expected: "11", | ||
}, | ||
"another": { | ||
data: "0x100000000000000000000000000000000000000000000000000000000000000", | ||
mSize: 1, | ||
expected: "10", | ||
}, | ||
"another1": { | ||
data: "0x1020000000000000000000000000000000000000000000000000000000000", | ||
mSize: 3, | ||
expected: "102000", | ||
}, | ||
"cardona-897048": { | ||
data: "0x0000000000000000000000000000000000000000000000000000000000000001", | ||
mSize: 32, | ||
expected: "0000000000000000000000000000000000000000000000000000000000000001", | ||
}, | ||
"cardona-896194": { | ||
data: "0x0000000000000000000000000000000000000000000000010000000000001e44000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009af3049dd15616fd627a35563b5282bea5c32e2000000000000000000000000000000000000000000000000000005af3107a4000", | ||
mSize: 160, | ||
expected: "0000000000000000000000000000000000000000000000010000000000001e44000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009af3049dd15616fd627a35563b5282bea5c32e2000000000000000000000000000000000000000000000000000005af3107a4000", | ||
}, | ||
"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 := applyHexPadBug(d, scenario.mSize) | ||
|
||
resultHex := hex.EncodeToString(result) | ||
|
||
if resultHex != scenario.expected { | ||
t.Errorf("Expected %s but got %s", scenario.expected, resultHex) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.