From 7fa48aa313cef9b6b5c3f139740d3d819c7273e6 Mon Sep 17 00:00:00 2001 From: Hans Moog <hm@mkjc.net> Date: Mon, 17 Jun 2019 13:44:56 +0200 Subject: [PATCH] Feat: started porting approvers to model package + fixed some issues --- packages/datastructure/lru_cache.go | 4 +++- packages/filter/byte_array_filter.go | 8 +++---- packages/model/approvers/approvers.go | 22 +++++++++++++++++++ .../typeutils.go} | 6 ++++- plugins/gossip/plugin.go | 6 ----- plugins/gossip/send_queue.go | 2 +- plugins/tangle/approvers.go | 6 ++--- plugins/tangle/transaction_metadata.go | 4 ++-- 8 files changed, 40 insertions(+), 18 deletions(-) create mode 100644 packages/model/approvers/approvers.go rename packages/{typeconversion/typeconversion.go => typeutils/typeutils.go} (74%) diff --git a/packages/datastructure/lru_cache.go b/packages/datastructure/lru_cache.go index c07da3ba..f2701bcc 100644 --- a/packages/datastructure/lru_cache.go +++ b/packages/datastructure/lru_cache.go @@ -2,6 +2,8 @@ package datastructure import ( "sync" + + "github.com/iotaledger/goshimmer/packages/typeutils" ) type lruCacheElement struct { @@ -101,7 +103,7 @@ func (cache *LRUCache) ComputeIfPresent(key interface{}, callback func(value int if entry, exists := cache.directory[key]; exists { result = entry.GetValue().(*lruCacheElement).value - if callbackResult := callback(result); result != nil { + if callbackResult := callback(result); !typeutils.IsInterfaceNil(callbackResult) { result = callbackResult cache.set(key, callbackResult) diff --git a/packages/filter/byte_array_filter.go b/packages/filter/byte_array_filter.go index ba5aca0a..d8cb89fe 100644 --- a/packages/filter/byte_array_filter.go +++ b/packages/filter/byte_array_filter.go @@ -3,7 +3,7 @@ package filter import ( "sync" - "github.com/iotaledger/goshimmer/packages/typeconversion" + "github.com/iotaledger/goshimmer/packages/typeutils" ) type ByteArrayFilter struct { @@ -25,20 +25,20 @@ func (filter *ByteArrayFilter) Contains(byteArray []byte) bool { filter.mutex.RLock() defer filter.mutex.RUnlock() - _, exists := filter.byteArraysByKey[typeconversion.BytesToString(byteArray)] + _, exists := filter.byteArraysByKey[typeutils.BytesToString(byteArray)] return exists } func (filter *ByteArrayFilter) Add(byteArray []byte) bool { - key := typeconversion.BytesToString(byteArray) + key := typeutils.BytesToString(byteArray) filter.mutex.Lock() defer filter.mutex.Unlock() if _, exists := filter.byteArraysByKey[key]; !exists { if len(filter.byteArrays) == filter.size { - delete(filter.byteArraysByKey, typeconversion.BytesToString(filter.byteArrays[0])) + delete(filter.byteArraysByKey, typeutils.BytesToString(filter.byteArrays[0])) filter.byteArrays = append(filter.byteArrays[1:], byteArray) } else { diff --git a/packages/model/approvers/approvers.go b/packages/model/approvers/approvers.go new file mode 100644 index 00000000..be5f7530 --- /dev/null +++ b/packages/model/approvers/approvers.go @@ -0,0 +1,22 @@ +package approvers + +import ( + "sync" + + "github.com/iotaledger/goshimmer/packages/ternary" +) + +type Approvers struct { + hash ternary.Trinary + hashes map[ternary.Trinary]bool + hashesMutex sync.RWMutex + modified bool +} + +func NewApprovers(hash ternary.Trinary) *Approvers { + return &Approvers{ + hash: hash, + hashes: make(map[ternary.Trinary]bool), + modified: false, + } +} diff --git a/packages/typeconversion/typeconversion.go b/packages/typeutils/typeutils.go similarity index 74% rename from packages/typeconversion/typeconversion.go rename to packages/typeutils/typeutils.go index ed053ee8..e59ebe2e 100644 --- a/packages/typeconversion/typeconversion.go +++ b/packages/typeutils/typeutils.go @@ -1,4 +1,4 @@ -package typeconversion +package typeutils import ( "reflect" @@ -16,3 +16,7 @@ func StringToBytes(str string) []byte { return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{Data: hdr.Data, Len: hdr.Len, Cap: hdr.Len})) } + +func IsInterfaceNil(param interface{}) bool { + return param == nil || (*[2]uintptr)(unsafe.Pointer(¶m))[1] == 0 +} diff --git a/plugins/gossip/plugin.go b/plugins/gossip/plugin.go index df702928..0f02bd14 100644 --- a/plugins/gossip/plugin.go +++ b/plugins/gossip/plugin.go @@ -1,8 +1,6 @@ package gossip import ( - "github.com/iotaledger/goshimmer/packages/events" - "github.com/iotaledger/goshimmer/packages/model/meta_transaction" "github.com/iotaledger/goshimmer/packages/node" ) @@ -12,10 +10,6 @@ func configure(plugin *node.Plugin) { configureNeighbors(plugin) configureServer(plugin) configureSendQueue(plugin) - - Events.ReceiveTransaction.Attach(events.NewClosure(func(tx *meta_transaction.MetaTransaction) { - plugin.LogDebug("Received TX " + string(tx.GetHash())) - })) } func run(plugin *node.Plugin) { diff --git a/plugins/gossip/send_queue.go b/plugins/gossip/send_queue.go index 354c0c3b..1945b7e4 100644 --- a/plugins/gossip/send_queue.go +++ b/plugins/gossip/send_queue.go @@ -150,7 +150,7 @@ var connectedNeighborsMutex sync.RWMutex var sendQueue = make(chan *meta_transaction.MetaTransaction, SEND_QUEUE_SIZE) const ( - SEND_QUEUE_SIZE = 500 + SEND_QUEUE_SIZE = 50000 ) // endregion /////////////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/plugins/tangle/approvers.go b/plugins/tangle/approvers.go index cdf3c7b4..f10291d4 100644 --- a/plugins/tangle/approvers.go +++ b/plugins/tangle/approvers.go @@ -9,7 +9,7 @@ import ( "github.com/iotaledger/goshimmer/packages/datastructure" "github.com/iotaledger/goshimmer/packages/errors" "github.com/iotaledger/goshimmer/packages/ternary" - "github.com/iotaledger/goshimmer/packages/typeconversion" + "github.com/iotaledger/goshimmer/packages/typeutils" ) // region global public api //////////////////////////////////////////////////////////////////////////////////////////// @@ -132,13 +132,13 @@ func (approvers *Approvers) Unmarshal(data []byte) (err errors.IdentifiableError approvers.hashesMutex.Lock() - approvers.hash = ternary.Trinary(typeconversion.BytesToString(data[MARSHALLED_APPROVERS_HASH_START:MARSHALLED_APPROVERS_HASH_END])) + approvers.hash = ternary.Trinary(typeutils.BytesToString(data[MARSHALLED_APPROVERS_HASH_START:MARSHALLED_APPROVERS_HASH_END])) approvers.hashes = make(map[ternary.Trinary]bool, hashesCount) for i := uint64(0); i < hashesCount; i++ { var HASH_START = MARSHALLED_APPROVERS_HASHES_START + i*(MARSHALLED_APPROVERS_HASH_SIZE) var HASH_END = HASH_START * MARSHALLED_APPROVERS_HASH_SIZE - approvers.hashes[ternary.Trinary(typeconversion.BytesToString(data[HASH_START:HASH_END]))] = true + approvers.hashes[ternary.Trinary(typeutils.BytesToString(data[HASH_START:HASH_END]))] = true } approvers.hashesMutex.Unlock() diff --git a/plugins/tangle/transaction_metadata.go b/plugins/tangle/transaction_metadata.go index c617f0c9..4b0dc22d 100644 --- a/plugins/tangle/transaction_metadata.go +++ b/plugins/tangle/transaction_metadata.go @@ -7,7 +7,7 @@ import ( "github.com/iotaledger/goshimmer/packages/bitutils" "github.com/iotaledger/goshimmer/packages/errors" "github.com/iotaledger/goshimmer/packages/ternary" - "github.com/iotaledger/goshimmer/packages/typeconversion" + "github.com/iotaledger/goshimmer/packages/typeutils" ) // region type definition and constructor ////////////////////////////////////////////////////////////////////////////// @@ -228,7 +228,7 @@ func (metadata *TransactionMetadata) Unmarshal(data []byte) errors.IdentifiableE metadata.finalizedMutex.Lock() defer metadata.finalizedMutex.Unlock() - metadata.hash = ternary.Trinary(typeconversion.BytesToString(data[MARSHALLED_HASH_START:MARSHALLED_HASH_END])) + metadata.hash = ternary.Trinary(typeutils.BytesToString(data[MARSHALLED_HASH_START:MARSHALLED_HASH_END])) if err := metadata.receivedTime.UnmarshalBinary(data[MARSHALLED_RECEIVED_TIME_START:MARSHALLED_RECEIVED_TIME_END]); err != nil { return ErrUnmarshalFailed.Derive(err, "could not unmarshal the received time") -- GitLab