Skip to content

Commit

Permalink
feat(solver): check target before accept
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinhalliday committed Nov 14, 2024
1 parent c3bbe4e commit 0e7c942
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 11 deletions.
12 changes: 6 additions & 6 deletions e2e/solve/devapp/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/omni-network/omni/contracts/bindings"
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/evmchain"
solver "github.com/omni-network/omni/solver/app"
solver "github.com/omni-network/omni/solver/types"

"github.com/ethereum/go-ethereum/common"
)
Expand All @@ -26,7 +26,7 @@ func (t App) Address() common.Address {
return t.L1Vault
}

func (t App) IsAllowedCall(call *bindings.SolveCall) bool {
func (t App) IsAllowedCall(call bindings.SolveCall) bool {
if call.DestChainId != t.ChainID() {
return false
}
Expand All @@ -40,7 +40,7 @@ func (t App) IsAllowedCall(call *bindings.SolveCall) bool {
return err == nil
}

func (t App) TokenPrereqs(call *bindings.SolveCall) ([]*bindings.SolveTokenPrereq, error) {
func (t App) TokenPrereqs(call bindings.SolveCall) ([]bindings.SolveTokenPrereq, error) {
if !t.IsAllowedCall(call) {
return nil, errors.New("call not allowed")
}
Expand All @@ -50,15 +50,15 @@ func (t App) TokenPrereqs(call *bindings.SolveCall) ([]*bindings.SolveTokenPrere
return nil, errors.Wrap(err, "unpack deposit")
}

return []*bindings.SolveTokenPrereq{
return []bindings.SolveTokenPrereq{
{
Token: t.L1Token,
Amount: args.Amount,
},
}, nil
}

func (t App) Verify(srcChainID uint64, call *bindings.SolveCall, deposits []*bindings.SolveDeposit) error {
func (t App) Verify(srcChainID uint64, call bindings.SolveCall, deposits []bindings.SolveDeposit) error {
// we only accept deposits from mock L2
if srcChainID != evmchain.IDMockL2 {
return errors.New("source chain not supported", "src", srcChainID)
Expand All @@ -77,7 +77,7 @@ func (t App) Verify(srcChainID uint64, call *bindings.SolveCall, deposits []*bin

for _, deposit := range deposits {
if deposit.Token == t.L2Token {
l2token = deposit
l2token = &deposit
}
}

Expand Down
2 changes: 1 addition & 1 deletion solver/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func startEventStreams(
ParseID: newIDParser(inboxContracts),
GetRequest: newRequestGetter(inboxContracts),
ShouldReject: newRequestValidator(def),
Accept: newAcceptor(inboxContracts, backends, solverAddr),
Accept: newAcceptor(network.ID, inboxContracts, backends, solverAddr),
Reject: newRejector(inboxContracts, backends, solverAddr),
Fulfill: newFulfiller(outboxContracts, backends, solverAddr),
Claim: newClaimer(inboxContracts, backends, solverAddr),
Expand Down
11 changes: 11 additions & 0 deletions solver/app/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/ethclient/ethbackend"
"github.com/omni-network/omni/lib/log"
"github.com/omni-network/omni/lib/netconf"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -136,11 +137,21 @@ func newRejector(
}

func newAcceptor(
network netconf.ID,
inboxContracts map[uint64]*bindings.SolveInbox,
backends ethbackend.Backends,
solverAddr common.Address,
) func(ctx context.Context, chainID uint64, req bindings.SolveRequest) error {
return func(ctx context.Context, chainID uint64, req bindings.SolveRequest) error {
target, ok := targetFor(network, req.Call)
if !ok {
return errors.New("no target")
}

if err := target.Verify(chainID, req.Call, req.Deposits); err != nil {
return errors.Wrap(err, "verify target")
}

inbox, ok := inboxContracts[chainID]
if !ok {
return errors.New("unknown chain")
Expand Down
24 changes: 24 additions & 0 deletions solver/app/targets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//nolint:unused // Some functions are unused but are kept for future use
package app

import (
"github.com/omni-network/omni/contracts/bindings"
"github.com/omni-network/omni/e2e/solve/devapp"
"github.com/omni-network/omni/lib/netconf"
"github.com/omni-network/omni/solver/types"
)

var (
targets map[netconf.ID]types.Targets = map[netconf.ID]types.Targets{
netconf.Devnet: {devapp.GetApp()},
}
)

func targetFor(network netconf.ID, call bindings.SolveCall) (types.Target, bool) {
t, ok := targets[network]
if !ok {
return nil, false
}

return t.ForCall(call)
}
20 changes: 16 additions & 4 deletions solver/app/target.go → solver/types/target.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package types

import (
"github.com/omni-network/omni/contracts/bindings"
Expand All @@ -15,11 +15,23 @@ type Target interface {
Address() common.Address

// IsAllowedCall returns true if the call is allowed.
IsAllowedCall(call *bindings.SolveCall) bool
IsAllowedCall(call bindings.SolveCall) bool

// TokenPrereqs returns the token prerequisites required for the call.
TokenPrereqs(call *bindings.SolveCall) ([]*bindings.SolveTokenPrereq, error)
TokenPrereqs(call bindings.SolveCall) ([]bindings.SolveTokenPrereq, error)

// Verify returns an error if the call should not be fulfilled.
Verify(srcChainID uint64, call *bindings.SolveCall, deposits []*bindings.SolveDeposit) error
Verify(srcChainID uint64, call bindings.SolveCall, deposits []bindings.SolveDeposit) error
}

type Targets []Target

func (t Targets) ForCall(call bindings.SolveCall) (Target, bool) {
for _, target := range t {
if target.IsAllowedCall(call) {
return target, true
}
}

return nil, false
}

0 comments on commit 0e7c942

Please sign in to comment.