github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/renter/hostdb/update.go (about) 1 package hostdb 2 3 import ( 4 "github.com/NebulousLabs/Sia/modules" 5 "github.com/NebulousLabs/Sia/types" 6 ) 7 8 // findHostAnnouncements returns a list of the host announcements found within 9 // a given block. No check is made to see that the ip address found in the 10 // announcement is actually a valid ip address. 11 func findHostAnnouncements(b types.Block) (announcements []modules.HostDBEntry) { 12 for _, t := range b.Transactions { 13 // the HostAnnouncement must be prefaced by the standard host 14 // announcement string 15 for _, arb := range t.ArbitraryData { 16 addr, pubKey, err := modules.DecodeAnnouncement(arb) 17 if err != nil { 18 continue 19 } 20 21 // Add the announcement to the slice being returned. 22 var host modules.HostDBEntry 23 host.NetAddress = addr 24 host.PublicKey = pubKey 25 announcements = append(announcements, host) 26 } 27 } 28 return 29 } 30 31 // ProcessConsensusChange will be called by the consensus set every time there 32 // is a change in the blockchain. Updates will always be called in order. 33 func (hdb *HostDB) ProcessConsensusChange(cc modules.ConsensusChange) { 34 hdb.mu.Lock() 35 defer hdb.mu.Unlock() 36 37 if hdb.blockHeight != 0 || cc.AppliedBlocks[len(cc.AppliedBlocks)-1].ID() != types.GenesisID { 38 hdb.blockHeight += types.BlockHeight(len(cc.AppliedBlocks)) 39 hdb.blockHeight -= types.BlockHeight(len(cc.RevertedBlocks)) 40 } 41 42 // Add hosts announced in blocks that were applied. 43 for _, block := range cc.AppliedBlocks { 44 for _, host := range findHostAnnouncements(block) { 45 hdb.log.Debugln("Found a host in a host announcement:", host.NetAddress, host.PublicKey.Key) 46 hdb.insertHost(host) 47 } 48 } 49 50 hdb.lastChange = cc.ID 51 err := hdb.save() 52 if err != nil { 53 hdb.log.Println(err) 54 } 55 }