本文整理汇总了Golang中github.com/conformal/btcwire.MsgBlock.BtcDecodeTxLoc方法的典型用法代码示例。如果您正苦于以下问题:Golang MsgBlock.BtcDecodeTxLoc方法的具体用法?Golang MsgBlock.BtcDecodeTxLoc怎么用?Golang MsgBlock.BtcDecodeTxLoc使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/conformal/btcwire.MsgBlock
的用法示例。
在下文中一共展示了MsgBlock.BtcDecodeTxLoc方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestBlockWireErrors
// TestBlockWireErrors performs negative tests against wire encode and decode
// of MsgBlock to confirm error paths work correctly.
func TestBlockWireErrors(t *testing.T) {
// Use protocol version 60002 specifically here instead of the latest
// because the test data is using bytes encoded with that protocol
// version.
pver := uint32(60002)
tests := []struct {
in *btcwire.MsgBlock // Value to encode
buf []byte // Wire encoding
pver uint32 // Protocol version for wire encoding
max int // Max size of fixed buffer to induce errors
writeErr error // Expected write error
readErr error // Expected read error
}{
// Force error in version.
{&blockOne, blockOneBytes, pver, 0, io.ErrShortWrite, io.EOF},
// Force error in prev block hash.
{&blockOne, blockOneBytes, pver, 4, io.ErrShortWrite, io.EOF},
// Force error in merkle root.
{&blockOne, blockOneBytes, pver, 36, io.ErrShortWrite, io.EOF},
// Force error in timestamp.
{&blockOne, blockOneBytes, pver, 68, io.ErrShortWrite, io.EOF},
// Force error in difficulty bits.
{&blockOne, blockOneBytes, pver, 72, io.ErrShortWrite, io.EOF},
// Force error in header nonce.
{&blockOne, blockOneBytes, pver, 76, io.ErrShortWrite, io.EOF},
// Force error in transaction count.
{&blockOne, blockOneBytes, pver, 80, io.ErrShortWrite, io.EOF},
// Force error in transactions.
{&blockOne, blockOneBytes, pver, 81, io.ErrShortWrite, io.EOF},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
// Encode to wire format.
w := newFixedWriter(test.max)
err := test.in.BtcEncode(w, test.pver)
if err != test.writeErr {
t.Errorf("BtcEncode #%d wrong error got: %v, want: %v",
i, err, test.writeErr)
continue
}
// Decode from wire format.
var msg btcwire.MsgBlock
r := newFixedReader(test.max, test.buf)
err = msg.BtcDecode(r, test.pver)
if err != test.readErr {
t.Errorf("BtcDecode #%d wrong error got: %v, want: %v",
i, err, test.readErr)
continue
}
var txLocMsg btcwire.MsgBlock
rbuf := bytes.NewBuffer(test.buf[0:test.max])
_, err = txLocMsg.BtcDecodeTxLoc(rbuf, test.pver)
if err != test.readErr {
t.Errorf("BtcDecodeTxLoc #%d wrong error got: %v, want: %v",
i, err, test.readErr)
continue
}
}
}
示例2: TestBlockWire
// TestBlockWire tests the MsgBlock wire encode and decode for various numbers
// of transaction inputs and outputs and protocol versions.
func TestBlockWire(t *testing.T) {
tests := []struct {
in *btcwire.MsgBlock // Message to encode
out *btcwire.MsgBlock // Expected decoded message
buf []byte // Wire encoding
txLocs []btcwire.TxLoc // Expected transaction locations
pver uint32 // Protocol version for wire encoding
}{
// Latest protocol version.
{
&blockOne,
&blockOne,
blockOneBytes,
blockOneTxLocs,
btcwire.ProtocolVersion,
},
// Protocol version BIP0035Version.
{
&blockOne,
&blockOne,
blockOneBytes,
blockOneTxLocs,
btcwire.BIP0035Version,
},
// Protocol version BIP0031Version.
{
&blockOne,
&blockOne,
blockOneBytes,
blockOneTxLocs,
btcwire.BIP0031Version,
},
// Protocol version NetAddressTimeVersion.
{
&blockOne,
&blockOne,
blockOneBytes,
blockOneTxLocs,
btcwire.NetAddressTimeVersion,
},
// Protocol version MultipleAddressVersion.
{
&blockOne,
&blockOne,
blockOneBytes,
blockOneTxLocs,
btcwire.MultipleAddressVersion,
},
}
t.Logf("Running %d tests", len(tests))
for i, test := range tests {
// Encode the message to wire format.
var buf bytes.Buffer
err := test.in.BtcEncode(&buf, test.pver)
if err != nil {
t.Errorf("BtcEncode #%d error %v", i, err)
continue
}
if !bytes.Equal(buf.Bytes(), test.buf) {
t.Errorf("BtcEncode #%d\n got: %s want: %s", i,
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
continue
}
// Decode the message from wire format.
var msg btcwire.MsgBlock
rbuf := bytes.NewBuffer(test.buf)
err = msg.BtcDecode(rbuf, test.pver)
if err != nil {
t.Errorf("BtcDecode #%d error %v", i, err)
continue
}
if !reflect.DeepEqual(&msg, test.out) {
t.Errorf("BtcDecode #%d\n got: %s want: %s", i,
spew.Sdump(&msg), spew.Sdump(test.out))
continue
}
var txLocMsg btcwire.MsgBlock
rbuf = bytes.NewBuffer(test.buf)
txLocs, err := txLocMsg.BtcDecodeTxLoc(rbuf, test.pver)
if err != nil {
t.Errorf("BtcDecodeTxLoc #%d error %v", i, err)
continue
}
if !reflect.DeepEqual(&txLocMsg, test.out) {
t.Errorf("BtcDecodeTxLoc #%d\n got: %s want: %s", i,
spew.Sdump(&txLocMsg), spew.Sdump(test.out))
continue
}
if !reflect.DeepEqual(txLocs, test.txLocs) {
t.Errorf("BtcDecodeTxLoc #%d\n got: %s want: %s", i,
spew.Sdump(txLocs), spew.Sdump(test.txLocs))
//.........这里部分代码省略.........