Skip to content

Commit 2d49cc4

Browse files
walkahnarbs91
andauthored
feat: allow address targeting (#307)
* feat: allow address targeting * feat: add JobOfferCancelled handling * fix: pr review comments --------- Co-authored-by: Narb <29411347+narbs91@users.noreply.github.com>
1 parent c9a349c commit 2d49cc4

File tree

13 files changed

+86
-3
lines changed

13 files changed

+86
-3
lines changed

cmd/lilypad/run.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ func runJob(cmd *cobra.Command, options jobcreator.JobCreatorOptions) error {
116116
case "ResultsRejected":
117117
desc = "Results rejected! Getting refund..."
118118
emoji = "🙀"
119+
case "JobOfferCancelled":
120+
desc = "Job cancelled..."
121+
emoji = "😭"
119122
default:
120123
desc = st
121124
emoji = "🌟"

pkg/data/enums.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var AgreementState = []string{
2626
"TimeoutSubmitResults",
2727
"TimeoutJudgeResults",
2828
"TimeoutMediateResults",
29+
"JobOfferCancelled",
2930
}
3031

3132
// PaymentReason corresponds to PaymentReason in TypeScript
@@ -83,7 +84,7 @@ func IsActiveAgreementState(itemType uint8) bool {
8384
}
8485

8586
func IsTerminalAgreementState(itemType uint8) bool {
86-
return itemType == GetAgreementStateIndex("ResultsAccepted") || itemType == GetAgreementStateIndex("MediationAccepted") || itemType == GetAgreementStateIndex("MediationRejected")
87+
return itemType == GetAgreementStateIndex("JobOfferCancelled") || itemType == GetAgreementStateIndex("ResultsAccepted") || itemType == GetAgreementStateIndex("MediationAccepted") || itemType == GetAgreementStateIndex("MediationRejected")
8788
}
8889

8990
// GetPaymentReason corresponds to getPaymentReason in TypeScript

pkg/data/types.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ type ServiceConfig struct {
8888
APIHost string `json:"api_host" toml:"api_host"`
8989
}
9090

91+
type TargetConfig struct {
92+
Address string `json:"address" toml:"address"`
93+
}
94+
9195
// posted to the solver by a job creator
9296
type JobOffer struct {
9397
// this is the cid of the job offer where ID is set to empty string
@@ -114,6 +118,9 @@ type JobOffer struct {
114118

115119
// which parties are trusted by the job creator
116120
Services ServiceConfig `json:"trusted_parties"`
121+
122+
// which node(s) (if any) to target
123+
Target TargetConfig `json:"target"`
117124
}
118125

119126
// this is what the solver keeps track of so we can know

pkg/data/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"fmt"
66
"math/big"
77

8-
"github.com/lilypad-tech/lilypad/pkg/web3/bindings/controller"
98
"github.com/ethereum/go-ethereum/common"
9+
"github.com/lilypad-tech/lilypad/pkg/web3/bindings/controller"
1010

1111
mdag "github.com/ipfs/go-merkledag"
1212
)

pkg/jobcreator/jobcreator.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ type JobCreatorOfferOptions struct {
3131
Inputs map[string]string
3232
// which mediators and directories this RP will trust
3333
Services data.ServiceConfig
34+
// which node(s) (if any) to target
35+
Target data.TargetConfig
3436
}
3537

3638
type JobCreatorOptions struct {

pkg/jobcreator/run.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ waitloop:
7979
}
8080
}
8181

82+
// Check if our job was cancelled
83+
if finalJobOffer.State == data.GetAgreementStateIndex("JobOfferCancelled") {
84+
return nil, fmt.Errorf("job was cancelled")
85+
}
86+
8287
result, err := jobCreatorService.GetResult(finalJobOffer.DealID)
8388
if err != nil {
8489
return nil, err

pkg/jobcreator/utils.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ func getJobOfferFromOptions(options JobCreatorOfferOptions, jobCreatorAddress st
2929
Pricing: options.Pricing,
3030
Timeouts: options.Timeouts,
3131
Services: options.Services,
32+
Target: options.Target,
3233
}, nil
3334
}

pkg/options/job-creator.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func AddJobCreatorOfferCliFlags(cmd *cobra.Command, offerOptions *jobcreator.Job
5555
AddTimeoutCliFlags(cmd, &offerOptions.Timeouts)
5656
AddModuleCliFlags(cmd, &offerOptions.Module)
5757
AddServicesCliFlags(cmd, &offerOptions.Services)
58+
AddTargetCliFlags(cmd, &offerOptions.Target)
5859
}
5960

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

116+
newTargetOptions, err := ProcessTargetOptions(options.Offer.Target)
117+
if err != nil {
118+
return options, err
119+
}
120+
options.Offer.Target = newTargetOptions
121+
115122
return options, CheckJobCreatorOptions(options)
116123
}
117124

