2016-08-08 21:04:33 +02:00
|
|
|
// Copyright (c) 2013-2016 The btcsuite developers
|
2013-11-06 02:56:12 +01:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-01-31 19:32:55 +01:00
|
|
|
package wire
|
2013-11-06 02:56:12 +01:00
|
|
|
|
|
|
|
import (
|
2013-11-06 04:35:28 +01:00
|
|
|
"bytes"
|
2016-04-25 23:51:27 +02:00
|
|
|
"compress/bzip2"
|
2016-04-26 00:32:29 +02:00
|
|
|
"fmt"
|
2013-11-06 02:56:12 +01:00
|
|
|
"io/ioutil"
|
2016-04-26 00:32:29 +02:00
|
|
|
"net"
|
2016-04-25 23:51:27 +02:00
|
|
|
"os"
|
2013-11-06 02:56:12 +01:00
|
|
|
"testing"
|
2016-08-08 21:04:33 +02:00
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2013-11-06 02:56:12 +01:00
|
|
|
)
|
|
|
|
|
2014-05-28 07:26:47 +02:00
|
|
|
// genesisCoinbaseTx is the coinbase transaction for the genesis blocks for
|
|
|
|
// the main network, regression test network, and test network (version 3).
|
2015-01-14 07:09:43 +01:00
|
|
|
var genesisCoinbaseTx = MsgTx{
|
2014-05-28 07:26:47 +02:00
|
|
|
Version: 1,
|
2015-01-14 07:09:43 +01:00
|
|
|
TxIn: []*TxIn{
|
2014-05-28 07:26:47 +02:00
|
|
|
{
|
2015-01-14 07:09:43 +01:00
|
|
|
PreviousOutPoint: OutPoint{
|
2016-08-08 21:04:33 +02:00
|
|
|
Hash: chainhash.Hash{},
|
2014-05-28 07:26:47 +02:00
|
|
|
Index: 0xffffffff,
|
|
|
|
},
|
|
|
|
SignatureScript: []byte{
|
|
|
|
0x04, 0xff, 0xff, 0x00, 0x1d, 0x01, 0x04, 0x45, /* |.......E| */
|
|
|
|
0x54, 0x68, 0x65, 0x20, 0x54, 0x69, 0x6d, 0x65, /* |The Time| */
|
|
|
|
0x73, 0x20, 0x30, 0x33, 0x2f, 0x4a, 0x61, 0x6e, /* |s 03/Jan| */
|
|
|
|
0x2f, 0x32, 0x30, 0x30, 0x39, 0x20, 0x43, 0x68, /* |/2009 Ch| */
|
|
|
|
0x61, 0x6e, 0x63, 0x65, 0x6c, 0x6c, 0x6f, 0x72, /* |ancellor| */
|
|
|
|
0x20, 0x6f, 0x6e, 0x20, 0x62, 0x72, 0x69, 0x6e, /* | on brin| */
|
|
|
|
0x6b, 0x20, 0x6f, 0x66, 0x20, 0x73, 0x65, 0x63, /* |k of sec|*/
|
|
|
|
0x6f, 0x6e, 0x64, 0x20, 0x62, 0x61, 0x69, 0x6c, /* |ond bail| */
|
|
|
|
0x6f, 0x75, 0x74, 0x20, 0x66, 0x6f, 0x72, 0x20, /* |out for |*/
|
|
|
|
0x62, 0x61, 0x6e, 0x6b, 0x73, /* |banks| */
|
|
|
|
},
|
|
|
|
Sequence: 0xffffffff,
|
|
|
|
},
|
|
|
|
},
|
2015-01-14 07:09:43 +01:00
|
|
|
TxOut: []*TxOut{
|
2014-05-28 07:26:47 +02:00
|
|
|
{
|
|
|
|
Value: 0x12a05f200,
|
|
|
|
PkScript: []byte{
|
|
|
|
0x41, 0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, /* |A.g....U| */
|
|
|
|
0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, /* |H'.g..q0| */
|
|
|
|
0xb7, 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, /* |..\..(.9| */
|
|
|
|
0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, /* |..yb...a| */
|
|
|
|
0xde, 0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, /* |..I..?L.| */
|
|
|
|
0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, /* |8..U....| */
|
|
|
|
0x12, 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, /* |..\8M...| */
|
|
|
|
0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, /* |.W.Lp+k.| */
|
|
|
|
0x1d, 0x5f, 0xac, /* |._.| */
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
LockTime: 0,
|
|
|
|
}
|
|
|
|
|
2013-11-06 02:56:12 +01:00
|
|
|
// BenchmarkWriteVarInt1 performs a benchmark on how long it takes to write
|
|
|
|
// a single byte variable length integer.
|
|
|
|
func BenchmarkWriteVarInt1(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
2016-02-22 17:13:11 +01:00
|
|
|
WriteVarInt(ioutil.Discard, 0, 1)
|
2013-11-06 02:56:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkWriteVarInt3 performs a benchmark on how long it takes to write
|
|
|
|
// a three byte variable length integer.
|
|
|
|
func BenchmarkWriteVarInt3(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
2016-02-22 17:13:11 +01:00
|
|
|
WriteVarInt(ioutil.Discard, 0, 65535)
|
2013-11-06 02:56:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkWriteVarInt5 performs a benchmark on how long it takes to write
|
|
|
|
// a five byte variable length integer.
|
|
|
|
func BenchmarkWriteVarInt5(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
2016-02-22 17:13:11 +01:00
|
|
|
WriteVarInt(ioutil.Discard, 0, 4294967295)
|
2013-11-06 02:56:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkWriteVarInt9 performs a benchmark on how long it takes to write
|
|
|
|
// a nine byte variable length integer.
|
|
|
|
func BenchmarkWriteVarInt9(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
2016-02-22 17:13:11 +01:00
|
|
|
WriteVarInt(ioutil.Discard, 0, 18446744073709551615)
|
2013-11-06 02:56:12 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-06 04:35:28 +01:00
|
|
|
|
2013-11-06 06:03:54 +01:00
|
|
|
// BenchmarkReadVarInt1 performs a benchmark on how long it takes to read
|
2013-11-06 04:35:28 +01:00
|
|
|
// a single byte variable length integer.
|
|
|
|
func BenchmarkReadVarInt1(b *testing.B) {
|
|
|
|
buf := []byte{0x01}
|
2016-04-25 23:57:44 +02:00
|
|
|
r := bytes.NewReader(buf)
|
2013-11-06 04:35:28 +01:00
|
|
|
for i := 0; i < b.N; i++ {
|
2016-04-25 23:57:44 +02:00
|
|
|
r.Seek(0, 0)
|
|
|
|
ReadVarInt(r, 0)
|
2013-11-06 04:35:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 06:03:54 +01:00
|
|
|
// BenchmarkReadVarInt3 performs a benchmark on how long it takes to read
|
2013-11-06 04:35:28 +01:00
|
|
|
// a three byte variable length integer.
|
|
|
|
func BenchmarkReadVarInt3(b *testing.B) {
|
|
|
|
buf := []byte{0x0fd, 0xff, 0xff}
|
2016-04-25 23:57:44 +02:00
|
|
|
r := bytes.NewReader(buf)
|
2013-11-06 04:35:28 +01:00
|
|
|
for i := 0; i < b.N; i++ {
|
2016-04-25 23:57:44 +02:00
|
|
|
r.Seek(0, 0)
|
|
|
|
ReadVarInt(r, 0)
|
2013-11-06 04:35:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 06:03:54 +01:00
|
|
|
// BenchmarkReadVarInt5 performs a benchmark on how long it takes to read
|
2013-11-06 04:35:28 +01:00
|
|
|
// a five byte variable length integer.
|
|
|
|
func BenchmarkReadVarInt5(b *testing.B) {
|
|
|
|
buf := []byte{0xfe, 0xff, 0xff, 0xff, 0xff}
|
2016-04-25 23:57:44 +02:00
|
|
|
r := bytes.NewReader(buf)
|
2013-11-06 04:35:28 +01:00
|
|
|
for i := 0; i < b.N; i++ {
|
2016-04-25 23:57:44 +02:00
|
|
|
r.Seek(0, 0)
|
|
|
|
ReadVarInt(r, 0)
|
2013-11-06 04:35:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 06:03:54 +01:00
|
|
|
// BenchmarkReadVarInt9 performs a benchmark on how long it takes to read
|
2013-11-06 04:35:28 +01:00
|
|
|
// a nine byte variable length integer.
|
|
|
|
func BenchmarkReadVarInt9(b *testing.B) {
|
|
|
|
buf := []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
|
2016-04-25 23:57:44 +02:00
|
|
|
r := bytes.NewReader(buf)
|
2013-11-06 04:35:28 +01:00
|
|
|
for i := 0; i < b.N; i++ {
|
2016-04-25 23:57:44 +02:00
|
|
|
r.Seek(0, 0)
|
|
|
|
ReadVarInt(r, 0)
|
2013-11-06 04:35:28 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-06 06:08:55 +01:00
|
|
|
|
|
|
|
// BenchmarkReadVarStr4 performs a benchmark on how long it takes to read a
|
|
|
|
// four byte variable length string.
|
|
|
|
func BenchmarkReadVarStr4(b *testing.B) {
|
|
|
|
buf := []byte{0x04, 't', 'e', 's', 't'}
|
2016-04-25 23:57:44 +02:00
|
|
|
r := bytes.NewReader(buf)
|
2013-11-06 06:08:55 +01:00
|
|
|
for i := 0; i < b.N; i++ {
|
2016-04-25 23:57:44 +02:00
|
|
|
r.Seek(0, 0)
|
|
|
|
ReadVarString(r, 0)
|
2013-11-06 06:08:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkReadVarStr10 performs a benchmark on how long it takes to read a
|
|
|
|
// ten byte variable length string.
|
|
|
|
func BenchmarkReadVarStr10(b *testing.B) {
|
|
|
|
buf := []byte{0x0a, 't', 'e', 's', 't', '0', '1', '2', '3', '4', '5'}
|
2016-04-25 23:57:44 +02:00
|
|
|
r := bytes.NewReader(buf)
|
2013-11-06 06:08:55 +01:00
|
|
|
for i := 0; i < b.N; i++ {
|
2016-04-25 23:57:44 +02:00
|
|
|
r.Seek(0, 0)
|
|
|
|
ReadVarString(r, 0)
|
2013-11-06 06:08:55 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-06 06:49:34 +01:00
|
|
|
|
|
|
|
// BenchmarkWriteVarStr4 performs a benchmark on how long it takes to write a
|
|
|
|
// four byte variable length string.
|
|
|
|
func BenchmarkWriteVarStr4(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
2015-10-16 17:52:39 +02:00
|
|
|
WriteVarString(ioutil.Discard, 0, "test")
|
2013-11-06 06:49:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkWriteVarStr10 performs a benchmark on how long it takes to write a
|
2013-11-06 14:33:37 +01:00
|
|
|
// ten byte variable length string.
|
2013-11-06 06:49:34 +01:00
|
|
|
func BenchmarkWriteVarStr10(b *testing.B) {
|
|
|
|
for i := 0; i < b.N; i++ {
|
2015-10-16 17:52:39 +02:00
|
|
|
WriteVarString(ioutil.Discard, 0, "test012345")
|
2013-11-06 06:49:34 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-06 07:43:52 +01:00
|
|
|
|
2013-11-06 14:33:37 +01:00
|
|
|
// BenchmarkReadOutPoint performs a benchmark on how long it takes to read a
|
|
|
|
// transaction output point.
|
|
|
|
func BenchmarkReadOutPoint(b *testing.B) {
|
|
|
|
buf := []byte{
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash
|
|
|
|
0xff, 0xff, 0xff, 0xff, // Previous output index
|
|
|
|
}
|
2016-04-25 23:57:44 +02:00
|
|
|
r := bytes.NewReader(buf)
|
2015-01-14 07:09:43 +01:00
|
|
|
var op OutPoint
|
2013-11-06 14:33:37 +01:00
|
|
|
for i := 0; i < b.N; i++ {
|
2016-04-25 23:57:44 +02:00
|
|
|
r.Seek(0, 0)
|
|
|
|
readOutPoint(r, 0, 0, &op)
|
2013-11-06 14:33:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 07:43:52 +01:00
|
|
|
// BenchmarkWriteOutPoint performs a benchmark on how long it takes to write a
|
|
|
|
// transaction output point.
|
|
|
|
func BenchmarkWriteOutPoint(b *testing.B) {
|
2015-01-14 07:09:43 +01:00
|
|
|
op := &OutPoint{
|
2016-08-08 21:04:33 +02:00
|
|
|
Hash: chainhash.Hash{},
|
2013-11-06 07:43:52 +01:00
|
|
|
Index: 0,
|
|
|
|
}
|
|
|
|
for i := 0; i < b.N; i++ {
|
2022-01-07 02:30:28 +01:00
|
|
|
WriteOutPoint(ioutil.Discard, 0, 0, op)
|
2013-11-06 07:43:52 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-06 22:21:37 +01:00
|
|
|
|
2013-11-06 22:40:46 +01:00
|
|
|
// BenchmarkReadTxOut performs a benchmark on how long it takes to read a
|
|
|
|
// transaction output.
|
|
|
|
func BenchmarkReadTxOut(b *testing.B) {
|
|
|
|
buf := []byte{
|
|
|
|
0x00, 0xf2, 0x05, 0x2a, 0x01, 0x00, 0x00, 0x00, // Transaction amount
|
|
|
|
0x43, // Varint for length of pk script
|
|
|
|
0x41, // OP_DATA_65
|
|
|
|
0x04, 0x96, 0xb5, 0x38, 0xe8, 0x53, 0x51, 0x9c,
|
|
|
|
0x72, 0x6a, 0x2c, 0x91, 0xe6, 0x1e, 0xc1, 0x16,
|
|
|
|
0x00, 0xae, 0x13, 0x90, 0x81, 0x3a, 0x62, 0x7c,
|
|
|
|
0x66, 0xfb, 0x8b, 0xe7, 0x94, 0x7b, 0xe6, 0x3c,
|
|
|
|
0x52, 0xda, 0x75, 0x89, 0x37, 0x95, 0x15, 0xd4,
|
|
|
|
0xe0, 0xa6, 0x04, 0xf8, 0x14, 0x17, 0x81, 0xe6,
|
|
|
|
0x22, 0x94, 0x72, 0x11, 0x66, 0xbf, 0x62, 0x1e,
|
|
|
|
0x73, 0xa8, 0x2c, 0xbf, 0x23, 0x42, 0xc8, 0x58,
|
|
|
|
0xee, // 65-byte signature
|
|
|
|
0xac, // OP_CHECKSIG
|
|
|
|
}
|
2016-04-25 23:57:44 +02:00
|
|
|
r := bytes.NewReader(buf)
|
2015-01-14 07:09:43 +01:00
|
|
|
var txOut TxOut
|
2013-11-06 22:40:46 +01:00
|
|
|
for i := 0; i < b.N; i++ {
|
2016-04-25 23:57:44 +02:00
|
|
|
r.Seek(0, 0)
|
2022-02-20 23:55:32 +01:00
|
|
|
ReadTxOut(r, 0, 0, &txOut)
|
2016-04-21 04:08:01 +02:00
|
|
|
scriptPool.Return(txOut.PkScript)
|
2013-11-06 22:40:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 22:21:37 +01:00
|
|
|
// BenchmarkWriteTxOut performs a benchmark on how long it takes to write
|
|
|
|
// a transaction output.
|
|
|
|
func BenchmarkWriteTxOut(b *testing.B) {
|
|
|
|
txOut := blockOne.Transactions[0].TxOut[0]
|
|
|
|
for i := 0; i < b.N; i++ {
|
2016-10-19 02:32:50 +02:00
|
|
|
WriteTxOut(ioutil.Discard, 0, 0, txOut)
|
2013-11-06 22:21:37 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-06 23:06:53 +01:00
|
|
|
|
|
|
|
// BenchmarkReadTxIn performs a benchmark on how long it takes to read a
|
|
|
|
// transaction input.
|
|
|
|
func BenchmarkReadTxIn(b *testing.B) {
|
|
|
|
buf := []byte{
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash
|
|
|
|
0xff, 0xff, 0xff, 0xff, // Previous output index
|
|
|
|
0x07, // Varint for length of signature script
|
|
|
|
0x04, 0xff, 0xff, 0x00, 0x1d, 0x01, 0x04, // Signature script
|
|
|
|
0xff, 0xff, 0xff, 0xff, // Sequence
|
|
|
|
}
|
2016-04-25 23:57:44 +02:00
|
|
|
r := bytes.NewReader(buf)
|
2015-01-14 07:09:43 +01:00
|
|
|
var txIn TxIn
|
2013-11-06 23:06:53 +01:00
|
|
|
for i := 0; i < b.N; i++ {
|
2016-04-25 23:57:44 +02:00
|
|
|
r.Seek(0, 0)
|
|
|
|
readTxIn(r, 0, 0, &txIn)
|
2016-04-21 04:08:01 +02:00
|
|
|
scriptPool.Return(txIn.SignatureScript)
|
2013-11-06 23:06:53 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-06 23:21:49 +01:00
|
|
|
|
|
|
|
// BenchmarkWriteTxIn performs a benchmark on how long it takes to write
|
|
|
|
// a transaction input.
|
|
|
|
func BenchmarkWriteTxIn(b *testing.B) {
|
|
|
|
txIn := blockOne.Transactions[0].TxIn[0]
|
|
|
|
for i := 0; i < b.N; i++ {
|
2015-01-14 07:09:43 +01:00
|
|
|
writeTxIn(ioutil.Discard, 0, 0, txIn)
|
2013-11-06 23:21:49 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-06 23:56:42 +01:00
|
|
|
|
2013-11-07 01:01:29 +01:00
|
|
|
// BenchmarkDeserializeTx performs a benchmark on how long it takes to
|
2016-04-25 23:51:27 +02:00
|
|
|
// deserialize a small transaction.
|
|
|
|
func BenchmarkDeserializeTxSmall(b *testing.B) {
|
2013-11-07 01:01:29 +01:00
|
|
|
buf := []byte{
|
|
|
|
0x01, 0x00, 0x00, 0x00, // Version
|
|
|
|
0x01, // Varint for number of input transactions
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // // Previous output hash
|
|
|
|
0xff, 0xff, 0xff, 0xff, // Prevous output index
|
|
|
|
0x07, // Varint for length of signature script
|
|
|
|
0x04, 0xff, 0xff, 0x00, 0x1d, 0x01, 0x04, // Signature script
|
|
|
|
0xff, 0xff, 0xff, 0xff, // Sequence
|
|
|
|
0x01, // Varint for number of output transactions
|
|
|
|
0x00, 0xf2, 0x05, 0x2a, 0x01, 0x00, 0x00, 0x00, // Transaction amount
|
|
|
|
0x43, // Varint for length of pk script
|
|
|
|
0x41, // OP_DATA_65
|
|
|
|
0x04, 0x96, 0xb5, 0x38, 0xe8, 0x53, 0x51, 0x9c,
|
|
|
|
0x72, 0x6a, 0x2c, 0x91, 0xe6, 0x1e, 0xc1, 0x16,
|
|
|
|
0x00, 0xae, 0x13, 0x90, 0x81, 0x3a, 0x62, 0x7c,
|
|
|
|
0x66, 0xfb, 0x8b, 0xe7, 0x94, 0x7b, 0xe6, 0x3c,
|
|
|
|
0x52, 0xda, 0x75, 0x89, 0x37, 0x95, 0x15, 0xd4,
|
|
|
|
0xe0, 0xa6, 0x04, 0xf8, 0x14, 0x17, 0x81, 0xe6,
|
|
|
|
0x22, 0x94, 0x72, 0x11, 0x66, 0xbf, 0x62, 0x1e,
|
|
|
|
0x73, 0xa8, 0x2c, 0xbf, 0x23, 0x42, 0xc8, 0x58,
|
|
|
|
0xee, // 65-byte signature
|
|
|
|
0xac, // OP_CHECKSIG
|
|
|
|
0x00, 0x00, 0x00, 0x00, // Lock time
|
|
|
|
}
|
2016-04-25 23:57:44 +02:00
|
|
|
|
|
|
|
r := bytes.NewReader(buf)
|
2015-01-14 07:09:43 +01:00
|
|
|
var tx MsgTx
|
2013-11-07 01:01:29 +01:00
|
|
|
for i := 0; i < b.N; i++ {
|
2016-04-25 23:57:44 +02:00
|
|
|
r.Seek(0, 0)
|
|
|
|
tx.Deserialize(r)
|
2016-04-25 23:51:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkDeserializeTxLarge performs a benchmark on how long it takes to
|
|
|
|
// deserialize a very large transaction.
|
|
|
|
func BenchmarkDeserializeTxLarge(b *testing.B) {
|
|
|
|
// tx bb41a757f405890fb0f5856228e23b715702d714d59bf2b1feb70d8b2b4e3e08
|
|
|
|
// from the main block chain.
|
|
|
|
fi, err := os.Open("testdata/megatx.bin.bz2")
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("Failed to read transaction data: %v", err)
|
|
|
|
}
|
|
|
|
defer fi.Close()
|
|
|
|
buf, err := ioutil.ReadAll(bzip2.NewReader(fi))
|
|
|
|
if err != nil {
|
|
|
|
b.Fatalf("Failed to read transaction data: %v", err)
|
|
|
|
}
|
2013-11-07 01:01:29 +01:00
|
|
|
|
2016-04-25 23:57:44 +02:00
|
|
|
r := bytes.NewReader(buf)
|
2016-04-25 23:51:27 +02:00
|
|
|
var tx MsgTx
|
|
|
|
for i := 0; i < b.N; i++ {
|
2016-04-25 23:57:44 +02:00
|
|
|
r.Seek(0, 0)
|
|
|
|
tx.Deserialize(r)
|
2013-11-07 01:01:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-06 23:56:42 +01:00
|
|
|
// BenchmarkSerializeTx performs a benchmark on how long it takes to serialize
|
|
|
|
// a transaction.
|
|
|
|
func BenchmarkSerializeTx(b *testing.B) {
|
|
|
|
tx := blockOne.Transactions[0]
|
|
|
|
for i := 0; i < b.N; i++ {
|
2013-11-07 01:01:29 +01:00
|
|
|
tx.Serialize(ioutil.Discard)
|
2013-11-06 23:56:42 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2013-11-07 03:35:17 +01:00
|
|
|
|
|
|
|
// BenchmarkReadBlockHeader performs a benchmark on how long it takes to
|
|
|
|
// deserialize a block header.
|
|
|
|
func BenchmarkReadBlockHeader(b *testing.B) {
|
|
|
|
buf := []byte{
|
|
|
|
0x01, 0x00, 0x00, 0x00, // Version 1
|
|
|
|
0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
|
|
|
|
0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
|
|
|
|
0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
|
|
|
|
0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, // PrevBlock
|
|
|
|
0x3b, 0xa3, 0xed, 0xfd, 0x7a, 0x7b, 0x12, 0xb2,
|
|
|
|
0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76, 0x8f, 0x61,
|
|
|
|
0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32,
|
|
|
|
0x3a, 0x9f, 0xb8, 0xaa, 0x4b, 0x1e, 0x5e, 0x4a, // MerkleRoot
|
|
|
|
0x29, 0xab, 0x5f, 0x49, // Timestamp
|
|
|
|
0xff, 0xff, 0x00, 0x1d, // Bits
|
|
|
|
0xf3, 0xe0, 0x01, 0x00, // Nonce
|
|
|
|
0x00, // TxnCount Varint
|
|
|
|
}
|
2016-04-25 23:57:44 +02:00
|
|
|
r := bytes.NewReader(buf)
|
2015-01-14 07:09:43 +01:00
|
|
|
var header BlockHeader
|
2013-11-07 03:35:17 +01:00
|
|
|
for i := 0; i < b.N; i++ {
|
2016-04-25 23:57:44 +02:00
|
|
|
r.Seek(0, 0)
|
|
|
|
readBlockHeader(r, 0, &header)
|
2013-11-07 03:35:17 +01:00
|
|
|
}
|
|
|
|
}
|
2013-11-07 07:30:20 +01:00
|
|
|
|
|
|
|
// BenchmarkWriteBlockHeader performs a benchmark on how long it takes to
|
|
|
|
// serialize a block header.
|
|
|
|
func BenchmarkWriteBlockHeader(b *testing.B) {
|
|
|
|
header := blockOne.Header
|
|
|
|
for i := 0; i < b.N; i++ {
|
2015-01-14 07:09:43 +01:00
|
|
|
writeBlockHeader(ioutil.Discard, 0, &header)
|
2013-11-07 07:30:20 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-20 16:46:03 +01:00
|
|
|
|
2016-04-26 00:32:29 +02:00
|
|
|
// BenchmarkDecodeGetHeaders performs a benchmark on how long it takes to
|
|
|
|
// decode a getheaders message with the maximum number of block locator hashes.
|
|
|
|
func BenchmarkDecodeGetHeaders(b *testing.B) {
|
|
|
|
// Create a message with the maximum number of block locators.
|
|
|
|
pver := ProtocolVersion
|
|
|
|
var m MsgGetHeaders
|
|
|
|
for i := 0; i < MaxBlockLocatorsPerMsg; i++ {
|
2016-08-08 21:04:33 +02:00
|
|
|
hash, err := chainhash.NewHashFromStr(fmt.Sprintf("%x", i))
|
2016-04-26 00:32:29 +02:00
|
|
|
if err != nil {
|
2016-08-08 21:04:33 +02:00
|
|
|
b.Fatalf("NewHashFromStr: unexpected error: %v", err)
|
2016-04-26 00:32:29 +02:00
|
|
|
}
|
|
|
|
m.AddBlockLocatorHash(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize it so the bytes are available to test the decode below.
|
|
|
|
var bb bytes.Buffer
|
2016-10-19 01:21:48 +02:00
|
|
|
if err := m.BtcEncode(&bb, pver, LatestEncoding); err != nil {
|
2016-04-26 00:32:29 +02:00
|
|
|
b.Fatalf("MsgGetHeaders.BtcEncode: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
buf := bb.Bytes()
|
|
|
|
|
|
|
|
r := bytes.NewReader(buf)
|
|
|
|
var msg MsgGetHeaders
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
r.Seek(0, 0)
|
2016-10-19 01:21:48 +02:00
|
|
|
msg.BtcDecode(r, pver, LatestEncoding)
|
2016-04-26 00:32:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkDecodeHeaders performs a benchmark on how long it takes to
|
|
|
|
// decode a headers message with the maximum number of headers.
|
|
|
|
func BenchmarkDecodeHeaders(b *testing.B) {
|
|
|
|
// Create a message with the maximum number of headers.
|
|
|
|
pver := ProtocolVersion
|
|
|
|
var m MsgHeaders
|
|
|
|
for i := 0; i < MaxBlockHeadersPerMsg; i++ {
|
2016-08-08 21:04:33 +02:00
|
|
|
hash, err := chainhash.NewHashFromStr(fmt.Sprintf("%x", i))
|
2016-04-26 00:32:29 +02:00
|
|
|
if err != nil {
|
2016-08-08 21:04:33 +02:00
|
|
|
b.Fatalf("NewHashFromStr: unexpected error: %v", err)
|
2016-04-26 00:32:29 +02:00
|
|
|
}
|
2016-08-29 00:51:55 +02:00
|
|
|
m.AddBlockHeader(NewBlockHeader(1, hash, hash, 0, uint32(i)))
|
2016-04-26 00:32:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize it so the bytes are available to test the decode below.
|
|
|
|
var bb bytes.Buffer
|
2016-10-19 01:21:48 +02:00
|
|
|
if err := m.BtcEncode(&bb, pver, LatestEncoding); err != nil {
|
2016-04-26 00:32:29 +02:00
|
|
|
b.Fatalf("MsgHeaders.BtcEncode: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
buf := bb.Bytes()
|
|
|
|
|
|
|
|
r := bytes.NewReader(buf)
|
|
|
|
var msg MsgHeaders
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
r.Seek(0, 0)
|
2016-10-19 01:21:48 +02:00
|
|
|
msg.BtcDecode(r, pver, LatestEncoding)
|
2016-04-26 00:32:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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++ {
|
2016-08-08 21:04:33 +02:00
|
|
|
hash, err := chainhash.NewHashFromStr(fmt.Sprintf("%x", i))
|
2016-04-26 00:32:29 +02:00
|
|
|
if err != nil {
|
2016-08-08 21:04:33 +02:00
|
|
|
b.Fatalf("NewHashFromStr: unexpected error: %v", err)
|
2016-04-26 00:32:29 +02:00
|
|
|
}
|
|
|
|
m.AddBlockLocatorHash(hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize it so the bytes are available to test the decode below.
|
|
|
|
var bb bytes.Buffer
|
2016-10-19 01:21:48 +02:00
|
|
|
if err := m.BtcEncode(&bb, pver, LatestEncoding); err != nil {
|
2016-04-26 00:32:29 +02:00
|
|
|
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)
|
2016-10-19 01:21:48 +02:00
|
|
|
msg.BtcDecode(r, pver, LatestEncoding)
|
2016-04-26 00:32:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkDecodeAddr performs a benchmark on how long it takes to decode an
|
|
|
|
// addr message with the maximum number of addresses.
|
|
|
|
func BenchmarkDecodeAddr(b *testing.B) {
|
|
|
|
// Create a message with the maximum number of addresses.
|
|
|
|
pver := ProtocolVersion
|
|
|
|
ip := net.ParseIP("127.0.0.1")
|
|
|
|
ma := NewMsgAddr()
|
|
|
|
for port := uint16(0); port < MaxAddrPerMsg; port++ {
|
|
|
|
ma.AddAddress(NewNetAddressIPPort(ip, port, SFNodeNetwork))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize it so the bytes are available to test the decode below.
|
|
|
|
var bb bytes.Buffer
|
2016-10-19 01:21:48 +02:00
|
|
|
if err := ma.BtcEncode(&bb, pver, LatestEncoding); err != nil {
|
2016-04-26 00:32:29 +02:00
|
|
|
b.Fatalf("MsgAddr.BtcEncode: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
buf := bb.Bytes()
|
|
|
|
|
|
|
|
r := bytes.NewReader(buf)
|
|
|
|
var msg MsgAddr
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
r.Seek(0, 0)
|
2016-10-19 01:21:48 +02:00
|
|
|
msg.BtcDecode(r, pver, LatestEncoding)
|
2016-04-26 00:32:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BenchmarkDecodeInv performs a benchmark on how long it takes to decode an inv
|
|
|
|
// message with the maximum number of entries.
|
|
|
|
func BenchmarkDecodeInv(b *testing.B) {
|
|
|
|
// Create a message with the maximum number of entries.
|
|
|
|
pver := ProtocolVersion
|
|
|
|
var m MsgInv
|
|
|
|
for i := 0; i < MaxInvPerMsg; i++ {
|
2016-08-08 21:04:33 +02:00
|
|
|
hash, err := chainhash.NewHashFromStr(fmt.Sprintf("%x", i))
|
2016-04-26 00:32:29 +02:00
|
|
|
if err != nil {
|
2016-08-08 21:04:33 +02:00
|
|
|
b.Fatalf("NewHashFromStr: unexpected error: %v", err)
|
2016-04-26 00:32:29 +02:00
|
|
|
}
|
|
|
|
m.AddInvVect(NewInvVect(InvTypeBlock, hash))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize it so the bytes are available to test the decode below.
|
|
|
|
var bb bytes.Buffer
|
2016-10-19 01:21:48 +02:00
|
|
|
if err := m.BtcEncode(&bb, pver, LatestEncoding); err != nil {
|
2016-04-26 00:32:29 +02:00
|
|
|
b.Fatalf("MsgInv.BtcEncode: unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
buf := bb.Bytes()
|
|
|
|
|
|
|
|
r := bytes.NewReader(buf)
|
|
|
|
var msg MsgInv
|
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
r.Seek(0, 0)
|
2016-10-19 01:21:48 +02:00
|
|
|
msg.BtcDecode(r, pver, LatestEncoding)
|
2016-04-26 00:32:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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++ {
|
2016-08-08 21:04:33 +02:00
|
|
|
hash, err := chainhash.NewHashFromStr(fmt.Sprintf("%x", i))
|
2016-04-26 00:32:29 +02:00
|
|
|
if err != nil {
|
2016-08-08 21:04:33 +02:00
|
|
|
b.Fatalf("NewHashFromStr: unexpected error: %v", err)
|
2016-04-26 00:32:29 +02:00
|
|
|
}
|
|
|
|
m.AddInvVect(NewInvVect(InvTypeBlock, hash))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize it so the bytes are available to test the decode below.
|
|
|
|
var bb bytes.Buffer
|
2016-10-19 01:21:48 +02:00
|
|
|
if err := m.BtcEncode(&bb, pver, LatestEncoding); err != nil {
|
2016-04-26 00:32:29 +02:00
|
|
|
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)
|
2016-10-19 01:21:48 +02:00
|
|
|
msg.BtcDecode(r, pver, LatestEncoding)
|
2016-04-26 00:32:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2016-08-08 21:04:33 +02:00
|
|
|
hash, err := chainhash.NewHashFromStr(fmt.Sprintf("%x", 10000))
|
2016-04-26 00:32:29 +02:00
|
|
|
if err != nil {
|
2016-08-08 21:04:33 +02:00
|
|
|
b.Fatalf("NewHashFromStr: unexpected error: %v", err)
|
2016-04-26 00:32:29 +02:00
|
|
|
}
|
2016-08-29 00:51:55 +02:00
|
|
|
m.Header = *NewBlockHeader(1, hash, hash, 0, uint32(10000))
|
2016-04-26 00:32:29 +02:00
|
|
|
for i := 0; i < 105; i++ {
|
2016-08-08 21:04:33 +02:00
|
|
|
hash, err := chainhash.NewHashFromStr(fmt.Sprintf("%x", i))
|
2016-04-26 00:32:29 +02:00
|
|
|
if err != nil {
|
2016-08-08 21:04:33 +02:00
|
|
|
b.Fatalf("NewHashFromStr: unexpected error: %v", err)
|
2016-04-26 00:32:29 +02:00
|
|
|
}
|
|
|
|
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
|
2016-10-19 01:21:48 +02:00
|
|
|
if err := m.BtcEncode(&bb, pver, LatestEncoding); err != nil {
|
2016-04-26 00:32:29 +02:00
|
|
|
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)
|
2016-10-19 01:21:48 +02:00
|
|
|
msg.BtcDecode(r, pver, LatestEncoding)
|
2016-04-26 00:32:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-08 21:04:33 +02:00
|
|
|
// BenchmarkTxHash performs a benchmark on how long it takes to hash a
|
2014-03-20 16:46:03 +01:00
|
|
|
// transaction.
|
2016-08-08 21:04:33 +02:00
|
|
|
func BenchmarkTxHash(b *testing.B) {
|
2014-03-20 16:46:03 +01:00
|
|
|
for i := 0; i < b.N; i++ {
|
2016-08-08 21:04:33 +02:00
|
|
|
genesisCoinbaseTx.TxHash()
|
2014-03-20 16:46:03 +01:00
|
|
|
}
|
|
|
|
}
|
2015-04-04 20:25:49 +02:00
|
|
|
|
2016-08-08 21:04:33 +02:00
|
|
|
// BenchmarkDoubleHashB performs a benchmark on how long it takes to perform a
|
|
|
|
// double hash returning a byte slice.
|
|
|
|
func BenchmarkDoubleHashB(b *testing.B) {
|
2015-04-04 20:25:49 +02:00
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := genesisCoinbaseTx.Serialize(&buf); err != nil {
|
|
|
|
b.Errorf("Serialize: unexpected error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
txBytes := buf.Bytes()
|
|
|
|
|
2016-04-26 00:32:29 +02:00
|
|
|
b.ResetTimer()
|
2015-04-04 20:25:49 +02:00
|
|
|
for i := 0; i < b.N; i++ {
|
2016-08-08 21:04:33 +02:00
|
|
|
_ = chainhash.DoubleHashB(txBytes)
|
2015-04-04 20:25:49 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-04 22:30:10 +02:00
|
|
|
|
2016-08-08 21:04:33 +02:00
|
|
|
// BenchmarkDoubleHashH performs a benchmark on how long it takes to perform
|
|
|
|
// a double hash returning a chainhash.Hash.
|
|
|
|
func BenchmarkDoubleHashH(b *testing.B) {
|
2015-04-04 22:30:10 +02:00
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := genesisCoinbaseTx.Serialize(&buf); err != nil {
|
|
|
|
b.Errorf("Serialize: unexpected error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
txBytes := buf.Bytes()
|
|
|
|
|
2016-04-26 00:32:29 +02:00
|
|
|
b.ResetTimer()
|
2015-04-04 22:30:10 +02:00
|
|
|
for i := 0; i < b.N; i++ {
|
2016-08-08 21:04:33 +02:00
|
|
|
_ = chainhash.DoubleHashH(txBytes)
|
2015-04-04 22:30:10 +02:00
|
|
|
}
|
|
|
|
}
|