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) }