Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Commit

Permalink
add update_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechampine committed Aug 28, 2015
1 parent 29e3ef9 commit fa384e3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
1 change: 1 addition & 0 deletions modules/host/negotiate.go
Expand Up @@ -381,6 +381,7 @@ func (h *Host) rpcRevise(conn net.Conn) error {
h.spaceRemaining -= int64(len(piece))
obligation.FileContract.RevisionNumber = rev.NewRevisionNumber
obligation.FileContract.FileSize = rev.NewFileSize
obligation.FileContract.FileMerkleRoot = rev.NewFileMerkleRoot
h.obligationsByID[obligation.ID] = obligation
heightObligations := h.obligationsByHeight[obligation.FileContract.WindowStart+StorageProofReorgDepth]
for i := range heightObligations {
Expand Down
4 changes: 2 additions & 2 deletions modules/host/update.go
Expand Up @@ -25,7 +25,7 @@ func (h *Host) threadedDeleteObligation(obligation contractObligation) {
// obligation and submits it to the blockchain.
//
// TODO: The printlns here should be logging messages.
func (h *Host) threadedCreateStorageProof(obligation contractObligation, heightForProof types.BlockHeight) {
func (h *Host) threadedCreateStorageProof(obligation contractObligation) {
defer h.threadedDeleteObligation(obligation)

file, err := os.Open(obligation.Path)
Expand Down Expand Up @@ -81,7 +81,7 @@ func (h *Host) ProcessConsensusChange(cc modules.ConsensusChange) {
for _ = range cc.AppliedBlocks {
h.blockHeight++
for _, obligation := range h.obligationsByHeight[h.blockHeight] {
go h.threadedCreateStorageProof(obligation, h.blockHeight)
go h.threadedCreateStorageProof(obligation)
}
// TODO: If something happens while the storage proofs are being
// created, those files will never get cleared from the host.
Expand Down
88 changes: 88 additions & 0 deletions modules/host/update_test.go
@@ -0,0 +1,88 @@
package host

import (
"bytes"
"crypto/rand"
"io/ioutil"
"path/filepath"
"testing"

"github.com/NebulousLabs/Sia/crypto"
"github.com/NebulousLabs/Sia/types"
)

func TestStorageProof(t *testing.T) {
ht := CreateHostTester("TestStorageProof", t)

// create a file contract
fc := types.FileContract{
WindowStart: types.MaturityDelay + 3,
WindowEnd: 1000,
Payout: types.NewCurrency64(1),
UnlockHash: types.UnlockConditions{}.UnlockHash(),
ValidProofOutputs: []types.SiacoinOutput{{Value: types.NewCurrency64(1)}},
MissedProofOutputs: []types.SiacoinOutput{{Value: types.NewCurrency64(1)}},
}
txnBuilder := ht.wallet.StartTransaction()
err := txnBuilder.FundSiacoins(fc.Payout)
if err != nil {
t.Fatal(err)
}
txnBuilder.AddFileContract(fc)
signedTxnSet, err := txnBuilder.Sign(true)
if err != nil {
t.Fatal(err)
}
fcid := signedTxnSet[len(signedTxnSet)-1].FileContractID(0)

// generate data
const dataSize = 777
data := make([]byte, dataSize)
rand.Read(data)
root, err := crypto.ReaderMerkleRoot(bytes.NewReader(data))
if err != nil {
t.Fatal(err)
}
ioutil.WriteFile(filepath.Join(ht.host.saveDir, "foo"), data, 0777)

// create revision
rev := types.FileContractRevision{
ParentID: fcid,
UnlockConditions: types.UnlockConditions{},
NewFileSize: dataSize,
NewWindowStart: fc.WindowStart,
NewFileMerkleRoot: root,
NewWindowEnd: fc.WindowEnd,
NewValidProofOutputs: fc.ValidProofOutputs,
NewMissedProofOutputs: fc.MissedProofOutputs,
NewRevisionNumber: 1,
}
revTxn := types.Transaction{
FileContractRevisions: []types.FileContractRevision{rev},
}

// create obligation
obligation := contractObligation{
ID: fcid,
FileContract: fc,
Path: filepath.Join(ht.host.saveDir, "foo"),
}
ht.host.obligationsByID[fcid] = obligation
ht.host.obligationsByHeight[fc.WindowStart+1] = []contractObligation{obligation}

// submit both to tpool
err = ht.tpool.AcceptTransactionSet(append(signedTxnSet, revTxn))
if err != nil {
t.Fatal(err)
}
_, err = ht.miner.AddBlock()
if err != nil {
t.Fatal(err)
}

// storage proof will be submitted after mining one more block
_, err = ht.miner.AddBlock()
if err != nil {
t.Fatal(err)
}
}

0 comments on commit fa384e3

Please sign in to comment.