github.com/johnathanhowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/host/announce_test.go (about)

     1  package host
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/NebulousLabs/Sia/modules"
     8  	"github.com/NebulousLabs/Sia/types"
     9  )
    10  
    11  // announcementFinder is a quick module that parses the blockchain for host
    12  // announcements, keeping a record of all the announcements that get found.
    13  type announcementFinder struct {
    14  	cs modules.ConsensusSet
    15  
    16  	// Announcements that have been seen. The two slices are wedded.
    17  	netAddresses []modules.NetAddress
    18  	publicKeys   []types.SiaPublicKey
    19  }
    20  
    21  // ProcessConsensusChange receives consensus changes from the consensus set and
    22  // parses them for valid host announcements.
    23  func (af *announcementFinder) ProcessConsensusChange(cc modules.ConsensusChange) {
    24  	for _, block := range cc.AppliedBlocks {
    25  		for _, txn := range block.Transactions {
    26  			for _, arb := range txn.ArbitraryData {
    27  				addr, pubKey, err := modules.DecodeAnnouncement(arb)
    28  				if err == nil {
    29  					af.netAddresses = append(af.netAddresses, addr)
    30  					af.publicKeys = append(af.publicKeys, pubKey)
    31  				}
    32  			}
    33  		}
    34  	}
    35  }
    36  
    37  // Close will shut down the announcement finder.
    38  func (af *announcementFinder) Close() error {
    39  	af.cs.Unsubscribe(af)
    40  	return nil
    41  }
    42  
    43  // newAnnouncementFinder will create and return an announcement finder.
    44  func newAnnouncementFinder(cs modules.ConsensusSet) (*announcementFinder, error) {
    45  	af := &announcementFinder{
    46  		cs: cs,
    47  	}
    48  	err := cs.ConsensusSetSubscribe(af, modules.ConsensusChangeBeginning)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	return af, nil
    53  }
    54  
    55  // TestHostAnnounce checks that the host announce function is operating
    56  // correctly.
    57  func TestHostAnnounce(t *testing.T) {
    58  	if testing.Short() {
    59  		t.SkipNow()
    60  	}
    61  	t.Parallel()
    62  	ht, err := newHostTester("TestHostAnnounce")
    63  	if err != nil {
    64  		t.Fatal(err)
    65  	}
    66  
    67  	// Create an announcement finder to scan the blockchain for host
    68  	// announcements.
    69  	af, err := newAnnouncementFinder(ht.cs)
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  	defer af.Close()
    74  
    75  	// Create an announcement, then use the address finding module to scan the
    76  	// blockchain for the host's address.
    77  	err = ht.host.Announce()
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  	_, err = ht.miner.AddBlock()
    82  	if err != nil {
    83  		t.Fatal(err)
    84  	}
    85  	if len(af.publicKeys) != 1 {
    86  		t.Fatal("could not find host announcement in blockchain")
    87  	}
    88  	if af.netAddresses[0] != ht.host.autoAddress {
    89  		t.Error("announcement has wrong address")
    90  	}
    91  	if !bytes.Equal(af.publicKeys[0].Key, ht.host.publicKey.Key) {
    92  		t.Error("announcement has wrong host key")
    93  	}
    94  }
    95  
    96  // TestHostAnnounceAddress checks that the host announce address function is
    97  // operating correctly.
    98  func TestHostAnnounceAddress(t *testing.T) {
    99  	if testing.Short() {
   100  		t.SkipNow()
   101  	}
   102  	t.Parallel()
   103  	ht, err := newHostTester("TestHostAnnounceAddress")
   104  	if err != nil {
   105  		t.Fatal(err)
   106  	}
   107  
   108  	// Create an announcement finder to scan the blockchain for host
   109  	// announcements.
   110  	af, err := newAnnouncementFinder(ht.cs)
   111  	if err != nil {
   112  		t.Fatal(err)
   113  	}
   114  	defer af.Close()
   115  
   116  	// Create an announcement, then use the address finding module to scan the
   117  	// blockchain for the host's address.
   118  	addr := modules.NetAddress("foo.com:1234")
   119  	err = ht.host.AnnounceAddress(addr)
   120  	if err != nil {
   121  		t.Fatal(err)
   122  	}
   123  	_, err = ht.miner.AddBlock()
   124  	if err != nil {
   125  		t.Fatal(err)
   126  	}
   127  	if len(af.netAddresses) != 1 {
   128  		t.Fatal("could not find host announcement in blockchain")
   129  	}
   130  	if af.netAddresses[0] != addr {
   131  		t.Error("announcement has wrong address")
   132  	}
   133  	if !bytes.Equal(af.publicKeys[0].Key, ht.host.publicKey.Key) {
   134  		t.Error("announcement has wrong host key")
   135  	}
   136  }