2017-03-23 11:41:02 -07:00
|
|
|
package raft
|
|
|
|
|
|
|
|
import (
|
2018-11-29 01:56:02 -08:00
|
|
|
"crypto/ecdsa"
|
2017-03-23 11:41:02 -07:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/eth"
|
2017-08-28 14:43:44 -07:00
|
|
|
"github.com/ethereum/go-ethereum/eth/downloader"
|
2017-03-23 11:41:02 -07:00
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2017-10-31 15:24:11 -07:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2017-03-23 11:41:02 -07:00
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2019-01-29 03:08:50 -08:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2017-10-31 15:24:11 -07:00
|
|
|
"github.com/ethereum/go-ethereum/params"
|
2017-03-23 11:41:02 -07:00
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RaftService struct {
|
|
|
|
blockchain *core.BlockChain
|
2017-10-31 15:24:11 -07:00
|
|
|
chainDb ethdb.Database // Block chain database
|
2017-03-23 11:41:02 -07:00
|
|
|
txMu sync.Mutex
|
|
|
|
txPool *core.TxPool
|
|
|
|
accountManager *accounts.Manager
|
2017-08-28 14:43:44 -07:00
|
|
|
downloader *downloader.Downloader
|
2017-03-23 11:41:02 -07:00
|
|
|
|
|
|
|
raftProtocolManager *ProtocolManager
|
2019-01-29 03:08:50 -08:00
|
|
|
startPeers []*enode.Node
|
2017-03-23 11:41:02 -07:00
|
|
|
|
|
|
|
// we need an event mux to instantiate the blockchain
|
|
|
|
eventMux *event.TypeMux
|
|
|
|
minter *minter
|
2018-10-03 17:14:48 -07:00
|
|
|
nodeKey *ecdsa.PrivateKey
|
2017-03-23 11:41:02 -07:00
|
|
|
}
|
|
|
|
|
2019-01-29 03:08:50 -08:00
|
|
|
func New(ctx *node.ServiceContext, chainConfig *params.ChainConfig, raftId, raftPort uint16, joinExisting bool, blockTime time.Duration, e *eth.Ethereum, startPeers []*enode.Node, datadir string) (*RaftService, error) {
|
2017-03-23 11:41:02 -07:00
|
|
|
service := &RaftService{
|
|
|
|
eventMux: ctx.EventMux,
|
|
|
|
chainDb: e.ChainDb(),
|
|
|
|
blockchain: e.BlockChain(),
|
|
|
|
txPool: e.TxPool(),
|
|
|
|
accountManager: e.AccountManager(),
|
2017-08-28 14:43:44 -07:00
|
|
|
downloader: e.Downloader(),
|
2017-03-23 11:41:02 -07:00
|
|
|
startPeers: startPeers,
|
2018-10-03 17:14:48 -07:00
|
|
|
nodeKey: ctx.NodeKey(),
|
2017-03-23 11:41:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
service.minter = newMinter(chainConfig, service, blockTime)
|
|
|
|
|
|
|
|
var err error
|
2017-08-28 14:43:44 -07:00
|
|
|
if service.raftProtocolManager, err = NewProtocolManager(raftId, raftPort, service.blockchain, service.eventMux, startPeers, joinExisting, datadir, service.minter, service.downloader); err != nil {
|
2017-03-23 11:41:02 -07:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return service, nil
|
|
|
|
}
|
|
|
|
|
2017-03-23 12:23:51 -07:00
|
|
|
// Backend interface methods:
|
|
|
|
|
2017-03-23 11:41:02 -07:00
|
|
|
func (service *RaftService) AccountManager() *accounts.Manager { return service.accountManager }
|
|
|
|
func (service *RaftService) BlockChain() *core.BlockChain { return service.blockchain }
|
|
|
|
func (service *RaftService) ChainDb() ethdb.Database { return service.chainDb }
|
|
|
|
func (service *RaftService) DappDb() ethdb.Database { return nil }
|
|
|
|
func (service *RaftService) EventMux() *event.TypeMux { return service.eventMux }
|
|
|
|
func (service *RaftService) TxPool() *core.TxPool { return service.txPool }
|
|
|
|
|
2017-03-23 12:23:51 -07:00
|
|
|
// node.Service interface methods:
|
|
|
|
|
2017-03-23 11:41:02 -07:00
|
|
|
func (service *RaftService) Protocols() []p2p.Protocol { return []p2p.Protocol{} }
|
|
|
|
func (service *RaftService) APIs() []rpc.API {
|
|
|
|
return []rpc.API{
|
|
|
|
{
|
|
|
|
Namespace: "raft",
|
2017-03-23 12:23:51 -07:00
|
|
|
Version: "1.0",
|
|
|
|
Service: NewPublicRaftAPI(service),
|
|
|
|
Public: true,
|
2017-03-23 11:41:02 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start implements node.Service, starting the background data propagation thread
|
|
|
|
// of the protocol.
|
2017-08-28 14:43:44 -07:00
|
|
|
func (service *RaftService) Start(p2pServer *p2p.Server) error {
|
|
|
|
service.raftProtocolManager.Start(p2pServer)
|
2017-03-23 11:41:02 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop implements node.Service, stopping the background data propagation thread
|
|
|
|
// of the protocol.
|
|
|
|
func (service *RaftService) Stop() error {
|
|
|
|
service.blockchain.Stop()
|
|
|
|
service.raftProtocolManager.Stop()
|
2017-08-28 14:43:44 -07:00
|
|
|
service.minter.stop()
|
2017-03-23 11:41:02 -07:00
|
|
|
service.eventMux.Stop()
|
|
|
|
|
|
|
|
service.chainDb.Close()
|
|
|
|
|
2017-10-31 15:24:11 -07:00
|
|
|
log.Info("Raft stopped")
|
2017-03-23 11:41:02 -07:00
|
|
|
return nil
|
|
|
|
}
|