mirror of
https://github.com/kaspanet/kaspad.git
synced 2026-02-27 13:43:19 +00:00
Compare commits
2 Commits
v1.9.14-te
...
v0.6.10-de
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1743dc694a | ||
|
|
8fb30a5895 |
@@ -44,8 +44,12 @@ func (m *Manager) routerInitializer(router *router.Router, netConnection *netada
|
||||
err := m.handleIncomingMessages(router, incomingRoute)
|
||||
m.handleError(err, netConnection)
|
||||
})
|
||||
|
||||
notificationListener := m.context.NotificationManager.AddListener(router)
|
||||
spawn("routerInitializer-handleOutgoingNotifications", func() {
|
||||
err := m.handleOutgoingNotifications(router)
|
||||
defer m.context.NotificationManager.RemoveListener(router)
|
||||
|
||||
err := m.handleOutgoingNotifications(notificationListener)
|
||||
m.handleError(err, netConnection)
|
||||
})
|
||||
}
|
||||
@@ -72,9 +76,7 @@ func (m *Manager) handleIncomingMessages(router *router.Router, incomingRoute *r
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Manager) handleOutgoingNotifications(router *router.Router) error {
|
||||
notificationListener := m.context.NotificationManager.AddListener(router)
|
||||
defer m.context.NotificationManager.RemoveListener(router)
|
||||
func (m *Manager) handleOutgoingNotifications(notificationListener *rpccontext.NotificationListener) error {
|
||||
for {
|
||||
err := notificationListener.ProcessNextNotification()
|
||||
if err != nil {
|
||||
|
||||
@@ -12,9 +12,13 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// OnErrorHandler defines a handler function for when errors occur
|
||||
type OnErrorHandler func(err error)
|
||||
|
||||
// GRPCClient is a gRPC-based RPC client
|
||||
type GRPCClient struct {
|
||||
stream protowire.RPC_MessageStreamClient
|
||||
stream protowire.RPC_MessageStreamClient
|
||||
onErrorHandler OnErrorHandler
|
||||
}
|
||||
|
||||
// Connect connects to the RPC server with the given address
|
||||
@@ -41,6 +45,11 @@ func (c *GRPCClient) Disconnect() error {
|
||||
return c.stream.CloseSend()
|
||||
}
|
||||
|
||||
// SetOnErrorHandler sets the client's onErrorHandler
|
||||
func (c *GRPCClient) SetOnErrorHandler(onErrorHandler OnErrorHandler) {
|
||||
c.onErrorHandler = onErrorHandler
|
||||
}
|
||||
|
||||
// AttachRouter attaches the given router to the client and starts
|
||||
// sending/receiving messages via it
|
||||
func (c *GRPCClient) AttachRouter(router *router.Router) {
|
||||
@@ -101,5 +110,9 @@ func (c *GRPCClient) handleError(err error) {
|
||||
}
|
||||
return
|
||||
}
|
||||
if c.onErrorHandler != nil {
|
||||
c.onErrorHandler(err)
|
||||
return
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ type RPCClient struct {
|
||||
func NewRPCClient(rpcAddress string) (*RPCClient, error) {
|
||||
rpcClient, err := grpcclient.Connect(rpcAddress)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error connecting to address %s", rpcClient)
|
||||
return nil, errors.Wrapf(err, "error connecting to address %s", rpcAddress)
|
||||
}
|
||||
rpcRouter, err := buildRPCRouter()
|
||||
if err != nil {
|
||||
|
||||
@@ -224,7 +224,16 @@ func HashToBig(hash *Hash) *big.Int {
|
||||
// +1 if hash > target
|
||||
//
|
||||
func (hash *Hash) Cmp(target *Hash) int {
|
||||
return HashToBig(hash).Cmp(HashToBig(target))
|
||||
// We compare the hashes backwards because Hash is stored as a little endian byte array.
|
||||
for i := HashSize - 1; i >= 0; i-- {
|
||||
switch {
|
||||
case hash[i] < target[i]:
|
||||
return -1
|
||||
case hash[i] > target[i]:
|
||||
return 1
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// Less returns true iff hash a is less than hash b
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"math/big"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
@@ -425,3 +426,43 @@ func TestSort(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func hashFlipBit(hash Hash, bit int) Hash {
|
||||
word := bit / 8
|
||||
bit = bit % 8
|
||||
hash[word] ^= 1 << bit
|
||||
return hash
|
||||
}
|
||||
|
||||
func TestHash_Cmp(t *testing.T) {
|
||||
r := rand.New(rand.NewSource(1))
|
||||
|
||||
for i := 0; i < 100; i++ {
|
||||
hash := Hash{}
|
||||
n, err := r.Read(hash[:])
|
||||
if err != nil {
|
||||
t.Fatalf("Failed generating a random hash '%s'", err)
|
||||
} else if n != len(hash) {
|
||||
t.Fatalf("Failed generating a random hash, expected reading: %d. instead read: %d.", len(hash), n)
|
||||
|
||||
}
|
||||
hashBig := HashToBig(&hash)
|
||||
// Iterate bit by bit, flip it and compare.
|
||||
for bit := 0; bit < HashSize*8; bit++ {
|
||||
newHash := hashFlipBit(hash, bit)
|
||||
if hash.Cmp(&newHash) != hashBig.Cmp(HashToBig(&newHash)) {
|
||||
t.Errorf("Hash.Cmp disagrees with big.Int.Cmp newHash: %s, hash: %s", newHash, hash)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkHash_Cmp(b *testing.B) {
|
||||
hash0, err := NewHashFromStr("3333333333333333333333333333333333333333333333333333333333333333")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
for n := 0; n < b.N; n++ {
|
||||
hash0.Cmp(hash0)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user