This repository was archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdeploy.go
144 lines (125 loc) · 4.04 KB
/
deploy.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
package commander
import (
"fmt"
"github.com/Worldcoin/hubble-commander/config"
"github.com/Worldcoin/hubble-commander/deployment"
"github.com/Worldcoin/hubble-commander/eth/chain"
"github.com/Worldcoin/hubble-commander/eth/deployer"
"github.com/Worldcoin/hubble-commander/eth/deployer/rollup"
"github.com/Worldcoin/hubble-commander/models"
st "github.com/Worldcoin/hubble-commander/storage"
"github.com/ethereum/go-ethereum/common"
log "github.com/sirupsen/logrus"
)
func Deploy(cfg *config.DeployerConfig, blockchain chain.Connection) (chainSpec *string, err error) {
tempStorage, err := st.NewTemporaryStorage()
if err != nil {
return nil, err
}
defer func() {
closeErr := tempStorage.Close()
if closeErr != nil {
err = fmt.Errorf("temporary storage close caused by: %w, failed with: %v", err, closeErr)
}
}()
log.Printf(
"Bootstrapping genesis state with %d accounts on chainId = %d",
len(cfg.Bootstrap.GenesisAccounts),
cfg.Ethereum.ChainID,
)
chainState, err := deployContractsAndSetupGenesisState(tempStorage.Storage, blockchain, cfg)
if err != nil {
return nil, err
}
chainSpec, err = GenerateChainSpec(chainState)
if err != nil {
return nil, err
}
return chainSpec, nil
}
func deployContractsAndSetupGenesisState(
storage *st.Storage,
blockchain chain.Connection,
cfg *config.DeployerConfig,
) (chainState *models.ChainState, err error) {
var chooserAddress *common.Address
if cfg.Bootstrap.Chooser != nil {
chooserAddress = cfg.Bootstrap.Chooser
} else {
chooserAddress, _, err = deployer.DeployProofOfAuthority(
blockchain,
cfg.Ethereum.MineTimeout,
[]common.Address{blockchain.GetAccount().From},
)
if err != nil {
return nil, err
}
}
accountTree := deployment.NewTree(st.AccountTreeDepth)
for i := range cfg.Bootstrap.GenesisAccounts {
account := cfg.Bootstrap.GenesisAccounts[i]
accountTree.RegisterAccount(&account.PublicKey)
}
accountTreeRoot := accountTree.LeftRoot()
accountSubtreesArray := (*[st.AccountTreeDepth - 1]common.Hash)(accountTree.Subtrees)
accountRegistryAddress, accountRegistryDeploymentBlock, _, err := deployer.DeployAccountRegistry(
blockchain,
chooserAddress,
cfg.Ethereum.MineTimeout,
&accountTreeRoot,
accountTree.AccountCount,
accountSubtreesArray,
)
if err != nil {
return nil, err
}
err = PopulateGenesisAccounts(storage, cfg.Bootstrap.GenesisAccounts)
if err != nil {
return nil, err
}
stateRoot, err := storage.StateTree.Root()
if err != nil {
return nil, err
}
totalGenesisAmount := models.NewUint256(0)
for i := range cfg.Bootstrap.GenesisAccounts {
account := cfg.Bootstrap.GenesisAccounts[i]
totalGenesisAmount = totalGenesisAmount.Add(&account.State.Balance)
}
wrappedConn, err := chain.NewManualNonceConnection(blockchain)
if err != nil {
return nil, err
}
contracts, err := rollup.DeployConfiguredRollup(wrappedConn, &rollup.DeploymentConfig{
Params: rollup.Params{
GenesisStateRoot: stateRoot,
BlocksToFinalise: models.NewUint256(uint64(cfg.Bootstrap.BlocksToFinalise)),
TotalGenesisAmount: totalGenesisAmount,
},
Dependencies: rollup.Dependencies{
AccountRegistry: accountRegistryAddress,
Chooser: chooserAddress,
},
MineTimeout: cfg.Ethereum.MineTimeout,
})
if err != nil {
return nil, err
}
initialSyncedBlock := getInitialSyncedBlock(*accountRegistryDeploymentBlock)
err = storage.SetSyncedBlock(initialSyncedBlock)
if err != nil {
return nil, err
}
chainState = &models.ChainState{
ChainID: blockchain.GetChainID(),
AccountRegistry: *accountRegistryAddress,
AccountRegistryDeploymentBlock: *accountRegistryDeploymentBlock,
TokenRegistry: contracts.TokenRegistryAddress,
SpokeRegistry: contracts.SpokeRegistryAddress,
DepositManager: contracts.DepositManagerAddress,
WithdrawManager: contracts.WithdrawManagerAddress,
Rollup: contracts.RollupAddress,
GenesisAccounts: cfg.Bootstrap.GenesisAccounts,
}
return chainState, nil
}