diff --git a/chain/chain.go b/chain/chain.go index 1b3a73a0af..1d82c0fe6d 100644 --- a/chain/chain.go +++ b/chain/chain.go @@ -114,18 +114,19 @@ func (g *Genesis) Hash() types.Hash { // MarshalJSON implements the json interface func (g *Genesis) MarshalJSON() ([]byte, error) { type Genesis struct { - Nonce string `json:"nonce"` - Timestamp *string `json:"timestamp,omitempty"` - ExtraData *string `json:"extraData,omitempty"` - GasLimit *string `json:"gasLimit,omitempty"` - Difficulty *string `json:"difficulty,omitempty"` - Mixhash types.Hash `json:"mixHash"` - Coinbase types.Address `json:"coinbase"` - Alloc *map[string]*GenesisAccount `json:"alloc,omitempty"` - Number *string `json:"number,omitempty"` - GasUsed *string `json:"gasUsed,omitempty"` - ParentHash types.Hash `json:"parentHash"` - BaseFee *string `json:"baseFee"` + Nonce string `json:"nonce"` + Timestamp *string `json:"timestamp,omitempty"` + ExtraData *string `json:"extraData,omitempty"` + GasLimit *string `json:"gasLimit,omitempty"` + Difficulty *string `json:"difficulty,omitempty"` + Mixhash types.Hash `json:"mixHash"` + Coinbase types.Address `json:"coinbase"` + Alloc *map[string]*GenesisAccount `json:"alloc,omitempty"` + Number *string `json:"number,omitempty"` + GasUsed *string `json:"gasUsed,omitempty"` + ParentHash types.Hash `json:"parentHash"` + BaseFee *string `json:"baseFee"` + StakeTokenAddr types.Address `json:"stakeTokenAddr"` } var enc Genesis @@ -140,6 +141,7 @@ func (g *Genesis) MarshalJSON() ([]byte, error) { enc.Mixhash = g.Mixhash enc.Coinbase = g.Coinbase + enc.StakeTokenAddr = g.StakeTokenAddr if g.Alloc != nil { alloc := make(map[string]*GenesisAccount, len(g.Alloc)) @@ -160,18 +162,19 @@ func (g *Genesis) MarshalJSON() ([]byte, error) { // UnmarshalJSON implements the json interface func (g *Genesis) UnmarshalJSON(data []byte) error { type Genesis struct { - Nonce *string `json:"nonce"` - Timestamp *string `json:"timestamp"` - ExtraData *string `json:"extraData"` - GasLimit *string `json:"gasLimit"` - Difficulty *string `json:"difficulty"` - Mixhash *types.Hash `json:"mixHash"` - Coinbase *types.Address `json:"coinbase"` - Alloc map[string]*GenesisAccount `json:"alloc"` - Number *string `json:"number"` - GasUsed *string `json:"gasUsed"` - ParentHash *types.Hash `json:"parentHash"` - BaseFee *string `json:"baseFee"` + Nonce *string `json:"nonce"` + Timestamp *string `json:"timestamp"` + ExtraData *string `json:"extraData"` + GasLimit *string `json:"gasLimit"` + Difficulty *string `json:"difficulty"` + Mixhash *types.Hash `json:"mixHash"` + Coinbase *types.Address `json:"coinbase"` + Alloc map[string]*GenesisAccount `json:"alloc"` + Number *string `json:"number"` + GasUsed *string `json:"gasUsed"` + ParentHash *types.Hash `json:"parentHash"` + BaseFee *string `json:"baseFee"` + StakeTokenAddr *types.Address `json:"stakeTokenAddr"` } var dec Genesis @@ -231,6 +234,10 @@ func (g *Genesis) UnmarshalJSON(data []byte) error { g.Coinbase = *dec.Coinbase } + if dec.StakeTokenAddr != nil { + g.StakeTokenAddr = *dec.StakeTokenAddr + } + if dec.Alloc != nil { g.Alloc = make(map[types.Address]*GenesisAccount, len(dec.Alloc)) for k, v := range dec.Alloc { diff --git a/consensus/polybft/contracts_initializer.go b/consensus/polybft/contracts_initializer.go index e19aa155ef..3fc76cfb7d 100644 --- a/consensus/polybft/contracts_initializer.go +++ b/consensus/polybft/contracts_initializer.go @@ -384,7 +384,7 @@ func mintStakeToken(polyBFTConfig PolyBFTConfig, transition *state.Transition) e return fmt.Errorf("StakeToken.mint params encoding failed: %w", err) } - if err := callContract(contracts.SystemCaller, polyBFTConfig.StakeTokenAddr, + if err := callContract(polyBFTConfig.BladeAdmin, polyBFTConfig.StakeTokenAddr, input, "StakeToken.mint", transition); err != nil { return err } diff --git a/consensus/polybft/polybft.go b/consensus/polybft/polybft.go index b28742e476..8afbfad23c 100644 --- a/consensus/polybft/polybft.go +++ b/consensus/polybft/polybft.go @@ -182,11 +182,6 @@ func GenesisPostHookFactory(config *chain.Chain, engineName string) func(txn *st return err } - // initialize StakeManager SC - if err = initStakeManager(polyBFTConfig, transition); err != nil { - return err - } - // approve EpochManager if err = approveEpochManagerAsSpender(polyBFTConfig, transition); err != nil { return err @@ -202,14 +197,19 @@ func GenesisPostHookFactory(config *chain.Chain, engineName string) func(txn *st return err } - bridgeCfg := polyBFTConfig.Bridge - if bridgeCfg != nil { - if !IsNativeStakeToken(polyBFTConfig.StakeTokenAddr) { - if err := mintStakeToken(polyBFTConfig, transition); err != nil { - return err - } + if !IsNativeStakeToken(polyBFTConfig.StakeTokenAddr) && polyBFTConfig.Bridge != nil { + if err := mintStakeToken(polyBFTConfig, transition); err != nil { + return err } + } + // initialize StakeManager SC + if err = initStakeManager(polyBFTConfig, transition); err != nil { + return err + } + + bridgeCfg := polyBFTConfig.Bridge + if bridgeCfg != nil { // check if there are Bridge Allow List Admins and Bridge Block List Admins // and if there are, get the first address as the Admin bridgeAllowListAdmin := types.ZeroAddress diff --git a/e2e-polybft/e2e/bridge_test.go b/e2e-polybft/e2e/bridge_test.go index f1a9e2b1b8..20068a3677 100644 --- a/e2e-polybft/e2e/bridge_test.go +++ b/e2e-polybft/e2e/bridge_test.go @@ -1457,6 +1457,9 @@ func TestE2E_Bridge_NonNative(t *testing.T) { stakeAmount = ethgo.Ether(500) ) + minter, err := wallet.GenerateKey() + require.NoError(t, err) + receiverKeys := make([]string, transfersCount) receivers := make([]string, transfersCount) receiversAddrs := make([]types.Address, transfersCount) @@ -1482,8 +1485,8 @@ func TestE2E_Bridge_NonNative(t *testing.T) { cluster := framework.NewTestCluster(t, 5, framework.WithNumBlockConfirmations(0), framework.WithEpochSize(epochSize), - framework.WithPremine(receiversAddrs...), framework.WithBridge(), + framework.WithBladeAdmin(minter.Address().String()), framework.WithSecretsCallback(func(addrs []types.Address, tcc *framework.TestClusterConfig) { for _, addr := range addrs { tcc.Premine = append(tcc.Premine, fmt.Sprintf("%s:%s", addr, premineBalance)) @@ -1513,7 +1516,7 @@ func TestE2E_Bridge_NonNative(t *testing.T) { cluster.WaitForBlock(epochSize*1, 1*time.Minute) - require.NoError(t, firstValidator.Stake(polybftCfg, big.NewInt(1000), types.ZeroAddress)) + require.NoError(t, firstValidator.Stake(polybftCfg, big.NewInt(1000), contracts.ERC20Contract)) cluster.WaitForBlock(epochSize*3, 90*time.Second) diff --git a/e2e-polybft/framework/test-cluster.go b/e2e-polybft/framework/test-cluster.go index 50a19de14b..7127dcee4e 100644 --- a/e2e-polybft/framework/test-cluster.go +++ b/e2e-polybft/framework/test-cluster.go @@ -17,10 +17,8 @@ import ( "testing" "time" - "github.com/0xPolygon/polygon-edge/chain" "github.com/0xPolygon/polygon-edge/command" "github.com/0xPolygon/polygon-edge/command/genesis" - cmdHelper "github.com/0xPolygon/polygon-edge/command/helper" "github.com/0xPolygon/polygon-edge/consensus/polybft" "github.com/0xPolygon/polygon-edge/consensus/polybft/contractsapi" "github.com/0xPolygon/polygon-edge/contracts" @@ -709,6 +707,10 @@ func NewTestCluster(t *testing.T, validatorsCount int, opts ...ClusterOption) *T } args = append(args, "--proxy-contracts-admin", proxyAdminAddr) + if config.PredeployNonNative { + args = append(args, "--stake-token", contracts.ERC20Contract.String()) + } + // run genesis command with all the arguments err = cluster.cmdRun(args...) require.NoError(t, err) @@ -725,15 +727,15 @@ func NewTestCluster(t *testing.T, validatorsCount int, opts ...ClusterOption) *T err = cluster.cmdRun(args...) require.NoError(t, err) - chainConfig, err := chain.ImportFromFile(genesisPath) - if err != nil { - require.NoError(t, err) - } - chainConfig.Genesis.StakeTokenAddr = contracts.ERC20Contract + /* chainConfig, err := chain.ImportFromFile(genesisPath) + if err != nil { + require.NoError(t, err) + } + chainConfig.Genesis.StakeTokenAddr = contracts.ERC20Contract - if err := cmdHelper.WriteGenesisConfigToDisk(chainConfig, genesisPath); err != nil { - require.NoError(t, err) - } + if err := cmdHelper.WriteGenesisConfigToDisk(chainConfig, genesisPath); err != nil { + require.NoError(t, err) + } */ } if cluster.Config.HasBridge { diff --git a/e2e-polybft/framework/test-server.go b/e2e-polybft/framework/test-server.go index 2ddac7c228..1874ac056c 100644 --- a/e2e-polybft/framework/test-server.go +++ b/e2e-polybft/framework/test-server.go @@ -242,7 +242,7 @@ func (t *TestServer) Stake(polybftConfig polybft.PolyBFTConfig, amount *big.Int, } if stakeTokenAddr != types.ZeroAddress { - args = append(args, "--stake-token", types.AddressToString(stakeTokenAddr)) + args = append(args, "--stake-token", stakeTokenAddr.String()) } return runCommand(t.clusterConfig.Binary, args, t.clusterConfig.GetStdout("stake"))