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 51f9182
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 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
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func Test_MockLogPoller(t *testing.T) {
LogWithPayload(t, 20, payload),
}, nil)

data, err := reader.GetLBTCMessageInTx(context.Background(), payloadHash[:], "0x0001")
data, err := reader.GetLBTCMessageInTx(context.Background(), payloadHash, "0x0001")
assert.NoError(t, err)
assert.Equal(t, payload, data)
})
Expand All @@ -67,7 +67,7 @@ func Test_MockLogPoller(t *testing.T) {
LogWithPayload(t, 30, []byte("0x2222")),
}, nil)

data, err := reader.GetLBTCMessageInTx(context.Background(), payloadHash[:], "0x0001")
data, err := reader.GetLBTCMessageInTx(context.Background(), payloadHash, "0x0001")
assert.NoError(t, err)
assert.Equal(t, payload, data)
})
Expand All @@ -82,7 +82,7 @@ func Test_MockLogPoller(t *testing.T) {
LogWithPayload(t, 30, []byte("0x2222")),
}, nil)

data, err := reader.GetLBTCMessageInTx(context.Background(), payloadHash[:], "0x0001")
data, err := reader.GetLBTCMessageInTx(context.Background(), payloadHash, "0x0001")
assert.Nil(t, data)
assert.Errorf(t, err, "payload with hash=%s not found in logs", payloadHash)
})
Expand All @@ -94,7 +94,7 @@ func Test_MockLogPoller(t *testing.T) {
lp.On("IndexedLogsByTxHash", mock.Anything, reader.eventID, reader.transmitterAddress, mock.Anything).
Return([]logpoller.Log{}, nil)

data, err := reader.GetLBTCMessageInTx(context.Background(), payloadHash[:], "0x0001")
data, err := reader.GetLBTCMessageInTx(context.Background(), payloadHash, "0x0001")
assert.Nil(t, data)
assert.Errorf(t, err, "payload with hash=%s not found in logs", payloadHash)
})
Expand All @@ -105,7 +105,7 @@ func Test_MockLogPoller(t *testing.T) {
require.NoError(t, err)
r, err := NewLBTCReaderWithCache(lggr, "job_1", utils.RandomAddress(), nil, rCache, false)
require.NoError(t, err)
data, err := r.GetLBTCMessageInTx(context.Background(), payloadHash[:], "0x0001")
data, err := r.GetLBTCMessageInTx(context.Background(), payloadHash, "0x0001")
assert.NoError(t, err)
assert.Equal(t, payload, data)
})
Expand Down Expand Up @@ -146,7 +146,7 @@ func Test_SimulatedLogPoller_FoundMultiple(t *testing.T) {
reader, err := NewLBTCReader(lggr, "job_1", transmitter, lp, true)
require.NoError(t, err)

data, err := reader.GetLBTCMessageInTx(context.Background(), payloadHash[:], common.Hash{}.Hex())
data, err := reader.GetLBTCMessageInTx(context.Background(), payloadHash, common.Hash{}.Hex())
assert.NoError(t, err)
assert.Equal(t, payload, data)
}
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 51f9182

Please sign in to comment.