Skip to content

Commit

Permalink
Merge pull request #15 from ExchangeUnion/neutrino-status
Browse files Browse the repository at this point in the history
feat: show Neutrino syncing status
  • Loading branch information
kilrau authored Nov 11, 2020
2 parents 9f54587 + 34c4f78 commit e23f6d4
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 16 deletions.
5 changes: 4 additions & 1 deletion service/abstract_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ type AbstractService struct {
}

func NewAbstractService(name string) *AbstractService {
logger := logrus.New()
logger.SetLevel(logrus.DebugLevel)

return &AbstractService{
name: name,
logger: logrus.New(),
logger: logger,
}
}

Expand Down
3 changes: 2 additions & 1 deletion service/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,13 @@ func (t *Container) ExecInteractive(command []string) (string, io.Reader, io.Wri
return execId, r, attachResp.Conn, nil
}

func (t *Container) GetLogs(since string, tail string) (<-chan string, error) {
func (t *Container) GetLogs(since string, tail string, follow bool) (<-chan string, error) {
reader, err := t.client.ContainerLogs(context.Background(), t.c.ID, types.ContainerLogsOptions{
ShowStdout: true,
ShowStderr: true,
Since: since,
Tail: tail,
Follow: follow,
})

if err != nil {
Expand Down
151 changes: 138 additions & 13 deletions service/lnd/lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,22 @@ import (
"time"
)

type NeutrinoSyncing struct {
current int64
total int64
done bool
}

type LndService struct {
*service.SingleContainerService
rpcOptions *service.RpcOptions
rpcClient pb.LightningClient
chain string
p *regexp.Regexp
rpcOptions *service.RpcOptions
rpcClient pb.LightningClient
chain string
p *regexp.Regexp
p0 *regexp.Regexp
p1 *regexp.Regexp
p2 *regexp.Regexp
neutrinoSyncing NeutrinoSyncing
}

func (t *LndService) GetBackendNode() (string, error) {
Expand All @@ -43,11 +53,48 @@ func New(name string, containerName string, chain string) (*LndService, error) {
return nil, err
}

return &LndService{
p0, err := regexp.Compile("^.*Fully caught up with cfheaders at height (\\d+), waiting at tip for new blocks$")
if err != nil {
return nil, err
}

var p1 *regexp.Regexp

if strings.Contains(containerName, "simnet") {
p1, err = regexp.Compile("^.*Writing cfheaders at height=(\\d+) to next checkpoint$")
if err != nil {
return nil, err
}
} else {
p1, err = regexp.Compile("^.*Fetching set of checkpointed cfheaders filters from height=(\\d+).*$")
if err != nil {
return nil, err
}
}

p2, err := regexp.Compile("^.*Syncing to block height (\\d+) from peer.*$")
if err != nil {
return nil, err
}

s := LndService{
SingleContainerService: service.NewSingleContainerService(name, containerName),
chain: chain,
p: p,
}, nil
p0: p0,
p1: p1,
p2: p2,
neutrinoSyncing: NeutrinoSyncing{current: 0, total: 0, done: false},
}

go func() {
err := s.watchNeutrinoSyncing()
if err != nil {
s.GetLogger().Error("Failed to watch Neutrino syncing", err)
}
}()

return &s, nil
}

func (t *LndService) ConfigureRpc(options *service.RpcOptions) {
Expand Down Expand Up @@ -201,6 +248,85 @@ func (t *LndService) getCurrentHeight() (uint32, error) {
return 0, nil
}

func (t *LndService) watchNeutrinoSyncing() error {
t.GetLogger().Debug("[watch] Neutrino syncing")
c, err := t.GetContainer()
for err != nil {
t.GetLogger().Debug("[watch] Waiting for Docker container to be created")
time.Sleep(1 * time.Second)
}
t.GetLogger().Debug("[watch] Got container")
startedAt := c.Unwrap().State.StartedAt
t.GetLogger().Debugf("[watch] startedAt=%s", startedAt)
logs, err := t.FollowLogs("1h", "")
if err != nil {
return err
}
t.GetLogger().Debug("[watch] Watch logs")
for line := range logs {

line = strings.TrimSpace(line)
var current string
var total string

if t.p0.MatchString(line) {
t.GetLogger().Debugf("[watch] <p0> %s", line)
current = t.p0.ReplaceAllString(line, "$1")
t.neutrinoSyncing.current, err = strconv.ParseInt(current, 10, 64)
if err != nil {
return err
}
t.neutrinoSyncing.done = true
} else {
if t.p1.MatchString(line) {
t.GetLogger().Debugf("[watch] <p1> %s", line)
current = t.p1.ReplaceAllString(line, "$1")
t.neutrinoSyncing.current, err = strconv.ParseInt(current, 10, 64)
if err != nil {
return err
}
} else {
if t.p2.MatchString(line) {
t.GetLogger().Debugf("[watch] <p2> %s", line)
total = t.p2.ReplaceAllString(line, "$1")
t.neutrinoSyncing.total, err = strconv.ParseInt(total, 10, 64)
if err != nil {
return err
}
}
}
}
}

t.GetLogger().Debug("[watch] Done")

return nil
}

func (t *LndService) Neutrino() bool {
// TODO get lnd backend type
return true
}

func syncingText(current int64, total int64) string {
if total < current {
total = current
}
p := float32(current) / float32(total) * 100.0
if p > 0.005 {
p = p - 0.005
} else {
p = 0
}
return fmt.Sprintf("Syncing %.2f%% (%d/%d)", p, current, total)
}

func (t *LndService) GetNeutrinoStatus() string {
current := t.neutrinoSyncing.current
total := t.neutrinoSyncing.total
return syncingText(current, total)
}

func (t *LndService) GetStatus() (string, error) {
status, err := t.SingleContainerService.GetStatus()
if err != nil {
Expand All @@ -212,6 +338,11 @@ func (t *LndService) GetStatus() (string, error) {
if strings.Contains(err.Error(), "Wallet is encrypted") {
return "Wallet locked. Unlock with lncli unlock.", nil
}
if strings.Contains(err.Error(), "no such file or directory") {
if t.Neutrino() {
return t.GetNeutrinoStatus(), nil
}
}
return "", err
}

Expand All @@ -225,13 +356,7 @@ func (t *LndService) GetStatus() (string, error) {
if total <= current {
return "Ready", nil
} else {
p := float32(current) / float32(total) * 100.0
if p > 0.005 {
p = p - 0.005
} else {
p = 0
}
return fmt.Sprintf("Syncing %.2f%% (%d/%d)", p, current, total), nil
return syncingText(int64(current), int64(total)), nil
}
} else {
if syncedToChain {
Expand Down
11 changes: 10 additions & 1 deletion service/single_container_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,16 @@ func (t *SingleContainerService) GetLogs(since string, tail string) (<-chan stri
if err != nil {
return nil, err
}
return c.GetLogs(since, tail)
return c.GetLogs(since, tail, false)
}

// GetContainerLog is a shortcut function
func (t *SingleContainerService) FollowLogs(since string, tail string) (<-chan string, error) {
c, err := t.GetContainer()
if err != nil {
return nil, err
}
return c.GetLogs(since, tail, true)
}

// GetContainerEnvironmentVariable is a shortcut function
Expand Down

0 comments on commit e23f6d4

Please sign in to comment.