Skip to content

Commit

Permalink
Merge branch 'zkevm' into 1298-acl-access-control-list-augmentation
Browse files Browse the repository at this point in the history
  • Loading branch information
afa7789 authored Oct 17, 2024
2 parents b6ccaac + 56b084c commit 8be071a
Show file tree
Hide file tree
Showing 12 changed files with 548 additions and 15 deletions.
16 changes: 10 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ jobs:
needs: [prepare, build-amd64, build-arm64]
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to DockerHub
Expand All @@ -109,8 +107,14 @@ jobs:
password: ${{ env.DOCKERHUB_TOKEN }}
- name: Create and push manifest
run: |
docker buildx create --use
docker buildx build --push --platform linux/amd64,linux/arm64 --tag ${{ env.IMAGE_NAME }}:${{ needs.prepare.outputs.version }} --file Dockerfile .
docker buildx imagetools create \
--tag ${{ env.IMAGE_NAME }}:${{ needs.prepare.outputs.version }} \
${{ env.IMAGE_NAME }}:${{ needs.prepare.outputs.version }}-amd64 \
${{ env.IMAGE_NAME }}:${{ needs.prepare.outputs.version }}-arm64
if [ "${{ needs.prepare.outputs.latest_tag }}" == "true" ]; then
docker buildx build --push --platform linux/amd64,linux/arm64 --tag ${{ env.IMAGE_NAME }}:latest --file Dockerfile .
fi
docker buildx imagetools create \
--tag ${{ env.IMAGE_NAME }}:latest \
${{ env.IMAGE_NAME }}:${{ needs.prepare.outputs.version }}-amd64 \
${{ env.IMAGE_NAME }}:${{ needs.prepare.outputs.version }}-arm64
fi
8 changes: 7 additions & 1 deletion debugToolsConfig.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@
url: "localhost:8545"
localUrl: "http://0.0.0.0:8123"
block: "0x9095F"
dumpFileName: "storageDump.json"
dumpFileName: "storageDump.json"
addressRollup: "0x`
l1Url: "http://localhost:8545"`
l1ChainId: 1
l1SyncStartBlock: 4000000
rollupId: 1
elderberryBachNo: 1
2 changes: 2 additions & 0 deletions zk/debug_tools/cast-scripts/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rpc_url="http://0.0.0.0:8467"
private_key=""
21 changes: 21 additions & 0 deletions zk/debug_tools/cast-scripts/balance-after-tx.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
set -a # automatically export all variables
source .env
set +a

if [ -z "$rpc_url" ]; then
echo "Please fill "rpc_url" in the .env file"
exit 1
fi

if [ -z "$private_key" ]; then
echo "Please fill "private_key" in the .env file"
exit 1
fi
# balance before sending funds
echo "Balance before sending funds $(cast balance --rpc-url $rpc_url 0x5e8f0f2f8b364e2f0f3f1f1f1f1f1f1f1f1f1f1f)"

# send some funds
cast send --rpc-url $rpc_url --legacy --value 0.1ether --private-key $private_key 0x5e8f0f2f8b364e2f0f3f1f1f1f1f1f1f1f1f1f1f

# now get the balance of the accounts
echo "Balance after sending funds $(cast balance --rpc-url $rpc_url 0x5e8f0f2f8b364e2f0f3f1f1f1f1f1f1f1f1f1f1f)"
19 changes: 19 additions & 0 deletions zk/debug_tools/cast-scripts/estimate-gas-infinite-loop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
set -a # automatically export all variables
source .env
set +a

if [ -z "$rpc_url" ]; then
echo "Please fill "rpc_url" in the .env file"
exit 1
fi

if [ -z "$private_key" ]; then
echo "Please fill "private_key" in the .env file"
exit 1
fi

addr=$(cast wallet address --private-key $private_key)
nonce=$(cast nonce --rpc-url $rpc_url $addr)

echo "Test address: $addr (nonce: $nonce) balance $(cast balance --rpc-url $rpc_url $addr)"
cast send --legacy --rpc-url $rpc_url --private-key $private_key --async --create 0x5b3456
25 changes: 25 additions & 0 deletions zk/debug_tools/cast-scripts/nonce-with-infinite-loop-sc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
set -a # automatically export all variables
source .env
set +a

if [ -z "$rpc_url" ]; then
echo "Please fill "rpc_url" in the .env file"
exit 1
fi

if [ -z "$private_key" ]; then
echo "Please fill "private_key" in the .env file"
exit 1
fi

addr=$(cast wallet address --private-key $private_key)
nonce=$(cast nonce --rpc-url $rpc_url $addr)

echo "Test address: $addr (nonce: $nonce) balance $(cast balance --rpc-url $rpc_url $addr)"
cast send --legacy --rpc-url $rpc_url --private-key $private_key --gas-limit 5000000000000000000 --gas-price 1100 --async --create 0x5b3456
echo "Test address: $addr (nonce: $(cast nonce --rpc-url $rpc_url $addr)) balance $(cast balance --rpc-url $rpc_url $addr)"
#sleep 5 seconds
echo "Sleeping for 5 seconds..."
sleep 5

echo "Test address: $addr (nonce: $(cast nonce --rpc-url $rpc_url $addr)) balance $(cast balance --rpc-url $rpc_url $addr)"
105 changes: 105 additions & 0 deletions zk/debug_tools/l1-sequences-downloader/acc-input-hash/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package main

import (
"encoding/json"
"fmt"
"math/big"
"os"

"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/zk/syncer"
"github.com/ledgerwatch/erigon/zk/types"
)

func main() {
fileSeq, err := os.Open("sequencesMainnet.json")
if err != nil {
panic(err)
}
defer fileSeq.Close()
sequences := make([]types.L1BatchInfo, 0)

encSeq := json.NewDecoder(fileSeq)
if err := encSeq.Decode(&sequences); err != nil {
panic(err)
}
fileSeq.Close()
fileCalldata, err := os.Open("calldataMainnet.json")
if err != nil {
panic(err)
}
defer fileCalldata.Close()
calldata := make(map[string]string)

encCalldata := json.NewDecoder(fileCalldata)
if err := encCalldata.Decode(&calldata); err != nil {
panic(err)
}
fileCalldata.Close()
fileAccInputHash, err := os.Open("accInputHashesMainnet.json")
if err != nil {
panic(err)
}
defer fileAccInputHash.Close()
accInputHashes := make(map[uint64]string)

encAccInputHash := json.NewDecoder(fileAccInputHash)
if err := encAccInputHash.Decode(&accInputHashes); err != nil {
panic(err)
}
fileAccInputHash.Close()

for i := 0; i < 40000; i++ {
delete(calldata, sequences[i].L1TxHash.String())
delete(accInputHashes, sequences[i].BatchNo)
}

prevSeq := sequences[40001]
for i := 40002; i < len(sequences); i++ {
nextSeq := sequences[i]
nextCalldata, ok := calldata[nextSeq.L1TxHash.String()]
if !ok {
panic(fmt.Errorf("calldata for tx %s not found", nextSeq.L1TxHash.String()))
}
prevAccInputHash, ok := accInputHashes[prevSeq.BatchNo]
if !ok {
panic(fmt.Errorf("accInputHash for batch %d not found", prevSeq.BatchNo))
}
lastAccInputHash, ok := accInputHashes[nextSeq.BatchNo]
if !ok {
panic(fmt.Errorf("accInputHash for batch %d not found", nextSeq.BatchNo))
}

decodedSequenceInterface, err := syncer.DecodeSequenceBatchesCalldata(common.FromHex(nextCalldata))
if err != nil {
panic(fmt.Errorf("failed to decode calldata for tx %s: %w", nextSeq.L1TxHash, err))
}

accInputHashCalcFn, totalSequenceBatches, err := syncer.GetAccInputDataCalcFunction(nextSeq.L1InfoRoot, decodedSequenceInterface)
if err != nil {
panic(fmt.Errorf("failed to get accInputHash calculation func: %w", err))
}

if totalSequenceBatches == 0 || nextSeq.BatchNo-prevSeq.BatchNo > uint64(totalSequenceBatches) {
panic(fmt.Errorf("batch %d is out of range of sequence calldata: %d %d", nextSeq.BatchNo, prevSeq.BatchNo, totalSequenceBatches))
}

prevAccInputBigInt := new(big.Int).SetBytes(common.FromHex(prevAccInputHash))
preVAccInputHash := common.BigToHash(prevAccInputBigInt)
accInputHash := &preVAccInputHash
// calculate acc input hash
for i := 0; i < int(nextSeq.BatchNo-prevSeq.BatchNo); i++ {
accInputHash = accInputHashCalcFn(*accInputHash, i)
}

if accInputHash.Hex() != lastAccInputHash {
panic(fmt.Errorf("accInputHash for tx %s and batchNum %d does not match", nextSeq.L1TxHash.String(), nextSeq.BatchNo))
}

prevSeq = nextSeq
if i%1000 == 0 {
fmt.Println(i, " sequence checked: ", nextSeq.BatchNo)
}
}

}
150 changes: 150 additions & 0 deletions zk/debug_tools/l1-sequences-downloader/sequence-accinputhash/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package main

import (
"context"
"encoding/binary"
"encoding/json"
"fmt"
"os"
"time"

ethereum "github.com/ledgerwatch/erigon"
"github.com/ledgerwatch/erigon-lib/common"

"github.com/iden3/go-iden3-crypto/keccak256"
"github.com/ledgerwatch/erigon/ethclient"
"github.com/ledgerwatch/erigon/zk/debug_tools"
"github.com/ledgerwatch/erigon/zk/types"
)

func main() {
ctx := context.Background()
cfg, err := debug_tools.GetConf()
if err != nil {
panic(fmt.Sprintf("RPGCOnfig: %s", err))
}

file, err := os.Open("sequencesMainnet.json")
if err != nil {
panic(err)
}
defer file.Close()
sequences := make([]types.L1BatchInfo, 0)

enc := json.NewDecoder(file)
if err := enc.Decode(&sequences); err != nil {
panic(err)
}

ethClient, err := ethclient.Dial(cfg.L1Url)
if err != nil {
panic(err)
}

emptyHash := common.Hash{}
rollupAddr := common.HexToAddress(cfg.AddressRollup)
accInputHashes := make(map[uint64]string)
index := 0
for {
seq := sequences[index]
// get call data for tx

var accInputHash common.Hash
var err error
if seq.BatchNo < cfg.ElderberryBachNo {
accInputHash, err = callSequencedBatchesMap(ctx, ethClient, &rollupAddr, seq.BatchNo)
} else {
accInputHash, _, err = callGetRollupSequencedBatches(ctx, ethClient, &rollupAddr, cfg.RollupId, seq.BatchNo)
}
if err != nil {
fmt.Println("Error fetching transaction: ", err, " for batch ", seq.BatchNo)
continue
}

if accInputHash == emptyHash {
fmt.Println("Error fetching transaction: accInputHash is empty for batch ", seq.BatchNo)
panic("Error fetching transaction: accInputHash is empty for batch")
}
accInputHashes[seq.BatchNo] = accInputHash.Hex()

index++
if index >= len(sequences) {
break
}

if index%100 == 0 {
fmt.Println("Processed ", index, "sequences from ", len(sequences))
}

time.Sleep(2 * time.Millisecond)
}

// write l1BatchInfos to file
file2, err := os.Create("accInputHashes.json")
if err != nil {
panic(err)
}
defer file2.Close()

enc2 := json.NewEncoder(file2)
enc2.SetIndent("", " ")
if err := enc2.Encode(accInputHashes); err != nil {
panic(err)
}
}

// calls the old rollup contract to get the accInputHash for a certain batch
// returns the accInputHash and lastBatchNumber
func callSequencedBatchesMap(ctx context.Context, client *ethclient.Client, addr *common.Address, batchNum uint64) (accInputHash common.Hash, err error) {
mapKeyHex := fmt.Sprintf("%064x%064x", batchNum, 114 /* _legacySequencedBatches slot*/)
mapKey := keccak256.Hash(common.FromHex(mapKeyHex))
mkh := common.BytesToHash(mapKey)

resp, err := client.StorageAt(ctx, *addr, mkh, nil)
if err != nil {
return
}

if err != nil {
return
}

if len(resp) < 32 {
return
}
accInputHash = common.BytesToHash(resp[:32])

return
}

var (
errorShortResponseLT32 = fmt.Errorf("response length is less than 32 bytes")
errorShortResponseLT96 = fmt.Errorf("response length is less than 96 bytes")
rollupSequencedBatchesSignature = "0x25280169" // hardcoded abi signature
)

func callGetRollupSequencedBatches(ctx context.Context, client *ethclient.Client, addr *common.Address, rollupId, batchNum uint64) (common.Hash, uint64, error) {
rollupID := fmt.Sprintf("%064x", rollupId)
batchNumber := fmt.Sprintf("%064x", batchNum)

resp, err := client.CallContract(ctx, ethereum.CallMsg{
To: addr,
Data: common.FromHex(rollupSequencedBatchesSignature + rollupID + batchNumber),
}, nil)

if err != nil {
return common.Hash{}, 0, err
}

if len(resp) < 32 {
return common.Hash{}, 0, errorShortResponseLT32
}
h := common.BytesToHash(resp[:32])

if len(resp) < 96 {
return common.Hash{}, 0, errorShortResponseLT96
}
lastBatchNumber := binary.BigEndian.Uint64(resp[88:96])

return h, lastBatchNumber, nil
}
Loading

0 comments on commit 8be071a

Please sign in to comment.