lotus/lotuspond/api.go

139 lines
2.2 KiB
Go
Raw Permalink Normal View History

2019-08-12 23:09:08 +00:00
package main
import (
2019-09-06 17:42:31 +00:00
"crypto/rand"
2019-08-12 23:09:08 +00:00
"io"
"io/ioutil"
"os"
"sync"
"golang.org/x/xerrors"
2020-05-20 17:43:22 +00:00
"github.com/filecoin-project/go-jsonrpc"
"github.com/filecoin-project/lotus/node/repo"
2019-08-12 23:09:08 +00:00
)
2019-09-17 12:03:28 +00:00
type NodeState int
const (
NodeUnknown = iota
NodeRunning
NodeStopped
)
2019-08-12 23:09:08 +00:00
type api struct {
cmds int32
2019-09-17 12:03:28 +00:00
running map[int32]*runningNode
2019-08-12 23:09:08 +00:00
runningLk sync.Mutex
genesis string
}
type nodeInfo struct {
Repo string
ID int32
APIPort int32
2019-09-17 12:03:28 +00:00
State NodeState
2019-08-12 23:09:08 +00:00
FullNode string // only for storage nodes
Storage bool
}
func (api *api) Nodes() []nodeInfo {
api.runningLk.Lock()
out := make([]nodeInfo, 0, len(api.running))
for _, node := range api.running {
out = append(out, node.meta)
}
api.runningLk.Unlock()
return out
}
func (api *api) TokenFor(id int32) (string, error) {
api.runningLk.Lock()
defer api.runningLk.Unlock()
rnd, ok := api.running[id]
if !ok {
return "", xerrors.New("no running node with this ID")
}
r, err := repo.NewFS(rnd.meta.Repo)
if err != nil {
return "", err
}
t, err := r.APIToken()
if err != nil {
return "", err
}
return string(t), nil
}
func (api *api) FullID(id int32) (int32, error) {
api.runningLk.Lock()
defer api.runningLk.Unlock()
stor, ok := api.running[id]
if !ok {
return 0, xerrors.New("storage node not found")
}
if !stor.meta.Storage {
return 0, xerrors.New("node is not a storage node")
}
for id, n := range api.running {
if n.meta.Repo == stor.meta.FullNode {
return id, nil
}
}
return 0, xerrors.New("node not found")
}
2019-09-06 17:42:31 +00:00
func (api *api) CreateRandomFile(size int64) (string, error) {
tf, err := ioutil.TempFile(os.TempDir(), "pond-random-")
if err != nil {
return "", err
}
_, err = io.CopyN(tf, rand.Reader, size)
if err != nil {
return "", err
}
if err := tf.Close(); err != nil {
return "", err
}
return tf.Name(), nil
}
2019-09-17 12:03:28 +00:00
func (api *api) Stop(node int32) error {
api.runningLk.Lock()
nd, ok := api.running[node]
api.runningLk.Unlock()
if !ok {
return nil
}
nd.stop()
return nil
}
2019-08-12 23:09:08 +00:00
type client struct {
Nodes func() []nodeInfo
}
func apiClient() (*client, error) {
c := &client{}
if _, err := jsonrpc.NewClient("ws://"+listenAddr+"/rpc/v0", "Pond", c, nil); err != nil {
return nil, err
}
return c, nil
}