github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/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) error
    16  	}
    17  	// in order to restrict the modules.TransactionBuilder interface, we must
    18  	// provide a shim to bridge the gap between modules.Wallet and
    19  	// transactionBuilder.
    20  	walletShim interface {
    21  		NextAddress() (types.UnlockConditions, error)
    22  		StartTransaction() modules.TransactionBuilder
    23  	}
    24  	wallet interface {
    25  		NextAddress() (types.UnlockConditions, error)
    26  		StartTransaction() transactionBuilder
    27  	}
    28  	transactionBuilder interface {
    29  		AddArbitraryData([]byte) uint64
    30  		AddFileContract(types.FileContract) uint64
    31  		AddMinerFee(types.Currency) uint64
    32  		AddParents([]types.Transaction)
    33  		AddSiacoinInput(types.SiacoinInput) uint64
    34  		AddSiacoinOutput(types.SiacoinOutput) uint64
    35  		AddTransactionSignature(types.TransactionSignature) uint64
    36  		Drop()
    37  		FundSiacoins(types.Currency) error
    38  		Sign(bool) ([]types.Transaction, error)
    39  		View() (types.Transaction, []types.Transaction)
    40  		ViewAdded() (parents, coins, funds, signatures []int)
    41  	}
    42  	transactionPool interface {
    43  		AcceptTransactionSet([]types.Transaction) error
    44  		FeeEstimation() (min types.Currency, max types.Currency)
    45  	}
    46  
    47  	hostDB interface {
    48  		Host(modules.NetAddress) (modules.HostDBEntry, bool)
    49  		RandomHosts(n int, exclude []modules.NetAddress) []modules.HostDBEntry
    50  	}
    51  
    52  	persister interface {
    53  		save(contractorPersist) error
    54  		saveSync(contractorPersist) error
    55  		load(*contractorPersist) error
    56  	}
    57  )
    58  
    59  // because wallet is not directly compatible with modules.Wallet (wrong
    60  // type signature for StartTransaction), we must provide a bridge type.
    61  type walletBridge struct {
    62  	w walletShim
    63  }
    64  
    65  func (ws *walletBridge) NextAddress() (types.UnlockConditions, error) { return ws.w.NextAddress() }
    66  func (ws *walletBridge) StartTransaction() transactionBuilder         { return ws.w.StartTransaction() }
    67  
    68  // stdPersist implements the persister interface via persist.SaveFile and
    69  // persist.LoadFile. The metadata and filename required by these functions is
    70  // internal to stdPersist.
    71  type stdPersist struct {
    72  	meta     persist.Metadata
    73  	filename string
    74  }
    75  
    76  func (p *stdPersist) save(data contractorPersist) error {
    77  	return persist.SaveFile(p.meta, data, p.filename)
    78  }
    79  
    80  func (p *stdPersist) saveSync(data contractorPersist) error {
    81  	return persist.SaveFileSync(p.meta, data, p.filename)
    82  }
    83  
    84  func (p *stdPersist) load(data *contractorPersist) error {
    85  	return persist.LoadFile(p.meta, data, p.filename)
    86  }
    87  
    88  func newPersist(dir string) *stdPersist {
    89  	return &stdPersist{
    90  		meta: persist.Metadata{
    91  			Header:  "Contractor Persistence",
    92  			Version: "0.5.2",
    93  		},
    94  		filename: filepath.Join(dir, "contractor.json"),
    95  	}
    96  }