Skip to content

Commit

Permalink
add basic revert test to TestApplyBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
chris124567 committed Oct 3, 2023
1 parent 7f4c2e9 commit c0e249a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
53 changes: 52 additions & 1 deletion consensus/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,14 @@ 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.AddCheckpoint(chain.Checkpoint{
Block: b,
State: cs,
Supplement: &bs,
})
return
}
checkUpdateElements := func(au consensus.ApplyUpdate, addedSCEs, spentSCEs []types.SiacoinElement, addedSFEs, spentSFEs []types.SiafundElement) {
Expand Down Expand Up @@ -123,6 +130,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,11 +229,18 @@ 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)
}

/*
Expand Down
8 changes: 3 additions & 5 deletions consensus/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,6 @@ func TestValidateBlock(t *testing.T) {
}
}
}

}

func updateProofs(cau consensus.ApplyUpdate, sces []types.SiacoinElement, sfes []types.SiafundElement, fces []types.V2FileContractElement) {
Expand Down Expand Up @@ -1020,8 +1019,8 @@ func TestValidateV2Block(t *testing.T) {
}
}

cs, testCau := consensus.ApplyBlock(cs, validBlock, consensus.V1BlockSupplement{}, time.Now())
if dbStore.ApplyBlock(cs, cau, true) != true {
cs, testCau := consensus.ApplyBlock(cs, validBlock, dbStore.SupplementTipBlock(validBlock), time.Now())
if dbStore.ApplyBlock(cs, testCau, true) != true {
t.Fatal("didn't commit block")
}
updateProofs(testCau, sces, sfes, fces)
Expand Down Expand Up @@ -1060,7 +1059,7 @@ func TestValidateV2Block(t *testing.T) {
if err := consensus.ValidateBlock(cs, b, dbStore.SupplementTipBlock(b)); err != nil {
t.Fatal(err)
}
cs, cau = consensus.ApplyBlock(cs, b, consensus.V1BlockSupplement{}, time.Now())
cs, cau = consensus.ApplyBlock(cs, b, dbStore.SupplementTipBlock(validBlock), time.Now())
if dbStore.ApplyBlock(cs, cau, true) != true {
t.Fatal("didn't commit block")
}
Expand Down Expand Up @@ -1092,7 +1091,6 @@ func TestValidateV2Block(t *testing.T) {

// initial block should be valid
validBlock = deepCopyBlock(b)
// TODO: this fails, fix it
if err := consensus.ValidateBlock(cs, validBlock, dbStore.SupplementTipBlock(validBlock)); err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit c0e249a

Please sign in to comment.