|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/dtynn/dix" |
| 9 | + "github.com/filecoin-project/lotus/api" |
| 10 | + "github.com/filecoin-project/lotus/chain/types" |
| 11 | + "go.uber.org/fx" |
| 12 | + |
| 13 | + "github.com/dtynn/chain-co/co" |
| 14 | + "github.com/dtynn/chain-co/proxy" |
| 15 | +) |
| 16 | + |
| 17 | +const ExtractFullNodeAPIKey dix.Invoke = 1 |
| 18 | + |
| 19 | +func Build(ctx context.Context, overrides ...dix.Option) (dix.StopFunc, error) { |
| 20 | + opts := []dix.Option{ |
| 21 | + dix.Override(new(co.NodeOption), co.DefaultNodeOption), |
| 22 | + dix.Override(new(*co.Ctx), co.NewCtx), |
| 23 | + dix.Override(new(*co.Connector), co.NewConnector), |
| 24 | + dix.Override(new(*co.Coordinator), buildCoordinator), |
| 25 | + dix.Override(new(*co.Selector), co.NewSelector), |
| 26 | + dix.Override(new(*proxy.Proxy), buildProxyAPI), |
| 27 | + dix.Override(new(*proxy.Local), buildLocalAPI), |
| 28 | + dix.Override(new(*proxy.UnSupport), buildUnSupportAPI), |
| 29 | + } |
| 30 | + opts = append(opts, overrides...) |
| 31 | + return dix.New(ctx, opts...) |
| 32 | +} |
| 33 | + |
| 34 | +func FullNode(full *api.FullNode) dix.Option { |
| 35 | + return dix.Override(ExtractFullNodeAPIKey, func(srv Service) error { |
| 36 | + *full = &srv |
| 37 | + return nil |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +func ParseNodeInfoList(raws []string) dix.Option { |
| 42 | + return dix.Override(new(co.NodeInfoList), func() (co.NodeInfoList, error) { |
| 43 | + list := make(co.NodeInfoList, 0, len(raws)) |
| 44 | + for _, str := range raws { |
| 45 | + info := co.ParseNodeInfo(str) |
| 46 | + if _, err := info.DialArgs(); err != nil { |
| 47 | + return nil, fmt.Errorf("invalid node info: %s", str) |
| 48 | + } |
| 49 | + |
| 50 | + list = append(list, info) |
| 51 | + } |
| 52 | + |
| 53 | + return list, nil |
| 54 | + }) |
| 55 | +} |
| 56 | + |
| 57 | +func buildCoordinator(lc fx.Lifecycle, ctx *co.Ctx, connector *co.Connector, infos co.NodeInfoList, sel *co.Selector) (*co.Coordinator, error) { |
| 58 | + nodes := make([]*co.Node, 0, len(infos)) |
| 59 | + allDone := false |
| 60 | + defer func() { |
| 61 | + if !allDone { |
| 62 | + for i := range nodes { |
| 63 | + nodes[i].Stop() |
| 64 | + } |
| 65 | + } |
| 66 | + }() |
| 67 | + |
| 68 | + var head *types.TipSet |
| 69 | + weight := types.NewInt(0) |
| 70 | + |
| 71 | + for i := range infos { |
| 72 | + info := infos[i] |
| 73 | + nlog := log.With("host", info.Host) |
| 74 | + |
| 75 | + node, err := connector.Connect(info) |
| 76 | + if err != nil { |
| 77 | + nlog.Errorf("connect failed: %s", err) |
| 78 | + continue |
| 79 | + } |
| 80 | + |
| 81 | + full := node.FullNode() |
| 82 | + h, w, err := getHeadCandidate(full) |
| 83 | + if err != nil { |
| 84 | + node.Stop() |
| 85 | + nlog.Errorf("failed to get head: %s", err) |
| 86 | + continue |
| 87 | + } |
| 88 | + |
| 89 | + if head == nil || w.GreaterThan(weight) { |
| 90 | + head = h |
| 91 | + weight = w |
| 92 | + } |
| 93 | + |
| 94 | + nodes = append(nodes, node) |
| 95 | + } |
| 96 | + |
| 97 | + if len(nodes) == 0 { |
| 98 | + return nil, fmt.Errorf("no available node") |
| 99 | + } |
| 100 | + |
| 101 | + coordinator, err := co.NewCoordinator(ctx, head, weight, sel) |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + |
| 106 | + lc.Append(fx.Hook{ |
| 107 | + OnStart: func(context.Context) error { |
| 108 | + go coordinator.Start() |
| 109 | + sel.ReplaceNodes(nodes, nil, false) |
| 110 | + return nil |
| 111 | + }, |
| 112 | + OnStop: func(context.Context) error { |
| 113 | + coordinator.Stop() |
| 114 | + return nil |
| 115 | + }, |
| 116 | + }) |
| 117 | + |
| 118 | + allDone = true |
| 119 | + return coordinator, nil |
| 120 | +} |
| 121 | + |
| 122 | +func getHeadCandidate(full api.FullNode) (*types.TipSet, types.BigInt, error) { |
| 123 | + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 124 | + defer cancel() |
| 125 | + |
| 126 | + head, err := full.ChainHead(ctx) |
| 127 | + if err != nil { |
| 128 | + return nil, types.BigInt{}, err |
| 129 | + } |
| 130 | + |
| 131 | + weight, err := full.ChainTipSetWeight(ctx, head.Key()) |
| 132 | + if err != nil { |
| 133 | + return nil, types.BigInt{}, err |
| 134 | + } |
| 135 | + |
| 136 | + return head, weight, nil |
| 137 | +} |
| 138 | + |
| 139 | +func buildProxyAPI(sel *co.Selector) *proxy.Proxy { |
| 140 | + return &proxy.Proxy{ |
| 141 | + Select: func() (proxy.ProxyAPI, error) { |
| 142 | + node, err := sel.Select() |
| 143 | + if err != nil { |
| 144 | + return nil, err |
| 145 | + } |
| 146 | + |
| 147 | + return node.FullNode(), nil |
| 148 | + }, |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +func buildLocalAPI(lsrv LocalChainService) *proxy.Local { |
| 153 | + return &proxy.Local{ |
| 154 | + Select: func() (proxy.LocalAPI, error) { |
| 155 | + return &lsrv, nil |
| 156 | + }, |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +func buildUnSupportAPI() *proxy.UnSupport { |
| 161 | + return &proxy.UnSupport{ |
| 162 | + Select: func() (proxy.UnSupportAPI, error) { |
| 163 | + return nil, fmt.Errorf("api not supported") |
| 164 | + }, |
| 165 | + } |
| 166 | +} |
0 commit comments