pkg/options/target.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package options
2+
3+
import (
4+
"github.com/lilypad-tech/lilypad/pkg/data"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
func AddTargetCliFlags(cmd *cobra.Command, targetConfig *data.TargetConfig) {
9+
cmd.PersistentFlags().StringVarP(
10+
&targetConfig.Address, "target", "t", targetConfig.Address,
11+
`The address to target (TARGET)`,
12+
)
13+
}
14+
15+
func ProcessTargetOptions(options data.TargetConfig) (data.TargetConfig, error) {
16+
return options, nil
17+
}

pkg/solver/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func (controller *SolverController) registerAsSolver() error {
268268

269269
func (controller *SolverController) solve() error {
270270
// find out which deals we can make from matching the offers
271-
deals, err := getMatchingDeals(controller.store)
271+
deals, err := getMatchingDeals(controller.store, controller.updateJobOfferState)
272272
if err != nil {
273273
return err
274274
}

pkg/solver/matcher.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ func doOffersMatch(
123123

124124
func getMatchingDeals(
125125
db store.SolverStore,
126+
updateJobOfferState func(string, string, uint8) (*data.JobOfferContainer, error),
126127
) ([]data.Deal, error) {
127128
deals := []data.Deal{}
128129

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

147+
// See if our jobOffer targets a specific address. If so, we will create a deal automatically
148+
// with the matcing resourceOffer.
149+
if jobOffer.JobOffer.Target.Address != "" {
150+
resourceOffer, err := db.GetResourceOfferByAddress(jobOffer.JobOffer.Target.Address)
151+
if err != nil {
152+
return nil, err
153+
}
154+
155+
// We don't have a resource provider for this address
156+
if resourceOffer == nil {
157+
log.Trace().
158+
Str("job offer", jobOffer.ID).
159+
Str("target address", jobOffer.JobOffer.Target.Address).
160+
Msgf("No resource provider found for address")
161+
updateJobOfferState(jobOffer.ID, "", data.GetAgreementStateIndex("JobOfferCancelled"))
162+
continue
163+
}
164+
165+
deal, err := data.GetDeal(jobOffer.JobOffer, resourceOffer.ResourceOffer)
166+
if err != nil {
167+
return nil, err
168+
}
169+
deals = append(deals, deal)
170+
continue
171+
}
172+
146173
// loop over resource offers
147174
matchingResourceOffers := []data.ResourceOffer{}
148175
for _, resourceOffer := range resourceOffers {

pkg/solver/store/memory/store.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package store
33
import (
44
"fmt"
55
"os"
6+
"strings"
67
"sync"
78

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

201+
func (s *SolverStoreMemory) GetResourceOfferByAddress(address string) (*data.ResourceOfferContainer, error) {
202+
s.mutex.RLock()
203+
defer s.mutex.RUnlock()
204+
for _, resourceOffer := range s.resourceOfferMap {
205+
if strings.ToLower(resourceOffer.ResourceProvider) == strings.ToLower(address) {
206+
return resourceOffer, nil
207+
}
208+
}
209+
return nil, nil
210+
}
211+
200212
func (s *SolverStoreMemory) GetDeal(id string) (*data.DealContainer, error) {
201213
s.mutex.RLock()
202214
defer s.mutex.RUnlock()

pkg/solver/store/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type SolverStore interface {
5353
GetDeals(query GetDealsQuery) ([]data.DealContainer, error)
5454
GetJobOffer(id string) (*data.JobOfferContainer, error)
5555
GetResourceOffer(id string) (*data.ResourceOfferContainer, error)
56+
GetResourceOfferByAddress(address string) (*data.ResourceOfferContainer, error)
5657
GetDeal(id string) (*data.DealContainer, error)
5758
GetResult(id string) (*data.Result, error)
5859
GetMatchDecision(resourceOffer string, jobOffer string) (*data.MatchDecision, error)

0 commit comments

Comments
 (0)