Skip to content

Commit

Permalink
Add final result task to pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
spooktheducks committed Oct 16, 2020
1 parent 3c60043 commit 382ea8e
Show file tree
Hide file tree
Showing 57 changed files with 1,089 additions and 1,007 deletions.
14 changes: 13 additions & 1 deletion core/cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/smartcontractkit/chainlink/core/logger"
"github.com/smartcontractkit/chainlink/core/services/chainlink"
"github.com/smartcontractkit/chainlink/core/services/eth"
"github.com/smartcontractkit/chainlink/core/store"
"github.com/smartcontractkit/chainlink/core/store/models"
"github.com/smartcontractkit/chainlink/core/store/orm"
Expand Down Expand Up @@ -65,7 +66,18 @@ type ChainlinkAppFactory struct{}

// NewApplication returns a new instance of the node with the given config.
func (n ChainlinkAppFactory) NewApplication(config *orm.Config, onConnectCallbacks ...func(chainlink.Application)) chainlink.Application {
return chainlink.NewApplication(config, onConnectCallbacks...)
var ethClient eth.Client
if config.EthereumDisabled() {
logger.Info("ETH_DISABLED is set, using Null eth.Client")
ethClient = &eth.NullClient{}
} else {
var err error
ethClient, err = eth.NewClient(config.EthereumURL(), config.EthereumSecondaryURL())
if err != nil {
logger.Fatal(fmt.Sprintf("Unable to create ETH client: %+v", err))
}
}
return chainlink.NewApplication(config, ethClient, onConnectCallbacks...)
}

// Runner implements the Run method.
Expand Down
50 changes: 29 additions & 21 deletions core/cmd/local_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,14 +695,16 @@ func TestClient_P2P_CreateKey(t *testing.T) {

require.NoError(t, client.CreateP2PKey(c))

keys, err := app.GetStore().FindEncryptedP2PKeys()
keys, err := app.GetStore().OCRKeyStore.FindEncryptedP2PKeys()
require.NoError(t, err)

require.Len(t, keys, 1)
// Created + fixture key
require.Len(t, keys, 2)

e := keys[0]
_, err = e.Decrypt(cltest.Password)
require.NoError(t, err)
for _, e := range keys {
_, err = e.Decrypt(cltest.Password)
require.NoError(t, err)
}
}

