Skip to content

Commit c3a18cd

Browse files
[CDK-46] Add DAP CLI on Validium node (#69)
* CLI Command * Remove unnecessary comments * Small fixes
1 parent b7fc8bc commit c3a18cd

File tree

4 files changed

+180
-0
lines changed

4 files changed

+180
-0
lines changed
+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package e2e
2+
3+
import (
4+
"context"
5+
"os/exec"
6+
"regexp"
7+
"testing"
8+
9+
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/polygondatacommittee"
10+
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/polygonzkevm"
11+
"github.com/0xPolygonHermez/zkevm-node/test/operations"
12+
"github.com/ethereum/go-ethereum/accounts/abi/bind"
13+
"github.com/ethereum/go-ethereum/common"
14+
"github.com/ethereum/go-ethereum/core/types"
15+
"github.com/ethereum/go-ethereum/ethclient"
16+
"github.com/stretchr/testify/require"
17+
)
18+
19+
func TestSetDataAvailabilityProtocol(t *testing.T) {
20+
if testing.Short() {
21+
t.Skip()
22+
}
23+
24+
ctx := context.Background()
25+
defer func() {
26+
require.NoError(t, operations.Teardown())
27+
}()
28+
29+
err := operations.Teardown()
30+
require.NoError(t, err)
31+
32+
opsCfg := operations.GetDefaultOperationsConfig()
33+
34+
opsman, err := operations.NewManager(ctx, opsCfg)
35+
require.NoError(t, err)
36+
37+
err = opsman.Setup()
38+
require.NoError(t, err)
39+
40+
clientL1, err := ethclient.Dial(operations.DefaultL1NetworkURL)
41+
require.NoError(t, err)
42+
43+
auth, err := operations.GetAuth(operations.DefaultSequencerPrivateKey, operations.DefaultL1ChainID)
44+
require.NoError(t, err)
45+
46+
zkEVM, err := polygonzkevm.NewPolygonzkevm(
47+
common.HexToAddress(operations.DefaultL1ZkEVMSmartContract),
48+
clientL1,
49+
)
50+
require.NoError(t, err)
51+
52+
currentDAPAddr, err := zkEVM.DataAvailabilityProtocol(&bind.CallOpts{Pending: false})
53+
require.NoError(t, err)
54+
require.Equal(t, common.HexToAddress(operations.DefaultL1DataCommitteeContract), currentDAPAddr)
55+
56+
// New DAC Setup
57+
newDAPAddr, tx, newDA, err := polygondatacommittee.DeployPolygondatacommittee(auth, clientL1)
58+
require.NoError(t, err)
59+
require.NotEqual(t, newDAPAddr, currentDAPAddr)
60+
require.NoError(t, operations.WaitTxToBeMined(ctx, clientL1, tx, operations.DefaultTimeoutTxToBeMined))
61+
62+
tx, err = newDA.Initialize(auth)
63+
require.NoError(t, err)
64+
require.NoError(t, operations.WaitTxToBeMined(ctx, clientL1, tx, operations.DefaultTimeoutTxToBeMined))
65+
66+
cmd := exec.Command("docker", "exec", "zkevm-sequence-sender",
67+
"/app/zkevm-node", "set-dap",
68+
"--da-addr", newDAPAddr.String(),
69+
"--network", "custom",
70+
"--custom-network-file", "/app/genesis.json",
71+
"--key-store-path", "/pk/sequencer.keystore",
72+
"--pw", "testonly",
73+
"--cfg", "/app/config.toml")
74+
75+
output, err := cmd.CombinedOutput()
76+
require.NoError(t, err)
77+
78+
txHash := common.HexToHash(extractHexFromString(string(output)))
79+
receipt, err := operations.WaitTxReceipt(ctx, txHash, operations.DefaultTimeoutTxToBeMined, clientL1)
80+
require.NoError(t, err)
81+
require.Equal(t, types.ReceiptStatusSuccessful, receipt.Status)
82+
83+
currentDAPAddr, err = zkEVM.DataAvailabilityProtocol(&bind.CallOpts{Pending: false})
84+
require.NoError(t, err)
85+
require.Equal(t, newDAPAddr, currentDAPAddr)
86+
}
87+
88+
func extractHexFromString(output string) string {
89+
re := regexp.MustCompile(`Transaction to set new data availability protocol sent. Hash: (0x[0-9a-fA-F]+)`)
90+
match := re.FindStringSubmatch(output)
91+
if len(match) > 1 {
92+
return match[1]
93+
}
94+
return ""
95+
}

cmd/main.go

+7
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ func main() {
186186
Action: restore,
187187
Flags: restoreFlags,
188188
},
189+
{
190+
Name: "set-data-availability-protocol",
191+
Aliases: []string{"set-dap"},
192+
Usage: "Sets the new data availability protocol",
193+
Action: setDataAvailabilityProtocol,
194+
Flags: setDataAvailabilityProtocolFlags,
195+
},
189196
}
190197

