github.com/Gessiux/neatchain@v1.3.1/chain/consensus/neatcon/backend.go (about)

     1  package neatcon
     2  
     3  import (
     4  	"crypto/ecdsa"
     5  	"sync"
     6  
     7  	cfg "github.com/Gessiux/go-config"
     8  	"github.com/Gessiux/neatchain/chain/consensus"
     9  	"github.com/Gessiux/neatchain/chain/consensus/neatcon/types"
    10  	"github.com/Gessiux/neatchain/chain/core"
    11  	neatTypes "github.com/Gessiux/neatchain/chain/core/types"
    12  	"github.com/Gessiux/neatchain/chain/log"
    13  	"github.com/Gessiux/neatchain/params"
    14  	"github.com/Gessiux/neatchain/utilities/common"
    15  	"github.com/Gessiux/neatchain/utilities/event"
    16  	"gopkg.in/urfave/cli.v1"
    17  )
    18  
    19  // New creates an Ethereum backend for NeatCon core engine.
    20  func New(chainConfig *params.ChainConfig, cliCtx *cli.Context,
    21  	privateKey *ecdsa.PrivateKey, cch core.CrossChainHelper) consensus.NeatCon {
    22  	// Allocate the snapshot caches and create the engine
    23  	//recents, _ := lru.NewARC(inmemorySnapshots)
    24  	//recentMessages, _ := lru.NewARC(inmemoryPeers)
    25  	//knownMessages, _ := lru.NewARC(inmemoryMessages)
    26  
    27  	config := GetNeatConConfig(chainConfig.NeatChainId, cliCtx)
    28  
    29  	backend := &backend{
    30  		config:          config,
    31  		chainConfig:     chainConfig,
    32  		neatconEventMux: new(event.TypeMux),
    33  		privateKey:      privateKey,
    34  		//address:          crypto.PubkeyToAddress(privateKey.PublicKey),
    35  		//core:             node,
    36  		//chain:     chain,
    37  		logger:    chainConfig.ChainLogger,
    38  		commitCh:  make(chan *neatTypes.Block, 1),
    39  		vcommitCh: make(chan *types.IntermediateBlockResult, 1),
    40  		//recents:          recents,
    41  		//candidates:  make(map[common.Address]bool),
    42  		coreStarted: false,
    43  		//recentMessages:   recentMessages,
    44  		//knownMessages:    knownMessages,
    45  	}
    46  	backend.core = MakeNeatConNode(backend, config, chainConfig, cch)
    47  	return backend
    48  }
    49  
    50  type backend struct {
    51  	config          cfg.Config
    52  	chainConfig     *params.ChainConfig
    53  	neatconEventMux *event.TypeMux
    54  	privateKey      *ecdsa.PrivateKey
    55  	address         common.Address
    56  	core            *Node
    57  	logger          log.Logger
    58  	chain           consensus.ChainReader
    59  	currentBlock    func() *neatTypes.Block
    60  	hasBadBlock     func(hash common.Hash) bool
    61  
    62  	// the channels for istanbul engine notifications
    63  	commitCh          chan *neatTypes.Block
    64  	vcommitCh         chan *types.IntermediateBlockResult
    65  	proposedBlockHash common.Hash
    66  	sealMu            sync.Mutex
    67  	shouldStart       bool
    68  	coreStarted       bool
    69  	coreMu            sync.RWMutex
    70  
    71  	// Current list of candidates we are pushing
    72  	//candidates map[common.Address]bool
    73  	// Protects the signer fields
    74  	//candidatesLock sync.RWMutex
    75  	// Snapshots for recent block to speed up reorgs
    76  	//recents *lru.ARCCache
    77  
    78  	// event subscription for ChainHeadEvent event
    79  	broadcaster consensus.Broadcaster
    80  
    81  	//recentMessages *lru.ARCCache // the cache of peer's messages
    82  	//knownMessages  *lru.ARCCache // the cache of self messages
    83  }
    84  
    85  // WaitForTxs returns true if the consensus should wait for transactions before entering the propose step
    86  func (b *backend) WaitForTxs() bool {
    87  
    88  	return !b.config.GetBool("create_empty_blocks") || b.config.GetInt("create_empty_blocks_interval") > 0
    89  }
    90  
    91  func (b *backend) GetCreateEmptyBlocks() bool {
    92  	return b.config.GetBool("create_empty_blocks")
    93  }
    94  
    95  func (b *backend) GetCreateEmptyBlocksInterval() int {
    96  	return b.config.GetInt("create_empty_blocks_interval")
    97  }
    98  
    99  func GetBackend() backend {
   100  	return backend{}
   101  }