From 991ed7db091894a99468e14eeb6266e7d84067b4 Mon Sep 17 00:00:00 2001 From: Christopher Tarry Date: Mon, 9 Dec 2024 17:57:51 -0500 Subject: [PATCH] update tests --- internal/testutil/check.go | 4 +- persist/sqlite/consensus_test.go | 81 ++++++++++++------------------ persist/sqlite/events.go | 1 + persist/sqlite/v2consensus_test.go | 53 +++++++++++-------- 4 files changed, 65 insertions(+), 74 deletions(-) diff --git a/internal/testutil/check.go b/internal/testutil/check.go index a873289..eba7a32 100644 --- a/internal/testutil/check.go +++ b/internal/testutil/check.go @@ -16,8 +16,8 @@ import ( func Equal[T any](t *testing.T, desc string, expect, got T) { t.Helper() - if !cmp.Equal(expect, got, cmpopts.EquateEmpty(), cmpopts.IgnoreUnexported(consensus.Work{})) { - t.Fatalf("expected %v %s, got %v", expect, desc, got) + if !cmp.Equal(expect, got, cmpopts.EquateEmpty(), cmpopts.IgnoreUnexported(consensus.Work{}), cmpopts.IgnoreTypes(types.StateElement{}, "MerkleProof")) { + t.Fatalf("%s expected != got, diff: %s", desc, cmp.Diff(expect, got)) } } diff --git a/persist/sqlite/consensus_test.go b/persist/sqlite/consensus_test.go index ee39563..9c6c30e 100644 --- a/persist/sqlite/consensus_test.go +++ b/persist/sqlite/consensus_test.go @@ -4,7 +4,6 @@ import ( "errors" "math" "path/filepath" - "reflect" "testing" "time" @@ -13,6 +12,7 @@ import ( "go.sia.tech/coreutils" "go.sia.tech/coreutils/chain" ctestutil "go.sia.tech/coreutils/testutil" + "go.sia.tech/coreutils/wallet" "go.sia.tech/explored/explorer" "go.sia.tech/explored/internal/testutil" "go.sia.tech/explored/persist/sqlite" @@ -499,6 +499,9 @@ func TestFileContract(t *testing.T) { pk1 := types.GeneratePrivateKey() addr1 := types.StandardUnlockHash(pk1.PublicKey()) + pk2 := types.GeneratePrivateKey() + addr2 := types.StandardUnlockHash(pk2.PublicKey()) + renterPrivateKey := types.GeneratePrivateKey() renterPublicKey := renterPrivateKey.PublicKey() @@ -515,7 +518,7 @@ func TestFileContract(t *testing.T) { windowStart := cm.Tip().Height + 10 windowEnd := windowStart + 10 - fc := testutil.PrepareContractFormation(renterPublicKey, hostPublicKey, types.Siacoins(1), types.Siacoins(1), windowStart, windowEnd, types.VoidAddress) + fc := testutil.PrepareContractFormation(renterPublicKey, hostPublicKey, types.Siacoins(1), types.Siacoins(1), windowStart, windowEnd, addr2) txn := types.Transaction{ SiacoinInputs: []types.SiacoinInput{{ ParentID: scOutputID, @@ -681,12 +684,19 @@ func TestFileContract(t *testing.T) { }) { - for _, addr := range []types.Address{types.VoidAddress, addr1} { - _, err := db.AddressEvents(addr, 0, math.MaxInt64) - if err != nil { - t.Fatal(err) - } + events, err := db.AddressEvents(addr2, 0, math.MaxInt64) + if err != nil { + t.Fatal(err) } + testutil.Equal(t, "events", 2, len(events)) + + ev0 := events[0].Data.(wallet.EventV1ContractResolution) + testutil.Equal(t, "event 0 parent ID", fcID, ev0.Parent.ID) + testutil.Equal(t, "event 0 output ID", fcID.MissedOutputID(0), ev0.SiacoinElement.ID) + testutil.Equal(t, "event 0 missed", true, ev0.Missed) + + ev1 := events[1].Data.(wallet.EventV1Transaction) + testutil.Equal(t, "event 1", txn, ev1.Transaction) } { @@ -1436,15 +1446,6 @@ func TestRevertSendTransactions(t *testing.T) { } } - { - for _, addr := range []types.Address{types.VoidAddress, addr1, addr2, addr3} { - _, err := db.AddressEvents(addr, 0, math.MaxInt64) - if err != nil { - t.Fatal(err) - } - } - } - { // take 3 blocks off the top // revertBlocks := blocks[len(blocks)-3:] @@ -1541,15 +1542,6 @@ func TestRevertSendTransactions(t *testing.T) { ActiveContracts: 0, StorageUtilization: 0, }) - - { - for _, addr := range []types.Address{types.VoidAddress, addr1, addr2, addr3} { - _, err := db.AddressEvents(addr, 0, math.MaxInt64) - if err != nil { - t.Fatal(err) - } - } - } } func TestHostAnnouncement(t *testing.T) { @@ -1647,12 +1639,9 @@ func TestHostAnnouncement(t *testing.T) { if err != nil { t.Fatal(err) } - t.Logf("%v", reflect.TypeOf(events[0].Data)) - // if v, ok := events[0].Data.(wallet.EventV1Transaction); !ok { - // t.Fatalf("expected EventV1Transaction, got: %v", reflect.TypeOf(events[0].Data)) - // } else { - // testutil.Equal(t, "transaction", txn1, v.Transaction) - // } + testutil.Equal(t, "events", 2, len(events)) + testutil.Equal(t, "event 0", txn1, events[0].Data.(wallet.EventV1Transaction).Transaction) + testutil.Equal(t, "event 1", genesisBlock.Transactions[0], events[1].Data.(wallet.EventV1Transaction).Transaction) } { @@ -2054,15 +2043,6 @@ func TestMultipleReorg(t *testing.T) { testutil.Equal(t, "addr3 sf utxos", 1, len(sfUtxos3)) } } - - { - for _, addr := range []types.Address{types.VoidAddress, addr1, addr2, addr3} { - _, err := db.AddressEvents(addr, 0, math.MaxInt64) - if err != nil { - t.Fatal(err) - } - } - } } func TestMultipleReorgFileContract(t *testing.T) { @@ -2146,12 +2126,13 @@ func TestMultipleReorgFileContract(t *testing.T) { } { - for _, addr := range []types.Address{types.VoidAddress, addr1} { - _, err := db.AddressEvents(addr, 0, math.MaxInt64) - if err != nil { - t.Fatal(err) - } + events, err := db.AddressEvents(addr1, 0, math.MaxInt64) + if err != nil { + t.Fatal(err) } + testutil.Equal(t, "events", 2, len(events)) + testutil.Equal(t, "event 0", txn, events[0].Data.(wallet.EventV1Transaction).Transaction) + testutil.Equal(t, "event 1", genesisBlock.Transactions[0], events[1].Data.(wallet.EventV1Transaction).Transaction) } uc := types.UnlockConditions{ @@ -2390,12 +2371,12 @@ func TestMultipleReorgFileContract(t *testing.T) { } { - for _, addr := range []types.Address{types.VoidAddress, addr1} { - _, err := db.AddressEvents(addr, 0, math.MaxInt64) - if err != nil { - t.Fatal(err) - } + events, err := db.AddressEvents(addr1, 0, math.MaxInt64) + if err != nil { + t.Fatal(err) } + testutil.Equal(t, "events", 1, len(events)) + testutil.Equal(t, "event 0", genesisBlock.Transactions[0], events[0].Data.(wallet.EventV1Transaction).Transaction) } } diff --git a/persist/sqlite/events.go b/persist/sqlite/events.go index 14fbb00..9a7a967 100644 --- a/persist/sqlite/events.go +++ b/persist/sqlite/events.go @@ -77,6 +77,7 @@ func explorerToTypesV2Resolution(e explorer.V2FileContractResolution) (fcr types HostSignature: v.HostSignature, } case *types.V2StorageProof: + fcr.Resolution = v case *types.V2FileContractExpiration: fcr.Resolution = v default: diff --git a/persist/sqlite/v2consensus_test.go b/persist/sqlite/v2consensus_test.go index 0740a4f..01f0d3f 100644 --- a/persist/sqlite/v2consensus_test.go +++ b/persist/sqlite/v2consensus_test.go @@ -3,7 +3,6 @@ package sqlite_test import ( "bytes" "math" - "reflect" "testing" "time" @@ -11,6 +10,7 @@ import ( rhp2 "go.sia.tech/core/rhp/v2" "go.sia.tech/core/types" "go.sia.tech/coreutils/chain" + "go.sia.tech/coreutils/wallet" "go.sia.tech/explored/explorer" "go.sia.tech/explored/internal/testutil" ) @@ -296,12 +296,10 @@ func TestV2Attestations(t *testing.T) { if err != nil { t.Fatal(err) } - t.Logf("events[0].Data: %v", reflect.TypeOf(events[0].Data)) - // if v, ok := events[0].Data.(wallet.EventV2Transaction); !ok { - // t.Fatalf("expected EventV2Transaction, got: %v", reflect.TypeOf(events[0].Data)) - // } else { - // testutil.Equal(t, "v2 transaction", txn1, types.V2Transaction(v)) - // } + testutil.Equal(t, "events", 2, len(events)) + + testutil.Equal(t, "event 0", txn1, types.V2Transaction(events[0].Data.(wallet.EventV2Transaction))) + testutil.Equal(t, "event 1", genesisBlock.Transactions[0], events[1].Data.(wallet.EventV1Transaction).Transaction) } { @@ -954,6 +952,9 @@ func TestV2FileContractResolution(t *testing.T) { addr1 := types.StandardUnlockHash(pk1.PublicKey()) addr1Policy := types.SpendPolicy{Type: types.PolicyTypeUnlockConditions(types.StandardUnlockConditions(pk1.PublicKey()))} + pk2 := types.GeneratePrivateKey() + addr2 := types.StandardUnlockHash(pk2.PublicKey()) + renterPrivateKey := types.GeneratePrivateKey() renterPublicKey := renterPrivateKey.PublicKey() @@ -967,7 +968,7 @@ func TestV2FileContractResolution(t *testing.T) { }) giftSC := genesisBlock.Transactions[0].SiacoinOutputs[0].Value - v1FC := testutil.PrepareContractFormation(renterPublicKey, hostPublicKey, types.Siacoins(1), types.Siacoins(1), 100, 105, types.VoidAddress) + v1FC := testutil.PrepareContractFormation(renterPublicKey, hostPublicKey, types.Siacoins(1), types.Siacoins(1), 100, 105, addr2) v1FC.Filesize = 65 data := make([]byte, 2*rhp2.LeafSize) @@ -1167,12 +1168,29 @@ func TestV2FileContractResolution(t *testing.T) { } { - for _, addr := range []types.Address{types.VoidAddress, addr1} { - _, err := db.AddressEvents(addr, 0, math.MaxInt64) - if err != nil { - t.Fatal(err) - } + events, err := db.AddressEvents(addr2, 0, math.MaxInt64) + if err != nil { + t.Fatal(err) } + testutil.Equal(t, "events", 3, len(events)) + + ev0 := events[0].Data.(wallet.EventV2ContractResolution) + testutil.Equal(t, "event 0 parent ID", v2FC3ID, ev0.Resolution.Parent.ID) + testutil.Equal(t, "event 0 output ID", v2FC3ID.V2RenterOutputID(), ev0.SiacoinElement.ID) + testutil.Equal(t, "event 0 missed", true, ev0.Missed) + testutil.Equal(t, "event 0 resolution", txn4.FileContractResolutions[0], ev0.Resolution) + + ev1 := events[1].Data.(wallet.EventV2ContractResolution) + testutil.Equal(t, "event 0 parent ID", v2FC2ID, ev1.Resolution.Parent.ID) + testutil.Equal(t, "event 0 output ID", v2FC2ID.V2RenterOutputID(), ev1.SiacoinElement.ID) + testutil.Equal(t, "event 0 missed", false, ev1.Missed) + testutil.Equal(t, "event 0 resolution", txn3.FileContractResolutions[0], ev1.Resolution) + + ev2 := events[2].Data.(wallet.EventV2ContractResolution) + testutil.Equal(t, "event 0 parent ID", v2FC1ID, ev2.Resolution.Parent.ID) + testutil.Equal(t, "event 0 output ID", v2FC1ID.V2RenterOutputID(), ev2.SiacoinElement.ID) + testutil.Equal(t, "event 0 missed", false, ev2.Missed) + testutil.Equal(t, "event 0 resolution", txn2.FileContractResolutions[0], ev2.Resolution) } // revert the block @@ -1268,13 +1286,4 @@ func TestV2FileContractResolution(t *testing.T) { testutil.CheckV2FC(t, txn1.FileContracts[2], fcs[2]) testutil.CheckV2FC(t, txn1.FileContracts[3], fcs[3]) } - - { - for _, addr := range []types.Address{types.VoidAddress, addr1} { - _, err := db.AddressEvents(addr, 0, math.MaxInt64) - if err != nil { - t.Fatal(err) - } - } - } }