Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chris124567 committed Dec 9, 2024
1 parent 6dd0557 commit 991ed7d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 74 deletions.
4 changes: 2 additions & 2 deletions internal/testutil/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down
81 changes: 31 additions & 50 deletions persist/sqlite/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"math"
"path/filepath"
"reflect"
"testing"
"time"

Expand All @@ -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"
Expand Down Expand Up @@ -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()

Expand All @@ -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,
Expand Down Expand Up @@ -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)
}

{
Expand Down Expand Up @@ -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:]
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}

{
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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)
}
}

Expand Down
1 change: 1 addition & 0 deletions persist/sqlite/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
53 changes: 31 additions & 22 deletions persist/sqlite/v2consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package sqlite_test
import (
"bytes"
"math"
"reflect"
"testing"
"time"

"go.sia.tech/core/consensus"
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"
)
Expand Down Expand Up @@ -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)
}

{
Expand Down Expand Up @@ -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()

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}
}

0 comments on commit 991ed7d

Please sign in to comment.