Skip to content

Commit

Permalink
feat(rfq-guard): proof checks (#3002)
Browse files Browse the repository at this point in the history
* Feat: check log address before verifying

* Feat: add tracing

* [goreleaser]

* Feat: add RelayerAddress to PendingProven model

* Feat: check relayer address

* Cleanup: add tracing

* [goreleaser]

* Cleanup: lint

* Fix: set RelayerAddress

* [goreleaser]
  • Loading branch information
dwasse authored Aug 12, 2024
1 parent afe1d83 commit 47ff4ba
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
20 changes: 12 additions & 8 deletions services/rfq/guard/guarddb/base/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ type PendingProvenModel struct {
UpdatedAt time.Time
// Origin is the origin chain id
Origin uint32
// RelayerAddress is the address of the relayer that called prove()
RelayerAddress string
// TransactionID is the transaction id of the event
TransactionID string `gorm:"column:transaction_id;primaryKey"`
// TxHash is the hash of the relay transaction on destination
Expand All @@ -45,10 +47,11 @@ type PendingProvenModel struct {
// FromPendingProven converts a quote request to an object that can be stored in the db.
func FromPendingProven(proven guarddb.PendingProven) PendingProvenModel {
return PendingProvenModel{
Origin: proven.Origin,
TransactionID: hexutil.Encode(proven.TransactionID[:]),
TxHash: proven.TxHash.Hex(),
Status: proven.Status,
Origin: proven.Origin,
RelayerAddress: proven.RelayerAddress.Hex(),
TransactionID: hexutil.Encode(proven.TransactionID[:]),
TxHash: proven.TxHash.Hex(),
Status: proven.Status,
}
}

Expand All @@ -65,10 +68,11 @@ func (p PendingProvenModel) ToPendingProven() (*guarddb.PendingProven, error) {
}

return &guarddb.PendingProven{
Origin: p.Origin,
TransactionID: transactionID,
TxHash: common.HexToHash(p.TxHash),
Status: p.Status,
Origin: p.Origin,
RelayerAddress: common.HexToAddress(p.RelayerAddress),
TransactionID: transactionID,
TxHash: common.HexToHash(p.TxHash),
Status: p.Status,
}, nil
}

Expand Down
9 changes: 5 additions & 4 deletions services/rfq/guard/guarddb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ type BridgeRequest struct {

// PendingProven is the pending proven object.
type PendingProven struct {
Origin uint32
TransactionID [32]byte
TxHash common.Hash
Status PendingProvenStatus
Origin uint32
RelayerAddress common.Address
TransactionID [32]byte
TxHash common.Hash
Status PendingProvenStatus
}

// PendingProvenStatus is the status of a quote request in the db.
Expand Down
33 changes: 25 additions & 8 deletions services/rfq/guard/service/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ func (g *Guard) handleProofProvidedLog(parentCtx context.Context, event *fastbri
}()

proven := guarddb.PendingProven{
Origin: uint32(chainID),
TransactionID: event.TransactionId,
TxHash: event.TransactionHash,
Status: guarddb.ProveCalled,
Origin: uint32(chainID),
RelayerAddress: event.Relayer,
TransactionID: event.TransactionId,
TxHash: event.TransactionHash,
Status: guarddb.ProveCalled,
}
err = g.db.StorePendingProven(ctx, proven)
if err != nil {
Expand Down Expand Up @@ -162,7 +163,10 @@ func (g *Guard) handleProveCalled(parentCtx context.Context, proven *guarddb.Pen
return nil
}

//nolint:cyclop
func (g *Guard) isProveValid(ctx context.Context, proven *guarddb.PendingProven, bridgeRequest *guarddb.BridgeRequest) (bool, error) {
span := trace.SpanFromContext(ctx)

// get the receipt for this tx on dest chain
chainClient, err := g.client.GetChainClient(ctx, int(bridgeRequest.Transaction.DestChainId))
if err != nil {
Expand All @@ -176,11 +180,11 @@ func (g *Guard) isProveValid(ctx context.Context, proven *guarddb.PendingProven,
if err != nil {
return false, fmt.Errorf("could not get receipt: %w", err)
}
addr, err := g.cfg.GetRFQAddress(int(bridgeRequest.Transaction.DestChainId))
rfqAddr, err := g.cfg.GetRFQAddress(int(bridgeRequest.Transaction.DestChainId))
if err != nil {
return false, fmt.Errorf("could not get rfq address: %w", err)
}
parser, err := fastbridge.NewParser(common.HexToAddress(addr))
parser, err := fastbridge.NewParser(common.HexToAddress(rfqAddr))
if err != nil {
return false, fmt.Errorf("could not get parser: %w", err)
}
Expand All @@ -191,10 +195,23 @@ func (g *Guard) isProveValid(ctx context.Context, proven *guarddb.PendingProven,
continue
}

if log.Address != common.HexToAddress(rfqAddr) {
span.AddEvent(fmt.Sprintf("log address %s does not match rfq address %s", log.Address.Hex(), rfqAddr))
continue
}

event, ok := parsedEvent.(*fastbridge.FastBridgeBridgeRelayed)
if ok {
return relayMatchesBridgeRequest(event, bridgeRequest), nil
if !ok {
span.AddEvent("event is not a BridgeRelayed event")
continue
}

if event.Relayer != proven.RelayerAddress {
span.AddEvent(fmt.Sprintf("relayer address %s does not match prover address %s", event.Relayer.Hex(), proven.RelayerAddress.Hex()))
continue
}

return relayMatchesBridgeRequest(event, bridgeRequest), nil
}

return false, nil
Expand Down

0 comments on commit 47ff4ba

Please sign in to comment.