github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/radar/orgchain/main_tx_affect.go (about)

     1  package orgchain
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/sixexorg/magnetic-ring/common"
     7  	maintypes "github.com/sixexorg/magnetic-ring/core/mainchain/types"
     8  	orgtypes "github.com/sixexorg/magnetic-ring/core/orgchain/types"
     9  	"github.com/sixexorg/magnetic-ring/errors"
    10  )
    11  
    12  const (
    13  	applyInterval = 3
    14  )
    15  
    16  type MainChainCfm struct {
    17  	Start     uint64
    18  	End       uint64
    19  	BlockRoot common.Hash
    20  	Locked    bool
    21  }
    22  type ResultOp struct {
    23  	Err   chan error
    24  	MCC   chan *MainChainCfm
    25  	ORGTX chan *orgtypes.Transaction
    26  }
    27  
    28  func (this *Radar) CheckCfm(tx *maintypes.Transaction) error {
    29  	if this.beVerified {
    30  		return errors.ERR_RADAR_VERIFIED
    31  	}
    32  	if this.cfmEndHeight+1 > tx.TxData.StartHeight {
    33  		return errors.ERR_RADAR_MAINTX_BEFORE
    34  	}
    35  	if this.cfmEndHeight+1 < tx.TxData.StartHeight {
    36  		return errors.ERR_RADAR_MAINTX_AFTER
    37  	}
    38  	return nil
    39  }
    40  
    41  func (this *Radar) AffectConsensusLeague(tx *maintypes.Transaction) (*MainChainCfm, error) {
    42  	err := this.CheckCfm(tx)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	this.cfmStartHeight = tx.TxData.StartHeight
    47  	this.cfmEndHeight = tx.TxData.EndHeight
    48  	this.blockRoot = tx.TxData.BlockRoot
    49  	this.beVerified = false
    50  	mcc := &MainChainCfm{
    51  		Start:     tx.TxData.StartHeight,
    52  		End:       tx.TxData.EndHeight,
    53  		BlockRoot: tx.TxData.BlockRoot,
    54  		Locked:    false,
    55  	}
    56  	return mcc, nil
    57  }
    58  func (this *Radar) AffectLockLeague(tx *maintypes.Transaction) (*MainChainCfm, error) {
    59  	err := this.CheckCfm(tx)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	this.cfmStartHeight = tx.TxData.StartHeight
    64  	this.cfmEndHeight = tx.TxData.EndHeight
    65  	this.blockRoot = tx.TxData.BlockRoot
    66  	this.leagueLocked = true
    67  	mcc := &MainChainCfm{
    68  		Start:     tx.TxData.StartHeight,
    69  		End:       tx.TxData.EndHeight,
    70  		BlockRoot: tx.TxData.BlockRoot,
    71  		Locked:    true,
    72  	}
    73  	return mcc, nil
    74  }
    75  
    76  func (this *Radar) AffectEnergyToLeague(tx *maintypes.Transaction) (*orgtypes.Transaction, error) {
    77  	fmt.Println("🔆 💸 ⭕️ The main chain turns energy into the circle")
    78  	/*	cp, err := sink.BytesToComplex(tx.TxData.LeagueRaw)
    79  		if err != nil {
    80  			return nil, err
    81  		}
    82  		orgTx := &orgtypes.Transaction{}
    83  		source := sink.NewZeroCopySource(cp.Data)
    84  		if err = orgTx.Deserialization(source); err != nil {
    85  			return nil, err
    86  		}
    87  		if orgTx.TxData.TxHash != tx.Hash() || orgTx.TxData.From != tx.TxData.From || orgTx.TxData.Energy != tx.TxData.Energy {
    88  			return nil, errors.ERR_RADAR_TX_MOT_MATCH
    89  		}*/
    90  	orgTx := &orgtypes.Transaction{
    91  		Version: orgtypes.TxVersion,
    92  		TxType:  orgtypes.EnergyFromMain,
    93  		TxData: &orgtypes.TxData{
    94  			TxHash: tx.Hash(),
    95  			From:   tx.TxData.From,
    96  			Energy:   tx.TxData.Energy,
    97  		},
    98  	}
    99  	return orgTx, nil
   100  }
   101  
   102  func (this *Radar) AffectJoinLeague(tx *maintypes.Transaction) (*orgtypes.Transaction, error) {
   103  	fmt.Println("🔆 🏃 ⭕️ Main chain members join the circle,this.isPrivate=",this.isPrivate)
   104  	//TODO receiptcheck等
   105  	var orgTx *orgtypes.Transaction
   106  	if this.isPrivate {
   107  		nextHeight := this.ledger.GetCurrentBlockHeight() + 1
   108  		orgTx = &orgtypes.Transaction{
   109  			Version: orgtypes.TxVersion,
   110  			TxType:  orgtypes.VoteApply,
   111  			TxData: &orgtypes.TxData{
   112  				TxHash: tx.Hash(),
   113  				From:   tx.TxData.Account,
   114  				Start:  nextHeight,
   115  				End:    nextHeight + applyInterval,
   116  			},
   117  		}
   118  		fmt.Println("⭕️ voteReply hash:", orgTx.Hash().String())
   119  	} else {
   120  		orgTx = &orgtypes.Transaction{
   121  			Version: orgtypes.TxVersion,
   122  			TxType:  orgtypes.Join,
   123  			TxData: &orgtypes.TxData{
   124  				TxHash: tx.Hash(),
   125  				From:   tx.TxData.Account,
   126  			},
   127  		}
   128  	}
   129  	return orgTx, nil
   130  }
   131  
   132  func (this *Radar) AffectRaiseUT(tx *maintypes.Transaction) (*orgtypes.Transaction, error) {
   133  	ltx, _, err := this.mainLedger.GetTxByLeagueId(this.leagueId)
   134  	if err != nil {
   135  		return nil, err
   136  	}
   137  	orgTx := &orgtypes.Transaction{
   138  		Version: orgtypes.TxVersion,
   139  		TxType:  orgtypes.RaiseUT,
   140  		TxData: &orgtypes.TxData{
   141  			MetaBox: tx.TxData.MetaBox,
   142  			TxHash:  tx.Hash(),
   143  			Rate:    ltx.TxData.Rate,
   144  		},
   145  	}
   146  	return orgTx, nil
   147  }