Skip to content

Commit

Permalink
Merge pull request #48 from FastLane-Labs/reputation-score
Browse files Browse the repository at this point in the history
Simple solver reputation score
  • Loading branch information
jj1980a authored Apr 18, 2024
2 parents 74f06d0 + f9e8a5a commit cdc8aa2
Show file tree
Hide file tree
Showing 15 changed files with 3,633 additions and 87 deletions.
38 changes: 19 additions & 19 deletions auction/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ type Auction struct {
open bool
userOpHash common.Hash

userOp *operation.UserOperation
userOperationPartial *operation.UserOperationPartial
solverOps []*operation.SolverOperation
userOp *operation.UserOperation
userOperationPartialRaw *operation.UserOperationPartialRaw
solverOpsWithScore []*operation.SolverOperationWithScore

completionSubs []chan []*operation.SolverOperation
completionSubs []chan []*operation.SolverOperationWithScore

createdAt time.Time

mu sync.RWMutex
}

func NewAuction(duration time.Duration, userOp *operation.UserOperation, userOperationPartial *operation.UserOperationPartial, userOpHash common.Hash) *Auction {
func NewAuction(duration time.Duration, userOp *operation.UserOperation, userOperationPartialRaw *operation.UserOperationPartialRaw, userOpHash common.Hash) *Auction {
auction := &Auction{
open: true,
userOpHash: userOpHash,
userOp: userOp,
userOperationPartial: userOperationPartial,
solverOps: make([]*operation.SolverOperation, 0),
completionSubs: make([]chan []*operation.SolverOperation, 0),
createdAt: time.Now(),
open: true,
userOpHash: userOpHash,
userOp: userOp,
userOperationPartialRaw: userOperationPartialRaw,
solverOpsWithScore: make([]*operation.SolverOperationWithScore, 0),
completionSubs: make([]chan []*operation.SolverOperationWithScore, 0),
createdAt: time.Now(),
}

time.AfterFunc(duration, auction.close)
Expand All @@ -54,34 +54,34 @@ func (a *Auction) close() {

for _, subChan := range a.completionSubs {
select {
case subChan <- a.solverOps:
case subChan <- a.solverOpsWithScore:
default:
// Sub isn't listening, don't block
}
}
}

func (a *Auction) addCompletionSub(subChan chan []*operation.SolverOperation) {
func (a *Auction) addCompletionSub(subChan chan []*operation.SolverOperationWithScore) {
a.mu.Lock()
defer a.mu.Unlock()

a.completionSubs = append(a.completionSubs, subChan)
}

func (a *Auction) addSolverOp(solverOp *operation.SolverOperation) *relayerror.Error {
func (a *Auction) addSolverOp(solverOp *operation.SolverOperationWithScore) *relayerror.Error {
a.mu.Lock()
defer a.mu.Unlock()

if !a.open {
log.Info("auction for this user operation has already ended", "userOpHash", solverOp.UserOpHash.Hex())
log.Info("auction for this user operation has already ended", "userOpHash", solverOp.SolverOperation.UserOpHash.Hex())
return ErrAuctionClosed
}

a.solverOps = append(a.solverOps, solverOp)
a.solverOpsWithScore = append(a.solverOpsWithScore, solverOp)
return nil
}

func (a *Auction) getSolverOps(completionChan chan []*operation.SolverOperation) ([]*operation.SolverOperation, *relayerror.Error) {
func (a *Auction) getSolverOps(completionChan chan []*operation.SolverOperationWithScore) ([]*operation.SolverOperationWithScore, *relayerror.Error) {
a.mu.RLock()
open := a.open
a.mu.RUnlock()
Expand All @@ -97,5 +97,5 @@ func (a *Auction) getSolverOps(completionChan chan []*operation.SolverOperation)
return nil, ErrAuctionOngoing
}

return a.solverOps, nil
return a.solverOpsWithScore, nil
}
24 changes: 17 additions & 7 deletions auction/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
)

type balanceOfBondedFn func(common.Address) (*big.Int, *relayerror.Error)
type reputationScoreFn func(account common.Address) int

type Manager struct {
ethClient *ethclient.Client
Expand All @@ -42,17 +43,19 @@ type Manager struct {
atlasDomainSeparator common.Hash

balanceOfBonded balanceOfBondedFn
reputationScore reputationScoreFn

mu sync.RWMutex
}

func NewManager(ethClient *ethclient.Client, config *config.Config, atlasDomainSeparator common.Hash, balanceOfBonded balanceOfBondedFn) *Manager {
func NewManager(ethClient *ethclient.Client, config *config.Config, atlasDomainSeparator common.Hash, balanceOfBonded balanceOfBondedFn, reputationScore reputationScoreFn) *Manager {
am := &Manager{
ethClient: ethClient,
config: config,
auctions: make(map[common.Hash]*Auction),
atlasDomainSeparator: atlasDomainSeparator,
balanceOfBonded: balanceOfBonded,
reputationScore: reputationScore,
}

go am.auctionsCleaner()
Expand All @@ -71,7 +74,7 @@ func (am *Manager) auctionsCleaner() {
}
}

func (am *Manager) NewUserOperation(userOp *operation.UserOperation, hints []common.Address) (common.Hash, *operation.UserOperationPartial, *relayerror.Error) {
func (am *Manager) NewUserOperation(userOp *operation.UserOperation, hints []common.Address) (common.Hash, *operation.UserOperationPartialRaw, *relayerror.Error) {
userOpHash, relayErr := userOp.Hash()
if relayErr != nil {
log.Info("failed to compute user operation hash", "err", relayErr.Message)
Expand Down Expand Up @@ -116,13 +119,13 @@ func (am *Manager) NewUserOperation(userOp *operation.UserOperation, hints []com
return common.Hash{}, nil, ErrAuctionAlreadyStarted
}

userOperationPartial := operation.NewUserOperationPartial(userOp, hints)
userOperationPartialRaw := operation.NewUserOperationPartialRaw(userOp, hints)

am.auctions[userOpHash] = NewAuction(am.config.Relay.Auction.Duration, userOp, userOperationPartial, userOpHash)
return userOpHash, userOperationPartial, nil
am.auctions[userOpHash] = NewAuction(am.config.Relay.Auction.Duration, userOp, userOperationPartialRaw, userOpHash)
return userOpHash, userOperationPartialRaw, nil
}

func (am *Manager) GetSolverOperations(userOpHash common.Hash, completionChan chan []*operation.SolverOperation) ([]*operation.SolverOperation, *relayerror.Error) {
func (am *Manager) GetSolverOperations(userOpHash common.Hash, completionChan chan []*operation.SolverOperationWithScore) ([]*operation.SolverOperationWithScore, *relayerror.Error) {
am.mu.RLock()
defer am.mu.RUnlock()

Expand Down Expand Up @@ -205,7 +208,14 @@ func (am *Manager) NewSolverOperation(solverOp *operation.SolverOperation) *rela
return ErrSolverOpFailedSimulation.AddMessage(fmt.Sprintf("result: %d, solverOutcomeResult: %s", result, solverOutcomeResult.String()))
}

return auction.addSolverOp(solverOp)
solverOpWithScore := &operation.SolverOperationWithScore{
SolverOperation: solverOp,
Score: am.reputationScore(solverOp.From),
}

log.Info("valid solver operation", "userOpHash", auction.userOpHash.Hex(), "from", solverOp.From.Hex(), "score", solverOpWithScore.Score)

return auction.addSolverOp(solverOpWithScore)
}

func SolverGasLimit(dAppControlAddress common.Address, ethClient *ethclient.Client) (uint32, error) {
Expand Down
3,449 changes: 3,449 additions & 0 deletions contract/storage/storage.go

Large diffs are not rendered by default.

Loading

0 comments on commit cdc8aa2

Please sign in to comment.