Skip to content

Commit

Permalink
Support pessimistic proofs consensus (#23)
Browse files Browse the repository at this point in the history
* Support for PP consensus type and populate batch l2 data

* Add comments

* Use the L2GERManager address

* Reuse the flags

* Increase the indentation when marshalling into json files

* Support loading genesis allocs by the l1 network, rollup manager and rollup aliases (for PP rollups)

* Move FEP allocs and create example for PP rollups

* Fix the import rollup manager cmd

* Update bali rollup manager and rollup definitions

* Encode tx signature separately and append it to the RLP encoded tx

* Rename l2ChainID json tag

* Encode effective percentage to the raw transaction

* Retrieve the last global exit root for PP and store it in combined json
  • Loading branch information
Stefan-Ethernal authored Nov 27, 2024
1 parent 44e5e6b commit c1a2f3d
Show file tree
Hide file tree
Showing 34 changed files with 684 additions and 231 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,34 @@ Some commands require using RPC endpoints. There are multiple ways to import the

### Genesis

Unfortunately the base genesis file cannot be retrieved from L1. Therefore they must be imported manually at `./genesis/<root>.json`
Unfortunately the base genesis file cannot be retrieved from L1. Therefore they must be imported manually.
#### Full execution proofs rollups
For the full execution proofs rollups, the location that should contain the genesis allocations is `./genesis/fep/<root>.json`

#### Pessimistic proofs rollups
For the pessimistic proofs rollups (since they are initialized with an empty genesis root), the location that should contain the genesis allocations is constructed following the pattern `./genesis/pp/<l1_network_alias>/<rollup_manager_alias>/<rollup_alias>/allocs.json`.

**Example**

Let's assume that we have a pessimistic proofs rollup named `cdk-pp-1` running on the `cardona`, that is pointing to `sepolia` l1 network.

The genesis file should be named as `allocs.json` and placed in the `./genesis/pp/sepolia/cardona/cdk-pp-1/`, so the full path is `./genesis/pp/sepolia/cardona/cdk-pp-1/allocs.json`.

### Examples

#### Generate genesis file for a node (`cdk-validium-node` or `zkevm-node`) and the config for a Bridge service (`zkevm-bridge-service`)

1. Import the rollup manager: `go run ./cmd import-rm -l1 sepolia -addr 0x32d33d5137a7cffb54c5bf8371172bcec5f310ff -alias cardona`. In this example:
- `-l1 sepolia` is the L1 network, make sure that your `wallets.toml` file contains a valid RPC for the L1 network this CDK belongs to
- `-l1 sepolia` is the L1 network, make sure that your `rpcs.toml` file contains a valid RPC for the L1 network this CDK belongs to
- `-addr 0x32d33d5137a7cffb54c5bf8371172bcec5f310ff` is the L1 address where the rollup manager is deployed
- `-alias cardona` is an arbitrary name. You can really name this however you want, but this is going to be used later to reference this rollup manager deployment
2. (OPTIONAL) After first step, all the attached rollups are imported using their name as alias, and defaulting to chainID for name if the name has the default value `networkName`. It's possible to manually import a rollup using a custom alias: `go run ./cmd import-r -l1 sepolia -rm cardona -r API3 -chainid 879490799`. In this example:
- `-rm cardona` rollup manager referenced by the alias used in the previous step
- `-chainid 879490799` is the chain ID of the rollup (L2 Chain ID)
- `-alias API3` is an arbitrary name. You can really name this however you want, but this is going to be used later to reference this rollup deployment
3. Generate the genesis file for the node: `go run ./cmd genesis -l1 sepolia -rm cardona -r API3 -output API3.json`. In this example:
- `-l1 sepolia` is the L1 network
- `-rm cardona` rollup manager referenced by the alias used in the step 1 (import rollup manager)
- `-r API3` rollup referenced by the alias used in the previous step
- `-output API3.json` file where the genesis will be stored
4. Generate the network config section of the bridge service: `go run ./cmd bridge -l1 sepolia -rm cardona -r API3 -output API3Bridge.toml`
Expand Down
2 changes: 1 addition & 1 deletion cmd/bridgeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func bridgeConfig(cliCtx *cli.Context) error {
rmAlias := cliCtx.String(rollupManagerAliasFlagName)
rAlias := cliCtx.String(rollupAliasFlagName)
outputFilePath := cliCtx.String(outputFileFlagName)
_, rm, r, _, err := config.Load(l1Network, rmAlias, rAlias, baseDir)
_, rm, r, err := config.Load(l1Network, rmAlias, rAlias, baseDir)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var (
l1Flag = &cli.StringFlag{
Name: l1FlagName,
Aliases: []string{"l1"},
Usage: "L1 network, such as sepolia, local, goerli, mainnet, ... Should match an entry on the rpcs.toml file",
Usage: "L1 network, such as sepolia, local, goerli, mainnet etc. Should match an entry on the rpcs.toml file",
Required: true,
}
outputFlag = &cli.StringFlag{
Expand Down
70 changes: 51 additions & 19 deletions cmd/importcombinedjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"path"

"github.com/0xPolygon/cdk-contracts-tooling/config"
"github.com/0xPolygon/cdk-contracts-tooling/rollup"
"github.com/0xPolygon/cdk-contracts-tooling/rollupmanager"
"github.com/ethereum/go-ethereum/common"
"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -38,9 +40,11 @@ type CombinedJSON struct {
RollupAddress common.Address `json:"rollupAddress"`
ConsensusContract string `json:"consensusContract"`
RollupID uint32 `json:"rollupID"`
L2ChainID uint64 `json:"L2ChainID"`
L2ChainID uint64 `json:"l2ChainID"`
RollupGasTokenAddress common.Address `json:"gasTokenAddress"`
DACAddress common.Address `json:"polygonDataCommitteeAddress"`
BatchL2Data string `json:"batchL2Data,omitempty"`
LastGlobalExitRoot common.Hash `json:"globalExitRoot,omitempty"`
}

func importCombinedJson(cliCtx *cli.Context) error {
Expand All @@ -53,7 +57,7 @@ func importCombinedJson(cliCtx *cli.Context) error {
rmAlias := cliCtx.String(rollupManagerAliasFlagName)
rAlias := cliCtx.String(rollupAliasFlagName)

rpcs, rollupManager, rollup, _, err := config.Load(l1Network, rmAlias, rAlias, baseDir)
rpcs, rollupManager, rollupMetadata, err := config.Load(l1Network, rmAlias, rAlias, baseDir)
if err != nil {
return err
}
Expand All @@ -67,18 +71,44 @@ func importCombinedJson(cliCtx *cli.Context) error {
return err
}

if err := rollup.InitContract(cliCtx.Context, client); err != nil {
return err
}

consensusDesc, err := rollupManager.GetConsensusDescription(cliCtx.Context, rollup.RollupID)
consensusDesc, err := rollupManager.GetConsensusDescription(cliCtx.Context, rollupMetadata.RollupID)
if err != nil {
return err
}

dac, err := rollup.Contract.DataAvailabilityProtocol(nil)
if err != nil {
fmt.Println("")
var (
dacAddr common.Address
batchL2Data string
lastGlobalExitRoot common.Hash
)

switch rollupMetadata.VerifierType {
case rollupmanager.Pessimistic:
r := &rollup.RollupPessimisticProofs{RollupMetadata: rollupMetadata}
if err := r.InitContract(cliCtx.Context, client); err != nil {
return err
}

batchL2Data, err = r.GetBatchL2Data(client)
if err != nil {
return fmt.Errorf("failed to retrieve batch l2 data %w", err)
}

lastGlobalExitRoot, err = r.GetLastGlobalExitRoot(rollupManager.GERAddr, client)
if err != nil {
return fmt.Errorf("failed to retrieve batch l2 data %w", err)
}

default:
r := &rollup.RollupValidium{RollupMetadata: rollupMetadata}
if err := r.InitContract(cliCtx.Context, client); err != nil {
return err
}

dacAddr, err = r.Contract.DataAvailabilityProtocol(nil)
if err != nil {
fmt.Println("failed to retrieve data availability protocol address", "reason:", err)
}
}

combinedJson := &CombinedJSON{
Expand All @@ -88,18 +118,20 @@ func importCombinedJson(cliCtx *cli.Context) error {
PolTokenAddress: rollupManager.POLAddr,
RollupManagerCreationBlock: rollupManager.CreationBlock,
UpdateToULxLyBlock: rollupManager.UpdateToULxLyBlock,
Genesis: rollup.GenesisRoot,
RollupCreationBlock: rollup.CreationBlock,
RollupCreationTimestamp: rollup.CreationTimestamp,
RollupAddress: rollup.Address,
Genesis: rollupMetadata.GenesisRoot,
RollupCreationBlock: rollupMetadata.CreationBlock,
RollupCreationTimestamp: rollupMetadata.CreationTimestamp,
RollupAddress: rollupMetadata.Address,
ConsensusContract: consensusDesc,
RollupID: rollup.RollupID,
L2ChainID: rollup.ChainID,
RollupGasTokenAddress: rollup.GasToken,
DACAddress: dac,
RollupID: rollupMetadata.RollupID,
L2ChainID: rollupMetadata.ChainID,
RollupGasTokenAddress: rollupMetadata.GasToken,
DACAddress: dacAddr,
BatchL2Data: batchL2Data,
LastGlobalExitRoot: lastGlobalExitRoot,
}

raw, err := json.MarshalIndent(combinedJson, "", " ")
raw, err := json.MarshalIndent(combinedJson, "", " ")
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/importcontracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func importContract(contractPath, storingPath string) error {
// add abi and bin files
splitted := strings.Split(contractPath, "/")
name := strings.TrimSuffix(splitted[len(splitted)-1], ".json")
abiData, err := json.MarshalIndent(contract.ABI, "", " ")
abiData, err := json.MarshalIndent(contract.ABI, "", " ")
if err != nil {
return err
}
Expand Down
14 changes: 3 additions & 11 deletions cmd/importrollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,7 @@ var (
Action: importRollup,
Flags: []cli.Flag{
l1Flag,
&cli.StringFlag{
Name: rollupManagerAliasFlagName,
Aliases: []string{"rm"},
Usage: fmt.Sprintf(
"Name of the rollup manager to which the rollup belongs. Needs to match an already imported rollup manager (can be done by running the %s command)",
importRollupManagerCommandName,
),
Required: true,
},
rollupManagerAliasFlag,
&cli.Uint64Flag{
Name: chainIDFlagName,
Aliases: []string{"id", "chainid", "chain-id"},
Expand Down Expand Up @@ -78,7 +70,7 @@ func importRollup(cliCtx *cli.Context) error {

fmt.Println("fetching on-chain info for the rollup")
chainID := cliCtx.Uint64(chainIDFlagName)
r, err := rollup.LoadFromL1ByChainID(client, rm, chainID)
r, err := rollup.LoadMetadataFromL1ByChainID(client, rm, chainID)
if err != nil {
return err
}
Expand All @@ -90,7 +82,7 @@ func importRollup(cliCtx *cli.Context) error {
if err != nil {
return err
}
rData, err := json.MarshalIndent(r, "", " ")
rData, err := json.MarshalIndent(r, "", " ")
if err != nil {
return err
}
Expand Down
24 changes: 14 additions & 10 deletions cmd/importrollupmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"os"
"path"
"strconv"

"github.com/0xPolygon/cdk-contracts-tooling/config"
"github.com/0xPolygon/cdk-contracts-tooling/rollup"
Expand Down Expand Up @@ -78,7 +77,7 @@ func importRollupManager(cliCtx *cli.Context) error {
if err != nil {
return err
}
rmData, err := json.MarshalIndent(rm, "", " ")
rmData, err := json.MarshalIndent(rm, "", " ")
if err != nil {
return err
}
Expand All @@ -99,26 +98,31 @@ func importRollupManager(cliCtx *cli.Context) error {
}
for chainID, name := range rollups {
fmt.Println("importing rollup ", name, " with chainID ", chainID)
r, err := rollup.LoadFromL1ByChainID(client, rm, chainID)
r, err := rollup.LoadMetadataFromL1ByChainID(client, rm, chainID)
if err != nil {
return err
}
rData, err := json.MarshalIndent(r, "", " ")
rData, err := json.MarshalIndent(r, "", " ")
if err != nil {
return err
}
if name == "networkName" || name == "" {
name = strconv.Itoa(int(chainID))
name = fmt.Sprintf("%d", chainID)
}
err = os.WriteFile(path.Join(rollupPath, name+".json"), rData, 0644)
if err != nil {
return err
}
if _, err := os.Stat(path.Join(baseDir, "genesis", r.GenesisRoot.Hex()+".json")); errors.Is(err, os.ErrNotExist) {
fmt.Printf(
"WARNING: the rollup %s with chain id %d uses a genesis with root %s. But there is no such genesis file. Please manually import it into the ./genesis directory.\n",
name, chainID, r.GenesisRoot.Hex(),
)

if r.VerifierType != rollupmanager.Pessimistic {
// for pessimistic proofs, the genesis root should be an empty hash
// (https://github.com/0xPolygonHermez/zkevm-contracts/blob/c8659e6282340de7bdb8fdbf7924a9bd2996bc98/contracts/v2/PolygonRollupManager.sol#L443-L446)
if _, err := os.Stat(path.Join(baseDir, "genesis", fepDirName, r.GenesisRoot.Hex()+".json")); errors.Is(err, os.ErrNotExist) {
fmt.Printf(
"WARNING: the rollup %s with chain id %d uses a genesis with root %s. But there is no such genesis file. Please manually import it into the ./genesis directory.\n",
name, chainID, r.GenesisRoot.Hex(),
)
}
}
}
return nil
Expand Down
Loading

0 comments on commit c1a2f3d

Please sign in to comment.