// TestProcessConsensusUpdate tests that contracts are removed at the expected // block height. func TestProcessConsensusUpdate(t *testing.T) { // create contractor with a contract ending at height 20 var stub newStub var rc modules.RenterContract rc.LastRevision.NewWindowStart = 20 rc.FileContract.ValidProofOutputs = []types.SiacoinOutput{{}} c := &Contractor{ cs: stub, hdb: stub, contracts: map[types.FileContractID]modules.RenterContract{ rc.ID: rc, }, persist: new(memPersist), log: persist.NewLogger(ioutil.Discard), } // process 20 blocks; contract should remain cc := modules.ConsensusChange{ // just need to increment blockheight by 1 AppliedBlocks: []types.Block{{}}, } for i := 0; i < 20; i++ { c.ProcessConsensusChange(cc) } if len(c.contracts) != 1 { t.Error("expected 1 contract, got", len(c.contracts)) } // process one more block; contract should be removed c.ProcessConsensusChange(cc) if len(c.contracts) != 0 { t.Error("expected 0 contracts, got", len(c.contracts)) } }
// StartContinuousProfiling will continuously print statistics about the cpu // usage, memory usage, and runtime stats of the program. func StartContinuousProfile(profileDir string) { // Create the folder for all of the profiling results. err := os.MkdirAll(profileDir, 0700) if err != nil { fmt.Println(err) return } // Continuously log statistics about the running Sia application. go func() { // Create the logger. log, err := persist.NewLogger(filepath.Join(profileDir, "continuousProfiling.log")) if err != nil { fmt.Println("Profile logging failed:", err) return } // Collect statistics in an infinite loop. sleepTime := time.Second * 20 for { // Sleep for an exponential amount of time each iteration, this // keeps the size of the log small while still providing lots of // information. time.Sleep(sleepTime) sleepTime = time.Duration(1.5 * float64(sleepTime)) var m runtime.MemStats runtime.ReadMemStats(&m) log.Printf("\n\tGoroutines: %v\n\tAlloc: %v\n\tTotalAlloc: %v\n\tHeapAlloc: %v\n\tHeapSys: %v\n", runtime.NumGoroutine(), m.Alloc, m.TotalAlloc, m.HeapAlloc, m.HeapSys) } }() }
// bareHostDB returns a HostDB with its fields initialized, but without any // dependencies or scanning threads. It is only intended for use in unit tests. func bareHostDB() *HostDB { return &HostDB{ log: persist.NewLogger(ioutil.Discard), activeHosts: make(map[modules.NetAddress]*hostNode), allHosts: make(map[modules.NetAddress]*hostEntry), scanPool: make(chan *hostEntry, scanPoolSize), } }
// New returns an initialized Host. func New(cs modules.ConsensusSet, tpool modules.TransactionPool, wallet modules.Wallet, address string, persistDir string) (*Host, error) { // Check that all the dependencies were provided. if cs == nil { return nil, errNilCS } if tpool == nil { return nil, errNilTpool } if wallet == nil { return nil, errNilWallet } // Create the host object. h := &Host{ cs: cs, tpool: tpool, wallet: wallet, actionItems: make(map[types.BlockHeight]map[types.FileContractID]*contractObligation), obligationsByID: make(map[types.FileContractID]*contractObligation), persistDir: persistDir, } // Create the perist directory if it does not yet exist. err := os.MkdirAll(h.persistDir, 0700) if err != nil { return nil, err } // Initialize the logger. Logging should be initialized ASAP, because the // rest of the initialization makes use of the logger. h.log, err = persist.NewLogger(filepath.Join(h.persistDir, logFile)) if err != nil { return nil, err } // Load the prior persistance structures. err = h.load() if err != nil { return nil, err } // Get the host established on the network. err = h.initNetworking(address) if err != nil { return nil, err } return h, nil }
// initPersist initializes the persistence of the miner. func (m *Miner) initPersist() error { // Create the miner directory. err := os.MkdirAll(m.persistDir, 0700) if err != nil { return err } // Add a logger. m.log, err = persist.NewLogger(filepath.Join(m.persistDir, logFile)) if err != nil { return err } return m.initSettings() }
// initPersist loads all of the saved host state into the host object. If there // is no saved state, suitable defaults are chosen instead. func (h *Host) initPersist() error { // Create the perist directory if it does not yet exist. err := os.MkdirAll(h.persistDir, 0700) if err != nil { return err } // Initialize the logger. Logger must be initialized first, because the // rest of the initialization makes use of the logger. h.log, err = persist.NewLogger(filepath.Join(h.persistDir, logFile)) if err != nil { return err } // Load the prior persistance structures. err = h.load() if err != nil { return err } return nil }
// newLogger creates a persist.Logger with the standard filename. func newLogger(dir string) (*persist.Logger, error) { return persist.NewLogger(filepath.Join(dir, "hostdb.log")) }