gitlab.com/flarenetwork/coreth@v0.1.1/chain/coreth.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package chain
     5  
     6  import (
     7  	"fmt"
     8  	"time"
     9  
    10  	"github.com/ethereum/go-ethereum/common"
    11  	"github.com/ethereum/go-ethereum/ethdb"
    12  	"gitlab.com/flarenetwork/coreth/consensus/dummy"
    13  	"gitlab.com/flarenetwork/coreth/core"
    14  	"gitlab.com/flarenetwork/coreth/core/state"
    15  	"gitlab.com/flarenetwork/coreth/core/types"
    16  	"gitlab.com/flarenetwork/coreth/eth"
    17  	"gitlab.com/flarenetwork/coreth/node"
    18  	"gitlab.com/flarenetwork/coreth/rpc"
    19  )
    20  
    21  var (
    22  	BlackholeAddr = common.Address{
    23  		1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    24  		0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    25  	}
    26  )
    27  
    28  type Tx = types.Transaction
    29  type Block = types.Block
    30  type Hash = common.Hash
    31  
    32  type ETHChain struct {
    33  	backend *eth.Ethereum
    34  }
    35  
    36  // NewETHChain creates an Ethereum blockchain with the given configs.
    37  func NewETHChain(config *eth.Config, nodecfg *node.Config, chainDB ethdb.Database, settings eth.Settings, consensusCallbacks *dummy.ConsensusCallbacks, lastAcceptedHash common.Hash) (*ETHChain, error) {
    38  	node, err := node.New(nodecfg)
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  	backend, err := eth.New(node, config, consensusCallbacks, chainDB, settings, lastAcceptedHash)
    43  	if err != nil {
    44  		return nil, fmt.Errorf("failed to create backend: %w", err)
    45  	}
    46  	chain := &ETHChain{backend: backend}
    47  	backend.SetEtherbase(BlackholeAddr)
    48  	return chain, nil
    49  }
    50  
    51  func (self *ETHChain) Start() {
    52  	self.backend.Start()
    53  }
    54  
    55  func (self *ETHChain) Stop() {
    56  	self.backend.Stop()
    57  }
    58  
    59  func (self *ETHChain) GenerateBlock() (*types.Block, error) {
    60  	return self.backend.Miner().GenerateBlock()
    61  }
    62  
    63  func (self *ETHChain) BlockChain() *core.BlockChain {
    64  	return self.backend.BlockChain()
    65  }
    66  
    67  func (self *ETHChain) APIBackend() *eth.EthAPIBackend {
    68  	return self.backend.APIBackend
    69  }
    70  
    71  func (self *ETHChain) PendingSize() (int, error) {
    72  	pending, err := self.backend.TxPool().Pending(true)
    73  	count := 0
    74  	for _, txs := range pending {
    75  		count += len(txs)
    76  	}
    77  	return count, err
    78  }
    79  
    80  func (self *ETHChain) AddRemoteTxs(txs []*types.Transaction) []error {
    81  	return self.backend.TxPool().AddRemotes(txs)
    82  }
    83  
    84  func (self *ETHChain) AddLocalTxs(txs []*types.Transaction) []error {
    85  	return self.backend.TxPool().AddLocals(txs)
    86  }
    87  
    88  func (self *ETHChain) CurrentBlock() *types.Block {
    89  	return self.backend.BlockChain().CurrentBlock()
    90  }
    91  
    92  // Returns a new mutable state based on the current HEAD block.
    93  func (self *ETHChain) CurrentState() (*state.StateDB, error) {
    94  	return self.backend.BlockChain().State()
    95  }
    96  
    97  // Returns a new mutable state based on the given block.
    98  func (self *ETHChain) BlockState(block *types.Block) (*state.StateDB, error) {
    99  	return self.backend.BlockChain().StateAt(block.Root())
   100  }
   101  
   102  // Retrives a block from the database by hash.
   103  func (self *ETHChain) GetBlockByHash(hash common.Hash) *types.Block {
   104  	return self.backend.BlockChain().GetBlockByHash(hash)
   105  }
   106  
   107  // Retrives a block from the database by number.
   108  func (self *ETHChain) GetBlockByNumber(num uint64) *types.Block {
   109  	return self.backend.BlockChain().GetBlockByNumber(num)
   110  }
   111  
   112  // Validate the canonical chain from current block to the genesis.
   113  // This should only be called as a convenience method in tests, not
   114  // in production as it traverses the entire chain.
   115  func (self *ETHChain) ValidateCanonicalChain() error {
   116  	return self.backend.BlockChain().ValidateCanonicalChain()
   117  }
   118  
   119  // SetPreference sets the current head block to the one provided as an argument
   120  // regardless of what the chain contents were prior.
   121  func (self *ETHChain) SetPreference(block *types.Block) error {
   122  	return self.BlockChain().SetPreference(block)
   123  }
   124  
   125  // Accept sets a minimum height at which no reorg can pass. Additionally,
   126  // this function may trigger a reorg if the block being accepted is not in the
   127  // canonical chain.
   128  func (self *ETHChain) Accept(block *types.Block) error {
   129  	return self.BlockChain().Accept(block)
   130  }
   131  
   132  // Reject tells the chain that [block] has been rejected.
   133  func (self *ETHChain) Reject(block *types.Block) error {
   134  	return self.BlockChain().Reject(block)
   135  }
   136  
   137  // LastAcceptedBlock returns the last block to be marked as accepted.
   138  func (self *ETHChain) LastAcceptedBlock() *types.Block {
   139  	return self.BlockChain().LastAcceptedBlock()
   140  }
   141  
   142  // RemoveRejectedBlocks removes the rejected blocks between heights
   143  // [start] and [end].
   144  func (self *ETHChain) RemoveRejectedBlocks(start, end uint64) error {
   145  	return self.BlockChain().RemoveRejectedBlocks(start, end)
   146  }
   147  
   148  func (self *ETHChain) GetReceiptsByHash(hash common.Hash) types.Receipts {
   149  	return self.backend.BlockChain().GetReceiptsByHash(hash)
   150  }
   151  
   152  func (self *ETHChain) GetGenesisBlock() *types.Block {
   153  	return self.backend.BlockChain().Genesis()
   154  }
   155  
   156  func (self *ETHChain) InsertBlock(block *types.Block) error {
   157  	return self.backend.BlockChain().InsertBlock(block)
   158  }
   159  
   160  func (self *ETHChain) NewRPCHandler(maximumDuration time.Duration) *rpc.Server {
   161  	return rpc.NewServer(maximumDuration)
   162  }
   163  
   164  func (self *ETHChain) AttachEthService(handler *rpc.Server, namespaces []string) {
   165  	nsmap := make(map[string]bool)
   166  	for _, ns := range namespaces {
   167  		nsmap[ns] = true
   168  	}
   169  	for _, api := range self.backend.APIs() {
   170  		if nsmap[api.Namespace] {
   171  			handler.RegisterName(api.Namespace, api.Service)
   172  		}
   173  	}
   174  }
   175  
   176  func (self *ETHChain) GetTxSubmitCh() <-chan core.NewTxsEvent {
   177  	newTxsChan := make(chan core.NewTxsEvent)
   178  	self.backend.TxPool().SubscribeNewTxsEvent(newTxsChan)
   179  	return newTxsChan
   180  }
   181  
   182  func (self *ETHChain) GetTxAcceptedSubmitCh() <-chan core.NewTxsEvent {
   183  	newTxsChan := make(chan core.NewTxsEvent)
   184  	self.backend.BlockChain().SubscribeAcceptedTransactionEvent(newTxsChan)
   185  	return newTxsChan
   186  }
   187  
   188  func (self *ETHChain) GetTxPool() *core.TxPool {
   189  	return self.backend.TxPool()
   190  }