Skip to content

Commit

Permalink
Integrate bridge access list contracts (#1456)
Browse files Browse the repository at this point in the history
* Integrate AccessLists predicates

In the same address as the alternative ERC20/721/1155 Predicates
  • Loading branch information
vcastellm authored May 4, 2023
1 parent 434e8bf commit 5e3d3ff
Show file tree
Hide file tree
Showing 10 changed files with 644 additions and 85 deletions.
77 changes: 57 additions & 20 deletions command/genesis/polybft_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,32 @@ func (p *genesisParams) generatePolyBftChainConfig(o command.OutputFormatter) er
}
}

// check if there are Bridge Allow List Admins and Bridge Block List Admins
// and if there are, get the first address as the Admin
var bridgeAllowListAdmin types.Address
if len(p.bridgeAllowListAdmin) > 0 {
bridgeAllowListAdmin = types.StringToAddress(p.bridgeAllowListAdmin[0])
}

var bridgeBlockListAdmin types.Address
if len(p.bridgeBlockListAdmin) > 0 {
bridgeBlockListAdmin = types.StringToAddress(p.bridgeBlockListAdmin[0])
}

polyBftConfig := &polybft.PolyBFTConfig{
InitialValidatorSet: initialValidators,
BlockTime: common.Duration{Duration: p.blockTime},
EpochSize: p.epochSize,
SprintSize: p.sprintSize,
EpochReward: p.epochReward,
// use 1st account as governance address
Governance: initialValidators[0].Address,
InitialTrieRoot: types.StringToHash(p.initialStateRoot),
MintableNativeToken: p.mintableNativeToken,
NativeTokenConfig: p.nativeTokenConfig,
MaxValidatorSetSize: p.maxNumValidators,
Governance: initialValidators[0].Address,
InitialTrieRoot: types.StringToHash(p.initialStateRoot),
MintableNativeToken: p.mintableNativeToken,
NativeTokenConfig: p.nativeTokenConfig,
BridgeAllowListAdmin: bridgeAllowListAdmin,
BridgeBlockListAdmin: bridgeBlockListAdmin,
MaxValidatorSetSize: p.maxNumValidators,
RewardConfig: &polybft.RewardsConfig{
TokenAddress: rewardTokenAddr,
WalletAddress: walletPremineInfo.address,
Expand Down Expand Up @@ -318,31 +332,16 @@ func (p *genesisParams) deployContracts(totalStake *big.Int,
artifact: contractsapi.ChildERC20,
address: contracts.ChildERC20Contract,
},
{
// ChildERC20Predicate contract
artifact: contractsapi.ChildERC20Predicate,
address: contracts.ChildERC20PredicateContract,
},
{
// ChildERC721 token contract
artifact: contractsapi.ChildERC721,
address: contracts.ChildERC721Contract,
},
{
// ChildERC721Predicate token contract
artifact: contractsapi.ChildERC721Predicate,
address: contracts.ChildERC721PredicateContract,
},
{
// ChildERC1155 contract
artifact: contractsapi.ChildERC1155,
address: contracts.ChildERC1155Contract,
},
{
// ChildERC1155Predicate token contract
artifact: contractsapi.ChildERC1155Predicate,
address: contracts.ChildERC1155PredicateContract,
},
{
// BLS contract
artifact: contractsapi.BLS,
Expand Down Expand Up @@ -376,6 +375,44 @@ func (p *genesisParams) deployContracts(totalStake *big.Int,
&contractInfo{artifact: contractsapi.NativeERC20Mintable, address: contracts.NativeERC20TokenContract})
}

if len(params.bridgeAllowListAdmin) != 0 || len(params.bridgeBlockListAdmin) != 0 {
genesisContracts = append(genesisContracts,
&contractInfo{
artifact: contractsapi.ChildERC20PredicateAccessList,
address: contracts.ChildERC20PredicateContract,
})

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

genesisContracts = append(genesisContracts,
&contractInfo{
artifact: contractsapi.ChildERC1155PredicateAccessList,
address: contracts.ChildERC1155PredicateContract,
})
} else {
genesisContracts = append(genesisContracts,
&contractInfo{
artifact: contractsapi.ChildERC20Predicate,
address: contracts.ChildERC20PredicateContract,
})

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

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

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

for _, contract := range genesisContracts {
Expand Down
97 changes: 97 additions & 0 deletions consensus/polybft/contracts_initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,43 @@ func getInitChildERC20PredicateInput(config *BridgeConfig) ([]byte, error) {
return params.EncodeAbi()
}

// getInitChildERC20PredicateAccessListInput builds input parameters for ChildERC20PredicateAccessList SC initialization
func getInitChildERC20PredicateAccessListInput(config PolyBFTConfig) ([]byte, error) {
//nolint:godox
// to be fixed with EVM-541
// TODO: @Stefan-Ethernal Temporary workaround just to be able to run cluster in non-bridge mode, until SC is fixed
rootERC20PredicateAddr := types.StringToAddress(disabledBridgeRootPredicateAddr)
rootERC20Addr := types.ZeroAddress

//nolint:godox
// TODO: This can be removed as we'll always have a bridge config
if config.Bridge != nil {
rootERC20PredicateAddr = config.Bridge.RootERC20PredicateAddr
rootERC20Addr = config.Bridge.RootNativeERC20Addr
}

// The owner of the contract will be the allow list admin or the block list admin, if any of them is set.
owner := contracts.SystemCaller
if config.BridgeAllowListAdmin != types.ZeroAddress {
owner = config.BridgeAllowListAdmin
} else if config.BridgeBlockListAdmin != types.ZeroAddress {
owner = config.BridgeBlockListAdmin
}

params := &contractsapi.InitializeChildERC20PredicateAccessListFn{
NewL2StateSender: contracts.L2StateSenderContract,
NewStateReceiver: contracts.StateReceiverContract,
NewRootERC20Predicate: rootERC20PredicateAddr,
NewChildTokenTemplate: contracts.ChildERC20Contract,
NewNativeTokenRootAddress: rootERC20Addr,
UseAllowList: config.BridgeAllowListAdmin != types.ZeroAddress,
UseBlockList: config.BridgeBlockListAdmin != types.ZeroAddress,
NewOwner: owner,
}

return params.EncodeAbi()
}

// getInitChildERC721PredicateInput builds input parameters for ChildERC721Predicate SC initialization
func getInitChildERC721PredicateInput(config *BridgeConfig) ([]byte, error) {
rootERC721PredicateAddr := types.StringToAddress(disabledBridgeRootPredicateAddr)
Expand All @@ -94,6 +131,36 @@ func getInitChildERC721PredicateInput(config *BridgeConfig) ([]byte, error) {
return params.EncodeAbi()
}

// getInitChildERC721PredicateAccessListInput builds input parameters
// for ChildERC721PredicateAccessList SC initialization
func getInitChildERC721PredicateAccessListInput(config PolyBFTConfig) ([]byte, error) {
rootERC721PredicateAccessListAddr := types.StringToAddress(disabledBridgeRootPredicateAddr)

if config.Bridge != nil {
rootERC721PredicateAccessListAddr = config.Bridge.RootERC721PredicateAddr
}

// The owner of the contract will be the allow list admin or the block list admin, if any of them is set.
owner := contracts.SystemCaller
if config.BridgeAllowListAdmin != types.ZeroAddress {
owner = config.BridgeAllowListAdmin
} else if config.BridgeBlockListAdmin != types.ZeroAddress {
owner = config.BridgeBlockListAdmin
}

params := &contractsapi.InitializeChildERC721PredicateAccessListFn{
NewL2StateSender: contracts.L2StateSenderContract,
NewStateReceiver: contracts.StateReceiverContract,
NewRootERC721Predicate: rootERC721PredicateAccessListAddr,
NewChildTokenTemplate: contracts.ChildERC721Contract,
UseAllowList: config.BridgeAllowListAdmin != types.ZeroAddress,
UseBlockList: config.BridgeBlockListAdmin != types.ZeroAddress,
NewOwner: owner,
}

return params.EncodeAbi()
}

// getInitChildERC1155PredicateInput builds input parameters for ChildERC1155Predicate SC initialization
func getInitChildERC1155PredicateInput(config *BridgeConfig) ([]byte, error) {
rootERC1155PredicateAddr := types.StringToAddress(disabledBridgeRootPredicateAddr)
Expand All @@ -112,6 +179,36 @@ func getInitChildERC1155PredicateInput(config *BridgeConfig) ([]byte, error) {
return params.EncodeAbi()
}

// getInitChildERC1155PredicateAccessListInput builds input parameters
// for ChildERC1155PredicateAccessList SC initialization
func getInitChildERC1155PredicateAccessListInput(config PolyBFTConfig) ([]byte, error) {
rootERC1155PredicateAccessListAddr := types.StringToAddress(disabledBridgeRootPredicateAddr)

if config.Bridge != nil {
rootERC1155PredicateAccessListAddr = config.Bridge.RootERC1155PredicateAddr
}

// The owner of the contract will be the allow list admin or the block list admin, if any of them is set.
owner := contracts.SystemCaller
if config.BridgeAllowListAdmin != types.ZeroAddress {
owner = config.BridgeAllowListAdmin
} else if config.BridgeBlockListAdmin != types.ZeroAddress {
owner = config.BridgeBlockListAdmin
}

params := &contractsapi.InitializeChildERC1155PredicateAccessListFn{
NewL2StateSender: contracts.L2StateSenderContract,
NewStateReceiver: contracts.StateReceiverContract,
NewRootERC1155Predicate: rootERC1155PredicateAccessListAddr,
NewChildTokenTemplate: contracts.ChildERC1155Contract,
UseAllowList: config.BridgeAllowListAdmin != types.ZeroAddress,
UseBlockList: config.BridgeBlockListAdmin != types.ZeroAddress,
NewOwner: owner,
}

return params.EncodeAbi()
}

// mintRewardTokensToWalletAddress mints configured amount of reward tokens to reward wallet address
func mintRewardTokensToWalletAddress(polyBFTConfig *PolyBFTConfig, transition *state.Transition) error {
approveFn := &contractsapi.ApproveRootERC20Fn{
Expand Down
12 changes: 12 additions & 0 deletions consensus/polybft/contractsapi/artifacts-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func main() {
"child/ChildERC20Predicate.sol",
"ChildERC20Predicate",
},
{
"child/ChildERC20PredicateAccessList.sol",
"ChildERC20PredicateAccessList",
},
{
"child/ChildERC721.sol",
"ChildERC721",
Expand All @@ -55,6 +59,10 @@ func main() {
"child/ChildERC721Predicate.sol",
"ChildERC721Predicate",
},
{
"child/ChildERC721PredicateAccessList.sol",
"ChildERC721PredicateAccessList",
},
{
"child/ChildERC1155.sol",
"ChildERC1155",
Expand All @@ -63,6 +71,10 @@ func main() {
"child/ChildERC1155Predicate.sol",
"ChildERC1155Predicate",
},
{
"child/ChildERC1155PredicateAccessList.sol",
"ChildERC1155PredicateAccessList",
},
{
"child/System.sol",
"System",
Expand Down
30 changes: 30 additions & 0 deletions consensus/polybft/contractsapi/bindings-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ func main() {
},
[]string{},
},
{
"ChildERC20PredicateAccessList",
gensc.ChildERC20PredicateAccessList,
false,
[]string{
"initialize",
"withdrawTo",
},
[]string{},
},
{
"NativeERC20",
gensc.NativeERC20,
Expand Down Expand Up @@ -169,6 +179,16 @@ func main() {
},
[]string{},
},
{
"ChildERC1155PredicateAccessList",
gensc.ChildERC1155PredicateAccessList,
false,
[]string{
"initialize",
"withdrawBatch",
},
[]string{},
},
{
"ChildERC1155",
gensc.ChildERC1155,
Expand Down Expand Up @@ -209,6 +229,16 @@ func main() {
},
[]string{},
},
{
"ChildERC721PredicateAccessList",
gensc.ChildERC721PredicateAccessList,
false,
[]string{
"initialize",
"withdrawBatch",
},
[]string{},
},
{
"ChildERC721",
gensc.ChildERC721,
Expand Down
Loading

0 comments on commit 5e3d3ff

Please sign in to comment.