gitlab.com/jokerrs1/Sia@v1.3.2/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
    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  // Because wallet is not directly compatible with modules.Wallet (wrong
    66  // type signature for StartTransaction), we must provide a bridge type.
    67  type walletBridge struct {
    68  	w walletShim
    69  }
    70  
    71  func (ws *walletBridge) NextAddress() (types.UnlockConditions, error) { return ws.w.NextAddress() }
    72  func (ws *walletBridge) StartTransaction() transactionBuilder         { return ws.w.StartTransaction() }
    73  
    74  // stdPersist implements the persister interface. The filename required by
    75  // these functions is internal to stdPersist.
    76  type stdPersist struct {
    77  	filename string
    78  }
    79  
    80  var persistMeta = persist.Metadata{
    81  	Header:  "Contractor Persistence",
    82  	Version: "1.3.1",
    83  }
    84  
    85  func (p *stdPersist) save(data contractorPersist) error {
    86  	return persist.SaveJSON(persistMeta, data, p.filename)
    87  }
    88  
    89  func (p *stdPersist) load(data *contractorPersist) error {
    90  	return persist.LoadJSON(persistMeta, &data, p.filename)
    91  }
    92  
    93  func newPersist(dir string) *stdPersist {
    94  	return &stdPersist{
    95  		filename: filepath.Join(dir, "contractor.json"),
    96  	}
    97  }