Commit 21806ac7 authored by Luke Champine's avatar Luke Champine

embed HostAnnouncement field

parent 5e61eb2c
......@@ -43,85 +43,83 @@ func (h *Host) nextFilename() string {
//
// TODO: Make the host able to parse multiple contracts at once.
func (h *Host) considerContract(t consensus.Transaction, startBlock consensus.BlockHeight) (updatedTransaction consensus.Transaction, err error) {
/*
// Check that there is exactly one file contract.
if len(t.FileContracts) != 1 {
err = errors.New("transaction must have exactly one contract")
return
}
if startBlock > h.state.Height()+20 {
err = errors.New("startBlock is too far in the future")
return
}
// Check that there is exactly one file contract.
if len(t.FileContracts) != 1 {
err = errors.New("transaction must have exactly one contract")
return
}
if startBlock > h.state.Height()+20 {
err = errors.New("startBlock is too far in the future")
return
}
// These variables are here for convenience.
contract := t.FileContracts[0]
window := contract.End - contract.Start
duration := contract.End - startBlock
fileSize := contract.FileSize
// These variables are here for convenience.
contract := t.FileContracts[0]
window := contract.End - contract.Start
duration := contract.End - startBlock
fileSize := contract.FileSize
// Check that the file size listed in the contract is in bounds.
if fileSize < h.announcement.MinFilesize || fileSize > h.announcement.MaxFilesize {
err = fmt.Errorf("file is of incorrect size - filesize %v, min %v, max %v", fileSize, h.announcement.MinFilesize, h.announcement.MaxFilesize)
return
}
// Check that there is space for the file.
if fileSize > uint64(h.spaceRemaining) {
err = HostCapacityErr
return
}
// Check that the duration of the contract is in bounds.
if duration < h.announcement.MinDuration || duration > h.announcement.MaxDuration {
err = errors.New("contract duration is out of bounds")
return
}
// Check that the window is large enough.
if window < h.announcement.MinWindow {
err = errors.New("challenge window is not large enough")
return
}
// Outputs for successful proofs need to go to the correct address.
if contract.ValidProofAddress != h.announcement.CoinAddress {
err = errors.New("coins are not paying out to correct address")
return
}
// Output for failed proofs needs to be the 0 address.
emptyAddress := consensus.CoinAddress{}
if contract.MissedProofAddress != emptyAddress {
err = errors.New("burn payout needs to go to the empty address")
return
}
// Check that the file size listed in the contract is in bounds.
if fileSize < h.MinFilesize || fileSize > h.MaxFilesize {
err = fmt.Errorf("file is of incorrect size - filesize %v, min %v, max %v", fileSize, h.MinFilesize, h.MaxFilesize)
return
}
// Check that there is space for the file.
if fileSize > uint64(h.spaceRemaining) {
err = HostCapacityErr
return
}
// Check that the duration of the contract is in bounds.
if duration < h.MinDuration || duration > h.MaxDuration {
err = errors.New("contract duration is out of bounds")
return
}
// Check that the window is large enough.
if window < h.MinWindow {
err = errors.New("challenge window is not large enough")
return
}
// Outputs for successful proofs need to go to the correct address.
if contract.ValidProofAddress != h.CoinAddress {
err = errors.New("coins are not paying out to correct address")
return
}
// Output for failed proofs needs to be the 0 address.
emptyAddress := consensus.CoinAddress{}
if contract.MissedProofAddress != emptyAddress {
err = errors.New("burn payout needs to go to the empty address")
return
}
// Verify that the contract fund covers the payout and burn for the whole
// duration.
requiredFund := (h.announcement.Price + h.announcement.Burn) * consensus.Currency(duration) * consensus.Currency(fileSize)
if contract.Payout != requiredFund {
err = errors.New("ContractFund does not match the terms of service.")
return
}
// Verify that the contract fund covers the payout and burn for the whole
// duration.
requiredFund := (h.Price + h.Burn) * consensus.Currency(duration) * consensus.Currency(fileSize)
if contract.Payout != requiredFund {
err = errors.New("ContractFund does not match the terms of service.")
return
}
// Add enough funds to the transaction to cover the penalty half of the
// agreement.
penalty := h.announcement.Burn * consensus.Currency(fileSize) * consensus.Currency(duration)
id, err := h.wallet.RegisterTransaction(t)
if err != nil {
err = HostCapacityErr // hide the fact that the host is having wallet issues.
return
}
err = h.wallet.FundTransaction(id, penalty)
if err != nil {
err = HostCapacityErr // hide the fact that the host is having wallet issues.
return
}
updatedTransaction, err = h.wallet.SignTransaction(id, true)
if err != nil {
err = HostCapacityErr // hide the fact that the host is having wallet issues.
return
}
// Add enough funds to the transaction to cover the penalty half of the
// agreement.
penalty := h.Burn * consensus.Currency(fileSize) * consensus.Currency(duration)
id, err := h.wallet.RegisterTransaction(t)
if err != nil {
err = HostCapacityErr // hide the fact that the host is having wallet issues.
return
}
err = h.wallet.FundTransaction(id, penalty)
if err != nil {
err = HostCapacityErr // hide the fact that the host is having wallet issues.
return
}
updatedTransaction, err = h.wallet.SignTransaction(id, true)
if err != nil {
err = HostCapacityErr // hide the fact that the host is having wallet issues.
return
}
// Update the amount of space the host has for sale.
h.spaceRemaining -= int64(fileSize)
*/
// Update the amount of space the host has for sale.
h.spaceRemaining -= int64(fileSize)
return
}
......
......@@ -31,8 +31,10 @@ type Host struct {
wallet modules.Wallet
latestBlock consensus.BlockID
hostDir string
// announcement modules.HostAnnouncement
// embed HostEntry fields for convenience
modules.HostAnnouncement
hostDir string
spaceRemaining int64
fileCounter int
......@@ -60,16 +62,14 @@ func New(state *consensus.State, wallet modules.Wallet) (h *Host, err error) {
state: state,
wallet: wallet,
/*
announcement: modules.HostAnnouncement{
MaxFilesize: 4 * 1000 * 1000,
MaxDuration: 1008, // One week.
MinWindow: 20,
Price: 1,
Burn: 1,
CoinAddress: addr,
},
*/
HostAnnouncement: modules.HostAnnouncement{
MaxFilesize: 4 * 1000 * 1000,
MaxDuration: 1008, // One week.
MinWindow: 20,
Price: 1,
Burn: 1,
CoinAddress: addr,
},
contracts: make(map[consensus.ContractID]contractObligation),
}
......@@ -123,7 +123,7 @@ func (h *Host) RetrieveFile(conn net.Conn) (err error) {
func (h *Host) SetAnnouncement(ha modules.HostAnnouncement) {
h.mu.Lock()
defer h.mu.Unlock()
h.announcement = ha
h.HostAnnouncement = ha
}
type HostInfo struct {
......@@ -138,7 +138,7 @@ func (h *Host) Info() HostInfo {
defer h.mu.RUnlock()
info := HostInfo{
HostAnnouncement: h.announcement,
HostAnnouncement: h.HostAnnouncement,
StorageRemaining: h.spaceRemaining,
ContractCount: len(h.contracts),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment