Make use of new btcwire SerializeSize API.
This commit changes the various cases that were serializing transactions into a buffer and taking the length to use the new faster SerializeSize API. It also completes a TODO since the serialized size of a transaction output is now available.
This commit is contained in:
parent
470ef8c58e
commit
a4794798d4
2 changed files with 6 additions and 26 deletions
25
mempool.go
25
mempool.go
|
@ -5,7 +5,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"container/list"
|
"container/list"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -89,14 +88,6 @@ type txMemPool struct {
|
||||||
// relay fee. In particular, if the cost to the network to spend coins is more
|
// relay fee. In particular, if the cost to the network to spend coins is more
|
||||||
// than 1/3 of the minimum transaction relay fee, it is considered dust.
|
// than 1/3 of the minimum transaction relay fee, it is considered dust.
|
||||||
func isDust(txOut *btcwire.TxOut) bool {
|
func isDust(txOut *btcwire.TxOut) bool {
|
||||||
// Get the serialized size of the transaction output.
|
|
||||||
//
|
|
||||||
// TODO(davec): The serialized size should come from btcwire, but it
|
|
||||||
// currently doesn't provide a way to do so for transaction outputs, so
|
|
||||||
// calculate it here based on the current format.
|
|
||||||
// 8 bytes for value + 1 byte for script length + script length
|
|
||||||
txOutSize := 9 + len(txOut.PkScript)
|
|
||||||
|
|
||||||
// The total serialized size consists of the output and the associated
|
// The total serialized size consists of the output and the associated
|
||||||
// input script to redeem it. Since there is no input script
|
// input script to redeem it. Since there is no input script
|
||||||
// to redeem it yet, use the minimum size of a typical input script.
|
// to redeem it yet, use the minimum size of a typical input script.
|
||||||
|
@ -139,7 +130,7 @@ func isDust(txOut *btcwire.TxOut) bool {
|
||||||
// The most common scripts are pay-to-pubkey-hash, and as per the above
|
// The most common scripts are pay-to-pubkey-hash, and as per the above
|
||||||
// breakdown, the minimum size of a p2pkh input script is 148 bytes. So
|
// breakdown, the minimum size of a p2pkh input script is 148 bytes. So
|
||||||
// that figure is used.
|
// that figure is used.
|
||||||
totalSize := txOutSize + 148
|
totalSize := txOut.SerializeSize() + 148
|
||||||
|
|
||||||
// The output is considered dust if the cost to the network to spend the
|
// The output is considered dust if the cost to the network to spend the
|
||||||
// coins is more than 1/3 of the minimum transaction relay fee.
|
// coins is more than 1/3 of the minimum transaction relay fee.
|
||||||
|
@ -214,12 +205,7 @@ func checkTransactionStandard(tx *btcutil.Tx, height int64) error {
|
||||||
// almost as much to process as the sender fees, limit the maximum
|
// almost as much to process as the sender fees, limit the maximum
|
||||||
// size of a transaction. This also helps mitigate CPU exhaustion
|
// size of a transaction. This also helps mitigate CPU exhaustion
|
||||||
// attacks.
|
// attacks.
|
||||||
var serializedTxBuf bytes.Buffer
|
serializedLen := msgTx.SerializeSize()
|
||||||
err := msgTx.Serialize(&serializedTxBuf)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
serializedLen := serializedTxBuf.Len()
|
|
||||||
if serializedLen > maxStandardTxSize {
|
if serializedLen > maxStandardTxSize {
|
||||||
str := fmt.Sprintf("transaction size of %v is larger than max "+
|
str := fmt.Sprintf("transaction size of %v is larger than max "+
|
||||||
"allowed size of %v", serializedLen, maxStandardTxSize)
|
"allowed size of %v", serializedLen, maxStandardTxSize)
|
||||||
|
@ -384,12 +370,7 @@ func (mp *txMemPool) maybeAddOrphan(tx *btcutil.Tx) error {
|
||||||
// also limited, so this equates to a maximum memory used of
|
// also limited, so this equates to a maximum memory used of
|
||||||
// maxOrphanTxSize * maxOrphanTransactions (which is 500MB as of the
|
// maxOrphanTxSize * maxOrphanTransactions (which is 500MB as of the
|
||||||
// time this comment was written).
|
// time this comment was written).
|
||||||
var serializedTxBuf bytes.Buffer
|
serializedLen := tx.MsgTx().SerializeSize()
|
||||||
err := tx.MsgTx().Serialize(&serializedTxBuf)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
serializedLen := serializedTxBuf.Len()
|
|
||||||
if serializedLen > maxOrphanTxSize {
|
if serializedLen > maxOrphanTxSize {
|
||||||
str := fmt.Sprintf("orphan transaction size of %d bytes is "+
|
str := fmt.Sprintf("orphan transaction size of %d bytes is "+
|
||||||
"larger than max allowed size of %d bytes",
|
"larger than max allowed size of %d bytes",
|
||||||
|
|
|
@ -603,6 +603,7 @@ func handleSendRawTransaction(s *rpcServer, cmd btcjson.Cmd, walletNotification
|
||||||
}
|
}
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
tx := btcutil.NewTx(msgtx)
|
tx := btcutil.NewTx(msgtx)
|
||||||
err = s.server.txMemPool.ProcessTransaction(tx)
|
err = s.server.txMemPool.ProcessTransaction(tx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -624,15 +625,13 @@ func handleSendRawTransaction(s *rpcServer, cmd btcjson.Cmd, walletNotification
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
txsha := tx.Sha()
|
|
||||||
|
|
||||||
// If called from websocket code, add a mined tx hashes
|
// If called from websocket code, add a mined tx hashes
|
||||||
// request.
|
// request.
|
||||||
if walletNotification != nil {
|
if walletNotification != nil {
|
||||||
s.ws.requests.AddMinedTxRequest(walletNotification, txsha)
|
s.ws.requests.AddMinedTxRequest(walletNotification, tx.Sha())
|
||||||
}
|
}
|
||||||
|
|
||||||
return txsha.String(), nil
|
return tx.Sha().String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// handleSetGenerate implements the setgenerate command.
|
// handleSetGenerate implements the setgenerate command.
|
||||||
|
|
Loading…
Add table
Reference in a new issue