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

     1  package orgchain
     2  
     3  import (
     4  	"math/big"
     5  	"sync"
     6  
     7  	"log"
     8  
     9  	"fmt"
    10  
    11  	"github.com/sixexorg/magnetic-ring/common"
    12  	maintypes "github.com/sixexorg/magnetic-ring/core/mainchain/types"
    13  	orgtypes "github.com/sixexorg/magnetic-ring/core/orgchain/types"
    14  	mainstorages "github.com/sixexorg/magnetic-ring/store/mainchain/storages"
    15  	"github.com/sixexorg/magnetic-ring/store/orgchain/genesis"
    16  	"github.com/sixexorg/magnetic-ring/store/orgchain/storages"
    17  	"github.com/sixexorg/magnetic-ring/store/storelaw"
    18  )
    19  
    20  var (
    21  	txTypeForReference = make(map[maintypes.TransactionType]struct{})
    22  )
    23  
    24  func init() {
    25  	txTypeForReference[maintypes.JoinLeague] = struct{}{}
    26  	txTypeForReference[maintypes.EnergyToLeague] = struct{}{}
    27  	txTypeForReference[maintypes.LockLeague] = struct{}{}
    28  	txTypeForReference[maintypes.ConsensusLeague] = struct{}{}
    29  	txTypeForReference[maintypes.RaiseUT] = struct{}{}
    30  
    31  }
    32  
    33  type Radar struct {
    34  	leagueId       common.Address
    35  	cfmStartHeight uint64 //Changes can only be made after the current phase has been processed
    36  	cfmEndHeight   uint64 //Changes can only be made after the current phase has been processed
    37  	isPrivate      bool
    38  	blockRoot      common.Hash //Blockroot currently required to verify
    39  	beVerified     bool        //Whether the current verification is complete
    40  	verifing       bool
    41  	growing        bool //mark genesis block has been created
    42  
    43  	//MCCMap       map[uint64]*MainChainCfm
    44  	//adapter      *radarAdapter //ledger
    45  	ledger       *storages.LedgerStoreImp
    46  	leagueLocked bool //Whether the league is locked by the main chain
    47  	mainLedger   *mainstorages.LightLedger
    48  	//chanTxCmf  chan *MainChainCfm
    49  	//needVerify chan *MainChainCfm
    50  	m *sync.Mutex
    51  
    52  	//currentHeight  uint64      //Block height verified
    53  	//mainY        bool          //if diff
    54  	//cmpBlocks          []*orgtypes.Block        //local blocks
    55  	//diffHeightTxHashes map[uint64][]common.Hash //local txHashes which height is not in the current block
    56  	//cmpLD              *league_data.LeagueData            //Main chain consensus data which needs to be compared right now
    57  	//LeagueDataPool     map[uint64]*league_data.LeagueData //all mian chain consensus data order by height
    58  	//diffBlocks   []*orgtypes.Block //current different blocks ,need cmp
    59  }
    60  
    61  func NewRadar(mainLedger *mainstorages.LightLedger) *Radar {
    62  	radar := &Radar{
    63  		m: new(sync.Mutex),
    64  		//adapter:    adapter,
    65  		beVerified: true,
    66  		mainLedger: mainLedger,
    67  		/*MCCMap:     make(map[uint64]*MainChainCfm),
    68  		chanTxCmf:  make(chan *MainChainCfm),
    69  		needVerify: make(chan *MainChainCfm),*/
    70  	}
    71  	return radar
    72  }
    73  func (this *Radar) SetStorage(ledger *storages.LedgerStoreImp) {
    74  	this.ledger = ledger
    75  }
    76  func (this *Radar) SetPrivate(isPrivate bool) {
    77  	this.isPrivate = isPrivate
    78  
    79  }
    80  
    81  //MonitorGenesis is monitor the transactions of the main chain and analyze the information needed for the genesis info
    82  func (this *Radar) MonitorGenesis(txch <-chan *maintypes.Transaction, account common.Address) (
    83  	block *orgtypes.Block, //genesis block
    84  	rate uint32,
    85  	frozenBox *big.Int,
    86  	minBox uint64,
    87  	isPrivate bool,
    88  	sts storelaw.AccountStaters,
    89  	//to init accountstate
    90  ) {
    91  	this.m.Lock()
    92  	defer this.m.Unlock()
    93  	{
    94  		if !this.growing {
    95  			fmt.Println("🌲 monitorGenesis start")
    96  			i := 0
    97  			for tx := range txch {
    98  				fmt.Printf("genesis consume 👻 👻 👻 👻 👻 👻 👻 👻 👻 👻  👻  tx got hash-->%s\n",tx.TransactionHash)
    99  				i++
   100  				if tx.TxType == maintypes.CreateLeague {
   101  					//todo Create a circle
   102  					if tx.TxData.From.Equals(account) {
   103  						//TODO Need to verify receipt
   104  						frozenBox = tx.TxData.MetaBox
   105  						minBox = tx.TxData.MinBox
   106  						rate = tx.TxData.Rate
   107  						isPrivate = tx.TxData.Private
   108  						_, height, _ := this.mainLedger.GetTxByHash(tx.Hash())
   109  						blockRef, _ := this.mainLedger.GetBlockByHeight(height)
   110  						block, sts = genesis.GensisBlock(tx, blockRef.Header.Timestamp)
   111  						this.leagueId = block.Header.LeagueId
   112  						break
   113  					}
   114  				}
   115  			}
   116  		}
   117  		this.growing = true
   118  		fmt.Println("⭕️ league create ok,the id is ", this.leagueId.ToString(), block.Header.Difficulty.Uint64())
   119  		return
   120  	}
   121  }
   122  
   123  func (this *Radar) GetBlocks(start, end uint64) ([]*orgtypes.Block, error) {
   124  	blocks := []*orgtypes.Block{}
   125  	for i := start; i <= end; i++ {
   126  		/*blockHash, err := this.adapter.blockStore.GetBlockHash(i)
   127  		if err != nil {
   128  			panic(err)
   129  		}*/
   130  		//log.Println(blockHash.String())
   131  		//block, err := this.adapter.blockStore.GetBlock(blockHash)
   132  		block, err := this.ledger.GetBlockByHeight(i)
   133  		if err != nil {
   134  			return nil, err
   135  		}
   136  		blocks = append(blocks, block)
   137  	}
   138  	return blocks, nil
   139  }
   140  
   141  //VerifyMainAndLeague is cmp remote and local data,
   142  // if the two sets of data are the same then return true and
   143  // changes the current validation height, otherwise return false
   144  func (this *Radar) VerifyMainAndLeague(blocks []*orgtypes.Block) bool {
   145  	this.m.Lock()
   146  	defer this.m.Unlock()
   147  	{
   148  		blockhashes := make(common.HashArray, 0, len(blocks))
   149  		for _, v := range blocks {
   150  			blockhashes = append(blockhashes, v.Hash())
   151  		}
   152  		log.Println(blockhashes.GetHashRoot().String())
   153  		log.Println(this.blockRoot.String())
   154  		if blockhashes.GetHashRoot() == this.blockRoot {
   155  			this.beVerified = true
   156  			return true
   157  		}
   158  		return false
   159  	}
   160  }
   161  
   162  func containMainType(key maintypes.TransactionType) bool {
   163  	_, ok := txTypeForReference[key]
   164  	return ok
   165  }