Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More tests for its-happening #125

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion consensus/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ func TestApplyBlock(t *testing.T) {
if err = consensus.ValidateBlock(cs, b, bs); err != nil {
return
}
cs, au = consensus.ApplyBlock(cs, b, bs, ancestorTimestamp(dbStore, b.ParentID, cs.AncestorDepth()))
prev := cs
cs, au = consensus.ApplyBlock(prev, b, bs, ancestorTimestamp(dbStore, b.ParentID, cs.AncestorDepth()))
dbStore.ApplyBlock(prev, au, true)
dbStore.AddBlock(b, &bs)
dbStore.AddState(cs)
return
}
checkUpdateElements := func(au consensus.ApplyUpdate, addedSCEs, spentSCEs []types.SiacoinElement, addedSFEs, spentSFEs []types.SiafundElement) {
Expand Down Expand Up @@ -123,6 +127,43 @@ func TestApplyBlock(t *testing.T) {
t.Fatal("extraneous elements")
}
}
checkRevertElements := func(ru consensus.RevertUpdate, addedSCEs, spentSCEs []types.SiacoinElement, addedSFEs, spentSFEs []types.SiafundElement) {
ru.ForEachSiacoinElement(func(sce types.SiacoinElement, spent bool) {
sces := &addedSCEs
if spent {
sces = &spentSCEs
}
if len(*sces) == 0 {
t.Fatal("unexpected spent siacoin element")
}
sce.StateElement = types.StateElement{}
if !reflect.DeepEqual(sce, (*sces)[len(*sces)-1]) {
js1, _ := json.MarshalIndent(sce, "", " ")
js2, _ := json.MarshalIndent((*sces)[len(*sces)-1], "", " ")
t.Fatalf("siacoin element doesn't match:\n%s\nvs\n%s\n", js1, js2)
}
*sces = (*sces)[:len(*sces)-1]
})
ru.ForEachSiafundElement(func(sfe types.SiafundElement, spent bool) {
sfes := &addedSFEs
if spent {
sfes = &spentSFEs
}
if len(*sfes) == 0 {
t.Fatal("unexpected spent siafund element")
}
sfe.StateElement = types.StateElement{}
if !reflect.DeepEqual(sfe, (*sfes)[len(*sfes)-1]) {
js1, _ := json.MarshalIndent(sfe, "", " ")
js2, _ := json.MarshalIndent((*sfes)[len(*sfes)-1], "", " ")
t.Fatalf("siafund element doesn't match:\n%s\nvs\n%s\n", js1, js2)
}
*sfes = (*sfes)[:len(*sfes)-1]
})
if len(addedSCEs)+len(spentSCEs)+len(addedSFEs)+len(spentSFEs) > 0 {
t.Fatal("extraneous elements")
}
}

// block with nothing except block reward
b1 := types.Block{
Expand Down Expand Up @@ -185,9 +226,16 @@ func TestApplyBlock(t *testing.T) {
spentSFEs = []types.SiafundElement{
{SiafundOutput: giftTxn.SiafundOutputs[0]},
}

prev := cs
bs := dbStore.SupplementTipBlock(b2)
if au, err := addBlock(b2); err != nil {
t.Fatal(err)
} else {
checkUpdateElements(au, addedSCEs, spentSCEs, addedSFEs, spentSFEs)
}

ru := consensus.RevertBlock(prev, b2, bs)
dbStore.RevertBlock(cs, ru)
checkRevertElements(ru, addedSCEs, spentSCEs, addedSFEs, spentSFEs)
}
Loading