本文整理汇总了Golang中github.com/btcsuite/btcd/chaincfg/chainhash.NewHashFromStr函数的典型用法代码示例。如果您正苦于以下问题:Golang NewHashFromStr函数的具体用法?Golang NewHashFromStr怎么用?Golang NewHashFromStr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewHashFromStr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: BenchmarkDecodeMerkleBlock
// BenchmarkDecodeMerkleBlock performs a benchmark on how long it takes to
// decode a reasonably sized merkleblock message.
func BenchmarkDecodeMerkleBlock(b *testing.B) {
// Create a message with random data.
pver := ProtocolVersion
var m MsgMerkleBlock
hash, err := chainhash.NewHashFromStr(fmt.Sprintf("%x", 10000))
if err != nil {
b.Fatalf("NewHashFromStr: unexpected error: %v", err)
}
m.Header = *NewBlockHeader(hash, hash, 0, uint32(10000))
for i := 0; i < 105; i++ {
hash, err := chainhash.NewHashFromStr(fmt.Sprintf("%x", i))
if err != nil {
b.Fatalf("NewHashFromStr: unexpected error: %v", err)
}
m.AddTxHash(hash)
if i%8 == 0 {
m.Flags = append(m.Flags, uint8(i))
}
}
// Serialize it so the bytes are available to test the decode below.
var bb bytes.Buffer
if err := m.BtcEncode(&bb, pver); err != nil {
b.Fatalf("MsgMerkleBlock.BtcEncode: unexpected error: %v", err)
}
buf := bb.Bytes()
r := bytes.NewReader(buf)
var msg MsgMerkleBlock
b.ResetTimer()
for i := 0; i < b.N; i++ {
r.Seek(0, 0)
msg.BtcDecode(r, pver)
}
}
示例2: TestFilterInsertUpdateNone
func TestFilterInsertUpdateNone(t *testing.T) {
f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateNone)
// Add the generation pubkey
inputStr := "04eaafc2314def4ca98ac970241bcab022b9c1e1f4ea423a20f134c" +
"876f2c01ec0f0dd5b2e86e7168cefe0d81113c3807420ce13ad1357231a" +
"2252247d97a46a91"
inputBytes, err := hex.DecodeString(inputStr)
if err != nil {
t.Errorf("TestFilterInsertUpdateNone DecodeString failed: %v", err)
return
}
f.Add(inputBytes)
// Add the output address for the 4th transaction
inputStr = "b6efd80d99179f4f4ff6f4dd0a007d018c385d21"
inputBytes, err = hex.DecodeString(inputStr)
if err != nil {
t.Errorf("TestFilterInsertUpdateNone DecodeString failed: %v", err)
return
}
f.Add(inputBytes)
inputStr = "147caa76786596590baa4e98f5d9f48b86c7765e489f7a6ff3360fe5c674360b"
hash, err := chainhash.NewHashFromStr(inputStr)
if err != nil {
t.Errorf("TestFilterInsertUpdateNone NewHashFromStr failed: %v", err)
return
}
outpoint := wire.NewOutPoint(hash, 0)
if f.MatchesOutPoint(outpoint) {
t.Errorf("TestFilterInsertUpdateNone matched outpoint %s", inputStr)
return
}
inputStr = "02981fa052f0481dbc5868f4fc2166035a10f27a03cfd2de67326471df5bc041"
hash, err = chainhash.NewHashFromStr(inputStr)
if err != nil {
t.Errorf("TestFilterInsertUpdateNone NewHashFromStr failed: %v", err)
return
}
outpoint = wire.NewOutPoint(hash, 0)
if f.MatchesOutPoint(outpoint) {
t.Errorf("TestFilterInsertUpdateNone matched outpoint %s", inputStr)
return
}
}
示例3: parseRescanProgressParams
// parseRescanProgressParams parses out the height of the last rescanned block
// from the parameters of rescanfinished and rescanprogress notifications.
func parseRescanProgressParams(params []json.RawMessage) (*chainhash.Hash, int32, time.Time, error) {
if len(params) != 3 {
return nil, 0, time.Time{}, wrongNumParams(len(params))
}
// Unmarshal first parameter as an string.
var hashStr string
err := json.Unmarshal(params[0], &hashStr)
if err != nil {
return nil, 0, time.Time{}, err
}
// Unmarshal second parameter as an integer.
var height int32
err = json.Unmarshal(params[1], &height)
if err != nil {
return nil, 0, time.Time{}, err
}
// Unmarshal third parameter as an integer.
var blkTime int64
err = json.Unmarshal(params[2], &blkTime)
if err != nil {
return nil, 0, time.Time{}, err
}
// Decode string encoding of block hash.
hash, err := chainhash.NewHashFromStr(hashStr)
if err != nil {
return nil, 0, time.Time{}, err
}
return hash, height, time.Unix(blkTime, 0), nil
}
示例4: parseTxAcceptedNtfnParams
// parseTxAcceptedNtfnParams parses out the transaction hash and total amount
// from the parameters of a txaccepted notification.
func parseTxAcceptedNtfnParams(params []json.RawMessage) (*chainhash.Hash,
btcutil.Amount, error) {
if len(params) != 2 {
return nil, 0, wrongNumParams(len(params))
}
// Unmarshal first parameter as a string.
var txHashStr string
err := json.Unmarshal(params[0], &txHashStr)
if err != nil {
return nil, 0, err
}
// Unmarshal second parameter as a floating point number.
var famt float64
err = json.Unmarshal(params[1], &famt)
if err != nil {
return nil, 0, err
}
// Bounds check amount.
amt, err := btcutil.NewAmount(famt)
if err != nil {
return nil, 0, err
}
// Decode string encoding of transaction sha.
txHash, err := chainhash.NewHashFromStr(txHashStr)
if err != nil {
return nil, 0, err
}
return txHash, amt, nil
}
示例5: parseOutPoint
func parseOutPoint(input *btcjson.ListUnspentResult) (wire.OutPoint, error) {
txHash, err := chainhash.NewHashFromStr(input.TxID)
if err != nil {
return wire.OutPoint{}, err
}
return wire.OutPoint{Hash: *txHash, Index: input.Vout}, nil
}
示例6: newHashFromStr
// newHashFromStr converts the passed big-endian hex string into a
// wire.Hash. It only differs from the one available in chainhash in that
// it panics on an error since it will only (and must only) be called with
// hard-coded, and therefore known good, hashes.
func newHashFromStr(hexStr string) *chainhash.Hash {
hash, err := chainhash.NewHashFromStr(hexStr)
if err != nil {
panic(err)
}
return hash
}
示例7: newHashFromStr
// newHashFromStr converts the passed big-endian hex string into a
// chainhash.Hash. It only differs from the one available in chainhash in that
// it panics on an error since it will only (and must only) be called with
// hard-coded, and therefore known good, hashes.
func newHashFromStr(hexStr string) *chainhash.Hash {
hash, err := chainhash.NewHashFromStr(hexStr)
if err != nil {
panic("invalid hash in source file: " + hexStr)
}
return hash
}
示例8: Execute
// Execute is the main entry point for the command. It's invoked by the parser.
func (cmd *fetchBlockCmd) Execute(args []string) error {
// Setup the global config options and ensure they are valid.
if err := setupGlobalConfig(); err != nil {
return err
}
if len(args) < 1 {
return errors.New("required block hash parameter not specified")
}
blockHash, err := chainhash.NewHashFromStr(args[0])
if err != nil {
return err
}
// Load the block database.
db, err := loadBlockDB()
if err != nil {
return err
}
defer db.Close()
return db.View(func(tx database.Tx) error {
log.Infof("Fetching block %s", blockHash)
startTime := time.Now()
blockBytes, err := tx.FetchBlock(blockHash)
if err != nil {
return err
}
log.Infof("Loaded block in %v", time.Now().Sub(startTime))
log.Infof("Block Hex: %s", hex.EncodeToString(blockBytes))
return nil
})
}
示例9: Receive
// Receive waits for the response promised by the future and returns the hashes
// of all transactions in the memory pool.
func (r FutureGetRawMempoolResult) Receive() ([]*chainhash.Hash, error) {
res, err := receiveFuture(r)
if err != nil {
return nil, err
}
// Unmarshal the result as an array of strings.
var txHashStrs []string
err = json.Unmarshal(res, &txHashStrs)
if err != nil {
return nil, err
}
// Create a slice of ShaHash arrays from the string slice.
txHashes := make([]*chainhash.Hash, 0, len(txHashStrs))
for _, hashStr := range txHashStrs {
txHash, err := chainhash.NewHashFromStr(hashStr)
if err != nil {
return nil, err
}
txHashes = append(txHashes, txHash)
}
return txHashes, nil
}
示例10: BenchmarkDecodeNotFound
// BenchmarkDecodeNotFound performs a benchmark on how long it takes to decode
// a notfound message with the maximum number of entries.
func BenchmarkDecodeNotFound(b *testing.B) {
// Create a message with the maximum number of entries.
pver := ProtocolVersion
var m MsgNotFound
for i := 0; i < MaxInvPerMsg; i++ {
hash, err := chainhash.NewHashFromStr(fmt.Sprintf("%x", i))
if err != nil {
b.Fatalf("NewHashFromStr: unexpected error: %v", err)
}
m.AddInvVect(NewInvVect(InvTypeBlock, hash))
}
// Serialize it so the bytes are available to test the decode below.
var bb bytes.Buffer
if err := m.BtcEncode(&bb, pver); err != nil {
b.Fatalf("MsgNotFound.BtcEncode: unexpected error: %v", err)
}
buf := bb.Bytes()
r := bytes.NewReader(buf)
var msg MsgNotFound
b.ResetTimer()
for i := 0; i < b.N; i++ {
r.Seek(0, 0)
msg.BtcDecode(r, pver)
}
}
示例11: BenchmarkDecodeGetBlocks
// BenchmarkDecodeGetBlocks performs a benchmark on how long it takes to
// decode a getblocks message with the maximum number of block locator hashes.
func BenchmarkDecodeGetBlocks(b *testing.B) {
// Create a message with the maximum number of block locators.
pver := ProtocolVersion
var m MsgGetBlocks
for i := 0; i < MaxBlockLocatorsPerMsg; i++ {
hash, err := chainhash.NewHashFromStr(fmt.Sprintf("%x", i))
if err != nil {
b.Fatalf("NewHashFromStr: unexpected error: %v", err)
}
m.AddBlockLocatorHash(hash)
}
// Serialize it so the bytes are available to test the decode below.
var bb bytes.Buffer
if err := m.BtcEncode(&bb, pver); err != nil {
b.Fatalf("MsgGetBlocks.BtcEncode: unexpected error: %v", err)
}
buf := bb.Bytes()
r := bytes.NewReader(buf)
var msg MsgGetBlocks
b.ResetTimer()
for i := 0; i < b.N; i++ {
r.Seek(0, 0)
msg.BtcDecode(r, pver)
}
}
示例12: TestTx
// TestTx tests the API for Tx.
func TestTx(t *testing.T) {
testTx := Block100000.Transactions[0]
tx := btcutil.NewTx(testTx)
// Ensure we get the same data back out.
if msgTx := tx.MsgTx(); !reflect.DeepEqual(msgTx, testTx) {
t.Errorf("MsgTx: mismatched MsgTx - got %v, want %v",
spew.Sdump(msgTx), spew.Sdump(testTx))
}
// Ensure transaction index set and get work properly.
wantIndex := 0
tx.SetIndex(0)
if gotIndex := tx.Index(); gotIndex != wantIndex {
t.Errorf("Index: mismatched index - got %v, want %v",
gotIndex, wantIndex)
}
// Hash for block 100,000 transaction 0.
wantHashStr := "8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87"
wantHash, err := chainhash.NewHashFromStr(wantHashStr)
if err != nil {
t.Errorf("NewHashFromStr: %v", err)
}
// Request the hash multiple times to test generation and caching.
for i := 0; i < 2; i++ {
hash := tx.Hash()
if !hash.IsEqual(wantHash) {
t.Errorf("Hash #%d mismatched hash - got %v, want %v", i,
hash, wantHash)
}
}
}
示例13: ExampleNewFilter
// This example demonstrates how to create a new bloom filter, add a transaction
// hash to it, and check if the filter matches the transaction.
func ExampleNewFilter() {
rand.Seed(time.Now().UnixNano())
tweak := rand.Uint32()
// Create a new bloom filter intended to hold 10 elements with a 0.01%
// false positive rate and does not include any automatic update
// functionality when transactions are matched.
filter := bloom.NewFilter(10, tweak, 0.0001, wire.BloomUpdateNone)
// Create a transaction hash and add it to the filter. This particular
// trasaction is the first transaction in block 310,000 of the main
// bitcoin block chain.
txHashStr := "fd611c56ca0d378cdcd16244b45c2ba9588da3adac367c4ef43e808b280b8a45"
txHash, err := chainhash.NewHashFromStr(txHashStr)
if err != nil {
fmt.Println(err)
return
}
filter.AddHash(txHash)
// Show that the filter matches.
matches := filter.Matches(txHash[:])
fmt.Println("Filter Matches?:", matches)
// Output:
// Filter Matches?: true
}
示例14: Execute
// Execute is the main entry point for the command. It's invoked by the parser.
func (cmd *blockRegionCmd) Execute(args []string) error {
// Setup the global config options and ensure they are valid.
if err := setupGlobalConfig(); err != nil {
return err
}
// Ensure expected arguments.
if len(args) < 1 {
return errors.New("required block hash parameter not specified")
}
if len(args) < 2 {
return errors.New("required start offset parameter not " +
"specified")
}
if len(args) < 3 {
return errors.New("required region length parameter not " +
"specified")
}
// Parse arguments.
blockHash, err := chainhash.NewHashFromStr(args[0])
if err != nil {
return err
}
startOffset, err := strconv.ParseUint(args[1], 10, 32)
if err != nil {
return err
}
regionLen, err := strconv.ParseUint(args[2], 10, 32)
if err != nil {
return err
}
// Load the block database.
db, err := loadBlockDB()
if err != nil {
return err
}
defer db.Close()
return db.View(func(tx database.Tx) error {
log.Infof("Fetching block region %s<%d:%d>", blockHash,
startOffset, startOffset+regionLen-1)
region := database.BlockRegion{
Hash: blockHash,
Offset: uint32(startOffset),
Len: uint32(regionLen),
}
startTime := time.Now()
regionBytes, err := tx.FetchBlockRegion(®ion)
if err != nil {
return err
}
log.Infof("Loaded block region in %v", time.Now().Sub(startTime))
log.Infof("Double Hash: %s", chainhash.DoubleHashH(regionBytes))
log.Infof("Region Hex: %s", hex.EncodeToString(regionBytes))
return nil
})
}
示例15: TestMerkleBlock3
func TestMerkleBlock3(t *testing.T) {
blockStr := "0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b" +
"4b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdc" +
"c96b2c3ff60abe184f196367291b4d4c86041b8fa45d630101000000010" +
"00000000000000000000000000000000000000000000000000000000000" +
"0000ffffffff08044c86041b020a02ffffffff0100f2052a01000000434" +
"104ecd3229b0571c3be876feaac0442a9f13c5a572742927af1dc623353" +
"ecf8c202225f64868137a18cdd85cbbb4c74fbccfd4f49639cf1bdc94a5" +
"672bb15ad5d4cac00000000"
blockBytes, err := hex.DecodeString(blockStr)
if err != nil {
t.Errorf("TestMerkleBlock3 DecodeString failed: %v", err)
return
}
blk, err := btcutil.NewBlockFromBytes(blockBytes)
if err != nil {
t.Errorf("TestMerkleBlock3 NewBlockFromBytes failed: %v", err)
return
}
f := bloom.NewFilter(10, 0, 0.000001, wire.BloomUpdateAll)
inputStr := "63194f18be0af63f2c6bc9dc0f777cbefed3d9415c4af83f3ee3a3d669c00cb5"
hash, err := chainhash.NewHashFromStr(inputStr)
if err != nil {
t.Errorf("TestMerkleBlock3 NewHashFromStr failed: %v", err)
return
}
f.AddHash(hash)
mBlock, _ := bloom.NewMerkleBlock(blk, f)
wantStr := "0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4" +
"b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc" +
"96b2c3ff60abe184f196367291b4d4c86041b8fa45d630100000001b50c" +
"c069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f196" +
"30101"
want, err := hex.DecodeString(wantStr)
if err != nil {
t.Errorf("TestMerkleBlock3 DecodeString failed: %v", err)
return
}
got := bytes.NewBuffer(nil)
err = mBlock.BtcEncode(got, wire.ProtocolVersion)
if err != nil {
t.Errorf("TestMerkleBlock3 BtcEncode failed: %v", err)
return
}
if !bytes.Equal(want, got.Bytes()) {
t.Errorf("TestMerkleBlock3 failed merkle block comparison: "+
"got %v want %v", got.Bytes(), want)
return
}
}