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