本文整理汇总了Golang中github.com/decred/dcrd/blockchain/stake.TicketDB.Initialize方法的典型用法代码示例。如果您正苦于以下问题:Golang TicketDB.Initialize方法的具体用法?Golang TicketDB.Initialize怎么用?Golang TicketDB.Initialize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/decred/dcrd/blockchain/stake.TicketDB
的用法示例。
在下文中一共展示了TicketDB.Initialize方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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
//.........这里部分代码省略.........
示例2: chainSetup
// chainSetup is used to create a new db and chain instance with the genesis
// block already inserted. In addition to the new chain instnce, it returns
// a teardown function the caller should invoke when done testing to clean up.
func chainSetup(dbName string, params *chaincfg.Params) (*blockchain.BlockChain, func(), error) {
if !isSupportedDbType(testDbType) {
return nil, nil, fmt.Errorf("unsupported db type %v", testDbType)
}
// Handle memory database specially since it doesn't need the disk
// specific handling.
var db database.Db
tmdb := new(stake.TicketDB)
var teardown func()
if testDbType == "memdb" {
ndb, err := database.CreateDB(testDbType)
if err != nil {
return nil, nil, fmt.Errorf("error creating db: %v", err)
}
db = ndb
// Setup a teardown function for cleaning up. This function is
// returned to the caller to be invoked when it is done testing.
teardown = func() {
tmdb.Close()
db.Close()
}
} else {
// Create the root directory for test databases.
if !fileExists(testDbRoot) {
if err := os.MkdirAll(testDbRoot, 0700); err != nil {
err := fmt.Errorf("unable to create test db "+
"root: %v", err)
return nil, nil, err
}
}
// Create a new database to store the accepted blocks into.
dbPath := filepath.Join(testDbRoot, dbName)
_ = os.RemoveAll(dbPath)
ndb, err := database.CreateDB(testDbType, dbPath)
if err != nil {
return nil, nil, fmt.Errorf("error creating db: %v", err)
}
db = ndb
// Setup a teardown function for cleaning up. This function is
// returned to the caller to be invoked when it is done testing.
teardown = func() {
dbVersionPath := filepath.Join(testDbRoot, dbName+".ver")
tmdb.Close()
db.Sync()
db.Close()
os.RemoveAll(dbPath)
os.Remove(dbVersionPath)
os.RemoveAll(testDbRoot)
}
}
// Insert the main network genesis block. This is part of the initial
// database setup.
genesisBlock := dcrutil.NewBlock(params.GenesisBlock)
genesisBlock.SetHeight(int64(0))
_, err := db.InsertBlock(genesisBlock)
if err != nil {
teardown()
err := fmt.Errorf("failed to insert genesis block: %v", err)
return nil, nil, err
}
// Start the ticket database.
tmdb.Initialize(params, db)
tmdb.RescanTicketDB()
chain := blockchain.New(db, tmdb, params, nil)
return chain, teardown, nil
}