Compare commits

..

2 Commits

Author SHA1 Message Date
stasatdaglabs
69d6148515 [NOD-1367] Fix race condition with notification listeners. 2020-09-10 15:19:22 +03:00
stasatdaglabs
b73d9f4045 [NOD-1367] Add an error handler to GRPCClient. 2020-09-10 14:08:19 +03:00
4 changed files with 2 additions and 54 deletions

View File

@@ -12,7 +12,6 @@ import (
"time"
)
// OnErrorHandler defines a handler function for when errors occur
type OnErrorHandler func(err error)
// GRPCClient is a gRPC-based RPC client
@@ -45,7 +44,6 @@ func (c *GRPCClient) Disconnect() error {
return c.stream.CloseSend()
}
// SetOnErrorHandler sets the client's onErrorHandler
func (c *GRPCClient) SetOnErrorHandler(onErrorHandler OnErrorHandler) {
c.onErrorHandler = onErrorHandler
}

View File

@@ -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", rpcAddress)
return nil, errors.Wrapf(err, "error connecting to address %s", rpcClient)
}
rpcRouter, err := buildRPCRouter()
if err != nil {

View File

@@ -224,16 +224,7 @@ func HashToBig(hash *Hash) *big.Int {
// +1 if hash > target
//
func (hash *Hash) Cmp(target *Hash) int {
// 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
return HashToBig(hash).Cmp(HashToBig(target))
}
// Less returns true iff hash a is less than hash b

View File

@@ -9,7 +9,6 @@ import (
"encoding/hex"
"errors"
"math/big"
"math/rand"
"reflect"
"testing"
)
@@ -426,43 +425,3 @@ 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)
}
}