Skip to content

Commit

Permalink
Modify ocr actions (#193)
Browse files Browse the repository at this point in the history
* Modify ocr actions

* var
  • Loading branch information
gheorghestrimtu authored Jan 21, 2022
1 parent c251619 commit ffa9a24
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
32 changes: 25 additions & 7 deletions actions/ocr.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func DeployOCRContracts(
chainlinkNodes []client.Chainlink,
networks *client.Networks,
) []contracts.OffchainAggregator {
ocrInstances := []contracts.OffchainAggregator{}
var ocrInstances []contracts.OffchainAggregator
for i := 0; i < numberOfContracts; i++ {
ocrInstance, err := contractDeployer.DeployOffChainAggregator(
linkTokenContract.Address(),
Expand Down Expand Up @@ -78,15 +78,15 @@ func CreateOCRJobs(
Expect(err).ShouldNot(HaveOccurred(), "Shouldn't fail getting OCR keys from OCR node %d", nodeIndex+1)
nodeOCRKeyId := nodeOCRKeys.Data[0].ID

nodeContractPairID := buildNodeContractPairID(chainlinkNodes[nodeIndex], ocrInstance)
nodeContractPairID := BuildNodeContractPairID(chainlinkNodes[nodeIndex], ocrInstance)
Expect(err).ShouldNot(HaveOccurred())
bta := client.BridgeTypeAttributes{
Name: nodeContractPairID,
URL: fmt.Sprintf("%s/%s", mockserver.Config.ClusterURL, nodeContractPairID),
}

// This sets a default value for all node and ocr instances in order to avoid 404 issues
SetAllAdapterResponses(0, ocrInstances, chainlinkNodes, mockserver)
SetAllAdapterResponsesToTheSameValue(0, ocrInstances, chainlinkNodes, mockserver)

err = chainlinkNodes[nodeIndex].CreateBridge(&bta)
Expect(err).ShouldNot(HaveOccurred(), "Shouldn't fail creating bridge in OCR node %d", nodeIndex+1)
Expand Down Expand Up @@ -114,16 +114,16 @@ func SetAdapterResponse(
mockserver *client.MockserverClient,
) func() {
return func() {
nodeContractPairID := buildNodeContractPairID(chainlinkNode, ocrInstance)
nodeContractPairID := BuildNodeContractPairID(chainlinkNode, ocrInstance)
path := fmt.Sprintf("/%s", nodeContractPairID)
err := mockserver.SetValuePath(path, response)
Expect(err).ShouldNot(HaveOccurred(), "Setting mockserver value path shouldn't fail")
}
}

// SetAllAdapterResponses sets the mock responses in mockserver that are read by chainlink nodes
// SetAllAdapterResponsesToTheSameValue sets the mock responses in mockserver that are read by chainlink nodes
// to simulate different adapters. This sets all adapter responses for each node and contract to the same response
func SetAllAdapterResponses(
func SetAllAdapterResponsesToTheSameValue(
response int,
ocrInstances []contracts.OffchainAggregator,
chainlinkNodes []client.Chainlink,
Expand All @@ -138,6 +138,24 @@ func SetAllAdapterResponses(
}
}

// SetAllAdapterResponsesToDifferentValues sets the mock responses in mockserver that are read by chainlink nodes
// to simulate different adapters. This sets all adapter responses for each node and contract to different responses
func SetAllAdapterResponsesToDifferentValues(
responses []int,
ocrInstances []contracts.OffchainAggregator,
chainlinkNodes []client.Chainlink,
mockserver *client.MockserverClient,
) func() {
return func() {
Expect(len(responses)).Should(BeNumerically("==", len(chainlinkNodes[1:])))
for _, ocrInstance := range ocrInstances {
for nodeIndex := 1; nodeIndex < len(chainlinkNodes); nodeIndex++ {
SetAdapterResponse(responses[nodeIndex-1], ocrInstance, chainlinkNodes[nodeIndex], mockserver)()
}
}
}
}

// StartNewRound requests a new round from the ocr contract and waits for confirmation
func StartNewRound(
roundNr int64,
Expand All @@ -157,7 +175,7 @@ func StartNewRound(
}
}

func buildNodeContractPairID(node client.Chainlink, ocrInstance contracts.OffchainAggregator) string {
func BuildNodeContractPairID(node client.Chainlink, ocrInstance contracts.OffchainAggregator) string {
Expect(node).ShouldNot(BeNil())
Expect(ocrInstance).ShouldNot(BeNil())
nodeAddress, err := node.PrimaryEthAddress()
Expand Down
4 changes: 2 additions & 2 deletions suite/smoke/ocr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var _ = Describe("OCR Feed @ocr", func() {

Describe("With a single OCR contract", func() {
It("performs two rounds", func() {
By("Setting adapter responses", actions.SetAllAdapterResponses(5, ocrInstances, chainlinkNodes, mockserver))
By("Setting adapter responses", actions.SetAllAdapterResponsesToTheSameValue(5, ocrInstances, chainlinkNodes, mockserver))
By("Creating OCR jobs", actions.CreateOCRJobs(ocrInstances, chainlinkNodes, mockserver))

By("Starting new round", actions.StartNewRound(1, ocrInstances, networks))
Expand All @@ -82,7 +82,7 @@ var _ = Describe("OCR Feed @ocr", func() {
Expect(err).ShouldNot(HaveOccurred(), "Getting latest answer from OCR contract shouldn't fail")
Expect(answer.Int64()).Should(Equal(int64(5)), "Expected latest answer from OCR contract to be 5 but got %d", answer.Int64())

By("setting adapter responses", actions.SetAllAdapterResponses(10, ocrInstances, chainlinkNodes, mockserver))
By("setting adapter responses", actions.SetAllAdapterResponsesToTheSameValue(10, ocrInstances, chainlinkNodes, mockserver))
By("starting new round", actions.StartNewRound(2, ocrInstances, networks))

answer, err = ocrInstances[0].GetLatestAnswer(context.Background())
Expand Down

0 comments on commit ffa9a24

Please sign in to comment.