diff --git a/consensus/update_test.go b/consensus/update_test.go index c94387f8..bc354b0a 100644 --- a/consensus/update_test.go +++ b/consensus/update_test.go @@ -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) { @@ -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{ @@ -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) } /* diff --git a/consensus/validation_test.go b/consensus/validation_test.go index ee400f73..e66108c5 100644 --- a/consensus/validation_test.go +++ b/consensus/validation_test.go @@ -523,7 +523,6 @@ func TestValidateBlock(t *testing.T) { } } } - } func updateProofs(cau consensus.ApplyUpdate, sces []types.SiacoinElement, sfes []types.SiafundElement, fces []types.V2FileContractElement) { @@ -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) @@ -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") } @@ -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) }