Skip to content

Commit

Permalink
Use chainlink-commons for hash and merkle pkg in ocr2 (#1049)
Browse files Browse the repository at this point in the history
## Motivation
With
[chainlink-common#527](smartcontractkit/chainlink-common#527)
we moved hashlib (now hashutil) and merklemulti to a common location.

## Solution
Update [ccip repo](https://github.com/smartcontractkit/ccip) to remove
`./core/services/ocr2/plugins/ccip/pkg/hashlib/` and
`./core/services/ocr2/plugins/ccip/pkg/merklemulti/`. Replace them with
the versions in chainlink-common.
  • Loading branch information
agusaldasoro authored Jun 19, 2024
1 parent 15b7daf commit 7f834cc
Show file tree
Hide file tree
Showing 24 changed files with 52 additions and 873 deletions.
6 changes: 3 additions & 3 deletions core/services/ocr2/plugins/ccip/ccipcommit/ocr2.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors"

"github.com/smartcontractkit/chainlink-common/pkg/hashutil"
"github.com/smartcontractkit/chainlink-common/pkg/merklemulti"
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccip"

"github.com/smartcontractkit/libocr/offchainreporting2plus/types"
Expand All @@ -22,8 +24,6 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdata"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdata/ccipdataprovider"
db "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdb"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/pkg/hashlib"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/pkg/merklemulti"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/prices"
)

Expand Down Expand Up @@ -510,7 +510,7 @@ func (r *CommitReportingPlugin) buildReport(ctx context.Context, lggr logger.Log
if !ccipcalc.ContiguousReqs(lggr, interval.Min, interval.Max, seqNrs) {
return cciptypes.CommitStoreReport{}, errors.Errorf("do not have full range [%v, %v] have %v", interval.Min, interval.Max, seqNrs)
}
tree, err := merklemulti.NewTree(hashlib.NewKeccakCtx(), leaves)
tree, err := merklemulti.NewTree(hashutil.NewKeccak(), leaves)
if err != nil {
return cciptypes.CommitStoreReport{}, err
}
Expand Down
10 changes: 5 additions & 5 deletions core/services/ocr2/plugins/ccip/ccipcommit/ocr2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ import (
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"

"github.com/smartcontractkit/chainlink-common/pkg/config"
"github.com/smartcontractkit/chainlink-common/pkg/hashutil"
"github.com/smartcontractkit/chainlink-common/pkg/merklemulti"
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccip"
"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"

"github.com/smartcontractkit/libocr/offchainreporting2plus/types"

Expand All @@ -43,9 +44,8 @@ import (
ccipdatamocks "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdata/mocks"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdata/v1_0_0"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdata/v1_2_0"

ccipdbmocks "github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdb/mocks"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/pkg/hashlib"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/pkg/merklemulti"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/prices"
)

Expand Down Expand Up @@ -1498,7 +1498,7 @@ func Test_calculateIntervalConsensus(t *testing.T) {
}

func TestCommitReportToEthTxMeta(t *testing.T) {
mctx := hashlib.NewKeccakCtx()
mctx := hashutil.NewKeccak()
tree, err := merklemulti.NewTree(mctx, [][32]byte{mctx.Hash([]byte{0xaa})})
require.NoError(t, err)

Expand Down
6 changes: 3 additions & 3 deletions core/services/ocr2/plugins/ccip/ccipexec/batching.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors"

"github.com/smartcontractkit/chainlink-common/pkg/hashutil"
"github.com/smartcontractkit/chainlink-common/pkg/merklemulti"
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccip"

"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/abihelpers"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdata"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/pkg/hashlib"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/pkg/merklemulti"
)

func getProofData(
Expand All @@ -35,7 +35,7 @@ func getProofData(
for _, req := range sendReqs {
leaves = append(leaves, req.Hash)
}
tree, err = merklemulti.NewTree(hashlib.NewKeccakCtx(), leaves)
tree, err = merklemulti.NewTree(hashutil.NewKeccak(), leaves)
if err != nil {
return nil, nil, nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions core/services/ocr2/plugins/ccip/ccipexec/ocr2.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/smartcontractkit/libocr/offchainreporting2plus/types"

"github.com/smartcontractkit/chainlink-common/pkg/hashutil"
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccip"

"github.com/smartcontractkit/chainlink/v2/core/logger"
Expand All @@ -24,7 +25,6 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdata"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdata/batchreader"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdata/ccipdataprovider"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/pkg/hashlib"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/prices"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/tokendata"
)
Expand Down Expand Up @@ -668,7 +668,7 @@ func calculateObservedMessagesConsensus(observations []ccip.ExecutionObservation
tally := make(map[tallyKey]tallyVal)
for _, obs := range observations {
for seqNr, msgData := range obs.Messages {
tokenDataHash, err := hashlib.BytesOfBytesKeccak(msgData.TokenData)
tokenDataHash, err := hashutil.BytesOfBytesKeccak(msgData.TokenData)
if err != nil {
return nil, fmt.Errorf("bytes of bytes keccak: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package ccipdata
import (
"github.com/ethereum/go-ethereum/core/types"

"github.com/smartcontractkit/chainlink-common/pkg/hashutil"
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccip"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/pkg/hashlib"
)

type LeafHasherInterface[H hashlib.Hash] interface {
type LeafHasherInterface[H hashutil.Hash] interface {
HashLeaf(log types.Log) (H, error)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types"

"github.com/smartcontractkit/chainlink-common/pkg/hashutil"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/evm_2_evm_onramp_1_0_0"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/abihelpers"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/pkg/hashlib"
)

const (
Expand All @@ -21,16 +21,16 @@ var LeafDomainSeparator = [1]byte{0x00}

type LeafHasher struct {
metaDataHash [32]byte
ctx hashlib.Ctx[[32]byte]
ctx hashutil.Hasher[[32]byte]
onRamp *evm_2_evm_onramp_1_0_0.EVM2EVMOnRamp
}

func GetMetaDataHash[H hashlib.Hash](ctx hashlib.Ctx[H], prefix [32]byte, sourceChainSelector uint64, onRampId common.Address, destChainSelector uint64) H {
func GetMetaDataHash[H hashutil.Hash](ctx hashutil.Hasher[H], prefix [32]byte, sourceChainSelector uint64, onRampId common.Address, destChainSelector uint64) H {
paddedOnRamp := common.BytesToHash(onRampId[:])
return ctx.Hash(utils.ConcatBytes(prefix[:], math.U256Bytes(big.NewInt(0).SetUint64(sourceChainSelector)), math.U256Bytes(big.NewInt(0).SetUint64(destChainSelector)), paddedOnRamp[:]))
}

func NewLeafHasher(sourceChainSelector uint64, destChainSelector uint64, onRampId common.Address, ctx hashlib.Ctx[[32]byte], onRamp *evm_2_evm_onramp_1_0_0.EVM2EVMOnRamp) *LeafHasher {
func NewLeafHasher(sourceChainSelector uint64, destChainSelector uint64, onRampId common.Address, ctx hashutil.Hasher[[32]byte], onRamp *evm_2_evm_onramp_1_0_0.EVM2EVMOnRamp) *LeafHasher {
return &LeafHasher{
metaDataHash: GetMetaDataHash(ctx, ctx.Hash([]byte(MetaDataHashPrefix)), sourceChainSelector, onRampId, destChainSelector),
ctx: ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-common/pkg/hashutil"

"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/evm_2_evm_onramp_1_0_0"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/abihelpers"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/pkg/hashlib"
)

func TestHasherV1_0_0(t *testing.T) {
Expand All @@ -21,7 +22,7 @@ func TestHasherV1_0_0(t *testing.T) {

ramp, err := evm_2_evm_onramp_1_0_0.NewEVM2EVMOnRamp(onRampAddress, nil)
require.NoError(t, err)
hashingCtx := hashlib.NewKeccakCtx()
hashingCtx := hashutil.NewKeccak()
hasher := NewLeafHasher(sourceChainSelector, destChainSelector, onRampAddress, hashingCtx, ramp)

message := evm_2_evm_onramp_1_0_0.InternalEVM2EVMMessage{
Expand Down Expand Up @@ -77,7 +78,7 @@ func TestHasherV1_0_0(t *testing.T) {
func TestMetaDataHash(t *testing.T) {
sourceChainSelector, destChainSelector := uint64(1), uint64(4)
onRampAddress := common.HexToAddress("0x5550000000000000000000000000000000000001")
ctx := hashlib.NewKeccakCtx()
ctx := hashutil.NewKeccak()
hash := GetMetaDataHash(ctx, ctx.Hash([]byte(MetaDataHashPrefix)), sourceChainSelector, onRampAddress, destChainSelector)
require.Equal(t, "1409948abde219f43870c3d6d1c16beabd8878eb5039a3fa765eb56e4b8ded9e", hex.EncodeToString(hash[:]))
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"

"github.com/smartcontractkit/chainlink-common/pkg/hashutil"
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccip"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller"
Expand All @@ -19,7 +20,6 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/cache"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdata"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/logpollerutil"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/pkg/hashlib"
)

const (
Expand Down Expand Up @@ -86,7 +86,7 @@ func NewOnRamp(lggr logger.Logger, sourceSelector, destSelector uint64, onRampAd
client: source,
filters: filters,
lp: sourceLP,
leafHasher: NewLeafHasher(sourceSelector, destSelector, onRampAddress, hashlib.NewKeccakCtx(), onRamp),
leafHasher: NewLeafHasher(sourceSelector, destSelector, onRampAddress, hashutil.NewKeccak(), onRamp),
// offset || sourceChainID || seqNum || ...
sendRequestedSeqNumberWord: 2,
sendRequestedEventSig: eventSig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"

"github.com/smartcontractkit/chainlink-common/pkg/hashutil"

"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/evm_2_evm_onramp_1_2_0"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/abihelpers"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdata/v1_0_0"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/pkg/hashlib"
)

const (
Expand All @@ -17,11 +18,11 @@ const (

type LeafHasher struct {
metaDataHash [32]byte
ctx hashlib.Ctx[[32]byte]
ctx hashutil.Hasher[[32]byte]
onRamp *evm_2_evm_onramp_1_2_0.EVM2EVMOnRamp
}

func NewLeafHasher(sourceChainSelector uint64, destChainSelector uint64, onRampId common.Address, ctx hashlib.Ctx[[32]byte], onRamp *evm_2_evm_onramp_1_2_0.EVM2EVMOnRamp) *LeafHasher {
func NewLeafHasher(sourceChainSelector uint64, destChainSelector uint64, onRampId common.Address, ctx hashutil.Hasher[[32]byte], onRamp *evm_2_evm_onramp_1_2_0.EVM2EVMOnRamp) *LeafHasher {
return &LeafHasher{
metaDataHash: v1_0_0.GetMetaDataHash(ctx, ctx.Hash([]byte(MetaDataHashPrefix)), sourceChainSelector, onRampId, destChainSelector),
ctx: ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-common/pkg/hashutil"

"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/evm_2_evm_onramp_1_2_0"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/abihelpers"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/pkg/hashlib"
)

func TestHasherV1_2_0(t *testing.T) {
sourceChainSelector, destChainSelector := uint64(1), uint64(4)
onRampAddress := common.HexToAddress("0x5550000000000000000000000000000000000001")
onRampABI := abihelpers.MustParseABI(evm_2_evm_onramp_1_2_0.EVM2EVMOnRampABI)

hashingCtx := hashlib.NewKeccakCtx()
hashingCtx := hashutil.NewKeccak()
ramp, err := evm_2_evm_onramp_1_2_0.NewEVM2EVMOnRamp(onRampAddress, nil)
require.NoError(t, err)
hasher := NewLeafHasher(sourceChainSelector, destChainSelector, onRampAddress, hashingCtx, ramp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"

"github.com/smartcontractkit/chainlink-common/pkg/hashutil"
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccip"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/logpoller"
Expand All @@ -20,7 +21,6 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/cache"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdata"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/logpollerutil"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/pkg/hashlib"
)

var (
Expand Down Expand Up @@ -101,7 +101,7 @@ func NewOnRamp(lggr logger.Logger, sourceSelector, destSelector uint64, onRampAd
lggr: lggr,
client: source,
lp: sourceLP,
leafHasher: NewLeafHasher(sourceSelector, destSelector, onRampAddress, hashlib.NewKeccakCtx(), onRamp),
leafHasher: NewLeafHasher(sourceSelector, destSelector, onRampAddress, hashutil.NewKeccak(), onRamp),
onRamp: onRamp,
filters: filters,
address: onRampAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"

"github.com/smartcontractkit/chainlink-common/pkg/hashutil"

"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/evm_2_evm_onramp"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/abihelpers"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdata/v1_0_0"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/pkg/hashlib"
)

const (
Expand All @@ -17,11 +18,11 @@ const (

type LeafHasher struct {
metaDataHash [32]byte
ctx hashlib.Ctx[[32]byte]
ctx hashutil.Hasher[[32]byte]
onRamp *evm_2_evm_onramp.EVM2EVMOnRamp
}

func NewLeafHasher(sourceChainSelector uint64, destChainSelector uint64, onRampId common.Address, ctx hashlib.Ctx[[32]byte], onRamp *evm_2_evm_onramp.EVM2EVMOnRamp) *LeafHasher {
func NewLeafHasher(sourceChainSelector uint64, destChainSelector uint64, onRampId common.Address, ctx hashutil.Hasher[[32]byte], onRamp *evm_2_evm_onramp.EVM2EVMOnRamp) *LeafHasher {
return &LeafHasher{
metaDataHash: v1_0_0.GetMetaDataHash(ctx, ctx.Hash([]byte(MetaDataHashPrefix)), sourceChainSelector, onRampId, destChainSelector),
ctx: ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-common/pkg/hashutil"

"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/ccip/generated/evm_2_evm_onramp"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/abihelpers"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/pkg/hashlib"
)

func TestHasherV1_4_0(t *testing.T) {
sourceChainSelector, destChainSelector := uint64(1), uint64(4)
onRampAddress := common.HexToAddress("0x5550000000000000000000000000000000000001")
onRampABI := abihelpers.MustParseABI(evm_2_evm_onramp.EVM2EVMOnRampABI)

hashingCtx := hashlib.NewKeccakCtx()
hashingCtx := hashutil.NewKeccak()
ramp, err := evm_2_evm_onramp.NewEVM2EVMOnRamp(onRampAddress, nil)
require.NoError(t, err)
hasher := NewLeafHasher(sourceChainSelector, destChainSelector, onRampAddress, hashingCtx, ramp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"

"github.com/smartcontractkit/chainlink-common/pkg/hashutil"
cciptypes "github.com/smartcontractkit/chainlink-common/pkg/types/ccip"

"github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
Expand All @@ -23,7 +24,6 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipcommon"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/ccipdata"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/internal/logpollerutil"
"github.com/smartcontractkit/chainlink/v2/core/services/ocr2/plugins/ccip/pkg/hashlib"
)

var (
Expand Down Expand Up @@ -106,7 +106,7 @@ func NewOnRamp(lggr logger.Logger, sourceSelector, destSelector uint64, onRampAd
client: source,
destChainSelectorBytes: ccipcommon.SelectorToBytes(destSelector),
lp: sourceLP,
leafHasher: NewLeafHasher(sourceSelector, destSelector, onRampAddress, hashlib.NewKeccakCtx(), onRamp),
leafHasher: NewLeafHasher(sourceSelector, destSelector, onRampAddress, hashutil.NewKeccak(), onRamp),
onRamp: onRamp,
filters: filters,
address: onRampAddress,
Expand Down
Loading

0 comments on commit 7f834cc

Please sign in to comment.