Skip to content

Commit

Permalink
Deploy proxy contracts on genesis
Browse files Browse the repository at this point in the history
  • Loading branch information
stana-miric committed Aug 28, 2023
1 parent e72ab8f commit 4d35e9d
Show file tree
Hide file tree
Showing 9 changed files with 274 additions and 94 deletions.
118 changes: 80 additions & 38 deletions command/genesis/polybft_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ var (
"except for zero address and reward wallet if native token is used as reward token")
)

type contractInfo struct {
artifact *artifact.Artifact
address types.Address
}

// generatePolyBftChainConfig creates and persists polybft chain configuration to the provided file path
func (p *genesisParams) generatePolyBftChainConfig(o command.OutputFormatter) error {
// populate premine balance map
Expand Down Expand Up @@ -365,78 +370,97 @@ func (p *genesisParams) deployContracts(
polybftConfig *polyCommon.PolyBFTConfig,
chainConfig *chain.Chain,
burnContractAddr types.Address) (map[types.Address]*chain.GenesisAccount, error) {

Check failure on line 372 in command/genesis/polybft_params.go

View workflow job for this annotation

GitHub Actions / golangci_lint

unnecessary leading newline (whitespace)
type contractInfo struct {
artifact *artifact.Artifact
address types.Address

proxyInputs := []types.Address{
contracts.StateReceiverContract,
contracts.ChildERC20Contract,
contracts.ChildERC721Contract,
contracts.ChildERC1155Contract,
contracts.BLSContract,
contracts.MerkleContract,
contracts.L2StateSenderContract,
contracts.ValidatorSetContract,
contracts.RewardPoolContract,
contracts.NetworkParamsContract,
contracts.ForkParamsContract,
contracts.ChildGovernorContract,
contracts.ChildTimelockContract,
contracts.NativeERC20TokenContract,
contracts.ChildERC20PredicateContract,
contracts.ChildERC721PredicateContract,
contracts.ChildERC1155PredicateContract,
contracts.RootMintableERC20PredicateContract,
contracts.RootMintableERC721PredicateContract,
contracts.RootMintableERC1155PredicateContract,
}

genesisContracts := []*contractInfo{
{
// State receiver contract
artifact: contractsapi.StateReceiver,
address: contracts.StateReceiverContract,
address: contracts.StateReceiverContractV1,
},
{
// ChildERC20 token contract
artifact: contractsapi.ChildERC20,
address: contracts.ChildERC20Contract,
address: contracts.ChildERC20ContractV1,
},
{
// ChildERC721 token contract
artifact: contractsapi.ChildERC721,
address: contracts.ChildERC721Contract,
address: contracts.ChildERC721ContractV1,
},
{
// ChildERC1155 contract
artifact: contractsapi.ChildERC1155,
address: contracts.ChildERC1155Contract,
address: contracts.ChildERC1155ContractV1,
},
{
// BLS contract
artifact: contractsapi.BLS,
address: contracts.BLSContract,
address: contracts.BLSContractV1,
},
{
// Merkle contract
artifact: contractsapi.Merkle,
address: contracts.MerkleContract,
address: contracts.MerkleContractV1,
},
{
// L2StateSender contract
artifact: contractsapi.L2StateSender,
address: contracts.L2StateSenderContract,
address: contracts.L2StateSenderContractV1,
},
{
artifact: contractsapi.ValidatorSet,
address: contracts.ValidatorSetContract,
address: contracts.ValidatorSetContractV1,
},
{
artifact: contractsapi.RewardPool,
address: contracts.RewardPoolContract,
address: contracts.RewardPoolContractV1,
},
{
artifact: contractsapi.NetworkParams,
address: contracts.NetworkParamsContract,
address: contracts.NetworkParamsContractV1,
},
{
artifact: contractsapi.ForkParams,
address: contracts.ForkParamsContract,
address: contracts.ForkParamsContractV1,
},
{
artifact: contractsapi.ChildGovernor,
address: contracts.ChildGovernorContract,
address: contracts.ChildGovernorContractV1,
},
{
artifact: contractsapi.ChildTimelock,
address: contracts.ChildTimelockContract,
address: contracts.ChildTimelockContractV1,
},
}

if !params.nativeTokenConfig.IsMintable {
genesisContracts = append(genesisContracts,
&contractInfo{
artifact: contractsapi.NativeERC20,
address: contracts.NativeERC20TokenContract,
address: contracts.NativeERC20TokenContractV1,
})

// burn contract can be set only for non-mintable native token. If burn contract is set,
Expand All @@ -447,12 +471,14 @@ func (p *genesisParams) deployContracts(
artifact: contractsapi.EIP1559Burn,
address: burnContractAddr,
})

proxyInputs = append(proxyInputs, contracts.DefaultBurnContract)
}
} else {
genesisContracts = append(genesisContracts,
&contractInfo{
artifact: contractsapi.NativeERC20Mintable,
address: contracts.NativeERC20TokenContract,
address: contracts.NativeERC20TokenContractV1,
})
}

Expand All @@ -461,95 +487,98 @@ func (p *genesisParams) deployContracts(
genesisContracts = append(genesisContracts,
&contractInfo{
artifact: contractsapi.ChildERC20PredicateACL,
address: contracts.ChildERC20PredicateContract,
address: contracts.ChildERC20PredicateContractV1,
})

genesisContracts = append(genesisContracts,
&contractInfo{
artifact: contractsapi.ChildERC721PredicateACL,
address: contracts.ChildERC721PredicateContract,
address: contracts.ChildERC721PredicateContractV1,
})

genesisContracts = append(genesisContracts,
&contractInfo{
artifact: contractsapi.ChildERC1155PredicateACL,
address: contracts.ChildERC1155PredicateContract,
address: contracts.ChildERC1155PredicateContractV1,
})

// childchain originated tokens predicates (with access lists)
genesisContracts = append(genesisContracts,
&contractInfo{
artifact: contractsapi.RootMintableERC20PredicateACL,
address: contracts.RootMintableERC20PredicateContract,
address: contracts.RootMintableERC20PredicateContractV1,
})

genesisContracts = append(genesisContracts,
&contractInfo{
artifact: contractsapi.RootMintableERC721PredicateACL,
address: contracts.RootMintableERC721PredicateContract,
address: contracts.RootMintableERC721PredicateContractV1,
})

genesisContracts = append(genesisContracts,
&contractInfo{
artifact: contractsapi.RootMintableERC1155PredicateACL,
address: contracts.RootMintableERC1155PredicateContract,
address: contracts.RootMintableERC1155PredicateContractV1,
})
} else {
// rootchain originated tokens predicates
genesisContracts = append(genesisContracts,
&contractInfo{
artifact: contractsapi.ChildERC20Predicate,
address: contracts.ChildERC20PredicateContract,
address: contracts.ChildERC20PredicateContractV1,
})

genesisContracts = append(genesisContracts,
&contractInfo{
artifact: contractsapi.ChildERC721Predicate,
address: contracts.ChildERC721PredicateContract,
address: contracts.ChildERC721PredicateContractV1,
})

genesisContracts = append(genesisContracts,
&contractInfo{
artifact: contractsapi.ChildERC1155Predicate,
address: contracts.ChildERC1155PredicateContract,
address: contracts.ChildERC1155PredicateContractV1,
})

// childchain originated tokens predicates
genesisContracts = append(genesisContracts,
&contractInfo{
artifact: contractsapi.RootMintableERC20Predicate,
address: contracts.RootMintableERC20PredicateContract,
address: contracts.RootMintableERC20PredicateContractV1,
})

genesisContracts = append(genesisContracts,
&contractInfo{
artifact: contractsapi.RootMintableERC721Predicate,
address: contracts.RootMintableERC721PredicateContract,
address: contracts.RootMintableERC721PredicateContractV1,
})

genesisContracts = append(genesisContracts,
&contractInfo{
artifact: contractsapi.RootMintableERC1155Predicate,
address: contracts.RootMintableERC1155PredicateContract,
address: contracts.RootMintableERC1155PredicateContractV1,
})
}

allocations := make(map[types.Address]*chain.GenesisAccount, len(genesisContracts)+1)

for _, contract := range genesisContracts {
allocations[contract.address] = &chain.GenesisAccount{
Balance: big.NewInt(0),
Code: contract.artifact.DeployedBytecode,
}
}

if rewardTokenByteCode != nil {
// if reward token is provided in genesis then, add it to allocations
// to RewardTokenContract address and update Polybft config
allocations[contracts.RewardTokenContract] = &chain.GenesisAccount{
allocations[contracts.RewardTokenContractV1] = &chain.GenesisAccount{
Balance: big.NewInt(0),
Code: rewardTokenByteCode,
}
proxyInputs = append(proxyInputs, contracts.RewardTokenContract)

Check failure on line 572 in command/genesis/polybft_params.go

View workflow job for this annotation

GitHub Actions / golangci_lint

append only allowed to cuddle with appended value (wsl)
}

genesisContracts = append(genesisContracts, deployProxyContracts(proxyInputs)...)

for _, contract := range genesisContracts {
allocations[contract.address] = &chain.GenesisAccount{
Balance: big.NewInt(0),
Code: contract.artifact.DeployedBytecode,
}
}

return allocations, nil
Expand Down Expand Up @@ -615,3 +644,16 @@ func stringSliceToAddressSlice(addrs []string) []types.Address {

return res
}

func deployProxyContracts(inputs []types.Address) []*contractInfo {
res := make([]*contractInfo, len(inputs))

for i, proxyAddress := range inputs {
res[i] = &contractInfo{
artifact: contractsapi.GenesisProxy,
address: proxyAddress,
}
}

return res
}
4 changes: 4 additions & 0 deletions consensus/polybft/contractsapi/artifacts-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ func main() {
"child/governance/ChildTimelock.sol",
"ChildTimelock",
},
{
"lib/GenesisProxy.sol",
"GenesisProxy",
},
}

str := `// This is auto-generated file. DO NOT EDIT.
Expand Down
9 changes: 9 additions & 0 deletions consensus/polybft/contractsapi/bindings-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,15 @@ func main() {
},
[]string{},
},
{
"GenesisProxy",
gensc.GenesisProxy,
false,
[]string{
"setUpProxy",
},
[]string{},
},
}

generatedData := &generatedData{}
Expand Down
18 changes: 18 additions & 0 deletions consensus/polybft/contractsapi/contractsapi.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 12 additions & 11 deletions consensus/polybft/contractsapi/gen_sc_data.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions consensus/polybft/contractsapi/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ var (
RootERC721 *artifact.Artifact
RootERC1155 *artifact.Artifact
EIP1559Burn *artifact.Artifact
GenesisProxy *artifact.Artifact

// Governance
NetworkParams *artifact.Artifact
Expand Down Expand Up @@ -307,6 +308,11 @@ func init() {
if err != nil {
log.Fatal(err)
}

GenesisProxy, err = artifact.DecodeArtifact([]byte(GenesisProxyArtifact))
if err != nil {
log.Fatal(err)
}
}

func readTestContractContent(contractFileName string) []byte {
Expand Down
Loading

0 comments on commit 4d35e9d

Please sign in to comment.