kaspad/blockdag/notifications_test.go
stasatdaglabs 4be23bff07 [DEV-348] Don't send transactions of wrong sub-network to partial peers, and reject messages of such (#164)
* [DEV-348] Removed a couple of unused methods.

* [DEV-348] Implemented validating incoming transactions for bad partial transactions.

* [DEV-348] Added a (incomplete) filter for propogation of transactions.

* [DEV-348] Implemented filtering inventory by subnetwork.

* [DEV-348] Fixed broken tests.

* [DEV-348] Added test for non-zero payload partial transactions.

* [DEV-348] Added a comment for Config.SubnetworkID.

* [DEV-348] Fixed formatting.

* [DEV-348] Renamed isRemoteTransactionFull to shouldTxBeFull.

* [DEV-348] Added a check for invalid transaction in maybeAcceptTransaction. Added handling for native networks.

* [DEV-348] Fixed formatting.

* [DEV-348] Fixed a bug in transaction validation.

* [DEV-348] Rephrased a comment.

* [DEV-348] Extracted subnetwork compatibility to a method. Wrote a test for it.

* [DEV-348] Removed an unnecessary check over the native subnetwork.
2019-01-22 13:56:40 +02:00

59 lines
1.5 KiB
Go

// Copyright (c) 2017 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package blockdag
import (
"github.com/daglabs/btcd/wire"
"testing"
"github.com/daglabs/btcd/dagconfig"
)
// TestNotifications ensures that notification callbacks are fired on events.
func TestNotifications(t *testing.T) {
blocks, err := loadBlocks("blk_0_to_4.dat")
if err != nil {
t.Fatalf("Error loading file: %v\n", err)
}
// Create a new database and dag instance to run tests against.
dag, teardownFunc, err := DAGSetup("notifications", Config{
DAGParams: &dagconfig.SimNetParams,
SubnetworkID: &wire.SubnetworkIDSupportsAll,
})
if err != nil {
t.Fatalf("Failed to setup dag instance: %v", err)
}
defer teardownFunc()
notificationCount := 0
callback := func(notification *Notification) {
if notification.Type == NTBlockAccepted {
notificationCount++
}
}
// Register callback multiple times then assert it is called that many
// times.
const numSubscribers = 3
for i := 0; i < numSubscribers; i++ {
dag.Subscribe(callback)
}
isOrphan, err := dag.ProcessBlock(blocks[1], BFNone)
if isOrphan {
t.Fatalf("ProcessBlock incorrectly returned block " +
"is an orphan\n")
}
if err != nil {
t.Fatalf("ProcessBlock fail on block 1: %v\n", err)
}
if notificationCount != numSubscribers {
t.Fatalf("Expected notification callback to be executed %d "+
"times, found %d", numSubscribers, notificationCount)
}
}