Skip to content

Commit

Permalink
Integration testing updates
Browse files Browse the repository at this point in the history
  • Loading branch information
AeonSw4n committed Dec 18, 2023
1 parent 12fc674 commit a1f816a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 16 deletions.
2 changes: 1 addition & 1 deletion integration_testing/blocksync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestSimpleBlockSync(t *testing.T) {
// wait for node1 to sync blocks
waitForNodeToFullySync(node1)

node2.Server.CreateOutboundConnection(node1.Listeners[0].Addr().String())
// TODO: Dial an outbound connection from node2 to node1

// wait for node2 to sync blocks.
waitForNodeToFullySync(node2)
Expand Down
44 changes: 32 additions & 12 deletions integration_testing/connection_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,25 +142,28 @@ func (bridge *ConnectionBridge) createOutboundConnection(node *cmd.Node, otherNo

na, err := lib.IPToNetAddr(conn.RemoteAddr().String(), otherNode.Server.GetConnectionManager().AddrMgr,
otherNode.Params)
messagesFromPeer := make(chan *lib.ServerMessage)
peer := lib.NewPeer(conn, false, na, false,
10000, 0, bridge.nodeB.Params,
messagesFromPeer, nil, nil, lib.NodeSyncTypeAny)
peer.ID = uint64(lib.RandInt64(math.MaxInt64))
messagesFromPeer := make(chan *lib.ServerMessage, 100)
newPeerChan := make(chan *lib.Peer, 100)
donePeerChan := make(chan *lib.Peer, 100)
peer := lib.NewPeer(uint64(lib.RandInt64(math.MaxInt64)), uint64(lib.RandInt64(math.MaxInt64)), conn,
false, na, false, 10000, 0, bridge.nodeB.Params,
messagesFromPeer, nil, nil, lib.NodeSyncTypeAny, newPeerChan, donePeerChan)
bridge.newPeerChan <- peer
//}
}(ll)

// Make the provided node to make an outbound connection to our listener.
//netAddress, _ := lib.IPToNetAddr(ll.Addr().String(), addrmgr.New("", net.LookupIP), &lib.DeSoMainnetParams)
//fmt.Println("createOutboundConnection: IP:", netAddress.IP, "Port:", netAddress.Port)
go node.Server.GetConnectionManager().CreateOutboundConnection(ll.Addr().String())
addrMgr := addrmgr.New("", net.LookupIP)
addr, _ := lib.IPToNetAddr(ll.Addr().String(), addrMgr, node.Params)
go node.Server.GetConnectionManager().DialOutboundConnection(addr)
}

// getVersionMessage simulates a version message that the provided node would have sent.
func (bridge *ConnectionBridge) getVersionMessage(node *cmd.Node) *lib.MsgDeSoVersion {
ver := lib.NewMessage(lib.MsgTypeVersion).(*lib.MsgDeSoVersion)
ver.Version = node.Params.ProtocolVersion
ver.Version = node.Params.ProtocolVersion.ToUint64()
ver.TstampSecs = time.Now().Unix()
ver.Nonce = uint64(lib.RandInt64(math.MaxInt64))
ver.UserAgent = node.Params.UserAgent
Expand All @@ -179,6 +182,23 @@ func (bridge *ConnectionBridge) getVersionMessage(node *cmd.Node) *lib.MsgDeSoVe
return ver
}

func ReadWithTimeout(readFunc func() error, readTimeout time.Duration) error {
errChan := make(chan error)
go func() {
errChan <- readFunc()
}()
select {
case err := <-errChan:
{
return err
}
case <-time.After(readTimeout):
{
return fmt.Errorf("ReadWithTimeout: Timed out reading message")
}
}
}

