Skip to content

Commit

Permalink
Extract core keystore from evm (#12380)
Browse files Browse the repository at this point in the history
* Extract core keystore from evm

* Migrate keystore mocks

* Fixes

* Remove pg package from keystore
  • Loading branch information
dimriou authored Mar 15, 2024
1 parent 9da6c2f commit 4cb56fd
Show file tree
Hide file tree
Showing 79 changed files with 584 additions and 419 deletions.
9 changes: 4 additions & 5 deletions core/capabilities/targets/write_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,20 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/capabilities/targets"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/txmgr"
txmmocks "github.com/smartcontractkit/chainlink/v2/core/chains/evm/txmgr/mocks"
evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
evmmocks "github.com/smartcontractkit/chainlink/v2/core/chains/legacyevm/mocks"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/keystone/generated/forwarder"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ethkey"

"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

var forwardABI = evmtypes.MustGetABI(forwarder.KeystoneForwarderMetaData.ABI)
var forwardABI = types.MustGetABI(forwarder.KeystoneForwarderMetaData.ABI)

func TestEvmWrite(t *testing.T) {
chain := evmmocks.NewChain(t)
Expand All @@ -34,12 +33,12 @@ func TestEvmWrite(t *testing.T) {

cfg := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) {
a := testutils.NewAddress()
addr, err := ethkey.NewEIP55Address(a.Hex())
addr, err := types.NewEIP55Address(a.Hex())
require.NoError(t, err)
c.EVM[0].ChainWriter.FromAddress = &addr

forwarderA := testutils.NewAddress()
forwarderAddr, err := ethkey.NewEIP55Address(forwarderA.Hex())
forwarderAddr, err := types.NewEIP55Address(forwarderA.Hex())
require.NoError(t, err)
c.EVM[0].ChainWriter.ForwarderAddress = &forwarderAddr
})
Expand Down
6 changes: 3 additions & 3 deletions core/chains/evm/config/chain_scoped_chain_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package config

import (
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/config/toml"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ethkey"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
)

type chainWriterConfig struct {
c toml.ChainWriter
}

func (b *chainWriterConfig) FromAddress() *ethkey.EIP55Address {
func (b *chainWriterConfig) FromAddress() *types.EIP55Address {
return b.c.FromAddress
}

func (b *chainWriterConfig) ForwarderAddress() *ethkey.EIP55Address {
func (b *chainWriterConfig) ForwarderAddress() *types.EIP55Address {
return b.c.ForwarderAddress
}
6 changes: 3 additions & 3 deletions core/chains/evm/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

commonconfig "github.com/smartcontractkit/chainlink/v2/common/config"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
"github.com/smartcontractkit/chainlink/v2/core/config"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ethkey"
)

type EVM interface {
Expand Down Expand Up @@ -130,8 +130,8 @@ type BlockHistory interface {
}

type ChainWriter interface {
FromAddress() *ethkey.EIP55Address
ForwarderAddress() *ethkey.EIP55Address
FromAddress() *types.EIP55Address
ForwarderAddress() *types.EIP55Address
}

type NodePool interface {
Expand Down
18 changes: 9 additions & 9 deletions core/chains/evm/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import (
commonconfig "github.com/smartcontractkit/chainlink/v2/common/config"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/config/toml"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
ubig "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big"
"github.com/smartcontractkit/chainlink/v2/core/config"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ethkey"
)

func TestChainScopedConfig(t *testing.T) {
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestChainScopedConfig(t *testing.T) {
gcfg2 := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) {
overrides(c, s)
c.EVM[0].KeySpecific = toml.KeySpecificConfig{
{Key: ptr(ethkey.EIP55AddressFromAddress(randomOtherAddr)),
{Key: ptr(types.EIP55AddressFromAddress(randomOtherAddr)),
GasEstimator: toml.KeySpecificGasEstimator{
PriceMax: assets.GWei(850),
},
Expand Down Expand Up @@ -124,7 +124,7 @@ func TestChainScopedConfig(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
gcfg3 := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) {
c.EVM[0].KeySpecific = toml.KeySpecificConfig{
{Key: ptr(ethkey.EIP55AddressFromAddress(addr)),
{Key: ptr(types.EIP55AddressFromAddress(addr)),
GasEstimator: toml.KeySpecificGasEstimator{
PriceMax: tt.val,
},
Expand All @@ -143,7 +143,7 @@ func TestChainScopedConfig(t *testing.T) {
gcfg3 := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) {
c.EVM[0].GasEstimator.PriceMax = chainSpecificPrice
c.EVM[0].KeySpecific = toml.KeySpecificConfig{
{Key: ptr(ethkey.EIP55AddressFromAddress(addr)),
{Key: ptr(types.EIP55AddressFromAddress(addr)),
GasEstimator: toml.KeySpecificGasEstimator{
PriceMax: keySpecificPrice,
},
Expand All @@ -160,7 +160,7 @@ func TestChainScopedConfig(t *testing.T) {
gcfg3 := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) {
c.EVM[0].GasEstimator.PriceMax = chainSpecificPrice
c.EVM[0].KeySpecific = toml.KeySpecificConfig{
{Key: ptr(ethkey.EIP55AddressFromAddress(addr)),
{Key: ptr(types.EIP55AddressFromAddress(addr)),
GasEstimator: toml.KeySpecificGasEstimator{
PriceMax: keySpecificPrice,
},
Expand All @@ -175,7 +175,7 @@ func TestChainScopedConfig(t *testing.T) {
keySpecificPrice := assets.GWei(900)
gcfg3 := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) {
c.EVM[0].KeySpecific = toml.KeySpecificConfig{
{Key: ptr(ethkey.EIP55AddressFromAddress(addr)),
{Key: ptr(types.EIP55AddressFromAddress(addr)),
GasEstimator: toml.KeySpecificGasEstimator{
PriceMax: keySpecificPrice,
},
Expand All @@ -192,7 +192,7 @@ func TestChainScopedConfig(t *testing.T) {
gcfg3 := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) {
c.EVM[0].GasEstimator.PriceMax = chainSpecificPrice
c.EVM[0].KeySpecific = toml.KeySpecificConfig{
{Key: ptr(ethkey.EIP55AddressFromAddress(addr)),
{Key: ptr(types.EIP55AddressFromAddress(addr)),
GasEstimator: toml.KeySpecificGasEstimator{
PriceMax: keySpecificPrice,
},
Expand Down Expand Up @@ -224,7 +224,7 @@ func TestChainScopedConfig(t *testing.T) {
val := testutils.NewAddress()

gcfg3 := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) {
c.EVM[0].LinkContractAddress = ptr(ethkey.EIP55AddressFromAddress(val))
c.EVM[0].LinkContractAddress = ptr(types.EIP55AddressFromAddress(val))
})
cfg3 := evmtest.NewChainScopedConfig(t, gcfg3)

Expand All @@ -241,7 +241,7 @@ func TestChainScopedConfig(t *testing.T) {
val := testutils.NewAddress()

gcfg3 := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) {
c.EVM[0].OperatorFactoryAddress = ptr(ethkey.EIP55AddressFromAddress(val))
c.EVM[0].OperatorFactoryAddress = ptr(types.EIP55AddressFromAddress(val))
})
cfg3 := evmtest.NewChainScopedConfig(t, gcfg3)

Expand Down
13 changes: 6 additions & 7 deletions core/chains/evm/config/toml/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore/keys/ethkey"
)

type HasEVMConfigs interface {
Expand Down Expand Up @@ -347,8 +346,8 @@ type Chain struct {
ChainType *string
FinalityDepth *uint32
FinalityTagEnabled *bool
FlagsContractAddress *ethkey.EIP55Address
LinkContractAddress *ethkey.EIP55Address
FlagsContractAddress *types.EIP55Address
LinkContractAddress *types.EIP55Address
LogBackfillBatchSize *uint32
LogPollInterval *commonconfig.Duration
LogKeepBlocksDepth *uint32
Expand All @@ -358,7 +357,7 @@ type Chain struct {
MinContractPayment *commonassets.Link
NonceAutoSync *bool
NoNewHeadsThreshold *commonconfig.Duration
OperatorFactoryAddress *ethkey.EIP55Address
OperatorFactoryAddress *types.EIP55Address
RPCDefaultBatchSize *uint32
RPCBlockQueryDelay *uint16

Expand Down Expand Up @@ -451,8 +450,8 @@ func (a *Automation) setFrom(f *Automation) {
}

type ChainWriter struct {
FromAddress *ethkey.EIP55Address `toml:",omitempty"`
ForwarderAddress *ethkey.EIP55Address `toml:",omitempty"`
FromAddress *types.EIP55Address `toml:",omitempty"`
ForwarderAddress *types.EIP55Address `toml:",omitempty"`
}

func (m *ChainWriter) setFrom(f *ChainWriter) {
Expand Down Expand Up @@ -668,7 +667,7 @@ func (ks KeySpecificConfig) ValidateConfig() (err error) {
}

type KeySpecific struct {
Key *ethkey.EIP55Address
Key *types.EIP55Address
GasEstimator KeySpecificGasEstimator `toml:",omitempty"`
}

Expand Down
19 changes: 19 additions & 0 deletions core/chains/evm/keystore/eth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package keystore

import (
"context"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)

// Eth is the external interface for EthKeyStore
//
//go:generate mockery --quiet --name Eth --output mocks/ --case=underscore
type Eth interface {
CheckEnabled(ctx context.Context, address common.Address, chainID *big.Int) error
EnabledAddressesForChain(ctx context.Context, chainID *big.Int) (addresses []common.Address, err error)
SignTx(ctx context.Context, fromAddress common.Address, tx *types.Transaction, chainID *big.Int) (*types.Transaction, error)
SubscribeToKeyChanges(ctx context.Context) (ch chan struct{}, unsub func())
}
143 changes: 143 additions & 0 deletions core/chains/evm/keystore/mocks/eth.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion core/chains/evm/monitor/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client"
httypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/headtracker/types"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/keystore"
evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore"
)

//go:generate mockery --quiet --name BalanceMonitor --output ../mocks/ --case=underscore
Expand Down
Loading

0 comments on commit 4cb56fd

Please sign in to comment.