func TestCoveredFieldsEncoding(t *testing.T) {
	var cf CoveredFields
	if h := hashStr(cf); h != "aecfdceb8b630b5b00668d229221f876b3be1630703c4615a642db2c666b4fd7" {
		t.Error("encoding mismatch:", h)
	}
	cf = CoveredFields{
		WholeTransaction:      true,
		SiacoinInputs:         []uint64{0},
		SiacoinOutputs:        []uint64{1},
		FileContracts:         []uint64{2},
		FileContractRevisions: []uint64{3},
		StorageProofs:         []uint64{4},
		SiafundInputs:         []uint64{5},
		SiafundOutputs:        []uint64{6},
		MinerFees:             []uint64{7},
		ArbitraryData:         []uint64{8},
		TransactionSignatures: []uint64{9, 10},
	}
	if h := hashStr(cf); h != "5b10cd6b50b09447aae02829643e62b513ce99b969a80aeb620f74e77ca9bbba" {
		t.Error("encoding mismatch:", h)
	}
}
TestCoveredFieldsEncoding tests that optimizations applied to the encoding
of the CoveredFields type do not change its encoding.
package types
import (
	"bytes"
	"encoding/json"
	"fmt"
	"math/big"
	"strings"
	"testing"
	"github.com/NebulousLabs/Sia/crypto"
	"github.com/NebulousLabs/Sia/encoding"
	"github.com/NebulousLabs/fastrand"
)
func hashStr(v interface{}) string {
	h := crypto.HashObject(v)
	return fmt.Sprintf("%x", h[:])
}
var heavyBlock = func() Block {
	b := Block{
		MinerPayouts: []SiacoinOutput{
			{Value: CalculateCoinbase(0)},
			{Value: CalculateCoinbase(1)},
		},
		Transactions: []Transaction{
			{
				SiacoinInputs: []SiacoinInput{{
					UnlockConditions: UnlockConditions{
						PublicKeys: []SiaPublicKey{{
							Algorithm: SignatureEd25519,
							Key:       fastrand.Bytes(32),
						}},
						SignaturesRequired: 6,
					},
				}},
				SiacoinOutputs: []SiacoinOutput{{
					Value: NewCurrency64(20),
				}},
				FileContracts: []FileContract{{
					FileSize:       12,
					Payout:         NewCurrency64(100),
					RevisionNumber: 8,
					ValidProofOutputs: []SiacoinOutput{{
						Value: NewCurrency64(2),
					}},
					MissedProofOutputs: []SiacoinOutput{{
						Value: NewCurrency64(3),
					}},
				}},
				FileContractRevisions: []FileContractRevision{{
					NewFileSize:       13,
					NewRevisionNumber: 9,
					UnlockConditions: UnlockConditions{
						PublicKeys: []SiaPublicKey{{
							Algorithm: SignatureEd25519,
							Key:       fastrand.Bytes(32),
						}},
					},
					NewValidProofOutputs: []SiacoinOutput{{
						Value: NewCurrency64(4),
					}},
					NewMissedProofOutputs: []SiacoinOutput{{
						Value: NewCurrency64(5),
					}},
				}},
				StorageProofs: []StorageProof{{
					HashSet: []crypto.Hash{{}},
				}},
				SiafundInputs: []SiafundInput{{
					UnlockConditions: UnlockConditions{
						PublicKeys: []SiaPublicKey{{
							Algorithm: SignatureEd25519,
							Key:       fastrand.Bytes(32),
						}},
					},
				}},
				SiafundOutputs: []SiafundOutput{{
					ClaimStart: NewCurrency64(99),
					Value:      NewCurrency64(25),
				}},
				MinerFees:     []Currency{NewCurrency64(215)},
				ArbitraryData: [][]byte{fastrand.Bytes(10)},
				TransactionSignatures: []TransactionSignature{{
					PublicKeyIndex: 5,
					Timelock:       80,
					CoveredFields: CoveredFields{
						WholeTransaction:      true,
						SiacoinInputs:         []uint64{0},
						SiacoinOutputs:        []uint64{1},
						FileContracts:         []uint64{2},
						FileContractRevisions: []uint64{3},
						StorageProofs:         []uint64{4},
						SiafundInputs:         []uint64{5},
						SiafundOutputs:        []uint64{6},
						MinerFees:             []uint64{7},
						ArbitraryData:         []uint64{8},
						TransactionSignatures: []uint64{9},
					},
					Signature: fastrand.Bytes(32),
				}},
			},
		},
	}
	fastrand.Read(b.Transactions[0].SiacoinInputs[0].ParentID[:])
	fastrand.Read(b.Transactions[0].SiacoinOutputs[0].UnlockHash[:])
	fastrand.Read(b.Transactions[0].FileContracts[0].FileMerkleRoot[:])
	fastrand.Read(b.Transactions[0].FileContracts[0].ValidProofOutputs[0].UnlockHash[:])
	fastrand.Read(b.Transactions[0].FileContracts[0].MissedProofOutputs[0].UnlockHash[:])
	fastrand.Read(b.Transactions[0].FileContractRevisions[0].ParentID[:])
	fastrand.Read(b.Transactions[0].FileContractRevisions[0].NewFileMerkleRoot[:])
	fastrand.Read(b.Transactions[0].FileContractRevisions[0].NewValidProofOutputs[0].UnlockHash[:])
	fastrand.Read(b.Transactions[0].FileContractRevisions[0].NewMissedProofOutputs[0].UnlockHash[:])
	fastrand.Read(b.Transactions[0].StorageProofs[0].ParentID[:])
	fastrand.Read(b.Transactions[0].StorageProofs[0].HashSet[0][:])
	fastrand.Read(b.Transactions[0].StorageProofs[0].Segment[:])
	fastrand.Read(b.Transactions[0].SiafundInputs[0].ParentID[:])
	fastrand.Read(b.Transactions[0].SiafundInputs[0].ClaimUnlockHash[:])
	fastrand.Read(b.Transactions[0].SiafundOutputs[0].UnlockHash[:])
	fastrand.Read(b.Transactions[0].TransactionSignatures[0].ParentID[:])
	return b
}()
func TestBlockEncoding(t *testing.T) {
	var decB Block
	err := encoding.Unmarshal(encoding.Marshal(heavyBlock), &decB)
	if err != nil {
		t.Fatal(err)
	}
	if hashStr(heavyBlock) != hashStr(decB) {
		t.Fatal("block changed after encode/decode:", heavyBlock, decB)
	}
}
func TestBadBlock(t *testing.T) {
	badData := "000000000000000000000000000000000000000000000000\x00\x00\x00\x00\x00\x00\x00\x00\x00"
	var block Block
	err := encoding.Unmarshal([]byte(badData), &block)
	if err == nil {
		t.Fatal("invalid block decoded successfully")
	}
}
func TestTooLargeDecoder(t *testing.T) {
	if testing.Short() {
		t.SkipNow()
	}
	enc := encoding.Marshal(Block{})
	copy(enc[len(enc)-8:], encoding.EncUint64(^uint64(0)))
	var block Block
	err := encoding.Unmarshal(enc, &block)
	if err == nil {
		t.Fatal("expected error, got nil")
	}
	var arb [][]byte
	for i := 0; i < 4; i++ {
		arb = append(arb, make([]byte, encoding.MaxSliceSize-1))
	}
	block.Transactions = []Transaction{{
		ArbitraryData: arb,
	}}
	enc = encoding.Marshal(block)
	err = encoding.Unmarshal(enc, &block)
	if err == nil {
		t.Fatal("expected error, got nil")
	}
}
func TestCurrencyMarshalJSON(t *testing.T) {
	b30 := big.NewInt(30)
	c30 := NewCurrency64(30)
	bMar30, err := b30.MarshalJSON()
	if err != nil {
		t.Fatal(err)
	}
	cMar30, err := c30.MarshalJSON()
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(bMar30, bytes.Trim(cMar30, `"`)) {
		t.Error("Currency does not match the marshalling of its math/big equivalent")
	}
	var cUmar30 Currency
	err = cUmar30.UnmarshalJSON(cMar30)
	if err != nil {
		t.Fatal(err)
	}
	if c30.Cmp(cUmar30) != 0 {
		t.Error("Incorrect unmarshalling of currency type.")
	}
	cMar30[0] = 0
	err = cUmar30.UnmarshalJSON(cMar30)
	if err == nil {
		t.Error("JSON decoded nonsense input")
	}
}
// TestCurrencyMarshalSia probes the MarshalSia and UnmarshalSia functions of
// the currency type.
func TestCurrencyMarshalSia(t *testing.T) {
	c := NewCurrency64(1656)
	buf := new(bytes.Buffer)
	err := c.MarshalSia(buf)
	if err != nil {
		t.Fatal(err)
	}
	var cUmar Currency
	cUmar.UnmarshalSia(buf)
	if c.Cmp(cUmar) != 0 {
		t.Error("marshal and unmarshal mismatch for currency type")
	}
}
// TestCurrencyString probes the String function of the currency type.
func TestCurrencyString(t *testing.T) {
	b := big.NewInt(7135)
	c := NewCurrency64(7135)
	if b.String() != c.String() {
		t.Error("string function not behaving as expected")
	}
}
// TestCurrencyScan probes the Scan function of the currency type.
func TestCurrencyScan(t *testing.T) {
	var c0 Currency
	c1 := NewCurrency64(81293)
	_, err := fmt.Sscan("81293", &c0)
	if err != nil {
		t.Fatal(err)
	}
	if c0.Cmp(c1) != 0 {
		t.Error("scanned number does not equal expected value")
	}
	_, err = fmt.Sscan("z", &c0)
	if err == nil {
		t.Fatal("scan is accepting garbage input")
	}
}
// TestCurrencyEncoding checks that a currency can encode and decode without
// error.
func TestCurrencyEncoding(t *testing.T) {
	c := NewCurrency64(351)
	cMar := encoding.Marshal(c)
	var cUmar Currency
	err := encoding.Unmarshal(cMar, &cUmar)
	if err != nil {
		t.Error("Error unmarshalling a currency:", err)
	}
	if cUmar.Cmp(c) != 0 {
		t.Error("Marshalling and Unmarshalling a currency did not work correctly")
	}
}
// TestNegativeCurrencyUnmarshalJSON tries to unmarshal a negative number from
// JSON.
func TestNegativeCurrencyUnmarshalJSON(t *testing.T) {
	// Marshal a 2 digit number.
	c := NewCurrency64(35)
	cMar, err := c.MarshalJSON()
	if err != nil {
		t.Fatal(err)
	}
	// Change the first digit to a negative character.
	cMar[0] = 45
	// Try unmarshalling the negative currency.
	var cNeg Currency
	err = cNeg.UnmarshalJSON(cMar)
	if err != ErrNegativeCurrency {
		t.Error("expecting ErrNegativeCurrency:", err)
	}
	if cNeg.i.Sign() < 0 {
		t.Error("negative currency returned")
	}
}
// TestNegativeCurrencyScan tries to scan in a negative number and checks for
// an error.
func TestNegativeCurrencyScan(t *testing.T) {
	var c Currency
	_, err := fmt.Sscan("-23", &c)
	if err != ErrNegativeCurrency {
		t.Error("expecting ErrNegativeCurrency:", err)
	}
}
// TestCurrencyUnsafeDecode tests that decoding into an existing Currency
// value does not overwrite its contents.
func TestCurrencyUnsafeDecode(t *testing.T) {
	// Scan
	backup := SiacoinPrecision.Mul64(1)
	c := SiacoinPrecision
	_, err := fmt.Sscan("7", &c)
	if err != nil {
		t.Error(err)
	} else if !SiacoinPrecision.Equals(backup) {
		t.Errorf("Scan changed value of SiacoinPrecision: %v -> %v", backup, SiacoinPrecision)
	}
	// UnmarshalSia
	c = SiacoinPrecision
	err = encoding.Unmarshal(encoding.Marshal(NewCurrency64(7)), &c)
	if err != nil {
		t.Error(err)
	} else if !SiacoinPrecision.Equals(backup) {
		t.Errorf("UnmarshalSia changed value of SiacoinPrecision: %v -> %v", backup, SiacoinPrecision)
	}
}
// TestTransactionEncoding tests that optimizations applied to the encoding of
// the Transaction type do not change its encoding.
func TestTransactionEncoding(t *testing.T) {
	var txn Transaction
	if h := hashStr(txn); h != "143aa0da2b6a4ca39eee3ee50a6536d75eedff3b5ef0229a6d603afa7854d5b8" {
		t.Error("encoding mismatch:", h)
	}
	txn = Transaction{
		SiacoinInputs:         []SiacoinInput{{}},
		SiacoinOutputs:        []SiacoinOutput{{}},
		FileContracts:         []FileContract{{}},
		FileContractRevisions: []FileContractRevision{{}},
		StorageProofs:         []StorageProof{{}},
		SiafundInputs:         []SiafundInput{{}},
		SiafundOutputs:        []SiafundOutput{{}},
		MinerFees:             []Currency{{}},
		ArbitraryData:         [][]byte{{}},
		TransactionSignatures: []TransactionSignature{{}},
	}
	if h := hashStr(txn); h != "a6c0f41cb89aaede0682ab06c1e757e12d662a0156ec878f85b935bc219fb3ca" {
		t.Error("encoding mismatch:", h)
	}
}
// TestSiacoinInputEncoding tests that optimizations applied to the encoding
// of the SiacoinInput type do not change its encoding.
func TestSiacoinInputEncoding(t *testing.T) {
	var sci SiacoinInput
	if h := hashStr(sci); h != "2f806f905436dc7c5079ad8062467266e225d8110a3c58d17628d609cb1c99d0" {
		t.Error("encoding mismatch:", h)
	}
	sci = SiacoinInput{
		ParentID:         SiacoinOutputID{1, 2, 3},
		UnlockConditions: UnlockConditions{},
	}
	if h := hashStr(sci); h != "f172a8f5892bb2b63eff32de6fd83c132be5ad134d1227d8881632bd809ae075" {
		t.Error("encoding mismatch:", h)
	}
}
// TestSiacoinOutputEncoding tests that optimizations applied to the encoding
// of the SiacoinOutput type do not change its encoding.
func TestSiacoinOutputEncoding(t *testing.T) {
	var sco SiacoinOutput
	if h := hashStr(sco); h != "4a1931803561f431decab002e7425f0a8531d5e456a1a47fd9998a2530c0f800" {
		t.Error("encoding mismatch:", h)
	}
	sco = SiacoinOutput{
		Value:      NewCurrency64(0),
		UnlockHash: UnlockHash{1, 2, 3},
	}
	if h := hashStr(sco); h != "32fb94ae64201f3e0a373947382367666bcf205d47a58ece9260c459986ae6fd" {
		t.Error("encoding mismatch:", h)
	}
}
// TestSiafundInputEncoding tests that optimizations applied to the encoding
// of the SiafundInput type do not change its encoding.
func TestSiafundInputEncoding(t *testing.T) {
	var sci SiafundInput
	if h := hashStr(sci); h != "978a948b1a92bcddcea382bafc7718a25f8cc49b8fb11db5d9159afa960cf70a" {
		t.Error("encoding mismatch:", h)
	}
	sci = SiafundInput{
		ParentID:         SiafundOutputID{1, 2, 3},
		UnlockConditions: UnlockConditions{1, nil, 3},
		ClaimUnlockHash:  UnlockHash{1, 2, 3},
	}
	if h := hashStr(sci); h != "1a6781ca002262e1def98e294f86dd81f866e2db9029954c64a36d20d0c6b46f" {
		t.Error("encoding mismatch:", h)
	}
}
// TestSiafundOutputEncoding tests that optimizations applied to the encoding
// of the SiafundOutput type do not change its encoding.
func TestSiafundOutputEncoding(t *testing.T) {
	var sco SiafundOutput
	if h := hashStr(sco); h != "df69a516de12056d0895fdea7a0274c5aba67091543238670513104c1af69c1f" {
		t.Error("encoding mismatch:", h)
	}
	sco = SiafundOutput{
		Value:      NewCurrency64(0),
		UnlockHash: UnlockHash{1, 2, 3},
		ClaimStart: NewCurrency64(4),
	}
	if h := hashStr(sco); h != "9524d2250b21adc76967e9f86d26a68982727329e5c42a6bf5e62504891a5176" {
		t.Error("encoding mismatch:", h)
	}
}