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