forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
finder.go
58 lines (47 loc) · 1.47 KB
/
finder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package simplepath
import (
"github.com/go-errors/errors"
"github.com/cowry-network/go/services/horizon/internal/db2/core"
"github.com/cowry-network/go/services/horizon/internal/paths"
"github.com/cowry-network/go/support/log"
)
// Finder implements the paths.Finder interface and searchs for
// payment paths using a simple breadth first search of the offers table of a stellar-core.
//
// This implementation is not meant to be fast or to provide the lowest costs paths, but
// rather is meant to be a simple implementation that gives usable paths.
type Finder struct {
Q *core.Q
}
// ensure the struct is paths.Finder compliant
var _ paths.Finder = &Finder{}
// Find performs a path find with the provided query.
func (f *Finder) Find(q paths.Query, maxLength uint) (result []paths.Path, err error) {
log.WithField("source_assets", q.SourceAssets).
WithField("destination_asset", q.DestinationAsset).
WithField("destination_amount", q.DestinationAmount).
Info("Starting pathfind")
if len(q.SourceAssets) == 0 {
err = errors.New("No source assets")
return
}
if maxLength == 0 {
maxLength = MaxPathLength
}
if maxLength < 2 || maxLength > MaxPathLength {
err = errors.New("invalid value of maxLength")
return
}
s := &search{
Query: q,
Q: &core.Q{f.Q.Clone()},
MaxLength: maxLength,
}
s.Init()
s.Run()
result, err = s.Results, s.Err
log.WithField("found", len(s.Results)).
WithField("err", s.Err).
Info("Finished pathfind")
return
}