From adc880d323537c9130e3c67f04fbc69f98ffcc6a Mon Sep 17 00:00:00 2001 From: Vsevolod Brekelov Date: Tue, 17 Sep 2019 15:27:40 +0300 Subject: [PATCH] blockchain: server runs goroutine instead of blockchain init rework initBlockChain in order to have controllable way of running blockchain; remove context from initBlockChain func; --- cli/server/server.go | 7 ++++--- pkg/core/blockchain.go | 6 +++--- pkg/core/blockchain_test.go | 2 +- pkg/rpc/server_test.go | 3 +-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/cli/server/server.go b/cli/server/server.go index 6c4e48f37..53eb68c43 100644 --- a/cli/server/server.go +++ b/cli/server/server.go @@ -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) } diff --git a/pkg/core/blockchain.go b/pkg/core/blockchain.go index 21ca332a4..ff1cd3917 100644 --- a/pkg/core/blockchain.go +++ b/pkg/core/blockchain.go @@ -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() diff --git a/pkg/core/blockchain_test.go b/pkg/core/blockchain_test.go index 8f35861c2..ca96b458e 100644 --- a/pkg/core/blockchain_test.go +++ b/pkg/core/blockchain_test.go @@ -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) } diff --git a/pkg/rpc/server_test.go b/pkg/rpc/server_test.go index d891ffe30..575dafde2 100644 --- a/pkg/rpc/server_test.go +++ b/pkg/rpc/server_test.go @@ -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)