Skip to content

Commit

Permalink
fix ut
Browse files Browse the repository at this point in the history
  • Loading branch information
zjg555543 committed Oct 24, 2023
1 parent 61d2c56 commit d962309
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion etherman/etherman_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func newTestingEnv() (ethman *Client, ethBackend *backends.SimulatedBackend, aut
if err != nil {
log.Fatal(err)
}
ethman, ethBackend, maticAddr, bridge, err = NewSimulatedEtherman(Config{}, auth)
ethman, ethBackend, maticAddr, bridge, _, err = NewSimulatedEtherman(Config{}, auth)
if err != nil {
log.Fatal(err)
}
Expand Down
49 changes: 32 additions & 17 deletions etherman/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package etherman
import (
"context"
"fmt"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/datacommittee"

Check failure on line 6 in etherman/simulated.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard,default (gci)
"math/big"

Check failure on line 8 in etherman/simulated.go

View workflow job for this annotation

GitHub Actions / lint

File is not `goimports`-ed (goimports)
mockbridge "github.com/0xPolygonHermez/zkevm-bridge-service/test/mocksmartcontracts/polygonzkevmbridge"

Check failure on line 9 in etherman/simulated.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard,default (gci)
Expand All @@ -20,7 +21,7 @@ import (

// NewSimulatedEtherman creates an etherman that uses a simulated blockchain. It's important to notice that the ChainID of the auth
// must be 1337. The address that holds the auth will have an initial balance of 10 ETH
func NewSimulatedEtherman(cfg Config, auth *bind.TransactOpts) (etherman *Client, ethBackend *backends.SimulatedBackend, maticAddr common.Address, mockBridge *mockbridge.Polygonzkevmbridge, err error) {
func NewSimulatedEtherman(cfg Config, auth *bind.TransactOpts) (etherman *Client, ethBackend *backends.SimulatedBackend, maticAddr common.Address, mockBridge *mockbridge.Polygonzkevmbridge, da *datacommittee.Datacommittee, err error) {
// 10000000 ETH in wei
balance, _ := new(big.Int).SetString("10000000000000000000000000", 10) //nolint:gomnd
address := auth.From
Expand All @@ -32,20 +33,34 @@ func NewSimulatedEtherman(cfg Config, auth *bind.TransactOpts) (etherman *Client
blockGasLimit := uint64(999999999999999999) //nolint:gomnd
client := backends.NewSimulatedBackend(genesisAlloc, blockGasLimit)

// DAC Setup
dataCommitteeAddr, _, da, err := datacommittee.DeployDatacommittee(auth, client)
if err != nil {
return nil, nil, common.Address{}, nil, nil, err
}
_, err = da.Initialize(auth)
if err != nil {
return nil, nil, common.Address{}, nil, nil, err
}
_, err = da.SetupCommittee(auth, big.NewInt(0), []string{}, []byte{})
if err != nil {
return nil, nil, common.Address{}, nil, nil, err
}

// Deploy contracts
const maticDecimalPlaces = 18
totalSupply, _ := new(big.Int).SetString("10000000000000000000000000000", 10) //nolint:gomnd
maticAddr, _, maticContract, err := matic.DeployMatic(auth, client, "Matic Token", "MATIC", maticDecimalPlaces, totalSupply)
if err != nil {
return nil, nil, common.Address{}, nil, err
return nil, nil, common.Address{}, nil, nil, err
}
rollupVerifierAddr, _, _, err := mockverifier.DeployMockverifier(auth, client)
if err != nil {
return nil, nil, common.Address{}, nil, err
return nil, nil, common.Address{}, nil, nil, err
}
nonce, err := client.PendingNonceAt(context.TODO(), auth.From)
if err != nil {
return nil, nil, common.Address{}, nil, err
return nil, nil, common.Address{}, nil, nil, err
}
const posBridge = 1
calculatedBridgeAddr := crypto.CreateAddress(auth.From, nonce+posBridge)
Expand All @@ -54,23 +69,23 @@ func NewSimulatedEtherman(cfg Config, auth *bind.TransactOpts) (etherman *Client
genesis := common.HexToHash("0xfd3434cd8f67e59d73488a2b8da242dd1f02849ea5dd99f0ca22c836c3d5b4a9") // Random value. Needs to be different to 0x0
exitManagerAddr, _, globalExitRoot, err := polygonzkevmglobalexitroot.DeployPolygonzkevmglobalexitroot(auth, client, calculatedPolygonZkEVMAddress, calculatedBridgeAddr)
if err != nil {
return nil, nil, common.Address{}, nil, err
return nil, nil, common.Address{}, nil, nil, err
}
bridgeAddr, _, mockbr, err := mockbridge.DeployPolygonzkevmbridge(auth, client)
if err != nil {
return nil, nil, common.Address{}, nil, err
return nil, nil, common.Address{}, nil, nil, err
}
polygonZkEVMAddress, _, polygonZkEVMContract, err := polygonzkevm.DeployPolygonzkevm(auth, client, exitManagerAddr, maticAddr, rollupVerifierAddr, bridgeAddr, bridgeAddr, 1000, 1) //nolint
polygonZkEVMAddress, _, polygonZkEVMContract, err := polygonzkevm.DeployPolygonzkevm(auth, client, exitManagerAddr, maticAddr, rollupVerifierAddr, bridgeAddr, dataCommitteeAddr, 1000, 1) //nolint
if err != nil {
return nil, nil, common.Address{}, nil, err
return nil, nil, common.Address{}, nil, nil, err
}
_, err = mockbr.Initialize(auth, 0, exitManagerAddr, polygonZkEVMAddress)
if err != nil {
return nil, nil, common.Address{}, nil, err
return nil, nil, common.Address{}, nil, nil, err
}
br, err := polygonzkevmbridge.NewPolygonzkevmbridge(bridgeAddr, client)
if err != nil {
return nil, nil, common.Address{}, nil, err
return nil, nil, common.Address{}, nil, nil, err
}
polygonZkEVMParams := polygonzkevm.PolygonZkEVMInitializePackedParameters{
Admin: auth.From,
Expand All @@ -81,33 +96,33 @@ func NewSimulatedEtherman(cfg Config, auth *bind.TransactOpts) (etherman *Client
}
_, err = polygonZkEVMContract.Initialize(auth, polygonZkEVMParams, genesis, "http://localhost", "L2", "v1") //nolint:gomnd
if err != nil {
return nil, nil, common.Address{}, nil, err
return nil, nil, common.Address{}, nil, nil, err
}

if calculatedBridgeAddr != bridgeAddr {
return nil, nil, common.Address{}, nil, fmt.Errorf("bridgeAddr (%s) is different from the expected contract address (%s)",
return nil, nil, common.Address{}, nil, nil, fmt.Errorf("bridgeAddr (%s) is different from the expected contract address (%s)",
bridgeAddr.String(), calculatedBridgeAddr.String())
}
if calculatedPolygonZkEVMAddress != polygonZkEVMAddress {
return nil, nil, common.Address{}, nil, fmt.Errorf("polygonZkEVMAddress (%s) is different from the expected contract address (%s)",
return nil, nil, common.Address{}, nil, nil, fmt.Errorf("polygonZkEVMAddress (%s) is different from the expected contract address (%s)",
polygonZkEVMAddress.String(), calculatedPolygonZkEVMAddress.String())
}

// Approve the bridge and polygonZkEVM to spend 10000 matic tokens.
approvedAmount, _ := new(big.Int).SetString("10000000000000000000000", 10) //nolint:gomnd
_, err = maticContract.Approve(auth, bridgeAddr, approvedAmount)
if err != nil {
return nil, nil, common.Address{}, nil, err
return nil, nil, common.Address{}, nil, nil, err
}
_, err = maticContract.Approve(auth, polygonZkEVMAddress, approvedAmount)
if err != nil {
return nil, nil, common.Address{}, nil, err
return nil, nil, common.Address{}, nil, nil, err
}
_, err = polygonZkEVMContract.ActivateForceBatches(auth)
if err != nil {
return nil, nil, common.Address{}, nil, err
return nil, nil, common.Address{}, nil, nil, err
}

client.Commit()
return &Client{EtherClient: client, PolygonBridge: br, PolygonZkEVMGlobalExitRoot: globalExitRoot, SCAddresses: []common.Address{exitManagerAddr, bridgeAddr}}, client, maticAddr, mockbr, nil
return &Client{EtherClient: client, PolygonBridge: br, PolygonZkEVMGlobalExitRoot: globalExitRoot, SCAddresses: []common.Address{exitManagerAddr, bridgeAddr}}, client, maticAddr, mockbr, nil, nil
}

0 comments on commit d962309

Please sign in to comment.