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
V-Staykov authored Oct 17, 2024
2 parents 10d42ae + a30f347 commit b6ccaac
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 23 deletions.
116 changes: 116 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
name: Build and Push Docker

on:
push:
tags:
- 'v*'
release:
types: [published]
workflow_dispatch:
inputs:
tag:
description: 'Custom Docker Tag (leave blank to use the branch name or commit SHA)'
required: false

env:
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
IMAGE_NAME: hermeznetwork/cdk-erigon

jobs:
prepare:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.prep.outputs.version }}
latest_tag: ${{ env.LATEST_TAG }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Prepare
id: prep
run: |
BRANCH_NAME=$(echo ${GITHUB_REF#refs/heads/} | sed 's/[^a-zA-Z0-9._-]/-/g')
SHORT_SHA=$(echo ${{ github.sha }} | head -c 7)
echo "BRANCH_NAME=${BRANCH_NAME}" >> $GITHUB_ENV
echo "SHORT_SHA=${SHORT_SHA}" >> $GITHUB_ENV
TAG=${{ github.event.inputs.tag }}
if [[ -z "$TAG" ]]; then
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
TAG=$BRANCH_NAME-$SHORT_SHA
elif [[ "${{ github.event_name }}" == "push" ]]; then
TAG=$SHORT_SHA
elif [[ "${{ github.event_name }}" == "release" ]]; then
TAG=${{ github.event.release.tag_name }}
fi
fi
echo "version=$TAG" >> $GITHUB_OUTPUT
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
echo "LATEST_TAG=false" >> $GITHUB_ENV
else
echo "LATEST_TAG=true" >> $GITHUB_ENV
fi
build-amd64:
needs: prepare
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
uses: docker/login-action@v3
with:
username: ${{ env.DOCKERHUB_USERNAME }}
password: ${{ env.DOCKERHUB_TOKEN }}
- name: Build and push AMD64 image
uses: docker/build-push-action@v3
with:
context: .
file: Dockerfile
push: true
tags: ${{ env.IMAGE_NAME }}:${{ needs.prepare.outputs.version }}-amd64
platforms: linux/amd64

build-arm64:
needs: prepare
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
uses: docker/login-action@v3
with:
username: ${{ env.DOCKERHUB_USERNAME }}
password: ${{ env.DOCKERHUB_TOKEN }}
- name: Build and push ARM64 image
uses: docker/build-push-action@v3
with:
context: .
file: Dockerfile
push: true
tags: ${{ env.IMAGE_NAME }}:${{ needs.prepare.outputs.version }}-arm64
platforms: linux/arm64

create-and-push-manifest:
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
uses: docker/login-action@v3
with:
username: ${{ env.DOCKERHUB_USERNAME }}
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 .
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
23 changes: 21 additions & 2 deletions core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1159,12 +1159,21 @@ func TruncateBlocks(ctx context.Context, tx kv.RwTx, blockFrom uint64) error {
if blockFrom < 1 { //protect genesis
blockFrom = 1
}
return tx.ForEach(kv.Headers, hexutility.EncodeTs(blockFrom), func(k, v []byte) error {

var newSequenceValue *uint64 = nil
if err := tx.ForEach(kv.Headers, hexutility.EncodeTs(blockFrom), func(k, v []byte) error {
b, err := ReadBodyForStorageByKey(tx, k)
if err != nil {
return err
}

// set it only once on the lowest block
// we go over blocks incrementally from the lowest, so this check should suffice
if b != nil {
if newSequenceValue == nil {
tmp := b.BaseTxId // copy it before taking the value
newSequenceValue = &tmp
}
txIDBytes := make([]byte, 8)
for txID := b.BaseTxId; txID < b.BaseTxId+uint64(b.TxAmount); txID++ {
binary.BigEndian.PutUint64(txIDBytes, txID)
Expand Down Expand Up @@ -1194,7 +1203,17 @@ func TruncateBlocks(ctx context.Context, tx kv.RwTx, blockFrom uint64) error {
default:
}
return nil
})
}); err != nil {
return err
}

if newSequenceValue != nil {
if err := ResetSequence(tx, kv.EthTx, *newSequenceValue); err != nil {
return err
}
}

return nil
}

func ReadBlockByNumber(db kv.Tx, number uint64) (*types.Block, error) {
Expand Down
75 changes: 75 additions & 0 deletions core/rawdb/accessors_chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"testing"

libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/memdb"
"github.com/ledgerwatch/erigon/core/rawdb"
"github.com/ledgerwatch/erigon/turbo/stages/mock"
Expand Down Expand Up @@ -742,6 +743,80 @@ func TestShanghaiBodyForStorageNoWithdrawals(t *testing.T) {
require.Equal(uint32(2), body.TxAmount)
}

func TestTruncateBlocks(t *testing.T) {
testCases := []struct {
name string
blocks []uint64
truncateTo uint64
}{
{
name: "truncate 1 block",
blocks: []uint64{
2,
2,
},
truncateTo: 1,
},
{
name: "truncate 2 blocks",
blocks: []uint64{
2,
3,
4,
},
truncateTo: 1,
},
}

t.Parallel()
require := require.New(t)
m := mock.Mock(t)
ctx := m.Ctx
var testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
testAddr := crypto.PubkeyToAddress(testKey.PublicKey)
signer1 := types.MakeSigner(params.MainnetChainConfig, 1, 0)

mustSign := func(tx types.Transaction, s types.Signer) types.Transaction {
r, err := types.SignTx(tx, s, testKey)
require.NoError(err)
return r
}

for _, tc := range testCases {
tx, err := m.DB.BeginRw(m.Ctx)
require.NoError(err)

sequences := make([]uint64, len(tc.blocks)+1)
// get the sequence before
seqEthTxBefore, err := tx.ReadSequence(kv.EthTx)
require.NoError(err)
sequences[0] = seqEthTxBefore
for i, txCount := range tc.blocks {
txs := make([]types.Transaction, txCount)
for j := uint64(1); j <= txCount; j++ {
txs[j-1] = mustSign(types.NewTransaction(j, testAddr, u256.Num1, j, u256.Num1, nil), *signer1)
}
body := &types.Body{
Transactions: txs,
Uncles: []*types.Header{{Extra: []byte("test header")}},
}
header := &types.Header{Number: big.NewInt(int64(i + 1))}
require.NoError(rawdb.WriteHeader(tx, header))
require.NoError(rawdb.WriteBody(tx, header.Hash(), uint64(i+1), body))
seqEthTxStep1, err := tx.ReadSequence(kv.EthTx)
require.NoError(err)
sequences[i+1] = seqEthTxStep1
}

require.NoError(rawdb.TruncateBlocks(ctx, tx, tc.truncateTo+1))
seqEthTxAfterTruncate, err := tx.ReadSequence(kv.EthTx)
require.NoError(err)
require.Equal(sequences[tc.truncateTo], seqEthTxAfterTruncate, tc.name)

tx.Rollback()
}
}

func checkReceiptsRLP(have, want types.Receipts) error {
if len(have) != len(want) {
return fmt.Errorf("receipts sizes mismatch: have %d, want %d", len(have), len(want))
Expand Down
12 changes: 11 additions & 1 deletion core/vm/instructions_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,19 @@ func opCreate2_zkevm_lastOpCode(pc *uint64, interpreter *EVMInterpreter, scope *
endowment = scope.Stack.Pop()
offset, size = scope.Stack.Pop(), scope.Stack.Pop()
salt = scope.Stack.Pop()
input = scope.Memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64()))
)

if size.Uint64() > params.MaxInitCodeSize {
// if we are over the maximum size this instruction is invalid and we could be open
// to trying to allocate too much memory causing a panic. Because the address never
// actually gets created in this insance because of out of gas we can just spam
// a random address by cutting the size down to maximum
newSize := uint256.NewInt(params.MaxInitCodeSize)
size = *newSize
}

input := scope.Memory.GetCopy(int64(offset.Uint64()), int64(size.Uint64()))

caller := scope.Contract
codeAndHash := &codeAndHash{code: input}
address := crypto.CreateAddress2(caller.Address(), salt.Bytes32(), codeAndHash.Hash().Bytes())
Expand Down
1 change: 1 addition & 0 deletions turbo/jsonrpc/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func APIList(db kv.RoDB, eth rpchelper.ApiBackend, txPool txpool.TxpoolClient, r

base := NewBaseApi(filters, stateCache, blockReader, agg, cfg.WithDatadir, cfg.EvmCallTimeout, engine, cfg.Dirs)
base.SetL2RpcUrl(ethCfg.Zk.L2RpcUrl)
base.SetGasless(ethCfg.AllowFreeTransactions)
ethImpl := NewEthAPI(base, db, eth, txPool, mining, cfg.Gascap, cfg.Feecap, cfg.ReturnDataLimit, ethCfg, cfg.AllowUnprotectedTxs, cfg.MaxGetProofRewindBlockCount, cfg.WebsocketSubscribeLogsChannelSize, logger)
erigonImpl := NewErigonAPI(base, db, eth)
txpoolImpl := NewTxPoolAPI(base, db, txPool, rawPool, rpcUrl)
Expand Down
4 changes: 4 additions & 0 deletions turbo/jsonrpc/eth_api_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func (api *BaseAPI) GetL2RpcUrl() string {
return api.l2RpcUrl
}

func (api *BaseAPI) SetGasless(gasless bool) {
api.gasless = gasless
}

// RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction
type RPCTransaction struct {
BlockHash *common.Hash `json:"blockHash"`
Expand Down
4 changes: 0 additions & 4 deletions turbo/stages/stageloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,9 @@ func StageLoop(ctx context.Context,
if errors.Is(err, libcommon.ErrStopped) || errors.Is(err, context.Canceled) {
return
}

if !errors.Is(err, zk.ErrLimboState) {
logger.Error("Staged Sync", "err", err)
}
if recoveryErr := hd.RecoverFromDb(db); recoveryErr != nil {
logger.Error("Failed to recover header sentriesClient", "err", recoveryErr)
}
time.Sleep(500 * time.Millisecond) // just to avoid too much similar errors in logs
continue
}
Expand Down
28 changes: 14 additions & 14 deletions zk/legacy_executor_verifier/legacy_executor_verifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,20 +227,6 @@ func (v *LegacyExecutorVerifier) VerifyAsync(request *VerifierRequest) *Promise[
verifierBundle := NewVerifierBundle(request, nil, false)
blockNumbers := verifierBundle.Request.BlockNumbers

e := v.GetNextOnlineAvailableExecutor()
if e == nil {
return verifierBundle, ErrNoExecutorAvailable
}

t := utils.StartTimer("legacy-executor-verifier", "verify-async")
defer t.LogTimer()

e.AquireAccess()
defer e.ReleaseAccess()
if v.cancelAllVerifications.Load() {
return nil, ErrPromiseCancelled
}

var err error
ctx := context.Background()
// mapmutation has some issue with us not having a quit channel on the context call to `Done` so
Expand Down Expand Up @@ -300,6 +286,20 @@ func (v *LegacyExecutorVerifier) VerifyAsync(request *VerifierRequest) *Promise[

verifierBundle.markAsreadyForSendingRequest()

e := v.GetNextOnlineAvailableExecutor()
if e == nil {
return verifierBundle, ErrNoExecutorAvailable
}

t := utils.StartTimer("legacy-executor-verifier", "verify-async")
defer t.LogTimer()

e.AquireAccess()
defer e.ReleaseAccess()
if v.cancelAllVerifications.Load() {
return nil, ErrPromiseCancelled
}

ok, executorResponse, executorErr, generalErr := e.Verify(payload, request, previousBlock.Root())
if generalErr != nil {
return verifierBundle, generalErr
Expand Down
8 changes: 6 additions & 2 deletions zk/tests/unwinds/unwind.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ for file in $(ls $dataPath/phase2-dump1); do
if cmp -s $dataPath/phase2-dump1/$filename $dataPath/phase2-dump2/$filename; then
echo "No difference found in $filename"
else
echo "Unexpected differences in $filename"
exit 2
if [ "$filename" = "BadHeaderNumber.txt" ]; then
echo "Expected differences in $filename"
else
echo "Unexpected differences in $filename"
exit 2
fi
fi
done

0 comments on commit b6ccaac

Please sign in to comment.