harmony api compat: support AuthNew

This commit is contained in:
Łukasz Magiera 2024-02-02 18:21:50 +01:00
parent 75c908dd0b
commit 74bf18e8ce
2 changed files with 63 additions and 16 deletions

View File

@ -33,19 +33,19 @@ var _ sectorblocks.SectorBuilder = *new(MinerSealingService)
func harmonyApiInfoToConf(apiInfo string) (config.HarmonyDB, error) {
hc := config.HarmonyDB{}
// apiInfo - harmony:maddr:user:pass:dbname:host:port
// apiInfo - harmony:layer:maddr:user:pass:dbname:host:port
parts := strings.Split(apiInfo, ":")
if len(parts) != 7 {
if len(parts) != 8 {
return config.HarmonyDB{}, xerrors.Errorf("invalid harmonydb info '%s'", apiInfo)
}
hc.Username = parts[2]
hc.Password = parts[3]
hc.Database = parts[4]
hc.Hosts = []string{parts[5]}
hc.Port = parts[6]
hc.Username = parts[3]
hc.Password = parts[4]
hc.Database = parts[5]
hc.Hosts = []string{parts[6]}
hc.Port = parts[7]
return hc, nil
}
@ -64,7 +64,7 @@ func connectHarmony(apiInfo string, fapi v1api.FullNode, mctx helpers.MetricsCtx
}
parts := strings.Split(apiInfo, ":")
maddr, err := address.NewFromString(parts[1])
maddr, err := address.NewFromString(parts[2])
if err != nil {
return nil, xerrors.Errorf("parsing miner address: %w", err)
}
@ -83,10 +83,12 @@ func connectHarmony(apiInfo string, fapi v1api.FullNode, mctx helpers.MetricsCtx
return nil, xerrors.Errorf("getting miner info: %w", err)
}
lp := fakelm.NewLMRPCProvider(si, maddr, abi.ActorID(mid), mi.SectorSize, pin)
lp := fakelm.NewLMRPCProvider(si, maddr, abi.ActorID(mid), mi.SectorSize, pin, db, parts[1])
ast := api.StorageMinerStruct{}
ast.CommonStruct.Internal.AuthNew = lp.AuthNew
ast.Internal.ActorAddress = lp.ActorAddress
ast.Internal.WorkerJobs = lp.WorkerJobs
ast.Internal.SectorsStatus = lp.SectorsStatus

View File

@ -2,6 +2,12 @@ package fakelm
import (
"context"
"encoding/base64"
"github.com/BurntSushi/toml"
"github.com/filecoin-project/go-jsonrpc/auth"
"github.com/filecoin-project/lotus/lib/harmony/harmonydb"
"github.com/filecoin-project/lotus/node/config"
"github.com/gbrlsnchs/jwt/v3"
"net/http"
"net/url"
@ -26,16 +32,20 @@ type LMRPCProvider struct {
ssize abi.SectorSize
pi lpmarket.Ingester
pi lpmarket.Ingester
db *harmonydb.DB
confLayer string
}
func NewLMRPCProvider(si paths.SectorIndex, maddr address.Address, minerID abi.ActorID, ssize abi.SectorSize, pi lpmarket.Ingester) *LMRPCProvider {
func NewLMRPCProvider(si paths.SectorIndex, maddr address.Address, minerID abi.ActorID, ssize abi.SectorSize, pi lpmarket.Ingester, db *harmonydb.DB, confLayer string) *LMRPCProvider {
return &LMRPCProvider{
si: si,
maddr: maddr,
minerID: minerID,
ssize: ssize,
pi: pi,
si: si,
maddr: maddr,
minerID: minerID,
ssize: ssize,
pi: pi,
db: db,
confLayer: confLayer,
}
}
@ -275,4 +285,39 @@ func (l *LMRPCProvider) AllocatePieceToSector(ctx context.Context, maddr address
return l.pi.AllocatePieceToSector(ctx, maddr, piece, rawSize, source, header)
}
func (l *LMRPCProvider) AuthNew(ctx context.Context, perms []auth.Permission) ([]byte, error) {
var cs []struct {
Config string
}
err := l.db.Select(ctx, &cs, "select config from harmony_config where title = $1", l.confLayer)
if err != nil {
return nil, err
}
if len(cs) == 0 {
return nil, xerrors.Errorf("no harmony config found")
}
lp := config.DefaultLotusProvider()
if _, err := toml.Decode(cs[0].Config, lp); err != nil {
return nil, xerrors.Errorf("decode harmony config: %w", err)
}
type jwtPayload struct {
Allow []auth.Permission
}
p := jwtPayload{
Allow: perms,
}
sk, err := base64.StdEncoding.DecodeString(lp.Apis.StorageRPCSecret)
if err != nil {
return nil, xerrors.Errorf("decode secret: %w", err)
}
return jwt.Sign(&p, jwt.NewHS256(sk))
}
var _ MinimalLMApi = &LMRPCProvider{}