func TestClient_P2P_DeleteKey(t *testing.T) {
Expand All @@ -727,12 +729,13 @@ func TestClient_P2P_DeleteKey(t *testing.T) {
require.NoError(t, err)
encKey, err := key.ToEncryptedP2PKey("password")
require.NoError(t, err)
err = store.UpsertEncryptedP2PKey(&encKey)
err = store.OCRKeyStore.UpsertEncryptedP2PKey(&encKey)
require.NoError(t, err)

keys, err := store.FindEncryptedP2PKeys()
keys, err := store.OCRKeyStore.FindEncryptedP2PKeys()
require.NoError(t, err)
require.Len(t, keys, 1)
// Created + fixture key
require.Len(t, keys, 2)

strID := strconv.FormatInt(int64(encKey.ID), 10)
set := flag.NewFlagSet("test", 0)
Expand All @@ -742,9 +745,10 @@ func TestClient_P2P_DeleteKey(t *testing.T) {
err = client.DeleteP2PKey(c)
require.NoError(t, err)

keys, err = store.FindEncryptedP2PKeys()
keys, err = store.OCRKeyStore.FindEncryptedP2PKeys()
require.NoError(t, err)
require.Len(t, keys, 0)
// fixture key only
require.Len(t, keys, 1)
}

func TestClient_CreateOCRKeyBundle(t *testing.T) {
Expand All @@ -771,14 +775,16 @@ func TestClient_CreateOCRKeyBundle(t *testing.T) {

require.NoError(t, client.CreateOCRKeyBundle(c))

keys, err := app.GetStore().FindEncryptedOCRKeyBundles()
keys, err := app.GetStore().OCRKeyStore.FindEncryptedOCRKeyBundles()
require.NoError(t, err)

require.Len(t, keys, 1)
// Created key + fixture key
require.Len(t, keys, 2)

e := keys[0]
_, err = e.Decrypt(cltest.Password)
require.NoError(t, err)
for _, e := range keys {
_, err = e.Decrypt(cltest.Password)
require.NoError(t, err)
}
}

func TestClient_DeleteOCRKeyBundle(t *testing.T) {
Expand All @@ -803,21 +809,23 @@ func TestClient_DeleteOCRKeyBundle(t *testing.T) {
require.NoError(t, err)
encKey, err := key.Encrypt("password")
require.NoError(t, err)
err = store.CreateEncryptedOCRKeyBundle(encKey)
err = store.OCRKeyStore.CreateEncryptedOCRKeyBundle(encKey)
require.NoError(t, err)

keys, err := store.FindEncryptedOCRKeyBundles()
keys, err := store.OCRKeyStore.FindEncryptedOCRKeyBundles()
require.NoError(t, err)
require.Len(t, keys, 1)
// Created key + fixture key
require.Len(t, keys, 2)

set := flag.NewFlagSet("test", 0)
set.Parse([]string{key.ID})
set.Parse([]string{key.ID.String()})
c := cli.NewContext(nil, set, nil)

err = client.DeleteOCRKeyBundle(c)
require.NoError(t, err)

keys, err = store.FindEncryptedOCRKeyBundles()
keys, err = store.OCRKeyStore.FindEncryptedOCRKeyBundles()
require.NoError(t, err)
require.Len(t, keys, 0)
// Only fixture key remains
require.Len(t, keys, 1)
}
56 changes: 44 additions & 12 deletions core/internal/cltest/cltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@ import (
"testing"
"time"

p2ppeer "github.com/libp2p/go-libp2p-core/peer"
"github.com/smartcontractkit/chainlink/core/assets"
"github.com/smartcontractkit/chainlink/core/auth"
"github.com/smartcontractkit/chainlink/core/cmd"
"github.com/smartcontractkit/chainlink/core/gracefulpanic"
"github.com/smartcontractkit/chainlink/core/internal/mocks"
"github.com/smartcontractkit/chainlink/core/logger"
"github.com/smartcontractkit/chainlink/core/services/chainlink"
"github.com/smartcontractkit/chainlink/core/services/eth"
strpkg "github.com/smartcontractkit/chainlink/core/store"
"github.com/smartcontractkit/chainlink/core/store/models"
"github.com/smartcontractkit/chainlink/core/store/models/ocrkey"
"github.com/smartcontractkit/chainlink/core/store/models/p2pkey"
"github.com/smartcontractkit/chainlink/core/store/orm"
"github.com/smartcontractkit/chainlink/core/store/presenters"
"github.com/smartcontractkit/chainlink/core/utils"
Expand All @@ -47,6 +49,7 @@ import (
"github.com/gorilla/sessions"
"github.com/gorilla/websocket"
"github.com/jinzhu/gorm"
cryptop2p "github.com/libp2p/go-libp2p-core/crypto"
"github.com/manyminds/api2go/jsonapi"
"github.com/onsi/gomega"
"github.com/stretchr/testify/assert"
Expand All @@ -71,10 +74,6 @@ const (
SessionSecret = "clsession_test_secret"
// DefaultKey is the address of the fixture key
DefaultKey = "0x3cb8e3FD9d27e39a5e9e6852b0e96160061fd4ea"
// DefaultPeerID is the peer ID of the fixture p2p key
DefaultPeerID = "12D3KooWGKbY6ymznHbQtqYq28Bdk9eifkxskuYoG4bHgccRoLvc"
// DefaultOCRKeyBundleID is the ID of the fixture ocr key bundle
DefaultOCRKeyBundleID = "1119b1811c471df8738ecaace1224eeaf2fecaf82edb1fa345e3ddc3f8882e80"
// AllowUnstarted enable an application that can be used in tests without being started
AllowUnstarted = "allow_unstarted"
)
Expand All @@ -83,10 +82,17 @@ var (
// DefaultKeyAddress is the address of the fixture key
DefaultKeyAddress = common.HexToAddress(DefaultKey)
DefaultKeyAddressEIP55 models.EIP55Address

// DefaultP2PPeerID is the fixture p2p key
DefaultP2PKey *p2pkey.Key
// DefaultP2PPeerID is the peer ID of the fixture p2p key
DefaultP2PPeerID p2ppeer.ID
// DefaultOCRKeyBundleIDSha256 is the ID of the fixture ocr key bundle
DefaultOCRKeyBundleIDSha256 models.Sha256Hash
DefaultP2PPeerID models.PeerID
// DefaultP2PPeerID is the fixture p2p key, encrypted with `cltest.Password`
DefaultEncryptedP2PKey *p2pkey.EncryptedP2PKey
// DefaultOCRKeyBundleIDSha256 is the fixture ocr key bundle
DefaultOCRKeyBundle *ocrkey.KeyBundle
// DefaultOCRKeyBundleIDSha256 is the fixture ocr key bundle, encrypted with `cltest.Password`
DefaultEncryptedOCRKeyBundle *ocrkey.EncryptedKeyBundle
)

var storeCounter uint64
Expand Down Expand Up @@ -126,18 +132,36 @@ func init() {
logger.Debugf("Using seed: %v", seed)
rand.Seed(seed)

DefaultP2PPeerID, err = p2ppeer.Decode(DefaultPeerID)
p2pPrivkey, _, err := cryptop2p.GenerateEd25519Key(bytes.NewBufferString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"))
if err != nil {
panic(err)
}
DefaultP2PKey = &p2pkey.Key{p2pPrivkey}
DefaultP2PPeerID, err = DefaultP2PKey.GetPeerID()
if err != nil {
panic(err)
}
DefaultOCRKeyBundleIDSha256, err = models.Sha256HashFromHex(DefaultOCRKeyBundleID)
encp2pkey, err := DefaultP2PKey.ToEncryptedP2PKey(Password)
if err != nil {
panic(err)
}
DefaultEncryptedP2PKey = &encp2pkey
DefaultOCRKeyBundle, err = ocrkey.NewKeyBundleFrom(
bytes.NewBufferString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"),
bytes.NewBufferString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"),
bytes.NewBufferString("deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"),
)
if err != nil {
panic(err)
}
DefaultKeyAddressEIP55, err = models.NewEIP55Address(DefaultKey)
if err != nil {
panic(err)
}
DefaultEncryptedOCRKeyBundle, err = DefaultOCRKeyBundle.Encrypt(Password)
if err != nil {
panic(err)
}
}

func logLevelFromEnv() zapcore.Level {
Expand Down Expand Up @@ -314,8 +338,16 @@ func NewApplicationWithConfigAndKey(t testing.TB, tc *TestConfig, flagsAndDeps .
func NewApplicationWithConfig(t testing.TB, tc *TestConfig, flagsAndDeps ...interface{}) (*TestApplication, func()) {
t.Helper()

var ethClient eth.Client = &eth.NullClient{}
for _, flag := range flagsAndDeps {
switch dep := flag.(type) {
case eth.Client:
ethClient = dep
}
}

ta := &TestApplication{t: t, connectedChannel: make(chan struct{}, 1)}
app := chainlink.NewApplication(tc.Config, func(app chainlink.Application) {
app := chainlink.NewApplication(tc.Config, ethClient, func(app chainlink.Application) {
ta.connectedChannel <- struct{}{}
}).(*chainlink.ChainlinkApplication)
ta.ChainlinkApplication = app
Expand Down Expand Up @@ -511,7 +543,7 @@ func (ta *TestApplication) MustCreateJobRun(txHashBytes []byte, blockHashBytes [

// NewStoreWithConfig creates a new store with given config
func NewStoreWithConfig(config *TestConfig) (*strpkg.Store, func()) {
s := strpkg.NewInsecureStore(config.Config, gracefulpanic.NewSignal())
s := strpkg.NewInsecureStore(config.Config, &eth.NullClient{}, gracefulpanic.NewSignal())
return s, func() {
cleanUpStore(config.t, s)
}
Expand Down
58 changes: 55 additions & 3 deletions core/internal/cltest/factories.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import (
"github.com/smartcontractkit/chainlink/core/internal/mocks"
"github.com/smartcontractkit/chainlink/core/logger"
"github.com/smartcontractkit/chainlink/core/services/fluxmonitor"
"github.com/smartcontractkit/chainlink/core/services/offchainreporting"
strpkg "github.com/smartcontractkit/chainlink/core/store"
"github.com/smartcontractkit/chainlink/core/store/models"
"github.com/smartcontractkit/chainlink/core/store/models/ocrkey"
"github.com/smartcontractkit/chainlink/core/store/models/p2pkey"
"github.com/smartcontractkit/chainlink/core/utils"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -820,15 +823,58 @@ func MustInsertRandomKey(t *testing.T, store *strpkg.Store) models.Key {
return k
}

func MustInsertOffchainreportingOracleSpec(t *testing.T, store *strpkg.Store) models.OffchainReportingOracleSpec {
func MustInsertOffchainreportingOracleSpec(t *testing.T, store *strpkg.Store, dependencies ...interface{}) models.OffchainReportingOracleSpec {
t.Helper()

// Insert keys into the store
keystore := offchainreporting.NewKeyStore(store.DB)

var peerID models.PeerID
var p2pKey *p2pkey.EncryptedP2PKey
var ocrKey *ocrkey.EncryptedKeyBundle
for _, dep := range dependencies {
switch d := dep.(type) {
case *p2pkey.EncryptedP2PKey:
p2pKey = d
case p2pkey.EncryptedP2PKey:
p2pKey = &d
case *ocrkey.EncryptedKeyBundle:
ocrKey = d
case ocrkey.EncryptedKeyBundle:
ocrKey = &d
default:
t.Fatalf("cltest.MustInsertOffchainreportingOracleSpec does not accept a %T as an injected dependency", dep)
}
}
if p2pKey == nil {
dk, p2pKey_, err := keystore.GenerateEncryptedP2PKey(Password)
require.NoError(t, err)
p2pKey = &p2pKey_
peerID, err = dk.GetPeerID()
require.NoError(t, err)
} else {
err := keystore.UpsertEncryptedP2PKey(p2pKey)
require.NoError(t, err)
dk, err := p2pKey.Decrypt(Password)
require.NoError(t, err)
peerID, err = dk.GetPeerID()
require.NoError(t, err)
}
if ocrKey == nil {
_, ocrKey_, err := keystore.GenerateEncryptedOCRKeyBundle(Password)
require.NoError(t, err)
ocrKey = &ocrKey_
} else {
err := keystore.CreateEncryptedOCRKeyBundle(ocrKey)
require.NoError(t, err)
}

spec := models.OffchainReportingOracleSpec{
ContractAddress: NewEIP55Address(),
P2PPeerID: models.PeerID(DefaultP2PPeerID),
P2PPeerID: peerID,
P2PBootstrapPeers: []string{},
IsBootstrapPeer: false,
EncryptedOCRKeyBundleID: DefaultOCRKeyBundleIDSha256,
EncryptedOCRKeyBundleID: ocrKey.ID,
TransmitterAddress: DefaultKeyAddressEIP55,
ObservationTimeout: 0,
BlockchainTimeout: 0,
Expand All @@ -839,3 +885,9 @@ func MustInsertOffchainreportingOracleSpec(t *testing.T, store *strpkg.Store) mo
require.NoError(t, store.DB.Create(&spec).Error)
return spec
}

func MustInsertJobSpec(t *testing.T, s *strpkg.Store) models.JobSpec {
j := NewJob()
require.NoError(t, s.CreateJob(&j))
return j
}
2 changes: 2 additions & 0 deletions core/internal/cltest/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const EthMockRegisterGetBalance = "eth_mock_register_get_balance"
const EthMockRegisterGetBlockByNumber = "eth_mock_register_get_block_by_number"

// MockEthOnStore given store return new EthMock Client
// TODO(sam): Remove this function entirely and pass in eth client via dependency injection in NewApplication
// See: https://www.pivotaltracker.com/story/show/171753295
func MockEthOnStore(t testing.TB, s *store.Store, flagsAndDependencies ...interface{}) *EthMock {
mock := &EthMock{
t: t,
Expand Down
Loading

0 comments on commit 382ea8e

Please sign in to comment.