Skip to content

Commit

Permalink
core/internal/testutils: use github.com/hashicorp/consul/sdk/freeport (
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 authored Oct 10, 2023
1 parent 5efd686 commit 9fe119d
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 84 deletions.
32 changes: 18 additions & 14 deletions core/internal/features/features_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/ethereum/go-ethereum/rpc"
"github.com/google/uuid"
"github.com/hashicorp/consul/sdk/freeport"
"github.com/onsi/gomega"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -670,7 +671,7 @@ func setupOCRContracts(t *testing.T) (*bind.TransactOpts, *backends.SimulatedBac
return owner, b, ocrContractAddress, ocrContract, flagsContract, flagsContractAddress
}

func setupNode(t *testing.T, owner *bind.TransactOpts, portV1, portV2 uint16, dbName string,
func setupNode(t *testing.T, owner *bind.TransactOpts, portV1, portV2 int, dbName string,
b *backends.SimulatedBackend, ns ocrnetworking.NetworkingStack, overrides func(c *chainlink.Config, s *chainlink.Secrets),
) (*cltest.TestApplication, string, common.Address, ocrkey.KeyV2) {
p2pKey, err := p2pkey.NewV2()
Expand Down Expand Up @@ -743,7 +744,7 @@ func setupForwarderEnabledNode(
t *testing.T,
owner *bind.TransactOpts,
portV1,
portV2 uint16,
portV2 int,
dbName string,
b *backends.SimulatedBackend,
ns ocrnetworking.NetworkingStack,
Expand Down Expand Up @@ -856,8 +857,9 @@ func TestIntegration_OCR(t *testing.T) {
for _, tt := range tests {
test := tt
t.Run(test.name, func(t *testing.T) {
bootstrapNodePortV1 := testutils.GetFreePort(t)
bootstrapNodePortV2 := testutils.GetFreePort(t)
t.Parallel()
bootstrapNodePortV1 := freeport.GetOne(t)
bootstrapNodePortV2 := freeport.GetOne(t)
g := gomega.NewWithT(t)
owner, b, ocrContractAddress, ocrContract, flagsContract, flagsContractAddress := setupOCRContracts(t)

Expand All @@ -870,9 +872,10 @@ func TestIntegration_OCR(t *testing.T) {
keys []ocrkey.KeyV2
apps []*cltest.TestApplication
)
ports := freeport.GetN(t, 2*numOracles)
for i := 0; i < numOracles; i++ {
portV1 := testutils.GetFreePort(t)
portV2 := testutils.GetFreePort(t)
portV1 := ports[2*i]
portV2 := ports[2*i+1]
app, peerID, transmitter, key := setupNode(t, owner, portV1, portV2, fmt.Sprintf("o%d_%d", i, test.id), b, test.ns, func(c *chainlink.Config, s *chainlink.Secrets) {
c.EVM[0].FlagsContractAddress = ptr(ethkey.EIP55AddressFromAddress(flagsContractAddress))
c.EVM[0].GasEstimator.EIP1559DynamicFees = ptr(test.eip1559)
Expand All @@ -891,10 +894,10 @@ func TestIntegration_OCR(t *testing.T) {
OracleIdentity: confighelper.OracleIdentity{
OnChainSigningAddress: ocrtypes.OnChainSigningAddress(key.OnChainSigning.Address()),
TransmitAddress: transmitter,
OffchainPublicKey: ocrtypes.OffchainPublicKey(key.PublicKeyOffChain()),
OffchainPublicKey: key.PublicKeyOffChain(),
PeerID: peerID,
},
SharedSecretEncryptionPublicKey: ocrtypes.SharedSecretEncryptionPublicKey(key.PublicKeyConfig()),
SharedSecretEncryptionPublicKey: key.PublicKeyConfig(),
})
}

Expand Down Expand Up @@ -1080,8 +1083,8 @@ func TestIntegration_OCR_ForwarderFlow(t *testing.T) {
t.Parallel()
numOracles := 4
t.Run("ocr_forwarder_flow", func(t *testing.T) {
bootstrapNodePortV1 := testutils.GetFreePort(t)
bootstrapNodePortV2 := testutils.GetFreePort(t)
bootstrapNodePortV1 := freeport.GetOne(t)
bootstrapNodePortV2 := freeport.GetOne(t)
g := gomega.NewWithT(t)
owner, b, ocrContractAddress, ocrContract, flagsContract, flagsContractAddress := setupOCRContracts(t)

Expand All @@ -1096,9 +1099,10 @@ func TestIntegration_OCR_ForwarderFlow(t *testing.T) {
keys []ocrkey.KeyV2
apps []*cltest.TestApplication
)
ports := freeport.GetN(t, 2*numOracles)
for i := 0; i < numOracles; i++ {
portV1 := testutils.GetFreePort(t)
portV2 := testutils.GetFreePort(t)
portV1 := ports[2*i]
portV2 := ports[2*i+1]
app, peerID, transmitter, forwarder, key := setupForwarderEnabledNode(t, owner, portV1, portV2, fmt.Sprintf("o%d_%d", i, 1), b, ocrnetworking.NetworkingStackV2, func(c *chainlink.Config, s *chainlink.Secrets) {
c.Feature.LogPoller = ptr(true)
c.EVM[0].FlagsContractAddress = ptr(ethkey.EIP55AddressFromAddress(flagsContractAddress))
Expand All @@ -1117,10 +1121,10 @@ func TestIntegration_OCR_ForwarderFlow(t *testing.T) {
OracleIdentity: confighelper.OracleIdentity{
OnChainSigningAddress: ocrtypes.OnChainSigningAddress(key.OnChainSigning.Address()),
TransmitAddress: forwarder,
OffchainPublicKey: ocrtypes.OffchainPublicKey(key.PublicKeyOffChain()),
OffchainPublicKey: key.PublicKeyOffChain(),
PeerID: peerID,
},
SharedSecretEncryptionPublicKey: ocrtypes.SharedSecretEncryptionPublicKey(key.PublicKeyConfig()),
SharedSecretEncryptionPublicKey: key.PublicKeyConfig(),
})
}

Expand Down
15 changes: 9 additions & 6 deletions core/internal/features/ocr2/features_ocr2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/hashicorp/consul/sdk/freeport"
"github.com/onsi/gomega"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -103,7 +104,7 @@ func setupOCR2Contracts(t *testing.T) (*bind.TransactOpts, *backends.SimulatedBa
func setupNodeOCR2(
t *testing.T,
owner *bind.TransactOpts,
port uint16,
port int,
dbName string,
useForwarder bool,
b *backends.SimulatedBackend,
Expand Down Expand Up @@ -192,7 +193,7 @@ func TestIntegration_OCR2(t *testing.T) {
owner, b, ocrContractAddress, ocrContract := setupOCR2Contracts(t)

lggr := logger.TestLogger(t)
bootstrapNodePort := testutils.GetFreePort(t)
bootstrapNodePort := freeport.GetOne(t)
bootstrapNode := setupNodeOCR2(t, owner, bootstrapNodePort, "bootstrap", false /* useForwarders */, b, nil)

var (
Expand All @@ -201,8 +202,9 @@ func TestIntegration_OCR2(t *testing.T) {
kbs []ocr2key.KeyBundle
apps []*cltest.TestApplication
)
for i := uint16(0); i < 4; i++ {
node := setupNodeOCR2(t, owner, bootstrapNodePort+1+i, fmt.Sprintf("oracle%d", i), false /* useForwarders */, b, []commontypes.BootstrapperLocator{
ports := freeport.GetN(t, 4)
for i := 0; i < 4; i++ {
node := setupNodeOCR2(t, owner, ports[i], fmt.Sprintf("oracle%d", i), false /* useForwarders */, b, []commontypes.BootstrapperLocator{
// Supply the bootstrap IP and port as a V2 peer address
{PeerID: bootstrapNode.peerID, Addrs: []string{fmt.Sprintf("127.0.0.1:%d", bootstrapNodePort)}},
})
Expand Down Expand Up @@ -461,7 +463,7 @@ func TestIntegration_OCR2_ForwarderFlow(t *testing.T) {
owner, b, ocrContractAddress, ocrContract := setupOCR2Contracts(t)

lggr := logger.TestLogger(t)
bootstrapNodePort := testutils.GetFreePort(t)
bootstrapNodePort := freeport.GetOne(t)
bootstrapNode := setupNodeOCR2(t, owner, bootstrapNodePort, "bootstrap", true /* useForwarders */, b, nil)

var (
Expand All @@ -471,8 +473,9 @@ func TestIntegration_OCR2_ForwarderFlow(t *testing.T) {
kbs []ocr2key.KeyBundle
apps []*cltest.TestApplication
)
ports := freeport.GetN(t, 4)
for i := uint16(0); i < 4; i++ {
node := setupNodeOCR2(t, owner, bootstrapNodePort+1+i, fmt.Sprintf("oracle%d", i), true /* useForwarders */, b, []commontypes.BootstrapperLocator{
node := setupNodeOCR2(t, owner, ports[i], fmt.Sprintf("oracle%d", i), true /* useForwarders */, b, []commontypes.BootstrapperLocator{
// Supply the bootstrap IP and port as a V2 peer address
{PeerID: bootstrapNode.peerID, Addrs: []string{fmt.Sprintf("127.0.0.1:%d", bootstrapNodePort)}},
})
Expand Down
14 changes: 0 additions & 14 deletions core/internal/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"math"
"math/big"
mrand "math/rand"
"net"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -452,16 +451,3 @@ func MustDecodeBase64(s string) (b []byte) {
}
return
}

// GetFreePort returns a free port.
// NOTE: This approach is technically incorrect because the returned port
// can still be taken by the time the caller attempts to bind to it.
// Unfortunately, we can't specify zero port in P2P.V2.ListenAddresses at the moment.
func GetFreePort(t *testing.T) uint16 {
addr, err := net.ResolveTCPAddr("tcp", "localhost:0")
require.NoError(t, err)
listener, err := net.ListenTCP("tcp", addr)
require.NoError(t, err)
require.NoError(t, listener.Close())
return uint16(listener.Addr().(*net.TCPAddr).Port)
}
2 changes: 2 additions & 0 deletions core/scripts/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,8 @@ github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfm
github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48=
github.com/hashicorp/consul/api v1.1.0/go.mod h1:VmuI/Lkw1nC05EYQWNKwWGbkg+FbDBtguAZLlVdkD9Q=
github.com/hashicorp/consul/sdk v0.1.1/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8=
github.com/hashicorp/consul/sdk v0.14.1 h1:ZiwE2bKb+zro68sWzZ1SgHF3kRMBZ94TwOCFRF4ylPs=
github.com/hashicorp/consul/sdk v0.14.1/go.mod h1:vFt03juSzocLRFo59NkeQHHmQa6+g7oU0pfzdI1mUhg=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/hashicorp/consul/sdk/freeport"
"github.com/onsi/gomega"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -228,7 +229,7 @@ type Node struct {
func StartNewNode(
t *testing.T,
owner *bind.TransactOpts,
port uint16,
port int,
dbName string,
b *backends.SimulatedBackend,
maxGas uint32,
Expand Down Expand Up @@ -469,11 +470,12 @@ func CreateFunctionsNodes(
require.Fail(t, "ocr2Keystores and thresholdKeyShares must have the same length")
}

bootstrapPort := testutils.GetFreePort(t)
bootstrapPort := freeport.GetOne(t)
bootstrapNode = StartNewNode(t, owner, bootstrapPort, "bootstrap", b, uint32(maxGas), nil, nil, "")
AddBootstrapJob(t, bootstrapNode.App, oracleContractAddress)

// oracle nodes with jobs, bridges and mock EAs
ports := freeport.GetN(t, nOracleNodes)
for i := 0; i < nOracleNodes; i++ {
var thresholdKeyShare string
if len(thresholdKeyShares) == 0 {
Expand All @@ -487,8 +489,7 @@ func CreateFunctionsNodes(
} else {
ocr2Keystore = ocr2Keystores[i]
}
nodePort := testutils.GetFreePort(t)
oracleNode := StartNewNode(t, owner, nodePort, fmt.Sprintf("oracle%d", i), b, uint32(maxGas), []commontypes.BootstrapperLocator{
oracleNode := StartNewNode(t, owner, ports[i], fmt.Sprintf("oracle%d", i), b, uint32(maxGas), []commontypes.BootstrapperLocator{
{PeerID: bootstrapNode.PeerID, Addrs: []string{fmt.Sprintf("127.0.0.1:%d", bootstrapPort)}},
}, ocr2Keystore, thresholdKeyShare)
oracleNodes = append(oracleNodes, oracleNode.App)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth/ethconfig"
"github.com/hashicorp/consul/sdk/freeport"
"github.com/onsi/gomega"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -300,7 +301,7 @@ type Node struct {
func StartNewNode(
t *testing.T,
owner *bind.TransactOpts,
port uint16,
port int,
dbName string,
b *backends.SimulatedBackend,
maxGas uint32,
Expand Down Expand Up @@ -549,11 +550,12 @@ func CreateFunctionsNodes(
require.Fail(t, "ocr2Keystores and thresholdKeyShares must have the same length")
}

bootstrapPort := testutils.GetFreePort(t)
bootstrapPort := freeport.GetOne(t)
bootstrapNode = StartNewNode(t, owner, bootstrapPort, "bootstrap", b, uint32(maxGas), nil, nil, "")
AddBootstrapJob(t, bootstrapNode.App, routerAddress)

// oracle nodes with jobs, bridges and mock EAs
ports := freeport.GetN(t, nOracleNodes)
for i := 0; i < nOracleNodes; i++ {
var thresholdKeyShare string
if len(thresholdKeyShares) == 0 {
Expand All @@ -567,8 +569,7 @@ func CreateFunctionsNodes(
} else {
ocr2Keystore = ocr2Keystores[i]
}
nodePort := testutils.GetFreePort(t)
oracleNode := StartNewNode(t, owner, nodePort, fmt.Sprintf("oracle%d", i), b, uint32(maxGas), []commontypes.BootstrapperLocator{
oracleNode := StartNewNode(t, owner, ports[i], fmt.Sprintf("oracle%d", i), b, uint32(maxGas), []commontypes.BootstrapperLocator{
{PeerID: bootstrapNode.PeerID, Addrs: []string{fmt.Sprintf("127.0.0.1:%d", bootstrapPort)}},
}, ocr2Keystore, thresholdKeyShare)
oracleNodes = append(oracleNodes, oracleNode.App)
Expand Down
19 changes: 10 additions & 9 deletions core/services/ocr2/plugins/mercury/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import (

"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
"github.com/ethereum/go-ethereum/common"
"github.com/smartcontractkit/wsrpc"
"github.com/smartcontractkit/wsrpc/credentials"
"github.com/smartcontractkit/wsrpc/peer"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"

"github.com/smartcontractkit/wsrpc"
"github.com/smartcontractkit/wsrpc/credentials"
"github.com/smartcontractkit/wsrpc/peer"

"github.com/smartcontractkit/libocr/offchainreporting2/chains/evmutil"
ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2plus/types"

Expand Down Expand Up @@ -150,14 +151,14 @@ func (node *Node) AddBootstrapJob(t *testing.T, spec string) {

func setupNode(
t *testing.T,
port int64,
port int,
dbName string,
backend *backends.SimulatedBackend,
csaKey csakey.KeyV2,
) (app chainlink.Application, peerID string, clientPubKey credentials.StaticSizedPublicKey, ocr2kb ocr2key.KeyBundle, observedLogs *observer.ObservedLogs) {
k := big.NewInt(port) // keys unique to port
k := big.NewInt(int64(port)) // keys unique to port
p2pKey := p2pkey.MustNewV2XXXTestingOnly(k)
rdr := keystest.NewRandReaderFromSeed(port)
rdr := keystest.NewRandReaderFromSeed(int64(port))
ocr2kb = ocr2key.MustNewInsecure(rdr, chaintype.EVM)

p2paddresses := []string{fmt.Sprintf("127.0.0.1:%d", port)}
Expand Down Expand Up @@ -241,7 +242,7 @@ func addV1MercuryJob(
i int,
verifierAddress common.Address,
bootstrapPeerID string,
bootstrapNodePort int64,
bootstrapNodePort int,
bmBridge,
bidBridge,
askBridge,
Expand Down Expand Up @@ -324,7 +325,7 @@ func addV2MercuryJob(
i int,
verifierAddress common.Address,
bootstrapPeerID string,
bootstrapNodePort int64,
bootstrapNodePort int,
bmBridge,
serverURL string,
serverPubKey,
Expand Down Expand Up @@ -389,7 +390,7 @@ func addV3MercuryJob(
i int,
verifierAddress common.Address,
bootstrapPeerID string,
bootstrapNodePort int64,
bootstrapNodePort int,
bmBridge,
bidBridge,
askBridge,
Expand Down
Loading

0 comments on commit 9fe119d

Please sign in to comment.