Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/zkevm' into zkevm-2.60
Browse files Browse the repository at this point in the history
  • Loading branch information
cffls committed Jul 12, 2024
2 parents bcea43d + a22ead3 commit 0c6a84a
Show file tree
Hide file tree
Showing 66 changed files with 1,683 additions and 673 deletions.
13 changes: 13 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
**/*_test.go
**/*.a
**/*.dylib
**/*.o
**/*.dSYM

build
tests/testdata
cmd/prometheus
vendor

cache.db
.git
3 changes: 2 additions & 1 deletion .github/workflows/ci_zkevm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ jobs:
cd bridge
mkdir tmp
cat <<EOF > ./tmp/test.toml
TestAddrPrivate= "0x12d7de8621a77640c9241b2595ba78ce443d05e94090365ab3bb5e19df82c625"
TestL1AddrPrivate="0x12d7de8621a77640c9241b2595ba78ce443d05e94090365ab3bb5e19df82c625"
TestL2AddrPrivate="0x12d7de8621a77640c9241b2595ba78ce443d05e94090365ab3bb5e19df82c625"
[ConnectionConfig]
L1NodeURL="${ETH_RPC_URL}"
L2NodeURL="${L2_RPC_URL}"
Expand Down
23 changes: 4 additions & 19 deletions .github/workflows/hourly-env-checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,8 @@ on:
workflow_dispatch:

jobs:
test-node:
run-checks:
runs-on: ubuntu-latest
continue-on-error: true
strategy:
matrix:
include:
- name: "Integration 5 - Legacy"
network: "integration-5"
rpc: "http://34.175.214.161:8505"
- name: "Integration 5 - Erigon RPC"
network: "integration-5"
rpc: "http://34.175.214.161:8500"
- name: "Integration 5 - Erigon Sequencer"
network: "integration-5"
rpc: "http://34.175.214.161:8005"
steps:
- name: Checkout code
uses: actions/checkout@v3
Expand All @@ -30,8 +17,6 @@ jobs:
with:
go-version: '1.20'

- name: Build Go application
run: go build -o check_node

- name: Run tests
run: ./check_node -rpcURL=${{ matrix.rpc }} -nodeName=${{ matrix.name }}
- name: Run checks
working-directory: ./zk/debug_tools/env-checker
run: go run main.go -envFile envs.json
2 changes: 1 addition & 1 deletion .github/workflows/nightly-bridge-erc20.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

steps:
- name: Clone bridge repository
run: git clone --recurse-submodules -j8 https://github.com/0xPolygonHermez/zkevm-bridge-service.git -b feature/test_bridge_messages_real_netowrk bridge
run: git clone --recurse-submodules -j8 https://github.com/0xPolygonHermez/zkevm-bridge-service.git -b feature/test_bridge_messages_real_network-v2 bridge

- name: Build docker image
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nightly-bridge-msg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

steps:
- name: Clone bridge repository
run: git clone --recurse-submodules -j8 https://github.com/0xPolygonHermez/zkevm-bridge-service.git -b feature/test_bridge_messages_real_netowrk bridge
run: git clone --recurse-submodules -j8 https://github.com/0xPolygonHermez/zkevm-bridge-service.git -b develop bridge

- name: Build docker image
run: |
Expand Down
1 change: 0 additions & 1 deletion cmd/hack/rpc_cache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
}

url, _ := url2.Parse(endpoint)
fmt.Println("Fetching from upstream", url.Host)

resp, err := http.Post(endpoint, "application/json", bytes.NewBuffer(body))
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,11 @@ var (
Usage: "The URL of the data availability service",
Value: "",
}
DebugTimers = cli.BoolFlag{
Name: "debug.timers",
Usage: "Enable debug timers",
Value: false,
}
DebugNoSync = cli.BoolFlag{
Name: "debug.no-sync",
Usage: "Disable syncing",
Expand Down
87 changes: 51 additions & 36 deletions core/blockchain_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,41 +118,8 @@ func ExecuteBlockEphemerallyZk(
vmConfig.Tracer = nil
}

//[hack]TODO: remove this after bug is fixed
localReceipt := *receipt
if !chainConfig.IsForkID8Elderberry(blockNum) && errors.Is(execResult.Err, vm.ErrUnsupportedPrecompile) {
localReceipt.Status = 1
}

// forkid8 the poststate is empty
// forkid8 also fixed the bugs with logs and cumulative gas used
if !chainConfig.IsForkID8Elderberry(blockNum) {
// the stateroot in the transactions that comes from the datastream
// is the one after smart contract writes so it can't be used
// but since pre forkid7 blocks have 1 tx only, we can use the block root
if chainConfig.IsForkID7Etrog(blockNum) {
// receipt root holds the intermediate stateroot after the tx
intermediateState, err := roHermezDb.GetIntermediateTxStateRoot(blockNum, tx.Hash())
if err != nil {
return nil, err
}
receipt.PostState = intermediateState.Bytes()
} else {
receipt.PostState = header.Root.Bytes()
}

//[hack] log0 pre forkid8 are not included in the rpc logs
// also pre forkid8 comulative gas used is same as gas used
var fixedLogs types.Logs
for _, l := range receipt.Logs {
if len(l.Topics) == 0 && len(l.Data) == 0 {
continue
}
fixedLogs = append(fixedLogs, l)
}
receipt.Logs = fixedLogs
receipt.CumulativeGasUsed = receipt.GasUsed
}
localReceipt := CreateReceiptForBlockInfoTree(receipt, chainConfig, blockNum, execResult)
ProcessReceiptForBlockExecution(receipt, roHermezDb, chainConfig, blockNum, header, tx)

if err != nil {
if !vmConfig.StatelessExec {
Expand Down Expand Up @@ -182,7 +149,7 @@ func ExecuteBlockEphemerallyZk(

txInfos = append(txInfos, blockinfo.ExecutedTxInfo{
Tx: tx,
Receipt: &localReceipt,
Receipt: localReceipt,
EffectiveGasPrice: effectiveGasPricePercentage,
Signer: &txSender,
})
Expand Down Expand Up @@ -361,3 +328,51 @@ func FinalizeBlockExecutionWithHistoryWrite(

return newBlock, newTxs, newReceipt, nil
}

func CreateReceiptForBlockInfoTree(receipt *types.Receipt, chainConfig *chain.Config, blockNum uint64, execResult *ExecutionResult) *types.Receipt {
// [hack]TODO: remove this after bug is fixed
localReceipt := receipt.Clone()
if !chainConfig.IsForkID8Elderberry(blockNum) && errors.Is(execResult.Err, vm.ErrUnsupportedPrecompile) {
localReceipt.Status = 1
}

return localReceipt
}

func ProcessReceiptForBlockExecution(receipt *types.Receipt, roHermezDb state.ReadOnlyHermezDb, chainConfig *chain.Config, blockNum uint64, header *types.Header, tx types.Transaction) error {
// forkid8 the poststate is empty
// forkid8 also fixed the bugs with logs and cumulative gas used
if !chainConfig.IsForkID8Elderberry(blockNum) {
// the stateroot in the transactions that comes from the datastream
// is the one after smart contract writes so it can't be used
// but since pre forkid7 blocks have 1 tx only, we can use the block root
if chainConfig.IsForkID7Etrog(blockNum) {
// receipt root holds the intermediate stateroot after the tx
intermediateState, err := roHermezDb.GetIntermediateTxStateRoot(blockNum, tx.Hash())
if err != nil {
return err
}
receipt.PostState = intermediateState.Bytes()
} else {
receipt.PostState = header.Root.Bytes()
}

//[hack] log0 pre forkid8 are not included in the rpc logs
// also pre forkid8 comulative gas used is same as gas used
var fixedLogs types.Logs
for _, l := range receipt.Logs {
if len(l.Topics) == 0 && len(l.Data) == 0 {
continue
}
fixedLogs = append(fixedLogs, l)
}
receipt.Logs = fixedLogs
receipt.CumulativeGasUsed = receipt.GasUsed
}

for _, l := range receipt.Logs {
l.ApplyPaddingToLogsData(chainConfig.IsForkID8Elderberry(blockNum), chainConfig.IsForkID12Banana(blockNum))
}

return nil
}
123 changes: 123 additions & 0 deletions core/types/log_zkevm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package types

import (
"encoding/hex"

libcommon "github.com/ledgerwatch/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, isForkId12 bool) {
d := _this.Data
mSize := len(d)

if isForkId8 && !isForkId12 {
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
}
69 changes: 69 additions & 0 deletions core/types/log_zkevm_test.go
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)
}
})
}
}
Loading

0 comments on commit 0c6a84a

Please sign in to comment.