Skip to content

Commit

Permalink
handle extra-data
Browse files Browse the repository at this point in the history
  • Loading branch information
bukata-sa committed Dec 2, 2024
1 parent 38badc5 commit c4b0ba5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ccipdata

import (
"bytes"
"context"
"fmt"

Expand Down Expand Up @@ -39,7 +38,7 @@ func (d lbtcPayload) Validate() error {
}

type LBTCReader interface {
GetLBTCMessageInTx(ctx context.Context, payloadHash []byte, txHash string) ([]byte, error)
GetLBTCMessageInTx(ctx context.Context, payloadHash [32]byte, txHash string) ([]byte, error)
Close() error
}

Expand Down Expand Up @@ -83,7 +82,7 @@ func NewLBTCReaderWithCache(lggr logger.Logger, jobID string, transmitter common
return r, nil
}

func (r *LBTCReaderImpl) GetLBTCMessageInTx(ctx context.Context, payloadHash []byte, txHash string) ([]byte, error) {
func (r *LBTCReaderImpl) GetLBTCMessageInTx(ctx context.Context, payloadHash [32]byte, txHash string) ([]byte, error) {
var lpLogs []logpoller.Log

// fetch all the lbtc logs for the provided tx hash
Expand Down Expand Up @@ -113,11 +112,11 @@ func (r *LBTCReaderImpl) GetLBTCMessageInTx(ctx context.Context, payloadHash []b
}
for _, log := range lpLogs {
topics := log.GetTopics()
if currentPayloadHash := topics[3]; bytes.Equal(currentPayloadHash[:], payloadHash) {
if currentPayloadHash := topics[3]; currentPayloadHash == payloadHash {
return parseLBTCDepositPayload(log.Data)
}
}
return nil, fmt.Errorf("payload with hash=%s not found in logs", hexutil.Encode(payloadHash))
return nil, fmt.Errorf("payload with hash=%s not found in logs", hexutil.Encode(payloadHash[:]))
}

func parseLBTCDepositPayload(logData []byte) ([]byte, error) {
Expand Down
21 changes: 13 additions & 8 deletions core/services/ocr2/plugins/ccip/tokendata/lbtc/lbtc.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package lbtc

import (
"bytes"
"context"
"crypto/sha256"
"fmt"
Expand Down Expand Up @@ -262,17 +261,23 @@ func (s *TokenDataReader) getLBTCPayloadAndHash(ctx context.Context, msg cciptyp
if err != nil {
return nil, [32]byte{}, err
}
payloadHash := decodedSourceTokenData.ExtraData
if len(payloadHash) != 32 {
s.lggr.Warnw("SourceTokenData.extraData is not 32 bytes. LBTC Attestation probably disabled onchain", "payloadHash", payloadHash)
destTokenData := decodedSourceTokenData.ExtraData
var payloadHash [32]byte
if len(destTokenData) != 32 {
payloadHash = sha256.Sum256(destTokenData)
s.lggr.Warnw("SourceTokenData.extraData size is not 32. It could be a LBTC payload, not LBTC payload sha256. "+
"Probably this message is sent when LBTC attestation was disabled onchain. Will use sha256 from this value",
"destTokenData", destTokenData, "newPayloadHash", payloadHash)
} else {
payloadHash = [32]byte(destTokenData)
}
payload, err := s.lbtcReader.GetLBTCMessageInTx(ctx, payloadHash, msg.TxHash)
actualPayload, err := s.lbtcReader.GetLBTCMessageInTx(ctx, payloadHash, msg.TxHash)
if err != nil {
return nil, [32]byte{}, err
}
actualPayloadHash := sha256.Sum256(payload)
if bytes.Equal(actualPayloadHash[:], payloadHash) {
return payload, [32]byte(payloadHash), nil
actualPayloadHash := sha256.Sum256(actualPayload)
if actualPayloadHash == payloadHash {
return actualPayload, payloadHash, nil
}
return nil, [32]byte{}, fmt.Errorf("payload hash mismatch: expected %x, got %x", payloadHash, actualPayloadHash)
}
Expand Down

0 comments on commit c4b0ba5

Please sign in to comment.