Skip to content

Commit

Permalink
feat: allow address targeting (#307)
Browse files Browse the repository at this point in the history
* feat: allow address targeting

* feat: add JobOfferCancelled handling

* fix: pr review comments

---------

Co-authored-by: Narb <29411347+narbs91@users.noreply.github.com>
  • Loading branch information
walkah and narbs91 authored Sep 11, 2024
1 parent c9a349c commit 2d49cc4
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 3 deletions.
3 changes: 3 additions & 0 deletions cmd/lilypad/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ func runJob(cmd *cobra.Command, options jobcreator.JobCreatorOptions) error {
case "ResultsRejected":
desc = "Results rejected! Getting refund..."
emoji = "🙀"
case "JobOfferCancelled":
desc = "Job cancelled..."
emoji = "😭"
default:
desc = st
emoji = "🌟"
Expand Down
3 changes: 2 additions & 1 deletion pkg/data/enums.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var AgreementState = []string{
"TimeoutSubmitResults",
"TimeoutJudgeResults",
"TimeoutMediateResults",
"JobOfferCancelled",
}

// PaymentReason corresponds to PaymentReason in TypeScript
Expand Down Expand Up @@ -83,7 +84,7 @@ func IsActiveAgreementState(itemType uint8) bool {
}

func IsTerminalAgreementState(itemType uint8) bool {
return itemType == GetAgreementStateIndex("ResultsAccepted") || itemType == GetAgreementStateIndex("MediationAccepted") || itemType == GetAgreementStateIndex("MediationRejected")
return itemType == GetAgreementStateIndex("JobOfferCancelled") || itemType == GetAgreementStateIndex("ResultsAccepted") || itemType == GetAgreementStateIndex("MediationAccepted") || itemType == GetAgreementStateIndex("MediationRejected")
}

// GetPaymentReason corresponds to getPaymentReason in TypeScript
Expand Down
7 changes: 7 additions & 0 deletions pkg/data/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ type ServiceConfig struct {
APIHost string `json:"api_host" toml:"api_host"`
}

type TargetConfig struct {
Address string `json:"address" toml:"address"`
}

// posted to the solver by a job creator
type JobOffer struct {
// this is the cid of the job offer where ID is set to empty string
Expand All @@ -114,6 +118,9 @@ type JobOffer struct {

// which parties are trusted by the job creator
Services ServiceConfig `json:"trusted_parties"`

// which node(s) (if any) to target
Target TargetConfig `json:"target"`
}

// this is what the solver keeps track of so we can know
Expand Down
2 changes: 1 addition & 1 deletion pkg/data/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"fmt"
"math/big"

"github.com/lilypad-tech/lilypad/pkg/web3/bindings/controller"
"github.com/ethereum/go-ethereum/common"
"github.com/lilypad-tech/lilypad/pkg/web3/bindings/controller"

mdag "github.com/ipfs/go-merkledag"
)
Expand Down
2 changes: 2 additions & 0 deletions pkg/jobcreator/jobcreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type JobCreatorOfferOptions struct {
Inputs map[string]string
// which mediators and directories this RP will trust
Services data.ServiceConfig
// which node(s) (if any) to target
Target data.TargetConfig
}

type JobCreatorOptions struct {
Expand Down
5 changes: 5 additions & 0 deletions pkg/jobcreator/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ waitloop:
}
}

// Check if our job was cancelled
if finalJobOffer.State == data.GetAgreementStateIndex("JobOfferCancelled") {
return nil, fmt.Errorf("job was cancelled")
}

result, err := jobCreatorService.GetResult(finalJobOffer.DealID)
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions pkg/jobcreator/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ func getJobOfferFromOptions(options JobCreatorOfferOptions, jobCreatorAddress st
Pricing: options.Pricing,
Timeouts: options.Timeouts,
Services: options.Services,
Target: options.Target,
}, nil
}
7 changes: 7 additions & 0 deletions pkg/options/job-creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func AddJobCreatorOfferCliFlags(cmd *cobra.Command, offerOptions *jobcreator.Job
AddTimeoutCliFlags(cmd, &offerOptions.Timeouts)
AddModuleCliFlags(cmd, &offerOptions.Module)
AddServicesCliFlags(cmd, &offerOptions.Services)
AddTargetCliFlags(cmd, &offerOptions.Target)
}

