Skip to content

Commit

Permalink
Merge 5f6920a into 9205720
Browse files Browse the repository at this point in the history
  • Loading branch information
offerm committed Jul 1, 2018
2 parents 9205720 + 5f6920a commit 690b44d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 31 deletions.
50 changes: 24 additions & 26 deletions lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,45 +568,43 @@ func lndMain() error {
}()
}

// If we're not in simnet mode, We'll wait until we're fully synced to
// We'll wait until we're fully synced to
// continue the start up of the remainder of the daemon. This ensures
// that we don't accept any possibly invalid state transitions, or
// accept channels with spent funds.
if !(cfg.Bitcoin.SimNet || cfg.Litecoin.SimNet) {
_, bestHeight, err := activeChainControl.chainIO.GetBestBlock()
if err != nil {
return err
}

ltndLog.Infof("Waiting for chain backend to finish sync, "+
"start_height=%v", bestHeight)

for {
if !signal.Alive() {
return nil
}

synced, _, err := activeChainControl.wallet.IsSynced()
if err != nil {
return err
}
_, bestHeight, err := activeChainControl.chainIO.GetBestBlock()
if err != nil {
return err
}

if synced {
break
}
ltndLog.Infof("Waiting for chain backend to finish sync, "+
"start_height=%v", bestHeight)

time.Sleep(time.Second * 1)
for {
if !signal.Alive() {
return nil
}

_, bestHeight, err = activeChainControl.chainIO.GetBestBlock()
synced, _, err := activeChainControl.wallet.IsSynced()
if err != nil {
return err
}

ltndLog.Infof("Chain backend is fully synced (end_height=%v)!",
bestHeight)
if synced {
break
}

time.Sleep(time.Second * 1)
}

_, bestHeight, err = activeChainControl.chainIO.GetBestBlock()
if err != nil {
return err
}

ltndLog.Infof("Chain backend is fully synced (end_height=%v)!",
bestHeight)

// With all the relevant chains initialized, we can finally start the
// server itself.
if err := server.Start(); err != nil {
Expand Down
6 changes: 5 additions & 1 deletion lnd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ func checkChannelPolicy(policy, expectedPolicy *lnrpc.RoutingPolicy) error {
// testUpdateChannelPolicy tests that policy updates made to a channel
// gets propagated to other nodes in the network.
func testUpdateChannelPolicy(net *lntest.NetworkHarness, t *harnessTest) {
timeout := time.Duration(time.Second * 5)
timeout := time.Duration(time.Second * 15)
ctxb := context.Background()

// Launch notification clients for all nodes, such that we can
Expand Down Expand Up @@ -868,10 +868,12 @@ func testUpdateChannelPolicy(net *lntest.NetworkHarness, t *harnessTest) {
if err != nil {
t.Fatalf("alice didn't report channel: %v", err)
}
ctxt, _ = context.WithTimeout(ctxb, time.Second*15)
err = net.Bob.WaitForNetworkChannelOpen(ctxt, chanPoint2)
if err != nil {
t.Fatalf("bob didn't report channel: %v", err)
}
ctxt, _ = context.WithTimeout(ctxb, time.Second*15)
err = carol.WaitForNetworkChannelOpen(ctxt, chanPoint2)
if err != nil {
t.Fatalf("carol didn't report channel: %v", err)
Expand Down Expand Up @@ -962,6 +964,7 @@ func testUpdateChannelPolicy(net *lntest.NetworkHarness, t *harnessTest) {
if err != nil {
t.Fatalf("alice didn't report channel: %v", err)
}
ctxt, _ = context.WithTimeout(ctxb, time.Second*15)
err = carol.WaitForNetworkChannelOpen(ctxt, chanPoint3)
if err != nil {
t.Fatalf("bob didn't report channel: %v", err)
Expand Down Expand Up @@ -1024,6 +1027,7 @@ func testUpdateChannelPolicy(net *lntest.NetworkHarness, t *harnessTest) {
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint, false)
ctxt, _ = context.WithTimeout(ctxb, timeout)
closeChannelAndAssert(ctxt, t, net, net.Bob, chanPoint2, false)
ctxt, _ = context.WithTimeout(ctxb, time.Second*15)
closeChannelAndAssert(ctxt, t, net, net.Alice, chanPoint3, false)
ctxt, _ = context.WithTimeout(ctxb, timeout)
}
Expand Down
58 changes: 56 additions & 2 deletions lntest/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,15 @@ func (n *NetworkHarness) TearDownAll() error {
// current instance of the network harness. The created node is running, but
// not yet connected to other nodes within the network.
func (n *NetworkHarness) NewNode(name string, extraArgs []string) (*HarnessNode, error) {
return n.newNode(name, extraArgs, false)
node, err := n.newNode(name, extraArgs, false)

if err != nil{
return nil, err
}

err = ensureServerStarted(node)

return node, err
}

// NewNodeWithSeed fully initializes a new HarnessNode after creating a fresh
Expand Down Expand Up @@ -283,6 +291,11 @@ func (n *NetworkHarness) NewNodeWithSeed(name string, extraArgs []string,
return nil, nil, err
}

err = ensureServerStarted(node)
if err != nil {
return nil, nil, err
}

// With the node started, we can now record its public key within the
// global mapping.
n.RegisterNode(node)
Expand Down Expand Up @@ -315,6 +328,11 @@ func (n *NetworkHarness) RestoreNodeWithSeed(name string, extraArgs []string,
return nil, err
}

err = ensureServerStarted(node)
if err != nil {
return nil, err
}

// With the node started, we can now record its public key within the
// global mapping.
n.RegisterNode(node)
Expand Down Expand Up @@ -552,7 +570,43 @@ func (n *NetworkHarness) RestartNode(node *HarnessNode, callback func() error) e
}
}

return node.start(n.lndErrorChan)
if err := node.start(n.lndErrorChan); err != nil{
return err
}

return ensureServerStarted(node)
}

// ensureServerStarted verifies that the lnd server started.
// It is done by using RPC calls to DisconnectPeerRequest which returns an error "chain backend is still syncing"
// before the server started.
// TODO (Offer): replace this hack with a field in getInfo that indicates that the server staretd

func ensureServerStarted (node *HarnessNode) error{
// make sure the node completed the server startup
err := WaitPredicate(func() bool {
ctxb := context.Background()
req := &lnrpc.DisconnectPeerRequest{
PubKey: "Dummy key to get an error",
}
_, err := node.DisconnectPeer(
ctxb, req,
)
if err != nil {
if strings.Contains(err.Error(),"chain backend is still syncing"){
return false
}
return true
}
return false
}, time.Second*15)

if err != nil{
return fmt.Errorf("node (%v) is still not sync with chain after 15 seconds",node.cfg.Name)
}

return nil

}

// ShutdownNode stops an active lnd process and returns when the process has
Expand Down
8 changes: 6 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,8 +707,8 @@ func (s *server) Started() bool {
// NOTE: This function is safe for concurrent access.
func (s *server) Start() error {
// Already running?
if !atomic.CompareAndSwapInt32(&s.started, 0, 1) {
return nil
if s.Started(){
return fmt.Errorf("server started already")
}

if s.torController != nil {
Expand Down Expand Up @@ -777,6 +777,10 @@ func (s *server) Start() error {
srvrLog.Infof("Auto peer bootstrapping is disabled")
}

if !atomic.CompareAndSwapInt32(&s.started, 0, 1) {
return fmt.Errorf("can't mark server as started")
}

return nil
}

Expand Down

0 comments on commit 690b44d

Please sign in to comment.