本文整理匯總了Golang中github.com/decred/dcrd/wire.BlockHeader.Bytes方法的典型用法代碼示例。如果您正苦於以下問題:Golang BlockHeader.Bytes方法的具體用法?Golang BlockHeader.Bytes怎麽用?Golang BlockHeader.Bytes使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/decred/dcrd/wire.BlockHeader
的用法示例。
在下文中一共展示了BlockHeader.Bytes方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: disconnectNode
//.........這裏部分代碼省略.........
var err error
k := tickettreap.Key(undo.TicketHash)
v := &tickettreap.Value{
Height: undo.TicketHeight,
Missed: undo.Missed,
Revoked: undo.Revoked,
Spent: undo.Spent,
Expired: undo.Expired,
}
switch {
// All flags are unset; this is a newly added ticket.
// Remove it from the list of live tickets.
case !undo.Missed && !undo.Revoked && !undo.Spent:
restoredNode.liveTickets, err =
safeDelete(restoredNode.liveTickets, k)
if err != nil {
return nil, err
}
// The ticket was missed and revoked. It needs to
// be moved from the revoked ticket treap to the
// missed ticket treap.
case undo.Missed && undo.Revoked:
v.Revoked = false
restoredNode.revokedTickets, err =
safeDelete(restoredNode.revokedTickets, k)
if err != nil {
return nil, err
}
restoredNode.missedTickets, err =
safePut(restoredNode.missedTickets, k, v)
if err != nil {
return nil, err
}
// The ticket was missed and was previously live.
// Remove it from the missed tickets bucket and
// move it to the live tickets bucket.
case undo.Missed && !undo.Revoked:
// Expired tickets could never have been
// winners.
if !undo.Expired {
restoredNode.nextWinners = append(restoredNode.nextWinners,
undo.TicketHash)
stateBuffer = append(stateBuffer,
undo.TicketHash[:]...)
} else {
v.Expired = false
}
v.Missed = false
restoredNode.missedTickets, err =
safeDelete(restoredNode.missedTickets, k)
if err != nil {
return nil, err
}
restoredNode.liveTickets, err =
safePut(restoredNode.liveTickets, k, v)
if err != nil {
return nil, err
}
// The ticket was spent. Reinsert it into the live
// tickets treap and add it to the list of next
// winners.
case undo.Spent:
v.Spent = false
restoredNode.nextWinners = append(restoredNode.nextWinners,
undo.TicketHash)
stateBuffer = append(stateBuffer, undo.TicketHash[:]...)
restoredNode.liveTickets, err =
safePut(restoredNode.liveTickets, k, v)
if err != nil {
return nil, err
}
default:
return nil, stakeRuleError(ErrMemoryCorruption,
"unknown ticket state in undo data")
}
}
if node.height >= uint32(node.params.StakeValidationHeight) {
phB, err := parentHeader.Bytes()
if err != nil {
return nil, err
}
prng := NewHash256PRNG(phB)
_, err = findTicketIdxs(int64(restoredNode.liveTickets.Len()),
int(node.params.TicketsPerBlock), prng)
if err != nil {
return nil, err
}
lastHash := prng.StateHash()
stateBuffer = append(stateBuffer, lastHash[:]...)
copy(restoredNode.finalState[:], chainhash.HashFuncB(stateBuffer)[0:6])
}
return restoredNode, nil
}
示例2: LoadBestNode
// LoadBestNode is used when the blockchain is initialized, to get the initial
// stake node from the database bucket. The blockchain must pass the height
// and the blockHash to confirm that the ticket database is on the same
// location in the blockchain as the blockchain itself. This function also
// checks to ensure that the database has not failed the upgrade process and
// reports the current version. Upgrades are also handled by this function,
// when they become applicable.
func LoadBestNode(dbTx database.Tx, height uint32, blockHash chainhash.Hash, header wire.BlockHeader, params *chaincfg.Params) (*Node, error) {
info, err := ticketdb.DbFetchDatabaseInfo(dbTx)
if err != nil {
return nil, err
}
// Compare the tip and make sure it matches.
state, err := ticketdb.DbFetchBestState(dbTx)
if err != nil {
return nil, err
}
if state.Hash != blockHash || state.Height != height {
return nil, stakeRuleError(ErrDatabaseCorrupt, "best state corruption")
}
// Restore the best node treaps form the database.
node := new(Node)
node.height = height
node.params = params
node.liveTickets, err = ticketdb.DbLoadAllTickets(dbTx,
dbnamespace.LiveTicketsBucketName)
if err != nil {
return nil, err
}
if node.liveTickets.Len() != int(state.Live) {
return nil, stakeRuleError(ErrDatabaseCorrupt,
fmt.Sprintf("live tickets corruption (got "+
"%v in state but loaded %v)", int(state.Live),
node.liveTickets.Len()))
}
node.missedTickets, err = ticketdb.DbLoadAllTickets(dbTx,
dbnamespace.MissedTicketsBucketName)
if err != nil {
return nil, err
}
if node.missedTickets.Len() != int(state.Missed) {
return nil, stakeRuleError(ErrDatabaseCorrupt,
fmt.Sprintf("missed tickets corruption (got "+
"%v in state but loaded %v)", int(state.Missed),
node.missedTickets.Len()))
}
node.revokedTickets, err = ticketdb.DbLoadAllTickets(dbTx,
dbnamespace.RevokedTicketsBucketName)
if err != nil {
return nil, err
}
if node.revokedTickets.Len() != int(state.Revoked) {
return nil, stakeRuleError(ErrDatabaseCorrupt,
fmt.Sprintf("revoked tickets corruption (got "+
"%v in state but loaded %v)", int(state.Revoked),
node.revokedTickets.Len()))
}
// Restore the node undo and new tickets data.
node.databaseUndoUpdate, err = ticketdb.DbFetchBlockUndoData(dbTx, height)
if err != nil {
return nil, err
}
node.databaseBlockTickets, err = ticketdb.DbFetchNewTickets(dbTx, height)
if err != nil {
return nil, err
}
// Restore the next winners for the node.
node.nextWinners = make([]chainhash.Hash, 0)
if node.height >= uint32(node.params.StakeValidationHeight-1) {
node.nextWinners = make([]chainhash.Hash, len(state.NextWinners))
for i := range state.NextWinners {
node.nextWinners[i] = state.NextWinners[i]
}
// Calculate the final state from the block header.
stateBuffer := make([]byte, 0,
(node.params.TicketsPerBlock+1)*chainhash.HashSize)
for _, ticketHash := range node.nextWinners {
stateBuffer = append(stateBuffer, ticketHash[:]...)
}
hB, err := header.Bytes()
if err != nil {
return nil, err
}
prng := NewHash256PRNG(hB)
_, err = findTicketIdxs(int64(node.liveTickets.Len()),
int(node.params.TicketsPerBlock), prng)
if err != nil {
return nil, err
}
lastHash := prng.StateHash()
stateBuffer = append(stateBuffer, lastHash[:]...)
copy(node.finalState[:], chainhash.HashFuncB(stateBuffer)[0:6])
}
log.Infof("Stake database version %v loaded", info.Version)
//.........這裏部分代碼省略.........
示例3: connectNode
//.........這裏部分代碼省略.........
})
}
// Process all the revocations, moving them from the missed to the
// revoked treap and recording them in the undo data.
for _, revokedTicket := range revokedTickets {
v, err := safeGet(connectedNode.missedTickets,
tickettreap.Key(revokedTicket))
if err != nil {
return nil, err
}
v.Revoked = true
connectedNode.missedTickets, err =
safeDelete(connectedNode.missedTickets,
tickettreap.Key(revokedTicket))
if err != nil {
return nil, err
}
connectedNode.revokedTickets, err =
safePut(connectedNode.revokedTickets,
tickettreap.Key(revokedTicket), v)
if err != nil {
return nil, err
}
connectedNode.databaseUndoUpdate =
append(connectedNode.databaseUndoUpdate, ticketdb.UndoTicketData{
TicketHash: revokedTicket,
TicketHeight: v.Height,
Missed: v.Missed,
Revoked: v.Revoked,
Spent: v.Spent,
Expired: v.Expired,
})
}
}
// Add all the new tickets.
for _, newTicket := range newTickets {
k := tickettreap.Key(newTicket)
v := &tickettreap.Value{
Height: connectedNode.height,
Missed: false,
Revoked: false,
Spent: false,
Expired: false,
}
connectedNode.liveTickets, err =
safePut(connectedNode.liveTickets, k, v)
if err != nil {
return nil, err
}
connectedNode.databaseUndoUpdate =
append(connectedNode.databaseUndoUpdate, ticketdb.UndoTicketData{
TicketHash: newTicket,
TicketHeight: v.Height,
Missed: v.Missed,
Revoked: v.Revoked,
Spent: v.Spent,
Expired: v.Expired,
})
}
// The first block voted on is at StakeEnabledHeight, so begin calculating
// winners at the block before StakeEnabledHeight.
if connectedNode.height >=
uint32(connectedNode.params.StakeValidationHeight-1) {
// Find the next set of winners.
hB, err := header.Bytes()
if err != nil {
return nil, err
}
prng := NewHash256PRNG(hB)
idxs, err := findTicketIdxs(int64(connectedNode.liveTickets.Len()),
int(connectedNode.params.TicketsPerBlock), prng)
if err != nil {
return nil, err
}
stateBuffer := make([]byte, 0,
(connectedNode.params.TicketsPerBlock+1)*chainhash.HashSize)
nextWinnersKeys, err := fetchWinners(idxs, connectedNode.liveTickets)
if err != nil {
return nil, err
}
for _, treapKey := range nextWinnersKeys {
ticketHash := chainhash.Hash(*treapKey)
connectedNode.nextWinners = append(connectedNode.nextWinners,
ticketHash)
stateBuffer = append(stateBuffer, ticketHash[:]...)
}
lastHash := prng.StateHash()
stateBuffer = append(stateBuffer, lastHash[:]...)
copy(connectedNode.finalState[:], chainhash.HashFuncB(stateBuffer)[0:6])
}
return connectedNode, nil
}
示例4: estimateNextStakeDifficulty
// estimateNextStakeDifficulty returns a user-specified estimate for the next
// stake difficulty, with the passed ticketsInWindow indicating the number of
// fresh stake to pretend exists within this window. Optionally the user can
// also override this variable with useMaxTickets, which simply plugs in the
// maximum number of tickets the user can try.
func (b *BlockChain) estimateNextStakeDifficulty(curNode *blockNode,
ticketsInWindow int64, useMaxTickets bool) (int64, error) {
alpha := b.chainParams.StakeDiffAlpha
stakeDiffStartHeight := int64(b.chainParams.CoinbaseMaturity) +
1
maxRetarget := int64(b.chainParams.RetargetAdjustmentFactor)
TicketPoolWeight := int64(b.chainParams.TicketPoolSizeWeight)
// Number of nodes to traverse while calculating difficulty.
nodesToTraverse := (b.chainParams.StakeDiffWindowSize *
b.chainParams.StakeDiffWindows)
// Genesis block. Block at height 1 has these parameters.
if curNode == nil ||
curNode.height < stakeDiffStartHeight {
return b.chainParams.MinimumStakeDiff, nil
}
// Create a fake blockchain on top of the current best node with
// the number of freshly purchased tickets as indicated by the
// user.
oldDiff := curNode.header.SBits
topNode := curNode
if (curNode.height+1)%b.chainParams.StakeDiffWindowSize != 0 {
nextAdjHeight := ((curNode.height /
b.chainParams.StakeDiffWindowSize) + 1) *
b.chainParams.StakeDiffWindowSize
maxTickets := (nextAdjHeight - curNode.height) *
int64(b.chainParams.MaxFreshStakePerBlock)
// If the user has indicated that the automatically
// calculated maximum amount of tickets should be
// used, plug that in here.
if useMaxTickets {
ticketsInWindow = maxTickets
}
// Double check to make sure there isn't too much.
if ticketsInWindow > maxTickets {
return 0, fmt.Errorf("too much fresh stake to be used "+
"in evaluation requested; max %v, got %v", maxTickets,
ticketsInWindow)
}
// Insert all the tickets into bogus nodes that will be
// used to calculate the next difficulty below.
ticketsToInsert := ticketsInWindow
for i := curNode.height + 1; i < nextAdjHeight; i++ {
emptyHeader := new(wire.BlockHeader)
emptyHeader.Height = uint32(i)
// User a constant pool size for estimate, since
// this has much less fluctuation than freshStake.
// TODO Use a better pool size estimate?
emptyHeader.PoolSize = curNode.header.PoolSize
// Insert the fake fresh stake into each block,
// decrementing the amount we need to use each
// time until we hit 0.
freshStake := b.chainParams.MaxFreshStakePerBlock
if int64(freshStake) > ticketsToInsert {
freshStake = uint8(ticketsToInsert)
ticketsToInsert -= ticketsToInsert
} else {
ticketsToInsert -= int64(b.chainParams.MaxFreshStakePerBlock)
}
emptyHeader.FreshStake = freshStake
// Connect the header.
emptyHeader.PrevBlock = topNode.hash
// Make up a node hash.
hB, err := emptyHeader.Bytes()
if err != nil {
return 0, err
}
emptyHeaderHash := chainhash.HashFuncH(hB)
thisNode := new(blockNode)
thisNode.header = *emptyHeader
thisNode.hash = emptyHeaderHash
thisNode.height = i
thisNode.parent = topNode
topNode = thisNode
}
}
// The target size of the ticketPool in live tickets. Recast these as int64
// to avoid possible overflows for large sizes of either variable in
// params.
targetForTicketPool := int64(b.chainParams.TicketsPerBlock) *
int64(b.chainParams.TicketPoolSize)
// Initialize bigInt slice for the percentage changes for each window period
// above or below the target.
//.........這裏部分代碼省略.........