diff --git a/packages/daemon/daemon.go b/packages/daemon/daemon.go index d6961424582e40699ebe97b69f56593b5b61d9d8..2e6aaaddb1a00ee656174713f09764417d67477f 100644 --- a/packages/daemon/daemon.go +++ b/packages/daemon/daemon.go @@ -58,7 +58,7 @@ func BackgroundWorker(name string, handler func()) { lock.Unlock() } -func Run() { +func Start() { if !running { lock.Lock() @@ -76,6 +76,10 @@ func Run() { lock.Unlock() } +} + +func Run() { + Start() wg.Wait() } @@ -96,6 +100,24 @@ func Shutdown() { } } +func ShutdownAndWait() { + if running { + lock.Lock() + + if running { + close(ShutdownSignal) + + running = false + + Events.Shutdown.Trigger() + } + + lock.Unlock() + } + + wg.Wait() +} + func IsRunning() bool { return running } diff --git a/packages/node/node.go b/packages/node/node.go index bedaa4909af256682e5dee53185fa1f4d9bf6c29..9f86209f5e259cbe9e79f50efc6b057e56246eab 100644 --- a/packages/node/node.go +++ b/packages/node/node.go @@ -28,6 +28,13 @@ func Load(plugins ...*Plugin) *Node { return node } +func Start(plugins ...*Plugin) *Node { + node := Load(plugins...) + node.Start() + + return node +} + func Run(plugins ...*Plugin) *Node { node := Load(plugins...) node.Run() @@ -35,6 +42,10 @@ func Run(plugins ...*Plugin) *Node { return node } +func Shutdown() { + daemon.ShutdownAndWait() +} + func (node *Node) AddLogger(logger *Logger) { node.loggers = append(node.loggers, logger) } @@ -106,6 +117,22 @@ func (node *Node) Load(plugins ...*Plugin) { } } +func (node *Node) Start() { + node.LogInfo("Node", "Executing plugins ...") + + if len(node.loadedPlugins) >= 1 { + for _, plugin := range node.loadedPlugins { + plugin.Events.Run.Trigger(plugin) + + node.LogSuccess("Node", "Starting Plugin: "+plugin.Name+" ... done") + } + } + + node.LogSuccess("Node", "Starting background workers ...") + + daemon.Start() +} + func (node *Node) Run() { node.LogInfo("Node", "Executing plugins ...") diff --git a/packages/node/plugin.go b/packages/node/plugin.go index 1a6cb527a5eb3d5cd3aa0488e3ce22d2b7fb74e5..a98a22beff880d7b73b210c14e38f0f6730a1ce1 100644 --- a/packages/node/plugin.go +++ b/packages/node/plugin.go @@ -55,16 +55,3 @@ func (plugin *Plugin) LogFailure(message string) { func (plugin *Plugin) LogDebug(message string) { plugin.Node.LogDebug(plugin.Name, message) } - -var TestNode = &Node{ - loggers: make([]*Logger, 0), - wg: &sync.WaitGroup{}, - loadedPlugins: make([]*Plugin, 0), -} - -func (plugin *Plugin) InitTest() { - plugin.Node = TestNode - - plugin.Events.Configure.Trigger(plugin) - plugin.Events.Run.Trigger(plugin) -} diff --git a/packages/transactionspammer/transactionspammer.go b/packages/transactionspammer/transactionspammer.go index c59c6f544f888b83025376fc9b4e0793fb5e83ad..777671a2e5fb8338f34de4baa08c44ba73a4baf0 100644 --- a/packages/transactionspammer/transactionspammer.go +++ b/packages/transactionspammer/transactionspammer.go @@ -16,7 +16,9 @@ var startMutex sync.Mutex var shutdownSignal chan int -func Start(tps int64) { +var sentCounter = uint(0) + +func Start(tps uint) { startMutex.Lock() if !spamming { @@ -26,7 +28,6 @@ func Start(tps int64) { daemon.BackgroundWorker("Transaction Spammer", func() { for { start := time.Now() - sentCounter := int64(0) totalSentCounter := int64(0) for { @@ -81,3 +82,25 @@ func Stop() { startMutex.Unlock() } + +func GenerateBundle(bundleLength int) (result []*value_transaction.ValueTransaction) { + result = make([]*value_transaction.ValueTransaction, bundleLength) + + branch := tipselection.GetRandomTip() + trunk := tipselection.GetRandomTip() + + for i := 0; i < bundleLength; i++ { + tx := value_transaction.New() + tx.SetTail(i == 0) + tx.SetHead(i == bundleLength - 1) + tx.SetTimestamp(sentCounter) + tx.SetBranchTransactionHash(branch) + tx.SetTrunkTransactionHash(trunk) + + result[i] = tx + + trunk = tx.GetHash() + } + + return result +} diff --git a/plugins/bundleprocessor/bundleprocessor_test.go b/plugins/bundleprocessor/bundleprocessor_test.go index ee4cc3d467d3b7fe3c9a01cd9767d373deea87d3..a4853715920a651d6f96a3f47b7e452ee6a506db 100644 --- a/plugins/bundleprocessor/bundleprocessor_test.go +++ b/plugins/bundleprocessor/bundleprocessor_test.go @@ -4,6 +4,8 @@ import ( "fmt" "testing" + "github.com/iotaledger/goshimmer/packages/node" + "github.com/iotaledger/goshimmer/packages/events" "github.com/iotaledger/goshimmer/packages/model/bundle" "github.com/iotaledger/goshimmer/packages/model/value_transaction" @@ -13,7 +15,8 @@ import ( ) func TestProcessSolidBundleHead(t *testing.T) { - tangle.PLUGIN.InitTest() + // start a test node + node.Start(tangle.PLUGIN, PLUGIN) tx := value_transaction.New() tx.SetTail(true) @@ -39,4 +42,7 @@ func TestProcessSolidBundleHead(t *testing.T) { assert.Equal(t, result.GetHash(), trinary.Trytes("UFWJYEWKMEQDNSQUCUWBGOFRHVBGHVVYEZCLCGRDTRQSMAFALTIPMJEEYFDPMQCNJWLXUWFMBZGHQRO99"), "invalid bundle hash") assert.Equal(t, result.IsValueBundle(), true, "invalid value bundle status") } + + // shutdown test node + node.Shutdown() } diff --git a/plugins/tangle/bundle.go b/plugins/tangle/bundle.go index 759041c421244185678cd9adc2d61fdf2b60f193..4907c44c1556e12f2b780171a836ec234dc0dfb6 100644 --- a/plugins/tangle/bundle.go +++ b/plugins/tangle/bundle.go @@ -81,7 +81,7 @@ const ( var bundleDatabase database.Database func configureBundleDatabase(plugin *node.Plugin) { - if db, err := database.Get("bundle"); err != nil { + if db, err := database.Get("bundles"); err != nil { panic(err) } else { bundleDatabase = db diff --git a/plugins/tangle/solidifier_test.go b/plugins/tangle/solidifier_test.go index c7b04e9f39d659ad3821d33b4fd5f68989d23218..9db5e7c89e5b91659264d7907ac02acdd6cdc435 100644 --- a/plugins/tangle/solidifier_test.go +++ b/plugins/tangle/solidifier_test.go @@ -6,16 +6,17 @@ import ( "github.com/iotaledger/goshimmer/packages/events" "github.com/iotaledger/goshimmer/packages/model/value_transaction" + "github.com/iotaledger/goshimmer/packages/node" "github.com/iotaledger/goshimmer/plugins/gossip" "github.com/iotaledger/iota.go/trinary" ) func TestSolidifier(t *testing.T) { - // initialize plugin - configureTransactionDatabase(nil) - configureTransactionMetaDataDatabase(nil) - configureApproversDatabase(nil) - configureSolidifier(nil) + // show all error messages for tests + *node.LOG_LEVEL.Value = node.LOG_LEVEL_DEBUG + + // start a test node + node.Start(PLUGIN) // create transactions and chain them together transaction1 := value_transaction.New() @@ -42,4 +43,7 @@ func TestSolidifier(t *testing.T) { // wait until all are solid wg.Wait() + + // shutdown test node + node.Shutdown() }