blockchain: server runs goroutine instead of blockchain init

rework initBlockChain in order to have controllable way of running
blockchain;
remove context from initBlockChain func;
This commit is contained in:
Vsevolod Brekelov 2019-09-17 15:27:40 +03:00
parent 39a024eb03
commit adc880d323
4 changed files with 9 additions and 9 deletions

View file

@ -66,7 +66,7 @@ func startServer(ctx *cli.Context) error {
serverConfig := network.NewServerConfig(cfg)
chain, err := initBlockChain(grace, cfg)
chain, err := initBlockChain(cfg)
if err != nil {
return err
}
@ -79,6 +79,7 @@ func startServer(ctx *cli.Context) error {
rpcServer := rpc.NewServer(chain, cfg.ApplicationConfiguration.RPCPort, server)
errChan := make(chan error)
go chain.Run(grace)
go server.Start(errChan)
go rpcServer.Start(errChan)
@ -111,13 +112,13 @@ Main:
}
// initBlockChain initializes BlockChain with preselected DB.
func initBlockChain(context context.Context, cfg config.Config) (*core.Blockchain, error) {
func initBlockChain(cfg config.Config) (*core.Blockchain, error) {
store, err := storage.NewStore(cfg.ApplicationConfiguration.DBConfiguration)
if err != nil {
return nil, cli.NewExitError(fmt.Errorf("could not initialize storage: %s", err), 1)
}
chain, err := core.NewBlockchain(context, store, cfg.ProtocolConfiguration)
chain, err := core.NewBlockchain(store, cfg.ProtocolConfiguration)
if err != nil {
return nil, cli.NewExitError(fmt.Errorf("could not initialize blockchain: %s", err), 1)
}

View file

@ -63,7 +63,7 @@ type headersOpFunc func(headerList *HeaderHashList)
// NewBlockchain return a new blockchain object the will use the
// given Store as its underlying storage.
func NewBlockchain(ctx context.Context, s storage.Store, cfg config.ProtocolConfiguration) (*Blockchain, error) {
func NewBlockchain(s storage.Store, cfg config.ProtocolConfiguration) (*Blockchain, error) {
bc := &Blockchain{
config: cfg,
Store: s,
@ -74,7 +74,6 @@ func NewBlockchain(ctx context.Context, s storage.Store, cfg config.ProtocolConf
memPool: NewMemPool(50000),
}
go bc.run(ctx)
if err := bc.init(); err != nil {
return nil, err
}
@ -151,7 +150,8 @@ func (bc *Blockchain) init() error {
return nil
}
func (bc *Blockchain) run(ctx context.Context) {
// Run runs chain loop.
func (bc *Blockchain) Run(ctx context.Context) {
persistTimer := time.NewTimer(persistInterval)
defer func() {
persistTimer.Stop()

View file

@ -153,7 +153,7 @@ func newTestChain(t *testing.T) *Blockchain {
if err != nil {
t.Fatal(err)
}
chain, err := NewBlockchain(context.Background(), storage.NewMemoryStore(), cfg.ProtocolConfiguration)
chain, err := NewBlockchain(storage.NewMemoryStore(), cfg.ProtocolConfiguration)
if err != nil {
t.Fatal(err)
}

View file

@ -2,7 +2,6 @@ package rpc
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
@ -248,7 +247,7 @@ func TestHandler(t *testing.T) {
store, err := storage.NewLevelDBStore(cfg.ApplicationConfiguration.DBConfiguration.LevelDBOptions)
assert.Nil(t, err)
chain, err := core.NewBlockchain(context.Background(), store, cfg.ProtocolConfiguration)
chain, err := core.NewBlockchain(store, cfg.ProtocolConfiguration)
require.NoError(t, err, "could not create levelDB chain")
serverConfig := network.NewServerConfig(cfg)