Skip to content

Commit 6f8b55c

Browse files
chris124567ChrisSchinnerl
authored andcommitted
replace TestTip/TestRevertTip, TestMissingBlock with refactored equivalents
1 parent 86a069d commit 6f8b55c

File tree

2 files changed

+66
-112
lines changed

2 files changed

+66
-112
lines changed

persist/sqlite/consensus_refactored_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sqlite_test
22

33
import (
4+
"errors"
45
"math"
56
"path/filepath"
67
"testing"
@@ -843,3 +844,68 @@ func TestEphemeralSiafundBalance(t *testing.T) {
843844
checkBalance(addr2, 0)
844845
checkBalance(addr3, 0)
845846
}
847+
848+
func TestTip(t *testing.T) {
849+
n := newTestChain(t, false, nil)
850+
851+
checkTips := func() {
852+
t.Helper()
853+
854+
tip, err := n.db.Tip()
855+
if err != nil {
856+
t.Fatal(err)
857+
}
858+
testutil.Equal(t, "tip", n.tipState().Index, tip)
859+
860+
for _, state := range n.states {
861+
best, err := n.db.BestTip(state.Index.Height)
862+
if err != nil {
863+
t.Fatal(err)
864+
}
865+
testutil.Equal(t, "best tip", state.Index, best)
866+
}
867+
}
868+
checkTips()
869+
870+
n.mineTransactions(t)
871+
checkTips()
872+
873+
n.mineTransactions(t)
874+
checkTips()
875+
876+
n.revertBlock(t)
877+
checkTips()
878+
879+
n.revertBlock(t)
880+
checkTips()
881+
}
882+
883+
func TestMissingTip(t *testing.T) {
884+
n := newTestChain(t, false, nil)
885+
886+
_, err := n.db.BestTip(n.tipState().Index.Height)
887+
if err != nil {
888+
t.Fatalf("error retrieving tip known to exist: %v", err)
889+
}
890+
891+
_, err = n.db.BestTip(n.tipState().Index.Height + 1)
892+
if !errors.Is(err, explorer.ErrNoTip) {
893+
t.Fatalf("should have got ErrNoTip retrieving: %v", err)
894+
}
895+
}
896+
897+
func TestMissingBlock(t *testing.T) {
898+
n := newTestChain(t, false, nil)
899+
900+
id := n.tipState().Index.ID
901+
_, err := n.db.Block(id)
902+
if err != nil {
903+
t.Fatalf("error retrieving genesis block: %v", err)
904+
}
905+
906+
id[0] ^= 255
907+
_, err = n.db.Block(id)
908+
if !errors.Is(err, explorer.ErrNoBlock) {
909+
t.Fatalf("did not get ErrNoBlock retrieving missing block: %v", err)
910+
}
911+
}

persist/sqlite/consensus_test.go

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -424,50 +424,6 @@ func TestSendTransactions(t *testing.T) {
424424
}
425425
}
426426

427-
func TestTip(t *testing.T) {
428-
_, _, cm, db := newStore(t, false, nil)
429-
430-
const n = 100
431-
for i := cm.Tip().Height; i < n; i++ {
432-
if err := cm.AddBlocks([]types.Block{testutil.MineBlock(cm.TipState(), nil, types.VoidAddress)}); err != nil {
433-
t.Fatal(err)
434-
}
435-
syncDB(t, db, cm)
436-
437-
tip, err := db.Tip()
438-
if err != nil {
439-
t.Fatal(err)
440-
}
441-
testutil.Equal(t, "tip", cm.Tip(), tip)
442-
}
443-
444-
for i := 0; i < n; i++ {
445-
best, err := db.BestTip(uint64(i))
446-
if err != nil {
447-
t.Fatal(err)
448-
}
449-
if cmBest, ok := cm.BestIndex(uint64(i)); !ok || cmBest != best {
450-
t.Fatal("best tip mismatch")
451-
}
452-
}
453-
}
454-
455-
func TestMissingBlock(t *testing.T) {
456-
_, _, cm, db := newStore(t, false, nil)
457-
458-
id := cm.Tip().ID
459-
_, err := db.Block(id)
460-
if err != nil {
461-
t.Fatalf("error retrieving genesis block: %v", err)
462-
}
463-
464-
id[0] ^= 255
465-
_, err = db.Block(id)
466-
if !errors.Is(err, explorer.ErrNoBlock) {
467-
t.Fatalf("did not get ErrNoBlock retrieving missing block: %v", err)
468-
}
469-
}
470-
471427
func TestFileContract(t *testing.T) {
472428
pk1 := types.GeneratePrivateKey()
473429
addr1 := types.StandardUnlockHash(pk1.PublicKey())
@@ -968,74 +924,6 @@ func TestEphemeralFileContract(t *testing.T) {
968924
}
969925
}
970926

971-
func TestRevertTip(t *testing.T) {
972-
pk1 := types.GeneratePrivateKey()
973-
addr1 := types.StandardUnlockHash(pk1.PublicKey())
974-
975-
pk2 := types.GeneratePrivateKey()
976-
addr2 := types.StandardUnlockHash(pk2.PublicKey())
977-
978-
_, _, cm, db := newStore(t, false, nil)
979-
genesisState := cm.TipState()
980-
981-
const n = 100
982-
for i := cm.Tip().Height; i < n; i++ {
983-
if err := cm.AddBlocks([]types.Block{testutil.MineBlock(cm.TipState(), nil, addr1)}); err != nil {
984-
t.Fatal(err)
985-
}
986-
syncDB(t, db, cm)
987-
988-
tip, err := db.Tip()
989-
if err != nil {
990-
t.Fatal(err)
991-
}
992-
testutil.Equal(t, "tip", cm.Tip(), tip)
993-
}
994-
995-
CheckMetrics(t, db, cm, explorer.Metrics{
996-
TotalHosts: 0,
997-
ActiveContracts: 0,
998-
StorageUtilization: 0,
999-
})
1000-
1001-
{
1002-
// mine to trigger a reorg
1003-
var blocks []types.Block
1004-
state := genesisState
1005-
for i := uint64(0); i < n+5; i++ {
1006-
blocks = append(blocks, testutil.MineBlock(state, nil, addr2))
1007-
state.Index.ID = blocks[len(blocks)-1].ID()
1008-
state.Index.Height++
1009-
}
1010-
if err := cm.AddBlocks(blocks); err != nil {
1011-
t.Fatal(err)
1012-
}
1013-
syncDB(t, db, cm)
1014-
1015-
tip, err := db.Tip()
1016-
if err != nil {
1017-
t.Fatal(err)
1018-
}
1019-
testutil.Equal(t, "tip", cm.Tip(), tip)
1020-
}
1021-
1022-
CheckMetrics(t, db, cm, explorer.Metrics{
1023-
TotalHosts: 0,
1024-
ActiveContracts: 0,
1025-
StorageUtilization: 0,
1026-
})
1027-
1028-
for i := 0; i < n; i++ {
1029-
best, err := db.BestTip(uint64(i))
1030-
if err != nil {
1031-
t.Fatal(err)
1032-
}
1033-
if cmBest, ok := cm.BestIndex(uint64(i)); !ok || cmBest != best {
1034-
t.Fatal("best tip mismatch")
1035-
}
1036-
}
1037-
}
1038-
1039927
func TestRevertBalance(t *testing.T) {
1040928
// Generate three addresses: addr1, addr2, addr3
1041929
pk1 := types.GeneratePrivateKey()

0 commit comments

Comments
 (0)