github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/core/pending_ops.go (about)

     1  package core
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/neatlab/neatio/chain/consensus"
     7  	ncTypes "github.com/neatlab/neatio/chain/consensus/neatcon/types"
     8  	"github.com/neatlab/neatio/chain/core/types"
     9  )
    10  
    11  func ApplyOp(op types.PendingOp, bc *BlockChain, cch CrossChainHelper) error {
    12  	switch op := op.(type) {
    13  	case *types.CreateSideChainOp:
    14  		return cch.CreateSideChain(op.From, op.ChainId, op.MinValidators, op.MinDepositAmount, op.StartBlock, op.EndBlock)
    15  	case *types.JoinSideChainOp:
    16  		return cch.JoinSideChain(op.From, op.PubKey, op.ChainId, op.DepositAmount)
    17  	case *types.LaunchSideChainsOp:
    18  		if len(op.SideChainIds) > 0 {
    19  			var events []interface{}
    20  			for _, sideChainId := range op.SideChainIds {
    21  				events = append(events, CreateSideChainEvent{ChainId: sideChainId})
    22  			}
    23  			bc.PostChainEvents(events, nil)
    24  		}
    25  		if op.NewPendingIdx != nil || len(op.DeleteSideChainIds) > 0 {
    26  			cch.ProcessPostPendingData(op.NewPendingIdx, op.DeleteSideChainIds)
    27  		}
    28  		return nil
    29  	case *types.VoteNextEpochOp:
    30  		ep := bc.engine.(consensus.NeatCon).GetEpoch()
    31  		ep = ep.GetEpochByBlockNumber(bc.CurrentBlock().NumberU64())
    32  		return cch.VoteNextEpoch(ep, op.From, op.VoteHash, op.TxHash)
    33  	case *types.RevealVoteOp:
    34  		ep := bc.engine.(consensus.NeatCon).GetEpoch()
    35  		ep = ep.GetEpochByBlockNumber(bc.CurrentBlock().NumberU64())
    36  		return cch.RevealVote(ep, op.From, op.Pubkey, op.Amount, op.Salt, op.TxHash)
    37  	case *types.UpdateNextEpochOp:
    38  		ep := bc.engine.(consensus.NeatCon).GetEpoch()
    39  		ep = ep.GetEpochByBlockNumber(bc.CurrentBlock().NumberU64())
    40  		return cch.UpdateNextEpoch(ep, op.From, op.PubKey, op.Amount, op.Salt, op.TxHash)
    41  	case *types.SaveDataToMainChainOp:
    42  		return cch.SaveSideChainProofDataToMainChain(op.Data)
    43  	case *ncTypes.SwitchEpochOp:
    44  		eng := bc.engine.(consensus.NeatCon)
    45  		nextEp, err := eng.GetEpoch().EnterNewEpoch(op.NewValidators)
    46  		if err == nil {
    47  
    48  			if !op.NewValidators.HasAddress(eng.PrivateValidator().Bytes()) && eng.IsStarted() {
    49  				bc.PostChainEvents([]interface{}{StopMiningEvent{}}, nil)
    50  			}
    51  
    52  			if op.NewValidators.HasAddress(eng.PrivateValidator().Bytes()) && !eng.IsStarted() {
    53  				bc.PostChainEvents([]interface{}{StartMiningEvent{}}, nil)
    54  			}
    55  
    56  			eng.SetEpoch(nextEp)
    57  			cch.ChangeValidators(op.ChainId)
    58  		}
    59  		return err
    60  	default:
    61  		return fmt.Errorf("unknown op: %v", op)
    62  	}
    63  }