Skip to content

Commit

Permalink
unskipping the TestGhostNodeExample tests (#1670)
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalchangrani authored Nov 30, 2021
1 parent 923546c commit 7c4f69a
Showing 1 changed file with 17 additions and 59 deletions.
76 changes: 17 additions & 59 deletions integration/tests/common/ghost_node_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,24 @@ package common

import (
"context"
"fmt"
"testing"
"time"

"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"

sdk "github.com/onflow/flow-go-sdk"

"github.com/onflow/flow-go/engine"
"github.com/onflow/flow-go/integration/testnet"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/model/messages"
"github.com/onflow/flow-go/utils/unittest"
)

// These tests are just examples of how to use the ghost node in an integration tests.
// They do no test any functionality and hence are marked to be skipped
// Tests to check if the Ghost node works as expected

// TestGhostNodeExample_Subscribe demonstrates how to emulate a node and receive all inbound events for it
func TestGhostNodeExample_Subscribe(t *testing.T) {

t.Skip()
var (
// one collection node
collNode = testnet.NewNodeConfig(flow.RoleCollection, testnet.WithLogLevel(zerolog.FatalLevel), testnet.WithIDInt(1))
Expand All @@ -41,9 +38,11 @@ func TestGhostNodeExample_Subscribe(t *testing.T) {

// a verification node
verNode = testnet.NewNodeConfig(flow.RoleVerification, testnet.WithLogLevel(zerolog.FatalLevel))

accessNode = testnet.NewNodeConfig(flow.RoleAccess, testnet.WithLogLevel(zerolog.FatalLevel))
)

nodes := append([]testnet.NodeConfig{collNode, conNode1, conNode2, conNode3, realExeNode, verNode, ghostExeNode})
nodes := append([]testnet.NodeConfig{collNode, conNode1, conNode2, conNode3, realExeNode, verNode, ghostExeNode, accessNode})
conf := testnet.NewNetworkConfig("ghost_example_subscribe", nodes)

net := testnet.PrepareFlowNetwork(t, conf)
Expand All @@ -64,36 +63,25 @@ func TestGhostNodeExample_Subscribe(t *testing.T) {
msgReader, err := ghostClient.Subscribe(ctx)
assert.NoError(t, err)

// create and send a transaction to one of the collection node
sendTransaction(t, net, collNode.Identifier)

proposals := make([]*messages.BlockProposal, 0)

for {
_, event, err := msgReader.Next()
// wait for 5 blocks proposals
for i:=0;i<5;{
from, event, err := msgReader.Next()
assert.NoError(t, err)

// the following switch should be similar to the one defined in the actual node that is being emulated
switch v := event.(type) {
case *messages.BlockProposal:
proposals = append(proposals, v)
fmt.Printf("Received block proposal: %s from %s\n", v.Header.ID().String(), from.String())
i++
default:
t.Logf(" ignoring event: :%T: %v", v, v)
}

if len(proposals) == 2 {
break
}
}

assert.EqualValues(t, 1, proposals[0].Header.Height)
assert.EqualValues(t, 2, proposals[1].Header.Height)
}

// TestGhostNodeExample_Send demonstrates how to emulate a node and send an event from it
func TestGhostNodeExample_Send(t *testing.T) {

t.Skip()
var (
// one real collection node
realCollNode = testnet.NewNodeConfig(flow.RoleCollection, testnet.WithLogLevel(zerolog.DebugLevel), testnet.WithIDInt(1))
Expand All @@ -112,9 +100,11 @@ func TestGhostNodeExample_Send(t *testing.T) {

// a verification node
verNode = testnet.NewNodeConfig(flow.RoleVerification, testnet.WithLogLevel(zerolog.FatalLevel))

accessNode = testnet.NewNodeConfig(flow.RoleAccess, testnet.WithLogLevel(zerolog.FatalLevel))
)

nodes := append([]testnet.NodeConfig{realCollNode, ghostCollNode, conNode1, conNode2, conNode3, realExeNode, verNode})
nodes := append([]testnet.NodeConfig{realCollNode, ghostCollNode, conNode1, conNode2, conNode3, realExeNode, verNode, accessNode})
conf := testnet.NewNetworkConfig("ghost_example_send", nodes)

net := testnet.PrepareFlowNetwork(t, conf)
Expand All @@ -131,42 +121,10 @@ func TestGhostNodeExample_Send(t *testing.T) {
ghostClient, err := GetGhostClient(ghostContainer)
assert.NoError(t, err)

// generate a signed transaction
tx := generateTransaction(t, net, realCollNode.Identifier)
// generate a test transaction
tx := unittest.TransactionBodyFixture()

// send the transaction as an event to the real collection node
// send the transaction as an event to a real collection node
err = ghostClient.Send(ctx, engine.PushTransactions, &tx, realCollNode.Identifier)
assert.NoError(t, err)
}

func sendTransaction(t *testing.T, net *testnet.FlowNetwork, collectionID flow.Identifier) {
colContainer1 := net.ContainerByID(collectionID)

collectionClient, err := testnet.NewClient(colContainer1.Addr(testnet.ColNodeAPIPort), net.Root().Header.ChainID.Chain())
assert.Nil(t, err)

txFixture := SDKTransactionFixture()
tx, err := collectionClient.SignTransaction(&txFixture)
assert.Nil(t, err)

t.Log("sending transaction: ", tx.ID())

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

err = collectionClient.SendTransaction(ctx, tx)
assert.Nil(t, err)
}

func generateTransaction(t *testing.T, net *testnet.FlowNetwork, collectionID flow.Identifier) *sdk.Transaction {
colContainer1 := net.ContainerByID(collectionID)

collectionClient, err := testnet.NewClient(colContainer1.Addr(testnet.ColNodeAPIPort), net.Root().Header.ChainID.Chain())
assert.Nil(t, err)

txFixture := SDKTransactionFixture()
tx, err := collectionClient.SignTransaction(&txFixture)
assert.Nil(t, err)

return tx
}

0 comments on commit 7c4f69a

Please sign in to comment.