Set up tests for error handling in snowstorm

This commit is contained in:
StephenButtolph 2020-06-21 22:38:53 -04:00
parent 6c34fd79eb
commit fb7e491000
4 changed files with 351 additions and 211 deletions

View File

@ -4,6 +4,7 @@
package snowstorm
import (
"errors"
"testing"
"github.com/prometheus/client_golang/prometheus"
@ -19,6 +20,28 @@ var (
Green = &TestTx{Identifier: ids.Empty.Prefix(1)}
Blue = &TestTx{Identifier: ids.Empty.Prefix(2)}
Alpha = &TestTx{Identifier: ids.Empty.Prefix(3)}
Tests = []func(*testing.T, Factory){
MetricsTest,
ParamsTest,
IssuedTest,
LeftoverInputTest,
LowerConfidenceTest,
MiddleConfidenceTest,
IndependentTest,
VirtuousTest,
IsVirtuousTest,
QuiesceTest,
AcceptingDependencyTest,
RejectingDependencyTest,
VacuouslyAcceptedTest,
ConflictsTest,
VirtuousDependsOnRogueTest,
ErrorOnVacuouslyAcceptedTest,
ErrorOnAcceptedTest,
ErrorOnRejectingLowerConfidenceConflictTest,
ErrorOnRejectingHigherConfidenceConflictTest,
}
)
// R - G - B - A
@ -46,6 +69,52 @@ func Setup() {
Alpha.Reset()
}
// Execute all tests against a consensus implementation
func ConsensusTest(t *testing.T, factory Factory, prefix string) {
for _, test := range Tests {
test(t, factory)
}
StringTest(t, factory, prefix)
}
func MetricsTest(t *testing.T, factory Factory) {
Setup()
{
params := snowball.Parameters{
Metrics: prometheus.NewRegistry(),
K: 2, Alpha: 2, BetaVirtuous: 1, BetaRogue: 2,
}
params.Metrics.Register(prometheus.NewCounter(prometheus.CounterOpts{
Name: "tx_processing",
}))
graph := factory.New()
graph.Initialize(snow.DefaultContextTest(), params)
}
{
params := snowball.Parameters{
Metrics: prometheus.NewRegistry(),
K: 2, Alpha: 2, BetaVirtuous: 1, BetaRogue: 2,
}
params.Metrics.Register(prometheus.NewCounter(prometheus.CounterOpts{
Name: "tx_accepted",
}))
graph := factory.New()
graph.Initialize(snow.DefaultContextTest(), params)
}
{
params := snowball.Parameters{
Metrics: prometheus.NewRegistry(),
K: 2, Alpha: 2, BetaVirtuous: 1, BetaRogue: 2,
}
params.Metrics.Register(prometheus.NewCounter(prometheus.CounterOpts{
Name: "tx_rejected",
}))
graph := factory.New()
graph.Initialize(snow.DefaultContextTest(), params)
}
}
func ParamsTest(t *testing.T, factory Factory) {
Setup()
@ -81,15 +150,13 @@ func IssuedTest(t *testing.T, factory Factory) {
if issued := graph.Issued(Red); issued {
t.Fatalf("Haven't issued anything yet.")
}
graph.Add(Red)
if issued := graph.Issued(Red); !issued {
} else if err := graph.Add(Red); err != nil {
t.Fatal(err)
} else if issued := graph.Issued(Red); !issued {
t.Fatalf("Have already issued.")
}
Blue.Accept()
_ = Blue.Accept()
if issued := graph.Issued(Blue); !issued {
t.Fatalf("Have already accepted.")
@ -106,10 +173,12 @@ func LeftoverInputTest(t *testing.T, factory Factory) {
K: 2, Alpha: 2, BetaVirtuous: 1, BetaRogue: 1,
}
graph.Initialize(snow.DefaultContextTest(), params)
graph.Add(Red)
graph.Add(Green)
if prefs := graph.Preferences(); prefs.Len() != 1 {
if err := graph.Add(Red); err != nil {
t.Fatal(err)
} else if err := graph.Add(Green); err != nil {
t.Fatal(err)
} else if prefs := graph.Preferences(); prefs.Len() != 1 {
t.Fatalf("Wrong number of preferences.")
} else if !prefs.Contains(Red.ID()) {
t.Fatalf("Wrong preference. Expected %s got %s", Red.ID(), prefs.List()[0])
@ -120,15 +189,13 @@ func LeftoverInputTest(t *testing.T, factory Factory) {
r := ids.Bag{}
r.SetThreshold(2)
r.AddCount(Red.ID(), 2)
graph.RecordPoll(r)
if prefs := graph.Preferences(); prefs.Len() != 0 {
if err := graph.RecordPoll(r); err != nil {
t.Fatal(err)
} else if prefs := graph.Preferences(); prefs.Len() != 0 {
t.Fatalf("Wrong number of preferences.")
} else if !graph.Finalized() {
t.Fatalf("Finalized too late")
}
if Red.Status() != choices.Accepted {
} else if Red.Status() != choices.Accepted {
t.Fatalf("%s should have been accepted", Red.ID())
} else if Green.Status() != choices.Rejected {
t.Fatalf("%s should have been rejected", Green.ID())
@ -145,11 +212,14 @@ func LowerConfidenceTest(t *testing.T, factory Factory) {
K: 2, Alpha: 2, BetaVirtuous: 1, BetaRogue: 1,
}
graph.Initialize(snow.DefaultContextTest(), params)
graph.Add(Red)
graph.Add(Green)
graph.Add(Blue)
if prefs := graph.Preferences(); prefs.Len() != 1 {
if err := graph.Add(Red); err != nil {
t.Fatal(err)
} else if err := graph.Add(Green); err != nil {
t.Fatal(err)
} else if err := graph.Add(Blue); err != nil {
t.Fatal(err)
} else if prefs := graph.Preferences(); prefs.Len() != 1 {
t.Fatalf("Wrong number of preferences.")
} else if !prefs.Contains(Red.ID()) {
t.Fatalf("Wrong preference. Expected %s got %s", Red.ID(), prefs.List()[0])
@ -160,9 +230,9 @@ func LowerConfidenceTest(t *testing.T, factory Factory) {
r := ids.Bag{}
r.SetThreshold(2)
r.AddCount(Red.ID(), 2)
graph.RecordPoll(r)
if prefs := graph.Preferences(); prefs.Len() != 1 {
if err := graph.RecordPoll(r); err != nil {
t.Fatal(err)
} else if prefs := graph.Preferences(); prefs.Len() != 1 {
t.Fatalf("Wrong number of preferences.")
} else if !prefs.Contains(Blue.ID()) {
t.Fatalf("Wrong preference. Expected %s", Blue.ID())
@ -181,12 +251,16 @@ func MiddleConfidenceTest(t *testing.T, factory Factory) {
K: 2, Alpha: 2, BetaVirtuous: 1, BetaRogue: 1,
}
graph.Initialize(snow.DefaultContextTest(), params)
graph.Add(Red)
graph.Add(Green)
graph.Add(Alpha)
graph.Add(Blue)
if prefs := graph.Preferences(); prefs.Len() != 2 {
if err := graph.Add(Red); err != nil {
t.Fatal(err)
} else if err := graph.Add(Green); err != nil {
t.Fatal(err)
} else if err := graph.Add(Alpha); err != nil {
t.Fatal(err)
} else if err := graph.Add(Blue); err != nil {
t.Fatal(err)
} else if prefs := graph.Preferences(); prefs.Len() != 2 {
t.Fatalf("Wrong number of preferences.")
} else if !prefs.Contains(Red.ID()) {
t.Fatalf("Wrong preference. Expected %s", Red.ID())
@ -199,9 +273,9 @@ func MiddleConfidenceTest(t *testing.T, factory Factory) {
r := ids.Bag{}
r.SetThreshold(2)
r.AddCount(Red.ID(), 2)
graph.RecordPoll(r)
if prefs := graph.Preferences(); prefs.Len() != 1 {
if err := graph.RecordPoll(r); err != nil {
t.Fatal(err)
} else if prefs := graph.Preferences(); prefs.Len() != 1 {
t.Fatalf("Wrong number of preferences.")
} else if !prefs.Contains(Alpha.ID()) {
t.Fatalf("Wrong preference. Expected %s", Alpha.ID())
@ -209,6 +283,7 @@ func MiddleConfidenceTest(t *testing.T, factory Factory) {
t.Fatalf("Finalized too early")
}
}
func IndependentTest(t *testing.T, factory Factory) {
Setup()
@ -219,10 +294,12 @@ func IndependentTest(t *testing.T, factory Factory) {
K: 2, Alpha: 2, BetaVirtuous: 2, BetaRogue: 2,
}
graph.Initialize(snow.DefaultContextTest(), params)
graph.Add(Red)
graph.Add(Alpha)
if prefs := graph.Preferences(); prefs.Len() != 2 {
if err := graph.Add(Red); err != nil {
t.Fatal(err)
} else if err := graph.Add(Alpha); err != nil {
t.Fatal(err)
} else if prefs := graph.Preferences(); prefs.Len() != 2 {
t.Fatalf("Wrong number of preferences.")
} else if !prefs.Contains(Red.ID()) {
t.Fatalf("Wrong preference. Expected %s", Red.ID())
@ -236,9 +313,9 @@ func IndependentTest(t *testing.T, factory Factory) {
ra.SetThreshold(2)
ra.AddCount(Red.ID(), 2)
ra.AddCount(Alpha.ID(), 2)
graph.RecordPoll(ra)
if prefs := graph.Preferences(); prefs.Len() != 2 {
if err := graph.RecordPoll(ra); err != nil {
t.Fatal(err)
} else if prefs := graph.Preferences(); prefs.Len() != 2 {
t.Fatalf("Wrong number of preferences.")
} else if !prefs.Contains(Red.ID()) {
t.Fatalf("Wrong preference. Expected %s", Red.ID())
@ -246,11 +323,9 @@ func IndependentTest(t *testing.T, factory Factory) {
t.Fatalf("Wrong preference. Expected %s", Alpha.ID())
} else if graph.Finalized() {
t.Fatalf("Finalized too early")
}
graph.RecordPoll(ra)
if prefs := graph.Preferences(); prefs.Len() != 0 {
} else if err := graph.RecordPoll(ra); err != nil {
t.Fatal(err)
} else if prefs := graph.Preferences(); prefs.Len() != 0 {
t.Fatalf("Wrong number of preferences.")
} else if !graph.Finalized() {
t.Fatalf("Finalized too late")
@ -267,35 +342,30 @@ func VirtuousTest(t *testing.T, factory Factory) {
K: 2, Alpha: 2, BetaVirtuous: 1, BetaRogue: 1,
}
graph.Initialize(snow.DefaultContextTest(), params)
graph.Add(Red)
if virtuous := graph.Virtuous(); virtuous.Len() != 1 {
if err := graph.Add(Red); err != nil {
t.Fatal(err)
} else if virtuous := graph.Virtuous(); virtuous.Len() != 1 {
t.Fatalf("Wrong number of virtuous.")
} else if !virtuous.Contains(Red.ID()) {
t.Fatalf("Wrong virtuous. Expected %s", Red.ID())
}
graph.Add(Alpha)
if virtuous := graph.Virtuous(); virtuous.Len() != 2 {
} else if err := graph.Add(Alpha); err != nil {
t.Fatal(err)
} else if virtuous := graph.Virtuous(); virtuous.Len() != 2 {
t.Fatalf("Wrong number of virtuous.")
} else if !virtuous.Contains(Red.ID()) {
t.Fatalf("Wrong virtuous. Expected %s", Red.ID())
} else if !virtuous.Contains(Alpha.ID()) {
t.Fatalf("Wrong virtuous. Expected %s", Alpha.ID())
}
graph.Add(Green)
if virtuous := graph.Virtuous(); virtuous.Len() != 1 {
} else if err := graph.Add(Green); err != nil {
t.Fatal(err)
} else if virtuous := graph.Virtuous(); virtuous.Len() != 1 {
t.Fatalf("Wrong number of virtuous.")
} else if !virtuous.Contains(Alpha.ID()) {
t.Fatalf("Wrong virtuous. Expected %s", Alpha.ID())
}
graph.Add(Blue)
if virtuous := graph.Virtuous(); virtuous.Len() != 0 {
} else if err := graph.Add(Blue); err != nil {
t.Fatal(err)
} else if virtuous := graph.Virtuous(); virtuous.Len() != 0 {
t.Fatalf("Wrong number of virtuous.")
}
}
@ -319,11 +389,9 @@ func IsVirtuousTest(t *testing.T, factory Factory) {
t.Fatalf("Should be virtuous")
} else if !graph.IsVirtuous(Alpha) {
t.Fatalf("Should be virtuous")
}
graph.Add(Red)
if !graph.IsVirtuous(Red) {
} else if err := graph.Add(Red); err != nil {
t.Fatal(err)
} else if !graph.IsVirtuous(Red) {
t.Fatalf("Should be virtuous")
} else if graph.IsVirtuous(Green) {
t.Fatalf("Should not be virtuous")
@ -331,11 +399,9 @@ func IsVirtuousTest(t *testing.T, factory Factory) {
t.Fatalf("Should be virtuous")
} else if !graph.IsVirtuous(Alpha) {
t.Fatalf("Should be virtuous")
}
graph.Add(Green)
if graph.IsVirtuous(Red) {
} else if err := graph.Add(Green); err != nil {
t.Fatal(err)
} else if graph.IsVirtuous(Red) {
t.Fatalf("Should not be virtuous")
} else if graph.IsVirtuous(Green) {
t.Fatalf("Should not be virtuous")
@ -357,17 +423,13 @@ func QuiesceTest(t *testing.T, factory Factory) {
if !graph.Quiesce() {
t.Fatalf("Should quiesce")
}
graph.Add(Red)
if graph.Quiesce() {
} else if err := graph.Add(Red); err != nil {
t.Fatal(err)
} else if graph.Quiesce() {
t.Fatalf("Shouldn't quiesce")
}
graph.Add(Green)
if !graph.Quiesce() {
} else if err := graph.Add(Green); err != nil {
t.Fatal(err)
} else if !graph.Quiesce() {
t.Fatalf("Should quiesce")
}
}
@ -390,11 +452,13 @@ func AcceptingDependencyTest(t *testing.T, factory Factory) {
}
graph.Initialize(snow.DefaultContextTest(), params)
graph.Add(Red)
graph.Add(Green)
graph.Add(purple)
if prefs := graph.Preferences(); prefs.Len() != 2 {
if err := graph.Add(Red); err != nil {
t.Fatal(err)
} else if err := graph.Add(Green); err != nil {
t.Fatal(err)
} else if err := graph.Add(purple); err != nil {
t.Fatal(err)
} else if prefs := graph.Preferences(); prefs.Len() != 2 {
t.Fatalf("Wrong number of preferences.")
} else if !prefs.Contains(Red.ID()) {
t.Fatalf("Wrong preference. Expected %s", Red.ID())
@ -410,10 +474,9 @@ func AcceptingDependencyTest(t *testing.T, factory Factory) {
g := ids.Bag{}
g.Add(Green.ID())
graph.RecordPoll(g)
if prefs := graph.Preferences(); prefs.Len() != 2 {
if err := graph.RecordPoll(g); err != nil {
t.Fatal(err)
} else if prefs := graph.Preferences(); prefs.Len() != 2 {
t.Fatalf("Wrong number of preferences.")
} else if !prefs.Contains(Green.ID()) {
t.Fatalf("Wrong preference. Expected %s", Green.ID())
@ -429,10 +492,9 @@ func AcceptingDependencyTest(t *testing.T, factory Factory) {
rp := ids.Bag{}
rp.Add(Red.ID(), purple.ID())
graph.RecordPoll(rp)
if prefs := graph.Preferences(); prefs.Len() != 2 {
if err := graph.RecordPoll(rp); err != nil {
t.Fatal(err)
} else if prefs := graph.Preferences(); prefs.Len() != 2 {
t.Fatalf("Wrong number of preferences.")
} else if !prefs.Contains(Green.ID()) {
t.Fatalf("Wrong preference. Expected %s", Green.ID())
@ -448,10 +510,9 @@ func AcceptingDependencyTest(t *testing.T, factory Factory) {
r := ids.Bag{}
r.Add(Red.ID())
graph.RecordPoll(r)
if prefs := graph.Preferences(); prefs.Len() != 0 {
if err := graph.RecordPoll(r); err != nil {
t.Fatal(err)
} else if prefs := graph.Preferences(); prefs.Len() != 0 {
t.Fatalf("Wrong number of preferences.")
} else if Red.Status() != choices.Accepted {
t.Fatalf("Wrong status. %s should be %s", Red.ID(), choices.Accepted)
@ -480,12 +541,15 @@ func RejectingDependencyTest(t *testing.T, factory Factory) {
}
graph.Initialize(snow.DefaultContextTest(), params)
graph.Add(Red)
graph.Add(Green)
graph.Add(Blue)
graph.Add(purple)
if prefs := graph.Preferences(); prefs.Len() != 2 {
if err := graph.Add(Red); err != nil {
t.Fatal(err)
} else if err := graph.Add(Green); err != nil {
t.Fatal(err)
} else if err := graph.Add(Blue); err != nil {
t.Fatal(err)
} else if err := graph.Add(purple); err != nil {
t.Fatal(err)
} else if prefs := graph.Preferences(); prefs.Len() != 2 {
t.Fatalf("Wrong number of preferences.")
} else if !prefs.Contains(Red.ID()) {
t.Fatalf("Wrong preference. Expected %s", Red.ID())
@ -503,10 +567,9 @@ func RejectingDependencyTest(t *testing.T, factory Factory) {
gp := ids.Bag{}
gp.Add(Green.ID(), purple.ID())
graph.RecordPoll(gp)
if prefs := graph.Preferences(); prefs.Len() != 2 {
if err := graph.RecordPoll(gp); err != nil {
t.Fatal(err)
} else if prefs := graph.Preferences(); prefs.Len() != 2 {
t.Fatalf("Wrong number of preferences.")
} else if !prefs.Contains(Green.ID()) {
t.Fatalf("Wrong preference. Expected %s", Green.ID())
@ -520,11 +583,9 @@ func RejectingDependencyTest(t *testing.T, factory Factory) {
t.Fatalf("Wrong status. %s should be %s", Blue.ID(), choices.Processing)
} else if purple.Status() != choices.Processing {
t.Fatalf("Wrong status. %s should be %s", purple.ID(), choices.Processing)
}
graph.RecordPoll(gp)
if prefs := graph.Preferences(); prefs.Len() != 0 {
} else if err := graph.RecordPoll(gp); err != nil {
t.Fatal(err)
} else if prefs := graph.Preferences(); prefs.Len() != 0 {
t.Fatalf("Wrong number of preferences.")
} else if Red.Status() != choices.Rejected {
t.Fatalf("Wrong status. %s should be %s", Red.ID(), choices.Rejected)
@ -553,9 +614,9 @@ func VacuouslyAcceptedTest(t *testing.T, factory Factory) {
}
graph.Initialize(snow.DefaultContextTest(), params)
graph.Add(purple)
if prefs := graph.Preferences(); prefs.Len() != 0 {
if err := graph.Add(purple); err != nil {
t.Fatal(err)
} else if prefs := graph.Preferences(); prefs.Len() != 0 {
t.Fatalf("Wrong number of preferences.")
} else if status := purple.Status(); status != choices.Accepted {
t.Fatalf("Wrong status. %s should be %s", purple.ID(), choices.Accepted)
@ -593,17 +654,15 @@ func ConflictsTest(t *testing.T, factory Factory) {
Ins: insPurple,
}
graph.Add(purple)
if orangeConflicts := graph.Conflicts(orange); orangeConflicts.Len() != 1 {
if err := graph.Add(purple); err != nil {
t.Fatal(err)
} else if orangeConflicts := graph.Conflicts(orange); orangeConflicts.Len() != 1 {
t.Fatalf("Wrong number of conflicts")
} else if !orangeConflicts.Contains(purple.Identifier) {
t.Fatalf("Conflicts does not contain the right transaction")
}
graph.Add(orange)
if orangeConflicts := graph.Conflicts(orange); orangeConflicts.Len() != 1 {
} else if err := graph.Add(orange); err != nil {
t.Fatal(err)
} else if orangeConflicts := graph.Conflicts(orange); orangeConflicts.Len() != 1 {
t.Fatalf("Wrong number of conflicts")
} else if !orangeConflicts.Contains(purple.Identifier) {
t.Fatalf("Conflicts does not contain the right transaction")
@ -643,17 +702,20 @@ func VirtuousDependsOnRogueTest(t *testing.T, factory Factory) {
virtuous.Ins.Add(input2)
graph.Add(rogue1)
graph.Add(rogue2)
graph.Add(virtuous)
if err := graph.Add(rogue1); err != nil {
t.Fatal(err)
} else if err := graph.Add(rogue2); err != nil {
t.Fatal(err)
} else if err := graph.Add(virtuous); err != nil {
t.Fatal(err)
}
votes := ids.Bag{}
votes.Add(rogue1.ID())
votes.Add(virtuous.ID())
graph.RecordPoll(votes)
if status := rogue1.Status(); status != choices.Processing {
if err := graph.RecordPoll(votes); err != nil {
t.Fatal(err)
} else if status := rogue1.Status(); status != choices.Processing {
t.Fatalf("Rogue Tx is %s expected %s", status, choices.Processing)
} else if status := rogue2.Status(); status != choices.Processing {
t.Fatalf("Rogue Tx is %s expected %s", status, choices.Processing)
@ -664,6 +726,135 @@ func VirtuousDependsOnRogueTest(t *testing.T, factory Factory) {
}
}
func ErrorOnVacuouslyAcceptedTest(t *testing.T, factory Factory) {
Setup()
graph := factory.New()
purple := &TestTx{
Identifier: ids.Empty.Prefix(7),
Stat: choices.Processing,
Validity: errors.New(""),
}
params := snowball.Parameters{
Metrics: prometheus.NewRegistry(),
K: 1, Alpha: 1, BetaVirtuous: 1, BetaRogue: 2,
}
graph.Initialize(snow.DefaultContextTest(), params)
if err := graph.Add(purple); err == nil {
t.Fatalf("Should have errored on acceptance")
}
}
func ErrorOnAcceptedTest(t *testing.T, factory Factory) {
Setup()
graph := factory.New()
purple := &TestTx{
Identifier: ids.Empty.Prefix(7),
Stat: choices.Processing,
Validity: errors.New(""),
}
purple.Ins.Add(ids.Empty.Prefix(4))
params := snowball.Parameters{
Metrics: prometheus.NewRegistry(),
K: 1, Alpha: 1, BetaVirtuous: 1, BetaRogue: 2,
}
graph.Initialize(snow.DefaultContextTest(), params)
if err := graph.Add(purple); err != nil {
t.Fatal(err)
}
votes := ids.Bag{}
votes.Add(purple.ID())
if err := graph.RecordPoll(votes); err == nil {
t.Fatalf("Should have errored on accepting an invalid tx")
}
}
func ErrorOnRejectingLowerConfidenceConflictTest(t *testing.T, factory Factory) {
Setup()
graph := factory.New()
X := ids.Empty.Prefix(4)
purple := &TestTx{
Identifier: ids.Empty.Prefix(7),
Stat: choices.Processing,
}
purple.Ins.Add(X)
pink := &TestTx{
Identifier: ids.Empty.Prefix(8),
Stat: choices.Processing,
Validity: errors.New(""),
}
pink.Ins.Add(X)
params := snowball.Parameters{
Metrics: prometheus.NewRegistry(),
K: 1, Alpha: 1, BetaVirtuous: 1, BetaRogue: 1,
}
graph.Initialize(snow.DefaultContextTest(), params)
if err := graph.Add(purple); err != nil {
t.Fatal(err)
} else if err := graph.Add(pink); err != nil {
t.Fatal(err)
}
votes := ids.Bag{}
votes.Add(purple.ID())
if err := graph.RecordPoll(votes); err == nil {
t.Fatalf("Should have errored on rejecting an invalid tx")
}
}
func ErrorOnRejectingHigherConfidenceConflictTest(t *testing.T, factory Factory) {
Setup()
graph := factory.New()
X := ids.Empty.Prefix(4)
purple := &TestTx{
Identifier: ids.Empty.Prefix(7),
Stat: choices.Processing,
}
purple.Ins.Add(X)
pink := &TestTx{
Identifier: ids.Empty.Prefix(8),
Stat: choices.Processing,
Validity: errors.New(""),
}
pink.Ins.Add(X)
params := snowball.Parameters{
Metrics: prometheus.NewRegistry(),
K: 1, Alpha: 1, BetaVirtuous: 1, BetaRogue: 1,
}
graph.Initialize(snow.DefaultContextTest(), params)
if err := graph.Add(pink); err != nil {
t.Fatal(err)
} else if err := graph.Add(purple); err != nil {
t.Fatal(err)
}
votes := ids.Bag{}
votes.Add(purple.ID())
if err := graph.RecordPoll(votes); err == nil {
t.Fatalf("Should have errored on rejecting an invalid tx")
}
}
func StringTest(t *testing.T, factory Factory, prefix string) {
Setup()
@ -674,12 +865,16 @@ func StringTest(t *testing.T, factory Factory, prefix string) {
K: 2, Alpha: 2, BetaVirtuous: 1, BetaRogue: 2,
}
graph.Initialize(snow.DefaultContextTest(), params)
graph.Add(Red)
graph.Add(Green)
graph.Add(Blue)
graph.Add(Alpha)
if prefs := graph.Preferences(); prefs.Len() != 1 {
if err := graph.Add(Red); err != nil {
t.Fatal(err)
} else if err := graph.Add(Green); err != nil {
t.Fatal(err)
} else if err := graph.Add(Blue); err != nil {
t.Fatal(err)
} else if err := graph.Add(Alpha); err != nil {
t.Fatal(err)
} else if prefs := graph.Preferences(); prefs.Len() != 1 {
t.Fatalf("Wrong number of preferences.")
} else if !prefs.Contains(Red.ID()) {
t.Fatalf("Wrong preference. Expected %s got %s", Red.ID(), prefs.List()[0])
@ -691,8 +886,11 @@ func StringTest(t *testing.T, factory Factory, prefix string) {
rb.SetThreshold(2)
rb.AddCount(Red.ID(), 2)
rb.AddCount(Blue.ID(), 2)
graph.RecordPoll(rb)
graph.Add(Blue)
if err := graph.RecordPoll(rb); err != nil {
t.Fatal(err)
} else if err := graph.Add(Blue); err != nil {
t.Fatal(err)
}
{
expected := prefix + "(\n" +
@ -720,7 +918,9 @@ func StringTest(t *testing.T, factory Factory, prefix string) {
ga.SetThreshold(2)
ga.AddCount(Green.ID(), 2)
ga.AddCount(Alpha.ID(), 2)
graph.RecordPoll(ga)
if err := graph.RecordPoll(ga); err != nil {
t.Fatal(err)
}
{
expected := prefix + "(\n" +
@ -745,7 +945,9 @@ func StringTest(t *testing.T, factory Factory, prefix string) {
}
empty := ids.Bag{}
graph.RecordPoll(empty)
if err := graph.RecordPoll(empty); err != nil {
t.Fatal(err)
}
{
expected := prefix + "(\n" +
@ -767,10 +969,10 @@ func StringTest(t *testing.T, factory Factory, prefix string) {
t.Fatalf("Wrong preference. Expected %s", Blue.ID())
} else if graph.Finalized() {
t.Fatalf("Finalized too early")
} else if err := graph.RecordPoll(ga); err != nil {
t.Fatal(err)
}
graph.RecordPoll(ga)
{
expected := prefix + "(\n" +
" Choice[0] = ID: LUC1cmcxnfNR9LdkACS2ccGKLEK7SYqB4gLLTycQfg1koyfSq Confidence: 0 Bias: 1\n" +
@ -791,10 +993,10 @@ func StringTest(t *testing.T, factory Factory, prefix string) {
t.Fatalf("Wrong preference. Expected %s", Alpha.ID())
} else if graph.Finalized() {
t.Fatalf("Finalized too early")
} else if err := graph.RecordPoll(ga); err != nil {
t.Fatal(err)
}
graph.RecordPoll(ga)
{
expected := prefix + "()"
if str := graph.String(); str != expected {
@ -806,9 +1008,7 @@ func StringTest(t *testing.T, factory Factory, prefix string) {
t.Fatalf("Wrong number of preferences.")
} else if !graph.Finalized() {
t.Fatalf("Finalized too late")
}
if Green.Status() != choices.Accepted {
} else if Green.Status() != choices.Accepted {
t.Fatalf("%s should have been accepted", Green.ID())
} else if Alpha.Status() != choices.Accepted {
t.Fatalf("%s should have been accepted", Alpha.ID())
@ -816,10 +1016,10 @@ func StringTest(t *testing.T, factory Factory, prefix string) {
t.Fatalf("%s should have been rejected", Red.ID())
} else if Blue.Status() != choices.Rejected {
t.Fatalf("%s should have been rejected", Blue.ID())
} else if err := graph.RecordPoll(rb); err != nil {
t.Fatal(err)
}
graph.RecordPoll(rb)
{
expected := prefix + "()"
if str := graph.String(); str != expected {
@ -831,9 +1031,7 @@ func StringTest(t *testing.T, factory Factory, prefix string) {
t.Fatalf("Wrong number of preferences.")
} else if !graph.Finalized() {
t.Fatalf("Finalized too late")
}
if Green.Status() != choices.Accepted {
} else if Green.Status() != choices.Accepted {
t.Fatalf("%s should have been accepted", Green.ID())
} else if Alpha.Status() != choices.Accepted {
t.Fatalf("%s should have been accepted", Alpha.ID())

View File

@ -7,34 +7,4 @@ import (
"testing"
)
func TestDirectedParams(t *testing.T) { ParamsTest(t, DirectedFactory{}) }
func TestDirectedIssued(t *testing.T) { IssuedTest(t, DirectedFactory{}) }
func TestDirectedLeftoverInput(t *testing.T) { LeftoverInputTest(t, DirectedFactory{}) }
func TestDirectedLowerConfidence(t *testing.T) { LowerConfidenceTest(t, DirectedFactory{}) }
func TestDirectedMiddleConfidence(t *testing.T) { MiddleConfidenceTest(t, DirectedFactory{}) }
func TestDirectedIndependent(t *testing.T) { IndependentTest(t, DirectedFactory{}) }
func TestDirectedVirtuous(t *testing.T) { VirtuousTest(t, DirectedFactory{}) }
func TestDirectedIsVirtuous(t *testing.T) { IsVirtuousTest(t, DirectedFactory{}) }
func TestDirectedConflicts(t *testing.T) { ConflictsTest(t, DirectedFactory{}) }
func TestDirectedQuiesce(t *testing.T) { QuiesceTest(t, DirectedFactory{}) }
func TestDirectedAcceptingDependency(t *testing.T) { AcceptingDependencyTest(t, DirectedFactory{}) }
func TestDirectedRejectingDependency(t *testing.T) { RejectingDependencyTest(t, DirectedFactory{}) }
func TestDirectedVacuouslyAccepted(t *testing.T) { VacuouslyAcceptedTest(t, DirectedFactory{}) }
func TestDirectedVirtuousDependsOnRogue(t *testing.T) {
VirtuousDependsOnRogueTest(t, DirectedFactory{})
}
func TestDirectedString(t *testing.T) { StringTest(t, DirectedFactory{}, "DG") }
func TestDirectedConsensus(t *testing.T) { ConsensusTest(t, DirectedFactory{}, "DG") }

View File

@ -7,32 +7,4 @@ import (
"testing"
)
func TestInputParams(t *testing.T) { ParamsTest(t, InputFactory{}) }
func TestInputIssued(t *testing.T) { IssuedTest(t, InputFactory{}) }
func TestInputLeftoverInput(t *testing.T) { LeftoverInputTest(t, InputFactory{}) }
func TestInputLowerConfidence(t *testing.T) { LowerConfidenceTest(t, InputFactory{}) }
func TestInputMiddleConfidence(t *testing.T) { MiddleConfidenceTest(t, InputFactory{}) }
func TestInputIndependent(t *testing.T) { IndependentTest(t, InputFactory{}) }
func TestInputVirtuous(t *testing.T) { VirtuousTest(t, InputFactory{}) }
func TestInputIsVirtuous(t *testing.T) { IsVirtuousTest(t, InputFactory{}) }
func TestInputConflicts(t *testing.T) { ConflictsTest(t, InputFactory{}) }
func TestInputQuiesce(t *testing.T) { QuiesceTest(t, InputFactory{}) }
func TestInputAcceptingDependency(t *testing.T) { AcceptingDependencyTest(t, InputFactory{}) }
func TestInputRejectingDependency(t *testing.T) { RejectingDependencyTest(t, InputFactory{}) }
func TestInputVacuouslyAccepted(t *testing.T) { VacuouslyAcceptedTest(t, InputFactory{}) }
func TestInputVirtuousDependsOnRogue(t *testing.T) { VirtuousDependsOnRogueTest(t, InputFactory{}) }
func TestInputString(t *testing.T) { StringTest(t, InputFactory{}, "IG") }
func TestInputConsensus(t *testing.T) { ConsensusTest(t, InputFactory{}, "IG") }

View File

@ -31,10 +31,10 @@ func (tx *TestTx) InputIDs() ids.Set { return tx.Ins }
func (tx *TestTx) Status() choices.Status { return tx.Stat }
// Accept implements the Consumer interface
func (tx *TestTx) Accept() error { tx.Stat = choices.Accepted; return nil }
func (tx *TestTx) Accept() error { tx.Stat = choices.Accepted; return tx.Validity }
// Reject implements the Consumer interface
func (tx *TestTx) Reject() error { tx.Stat = choices.Rejected; return nil }
func (tx *TestTx) Reject() error { tx.Stat = choices.Rejected; return tx.Validity }
// Reset sets the status to pending
func (tx *TestTx) Reset() { tx.Stat = choices.Processing }