@@ -15,16 +15,19 @@ import (
1515 "sync/atomic"
1616 "time"
1717
18+ "github.com/ethereum/go-ethereum/common"
19+ "github.com/ethereum/go-ethereum/common/hexutil"
20+ ecrypto "github.com/ethereum/go-ethereum/crypto"
1821 "github.com/facebookgo/clock"
19- "github.com/iotexproject/go-pkgs/bloom"
20- "github.com/iotexproject/go-pkgs/hash"
21- "github.com/iotexproject/iotex-address/address"
2222 "github.com/pkg/errors"
2323 "github.com/prometheus/client_golang/prometheus"
2424 "go.uber.org/zap"
2525 "google.golang.org/grpc/codes"
2626 "google.golang.org/grpc/status"
2727
28+ "github.com/iotexproject/go-pkgs/bloom"
29+ "github.com/iotexproject/go-pkgs/hash"
30+ "github.com/iotexproject/iotex-address/address"
2831 "github.com/iotexproject/iotex-core/action"
2932 "github.com/iotexproject/iotex-core/action/protocol"
3033 "github.com/iotexproject/iotex-core/action/protocol/account"
@@ -45,6 +48,7 @@ import (
4548 "github.com/iotexproject/iotex-core/pkg/util/fileutil"
4649 "github.com/iotexproject/iotex-core/state"
4750 "github.com/iotexproject/iotex-core/state/factory"
51+ "github.com/iotexproject/iotex-proto/golang/iotextypes"
4852)
4953
5054var (
@@ -944,6 +948,7 @@ func (bc *blockchain) startExistingBlockchain() error {
944948 if err != nil {
945949 return errors .Wrap (err , "failed to get factory's height" )
946950 }
951+ bc .loadingNativeStakingContract ()
947952 log .L ().Info ("Restarting blockchain." ,
948953 zap .Uint64 ("chainHeight" ,
949954 bc .tipHeight ),
@@ -1330,6 +1335,11 @@ func (bc *blockchain) createGenesisStates(ws factory.WorkingSet) error {
13301335 return err
13311336 }
13321337 }
1338+ if bc .config .Genesis .NativeStakingContractCode != "" {
1339+ if err := bc .createNativeStakingContract (ctx , ws ); err != nil {
1340+ return err
1341+ }
1342+ }
13331343 return bc .createRewardingGenesisStates (ctx , ws )
13341344}
13351345
@@ -1388,6 +1398,61 @@ func (bc *blockchain) createPollGenesisStates(ctx context.Context, ws factory.Wo
13881398 return nil
13891399}
13901400
1401+ func (bc * blockchain ) createNativeStakingContract (ctx context.Context , ws factory.WorkingSet ) error {
1402+ raCtx := protocol .MustGetRunActionsCtx (ctx )
1403+ raCtx .Producer , _ = address .FromString (address .ZeroAddress )
1404+ raCtx .Caller , _ = address .FromString (address .ZeroAddress )
1405+ raCtx .GasLimit = bc .config .Genesis .BlockGasLimit
1406+ bytes , err := hexutil .Decode (bc .config .Genesis .NativeStakingContractCode )
1407+ if err != nil {
1408+ return err
1409+ }
1410+ hu := config .NewHeightUpgrade (bc .config )
1411+ execution , err := action .NewExecution (
1412+ "" ,
1413+ 0 ,
1414+ big .NewInt (0 ),
1415+ bc .config .Genesis .BlockGasLimit ,
1416+ big .NewInt (0 ),
1417+ bytes ,
1418+ )
1419+ if err != nil {
1420+ return err
1421+ }
1422+ _ , receipt , err := evm .ExecuteContract (protocol .WithRunActionsCtx (ctx , raCtx ), ws , execution , bc , hu )
1423+ if err != nil {
1424+ return err
1425+ }
1426+ if receipt .Status != uint64 (iotextypes .ReceiptStatus_Success ) {
1427+ return errors .Errorf ("error when deploying native staking contract, status=%d" , receipt .Status )
1428+ }
1429+ p , ok := bc .registry .Find (poll .ProtocolID )
1430+ if ok {
1431+ pp , ok := p .(poll.Protocol )
1432+ if ok {
1433+ pp .SetNativeStakingContract (receipt .ContractAddress )
1434+ log .L ().Info ("Deployed native staking contract" , zap .String ("address" , receipt .ContractAddress ))
1435+ }
1436+ }
1437+ return nil
1438+ }
1439+
1440+ func (bc * blockchain ) loadingNativeStakingContract () {
1441+ if bc .config .Genesis .NativeStakingContractAddress == "" && bc .config .Genesis .NativeStakingContractCode != "" {
1442+ p , ok := bc .registry .Find (poll .ProtocolID )
1443+ if ok {
1444+ pp , ok := p .(poll.Protocol )
1445+ if ok {
1446+ caller , _ := address .FromString (address .ZeroAddress )
1447+ ethAddr := ecrypto .CreateAddress (common .BytesToAddress (caller .Bytes ()), 0 )
1448+ iotxAddr , _ := address .FromBytes (ethAddr .Bytes ())
1449+ pp .SetNativeStakingContract (iotxAddr .String ())
1450+ log .L ().Info ("Loaded native staking contract" , zap .String ("address" , iotxAddr .String ()))
1451+ }
1452+ }
1453+ }
1454+ }
1455+
13911456func (bc * blockchain ) updateAleutianEpochRewardAmount (ctx context.Context , ws factory.WorkingSet ) error {
13921457 p , ok := bc .registry .Find (rewarding .ProtocolID )
13931458 if ! ok {
0 commit comments