Skip to content

Commit

Permalink
VRF-824: add reorg test for VRF v2 and v2 Plus (#12939)
Browse files Browse the repository at this point in the history
* VRF-824: add reorg test for VRF v2 Plus

* VRF-824: fixing lint

* VRF-824: cleaning up

* VRF-824: fixing test

* VRF-824: fixing test

* VRF-824: fixing test

* VRF-824: adding vrfv2 test for reorg

* VRF-824: fixing lint

* VRF-824: migrating to resty for raw rpc call
  • Loading branch information
iljapavlovs authored Apr 24, 2024
1 parent 5276e02 commit 0f23216
Show file tree
Hide file tree
Showing 8 changed files with 558 additions and 42 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -563,12 +563,12 @@ jobs:
pyroscope_env: ci-smoke-vrf-evm-simulated
- name: vrfv2
id: vrfv2
nodes: 4
nodes: 5
os: ubuntu-latest
pyroscope_env: ci-smoke-vrf2-evm-simulated
- name: vrfv2plus
id: vrfv2plus
nodes: 7
nodes: 8
os: ubuntu-latest
pyroscope_env: ci-smoke-vrf2plus-evm-simulated
- name: forwarder_ocr
Expand Down
102 changes: 102 additions & 0 deletions integration-tests/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import (
"errors"
"fmt"
"math/big"
"os"
"strings"
"sync"
"testing"
"time"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/go-resty/resty/v2"
"github.com/rs/zerolog"

"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/keystore"
Expand All @@ -33,6 +37,7 @@ import (
"github.com/smartcontractkit/chainlink-testing-framework/utils/testcontext"
"github.com/smartcontractkit/chainlink/integration-tests/client"
"github.com/smartcontractkit/chainlink/integration-tests/contracts"
"github.com/smartcontractkit/chainlink/integration-tests/docker/test_env"
)

// ContractDeploymentInterval After how many contract actions to wait before starting any more
Expand Down Expand Up @@ -562,3 +567,100 @@ func WaitForBlockNumberToBe(
}
}
}

// todo - move to EVMClient
func RewindSimulatedChainToBlockNumber(
ctx context.Context,
evmClient blockchain.EVMClient,
rpcURL string,
rewindChainToBlockNumber uint64,
l zerolog.Logger,
) (uint64, error) {
latestBlockNumberBeforeReorg, err := evmClient.LatestBlockNumber(ctx)
if err != nil {
return 0, fmt.Errorf("error getting latest block number: %w", err)
}

l.Info().
Str("RPC URL", rpcURL).
Uint64("Latest Block Number before Reorg", latestBlockNumberBeforeReorg).
Uint64("Rewind Chain to Block Number", rewindChainToBlockNumber).
Msg("Performing Reorg on chain by rewinding chain to specific block number")

_, err = NewRPCRawClient(rpcURL).SetHeadForSimulatedChain(rewindChainToBlockNumber)

if err != nil {
return 0, fmt.Errorf("error making reorg: %w", err)
}

err = evmClient.WaitForEvents()
if err != nil {
return 0, fmt.Errorf("error waiting for events: %w", err)
}

latestBlockNumberAfterReorg, err := evmClient.LatestBlockNumber(ctx)
if err != nil {
return 0, fmt.Errorf("error getting latest block number: %w", err)
}

l.Info().
Uint64("Block Number", latestBlockNumberAfterReorg).
Msg("Latest Block Number after Reorg")
return latestBlockNumberAfterReorg, nil
}

func GetRPCUrl(env *test_env.CLClusterTestEnv, chainID int64) (string, error) {
provider, err := env.GetRpcProvider(chainID)
if err != nil {
return "", err
}
return provider.PublicHttpUrls()[0], nil
}

// RPCRawClient
// created separate client since method evmClient.RawJsonRPCCall fails on "invalid argument 0: json: cannot unmarshal non-string into Go value of type hexutil.Uint64"
type RPCRawClient struct {
resty *resty.Client
}

func NewRPCRawClient(url string) *RPCRawClient {
isDebug := os.Getenv("DEBUG_RESTY") == "true"
restyClient := resty.New().SetDebug(isDebug).SetBaseURL(url)
return &RPCRawClient{
resty: restyClient,
}
}

