github.com/johnathanhowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/explorer/explorer.go (about) 1 // The explorer module provides a glimpse into what the Sia network 2 // currently looks like. 3 package explorer 4 5 import ( 6 "errors" 7 8 "github.com/NebulousLabs/Sia/modules" 9 "github.com/NebulousLabs/Sia/persist" 10 "github.com/NebulousLabs/Sia/types" 11 ) 12 13 const ( 14 // hashrateEstimationBlocks is the number of blocks that are used to 15 // estimate the current hashrate. 16 hashrateEstimationBlocks = 200 // 33 hours 17 ) 18 19 var ( 20 errNilCS = errors.New("explorer cannot use a nil consensus set") 21 ) 22 23 type ( 24 // fileContractHistory stores the original file contract and the chain of 25 // revisions that have affected a file contract through the life of the 26 // blockchain. 27 fileContractHistory struct { 28 Contract types.FileContract 29 Revisions []types.FileContractRevision 30 StorageProof types.StorageProof 31 } 32 33 // blockFacts contains a set of facts about the consensus set related to a 34 // certain block. The explorer needs some additional information in the 35 // history so that it can calculate certain values, which is one of the 36 // reasons that the explorer uses a separate struct instead of 37 // modules.BlockFacts. 38 blockFacts struct { 39 modules.BlockFacts 40 41 Timestamp types.Timestamp 42 } 43 44 // An Explorer contains a more comprehensive view of the blockchain, 45 // including various statistics and metrics. 46 Explorer struct { 47 cs modules.ConsensusSet 48 db *persist.BoltDatabase 49 persistDir string 50 } 51 ) 52 53 // New creates the internal data structures, and subscribes to 54 // consensus for changes to the blockchain 55 func New(cs modules.ConsensusSet, persistDir string) (*Explorer, error) { 56 // Check that input modules are non-nil 57 if cs == nil { 58 return nil, errNilCS 59 } 60 61 // Initialize the explorer. 62 e := &Explorer{ 63 cs: cs, 64 persistDir: persistDir, 65 } 66 67 // Initialize the persistent structures, including the database. 68 err := e.initPersist() 69 if err != nil { 70 return nil, err 71 } 72 73 // retrieve the current ConsensusChangeID 74 var recentChange modules.ConsensusChangeID 75 err = e.db.View(dbGetInternal(internalRecentChange, &recentChange)) 76 if err != nil { 77 return nil, err 78 } 79 80 err = cs.ConsensusSetSubscribe(e, recentChange) 81 if err != nil { 82 // TODO: restart from 0 83 return nil, errors.New("explorer subscription failed: " + err.Error()) 84 } 85 86 return e, nil 87 } 88 89 // Close closes the explorer. 90 func (e *Explorer) Close() error { 91 return e.db.Close() 92 }