func testFetchRangeHeight(t *testing.T, db btcdb.Db, blocks []*btcutil.Block) {

	var testincrement int64 = 50
	var testcnt int64 = 100

	shanames := make([]*btcwire.ShaHash, len(blocks))

	nBlocks := int64(len(blocks))

	for i := range blocks {
		blockSha, err := blocks[i].Sha()
		if err != nil {
			t.Errorf("FetchRangeHeight: unexpected failure computing block sah %v", err)
		}
		shanames[i] = blockSha
	}

	for startheight := int64(0); startheight < nBlocks; startheight += testincrement {
		endheight := startheight + testcnt

		if endheight > nBlocks {
			endheight = btcdb.AllShas
		}

		shalist, err := db.FetchHeightRange(startheight, endheight)
		if err != nil {
			t.Errorf("FetchRangeHeight: unexpected failure looking up shas %v", err)
		}

		if endheight == btcdb.AllShas {
			if int64(len(shalist)) != nBlocks-startheight {
				t.Errorf("FetchRangeHeight: expected A %v shas, got %v", nBlocks-startheight, len(shalist))
			}
		} else {
			if int64(len(shalist)) != testcnt {
				t.Errorf("FetchRangeHeight: expected %v shas, got %v", testcnt, len(shalist))
			}
		}

		for i := range shalist {
			sha0 := *shanames[int64(i)+startheight]
			sha1 := shalist[i]
			if sha0 != sha1 {
				t.Errorf("FetchRangeHeight: mismatch sha at %v requested range %v %v: %v %v ", int64(i)+startheight, startheight, endheight, sha0, sha1)
			}
		}
	}

}
Example #2
0
func testFetch(t *testing.T, db btcdb.Db, shas []btcwire.ShaHash,
	sync string) {

	// Test the newest sha is what we expect and call it twice to ensure
	// caching is working working properly.
	numShas := int64(len(shas))
	newestSha := shas[numShas-1]
	newestBlockID := int64(numShas)
	testNewestSha(t, db, newestSha, newestBlockID, sync)
	testNewestSha(t, db, newestSha, newestBlockID, sync+" cached")

	for i, sha := range shas {
		// Add one for genesis block skew.
		i = i + 1

		// Ensure the sha exists in the db as expected.
		if !db.ExistsSha(&sha) {
			t.Errorf("testSha %d doesn't exists (%s)", i, sync)
			break
		}

		// Fetch the sha from the db and ensure all fields are expected
		// values.
		buf, pver, idx, err := sqlite3.FetchSha(db, &sha)
		if err != nil {
			t.Errorf("Failed to fetch testSha %d (%s)", i, sync)
		}
		if !bytes.Equal(zeroBlock, buf) {
			t.Errorf("testSha %d incorrect block return (%s)", i,
				sync)
		}
		if pver != 1 {
			t.Errorf("pver is %d and not 1 for testSha %d (%s)",
				pver, i, sync)
		}
		if idx != int64(i) {
			t.Errorf("index isn't as expected %d vs %d (%s)",
				idx, i, sync)
		}

		// Fetch the sha by index and ensure it matches.
		tsha, err := db.FetchBlockShaByHeight(int64(i))
		if err != nil {
			t.Errorf("can't fetch sha at index %d: %v", i, err)
			continue
		}
		if !tsha.IsEqual(&sha) {
			t.Errorf("sha for index %d isn't shas[%d]", i, i)
		}
	}

	endBlockID := numShas + 1
	midBlockID := endBlockID / 2
	fetchIdxTests := []fetchIdxTest{
		// All shas.
		{1, btcdb.AllShas, shas, "fetch all shas"},

		//// All shas using known bounds.
		{1, endBlockID, shas, "fetch all shas2"},

		// Partial list starting at beginning.
		{1, midBlockID, shas[:midBlockID-1], "fetch first half"},

		// Partial list ending at end.
		{midBlockID, endBlockID, shas[midBlockID-1 : endBlockID-1],
			"fetch second half"},

		// Nonexistent off the end.
		{endBlockID, endBlockID * 2, []btcwire.ShaHash{},
			"fetch nonexistent"},
	}

	for _, test := range fetchIdxTests {
		t.Logf("numSha: %d - Fetch from %d to %d\n", numShas, test.start, test.end)
		if shalist, err := db.FetchHeightRange(test.start, test.end); err == nil {
			compareArray(t, shalist, test.exp, test.test, sync)
		} else {
			t.Errorf("failed to fetch index range for %s (%s)",
				test.test, sync)
		}
	}

	// Try and fetch nonexistent sha.
	if db.ExistsSha(&badSha) {
		t.Errorf("nonexistent sha exists (%s)!", sync)
	}
	_, _, _, err := sqlite3.FetchSha(db, &badSha)
	if err == nil {
		t.Errorf("Success when fetching a bad sha! (%s)", sync)
	}
	// XXX if not check to see it is the right value?

	testIterator(t, db, shas, sync)
}