// startConnection starts the connection by performing version and verack exchange with
// the provided connection, pretending to be the otherNode.
func (bridge *ConnectionBridge) startConnection(connection *lib.Peer, otherNode *cmd.Node) error {
Expand All @@ -193,7 +213,7 @@ func (bridge *ConnectionBridge) startConnection(connection *lib.Peer, otherNode
}

// Wait for a response to the version message.
if err := connection.ReadWithTimeout(
if err := ReadWithTimeout(
func() error {
msg, err := connection.ReadDeSoMessage()
if err != nil {
Expand All @@ -216,15 +236,15 @@ func (bridge *ConnectionBridge) startConnection(connection *lib.Peer, otherNode

// Now prepare the verack message.
verackMsg := lib.NewMessage(lib.MsgTypeVerack)
verackMsg.(*lib.MsgDeSoVerack).Nonce = connection.VersionNonceReceived
verackMsg.(*lib.MsgDeSoVerack).NonceReceived = connection.VersionNonceReceived

// And send it to the connection.
if err := connection.WriteDeSoMessage(verackMsg); err != nil {
return err
}

// And finally wait for connection's response to the verack message.
if err := connection.ReadWithTimeout(
if err := ReadWithTimeout(
func() error {
msg, err := connection.ReadDeSoMessage()
if err != nil {
Expand All @@ -235,9 +255,9 @@ func (bridge *ConnectionBridge) startConnection(connection *lib.Peer, otherNode
return fmt.Errorf("message is not verack! Type: %v", msg.GetMsgType())
}
verackMsg := msg.(*lib.MsgDeSoVerack)
if verackMsg.Nonce != connection.VersionNonceSent {
if verackMsg.NonceReceived != connection.VersionNonceSent {
return fmt.Errorf("verack message nonce doesn't match (received: %v, sent: %v)",
verackMsg.Nonce, connection.VersionNonceSent)
verackMsg.NonceReceived, connection.VersionNonceSent)
}
return nil
}, lib.DeSoMainnetParams.VersionNegotiationTimeout); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions integration_testing/networking_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ func TestSimpleConnectDisconnect(t *testing.T) {
node1 := cmd.NewNode(config1)
node1 = startNode(t, node1)

// connect node1 to deso-seed-2.io
node1.Server.CreateOutboundConnection("deso-seed-2.io:17000")
// TODO: connect node1 to deso-seed-2.io
node1.Server.GetConnectionManager().SetTargetOutboundPeers(0)

<-listenForBlockHeight(node1, 50)
node1.Server.CloseConnection(1)
// TODO: close connection
time.Sleep(3 * time.Second)
peers := node1.Server.GetConnectionManager().GetAllPeers()
for _, peer := range peers {
Expand Down
36 changes: 36 additions & 0 deletions integration_testing/remote_node_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package integration_testing

import (
"github.com/deso-protocol/core/cmd"
"github.com/deso-protocol/core/lib"
"github.com/stretchr/testify/require"
"os"
"testing"
"time"
)

func TestOutboundConnection(t *testing.T) {
require := require.New(t)
_ = require

dbDir1 := getDirectory(t)
dbDir2 := getDirectory(t)
defer os.RemoveAll(dbDir1)
defer os.RemoveAll(dbDir2)

config1 := generateConfig(t, 18000, dbDir1, 10)
config1.SyncType = lib.NodeSyncTypeBlockSync
config2 := generateConfig(t, 18001, dbDir2, 10)
config2.SyncType = lib.NodeSyncTypeBlockSync

node1 := cmd.NewNode(config1)
node2 := cmd.NewNode(config2)

node1 = startNode(t, node1)
node2 = startNode(t, node2)

time.Sleep(10 * time.Second)
rn := lib.NewRemoteNode(node1.Server, node1.Server.GetBlockchain(), node1.Server.GetConnectionManager(),
config1.MinFeerate, config1.HyperSync, true)
// TODO: initiate a connection
}
1 change: 1 addition & 0 deletions integration_testing/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func generateConfig(t *testing.T, port uint32, dataDir string, maxPeers uint32)
config.SnapshotBlockHeightPeriod = HyperSyncSnapshotPeriod
config.MaxSyncBlockHeight = MaxSyncBlockHeight
config.SyncType = lib.NodeSyncTypeBlockSync
config.PosValidatorSeed = "0x023f477056acd6807fd43958d2f8497c3b3522691173c2c4c65b8a3ecbc7db94"
//config.ArchivalMode = true

return config
Expand Down

0 comments on commit a1f816a

Please sign in to comment.