forked from RiemaLabs/cdk-validium-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulated.go
233 lines (219 loc) · 9.19 KB
/
simulated.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package etherman
import (
"context"
"fmt"
"math/big"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/mockpolygonrollupmanager"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/mockverifier"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/pol"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/polygondatacommittee"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/polygonrollupmanager"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/polygonzkevm"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/polygonzkevmbridge"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/polygonzkevmglobalexitroot"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/proxy"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient/simulated"
)
// 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, daBackend dataAvailabilityProvider) (etherman *Client, ethBackend *simulated.Backend, polAddr common.Address, br *polygonzkevmbridge.Polygonzkevmbridge, err error) {
if auth == nil {
// read only client
return &Client{}, nil, common.Address{}, nil, nil
}
// 10000000 ETH in wei
balance, _ := new(big.Int).SetString("10000000000000000000000000", 10) //nolint:gomnd
address := auth.From
genesisAlloc := map[common.Address]core.GenesisAccount{
address: {
Balance: balance,
},
}
blockGasLimit := uint64(999999999999999999) //nolint:gomnd
// client := simulated.NewBackend(genesisAlloc, simulated.WithBlockGasLimit(blockGasLimit))
client := simulated.NewBackend(genesisAlloc, simulated.WithBlockGasLimit(blockGasLimit))
// DAC Setup
daAddr, _, da, err := polygondatacommittee.DeployPolygondatacommittee(auth, client.Client())
if err != nil {
return nil, nil, common.Address{}, nil, err
}
client.Commit()
_, err = da.Initialize(auth)
if err != nil {
return nil, nil, common.Address{}, nil, err
}
client.Commit()
_, err = da.SetupCommittee(auth, big.NewInt(0), []string{}, []byte{})
if err != nil {
return nil, nil, common.Address{}, nil, err
}
client.Commit()
// Deploy contracts
const polDecimalPlaces = 18
totalSupply, _ := new(big.Int).SetString("10000000000000000000000000000", 10) //nolint:gomnd
polAddr, _, polContract, err := pol.DeployPol(auth, client.Client(), "Pol Token", "POL", polDecimalPlaces, totalSupply)
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
rollupVerifierAddr, _, _, err := mockverifier.DeployMockverifier(auth, client.Client())
if err != nil {
return nil, nil, common.Address{}, nil, err
}
nonce, err := client.Client().PendingNonceAt(context.TODO(), auth.From)
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
const posBridge = 3
calculatedBridgeAddr := crypto.CreateAddress(auth.From, nonce+posBridge)
const posRollupManager = 4
calculatedRollupManagerAddr := crypto.CreateAddress(auth.From, nonce+posRollupManager)
genesis := common.HexToHash("0xfd3434cd8f67e59d73488a2b8da242dd1f02849ea5dd99f0ca22c836c3d5b4a9") // Random value. Needs to be different to 0x0
exitManagerAddr, _, globalExitRoot, err := polygonzkevmglobalexitroot.DeployPolygonzkevmglobalexitroot(auth, client.Client(), calculatedRollupManagerAddr, calculatedBridgeAddr)
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
implementationBridgeAddr, _, _, err := polygonzkevmbridge.DeployPolygonzkevmbridge(auth, client.Client())
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
implementationMockRollupManagerAddr, _, _, err := mockpolygonrollupmanager.DeployMockpolygonrollupmanager(auth, client.Client(), exitManagerAddr, polAddr, calculatedBridgeAddr)
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
client.Commit()
bridgeAddr, _, _, err := proxy.DeployProxy(auth, client.Client(), implementationBridgeAddr, implementationBridgeAddr, []byte{})
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
mockRollupManagerAddr, _, _, err := proxy.DeployProxy(auth, client.Client(), implementationMockRollupManagerAddr, implementationMockRollupManagerAddr, []byte{})
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
if calculatedRollupManagerAddr != mockRollupManagerAddr {
return nil, nil, common.Address{}, nil, fmt.Errorf("RollupManagerAddr (%s) is different from the expected contract address (%s)",
mockRollupManagerAddr.String(), calculatedRollupManagerAddr.String())
}
initZkevmAddr, _, _, err := polygonzkevm.DeployPolygonzkevm(auth, client.Client(), exitManagerAddr, polAddr, bridgeAddr, mockRollupManagerAddr)
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
mockRollupManager, err := mockpolygonrollupmanager.NewMockpolygonrollupmanager(mockRollupManagerAddr, client.Client())
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
br, err = polygonzkevmbridge.NewPolygonzkevmbridge(bridgeAddr, client.Client())
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
client.Commit()
_, err = br.Initialize(auth, 0, common.Address{}, 0, exitManagerAddr, mockRollupManagerAddr, []byte{})
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
client.Commit()
_, err = mockRollupManager.Initialize(auth, auth.From, 10000, 10000, auth.From, auth.From, auth.From, common.Address{}, common.Address{}, 0, 0) //nolint:gomnd
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
client.Commit()
_, err = mockRollupManager.AddNewRollupType(auth, initZkevmAddr, rollupVerifierAddr, 6, 0, genesis, "PolygonZkEvm Rollup") //nolint:gomnd
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
client.Commit()
rollUpTypeID, err := mockRollupManager.RollupTypeCount(&bind.CallOpts{Pending: false})
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
var zkevmChainID uint64 = 100
_, err = mockRollupManager.CreateNewRollup(auth, rollUpTypeID, zkevmChainID, auth.From, auth.From, common.Address{}, "http://localhost", "PolygonZkEvm Rollup")
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
client.Commit()
rollupID, err := mockRollupManager.ChainIDToRollupID(&bind.CallOpts{Pending: false}, zkevmChainID)
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
rollupData, err := mockRollupManager.RollupIDToRollupData(&bind.CallOpts{Pending: false}, rollupID)
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
zkevmAddr := rollupData.RollupContract
if calculatedBridgeAddr != bridgeAddr {
return nil, nil, common.Address{}, nil, fmt.Errorf("bridgeAddr (%s) is different from the expected contract address (%s)",
bridgeAddr.String(), calculatedBridgeAddr.String())
}
rollupManager, err := polygonrollupmanager.NewPolygonrollupmanager(mockRollupManagerAddr, client.Client())
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
trueZkevm, err := polygonzkevm.NewPolygonzkevm(zkevmAddr, client.Client()) //nolint
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
// Approve the bridge and zkevm to spend 10000 pol tokens.
approvedAmount, _ := new(big.Int).SetString("10000000000000000000000", 10) //nolint:gomnd
_, err = polContract.Approve(auth, bridgeAddr, approvedAmount)
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
_, err = polContract.Approve(auth, zkevmAddr, approvedAmount)
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
_, err = trueZkevm.SetDataAvailabilityProtocol(auth, daAddr)
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
_, err = trueZkevm.SetForceBatchAddress(auth, common.Address{})
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
client.Commit()
c := &Client{
EthClient: client.Client(),
ZkEVM: trueZkevm,
RollupManager: rollupManager,
Pol: polContract,
GlobalExitRootManager: globalExitRoot,
RollupID: rollupID,
SCAddresses: []common.Address{zkevmAddr, mockRollupManagerAddr, exitManagerAddr},
auth: map[common.Address]bind.TransactOpts{},
cfg: cfg,
da: daBackend,
}
err = c.AddOrReplaceAuth(*auth)
if err != nil {
return nil, nil, common.Address{}, nil, err
}
return c, client, polAddr, br, nil
}