Skip to content

Commit e7f6884

Browse files
authored
Merge pull request #238 from SiaFoundation/christopher/add-misc-refactored-test
Finish refactoring consensus tests
2 parents a8ab59b + 5c477f0 commit e7f6884

File tree

6 files changed

+648
-997
lines changed

6 files changed

+648
-997
lines changed

api/api_test.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,35 @@ import (
2929

3030
const testPassword = "password"
3131

32+
// checkFC checks the retrieved file contract with the source file contract in
33+
// addition to checking the resolved and valid fields.
34+
func checkFC(t *testing.T, revision, resolved, valid bool, expected types.FileContract, got explorer.ExtendedFileContract) {
35+
t.Helper()
36+
37+
testutil.Equal(t, "resolved state", resolved, got.Resolved)
38+
testutil.Equal(t, "valid state", valid, got.Valid)
39+
40+
testutil.Equal(t, "filesize", expected.Filesize, got.Filesize)
41+
testutil.Equal(t, "file merkle root", expected.FileMerkleRoot, got.FileMerkleRoot)
42+
testutil.Equal(t, "window start", expected.WindowStart, got.WindowStart)
43+
testutil.Equal(t, "window end", expected.WindowEnd, got.WindowEnd)
44+
if !revision {
45+
testutil.Equal(t, "payout", expected.Payout, got.Payout)
46+
}
47+
testutil.Equal(t, "unlock hash", expected.UnlockHash, got.UnlockHash)
48+
testutil.Equal(t, "revision number", expected.RevisionNumber, got.RevisionNumber)
49+
testutil.Equal(t, "valid proof outputs", len(expected.ValidProofOutputs), len(got.ValidProofOutputs))
50+
for i := range expected.ValidProofOutputs {
51+
testutil.Equal(t, "valid proof output address", expected.ValidProofOutputs[i].Address, got.ValidProofOutputs[i].Address)
52+
testutil.Equal(t, "valid proof output value", expected.ValidProofOutputs[i].Value, got.ValidProofOutputs[i].Value)
53+
}
54+
testutil.Equal(t, "missed proof outputs", len(expected.MissedProofOutputs), len(got.MissedProofOutputs))
55+
for i := range expected.MissedProofOutputs {
56+
testutil.Equal(t, "missed proof output address", expected.MissedProofOutputs[i].Address, got.MissedProofOutputs[i].Address)
57+
testutil.Equal(t, "missed proof output value", expected.MissedProofOutputs[i].Value, got.MissedProofOutputs[i].Value)
58+
}
59+
}
60+
3261
func newExplorer(t *testing.T, network *consensus.Network, genesisBlock types.Block, scanCfg config.Scanner) (*explorer.Explorer, *chain.Manager, error) {
3362
log := zaptest.NewLogger(t)
3463
dir := t.TempDir()
@@ -498,23 +527,23 @@ func TestAPI(t *testing.T) {
498527
if err != nil {
499528
t.Fatal(err)
500529
}
501-
testutil.CheckFC(t, true, false, false, revFC, resp)
530+
checkFC(t, true, false, false, revFC, resp)
502531
}},
503532
{"Contracts", func(t *testing.T) {
504533
resp, err := client.Contracts([]types.FileContractID{txn1.FileContractID(0)})
505534
if err != nil {
506535
t.Fatal(err)
507536
}
508537
testutil.Equal(t, "len(contracts)", 1, len(resp))
509-
testutil.CheckFC(t, true, false, false, revFC, resp[0])
538+
checkFC(t, true, false, false, revFC, resp[0])
510539
}},
511540
{"ContractsKey", func(t *testing.T) {
512541
resp, err := client.ContractsKey(renterPublicKey)
513542
if err != nil {
514543
t.Fatal(err)
515544
}
516545
testutil.Equal(t, "len(contracts)", 1, len(resp))
517-
testutil.CheckFC(t, true, false, false, revFC, resp[0])
546+
checkFC(t, true, false, false, revFC, resp[0])
518547
}},
519548
{"Search siacoin", func(t *testing.T) {
520549
resp, err := client.Search(txn1.SiacoinOutputID(0).String())

internal/testutil/check.go

Lines changed: 28 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,6 @@ func Equal[T any](t *testing.T, desc string, expected, got T) {
2828
}
2929
}
3030

31-
// CheckBalance checks that an address has the balances we expect.
32-
func CheckBalance(t *testing.T, db explorer.Store, addr types.Address, expectSC, expectImmatureSC types.Currency, expectSF uint64) {
33-
t.Helper()
34-
35-
sc, immatureSC, sf, err := db.Balance(addr)
36-
if err != nil {
37-
t.Fatal(err)
38-
}
39-
Equal(t, "siacoins", expectSC, sc)
40-
Equal(t, "immature siacoins", expectImmatureSC, immatureSC)
41-
Equal(t, "siafunds", expectSF, sf)
42-
}
43-
4431
// CheckTransaction checks the inputs and outputs of the retrieved transaction
4532
// with the source transaction.
4633
func CheckTransaction(t *testing.T, expectTxn types.Transaction, gotTxn explorer.Transaction) {
@@ -150,6 +137,29 @@ func CheckTransaction(t *testing.T, expectTxn types.Transaction, gotTxn explorer
150137
func CheckV2Transaction(t *testing.T, expectTxn types.V2Transaction, gotTxn explorer.V2Transaction) {
151138
t.Helper()
152139

140+
// checkV2FC checks the retrieved file contract with the source file contract
141+
// in addition to checking the resolved and valid fields.
142+
checkV2FC := func(expected types.V2FileContract, got explorer.V2FileContract) {
143+
t.Helper()
144+
145+
gotFC := got.V2FileContractElement.V2FileContract
146+
Equal(t, "capacity", expected.Capacity, gotFC.Capacity)
147+
Equal(t, "filesize", expected.Filesize, gotFC.Filesize)
148+
Equal(t, "proof height", expected.ProofHeight, gotFC.ProofHeight)
149+
Equal(t, "expiration height", expected.ExpirationHeight, gotFC.ExpirationHeight)
150+
Equal(t, "renter output address", expected.RenterOutput.Address, gotFC.RenterOutput.Address)
151+
Equal(t, "renter output value", expected.RenterOutput.Address, gotFC.RenterOutput.Address)
152+
Equal(t, "host output address", expected.HostOutput.Address, gotFC.HostOutput.Address)
153+
Equal(t, "host output value", expected.HostOutput.Address, gotFC.HostOutput.Address)
154+
Equal(t, "missed host value", expected.MissedHostValue, gotFC.MissedHostValue)
155+
Equal(t, "total collateral", expected.TotalCollateral, gotFC.TotalCollateral)
156+
Equal(t, "renter public key", expected.RenterPublicKey, gotFC.RenterPublicKey)
157+
Equal(t, "host public key", expected.HostPublicKey, gotFC.HostPublicKey)
158+
Equal(t, "revision number", expected.RevisionNumber, gotFC.RevisionNumber)
159+
Equal(t, "renter signature", expected.RenterSignature, gotFC.RenterSignature)
160+
Equal(t, "host signature", expected.HostSignature, gotFC.HostSignature)
161+
}
162+
153163
txnID := expectTxn.ID()
154164
Equal(t, "id", txnID, gotTxn.ID)
155165
Equal(t, "new foundation address", expectTxn.NewFoundationAddress, gotTxn.NewFoundationAddress)
@@ -217,7 +227,7 @@ func CheckV2Transaction(t *testing.T, expectTxn types.V2Transaction, gotTxn expl
217227
expected := expectTxn.FileContracts[i]
218228
got := gotTxn.FileContracts[i]
219229

220-
CheckV2FC(t, expected, got)
230+
checkV2FC(expected, got)
221231
Equal(t, "id", expectTxn.V2FileContractID(txnID, i), got.ID)
222232
}
223233

@@ -228,23 +238,23 @@ func CheckV2Transaction(t *testing.T, expectTxn types.V2Transaction, gotTxn expl
228238

229239
Equal(t, "parent ID", expected.Parent.ID, got.Parent.ID)
230240
Equal(t, "revision ID", expected.Parent.ID, got.Revision.ID)
231-
CheckV2FC(t, expected.Parent.V2FileContract, got.Parent)
232-
CheckV2FC(t, expected.Revision, got.Revision)
241+
checkV2FC(expected.Parent.V2FileContract, got.Parent)
242+
checkV2FC(expected.Revision, got.Revision)
233243
}
234244

235245
Equal(t, "file contract resolutions", len(expectTxn.FileContractResolutions), len(gotTxn.FileContractResolutions))
236246
for i := range expectTxn.FileContractResolutions {
237247
expected := expectTxn.FileContractResolutions[i]
238248
got := gotTxn.FileContractResolutions[i]
239249

240-
CheckV2FC(t, expected.Parent.V2FileContract, got.Parent)
250+
checkV2FC(expected.Parent.V2FileContract, got.Parent)
241251

242252
switch v := expected.Resolution.(type) {
243253
case *types.V2FileContractRenewal:
244254
if gotV, ok := got.Resolution.(*explorer.V2FileContractRenewal); !ok {
245255
t.Fatalf("expected V2FileContractRenewal, got %v", reflect.TypeOf(got.Resolution))
246256
} else {
247-
CheckV2FC(t, v.NewContract, gotV.NewContract)
257+
checkV2FC(v.NewContract, gotV.NewContract)
248258

249259
Equal(t, "type", explorer.V2ResolutionRenewal, got.Type)
250260
Equal(t, "final renter output address", v.FinalRenterOutput.Address, gotV.FinalRenterOutput.Address)
@@ -314,72 +324,3 @@ func CheckV2Transaction(t *testing.T, expectTxn types.V2Transaction, gotTxn expl
314324
Equal(t, "arbitrary data value", expectTxn.ArbitraryData[i], gotTxn.ArbitraryData[i])
315325
}
316326
}
317-
318-
// CheckV2ChainIndices checks that the chain indices that a v2 transaction was
319-
// in from the explorer match the expected chain indices.
320-
func CheckV2ChainIndices(t *testing.T, db explorer.Store, txnID types.TransactionID, expected []types.ChainIndex) {
321-
t.Helper()
322-
323-
indices, err := db.V2TransactionChainIndices(txnID, 0, 100)
324-
switch {
325-
case err != nil:
326-
t.Fatal(err)
327-
case len(indices) != len(expected):
328-
t.Fatalf("expected %d indices, got %d", len(expected), len(indices))
329-
}
330-
for i := range indices {
331-
Equal(t, "index", expected[i], indices[i])
332-
}
333-
}
334-
335-
// CheckFC checks the retrieved file contract with the source file contract in
336-
// addition to checking the resolved and valid fields.
337-
func CheckFC(t *testing.T, revision, resolved, valid bool, expected types.FileContract, got explorer.ExtendedFileContract) {
338-
t.Helper()
339-
340-
Equal(t, "resolved state", resolved, got.Resolved)
341-
Equal(t, "valid state", valid, got.Valid)
342-
343-
Equal(t, "filesize", expected.Filesize, got.Filesize)
344-
Equal(t, "file merkle root", expected.FileMerkleRoot, got.FileMerkleRoot)
345-
Equal(t, "window start", expected.WindowStart, got.WindowStart)
346-
Equal(t, "window end", expected.WindowEnd, got.WindowEnd)
347-
if !revision {
348-
Equal(t, "payout", expected.Payout, got.Payout)
349-
}
350-
Equal(t, "unlock hash", expected.UnlockHash, got.UnlockHash)
351-
Equal(t, "revision number", expected.RevisionNumber, got.RevisionNumber)
352-
Equal(t, "valid proof outputs", len(expected.ValidProofOutputs), len(got.ValidProofOutputs))
353-
for i := range expected.ValidProofOutputs {
354-
Equal(t, "valid proof output address", expected.ValidProofOutputs[i].Address, got.ValidProofOutputs[i].Address)
355-
Equal(t, "valid proof output value", expected.ValidProofOutputs[i].Value, got.ValidProofOutputs[i].Value)
356-
}
357-
Equal(t, "missed proof outputs", len(expected.MissedProofOutputs), len(got.MissedProofOutputs))
358-
for i := range expected.MissedProofOutputs {
359-
Equal(t, "missed proof output address", expected.MissedProofOutputs[i].Address, got.MissedProofOutputs[i].Address)
360-
Equal(t, "missed proof output value", expected.MissedProofOutputs[i].Value, got.MissedProofOutputs[i].Value)
361-
}
362-
}
363-
364-
// CheckV2FC checks the retrieved file contract with the source file contract
365-
// in addition to checking the resolved and valid fields.
366-
func CheckV2FC(t *testing.T, expected types.V2FileContract, got explorer.V2FileContract) {
367-
t.Helper()
368-
369-
gotFC := got.V2FileContractElement.V2FileContract
370-
Equal(t, "capacity", expected.Capacity, gotFC.Capacity)
371-
Equal(t, "filesize", expected.Filesize, gotFC.Filesize)
372-
Equal(t, "proof height", expected.ProofHeight, gotFC.ProofHeight)
373-
Equal(t, "expiration height", expected.ExpirationHeight, gotFC.ExpirationHeight)
374-
Equal(t, "renter output address", expected.RenterOutput.Address, gotFC.RenterOutput.Address)
375-
Equal(t, "renter output value", expected.RenterOutput.Address, gotFC.RenterOutput.Address)
376-
Equal(t, "host output address", expected.HostOutput.Address, gotFC.HostOutput.Address)
377-
Equal(t, "host output value", expected.HostOutput.Address, gotFC.HostOutput.Address)
378-
Equal(t, "missed host value", expected.MissedHostValue, gotFC.MissedHostValue)
379-
Equal(t, "total collateral", expected.TotalCollateral, gotFC.TotalCollateral)
380-
Equal(t, "renter public key", expected.RenterPublicKey, gotFC.RenterPublicKey)
381-
Equal(t, "host public key", expected.HostPublicKey, gotFC.HostPublicKey)
382-
Equal(t, "revision number", expected.RevisionNumber, gotFC.RevisionNumber)
383-
Equal(t, "renter signature", expected.RenterSignature, gotFC.RenterSignature)
384-
Equal(t, "host signature", expected.HostSignature, gotFC.HostSignature)
385-
}

0 commit comments

Comments
 (0)