-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PoS Remote Node Indexer and Manager (#879)
* Add RemoteNodeIndexer * Add HandshakeController PoS Block Producer: TxnConnectStatusByIndex (#672) * TransactionConnectStatus and ConnectFailingTransaction * Revert "Merge branch 'p/bmf-status-connected' into p/failing-transactions" This reverts commit d3e543c4c3e6f03cc74087b05c268d4449ba1689, reversing changes made to 960001c. * Revert "Revert "Merge branch 'p/bmf-status-connected' into p/failing-transactions"" This reverts commit 10a147654c5147c28ec674d0650bb54c8d9cebce. * Revert "Merge branch 'p/bmf-status-connected' into p/failing-transactions" This reverts commit d3e543c4c3e6f03cc74087b05c268d4449ba1689, reversing changes made to a9f7827. * TransactionConnectStatus and ConnectFailingTransaction * Initial _connectFailingTransaction * ConnectFailingTransaction and GlobalParamsEntry updates * Fix merge conflicts * gofmt * Fix merge conflicts * Fix blockheight * Fix merge conflicts * gofmt * Revert connect failing transaction * Add TxnStatusConnectedIndex to block and header * Fix naming * Fix tests; remove asserts * Update comment Integration testing updates PoS Block Producer: TxnConnectStatusByIndex (#672) * TransactionConnectStatus and ConnectFailingTransaction * Revert "Merge branch 'p/bmf-status-connected' into p/failing-transactions" This reverts commit d3e543c4c3e6f03cc74087b05c268d4449ba1689, reversing changes made to 960001c. * Revert "Revert "Merge branch 'p/bmf-status-connected' into p/failing-transactions"" This reverts commit 10a147654c5147c28ec674d0650bb54c8d9cebce. * Revert "Merge branch 'p/bmf-status-connected' into p/failing-transactions" This reverts commit d3e543c4c3e6f03cc74087b05c268d4449ba1689, reversing changes made to a9f7827. * TransactionConnectStatus and ConnectFailingTransaction * Initial _connectFailingTransaction * ConnectFailingTransaction and GlobalParamsEntry updates * Fix merge conflicts * gofmt * Fix merge conflicts * Fix blockheight * Fix merge conflicts * gofmt * Revert connect failing transaction * Add TxnStatusConnectedIndex to block and header * Fix naming * Fix tests; remove asserts * Update comment RemoteNode and RemoteNodeId Initial remote node manager tests remote node tests Better connection testing framework Add validator integration test Fix validator-validator connection test; Add nonValidator-validator test Simplify indices Simplify remote node indexer; fix compilation Simplify RemoteNodeManager More RemoteNodeManager updates Nits
- Loading branch information
Showing
6 changed files
with
512 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package collections | ||
|
||
import "sync" | ||
|
||
type ConcurrentMap[Key comparable, Value any] struct { | ||
mtx sync.RWMutex | ||
m map[Key]Value | ||
} | ||
|
||
func NewConcurrentMap[Key comparable, Value any]() *ConcurrentMap[Key, Value] { | ||
return &ConcurrentMap[Key, Value]{ | ||
m: make(map[Key]Value), | ||
} | ||
} | ||
|
||
func (cm *ConcurrentMap[Key, Value]) Set(key Key, val Value) { | ||
cm.mtx.Lock() | ||
defer cm.mtx.Unlock() | ||
|
||
cm.m[key] = val | ||
} | ||
|
||
func (cm *ConcurrentMap[Key, Value]) Remove(key Key) { | ||
cm.mtx.Lock() | ||
defer cm.mtx.Unlock() | ||
|
||
_, ok := cm.m[key] | ||
if !ok { | ||
return | ||
} | ||
delete(cm.m, key) | ||
} | ||
|
||
func (cm *ConcurrentMap[Key, Value]) Get(key Key) (Value, bool) { | ||
cm.mtx.RLock() | ||
defer cm.mtx.RUnlock() | ||
|
||
val, ok := cm.m[key] | ||
return val, ok | ||
} | ||
|
||
func (cm *ConcurrentMap[Key, Value]) Copy() map[Key]Value { | ||
cm.mtx.RLock() | ||
defer cm.mtx.RUnlock() | ||
|
||
index := make(map[Key]Value) | ||
for key, node := range cm.m { | ||
index[key] = node | ||
} | ||
return index | ||
} | ||
|
||
func (cm *ConcurrentMap[Key, Value]) GetAll() []Value { | ||
cm.mtx.RLock() | ||
defer cm.mtx.RUnlock() | ||
|
||
var vals []Value | ||
for _, val := range cm.m { | ||
vals = append(vals, val) | ||
} | ||
return vals | ||
} | ||
|
||
func (cm *ConcurrentMap[Key, Value]) Count() int { | ||
cm.mtx.RLock() | ||
defer cm.mtx.RUnlock() | ||
|
||
return len(cm.m) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package collections | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestConcurrentMap(t *testing.T) { | ||
m := NewConcurrentMap[string, int]() | ||
control := make(map[string]int) | ||
|
||
// test add | ||
for ii := 0; ii < 100; ii++ { | ||
key := fmt.Sprintf("%v", ii) | ||
m.Set(key, ii) | ||
control[key] = ii | ||
} | ||
|
||
for key, val := range control { | ||
if mVal, ok := m.Get(key); !ok || mVal != val { | ||
t.Errorf("Expected %d, got %d", val, m.m[key]) | ||
} | ||
} | ||
|
||
// test remove | ||
for ii := 0; ii < 50; ii++ { | ||
key := fmt.Sprintf("%v", ii) | ||
m.Remove(key) | ||
delete(control, key) | ||
} | ||
|
||
for key, val := range control { | ||
if mVal, ok := m.Get(key); !ok || mVal != val { | ||
t.Errorf("Expected %d, got %d", val, m.m[key]) | ||
} | ||
} | ||
|
||
// test copy | ||
copy := m.Copy() | ||
for key, val := range control { | ||
if mVal, ok := copy[key]; !ok || mVal != val { | ||
t.Errorf("Expected %d, got %d", val, m.m[key]) | ||
} | ||
} | ||
if len(copy) != len(control) { | ||
t.Errorf("Expected %d, got %d", len(control), len(copy)) | ||
} | ||
|
||
// test get all | ||
vals := m.GetAll() | ||
for _, val := range vals { | ||
if _, ok := control[fmt.Sprintf("%v", val)]; !ok { | ||
t.Errorf("Expected %d, got %d", val, m.m[fmt.Sprintf("%v", val)]) | ||
} | ||
} | ||
|
||
// test size | ||
if m.Count() != len(control) { | ||
t.Errorf("Expected %d, got %d", len(control), m.Count()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package lib | ||
|
||
import ( | ||
"github.com/deso-protocol/core/bls" | ||
"github.com/deso-protocol/core/collections" | ||
) | ||
|
||
// RemoteNodeIndexer is a structure that holds information about all remote nodes and their indices. | ||
type RemoteNodeIndexer struct { | ||
// AllRemoteNodes is a map storing all remote nodes by their IDs. | ||
AllRemoteNodes *collections.ConcurrentMap[RemoteNodeId, *RemoteNode] | ||
|
||
// Indices for various types of remote nodes. | ||
ValidatorIndex *collections.ConcurrentMap[bls.SerializedPublicKey, *RemoteNode] | ||
NonValidatorOutboundIndex *collections.ConcurrentMap[RemoteNodeId, *RemoteNode] | ||
NonValidatorInboundIndex *collections.ConcurrentMap[RemoteNodeId, *RemoteNode] | ||
} | ||
|
||
// NewRemoteNodeIndexer initializes and returns a new instance of RemoteNodeIndexer. | ||
func NewRemoteNodeIndexer() *RemoteNodeIndexer { | ||
rni := &RemoteNodeIndexer{ | ||
AllRemoteNodes: collections.NewConcurrentMap[RemoteNodeId, *RemoteNode](), | ||
ValidatorIndex: collections.NewConcurrentMap[bls.SerializedPublicKey, *RemoteNode](), | ||
NonValidatorOutboundIndex: collections.NewConcurrentMap[RemoteNodeId, *RemoteNode](), | ||
NonValidatorInboundIndex: collections.NewConcurrentMap[RemoteNodeId, *RemoteNode](), | ||
} | ||
|
||
return rni | ||
} | ||
|
||
// Getter methods for accessing the different indices. | ||
func (rni *RemoteNodeIndexer) GetAllRemoteNodes() *collections.ConcurrentMap[RemoteNodeId, *RemoteNode] { | ||
return rni.AllRemoteNodes | ||
} | ||
|
||
func (rni *RemoteNodeIndexer) GetValidatorIndex() *collections.ConcurrentMap[bls.SerializedPublicKey, *RemoteNode] { | ||
return rni.ValidatorIndex | ||
} | ||
|
||
func (rni *RemoteNodeIndexer) GetNonValidatorOutboundIndex() *collections.ConcurrentMap[RemoteNodeId, *RemoteNode] { | ||
return rni.NonValidatorOutboundIndex | ||
} | ||
|
||
func (rni *RemoteNodeIndexer) GetNonValidatorInboundIndex() *collections.ConcurrentMap[RemoteNodeId, *RemoteNode] { | ||
return rni.NonValidatorInboundIndex | ||
} |
Oops, something went wrong.