gitlab.com/SiaPrime/SiaPrime@v1.4.1/modules/renter/contractor/dependencies.go (about) 1 package contractor 2 3 import ( 4 "path/filepath" 5 6 "gitlab.com/SiaPrime/SiaPrime/modules" 7 "gitlab.com/SiaPrime/SiaPrime/persist" 8 "gitlab.com/SiaPrime/SiaPrime/types" 9 ) 10 11 // These interfaces define the HostDB's dependencies. Using the smallest 12 // interface possible makes it easier to mock these dependencies in testing. 13 type ( 14 consensusSet interface { 15 ConsensusSetSubscribe(modules.ConsensusSetSubscriber, modules.ConsensusChangeID, <-chan struct{}) error 16 Synced() bool 17 Unsubscribe(modules.ConsensusSetSubscriber) 18 } 19 // In order to restrict the modules.TransactionBuilder interface, we must 20 // provide a shim to bridge the gap between modules.Wallet and 21 // transactionBuilder. 22 walletShim interface { 23 NextAddress() (types.UnlockConditions, error) 24 PrimarySeed() (modules.Seed, uint64, error) 25 StartTransaction() (modules.TransactionBuilder, error) 26 Unlocked() (bool, error) 27 } 28 wallet interface { 29 NextAddress() (types.UnlockConditions, error) 30 PrimarySeed() (modules.Seed, uint64, error) 31 StartTransaction() (transactionBuilder, error) 32 Unlocked() (bool, error) 33 } 34 transactionBuilder interface { 35 AddArbitraryData([]byte) uint64 36 AddFileContract(types.FileContract) uint64 37 AddMinerFee(types.Currency) uint64 38 AddParents([]types.Transaction) 39 AddSiacoinInput(types.SiacoinInput) uint64 40 AddSiacoinOutput(types.SiacoinOutput) uint64 41 AddTransactionSignature(types.TransactionSignature) uint64 42 Drop() 43 FundSiacoins(types.Currency) error 44 Sign(bool) ([]types.Transaction, error) 45 UnconfirmedParents() ([]types.Transaction, error) 46 View() (types.Transaction, []types.Transaction) 47 ViewAdded() (parents, coins, funds, signatures []int) 48 } 49 transactionPool interface { 50 AcceptTransactionSet([]types.Transaction) error 51 FeeEstimation() (min types.Currency, max types.Currency) 52 } 53 54 hostDB interface { 55 AllHosts() []modules.HostDBEntry 56 ActiveHosts() []modules.HostDBEntry 57 CheckForIPViolations([]types.SiaPublicKey) []types.SiaPublicKey 58 Filter() (modules.FilterMode, map[string]types.SiaPublicKey) 59 SetFilterMode(fm modules.FilterMode, hosts []types.SiaPublicKey) error 60 Host(types.SiaPublicKey) (modules.HostDBEntry, bool) 61 IncrementSuccessfulInteractions(key types.SiaPublicKey) 62 IncrementFailedInteractions(key types.SiaPublicKey) 63 RandomHosts(n int, blacklist, addressBlacklist []types.SiaPublicKey) ([]modules.HostDBEntry, error) 64 UpdateContracts([]modules.RenterContract) error 65 ScoreBreakdown(modules.HostDBEntry) (modules.HostScoreBreakdown, error) 66 SetAllowance(allowance modules.Allowance) error 67 IPViolationsCheck() bool 68 SetIPViolationCheck(enabled bool) 69 } 70 71 persister interface { 72 save(contractorPersist) error 73 load(*contractorPersist) error 74 } 75 ) 76 77 // WalletBridge is a bridge for the wallet because wallet is not directly 78 // compatible with modules.Wallet (wrong type signature for StartTransaction), 79 // we must provide a bridge type. 80 type WalletBridge struct { 81 W walletShim 82 } 83 84 // NextAddress computes and returns the next address of the wallet. 85 func (ws *WalletBridge) NextAddress() (types.UnlockConditions, error) { return ws.W.NextAddress() } 86 87 // PrimarySeed returns the primary wallet seed. 88 func (ws *WalletBridge) PrimarySeed() (modules.Seed, uint64, error) { return ws.W.PrimarySeed() } 89 90 // StartTransaction creates a new transactionBuilder that can be used to create 91 // and sign a transaction. 92 func (ws *WalletBridge) StartTransaction() (transactionBuilder, error) { return ws.W.StartTransaction() } 93 94 // Unlocked reports whether the wallet bridge is unlocked. 95 func (ws *WalletBridge) Unlocked() (bool, error) { return ws.W.Unlocked() } 96 97 // stdPersist implements the persister interface. The filename required by 98 // these functions is internal to stdPersist. 99 type stdPersist struct { 100 filename string 101 } 102 103 var persistMeta = persist.Metadata{ 104 Header: "Contractor Persistence", 105 Version: "1.3.1", 106 } 107 108 func (p *stdPersist) save(data contractorPersist) error { 109 return persist.SaveJSON(persistMeta, data, p.filename) 110 } 111 112 func (p *stdPersist) load(data *contractorPersist) error { 113 return persist.LoadJSON(persistMeta, &data, p.filename) 114 } 115 116 // NewPersist create a new stdPersist. 117 func NewPersist(dir string) *stdPersist { 118 return &stdPersist{ 119 filename: filepath.Join(dir, "contractor.json"), 120 } 121 }