Skip to content

Commit

Permalink
refactor(cli): moved types accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkness4 committed Jan 31, 2024
1 parent 7f79fe8 commit 95970bc
Show file tree
Hide file tree
Showing 36 changed files with 833 additions and 599 deletions.
6 changes: 3 additions & 3 deletions cli/.mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dir: 'mocks/mock{{.PackageName}}'
mockname: '{{.InterfaceName}}'
outpkg: 'mock{{.PackageName}}'
packages:
github.com/deepsquare-io/grid/cli/types:
github.com/deepsquare-io/grid/cli/types/job:
interfaces:
MetaScheduledJobsIdsFetcher:
JobFetcher:
MetaScheduledIdsFetcher:
Fetcher:
3 changes: 2 additions & 1 deletion cli/cmd/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"github.com/deepsquare-io/grid/cli/internal/utils"
"github.com/deepsquare-io/grid/cli/metascheduler"
"github.com/deepsquare-io/grid/cli/types"
"github.com/deepsquare-io/grid/cli/types/event"
"github.com/erikgeiser/promptkit/confirmation"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -263,7 +264,7 @@ var Command = cli.Command{
transitions := make(chan types.JobTransition, 1)
sub, err := watcher.SubscribeEvents(
ctx,
types.FilterJobTransition(transitions),
event.FilterJobTransition(transitions),
)
if err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions cli/cmd/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
"github.com/deepsquare-io/grid/cli/deepsquare"
"github.com/deepsquare-io/grid/cli/internal/utils"
"github.com/deepsquare-io/grid/cli/metascheduler"
"github.com/deepsquare-io/grid/cli/types"
"github.com/deepsquare-io/grid/cli/types/provider"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
Expand Down Expand Up @@ -128,9 +128,9 @@ var Command = cli.Command{
MetaschedulerAddress: common.HexToAddress(metaschedulerSmartContract),
ChainID: chainID,
})
opts := make([]types.GetProviderOption, 0)
opts := make([]provider.GetProviderOption, 0)
if proposal {
opts = append(opts, types.WithProposal())
opts = append(opts, provider.WithProposal())
}
providers, err := clientset.ProviderManager().GetProviders(ctx, opts...)
if err != nil {
Expand All @@ -155,7 +155,7 @@ var Command = cli.Command{
return err
}
providerAddress := common.HexToAddress(cCtx.Args().First())
return clientset.ProviderManager().Approve(cCtx.Context, providerAddress)
return clientset.ProviderManager().ApproveProvider(cCtx.Context, providerAddress)
},
},
{
Expand All @@ -169,7 +169,7 @@ var Command = cli.Command{
return err
}
providerAddress := common.HexToAddress(cCtx.Args().First())
return clientset.ProviderManager().Remove(cCtx.Context, providerAddress)
return clientset.ProviderManager().RemoveProvider(cCtx.Context, providerAddress)
},
},
},
Expand Down
21 changes: 12 additions & 9 deletions cli/cmd/submit/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ import (
"github.com/deepsquare-io/grid/cli/sbatch"
"github.com/deepsquare-io/grid/cli/types"
metaschedulerabi "github.com/deepsquare-io/grid/cli/types/abi/metascheduler"
"github.com/deepsquare-io/grid/cli/types/event"
"github.com/deepsquare-io/grid/cli/types/job"
"github.com/deepsquare-io/grid/cli/types/provider"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/common"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -326,8 +329,8 @@ var Command = cli.Command{
if err != nil {
return err
}
var job sbatch.Job
if err := yaml.Unmarshal(dat, &job); err != nil {
var j sbatch.Job
if err := yaml.Unmarshal(dat, &j); err != nil {
return err
}

Expand Down Expand Up @@ -389,11 +392,11 @@ var Command = cli.Command{
if !watch {
jobID, err := client.SubmitJob(
ctx,
&job,
&j,
credits,
jobNameB,
types.WithUse(usesLabels...),
types.WithAffinity(affinities...),
job.WithUse(usesLabels...),
job.WithAffinity(affinities...),
)
if err != nil {
return err
Expand All @@ -406,13 +409,13 @@ var Command = cli.Command{

// Watch submit logic
transitions := make(chan types.JobTransition, 1)
sub, err := watcher.SubscribeEvents(ctx, types.FilterJobTransition(transitions))
sub, err := watcher.SubscribeEvents(ctx, event.FilterJobTransition(transitions))
if err != nil {
return err
}
defer sub.Unsubscribe()

jobID, err := client.SubmitJob(ctx, &job, credits, jobNameB, types.WithUse(usesLabels...))
jobID, err := client.SubmitJob(ctx, &j, credits, jobNameB, job.WithUse(usesLabels...))
if err != nil {
return err
}
Expand All @@ -422,7 +425,7 @@ var Command = cli.Command{
fmt.Printf("---Waiting for job %s to be running...---\n", jobIDBig.String())
var finished = false
var allocatedProviderAddress common.Address
var provider types.ProviderDetail
var provider provider.Detail
msOrSchedLen, runningLen := int64(0), int64(0)
// Wait for finished or running
loop:
Expand Down Expand Up @@ -587,7 +590,7 @@ var forbiddenReplacer = strings.NewReplacer(
// computeWaitingTime returns min(running) + sum(waiting)
func computeWaitingTime(
jobID [32]byte,
provider types.ProviderDetail,
provider provider.Detail,
jobs []types.Job,
) (time.Duration, error) {
var waiting, running time.Duration
Expand Down
135 changes: 107 additions & 28 deletions cli/deepsquare/deepsquare_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"crypto/ecdsa"
"crypto/tls"
"crypto/x509"
"math/big"
"net"
"net/http"
"net/url"
Expand All @@ -30,6 +31,11 @@ import (
"github.com/deepsquare-io/grid/cli/metascheduler"
"github.com/deepsquare-io/grid/cli/sbatch"
"github.com/deepsquare-io/grid/cli/types"
"github.com/deepsquare-io/grid/cli/types/allowance"
"github.com/deepsquare-io/grid/cli/types/credit"
"github.com/deepsquare-io/grid/cli/types/event"
"github.com/deepsquare-io/grid/cli/types/job"
"github.com/deepsquare-io/grid/cli/types/provider"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
Expand All @@ -51,12 +57,12 @@ const (
// Users must call Close() at the end of the application to avoid pending connections.
type Client interface {
types.Logger
types.JobScheduler
types.JobFetcher
types.JobsByProviderFetcher
types.CreditManager
types.AllowanceManager
types.ProviderManager
job.Scheduler
job.Fetcher
job.ByProviderFetcher
credit.Manager
allowance.Manager
provider.Manager
// Close all connections.
Close() error
}
Expand Down Expand Up @@ -118,14 +124,87 @@ func (c *ClientConfig) applyDefault() {

type client struct {
types.Logger
types.JobScheduler
types.JobFetcher
types.JobsByProviderFetcher
types.CreditManager
types.AllowanceManager
types.ProviderManager
loggerConn *grpc.ClientConn
rpcClient *rpc.Client
job.Scheduler
job.Fetcher
job.ByProviderFetcher
CreditManager credit.Manager
AllowanceManager allowance.Manager
ProviderManager provider.Manager
loggerConn *grpc.ClientConn
rpcClient *rpc.Client
}

// GetProvider implements Client.
func (c *client) GetProvider(
ctx context.Context,
address common.Address,
opts ...provider.GetProviderOption,
) (provider provider.Detail, err error) {
return c.ProviderManager.GetProvider(ctx, address, opts...)
}

// GetProviders implements Client.
func (c *client) GetProviders(
ctx context.Context,
opts ...provider.GetProviderOption,
) (providers []provider.Detail, err error) {
return c.ProviderManager.GetProviders(ctx, opts...)
}

// Approve implements Client.
func (c *client) ApproveProvider(ctx context.Context, provider common.Address) error {
return c.ProviderManager.ApproveProvider(ctx, provider)
}

// Balance implements Client.
func (c *client) Balance(ctx context.Context) (*big.Int, error) {
return c.CreditManager.Balance(ctx)
}

// BalanceOf implements Client.
func (c *client) BalanceOf(ctx context.Context, address common.Address) (*big.Int, error) {
return c.CreditManager.BalanceOf(ctx, address)
}

// ClearAllowance implements Client.
func (c *client) ClearAllowance(ctx context.Context) error {
return c.AllowanceManager.ClearAllowance(ctx)
}

// GetAllowance implements Client.
func (c *client) GetAllowance(ctx context.Context) (*big.Int, error) {
return c.AllowanceManager.GetAllowance(ctx)
}

// ReduceToAllowance implements Client.
func (c *client) ReduceToAllowance(
ctx context.Context,
approvals <-chan types.Approval,
) (<-chan *big.Int, error) {
return c.AllowanceManager.ReduceToAllowance(ctx, approvals)
}

// ReduceToBalance implements Client.
func (c *client) ReduceToBalance(
ctx context.Context,
transfers <-chan types.Transfer,
) (<-chan *big.Int, error) {
return c.CreditManager.ReduceToBalance(ctx, transfers)
}

// RemoveProvider implements Client.
func (c *client) RemoveProvider(ctx context.Context, provider common.Address) error {
return c.ProviderManager.RemoveProvider(ctx, provider)
}

// SetAllowance implements Client.
func (c *client) SetAllowance(ctx context.Context, amount *big.Int) error {
return c.AllowanceManager.SetAllowance(ctx, amount)
}

// Transfer implements Client.
func (c *client) Transfer(ctx context.Context, to common.Address, amount *big.Int) error {
return c.CreditManager.Transfer(ctx, to, amount)
}

// NewClient creates a new Client for the given ClientConfig.
Expand Down Expand Up @@ -185,15 +264,15 @@ func NewClient(ctx context.Context, c *ClientConfig) (Client, error) {
fetcher := rpcClientSet.JobFetcher()
runningJobsByProviderFetcher := metascheduler.NewJobsByProviderFetcher(oracle, fetcher)
return &client{
JobFetcher: fetcher,
JobScheduler: jobScheduler,
JobsByProviderFetcher: runningJobsByProviderFetcher,
CreditManager: rpcClientSet.CreditManager(),
AllowanceManager: rpcClientSet.AllowanceManager(),
ProviderManager: rpcClientSet.ProviderManager(),
Logger: logger,
loggerConn: conn,
rpcClient: rpcClient,
Fetcher: fetcher,
Scheduler: jobScheduler,
ByProviderFetcher: runningJobsByProviderFetcher,
CreditManager: rpcClientSet.CreditManager(),
AllowanceManager: rpcClientSet.AllowanceManager(),
ProviderManager: rpcClientSet.ProviderManager(),
Logger: logger,
loggerConn: conn,
rpcClient: rpcClient,
}, nil
}

Expand All @@ -206,7 +285,7 @@ func (c *client) Close() error {
//
// Users must call Close() at the end of the application to avoid pending connections.
type Watcher interface {
types.EventSubscriber
event.Subscriber
// Close all connections.
Close() error
}
Expand Down Expand Up @@ -248,7 +327,7 @@ func (c *WatcherConfig) applyDefault() {
}

type watcher struct {
types.EventSubscriber
event.Subscriber
rpcClient *rpc.Client
wsClient *rpc.Client
}
Expand Down Expand Up @@ -283,9 +362,9 @@ func NewWatcher(ctx context.Context, c *WatcherConfig) (Watcher, error) {
UserPrivateKey: c.UserPrivateKey,
}
return &watcher{
EventSubscriber: metascheduler.NewEventSubscriber(metaschedulerRPC, metaschedulerWS),
rpcClient: rpcClient,
wsClient: wsClient,
Subscriber: metascheduler.NewEventSubscriber(metaschedulerRPC, metaschedulerWS),
rpcClient: rpcClient,
wsClient: wsClient,
}, nil
}

Expand Down
15 changes: 8 additions & 7 deletions cli/metascheduler/metascheduler_jobfetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/deepsquare-io/grid/cli/types"
metaschedulerabi "github.com/deepsquare-io/grid/cli/types/abi/metascheduler"
"github.com/deepsquare-io/grid/cli/types/job"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
)

Expand All @@ -40,7 +41,7 @@ func (c *jobFetcher) GetJob(ctx context.Context, id [32]byte) (types.Job, error)
}

type jobIterator struct {
types.JobFetcher
job.Fetcher
array [][32]byte
length int
index int
Expand Down Expand Up @@ -92,7 +93,7 @@ func (it *jobIterator) Error() error {
return it.err
}

func (c *jobFetcher) GetJobs(ctx context.Context) (types.JobLazyIterator, error) {
func (c *jobFetcher) GetJobs(ctx context.Context) (job.LazyIterator, error) {
jobIDs, err := c.GetByCustomer(&bind.CallOpts{
Context: ctx,
}, c.from())
Expand All @@ -104,10 +105,10 @@ func (c *jobFetcher) GetJobs(ctx context.Context) (types.JobLazyIterator, error)
jobIDs[i], jobIDs[j] = jobIDs[j], jobIDs[i]
}
return &jobIterator{
JobFetcher: c,
array: jobIDs,
length: len(jobIDs),
index: -1,
job: nil,
Fetcher: c,
array: jobIDs,
length: len(jobIDs),
index: -1,
job: nil,
}, nil
}
Loading

0 comments on commit 95970bc

Please sign in to comment.