github.com/nebulouslabs/sia@v1.3.7/modules/renter/contractor/dependencies.go (about)

     1  package contractor
     2  
     3  import (
     4  	"path/filepath"
     5  
     6  	"github.com/NebulousLabs/Sia/modules"
     7  	"github.com/NebulousLabs/Sia/persist"
     8  	"github.com/NebulousLabs/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, 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  		Host(types.SiaPublicKey) (modules.HostDBEntry, bool)
    54  		IncrementSuccessfulInteractions(key types.SiaPublicKey)
    55  		IncrementFailedInteractions(key types.SiaPublicKey)
    56  		RandomHosts(n int, exclude []types.SiaPublicKey) ([]modules.HostDBEntry, error)
    57  		ScoreBreakdown(modules.HostDBEntry) modules.HostScoreBreakdown
    58  	}
    59  
    60  	persister interface {
    61  		save(contractorPersist) error
    62  		load(*contractorPersist) error
    63  	}
    64  )
    65  
    66  // WalletBridge is a bridge for the wallet because wallet is not directly
    67  // compatible with modules.Wallet (wrong type signature for StartTransaction),
    68  // we must provide a bridge type.
    69  type WalletBridge struct {
    70  	W walletShim
    71  }
    72  
    73  // NextAddress computes and returns the next address of the wallet.
    74  func (ws *WalletBridge) NextAddress() (types.UnlockConditions, error) { return ws.W.NextAddress() }
    75  
    76  // StartTransaction creates a new transactionBuilder that can be used to create
    77  // and sign a transaction.
    78  func (ws *WalletBridge) StartTransaction() (transactionBuilder, error) { return ws.W.StartTransaction() }
    79  
    80  // stdPersist implements the persister interface. The filename required by
    81  // these functions is internal to stdPersist.
    82  type stdPersist struct {
    83  	filename string
    84  }
    85  
    86  var persistMeta = persist.Metadata{
    87  	Header:  "Contractor Persistence",
    88  	Version: "1.3.1",
    89  }
    90  
    91  func (p *stdPersist) save(data contractorPersist) error {
    92  	return persist.SaveJSON(persistMeta, data, p.filename)
    93  }
    94  
    95  func (p *stdPersist) load(data *contractorPersist) error {
    96  	return persist.LoadJSON(persistMeta, &data, p.filename)
    97  }
    98  
    99  // NewPersist create a new stdPersist.
   100  func NewPersist(dir string) *stdPersist {
   101  	return &stdPersist{
   102  		filename: filepath.Join(dir, "contractor.json"),
   103  	}
   104  }