This repository has been archived by the owner on Nov 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
29e3ef9
commit fa384e3
Showing
3 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |