本文整理汇总了Golang中github.com/decred/dcrd/chaincfg/chainhash.NewHash函数的典型用法代码示例。如果您正苦于以下问题:Golang NewHash函数的具体用法?Golang NewHash怎么用?Golang NewHash使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewHash函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: GetTransactions
// BUGS:
// - MinimumRecentTransactions is ignored.
// - Wrong error codes when a block height or hash is not recognized
func (s *walletServer) GetTransactions(ctx context.Context, req *pb.GetTransactionsRequest) (
resp *pb.GetTransactionsResponse, err error) {
var startBlock, endBlock *wallet.BlockIdentifier
if req.StartingBlockHash != nil && req.StartingBlockHeight != 0 {
return nil, errors.New(
"starting block hash and height may not be specified simultaneously")
} else if req.StartingBlockHash != nil {
startBlockHash, err := chainhash.NewHash(req.StartingBlockHash)
if err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, "%s", err.Error())
}
startBlock = wallet.NewBlockIdentifierFromHash(startBlockHash)
} else if req.StartingBlockHeight != 0 {
startBlock = wallet.NewBlockIdentifierFromHeight(req.StartingBlockHeight)
}
if req.EndingBlockHash != nil && req.EndingBlockHeight != 0 {
return nil, grpc.Errorf(codes.InvalidArgument,
"ending block hash and height may not be specified simultaneously")
} else if req.EndingBlockHash != nil {
endBlockHash, err := chainhash.NewHash(req.EndingBlockHash)
if err != nil {
return nil, grpc.Errorf(codes.InvalidArgument, "%s", err.Error())
}
endBlock = wallet.NewBlockIdentifierFromHash(endBlockHash)
} else if req.EndingBlockHeight != 0 {
endBlock = wallet.NewBlockIdentifierFromHeight(req.EndingBlockHeight)
}
var minRecentTxs int
if req.MinimumRecentTransactions != 0 {
if endBlock != nil {
return nil, grpc.Errorf(codes.InvalidArgument,
"ending block and minimum number of recent transactions "+
"may not be specified simultaneously")
}
minRecentTxs = int(req.MinimumRecentTransactions)
if minRecentTxs < 0 {
return nil, grpc.Errorf(codes.InvalidArgument,
"minimum number of recent transactions may not be negative")
}
}
_ = minRecentTxs
gtr, err := s.wallet.GetTransactions(startBlock, endBlock, ctx.Done())
if err != nil {
return nil, translateError(err)
}
return marshalGetTransactionsResult(gtr)
}
示例2: deserializeTicketHashes
// deserializeTicketHashes deserializes a list of ticket hashes. Empty but
// non-nil slices are deserialized empty.
func deserializeTicketHashes(b []byte) (TicketHashes, error) {
if b != nil && len(b) == 0 {
return make(TicketHashes, 0), nil
}
if len(b) < chainhash.HashSize {
return nil, ticketDBError(ErrTicketHashesShortRead, "short read when "+
"deserializing ticket hashes")
}
if len(b)%chainhash.HashSize != 0 {
return nil, ticketDBError(ErrTicketHashesCorrupt, "corrupt data found "+
"when deserializing ticket hashes")
}
entries := len(b) / chainhash.HashSize
ths := make(TicketHashes, entries)
offset := 0
for i := 0; i < entries; i++ {
hash, err := chainhash.NewHash(
b[offset : offset+chainhash.HashSize])
if err != nil {
return nil, ticketDBError(ErrUndoDataCorrupt, "corrupt hash found "+
"when deserializing block undo data")
}
offset += chainhash.HashSize
ths[i] = *hash
}
return ths, nil
}
示例3: loadOwnedSStxs
// loadManager returns a new stake manager that results from loading it from
// the passed opened database. The public passphrase is required to decrypt the
// public keys.
func (s *StakeStore) loadOwnedSStxs(ns walletdb.ReadBucket) error {
// Regenerate the list of tickets.
// Perform all database lookups in a read-only view.
ticketList := make(map[chainhash.Hash]struct{})
// Open the sstx records database.
bucket := ns.NestedReadBucket(sstxRecordsBucketName)
// Store each key sequentially.
err := bucket.ForEach(func(k []byte, v []byte) error {
var errNewHash error
var hash *chainhash.Hash
hash, errNewHash = chainhash.NewHash(k)
if errNewHash != nil {
return errNewHash
}
ticketList[*hash] = struct{}{}
return nil
})
if err != nil {
return err
}
s.ownedSStxs = ticketList
return nil
}
示例4: DbLoadAllTickets
// DbLoadAllTickets loads all the live tickets from the database into a treap.
func DbLoadAllTickets(dbTx database.Tx, ticketBucket []byte) (*tickettreap.Immutable, error) {
meta := dbTx.Metadata()
bucket := meta.Bucket(ticketBucket)
treap := tickettreap.NewImmutable()
err := bucket.ForEach(func(k []byte, v []byte) error {
if len(v) < 5 {
return ticketDBError(ErrLoadAllTickets, fmt.Sprintf("short "+
"read for ticket key %x when loading tickets", k))
}
h, err := chainhash.NewHash(k)
if err != nil {
return err
}
treapKey := tickettreap.Key(*h)
missed, revoked, spent, expired := undoBitFlagsFromByte(v[4])
treapValue := &tickettreap.Value{
Height: dbnamespace.ByteOrder.Uint32(v[0:4]),
Missed: missed,
Revoked: revoked,
Spent: spent,
Expired: expired,
}
treap = treap.Put(treapKey, treapValue)
return nil
})
if err != nil {
return nil, ticketDBError(ErrLoadAllTickets, fmt.Sprintf("failed to "+
"load all tickets for the bucket %s", string(ticketBucket)))
}
return treap, nil
}
示例5: deserializeUserInvalTickets
// deserializeUserInvalTickets deserializes the passed serialized pool
// users invalid tickets information.
func deserializeUserInvalTickets(serializedTickets []byte) ([]*chainhash.Hash,
error) {
// Cursory check to make sure that the number of records
// makes sense.
if len(serializedTickets)%chainhash.HashSize != 0 {
err := io.ErrUnexpectedEOF
return nil, err
}
numRecords := len(serializedTickets) / chainhash.HashSize
records := make([]*chainhash.Hash, numRecords)
// Loop through all the ssgen records, deserialize them, and
// store them.
for i := 0; i < numRecords; i++ {
start := i * chainhash.HashSize
end := (i + 1) * chainhash.HashSize
h, err := chainhash.NewHash(serializedTickets[start:end])
if err != nil {
str := "problem deserializing stake pool invalid user tickets"
return nil, stakeStoreError(ErrDatabase, str, err)
}
records[i] = h
}
return records, nil
}
示例6: SSGenBlockVotedOn
// SSGenBlockVotedOn takes an SSGen tx and returns the block voted on in the
// first OP_RETURN by hash and height.
//
// This function is only safe to be called on a transaction that
// has passed IsSSGen.
func SSGenBlockVotedOn(tx *wire.MsgTx) (chainhash.Hash, uint32, error) {
// Get the block header hash.
blockSha, err := chainhash.NewHash(tx.TxOut[0].PkScript[2:34])
if err != nil {
return chainhash.Hash{}, 0, err
}
// Get the block height.
height := binary.LittleEndian.Uint32(tx.TxOut[0].PkScript[34:38])
return *blockSha, height, nil
}
示例7: NewCoin
func NewCoin(index int64, value dcrutil.Amount, numConfs int64) coinset.Coin {
h := fastsha256.New()
h.Write([]byte(fmt.Sprintf("%d", index)))
hash, _ := chainhash.NewHash(h.Sum(nil))
c := &TestCoin{
TxHash: hash,
TxIndex: 0,
TxValue: value,
TxNumConfs: numConfs,
}
return coinset.Coin(c)
}
示例8: TargetStrToDiff
/* convert target come from getwork to big integer
"ffffffffffffffffffffffffffffffffffffffffffffffffffffff3f00000000"
*/
func TargetStrToDiff(targetHex string) *big.Int {
hashbytes, err := hex.DecodeString(targetHex)
if err != nil {
return big.NewInt(0)
}
hash, err := chainhash.NewHash(hashbytes)
if err != nil {
return big.NewInt(0)
}
targetdiff := blockchain.ShaHashToBig(hash)
if targetdiff.Cmp(big.NewInt(0)) != 0 {
targetdiff.Div(PowLimit, targetdiff)
}
return targetdiff
}
示例9: deserializeBlockUndoData
// deserializeBlockUndoData deserializes a list of UndoTicketData for an entire
// block. Empty but non-nil slices are deserialized empty.
func deserializeBlockUndoData(b []byte) ([]UndoTicketData, error) {
if b != nil && len(b) == 0 {
return make([]UndoTicketData, 0), nil
}
if len(b) < undoTicketDataSize {
return nil, ticketDBError(ErrUndoDataShortRead, "short read when "+
"deserializing block undo data")
}
if len(b)%undoTicketDataSize != 0 {
return nil, ticketDBError(ErrUndoDataCorrupt, "corrupt data found "+
"when deserializing block undo data")
}
entries := len(b) / undoTicketDataSize
utds := make([]UndoTicketData, entries)
offset := 0
for i := 0; i < entries; i++ {
hash, err := chainhash.NewHash(
b[offset : offset+chainhash.HashSize])
if err != nil {
return nil, ticketDBError(ErrUndoDataCorrupt, "corrupt hash found "+
"when deserializing block undo data")
}
offset += chainhash.HashSize
height := dbnamespace.ByteOrder.Uint32(b[offset : offset+4])
offset += 4
missed, revoked, spent, expired := undoBitFlagsFromByte(b[offset])
offset++
utds[i] = UndoTicketData{
TicketHash: *hash,
TicketHeight: height,
Missed: missed,
Revoked: revoked,
Spent: spent,
Expired: expired,
}
}
return utds, nil
}
示例10: dumpSSRtxTickets
// dumpSSRtxTickets fetches the entire list of tickets spent as revocations
// byt this wallet.
func (s *StakeStore) dumpSSRtxTickets(ns walletdb.ReadBucket) ([]chainhash.Hash, error) {
var ticketList []chainhash.Hash
// Open the revocation records database.
bucket := ns.NestedReadBucket(ssrtxRecordsBucketName)
// Store each hash sequentially.
err := bucket.ForEach(func(k []byte, v []byte) error {
ticket, errDeser := chainhash.NewHash(k)
if errDeser != nil {
return errDeser
}
ticketList = append(ticketList, *ticket)
return nil
})
return ticketList, err
}
示例11: BenchmarkMruInventoryList
// BenchmarkMruInventoryList performs basic benchmarks on the most recently
// used inventory handling.
func BenchmarkMruInventoryList(b *testing.B) {
// Create a bunch of fake inventory vectors to use in benchmarking
// the mru inventory code.
b.StopTimer()
numInvVects := 100000
invVects := make([]*wire.InvVect, 0, numInvVects)
for i := 0; i < numInvVects; i++ {
hashBytes := make([]byte, chainhash.HashSize)
rand.Read(hashBytes)
hash, _ := chainhash.NewHash(hashBytes)
iv := wire.NewInvVect(wire.InvTypeBlock, hash)
invVects = append(invVects, iv)
}
b.StartTimer()
// Benchmark the add plus evicition code.
limit := 20000
mruInvMap := NewMruInventoryMap(uint(limit))
for i := 0; i < b.N; i++ {
mruInvMap.Add(invVects[i%numInvVects])
}
}
示例12: NewHash256PRNG
// NewHash256PRNG creates a pointer to a newly created hash256PRNG.
func NewHash256PRNG(seed []byte) *Hash256PRNG {
// idx and lastHash are automatically initialized
// as 0. We initialize the seed by appending a constant
// to it and hashing to give 32 bytes. This ensures
// that regardless of the input, the PRNG is always
// doing a short number of rounds because it only
// has to hash < 64 byte messages. The constant is
// derived from the hexadecimal representation of
// pi.
cst := []byte{0x24, 0x3F, 0x6A, 0x88,
0x85, 0xA3, 0x08, 0xD3}
hp := new(Hash256PRNG)
hp.seed = chainhash.HashFuncB(append(seed, cst...))
initLH, err := chainhash.NewHash(hp.seed)
if err != nil {
return nil
}
hp.seedState = *initLH
hp.lastHash = *initLH
hp.idx = 0
return hp
}
示例13: TestBlockHeaderHashing
func TestBlockHeaderHashing(t *testing.T) {
dummyHeader := "0000000049e0b48ade043f729d60095ed92642d96096fe6aba42f2eda" +
"632d461591a152267dc840ff27602ce1968a81eb30a43423517207617a0150b56c4f72" +
"b803e497f00000000000000000000000000000000000000000000000000000000000000" +
"00010000000000000000000000b7000000ffff7f20204e0000000000005800000060010" +
"0008b990956000000000000000000000000000000000000000000000000000000000000" +
"0000000000000000ABCD"
// This hash has reversed endianness compared to what chainhash spits out.
hashStr := "0d40d58703482d81d711be0ffc1b313788d3c3937e1617e4876661d33a8c4c41"
hashB, _ := hex.DecodeString(hashStr)
hash, _ := chainhash.NewHash(hashB)
vecH, _ := hex.DecodeString(dummyHeader)
r := bytes.NewReader(vecH)
var bh wire.BlockHeader
bh.Deserialize(r)
hash2 := bh.BlockSha()
if !hash2.IsEqual(hash) {
t.Errorf("wrong block sha returned (want %v, got %v)",
hash,
hash2)
}
}
示例14: TicketDbThumbprint
// TicketDbThumbprint takes all the tickets in the respective ticket db,
// sorts them, hashes their contents into a list, and then hashes that list.
// The resultant hash is the thumbprint of the ticket database, and should
// be the same across all clients that are synced to the same block. Returns
// an array of hashes len 3, containing (1) live tickets (2) spent tickets
// and (3) missed tickets.
// Do NOT use on mainnet or in production. For debug use only! Make sure
// the blockchain is frozen when you call this function.
func TicketDbThumbprint(tmdb *stake.TicketDB, chainParams *chaincfg.Params) ([]*chainhash.Hash, error) {
// Container for the three master hashes to go into.
dbThumbprints := make([]*chainhash.Hash, 3, 3)
// (1) Live tickets.
allLiveTickets := stake.NewTicketDataSliceEmpty()
for i := 0; i < stake.BucketsSize; i++ {
bucketTickets, err := tmdb.DumpLiveTickets(uint8(i))
if err != nil {
return nil, err
}
for _, td := range bucketTickets {
allLiveTickets = append(allLiveTickets, td)
}
}
// Sort by the number data hash, since we already have this implemented
// and it's also unique.
sort.Sort(allLiveTickets)
// Create a buffer, dump all the data into it, and hash.
var buf bytes.Buffer
for _, td := range allLiveTickets {
writeTicketDataToBuf(&buf, td)
}
liveHash := chainhash.HashFunc(buf.Bytes())
liveThumbprint, err := chainhash.NewHash(liveHash[:])
if err != nil {
return nil, err
}
dbThumbprints[0] = liveThumbprint
// (2) Spent tickets.
height := tmdb.GetTopBlock()
allSpentTickets := stake.NewTicketDataSliceEmpty()
for i := int64(chainParams.StakeEnabledHeight); i <= height; i++ {
bucketTickets, err := tmdb.DumpSpentTickets(i)
if err != nil {
return nil, err
}
for _, td := range bucketTickets {
allSpentTickets = append(allSpentTickets, td)
}
}
sort.Sort(allSpentTickets)
buf.Reset() // Flush buffer
for _, td := range allSpentTickets {
writeTicketDataToBuf(&buf, td)
}
spentHash := chainhash.HashFunc(buf.Bytes())
spentThumbprint, err := chainhash.NewHash(spentHash[:])
if err != nil {
return nil, err
}
dbThumbprints[1] = spentThumbprint
// (3) Missed tickets.
allMissedTickets := stake.NewTicketDataSliceEmpty()
missedTickets, err := tmdb.DumpMissedTickets()
if err != nil {
return nil, err
}
for _, td := range missedTickets {
allMissedTickets = append(allMissedTickets, td)
}
sort.Sort(allMissedTickets)
buf.Reset() // Flush buffer
missedHash := chainhash.HashFunc(buf.Bytes())
missedThumbprint, err := chainhash.NewHash(missedHash[:])
if err != nil {
return nil, err
}
dbThumbprints[2] = missedThumbprint
return dbThumbprints, nil
}
示例15: GetTransactions
// BUGS:
// - MinimumRecentTransactions is ignored.
// - Wrong error codes when a block height or hash is not recognized
func (s *walletServer) GetTransactions(req *pb.GetTransactionsRequest,
server pb.WalletService_GetTransactionsServer) error {
var startBlock, endBlock *wallet.BlockIdentifier
if req.StartingBlockHash != nil && req.StartingBlockHeight != 0 {
return grpc.Errorf(codes.InvalidArgument,
"starting block hash and height may not be specified simultaneously")
} else if req.StartingBlockHash != nil {
startBlockHash, err := chainhash.NewHash(req.StartingBlockHash)
if err != nil {
return grpc.Errorf(codes.InvalidArgument, "%s", err.Error())
}
startBlock = wallet.NewBlockIdentifierFromHash(startBlockHash)
} else if req.StartingBlockHeight != 0 {
startBlock = wallet.NewBlockIdentifierFromHeight(req.StartingBlockHeight)
}
if req.EndingBlockHash != nil && req.EndingBlockHeight != 0 {
return grpc.Errorf(codes.InvalidArgument,
"ending block hash and height may not be specified simultaneously")
} else if req.EndingBlockHash != nil {
endBlockHash, err := chainhash.NewHash(req.EndingBlockHash)
if err != nil {
return grpc.Errorf(codes.InvalidArgument, "%s", err.Error())
}
endBlock = wallet.NewBlockIdentifierFromHash(endBlockHash)
} else if req.EndingBlockHeight != 0 {
endBlock = wallet.NewBlockIdentifierFromHeight(req.EndingBlockHeight)
}
var minRecentTxs int
if req.MinimumRecentTransactions != 0 {
if endBlock != nil {
return grpc.Errorf(codes.InvalidArgument,
"ending block and minimum number of recent transactions "+
"may not be specified simultaneously")
}
minRecentTxs = int(req.MinimumRecentTransactions)
if minRecentTxs < 0 {
return grpc.Errorf(codes.InvalidArgument,
"minimum number of recent transactions may not be negative")
}
}
_ = minRecentTxs
gtr, err := s.wallet.GetTransactions(startBlock, endBlock, server.Context().Done())
if err != nil {
return translateError(err)
}
for i := range gtr.MinedTransactions {
resp := &pb.GetTransactionsResponse{
MinedTransactions: marshalBlock(>r.MinedTransactions[i]),
}
err = server.Send(resp)
if err != nil {
return err
}
}
if len(gtr.UnminedTransactions) > 0 {
resp := &pb.GetTransactionsResponse{
UnminedTransactions: marshalTransactionDetails(gtr.UnminedTransactions),
}
err = server.Send(resp)
if err != nil {
return err
}
}
return nil
}