191198
err := app.Run(os.Args)

cmd/set_data_availability_protocol.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package main
2+
3+
import (
4+
"github.com/0xPolygonHermez/zkevm-node/config"
5+
"github.com/0xPolygonHermez/zkevm-node/log"
6+
"github.com/ethereum/go-ethereum/common"
7+
"github.com/urfave/cli/v2"
8+
)
9+
10+
const flagDAAddress = "data-availability-address"
11+
12+
var setDataAvailabilityProtocolFlags = []cli.Flag{
13+
&cli.StringFlag{
14+
Name: flagDAAddress,
15+
Aliases: []string{"da-addr"},
16+
Usage: "address of the new data availibility protocol",
17+
Required: true,
18+
},
19+
&cli.StringFlag{
20+
Name: config.FlagKeyStorePath,
21+
Aliases: []string{"ksp"},
22+
Usage: "the path of the key store file containing the private key of the account going to set new data availability protocol",
23+
Required: true,
24+
},
25+
&cli.StringFlag{
26+
Name: config.FlagPassword,
27+
Aliases: []string{"pw"},
28+
Usage: "the password do decrypt the key store file",
29+
Required: true,
30+
},
31+
&configFileFlag,
32+
&networkFlag,
33+
&customNetworkFlag,
34+
}
35+
36+
func setDataAvailabilityProtocol(ctx *cli.Context) error {
37+
c, err := config.Load(ctx, true)
38+
if err != nil {
39+
return err
40+
}
41+
42+
setupLog(c.Log)
43+
44+
daAddress := common.HexToAddress(ctx.String(flagDAAddress))
45+
addrKeyStorePath := ctx.String(config.FlagKeyStorePath)
46+
addrPassword := ctx.String(config.FlagPassword)
47+
48+
etherman, err := newEtherman(*c, nil)
49+
if err != nil {
50+
log.Fatal(err)
51+
return err
52+
}
53+
54+
auth, _, err := etherman.LoadAuthFromKeyStore(addrKeyStorePath, addrPassword)
55+
if err != nil {
56+
log.Fatal(err)
57+
return err
58+
}
59+
60+
tx, err := etherman.SetDataAvailabilityProtocol(auth.From, daAddress)
61+
if err != nil {
62+
return err
63+
}
64+
65+
log.Infof("Transaction to set new data availability protocol sent. Hash: %s", tx.Hash())
66+
67+
return nil
68+
}

etherman/etherman.go

+10
Original file line numberDiff line numberDiff line change
@@ -1874,3 +1874,13 @@ func (etherMan *Client) GetDAProtocolAddr() (common.Address, error) {
18741874
func (etherMan *Client) GetDAProtocolName() (string, error) {
18751875
return etherMan.DAProtocol.GetProcotolName(&bind.CallOpts{Pending: false})
18761876
}
1877+
1878+
// SetDataAvailabilityProtocol sets the address for the new data availability protocol
1879+
func (etherMan *Client) SetDataAvailabilityProtocol(from, daAddress common.Address) (*types.Transaction, error) {
1880+
auth, err := etherMan.getAuthByAddress(from)
1881+
if err != nil {
1882+
return nil, err
1883+
}
1884+
1885+
return etherMan.ZkEVM.SetDataAvailabilityProtocol(&auth, daAddress)
1886+
}

0 commit comments

Comments
 (0)