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

     1  package mainchain
     2  
     3  import (
     4  	"math/big"
     5  
     6  	"sync"
     7  
     8  	"github.com/ontio/ontology-eventbus/actor"
     9  	"github.com/sixexorg/magnetic-ring/bactor"
    10  	"github.com/sixexorg/magnetic-ring/common"
    11  	maintypes "github.com/sixexorg/magnetic-ring/core/mainchain/types"
    12  	orgtypes "github.com/sixexorg/magnetic-ring/core/orgchain/types"
    13  	"github.com/sixexorg/magnetic-ring/store/mainchain/extstorages"
    14  	"github.com/sixexorg/magnetic-ring/store/storelaw"
    15  )
    16  
    17  var tellerNil = &actor.PID{}
    18  
    19  type MacroConsumer interface {
    20  	////////////public///////////////
    21  	//ReceiverBlocks is puts the blocks to be validated into the validation pool ahead of time
    22  	ReceiveBlocks(blocks []*orgtypes.Block)
    23  	//Truncate is to notifies the program that this round of calculations will be closed,Stop gracefully
    24  	Truncate()
    25  	//Identify  is to actively launch validation,run new round
    26  	Identify()
    27  
    28  	////////////private///////////////
    29  	await()
    30  	newRound()
    31  	routing()
    32  	setStopSignal(leagueId common.Address, tx *maintypes.Transaction)
    33  	getLeagueConsume(leagueId common.Address) *LeagueConsumer
    34  }
    35  
    36  //run automatically
    37  type MicroConsumer interface {
    38  	registerConsume(adapter *ConsumerAdapter, setStopSignal func(leagueId common.Address, tx *maintypes.Transaction), getLightHouse func() bool)
    39  	receiveBlock(block *orgtypes.Block)
    40  	verifyBlock(block *orgtypes.Block, adpter *ConsumerAdapter) (storelaw.AccountStaters, *big.Int, error)
    41  	consume(adapter *ConsumerAdapter)
    42  	saveAll(block *orgtypes.Block, states storelaw.AccountStaters, fee *big.Int, adpter *ConsumerAdapter) error
    43  	createMainChainTx() (*maintypes.Transaction, error)
    44  	checkGenesisBlock(block *orgtypes.Block)
    45  	clearHistorical()
    46  	checkReferencedTransaction(tx *orgtypes.Transaction, adapter *ConsumerAdapter) error
    47  	authenticate(block *orgtypes.Block) bool
    48  }
    49  
    50  type ConsumerAdapter struct {
    51  	FuncGenesis   func(leagueId common.Address) (*maintypes.Transaction, uint64, error)
    52  	FuncTx        func(txHash common.Hash) (*maintypes.Transaction, uint64, error)
    53  	FuncBlk       func(height uint64) (*maintypes.Header, error)
    54  	Ledger        *extstorages.LedgerStoreImp
    55  	FuncValidate  func(block *orgtypes.Block, ledgerStore storelaw.Ledger4Validation) (blkInfo *storelaw.OrgBlockInfo, err error)
    56  	P2pTlr        bactor.Teller
    57  	TxPoolTlr     bactor.Teller
    58  	P2pTlrInit    bactor.InitTellerfunc
    59  	TxPoolTlrInit bactor.InitTellerfunc
    60  	NodeId        string
    61  	TlrReady      bool
    62  	m             sync.Mutex
    63  }
    64  
    65  func (this *ConsumerAdapter) initTeller() {
    66  	this.m.Lock()
    67  	this.m.Unlock()
    68  	if !this.TlrReady {
    69  		var err error
    70  		this.P2pTlr, err = this.P2pTlrInit()
    71  		if err != nil {
    72  			panic(err)
    73  		}
    74  		this.TxPoolTlr, err = this.TxPoolTlrInit()
    75  		if err != nil {
    76  			panic(err)
    77  		}
    78  		this.TlrReady = true
    79  	}
    80  }
    81  
    82  type LeagueStatePipe struct {
    83  	StateSignal chan interface{}
    84  	Successed   chan common.Address
    85  }
    86  
    87  func NewLeagueStatePipe() *LeagueStatePipe {
    88  	lsp := &LeagueStatePipe{
    89  		StateSignal: make(chan interface{}, 50),
    90  		Successed:   make(chan common.Address, 5),
    91  	}
    92  	return lsp
    93  }
    94  func (this *LeagueStatePipe) Kill() {
    95  	close(this.StateSignal)
    96  	close(this.Successed)
    97  }
    98  func (this *LeagueStatePipe) sendOk(leagueId common.Address) {
    99  	this.Successed <- leagueId
   100  }
   101  func (this *LeagueStatePipe) sendError(leagueId common.Address, err error) {
   102  	this.StateSignal <- &LeagueErr{LeagueId: leagueId, Err: err}
   103  }
   104  func (this *LeagueStatePipe) sendLeagueNeed(leagueId common.Address, height uint64) {
   105  	this.StateSignal <- &LeagueNeed{LeagueId: leagueId, BlockHeight: height}
   106  }
   107  func (this *LeagueStatePipe) sendLeagueExec(leagueId common.Address, height uint64) {
   108  	this.StateSignal <- &LeagueExec{LeagueId: leagueId, Confirmed: height}
   109  }
   110  
   111  type LeagueNeed struct {
   112  	LeagueId    common.Address
   113  	BlockHeight uint64
   114  }
   115  type LeagueExec struct {
   116  	LeagueId  common.Address
   117  	Confirmed uint64
   118  }
   119  type LeagueErr struct {
   120  	LeagueId common.Address
   121  	Err      error
   122  }
   123  
   124  type MainTxCradle struct {
   125  	LeagueId     common.Address
   126  	Check        *maintypes.Transaction
   127  	ReferenceTxs maintypes.Transactions
   128  }