Skip to content

Commit

Permalink
Etrog sync fix (#116)
Browse files Browse the repository at this point in the history
* remove hardcoded value

* minor fixes

* fixed sync almost to tip for Cardana
  • Loading branch information
V-Staykov authored Feb 9, 2024
1 parent 99d7cc8 commit 91c5513
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 63 deletions.
26 changes: 21 additions & 5 deletions core/blockchain_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/core/vm"
"github.com/ledgerwatch/erigon/smt/pkg/blockinfo"
txTypes "github.com/ledgerwatch/erigon/zk/tx"
)

// ExecuteBlockEphemerally runs a block from provided stateReader and
Expand All @@ -58,7 +59,7 @@ func ExecuteBlockEphemerallyZk(
block.Uncles()
ibs := state.New(stateReader)
header := block.Header()
blockTransaction := block.Transactions()
blockTransactions := block.Transactions()
blockGasLimit := block.GasLimit()
gp := new(GasPool)
gp.AddGas(blockGasLimit)
Expand All @@ -79,7 +80,7 @@ func ExecuteBlockEphemerallyZk(
}

if !vmConfig.ReadOnly {
if err := InitializeBlockExecution(engine, chainReader, header, blockTransaction, block.Uncles(), chainConfig, ibs, excessDataGas); err != nil {
if err := InitializeBlockExecution(engine, chainReader, header, blockTransactions, block.Uncles(), chainConfig, ibs, excessDataGas); err != nil {
return nil, err
}
}
Expand Down Expand Up @@ -115,7 +116,6 @@ func ExecuteBlockEphemerallyZk(
}
blockTime := block.Time()
ibs.SyncerPreExecuteStateSet(chainConfig, blockNum, blockTime, prevBlockHash, &blockGer, &l1BlockHash, &gersInBetween)

blockInfoTree := blockinfo.NewBlockInfoTree()
if chainConfig.IsForkID7Etrog(blockNum) {
coinbase := block.Coinbase()
Expand All @@ -137,7 +137,7 @@ func ExecuteBlockEphemerallyZk(
logIndex := int64(0)
usedGas := new(uint64)

for txIndex, tx := range blockTransaction {
for txIndex, tx := range blockTransactions {
ibs.Prepare(tx.Hash(), block.Hash(), txIndex)
writeTrace := false
if vmConfig.Debug && vmConfig.Tracer == nil {
Expand Down Expand Up @@ -180,9 +180,25 @@ func ExecuteBlockEphemerallyZk(
ibs.ScalableSetSmtRootHash(roHermezDb)
}

txSender, _ := tx.GetSender()
if chainConfig.IsForkID7Etrog(blockNum) {
l2TxHash, err := txTypes.ComputeL2TxHash(
chainConfig.ChainID,
tx.GetValue(),
tx.GetPrice(),
tx.GetNonce(),
tx.GetGas(),
tx.GetTo(),
&txSender,
tx.GetData(),
)
if err != nil {
return nil, err
}

//block info tree
_, err = blockInfoTree.SetBlockTx(
&l2TxHash,
txIndex,
receipt,
logIndex,
Expand Down Expand Up @@ -231,7 +247,7 @@ func ExecuteBlockEphemerallyZk(
//}
}
if !vmConfig.ReadOnly {
txs := blockTransaction
txs := blockTransactions
if _, _, _, err := FinalizeBlockExecution(engine, stateReader, block.Header(), txs, block.Uncles(), stateWriter, chainConfig, ibs, receipts, block.Withdrawals(), chainReader, false, excessDataGas); err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions core/state/intra_block_state_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (sdb *IntraBlockState) PreExecuteStateSet(chainConfig *chain.Config, blockN
}
}

func (sdb *IntraBlockState) SyncerPreExecuteStateSet(chainConfig *chain.Config, blockNumber uint64, blockTimestamp uint64, stateRoot, blockGer, l1BlockHash *libcommon.Hash, gerUpdates *[]*dstypes.GerUpdate) {
func (sdb *IntraBlockState) SyncerPreExecuteStateSet(chainConfig *chain.Config, blockNumber uint64, blockTimestamp uint64, prevBlockHash, blockGer, l1BlockHash *libcommon.Hash, gerUpdates *[]*dstypes.GerUpdate) {
if !sdb.Exist(ADDRESS_SCALABLE_L2) {
// create account if not exists
sdb.CreateAccount(ADDRESS_SCALABLE_L2, true)
Expand All @@ -83,7 +83,7 @@ func (sdb *IntraBlockState) SyncerPreExecuteStateSet(chainConfig *chain.Config,
}

//save prev block hash
sdb.scalableSetBlockHash(blockNumber-1, stateRoot)
sdb.scalableSetBlockHash(blockNumber-1, prevBlockHash)

//save ger with l1blockhash
if blockGer != nil && *blockGer != emptyHash {
Expand Down
2 changes: 1 addition & 1 deletion eth/stagedsync/stage_execute_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ Loop:
later.
*/
headerHash := header.Hash()
prevBlockHash = headerHash
prevBlockHash = header.Root

rawdb.WriteHeader(tx, header)
err = rawdb.WriteCanonicalHash(tx, headerHash, blockNum)
Expand Down
10 changes: 7 additions & 3 deletions smt/pkg/blockinfo/block_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,21 @@ func (b *BlockInfoTree) InitBlockHeader(oldBlockHash *libcommon.Hash, coinbase *
}

func (b *BlockInfoTree) SetBlockTx(
l2TxHash *libcommon.Hash,
txIndex int,
receipt *ethTypes.Receipt,
logIndex int64,
cumulativeGasUsed uint64,
effectivePercentage uint8,
) (*big.Int, error) {
txIndexBig := big.NewInt(int64(txIndex))
_, err := setL2TxHash(b.smt, txIndexBig, receipt.TxHash.Big())
_, err := setL2TxHash(b.smt, txIndexBig, l2TxHash.Big())
if err != nil {
return nil, err
}
_, err = setTxStatus(b.smt, txIndexBig, big.NewInt(int64(receipt.Status)))

bigStatus := big.NewInt(0).SetUint64(receipt.Status)
_, err = setTxStatus(b.smt, txIndexBig, bigStatus)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -110,7 +113,8 @@ func (b *BlockInfoTree) SetBlockTx(
logIndex += 1
}

root, err := setTxEffectivePercentage(b.smt, txIndexBig, big.NewInt(int64(effectivePercentage)))
bigEffectivePercentage := big.NewInt(0).SetUint64(uint64(effectivePercentage))
root, err := setTxEffectivePercentage(b.smt, txIndexBig, bigEffectivePercentage)
if err != nil {
return nil, err
}
Expand Down
8 changes: 1 addition & 7 deletions zk/stages/stage_batches.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,13 +407,7 @@ func writeL2Block(eriDb ErigonDb, hermezDb HermezDb, l2Block *types.FullL2Block)
return fmt.Errorf("write effective gas price percentage error: %v", err)
}

//TODO: temp datastream fix, remove later
//works only for Cardona
stateRoot := transaction.StateRoot
if bn.Uint64() == 59056 {
stateRoot = common.HexToHash("0x0c21a98253d5ef5cf77faa5fbbc44e627fd14ec6d410c6e87d40e639e54b41d7")
}
if err := hermezDb.WriteStateRoot(l2Block.L2BlockNumber, stateRoot); err != nil {
if err := hermezDb.WriteStateRoot(l2Block.L2BlockNumber, transaction.StateRoot); err != nil {
return fmt.Errorf("write rpc root error: %v", err)
}
}
Expand Down
4 changes: 3 additions & 1 deletion zk/stages/stage_sequence_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,9 @@ func postBlockStateHandling(
for i := 0; i < len(transactions); i++ {
receipt := receipts[i]
// todo: how to set the effective gas percentage as a the sequencer
_, err := infoTree.SetBlockTx(i, receipt, logIndex, receipt.CumulativeGasUsed, 0)
// TODO: calculate l2 tx hash
l2TxHash := common.Hash{}
_, err := infoTree.SetBlockTx(&l2TxHash, i, receipt, logIndex, receipt.CumulativeGasUsed, 0)
if err != nil {
return err
}
Expand Down
51 changes: 28 additions & 23 deletions zk/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import (

"bytes"

"regexp"
"strings"

"github.com/holiman/uint256"
"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/rlp"
"github.com/ledgerwatch/erigon/smt/pkg/utils"
"github.com/ledgerwatch/erigon/zkevm/hex"
"github.com/ledgerwatch/log/v3"
"regexp"
"strings"
)

const (
Expand Down Expand Up @@ -235,21 +236,26 @@ func rlpFieldsToLegacyTx(fields [][]byte, v, r, s []byte) (tx *types.LegacyTx, e
}, nil
}

const txGasLimit = 30000000
func ComputeL2TxHash(
chainId *big.Int,
value, gasPrice *uint256.Int,
nonce, txGasLimit uint64,
to, from *common.Address,
data []byte,
) (common.Hash, error) {

func ComputeL2TxHash(tx types.LegacyTx) (common.Hash, error) {
txType := "01"
if tx.GetChainID().Eq(uint256.NewInt(0)) {
if chainId != nil && chainId.Cmp(big.NewInt(0)) == 0 {
txType = "00"
}

// add txType, nonce, gasPrice and gasLimit
noncePart, err := formatL2TxHashParam(tx.GetNonce(), 8)
noncePart, err := formatL2TxHashParam(nonce, 8)
if err != nil {
return common.Hash{}, err

}
gasPricePart, err := formatL2TxHashParam(tx.GetPrice(), 32)
gasPricePart, err := formatL2TxHashParam(gasPrice, 32)
if err != nil {
return common.Hash{}, err
}
Expand All @@ -260,56 +266,55 @@ func ComputeL2TxHash(tx types.LegacyTx) (common.Hash, error) {
hash := fmt.Sprintf("%s%s%s%s", txType, noncePart, gasPricePart, gasLimitPart)

// check is deploy
if tx.GetTo() == nil || tx.GetTo().Hex() == "0x" {
if to == nil || to.Hex() == "0x0000000000000000000000000000000000000000" {
hash += "01"
} else {
toPart, err := formatL2TxHashParam(tx.GetTo().Hex(), 20)
toPart, err := formatL2TxHashParam(to.Hex(), 20)
if err != nil {
return common.Hash{}, err
}
hash += fmt.Sprintf("00%s", toPart)
}

// add value
valuePart, err := formatL2TxHashParam(tx.GetValue(), 32)
valuePart, err := formatL2TxHashParam(value, 32)
if err != nil {
return common.Hash{}, err
}
hash += valuePart

// compute data length
data := tx.GetData()
dataLength := len(data)
dataStr := hex.EncodeToHex(data)
if len(dataStr) > 1 && dataStr[:2] == "0x" {
dataStr = dataStr[2:]
}

//round to ceil
dataLength := (len(dataStr) + 1) / 2
dataLengthPart, err := formatL2TxHashParam(dataLength, 3)
if err != nil {
return common.Hash{}, err
}
hash += dataLengthPart

if dataLength > 0 {
dataPart, err := formatL2TxHashParam(data, dataLength)
dataPart, err := formatL2TxHashParam(dataStr, dataLength)
if err != nil {
return common.Hash{}, err
}
hash += dataPart
}

// add chainID
cid := tx.ChainID
if cid != nil {
chainIDPart, err := formatL2TxHashParam(cid, 8)
if chainId != nil {
chainIDPart, err := formatL2TxHashParam(chainId, 8)
if err != nil {
return common.Hash{}, err
}
hash += chainIDPart
}

// add from
sender, found := tx.GetSender()
if !found {
return common.Hash{}, fmt.Errorf("sender not found")
}
fromPart, err := formatL2TxHashParam(sender, 20)
fromPart, err := formatL2TxHashParam(from.Hex(), 20)
if err != nil {
return common.Hash{}, err
}
Expand Down Expand Up @@ -364,7 +369,7 @@ func formatL2TxHashParam(param interface{}, paramLength int) (string, error) {
paramStr = v.Hex()
}
case []uint8:
paramStr = hex.EncodeToString(v)
paramStr = hex.EncodeToHex(v)
case common.Address:
paramStr = v.Hex()
case string:
Expand Down
Loading

0 comments on commit 91c5513

Please sign in to comment.