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


Golang TicketDB.InsertBlock方法代码示例

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


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

示例1: TestTicketDB

func TestTicketDB(t *testing.T) {
	// Declare some useful variables
	testBCHeight := int64(168)

	// Set up a DB
	database, err := database.CreateDB("leveldb", "ticketdb_test")
	if err != nil {
		t.Errorf("Db create error: %v", err.Error())
	}

	// Make a new tmdb to fill with dummy live and used tickets
	var tmdb stake.TicketDB
	tmdb.Initialize(simNetParams, database)

	filename := filepath.Join("..", "/../blockchain/testdata", "blocks0to168.bz2")
	fi, err := os.Open(filename)
	bcStream := bzip2.NewReader(fi)
	defer fi.Close()

	// Create a buffer of the read file
	bcBuf := new(bytes.Buffer)
	bcBuf.ReadFrom(bcStream)

	// Create decoder from the buffer and a map to store the data
	bcDecoder := gob.NewDecoder(bcBuf)
	blockchain := make(map[int64][]byte)

	// Decode the blockchain into the map
	if err := bcDecoder.Decode(&blockchain); err != nil {
		t.Errorf("error decoding test blockchain")
	}

	var CopyOfMapsAtBlock50, CopyOfMapsAtBlock168 stake.TicketMaps
	var ticketsToSpendIn167 []chainhash.Hash
	var sortedTickets167 []*stake.TicketData

	for i := int64(0); i <= testBCHeight; i++ {
		block, err := dcrutil.NewBlockFromBytes(blockchain[i])
		if err != nil {
			t.Errorf("block deserialization error on block %v", i)
		}
		block.SetHeight(i)
		database.InsertBlock(block)
		tmdb.InsertBlock(block)

		if i == 50 {
			// Create snapshot of tmdb at block 50
			CopyOfMapsAtBlock50, err = cloneTicketDB(&tmdb)
			if err != nil {
				t.Errorf("db cloning at block 50 failure! %v", err)
			}
		}

		// Test to make sure that ticket selection is working correctly.
		if i == 167 {
			// Sort the entire list of tickets lexicographically by sorting
			// each bucket and then appending it to the list. Then store it
			// to use in the next block.
			totalTickets := 0
			sortedSlice := make([]*stake.TicketData, 0)
			for i := 0; i < stake.BucketsSize; i++ {
				tix, err := tmdb.DumpLiveTickets(uint8(i))
				if err != nil {
					t.Errorf("error dumping live tickets")
				}
				mapLen := len(tix)
				totalTickets += mapLen
				tempTdSlice := stake.NewTicketDataSlice(mapLen)
				itr := 0 // Iterator
				for _, td := range tix {
					tempTdSlice[itr] = td
					itr++
				}
				sort.Sort(tempTdSlice)
				sortedSlice = append(sortedSlice, tempTdSlice...)
			}
			sortedTickets167 = sortedSlice
		}

		if i == 168 {
			parentBlock, err := dcrutil.NewBlockFromBytes(blockchain[i-1])
			if err != nil {
				t.Errorf("block deserialization error on block %v", i-1)
			}
			pbhB, err := parentBlock.MsgBlock().Header.Bytes()
			if err != nil {
				t.Errorf("block header serialization error")
			}
			prng := stake.NewHash256PRNG(pbhB)
			ts, err := stake.FindTicketIdxs(int64(len(sortedTickets167)),
				int(simNetParams.TicketsPerBlock), prng)
			if err != nil {
				t.Errorf("failure on FindTicketIdxs")
			}
			for _, idx := range ts {
				ticketsToSpendIn167 =
					append(ticketsToSpendIn167, sortedTickets167[idx].SStxHash)
			}

			// Make sure that the tickets that were supposed to be spent or
//.........这里部分代码省略.........
开发者ID:alexlyp,项目名称:dcrd,代码行数:101,代码来源:ticketdb_test.go


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