func (g *RPCRawClient) SetHeadForSimulatedChain(setHeadToBlockNumber uint64) (JsonRPCResponse, error) {
var responseObject JsonRPCResponse
postBody, _ := json.Marshal(map[string]any{
"jsonrpc": "2.0",
"id": 1,
"method": "debug_setHead",
"params": []string{hexutil.EncodeUint64(setHeadToBlockNumber)},
})
resp, err := g.resty.R().
SetHeader("Content-Type", "application/json").
SetBody(postBody).
SetResult(&responseObject).
Post("")

if err != nil {
return JsonRPCResponse{}, fmt.Errorf("error making API request: %w", err)
}
statusCode := resp.StatusCode()
if statusCode != 200 && statusCode != 201 {
return JsonRPCResponse{}, fmt.Errorf("error invoking debug_setHead method, received unexpected status code %d: %s", statusCode, resp.String())
}
if responseObject.Error != "" {
return JsonRPCResponse{}, fmt.Errorf("received non-empty error field: %v", responseObject.Error)
}
return responseObject, nil
}

type JsonRPCResponse struct {
Version string `json:"jsonrpc"`
Id int `json:"id"`
Result string `json:"result,omitempty"`
Error string `json:"error,omitempty"`
}
10 changes: 5 additions & 5 deletions integration-tests/actions/vrf/vrfv2/contract_steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ func RequestRandomnessAndWaitForFulfillment(
randomnessRequestCountPerRequest uint16,
randomnessRequestCountPerRequestDeviation uint16,
randomWordsFulfilledEventTimeout time.Duration,
) (*contracts.CoordinatorRandomWordsFulfilled, error) {
) (*contracts.CoordinatorRandomWordsRequested, *contracts.CoordinatorRandomWordsFulfilled, error) {
randomWordsRequestedEvent, err := RequestRandomness(
l,
consumer,
Expand All @@ -520,18 +520,18 @@ func RequestRandomnessAndWaitForFulfillment(
randomnessRequestCountPerRequestDeviation,
)
if err != nil {
return nil, err
return nil, nil, err
}
fulfillmentEvents, err := WaitRandomWordsFulfilledEvent(
randomWordsFulfilledEvent, err := WaitRandomWordsFulfilledEvent(
coordinator,
randomWordsRequestedEvent.RequestId,
randomWordsFulfilledEventTimeout,
l,
)
if err != nil {
return nil, err
return nil, nil, err
}
return fulfillmentEvents, nil
return randomWordsRequestedEvent, randomWordsFulfilledEvent, nil
}

func RequestRandomness(
Expand Down
8 changes: 4 additions & 4 deletions integration-tests/actions/vrf/vrfv2plus/contract_steps.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func RequestRandomnessAndWaitForFulfillment(
isNativeBilling bool,
config *vrfv2plus_config.General,
l zerolog.Logger,
) (*contracts.CoordinatorRandomWordsFulfilled, error) {
) (*contracts.CoordinatorRandomWordsRequested, *contracts.CoordinatorRandomWordsFulfilled, error) {
randomWordsRequestedEvent, err := RequestRandomness(
consumer,
coordinator,
Expand All @@ -387,7 +387,7 @@ func RequestRandomnessAndWaitForFulfillment(
l,
)
if err != nil {
return nil, err
return nil, nil, err
}

randomWordsFulfilledEvent, err := WaitRandomWordsFulfilledEvent(
Expand All @@ -399,9 +399,9 @@ func RequestRandomnessAndWaitForFulfillment(
l,
)
if err != nil {
return nil, err
return nil, nil, err
}
return randomWordsFulfilledEvent, nil
return randomWordsRequestedEvent, randomWordsFulfilledEvent, nil

}

Expand Down
2 changes: 1 addition & 1 deletion integration-tests/load/vrfv2/gun.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (m *SingleHashGun) Call(_ *wasp.Generator) *wasp.Response {
vrfv2Config := m.testConfig.General
//randomly increase/decrease randomness request count per TX
randomnessRequestCountPerRequest := deviateValue(*vrfv2Config.RandomnessRequestCountPerRequest, *vrfv2Config.RandomnessRequestCountPerRequestDeviation)
_, err := vrfv2.RequestRandomnessAndWaitForFulfillment(
_, _, err := vrfv2.RequestRandomnessAndWaitForFulfillment(
m.logger,
//the same consumer is used for all requests and in all subs
m.contracts.VRFV2Consumers[0],
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/load/vrfv2plus/gun.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (m *SingleHashGun) Call(_ *wasp.Generator) *wasp.Response {
//randomly increase/decrease randomness request count per TX
reqCount := deviateValue(*m.testConfig.General.RandomnessRequestCountPerRequest, *m.testConfig.General.RandomnessRequestCountPerRequestDeviation)
m.testConfig.General.RandomnessRequestCountPerRequest = &reqCount
_, err = vrfv2plus.RequestRandomnessAndWaitForFulfillment(
_, _, err = vrfv2plus.RequestRandomnessAndWaitForFulfillment(
//the same consumer is used for all requests and in all subs
m.contracts.VRFV2PlusConsumer[0],
m.contracts.CoordinatorV2Plus,
Expand Down
Loading

0 comments on commit 0f23216

Please sign in to comment.