mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-23 03:38:35 +00:00
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:
parent
39a024eb03
commit
adc880d323
4 changed files with 9 additions and 9 deletions
|
@ -66,7 +66,7 @@ func startServer(ctx *cli.Context) error {
|
||||||
|
|
||||||
serverConfig := network.NewServerConfig(cfg)
|
serverConfig := network.NewServerConfig(cfg)
|
||||||
|
|
||||||
chain, err := initBlockChain(grace, cfg)
|
chain, err := initBlockChain(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -79,6 +79,7 @@ func startServer(ctx *cli.Context) error {
|
||||||
rpcServer := rpc.NewServer(chain, cfg.ApplicationConfiguration.RPCPort, server)
|
rpcServer := rpc.NewServer(chain, cfg.ApplicationConfiguration.RPCPort, server)
|
||||||
errChan := make(chan error)
|
errChan := make(chan error)
|
||||||
|
|
||||||
|
go chain.Run(grace)
|
||||||
go server.Start(errChan)
|
go server.Start(errChan)
|
||||||
go rpcServer.Start(errChan)
|
go rpcServer.Start(errChan)
|
||||||
|
|
||||||
|
@ -111,13 +112,13 @@ Main:
|
||||||
}
|
}
|
||||||
|
|
||||||
// initBlockChain initializes BlockChain with preselected DB.
|
// 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)
|
store, err := storage.NewStore(cfg.ApplicationConfiguration.DBConfiguration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, cli.NewExitError(fmt.Errorf("could not initialize storage: %s", err), 1)
|
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 {
|
if err != nil {
|
||||||
return nil, cli.NewExitError(fmt.Errorf("could not initialize blockchain: %s", err), 1)
|
return nil, cli.NewExitError(fmt.Errorf("could not initialize blockchain: %s", err), 1)
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,7 +63,7 @@ type headersOpFunc func(headerList *HeaderHashList)
|
||||||
|
|
||||||
// NewBlockchain return a new blockchain object the will use the
|
// NewBlockchain return a new blockchain object the will use the
|
||||||
// given Store as its underlying storage.
|
// 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{
|
bc := &Blockchain{
|
||||||
config: cfg,
|
config: cfg,
|
||||||
Store: s,
|
Store: s,
|
||||||
|
@ -74,7 +74,6 @@ func NewBlockchain(ctx context.Context, s storage.Store, cfg config.ProtocolConf
|
||||||
memPool: NewMemPool(50000),
|
memPool: NewMemPool(50000),
|
||||||
}
|
}
|
||||||
|
|
||||||
go bc.run(ctx)
|
|
||||||
if err := bc.init(); err != nil {
|
if err := bc.init(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -151,7 +150,8 @@ func (bc *Blockchain) init() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bc *Blockchain) run(ctx context.Context) {
|
// Run runs chain loop.
|
||||||
|
func (bc *Blockchain) Run(ctx context.Context) {
|
||||||
persistTimer := time.NewTimer(persistInterval)
|
persistTimer := time.NewTimer(persistInterval)
|
||||||
defer func() {
|
defer func() {
|
||||||
persistTimer.Stop()
|
persistTimer.Stop()
|
||||||
|
|
|
@ -153,7 +153,7 @@ func newTestChain(t *testing.T) *Blockchain {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
chain, err := NewBlockchain(context.Background(), storage.NewMemoryStore(), cfg.ProtocolConfiguration)
|
chain, err := NewBlockchain(storage.NewMemoryStore(), cfg.ProtocolConfiguration)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package rpc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -248,7 +247,7 @@ func TestHandler(t *testing.T) {
|
||||||
|
|
||||||
store, err := storage.NewLevelDBStore(cfg.ApplicationConfiguration.DBConfiguration.LevelDBOptions)
|
store, err := storage.NewLevelDBStore(cfg.ApplicationConfiguration.DBConfiguration.LevelDBOptions)
|
||||||
assert.Nil(t, err)
|
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")
|
require.NoError(t, err, "could not create levelDB chain")
|
||||||
|
|
||||||
serverConfig := network.NewServerConfig(cfg)
|
serverConfig := network.NewServerConfig(cfg)
|
||||||
|
|
Loading…
Reference in a new issue