github.com/Synthesix/Sia@v1.3.3-0.20180413141344-f863baeed3ca/modules/renter/contractor/dependencies.go (about) 1 package contractor 2 3 import ( 4 "path/filepath" 5 6 "github.com/Synthesix/Sia/modules" 7 "github.com/Synthesix/Sia/persist" 8 "github.com/Synthesix/Sia/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 StartTransaction() modules.TransactionBuilder 25 } 26 wallet interface { 27 NextAddress() (types.UnlockConditions, error) 28 StartTransaction() transactionBuilder 29 } 30 transactionBuilder interface { 31 AddArbitraryData([]byte) uint64 32 AddFileContract(types.FileContract) uint64 33 AddMinerFee(types.Currency) uint64 34 AddParents([]types.Transaction) 35 AddSiacoinInput(types.SiacoinInput) uint64 36 AddSiacoinOutput(types.SiacoinOutput) uint64 37 AddTransactionSignature(types.TransactionSignature) uint64 38 Drop() 39 FundSiacoins(types.Currency) error 40 Sign(bool) ([]types.Transaction, error) 41 View() (types.Transaction, []types.Transaction) 42 ViewAdded() (parents, coins, funds, signatures []int) 43 } 44 transactionPool interface { 45 AcceptTransactionSet([]types.Transaction) error 46 FeeEstimation() (min types.Currency, max types.Currency) 47 } 48 49 hostDB interface { 50 AllHosts() []modules.HostDBEntry 51 ActiveHosts() []modules.HostDBEntry 52 Host(types.SiaPublicKey) (modules.HostDBEntry, bool) 53 IncrementSuccessfulInteractions(key types.SiaPublicKey) 54 IncrementFailedInteractions(key types.SiaPublicKey) 55 RandomHosts(n int, exclude []types.SiaPublicKey) []modules.HostDBEntry 56 ScoreBreakdown(modules.HostDBEntry) modules.HostScoreBreakdown 57 } 58 59 persister interface { 60 save(contractorPersist) error 61 load(*contractorPersist) error 62 } 63 ) 64 65 // WalletBridge is a bridge for the wallet because wallet is not directly 66 // compatible with modules.Wallet (wrong type signature for StartTransaction), 67 // we must provide a bridge type. 68 type WalletBridge struct { 69 W walletShim 70 } 71 72 // NextAddress computes and returns the next address of the wallet. 73 func (ws *WalletBridge) NextAddress() (types.UnlockConditions, error) { return ws.W.NextAddress() } 74 75 // StartTransaction creates a new transactionBuilder that can be used to create 76 // and sign a transaction. 77 func (ws *WalletBridge) StartTransaction() transactionBuilder { return ws.W.StartTransaction() } 78 79 // stdPersist implements the persister interface. The filename required by 80 // these functions is internal to stdPersist. 81 type stdPersist struct { 82 filename string 83 } 84 85 var persistMeta = persist.Metadata{ 86 Header: "Contractor Persistence", 87 Version: "1.3.1", 88 } 89 90 func (p *stdPersist) save(data contractorPersist) error { 91 return persist.SaveJSON(persistMeta, data, p.filename) 92 } 93 94 func (p *stdPersist) load(data *contractorPersist) error { 95 return persist.LoadJSON(persistMeta, &data, p.filename) 96 } 97 98 // NewPersist create a new stdPersist. 99 func NewPersist(dir string) *stdPersist { 100 return &stdPersist{ 101 filename: filepath.Join(dir, "contractor.json"), 102 } 103 }