Skip to content

Commit

Permalink
update testutils to split the generate from solana-specific ones.
Browse files Browse the repository at this point in the history
  • Loading branch information
topliceanu committed Jan 26, 2022
1 parent 014cdd1 commit 693540f
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 31 deletions.
4 changes: 2 additions & 2 deletions pkg/monitoring/manager_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func BenchmarkManager(b *testing.B) {
log := logger.NewNullLogger()

cfg := config.Config{}
solanaCfg := SolanaConfig{}
chainCfg := generateChainConfig()
cfg.Feeds.URL = "http://some-fake-url-just-to-trigger-rdd-polling.com"
cfg.Feeds.RDDPollInterval = 1 * time.Second
cfg.Feeds.RDDReadTimeout = 1 * time.Second
Expand All @@ -46,7 +46,7 @@ func BenchmarkManager(b *testing.B) {
factory := NewRandomDataSourceFactory(ctx, wg, log)

monitor := NewMultiFeedMonitor(
solanaCfg,
chainCfg,

log,
factory,
Expand Down
2 changes: 1 addition & 1 deletion pkg/monitoring/multi_feed_monitor_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func BenchmarkMultichainMonitor(b *testing.B) {
defer cancel()

cfg := config.Config{}
chainCfg := SolanaConfig{}
chainCfg := generateChainConfig()
feeds := []FeedConfig{generateFeedConfig()}

transmissionSchema := fakeSchema{transmissionCodec}
Expand Down
16 changes: 7 additions & 9 deletions pkg/monitoring/multi_feed_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ func TestMultiFeedMonitorToMakeSureAllGoroutinesTerminate(t *testing.T) {
wg := &sync.WaitGroup{}

cfg := config.Config{}
solanaCfg := SolanaConfig{
PollInterval: 5 * time.Second,
}
feeds := []FeedConfig{}
chainCfg := fakeChainConfig{}
chainCfg.PollInterval = 5 * time.Second
feeds := make([]FeedConfig, numFeeds)
for i := 0; i < numFeeds; i++ {
feeds = append(feeds, generateFeedConfig())
feeds[i] = generateFeedConfig()
}

transmissionSchema := fakeSchema{transmissionCodec}
Expand All @@ -34,7 +33,7 @@ func TestMultiFeedMonitorToMakeSureAllGoroutinesTerminate(t *testing.T) {
factory := &fakeRandomDataSourceFactory{make(chan Envelope), ctx}

monitor := NewMultiFeedMonitor(
solanaCfg,
chainCfg,

logger.NewNullLogger(),
factory,
Expand Down Expand Up @@ -90,9 +89,8 @@ func TestMultiFeedMonitorForPerformance(t *testing.T) {
wg := &sync.WaitGroup{}

cfg := config.Config{}
chainCfg := SolanaConfig{
PollInterval: 5 * time.Second,
}
chainCfg := fakeChainConfig{}
chainCfg.PollInterval = 5 * time.Second
feeds := []FeedConfig{}
for i := 0; i < numFeeds; i++ {
feeds = append(feeds, generateFeedConfig())
Expand Down
24 changes: 24 additions & 0 deletions pkg/monitoring/solana_testutils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package monitoring

import (
"fmt"
"math/rand"
"time"

Expand All @@ -23,10 +24,33 @@ func generateSolanaConfig() SolanaConfig {
}
}

func generateSolanaFeedConfig() SolanaFeedConfig {
coins := []string{"btc", "eth", "matic", "link", "avax", "ftt", "srm", "usdc", "sol", "ray"}
coin := coins[rand.Intn(len(coins))]
contract, transmissions, state := generatePublicKey(), generatePublicKey(), generatePublicKey()
return SolanaFeedConfig{
Name: fmt.Sprintf("%s / usd", coin),
Path: fmt.Sprintf("%s-usd", coin),
Symbol: "$",
HeartbeatSec: 1,
ContractType: "ocr2",
ContractStatus: "status",

ContractAddressBase58: contract.String(),
TransmissionsAccountBase58: transmissions.String(),
StateAccountBase58: state.String(),

ContractAddress: contract,
TransmissionsAccount: transmissions,
StateAccount: state,
}
}

// This utilities are used primarely in tests but are present in the monitoring package because they are not inside a file ending in _test.go.
// This is done in order to expose NewRandomDataReader for use in cmd/monitoring.
// The following code is added to comply with the "unused" linter:
var (
_ = generateSolanaConfig()
_ = generatePublicKey()
_ = generateSolanaFeedConfig()
)
68 changes: 49 additions & 19 deletions pkg/monitoring/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package monitoring

import (
"context"
"encoding/base64"
"fmt"
"math/big"
"math/rand"
Expand Down Expand Up @@ -110,26 +111,55 @@ func generate32ByteArr() [32]byte {
return out
}

type fakeFeedConfig struct {
name string
path string
symbol string
heartbeatSec int64
contractType string
contractStatus string
// This functions as a feed identifier.
contractAddress []byte
}

func (f fakeFeedConfig) GetName() string { return f.name }
func (f fakeFeedConfig) GetPath() string { return f.path }
func (f fakeFeedConfig) GetSymbol() string { return f.symbol }
func (f fakeFeedConfig) GetHeartbeatSec() int64 { return f.heartbeatSec }
func (f fakeFeedConfig) GetContractType() string { return f.contractType }
func (f fakeFeedConfig) GetContractStatus() string { return f.contractStatus }
func (f fakeFeedConfig) GetContractAddress() string {
return base64.StdEncoding.EncodeToString(f.contractAddress)
}
func (f fakeFeedConfig) GetContractAddressBytes() []byte { return f.contractAddress }
func (f fakeFeedConfig) ToMapping() map[string]interface{} {
return map[string]interface{}{
"feed_name": f.name,
"feed_path": f.path,
"symbol": f.symbol,
"heartbeat_sec": int64(f.heartbeatSec),
"contract_type": f.contractType,
"contract_status": f.contractStatus,
"contract_address": f.contractAddress,
// These are solana specific but are kept here for backwards compatibility in Avro.
"transmissions_account": []byte{},
"state_account": []byte{},
}
}

func generateFeedConfig() FeedConfig {
coins := []string{"btc", "eth", "matic", "link", "avax", "ftt", "srm", "usdc", "sol", "ray"}
coin := coins[rand.Intn(len(coins))]
contract, transmissions, state := generatePublicKey(), generatePublicKey(), generatePublicKey()
return FeedConfig(SolanaFeedConfig{
Name: fmt.Sprintf("%s / usd", coin),
Path: fmt.Sprintf("%s-usd", coin),
Symbol: "$",
HeartbeatSec: 1,
ContractType: "ocr2",
ContractStatus: "status",

ContractAddressBase58: contract.String(),
TransmissionsAccountBase58: transmissions.String(),
StateAccountBase58: state.String(),

ContractAddress: contract,
TransmissionsAccount: transmissions,
StateAccount: state,
})
contractAddress := generate32ByteArr()
return fakeFeedConfig{
name: fmt.Sprintf("%s / usd", coin),
path: fmt.Sprintf("%s-usd", coin),
symbol: "$",
heartbeatSec: 1,
contractType: "ocr2",
contractStatus: "status",
contractAddress: contractAddress[:],
}
}

func generateNumericalMedianOffchainConfig() (*pb.NumericalMedianConfigProto, []byte, error) {
Expand Down Expand Up @@ -277,9 +307,9 @@ type fakeChainConfig struct {
func generateChainConfig() ChainConfig {
return fakeChainConfig{
RPCEndpoint: "http://some-chain-host:6666",
NetworkName: "solana-mainnet-beta",
NetworkName: "mainnet-beta",
NetworkID: "1",
ChainID: "solana-mainnet-beta",
ChainID: "mainnet-beta",
ReadTimeout: 100 * time.Millisecond,
PollInterval: time.Duration(1+rand.Intn(5)) * time.Second,
}
Expand Down

0 comments on commit 693540f

Please sign in to comment.