2024-01-12 13:09:12 +00:00
|
|
|
package hapi
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-01-15 16:34:10 +00:00
|
|
|
"time"
|
|
|
|
|
2024-04-11 16:30:53 +00:00
|
|
|
lru "github.com/hashicorp/golang-lru/v2"
|
|
|
|
blocks "github.com/ipfs/go-block-format"
|
|
|
|
|
2024-01-12 13:09:12 +00:00
|
|
|
"github.com/filecoin-project/lotus/api/client"
|
2024-04-11 16:30:53 +00:00
|
|
|
"github.com/filecoin-project/lotus/blockstore"
|
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
2024-01-12 13:09:12 +00:00
|
|
|
cliutil "github.com/filecoin-project/lotus/cli/util"
|
2024-04-11 16:30:53 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/must"
|
2024-01-12 13:09:12 +00:00
|
|
|
)
|
|
|
|
|
2024-04-11 16:30:53 +00:00
|
|
|
var ChainBlockCache = must.One(lru.New[blockstore.MhString, blocks.Block](4096))
|
|
|
|
|
2024-01-12 13:09:12 +00:00
|
|
|
func (a *app) watchRpc() {
|
|
|
|
ticker := time.NewTicker(watchInterval)
|
|
|
|
for {
|
|
|
|
err := a.updateRpc(context.TODO())
|
|
|
|
if err != nil {
|
|
|
|
log.Errorw("updating rpc info", "error", err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type minimalApiInfo struct {
|
|
|
|
Apis struct {
|
|
|
|
ChainApiInfo []string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *app) updateRpc(ctx context.Context) error {
|
|
|
|
rpcInfos := map[string]minimalApiInfo{} // config name -> api info
|
|
|
|
confNameToAddr := map[string]string{} // config name -> api address
|
|
|
|
|
|
|
|
err := forEachConfig[minimalApiInfo](a, func(name string, info minimalApiInfo) error {
|
|
|
|
if len(info.Apis.ChainApiInfo) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rpcInfos[name] = info
|
|
|
|
|
|
|
|
for _, addr := range info.Apis.ChainApiInfo {
|
|
|
|
ai := cliutil.ParseApiInfo(addr)
|
|
|
|
confNameToAddr[name] = ai.Addr
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
apiInfos := map[string][]byte{} // api address -> token
|
|
|
|
|
|
|
|
// for dedup by address
|
|
|
|
for _, info := range rpcInfos {
|
|
|
|
ai := cliutil.ParseApiInfo(info.Apis.ChainApiInfo[0])
|
|
|
|
apiInfos[ai.Addr] = ai.Token
|
|
|
|
}
|
|
|
|
|
|
|
|
a.rpcInfoLk.Lock()
|
|
|
|
|
|
|
|
// todo improve this shared rpc logic
|
|
|
|
if a.workingApi == nil {
|
|
|
|
for addr, token := range apiInfos {
|
|
|
|
ai := cliutil.APIInfo{
|
|
|
|
Addr: addr,
|
|
|
|
Token: token,
|
|
|
|
}
|
|
|
|
|
|
|
|
da, err := ai.DialArgs("v1")
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ah := ai.AuthHeader()
|
|
|
|
|
|
|
|
v1api, closer, err := client.NewFullNodeRPCV1(ctx, da, ah)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2024-02-21 12:28:49 +00:00
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
closer()
|
|
|
|
}()
|
2024-01-12 13:09:12 +00:00
|
|
|
|
|
|
|
a.workingApi = v1api
|
2024-04-11 16:30:53 +00:00
|
|
|
a.stor = store.ActorStore(ctx, blockstore.NewReadCachedBlockstore(blockstore.NewAPIBlockstore(a.workingApi), ChainBlockCache))
|
2024-01-12 13:09:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
a.rpcInfoLk.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|