Skip to content

Commit

Permalink
small test cleanups, no functional changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Larry Ruane committed Dec 19, 2019
1 parent d0f78a1 commit a9a9064
Showing 1 changed file with 32 additions and 36 deletions.
68 changes: 32 additions & 36 deletions common/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ package common
import (
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"testing"

"github.com/pkg/errors"

"github.com/zcash-hackworks/lightwalletd/parser"
"github.com/zcash-hackworks/lightwalletd/walletrpc"
)
Expand Down Expand Up @@ -41,15 +38,14 @@ func TestCache(t *testing.T) {
block := parser.NewBlock()
blockData, err = block.ParseFromSlice(blockData)
if err != nil {
t.Error(errors.Wrap(err, fmt.Sprintf("parsing testnet block %d", test.BlockHeight)))
continue
t.Fatal(err)
}
compacts = append(compacts, block.ToCompact())
}

// initially empty cache
if cache.GetLatestBlock() != -1 {
t.Fatal("reorg, unexpected GetLatestBlock")
t.Fatal("unexpected GetLatestBlock")
}

// normal, sunny-day case, 6 blocks, add as blocks 10-15
Expand All @@ -70,18 +66,18 @@ func TestCache(t *testing.T) {
}
}
if len(cache.m) != 4 { // max entries is 4
t.Fatal("unexpected number of cache entries:", len(cache.m))
t.Fatal("unexpected number of cache entries")
}
if cache.firstBlock != 16-4 {
t.Fatal("unexpected firstBlock:", cache.firstBlock)
t.Fatal("unexpected firstBlock")
}
if cache.nextBlock != 16 {
t.Fatal("unexpected nextBlock:", cache.nextBlock)
t.Fatal("unexpected nextBlock")
}

// No entries just before and just after the cache range
if cache.Get(11) != nil || cache.Get(16) != nil {
t.Fatal("unexpected Get:", cache.nextBlock)
t.Fatal("unexpected Get")
}

// We can re-add the last block (with the same data) and
Expand All @@ -91,16 +87,16 @@ func TestCache(t *testing.T) {
t.Fatal(err)
}
if reorg {
t.Fatal("replace, unexpected reorg")
t.Fatal("unexpected reorg")
}
if len(cache.m) != 4 {
t.Fatal("replace, unexpected number of blocks:", len(cache.m))
t.Fatal("unexpected number of blocks")
}
if cache.firstBlock != 16-4 {
t.Fatal("replace, unexpected firstBlock:", cache.firstBlock)
t.Fatal("unexpected firstBlock")
}
if cache.nextBlock != 16 {
t.Fatal("replace, unexpected nextBlock:", cache.nextBlock)
t.Fatal("unexpected nextBlock")
}

// Simulate a reorg by resubmitting as the next block, 16, any block with
Expand All @@ -114,16 +110,16 @@ func TestCache(t *testing.T) {
}
// The cache shouldn't have changed in any way
if cache.Get(16) != nil {
t.Fatal("reorg, unexpected block 16 exists")
t.Fatal("unexpected block 16 exists")
}
if cache.GetLatestBlock() != 15 {
t.Fatal("reorg, unexpected GetLatestBlock")
t.Fatal("unexpected GetLatestBlock")
}
if int(cache.Get(15).Height) != 289460+5 {
t.Fatal("reorg, unexpected Get")
t.Fatal("unexpected Get")
}
if len(cache.m) != 4 {
t.Fatal("reorg, unexpected number of cache entries")
t.Fatal("unexpected number of cache entries")
}

// In response to the reorg being detected, we must back up until we
Expand All @@ -140,17 +136,17 @@ func TestCache(t *testing.T) {
}
// the cache deleted block 15 (it's definitely wrong)
if cache.Get(15) != nil {
t.Fatal("reorg, unexpected block 15 exists")
t.Fatal("unexpected block 15 exists")
}
if cache.GetLatestBlock() != 14 {
t.Fatal("reorg, unexpected GetLatestBlock")
t.Fatal("unexpected GetLatestBlock")
}
if int(cache.Get(14).Height) != 289460+4 {
t.Fatal("reorg, unexpected Get")
t.Fatal("unexpected Get")
}
// now only 3 entries (12-14)
if len(cache.m) != 3 {
t.Fatal("reorg, unexpected number of cache entries")
t.Fatal("unexpected number of cache entries")
}

// Back up a couple more, try to re-add height 13, and suppose
Expand All @@ -168,20 +164,20 @@ func TestCache(t *testing.T) {
// 13 was replaced (with the same block), but that means
// everything after 13 is deleted
if cache.Get(14) != nil {
t.Fatal("reorg, unexpected block 14 exists")
t.Fatal("unexpected block 14 exists")
}
if cache.GetLatestBlock() != 13 {
t.Fatal("reorg, unexpected GetLatestBlock")
t.Fatal("unexpected GetLatestBlock")
}
if int(cache.Get(13).Height) != 289460+3 {
t.Fatal("reorg, unexpected Get")
t.Fatal("unexpected Get")
}
if int(cache.Get(12).Height) != 289460+2 {
t.Fatal("reorg, unexpected Get")
t.Fatal("unexpected Get")
}
// down to 2 entries (12-13)
if len(cache.m) != 2 {
t.Fatal("reorg, unexpected number of cache entries")
t.Fatal("unexpected number of cache entries")
}

// Now we can continue forward from here
Expand All @@ -193,13 +189,13 @@ func TestCache(t *testing.T) {
t.Fatal("unexpected reorg")
}
if cache.GetLatestBlock() != 14 {
t.Fatal("reorg, unexpected GetLatestBlock")
t.Fatal("unexpected GetLatestBlock")
}
if int(cache.Get(14).Height) != 289460+4 {
t.Fatal("reorg, unexpected Get")
t.Fatal("unexpected Get")
}
if len(cache.m) != 3 {
t.Fatal("reorg, unexpected number of cache entries")
t.Fatal("unexpected number of cache entries")
}

// It's possible, although unlikely, that after a reorg is detected,
Expand All @@ -217,13 +213,13 @@ func TestCache(t *testing.T) {
t.Fatal("unexpected reorg")
}
if cache.GetLatestBlock() != 10 {
t.Fatal("reorg, unexpected GetLatestBlock")
t.Fatal("unexpected GetLatestBlock")
}
if int(cache.Get(10).Height) != 289460+0 {
t.Fatal("reorg, unexpected Get")
t.Fatal("unexpected Get")
}
if len(cache.m) != 1 {
t.Fatal("reorg, unexpected number of cache entries")
t.Fatal("unexpected number of cache entries")
}

// Another weird case (not currently possible) is adding a block at
Expand All @@ -237,12 +233,12 @@ func TestCache(t *testing.T) {
t.Fatal("unexpected reorg")
}
if cache.GetLatestBlock() != 20 {
t.Fatal("reorg, unexpected GetLatestBlock")
t.Fatal("unexpected GetLatestBlock")
}
if int(cache.Get(20).Height) != 289460 {
t.Fatal("reorg, unexpected Get")
t.Fatal("unexpected Get")
}
if len(cache.m) != 1 {
t.Fatal("reorg, unexpected number of cache entries")
t.Fatal("unexpected number of cache entries")
}
}

0 comments on commit a9a9064

Please sign in to comment.