Skip to content

Commit

Permalink
fix off by one issue in overwriting transactions (#1234)
Browse files Browse the repository at this point in the history
# Conflicts:
#	core/rawdb/accessors_chain_zkevm.go
  • Loading branch information
hexoscott authored Sep 26, 2024
1 parent 1b47c48 commit da9bb34
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 2 deletions.
34 changes: 32 additions & 2 deletions core/rawdb/accessors_chain_zkevm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rawdb

import (
"bytes"
"encoding/binary"
"fmt"

Expand All @@ -9,6 +10,7 @@ import (
"github.com/gateway-fm/cdk-erigon-lib/common/hexutility"
"github.com/gateway-fm/cdk-erigon-lib/kv"
"github.com/gateway-fm/cdk-erigon-lib/kv/kvcfg"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/common/dbutils"
"github.com/ledgerwatch/erigon/core/types"
ethTypes "github.com/ledgerwatch/erigon/core/types"
Expand Down Expand Up @@ -83,16 +85,44 @@ func WriteBodyAndTransactions(db kv.RwTx, hash libcommon.Hash, number uint64, tx
}
transactionV3, _ := kvcfg.TransactionsV3.Enabled(db.(kv.Tx))
if transactionV3 {
err = WriteTransactions(db, txs, data.BaseTxId+1, &hash)
err = OverwriteTransactions(db, txs, data.BaseTxId+1, &hash)
} else {
err = WriteTransactions(db, txs, data.BaseTxId+1, nil)
err = OverwriteTransactions(db, txs, data.BaseTxId+1, nil)
}
if err != nil {
return fmt.Errorf("failed to WriteTransactions: %w", err)
}
return nil
}

func OverwriteTransactions(db kv.RwTx, txs []types.Transaction, baseTxId uint64, blockHash *libcommon.Hash) error {
txId := baseTxId
buf := bytes.NewBuffer(nil)
for _, tx := range txs {
txIdKey := make([]byte, 8)
binary.BigEndian.PutUint64(txIdKey, txId)
txId++

buf.Reset()
if err := rlp.Encode(buf, tx); err != nil {
return fmt.Errorf("broken tx rlp: %w", err)
}

// If next Append returns KeyExists error - it means you need to open transaction in App code before calling this func. Batch is also fine.
if blockHash != nil {
key := append(txIdKey, blockHash.Bytes()...)
if err := db.Put(kv.EthTxV3, key, common.CopyBytes(buf.Bytes())); err != nil {
return err
}
} else {
if err := db.Put(kv.EthTx, txIdKey, common.CopyBytes(buf.Bytes())); err != nil {
return err
}
}
}
return nil
}

func GetBodyTransactions(tx kv.RwTx, fromBlockNum, toBlockNum uint64) (*[]types.Transaction, error) {
var transactions []types.Transaction
if err := tx.ForEach(kv.BlockBody, hexutility.EncodeTs(fromBlockNum), func(k, v []byte) error {
Expand Down
95 changes: 95 additions & 0 deletions core/rawdb/accessors_chain_zkevm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package rawdb

import (
"testing"
"github.com/gateway-fm/cdk-erigon-lib/kv/memdb"
"github.com/stretchr/testify/require"
"golang.org/x/crypto/sha3"
libcommon "github.com/gateway-fm/cdk-erigon-lib/common"
"github.com/ledgerwatch/erigon/crypto"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/common/u256"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/rlp"
"github.com/ledgerwatch/erigon/common/dbutils"
)

func TestBodyStorageZkevm(t *testing.T) {
_, tx := memdb.NewTestTx(t)
require := require.New(t)

var testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
testAddr := crypto.PubkeyToAddress(testKey.PublicKey)

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

// prepare db so it works with our test
signer1 := types.MakeSigner(params.HermezMainnetChainConfig, 1)
body := &types.Body{
Transactions: []types.Transaction{
mustSign(types.NewTransaction(1, testAddr, u256.Num1, 1, u256.Num1, nil), *signer1),
mustSign(types.NewTransaction(2, testAddr, u256.Num1, 2, u256.Num1, nil), *signer1),
},
Uncles: []*types.Header{{Extra: []byte("test header")}},
}

// Create a test body to move around the database and make sure it's really new
hasher := sha3.NewLegacyKeccak256()
_ = rlp.Encode(hasher, body)
hash := libcommon.BytesToHash(hasher.Sum(nil))

if entry := ReadCanonicalBodyWithTransactions(tx, hash, 0); entry != nil {
t.Fatalf("Non existent body returned: %v", entry)
}
require.NoError(WriteBody(tx, hash, 0, body))
if entry := ReadCanonicalBodyWithTransactions(tx, hash, 0); entry == nil {
t.Fatalf("Stored body not found")
} else if types.DeriveSha(types.Transactions(entry.Transactions)) != types.DeriveSha(types.Transactions(body.Transactions)) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(body.Uncles) {
t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, body)
}
if entry := ReadBodyRLP(tx, hash, 0); entry == nil {
t.Fatalf("Stored body RLP not found")
} else {
hasher := sha3.NewLegacyKeccak256()
hasher.Write(entry)

if calc := libcommon.BytesToHash(hasher.Sum(nil)); calc != hash {
t.Fatalf("Retrieved RLP body mismatch: have %v, want %v", entry, body)
}
}

// zkevm check with overwriting transactions
bodyForStorage, err := ReadBodyForStorageByKey(tx, dbutils.BlockBodyKey(0, hash))
if err != nil {
t.Fatalf("ReadBodyForStorageByKey failed: %s", err)
}
// overwrite the transactions using the new code from zkevm
require.NoError(WriteBodyAndTransactions(tx, hash, 0, body.Transactions, bodyForStorage))

// now re-run the checks from above after reading the body again
if entry := ReadCanonicalBodyWithTransactions(tx, hash, 0); entry == nil {
t.Fatalf("Stored body not found")
} else if types.DeriveSha(types.Transactions(entry.Transactions)) != types.DeriveSha(types.Transactions(body.Transactions)) || types.CalcUncleHash(entry.Uncles) != types.CalcUncleHash(body.Uncles) {
t.Fatalf("Retrieved body mismatch: have %v, want %v", entry, body)
}
if entry := ReadBodyRLP(tx, hash, 0); entry == nil {
t.Fatalf("Stored body RLP not found")
} else {
hasher := sha3.NewLegacyKeccak256()
hasher.Write(entry)

if calc := libcommon.BytesToHash(hasher.Sum(nil)); calc != hash {
t.Fatalf("Retrieved RLP body mismatch: have %v, want %v", entry, body)
}
}

// Delete the body and verify the execution
deleteBody(tx, hash, 0)
if entry := ReadCanonicalBodyWithTransactions(tx, hash, 0); entry != nil {
t.Fatalf("Deleted body returned: %v", entry)
}
}

0 comments on commit da9bb34

Please sign in to comment.