Skip to content

Commit

Permalink
Revert "Code split"
Browse files Browse the repository at this point in the history
This reverts commit c0c32f3943ead0e06fdfb3343954a6b5273ea887.
  • Loading branch information
AeonSw4n committed Jan 28, 2024
1 parent e79150c commit fab9a5a
Show file tree
Hide file tree
Showing 20 changed files with 493 additions and 215 deletions.
13 changes: 12 additions & 1 deletion collections/concurrent_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,18 @@ func (cm *ConcurrentMap[Key, Value]) Get(key Key) (Value, bool) {
return val, ok
}

func (cm *ConcurrentMap[Key, Value]) Copy() map[Key]Value {
func (cm *ConcurrentMap[Key, Value]) Clone() *ConcurrentMap[Key, Value] {
cm.mtx.RLock()
defer cm.mtx.RUnlock()

clone := NewConcurrentMap[Key, Value]()
for key, val := range cm.m {
clone.Set(key, val)
}
return clone
}

func (cm *ConcurrentMap[Key, Value]) ToMap() map[Key]Value {
cm.mtx.RLock()
defer cm.mtx.RUnlock()

Expand Down
2 changes: 1 addition & 1 deletion collections/concurrent_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestConcurrentMap(t *testing.T) {
}

// test copy
copy := m.Copy()
copy := m.ToMap()
for key, val := range control {
if mVal, ok := copy[key]; !ok || mVal != val {
t.Errorf("Expected %d, got %d", val, m.m[key])
Expand Down
4 changes: 4 additions & 0 deletions consensus/integration_test_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ func (node *validatorNode) GetStakeAmount() *uint256.Int {
return node.stake
}

func (node *validatorNode) GetDomains() [][]byte {
return [][]byte{}
}

func (node *validatorNode) ProcessBlock(incomingBlock *block) {
node.lock.Lock()
defer node.lock.Unlock()
Expand Down
1 change: 1 addition & 0 deletions consensus/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type BlockHash interface {
type Validator interface {
GetPublicKey() *bls.PublicKey
GetStakeAmount() *uint256.Int
GetDomains() [][]byte
}

type AggregateQuorumCertificate interface {
Expand Down
4 changes: 4 additions & 0 deletions consensus/types_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ func (v *validator) GetStakeAmount() *uint256.Int {
return v.stakeAmount
}

func (v *validator) GetDomains() [][]byte {
return [][]byte{}
}

////////////////////////////////////////////////////////////////////////
// AggregateQuorumCertificate interface implementation for internal use.
// We use this type for unit tests, and to construct timeout QCs for
Expand Down
277 changes: 205 additions & 72 deletions integration_testing/connection_controller_routines_test.go

Large diffs are not rendered by default.

31 changes: 7 additions & 24 deletions integration_testing/connection_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
)

func TestConnectionControllerNonValidator(t *testing.T) {
SetDisableNetworkManagerRoutines(t)
require := require.New(t)

node1 := spawnNonValidatorNodeProtocol2(t, 18000, "node1")
node1 = startNode(t, node1)
defer node1.Stop()

// Make sure NonValidator Node1 can create an outbound connection to NonValidator Node2
node2 := spawnNonValidatorNodeProtocol2(t, 18001, "node2")
Expand Down Expand Up @@ -47,7 +47,6 @@ func TestConnectionControllerNonValidator(t *testing.T) {
require.NoError(err)
node4 := spawnValidatorNodeProtocol2(t, 18003, "node4", blsPriv4)
node4 = startNode(t, node4)
defer node4.Stop()

cc = node1.Server.GetConnectionController()
require.NoError(cc.CreateNonValidatorOutboundConnection(node4.Listeners[0].Addr().String()))
Expand All @@ -57,13 +56,13 @@ func TestConnectionControllerNonValidator(t *testing.T) {
}

func TestConnectionControllerValidator(t *testing.T) {
SetDisableNetworkManagerRoutines(t)
require := require.New(t)

blsPriv1, err := bls.NewPrivateKey()
require.NoError(err)
node1 := spawnValidatorNodeProtocol2(t, 18000, "node1", blsPriv1)
node1 = startNode(t, node1)
defer node1.Stop()

// Make sure Validator Node1 can create an outbound connection to Validator Node2
blsPriv2, err := bls.NewPrivateKey()
Expand Down Expand Up @@ -99,7 +98,6 @@ func TestConnectionControllerValidator(t *testing.T) {
require.NoError(err)
node4 := spawnValidatorNodeProtocol2(t, 18003, "node4", blsPriv4)
node4 = startNode(t, node4)
defer node4.Stop()

cc = node1.Server.GetConnectionController()
require.NoError(cc.CreateNonValidatorOutboundConnection(node4.Listeners[0].Addr().String()))
Expand All @@ -109,6 +107,7 @@ func TestConnectionControllerValidator(t *testing.T) {
}

func TestConnectionControllerHandshakeDataErrors(t *testing.T) {
SetDisableNetworkManagerRoutines(t)
require := require.New(t)

blsPriv1, err := bls.NewPrivateKey()
Expand All @@ -123,8 +122,6 @@ func TestConnectionControllerHandshakeDataErrors(t *testing.T) {

node1 = startNode(t, node1)
node2 = startNode(t, node2)
defer node1.Stop()
defer node2.Stop()

cc := node2.Server.GetConnectionController()
require.NoError(cc.CreateNonValidatorOutboundConnection(node1.Listeners[0].Addr().String()))
Expand All @@ -138,7 +135,6 @@ func TestConnectionControllerHandshakeDataErrors(t *testing.T) {
node3 := spawnValidatorNodeProtocol2(t, 18002, "node3", blsPriv3)
node3.Params.ProtocolVersion = lib.ProtocolVersionType(3)
node3 = startNode(t, node3)
defer node3.Stop()

cc = node1.Server.GetConnectionController()
require.NoError(cc.CreateNonValidatorOutboundConnection(node3.Listeners[0].Addr().String()))
Expand All @@ -150,7 +146,6 @@ func TestConnectionControllerHandshakeDataErrors(t *testing.T) {
node4 := spawnNonValidatorNodeProtocol2(t, 18003, "node4")
node4.Params.ProtocolVersion = lib.ProtocolVersion0
node4 = startNode(t, node4)
defer node4.Stop()

cc = node1.Server.GetConnectionController()
require.NoError(cc.CreateNonValidatorOutboundConnection(node4.Listeners[0].Addr().String()))
Expand All @@ -165,7 +160,6 @@ func TestConnectionControllerHandshakeDataErrors(t *testing.T) {
require.NoError(err)
node5 := spawnValidatorNodeProtocol2(t, 18004, "node5", blsPriv5)
node5 = startNode(t, node5)
defer node5.Stop()

cc = node1.Server.GetConnectionController()
require.NoError(cc.CreateValidatorConnection(node5.Listeners[0].Addr().String(), blsPriv5Wrong.PublicKey()))
Expand All @@ -178,7 +172,6 @@ func TestConnectionControllerHandshakeDataErrors(t *testing.T) {
require.NoError(err)
node6 := spawnNonValidatorNodeProtocol2(t, 18005, "node6")
node6 = startNode(t, node6)
defer node6.Stop()

cc = node1.Server.GetConnectionController()
require.NoError(cc.CreateValidatorConnection(node6.Listeners[0].Addr().String(), blsPriv6.PublicKey()))
Expand All @@ -190,7 +183,6 @@ func TestConnectionControllerHandshakeDataErrors(t *testing.T) {
node7 := spawnNonValidatorNodeProtocol2(t, 18006, "node7")
node7.Params.ProtocolVersion = lib.ProtocolVersion1
node7 = startNode(t, node7)
defer node7.Stop()

cc = node1.Server.GetConnectionController()
require.NoError(cc.CreateNonValidatorOutboundConnection(node7.Listeners[0].Addr().String()))
Expand All @@ -200,17 +192,16 @@ func TestConnectionControllerHandshakeDataErrors(t *testing.T) {
}

func TestConnectionControllerHandshakeTimeouts(t *testing.T) {
SetDisableNetworkManagerRoutines(t)
require := require.New(t)

// Set version negotiation timeout to 0 to make sure that the node will be disconnected
node1 := spawnNonValidatorNodeProtocol2(t, 18000, "node1")
node1.Params.VersionNegotiationTimeout = 0
node1 = startNode(t, node1)
defer node1.Stop()

node2 := spawnNonValidatorNodeProtocol2(t, 18001, "node2")
node2 = startNode(t, node2)
defer node2.Stop()

cc := node1.Server.GetConnectionController()
require.NoError(cc.CreateNonValidatorOutboundConnection(node2.Listeners[0].Addr().String()))
Expand All @@ -223,7 +214,6 @@ func TestConnectionControllerHandshakeTimeouts(t *testing.T) {
node3 := spawnNonValidatorNodeProtocol2(t, 18002, "node3")
node3.Params.VerackNegotiationTimeout = 0
node3 = startNode(t, node3)
defer node3.Stop()

cc = node3.Server.GetConnectionController()
require.NoError(cc.CreateNonValidatorOutboundConnection(node1.Listeners[0].Addr().String()))
Expand All @@ -237,13 +227,11 @@ func TestConnectionControllerHandshakeTimeouts(t *testing.T) {
node4 := spawnValidatorNodeProtocol2(t, 18003, "node4", blsPriv4)
node4.Params.HandshakeTimeoutMicroSeconds = 0
node4 = startNode(t, node4)
defer node4.Stop()

blsPriv5, err := bls.NewPrivateKey()
require.NoError(err)
node5 := spawnValidatorNodeProtocol2(t, 18004, "node5", blsPriv5)
node5 = startNode(t, node5)
defer node5.Stop()

cc = node4.Server.GetConnectionController()
require.NoError(cc.CreateValidatorConnection(node5.Listeners[0].Addr().String(), blsPriv5.PublicKey()))
Expand All @@ -253,11 +241,11 @@ func TestConnectionControllerHandshakeTimeouts(t *testing.T) {
}

func TestConnectionControllerValidatorDuplication(t *testing.T) {
SetDisableNetworkManagerRoutines(t)
require := require.New(t)

node1 := spawnNonValidatorNodeProtocol2(t, 18000, "node1")
node1 = startNode(t, node1)
defer node1.Stop()

// Create a validator Node2
blsPriv2, err := bls.NewPrivateKey()
Expand Down Expand Up @@ -294,11 +282,9 @@ func TestConnectionControllerValidatorDuplication(t *testing.T) {
require.NoError(err)
node4 := spawnValidatorNodeProtocol2(t, 18003, "node4", blsPriv4)
node4 = startNode(t, node4)
defer node4.Stop()

node5 := spawnValidatorNodeProtocol2(t, 18004, "node5", blsPriv4)
node5 = startNode(t, node5)
defer node5.Stop()

// Create validator connections from Node4 to Node1 and from Node5 to Node1
cc4 := node4.Server.GetConnectionController()
Expand All @@ -313,13 +299,13 @@ func TestConnectionControllerValidatorDuplication(t *testing.T) {
}

func TestConnectionControllerProtocolDifference(t *testing.T) {
SetDisableNetworkManagerRoutines(t)
require := require.New(t)

// Create a ProtocolVersion1 Node1
node1 := spawnNonValidatorNodeProtocol2(t, 18000, "node1")
node1.Params.ProtocolVersion = lib.ProtocolVersion1
node1 = startNode(t, node1)
defer node1.Stop()

// Create a ProtocolVersion2 NonValidator Node2
node2 := spawnNonValidatorNodeProtocol2(t, 18001, "node2")
Expand Down Expand Up @@ -353,7 +339,6 @@ func TestConnectionControllerProtocolDifference(t *testing.T) {
require.NoError(err)
node4 := spawnValidatorNodeProtocol2(t, 18003, "node4", blsPriv4)
node4 = startNode(t, node4)
defer node4.Stop()

// Attempt to create non-validator connection from Node4 to Node1
cc = node4.Server.GetConnectionController()
Expand All @@ -371,7 +356,6 @@ func TestConnectionControllerProtocolDifference(t *testing.T) {
// Create a ProtocolVersion2 non-validator Node5
node5 := spawnNonValidatorNodeProtocol2(t, 18004, "node5")
node5 = startNode(t, node5)
defer node5.Stop()

// Attempt to create non-validator connection from Node5 to Node1
cc = node5.Server.GetConnectionController()
Expand All @@ -382,6 +366,7 @@ func TestConnectionControllerProtocolDifference(t *testing.T) {
}

func TestConnectionControllerPersistentConnection(t *testing.T) {
SetDisableNetworkManagerRoutines(t)
require := require.New(t)

// Create a NonValidator Node1
Expand Down Expand Up @@ -423,7 +408,6 @@ func TestConnectionControllerPersistentConnection(t *testing.T) {
require.NoError(err)
node4 := spawnValidatorNodeProtocol2(t, 18003, "node4", blsPriv4)
node4 = startNode(t, node4)
defer node4.Stop()

// Create a non-validator Node5
node5 := spawnNonValidatorNodeProtocol2(t, 18004, "node5")
Expand All @@ -444,7 +428,6 @@ func TestConnectionControllerPersistentConnection(t *testing.T) {
require.NoError(err)
node6 := spawnValidatorNodeProtocol2(t, 18005, "node6", blsPriv6)
node6 = startNode(t, node6)
defer node6.Stop()

// Create a persistent connection from Node4 to Node6
_, err = cc.CreateNonValidatorPersistentOutboundConnection(node6.Listeners[0].Addr().String())
Expand Down
55 changes: 55 additions & 0 deletions integration_testing/connection_controller_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ func waitForCountRemoteNodeIndexer(t *testing.T, node1 *cmd.Node, allCount int,
waitForCondition(t, fmt.Sprintf("Waiting for Node (%s) to have appropriate RemoteNodes counts", userAgent), condition)
}

func waitForCountRemoteNodeIndexerHandshakeCompleted(t *testing.T, node1 *cmd.Node, allCount, validatorCount int,
nonValidatorOutboundCount int, nonValidatorInboundCount int) {

userAgent := node1.Params.UserAgent
rnManager := node1.Server.GetConnectionController().GetRemoteNodeManager()
condition := func() bool {
return checkRemoteNodeIndexerCountHandshakeCompleted(rnManager, allCount, validatorCount,
nonValidatorOutboundCount, nonValidatorInboundCount)
}
waitForCondition(t, fmt.Sprintf("Waiting for Node (%s) to have appropriate RemoteNodes counts", userAgent), condition)
}

func checkRemoteNodeIndexerUserAgent(manager *lib.RemoteNodeManager, userAgent string, validator bool,
nonValidatorOutbound bool, nonValidatorInbound bool) bool {

Expand Down Expand Up @@ -167,6 +179,42 @@ func checkRemoteNodeIndexerCount(manager *lib.RemoteNodeManager, allCount int, v
return true
}

func checkRemoteNodeIndexerCountHandshakeCompleted(manager *lib.RemoteNodeManager, allCount int, validatorCount int,
nonValidatorOutboundCount int, nonValidatorInboundCount int) bool {

if allCount != manager.GetAllRemoteNodes().Count() {
return false
}
if validatorCount != manager.GetValidatorIndex().Count() {
return false
}
for _, rn := range manager.GetValidatorIndex().GetAll() {
if !rn.IsHandshakeCompleted() {
return false
}
}

if nonValidatorOutboundCount != manager.GetNonValidatorOutboundIndex().Count() {
return false
}
for _, rn := range manager.GetNonValidatorOutboundIndex().GetAll() {
if !rn.IsHandshakeCompleted() {
return false
}
}

if nonValidatorInboundCount != manager.GetNonValidatorInboundIndex().Count() {
return false
}
for _, rn := range manager.GetNonValidatorInboundIndex().GetAll() {
if !rn.IsHandshakeCompleted() {
return false
}
}

return true
}

func checkRemoteNodeIndexerEmpty(manager *lib.RemoteNodeManager) bool {
if manager.GetAllRemoteNodes().Count() != 0 {
return false
Expand Down Expand Up @@ -232,3 +280,10 @@ func spawnValidatorNodeProtocol2(t *testing.T, port uint32, id string, blsPriv *
node.Params.ProtocolVersion = lib.ProtocolVersion2
return node
}

func SetDisableNetworkManagerRoutines(t *testing.T) {
lib.DisableNetworkManagerRoutines = true
t.Cleanup(func() {
lib.DisableNetworkManagerRoutines = false
})
}
2 changes: 1 addition & 1 deletion integration_testing/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ func waitForCondition(t *testing.T, id string, condition func() bool) {
signalChan <- struct{}{}
return
}
time.Sleep(1 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
}
}()

Expand Down
4 changes: 4 additions & 0 deletions lib/block_view_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ func (validatorEntry *ValidatorEntry) GetStakeAmount() *uint256.Int {
return validatorEntry.TotalStakeAmountNanos
}

func (validatorEntry *ValidatorEntry) GetDomains() [][]byte {
return validatorEntry.Domains
}

func (validatorEntry *ValidatorEntry) Status() ValidatorStatus {
// ValidatorEntry.Status() is a virtual/derived field that is not stored in
// the database, but instead constructed from other ValidatorEntry fields.
Expand Down
Loading

0 comments on commit fab9a5a

Please sign in to comment.