func AddJobCreatorCliFlags(cmd *cobra.Command, options *jobcreator.JobCreatorOptions) {
Expand Down Expand Up @@ -112,6 +113,12 @@ func ProcessJobCreatorOptions(options jobcreator.JobCreatorOptions, args []strin
}
options.Offer.Services = newServicesOptions

newTargetOptions, err := ProcessTargetOptions(options.Offer.Target)
if err != nil {
return options, err
}
options.Offer.Target = newTargetOptions

return options, CheckJobCreatorOptions(options)
}

Expand Down
17 changes: 17 additions & 0 deletions pkg/options/target.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package options

import (
"github.com/lilypad-tech/lilypad/pkg/data"
"github.com/spf13/cobra"
)

func AddTargetCliFlags(cmd *cobra.Command, targetConfig *data.TargetConfig) {
cmd.PersistentFlags().StringVarP(
&targetConfig.Address, "target", "t", targetConfig.Address,
`The address to target (TARGET)`,
)
}

func ProcessTargetOptions(options data.TargetConfig) (data.TargetConfig, error) {
return options, nil
}
2 changes: 1 addition & 1 deletion pkg/solver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (controller *SolverController) registerAsSolver() error {

func (controller *SolverController) solve() error {
// find out which deals we can make from matching the offers
deals, err := getMatchingDeals(controller.store)
deals, err := getMatchingDeals(controller.store, controller.updateJobOfferState)
if err != nil {
return err
}
Expand Down
27 changes: 27 additions & 0 deletions pkg/solver/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func doOffersMatch(

func getMatchingDeals(
db store.SolverStore,
updateJobOfferState func(string, string, uint8) (*data.JobOfferContainer, error),
) ([]data.Deal, error) {
deals := []data.Deal{}

Expand All @@ -143,6 +144,32 @@ func getMatchingDeals(
// loop over job offers
for _, jobOffer := range jobOffers {

// See if our jobOffer targets a specific address. If so, we will create a deal automatically
// with the matcing resourceOffer.
if jobOffer.JobOffer.Target.Address != "" {
resourceOffer, err := db.GetResourceOfferByAddress(jobOffer.JobOffer.Target.Address)
if err != nil {
return nil, err
}

// We don't have a resource provider for this address
if resourceOffer == nil {
log.Trace().
Str("job offer", jobOffer.ID).
Str("target address", jobOffer.JobOffer.Target.Address).
Msgf("No resource provider found for address")
updateJobOfferState(jobOffer.ID, "", data.GetAgreementStateIndex("JobOfferCancelled"))
continue
}

deal, err := data.GetDeal(jobOffer.JobOffer, resourceOffer.ResourceOffer)
if err != nil {
return nil, err
}
deals = append(deals, deal)
continue
}

// loop over resource offers
matchingResourceOffers := []data.ResourceOffer{}
for _, resourceOffer := range resourceOffers {
Expand Down
12 changes: 12 additions & 0 deletions pkg/solver/store/memory/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package store
import (
"fmt"
"os"
"strings"
"sync"

"github.com/lilypad-tech/lilypad/pkg/data"
Expand Down Expand Up @@ -197,6 +198,17 @@ func (s *SolverStoreMemory) GetResourceOffer(id string) (*data.ResourceOfferCont
return resourceOffer, nil
}

func (s *SolverStoreMemory) GetResourceOfferByAddress(address string) (*data.ResourceOfferContainer, error) {
s.mutex.RLock()
defer s.mutex.RUnlock()
for _, resourceOffer := range s.resourceOfferMap {
if strings.ToLower(resourceOffer.ResourceProvider) == strings.ToLower(address) {
return resourceOffer, nil
}
}
return nil, nil
}

func (s *SolverStoreMemory) GetDeal(id string) (*data.DealContainer, error) {
s.mutex.RLock()
defer s.mutex.RUnlock()
Expand Down
1 change: 1 addition & 0 deletions pkg/solver/store/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type SolverStore interface {
GetDeals(query GetDealsQuery) ([]data.DealContainer, error)
GetJobOffer(id string) (*data.JobOfferContainer, error)
GetResourceOffer(id string) (*data.ResourceOfferContainer, error)
GetResourceOfferByAddress(address string) (*data.ResourceOfferContainer, error)
GetDeal(id string) (*data.DealContainer, error)
GetResult(id string) (*data.Result, error)
GetMatchDecision(resourceOffer string, jobOffer string) (*data.MatchDecision, error)
Expand Down

0 comments on commit 2d49cc4

Please sign in to comment.