当前位置: 首页>>代码示例>>Golang>>正文


Golang Db.ExistsSha方法代码示例

本文整理汇总了Golang中github.com/conformal/btcdb.Db.ExistsSha方法的典型用法代码示例。如果您正苦于以下问题:Golang Db.ExistsSha方法的具体用法?Golang Db.ExistsSha怎么用?Golang Db.ExistsSha使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/conformal/btcdb.Db的用法示例。


在下文中一共展示了Db.ExistsSha方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: testFetch

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)
}
开发者ID:stevenroose,项目名称:btcdb,代码行数:94,代码来源:sqliteblock_test.go


注:本文中的github.com/conformal/btcdb.Db.ExistsSha方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。