apigen: Work with versioned apis

This commit is contained in:
Łukasz Magiera 2021-03-23 17:01:56 +01:00
parent 571114bc44
commit 3fe4b50a13
7 changed files with 1378 additions and 41 deletions

View File

@ -326,9 +326,9 @@ method-gen:
(cd ./lotuspond/front/src/chain && go run ./methodgen.go) (cd ./lotuspond/front/src/chain && go run ./methodgen.go)
api-gen: api-gen:
go run ./gen/api > api/apistruct/struct.go go run ./gen/api
goimports -w api/apistruct goimports -w api/apistruct api/v0api
goimports -w api/apistruct goimports -w api/apistruct api/v0api
.PHONY: api-gen .PHONY: api-gen
docsgen: docsgen-md docsgen-openrpc docsgen: docsgen-md docsgen-openrpc

View File

@ -32,10 +32,10 @@ import (
"github.com/filecoin-project/specs-storage/storage" "github.com/filecoin-project/specs-storage/storage"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p-core/metrics" metrics "github.com/libp2p/go-libp2p-core/metrics"
"github.com/libp2p/go-libp2p-core/network" "github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol" protocol "github.com/libp2p/go-libp2p-core/protocol"
) )
type ChainIOStruct struct { type ChainIOStruct struct {
@ -2162,11 +2162,11 @@ func (s *WorkerStruct) WaitQuiet(p0 context.Context) error {
return s.Internal.WaitQuiet(p0) return s.Internal.WaitQuiet(p0)
} }
var _ api.ChainIO = new(ChainIOStruct) var _ ChainIO = new(ChainIOStruct)
var _ api.Common = new(CommonStruct) var _ Common = new(CommonStruct)
var _ api.FullNode = new(FullNodeStruct) var _ FullNode = new(FullNodeStruct)
var _ api.Gateway = new(GatewayStruct) var _ Gateway = new(GatewayStruct)
var _ api.Signable = new(SignableStruct) var _ Signable = new(SignableStruct)
var _ api.StorageMiner = new(StorageMinerStruct) var _ StorageMiner = new(StorageMinerStruct)
var _ api.Wallet = new(WalletStruct) var _ Wallet = new(WalletStruct)
var _ api.Worker = new(WorkerStruct) var _ Worker = new(WorkerStruct)

12
api/apistruct/types.go Normal file
View File

@ -0,0 +1,12 @@
package apistruct
import "github.com/filecoin-project/lotus/api"
type ChainIO = api.ChainIO
type Common = api.Common
type FullNode = api.FullNode
type Gateway = api.Gateway
type Signable = api.Signable
type StorageMiner = api.StorageMiner
type Wallet = api.Wallet
type Worker = api.Worker

77
api/v0api/common.go Normal file
View File

@ -0,0 +1,77 @@
package v0api
import (
"context"
"github.com/filecoin-project/lotus/api"
"github.com/google/uuid"
"github.com/filecoin-project/go-jsonrpc/auth"
metrics "github.com/libp2p/go-libp2p-core/metrics"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
protocol "github.com/libp2p/go-libp2p-core/protocol"
apitypes "github.com/filecoin-project/lotus/api/types"
)
type Common interface {
// MethodGroup: Auth
AuthVerify(ctx context.Context, token string) ([]auth.Permission, error) //perm:read
AuthNew(ctx context.Context, perms []auth.Permission) ([]byte, error) //perm:admin
// MethodGroup: Net
NetConnectedness(context.Context, peer.ID) (network.Connectedness, error) //perm:read
NetPeers(context.Context) ([]peer.AddrInfo, error) //perm:read
NetConnect(context.Context, peer.AddrInfo) error //perm:write
NetAddrsListen(context.Context) (peer.AddrInfo, error) //perm:read
NetDisconnect(context.Context, peer.ID) error //perm:write
NetFindPeer(context.Context, peer.ID) (peer.AddrInfo, error) //perm:read
NetPubsubScores(context.Context) ([]api.PubsubScore, error) //perm:read
NetAutoNatStatus(context.Context) (api.NatInfo, error) //perm:read
NetAgentVersion(ctx context.Context, p peer.ID) (string, error) //perm:read
NetPeerInfo(context.Context, peer.ID) (*api.ExtendedPeerInfo, error) //perm:read
// NetBandwidthStats returns statistics about the nodes total bandwidth
// usage and current rate across all peers and protocols.
NetBandwidthStats(ctx context.Context) (metrics.Stats, error) //perm:read
// NetBandwidthStatsByPeer returns statistics about the nodes bandwidth
// usage and current rate per peer
NetBandwidthStatsByPeer(ctx context.Context) (map[string]metrics.Stats, error) //perm:read
// NetBandwidthStatsByProtocol returns statistics about the nodes bandwidth
// usage and current rate per protocol
NetBandwidthStatsByProtocol(ctx context.Context) (map[protocol.ID]metrics.Stats, error) //perm:read
// ConnectionGater API
NetBlockAdd(ctx context.Context, acl api.NetBlockList) error //perm:admin
NetBlockRemove(ctx context.Context, acl api.NetBlockList) error //perm:admin
NetBlockList(ctx context.Context) (api.NetBlockList, error) //perm:read
// MethodGroup: Common
// Discover returns an OpenRPC document describing an RPC API.
Discover(ctx context.Context) (apitypes.OpenRPCDocument, error) //perm:read
// ID returns peerID of libp2p node backing this API
ID(context.Context) (peer.ID, error) //perm:read
// Version provides information about API provider
Version(context.Context) (api.APIVersion, error) //perm:read
LogList(context.Context) ([]string, error) //perm:write
LogSetLevel(context.Context, string, string) error //perm:write
// trigger graceful shutdown
Shutdown(context.Context) error //perm:admin
// Session returns a random UUID of api provider session
Session(context.Context) (uuid.UUID, error) //perm:read
Closing(context.Context) (<-chan struct{}, error) //perm:read
}

View File

@ -25,7 +25,7 @@ import (
// FullNode API is a low-level interface to the Filecoin network full node // FullNode API is a low-level interface to the Filecoin network full node
type FullNode interface { type FullNode interface {
api.Common Common
// MethodGroup: Chain // MethodGroup: Chain
// The Chain method group contains methods for interacting with the // The Chain method group contains methods for interacting with the
@ -100,7 +100,7 @@ type FullNode interface {
// ChainTipSetWeight computes weight for the specified tipset. // ChainTipSetWeight computes weight for the specified tipset.
ChainTipSetWeight(context.Context, types.TipSetKey) (types.BigInt, error) //perm:read ChainTipSetWeight(context.Context, types.TipSetKey) (types.BigInt, error) //perm:read
ChainGetNode(ctx context.Context, p string) (*api.IpldObject, error) //perm:read ChainGetNode(ctx context.Context, p string) (*api.IpldObject, error) //perm:read
// ChainGetMessage reads a message referenced by the specified CID from the // ChainGetMessage reads a message referenced by the specified CID from the
// chain blockstore. // chain blockstore.
@ -223,7 +223,7 @@ type FullNode interface {
// MpoolGetNonce gets next nonce for the specified sender. // MpoolGetNonce gets next nonce for the specified sender.
// Note that this method may not be atomic. Use MpoolPushMessage instead. // Note that this method may not be atomic. Use MpoolPushMessage instead.
MpoolGetNonce(context.Context, address.Address) (uint64, error) //perm:read MpoolGetNonce(context.Context, address.Address) (uint64, error) //perm:read
MpoolSub(context.Context) (<-chan api.MpoolUpdate, error) //perm:read MpoolSub(context.Context) (<-chan api.MpoolUpdate, error) //perm:read
// MpoolClear clears pending messages from the mpool // MpoolClear clears pending messages from the mpool
MpoolClear(context.Context, bool) error //perm:write MpoolClear(context.Context, bool) error //perm:write
@ -653,22 +653,22 @@ type FullNode interface {
// MethodGroup: Paych // MethodGroup: Paych
// The Paych methods are for interacting with and managing payment channels // The Paych methods are for interacting with and managing payment channels
PaychGet(ctx context.Context, from, to address.Address, amt types.BigInt) (*api.ChannelInfo, error) //perm:sign PaychGet(ctx context.Context, from, to address.Address, amt types.BigInt) (*api.ChannelInfo, error) //perm:sign
PaychGetWaitReady(context.Context, cid.Cid) (address.Address, error) //perm:sign PaychGetWaitReady(context.Context, cid.Cid) (address.Address, error) //perm:sign
PaychAvailableFunds(ctx context.Context, ch address.Address) (*api.ChannelAvailableFunds, error) //perm:sign PaychAvailableFunds(ctx context.Context, ch address.Address) (*api.ChannelAvailableFunds, error) //perm:sign
PaychAvailableFundsByFromTo(ctx context.Context, from, to address.Address) (*api.ChannelAvailableFunds, error) //perm:sign PaychAvailableFundsByFromTo(ctx context.Context, from, to address.Address) (*api.ChannelAvailableFunds, error) //perm:sign
PaychList(context.Context) ([]address.Address, error) //perm:read PaychList(context.Context) ([]address.Address, error) //perm:read
PaychStatus(context.Context, address.Address) (*api.PaychStatus, error) //perm:read PaychStatus(context.Context, address.Address) (*api.PaychStatus, error) //perm:read
PaychSettle(context.Context, address.Address) (cid.Cid, error) //perm:sign PaychSettle(context.Context, address.Address) (cid.Cid, error) //perm:sign
PaychCollect(context.Context, address.Address) (cid.Cid, error) //perm:sign PaychCollect(context.Context, address.Address) (cid.Cid, error) //perm:sign
PaychAllocateLane(ctx context.Context, ch address.Address) (uint64, error) //perm:sign PaychAllocateLane(ctx context.Context, ch address.Address) (uint64, error) //perm:sign
PaychNewPayment(ctx context.Context, from, to address.Address, vouchers []api.VoucherSpec) (*api.PaymentInfo, error) //perm:sign PaychNewPayment(ctx context.Context, from, to address.Address, vouchers []api.VoucherSpec) (*api.PaymentInfo, error) //perm:sign
PaychVoucherCheckValid(context.Context, address.Address, *paych.SignedVoucher) error //perm:read PaychVoucherCheckValid(context.Context, address.Address, *paych.SignedVoucher) error //perm:read
PaychVoucherCheckSpendable(context.Context, address.Address, *paych.SignedVoucher, []byte, []byte) (bool, error) //perm:read PaychVoucherCheckSpendable(context.Context, address.Address, *paych.SignedVoucher, []byte, []byte) (bool, error) //perm:read
PaychVoucherCreate(context.Context, address.Address, types.BigInt, uint64) (*api.VoucherCreateResult, error) //perm:sign PaychVoucherCreate(context.Context, address.Address, types.BigInt, uint64) (*api.VoucherCreateResult, error) //perm:sign
PaychVoucherAdd(context.Context, address.Address, *paych.SignedVoucher, []byte, types.BigInt) (types.BigInt, error) //perm:write PaychVoucherAdd(context.Context, address.Address, *paych.SignedVoucher, []byte, types.BigInt) (types.BigInt, error) //perm:write
PaychVoucherList(context.Context, address.Address) ([]*paych.SignedVoucher, error) //perm:write PaychVoucherList(context.Context, address.Address) ([]*paych.SignedVoucher, error) //perm:write
PaychVoucherSubmit(context.Context, address.Address, *paych.SignedVoucher, []byte, []byte) (cid.Cid, error) //perm:sign PaychVoucherSubmit(context.Context, address.Address, *paych.SignedVoucher, []byte, []byte) (cid.Cid, error) //perm:sign
// CreateBackup creates node backup onder the specified file name. The // CreateBackup creates node backup onder the specified file name. The
// method requires that the lotus daemon is running with the // method requires that the lotus daemon is running with the
@ -676,4 +676,3 @@ type FullNode interface {
// the path specified when calling CreateBackup is within the base path // the path specified when calling CreateBackup is within the base path
CreateBackup(ctx context.Context, fpath string) error //perm:admin CreateBackup(ctx context.Context, fpath string) error //perm:admin
} }

1231
api/v0api/struct.go Normal file

File diff suppressed because it is too large Load Diff

View File

@ -53,7 +53,13 @@ func (v *Visitor) Visit(node ast.Node) ast.Visitor {
return v return v
} }
func main() { func main() {
if err := runMain(); err != nil { // latest (v1)
if err := generate("./api", "api", "apistruct", "./api/apistruct/struct.go"); err != nil {
fmt.Println("error: ", err)
}
// v0
if err := generate("./api/v0api", "v0api", "v0api", "./api/v0api/struct.go"); err != nil {
fmt.Println("error: ", err) fmt.Println("error: ", err)
} }
} }
@ -116,9 +122,13 @@ func typeName(e ast.Expr) (string, error) {
} }
} }
func runMain() error { func generate(path, pkg, outpkg, outfile string) error {
fset := token.NewFileSet() fset := token.NewFileSet()
apiDir, err := filepath.Abs("./api") apiDir, err := filepath.Abs(path)
if err != nil {
return err
}
outfile, err = filepath.Abs(outfile)
if err != nil { if err != nil {
return err return err
} }
@ -127,7 +137,7 @@ func runMain() error {
return err return err
} }
ap := pkgs["api"] ap := pkgs[pkg]
v := &Visitor{make(map[string]map[string]*methodMeta), map[string][]string{}} v := &Visitor{make(map[string]map[string]*methodMeta), map[string][]string{}}
ast.Walk(v, ap) ast.Walk(v, ap)
@ -148,9 +158,11 @@ func runMain() error {
type meta struct { type meta struct {
Infos map[string]*strinfo Infos map[string]*strinfo
Imports map[string]string Imports map[string]string
OutPkg string
} }
m := &meta{ m := &meta{
OutPkg: outpkg,
Infos: map[string]*strinfo{}, Infos: map[string]*strinfo{},
Imports: map[string]string{}, Imports: map[string]string{},
} }
@ -165,6 +177,9 @@ func runMain() error {
for _, im := range f.Imports { for _, im := range f.Imports {
m.Imports[im.Path.Value] = im.Path.Value m.Imports[im.Path.Value] = im.Path.Value
if im.Name != nil {
m.Imports[im.Path.Value] = im.Name.Name + " " + m.Imports[im.Path.Value]
}
} }
for ifname, methods := range v.Methods { for ifname, methods := range v.Methods {
@ -244,14 +259,17 @@ func runMain() error {
} }
fmt.Println(string(jb))*/ fmt.Println(string(jb))*/
w := os.Stdout w, err := os.OpenFile(outfile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
if err != nil {
return err
}
err = doTemplate(w, m, `// Code generated by github.com/filecoin-project/lotus/gen/api. DO NOT EDIT. err = doTemplate(w, m, `// Code generated by github.com/filecoin-project/lotus/gen/api. DO NOT EDIT.
package apistruct package {{.OutPkg}}
import ( import (
{{range .Imports}}{{.}} {{range .Imports}} {{.}}
{{end}} {{end}}
) )
`) `)
@ -282,7 +300,7 @@ func (s *{{$name}}Struct) {{.Name}}({{.NamedParams}}) ({{.Results}}) {
{{end}} {{end}}
{{end}} {{end}}
{{range .Infos}}var _ api.{{.Name}} = new({{.Name}}Struct) {{range .Infos}}var _ {{.Name}} = new({{.Name}}Struct)
{{end}} {{end}}
`) `)