From b9c21bd5186a3ba654eafce7315419ba1cdd2ed0 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Mon, 3 Mar 2014 18:43:09 -0600 Subject: [PATCH] Export VarIntSerializeSize function. This commit exports the VarIntSerializeSize function to provide callers with an easy method to determine how many bytes it would take to serialize the passed value as a variable length integer. --- common.go | 4 ++-- common_test.go | 4 ++-- internal_test.go | 6 ------ msgtx.go | 8 ++++---- 4 files changed, 8 insertions(+), 14 deletions(-) diff --git a/common.go b/common.go index ef47f7ac..375bff0a 100644 --- a/common.go +++ b/common.go @@ -325,9 +325,9 @@ func writeVarInt(w io.Writer, pver uint32, val uint64) error { return err } -// varIntSerializeSize returns the number of bytes it would take to serialize +// VarIntSerializeSize returns the number of bytes it would take to serialize // val as a variable length integer. -func varIntSerializeSize(val uint64) int { +func VarIntSerializeSize(val uint64) int { // The value is small enough to be represented by itself, so it's // just 1 byte. if val < 0xfd { diff --git a/common_test.go b/common_test.go index f75ad8c6..d27f41a3 100644 --- a/common_test.go +++ b/common_test.go @@ -352,9 +352,9 @@ func TestVarIntSerializeSize(t *testing.T) { t.Logf("Running %d tests", len(tests)) for i, test := range tests { - serializedSize := btcwire.TstVarIntSerializeSize(test.val) + serializedSize := btcwire.VarIntSerializeSize(test.val) if serializedSize != test.size { - t.Errorf("varIntSerializeSize #%d got: %d, want: %d", i, + t.Errorf("VarIntSerializeSize #%d got: %d, want: %d", i, serializedSize, test.size) continue } diff --git a/internal_test.go b/internal_test.go index c2fce9c5..2bb0c380 100644 --- a/internal_test.go +++ b/internal_test.go @@ -53,12 +53,6 @@ func TstWriteVarInt(w io.Writer, pver uint32, val uint64) error { return writeVarInt(w, pver, val) } -// TstVarIntSerializeSize makes the internal varIntSerializeSize function -// available to the test package. -func TstVarIntSerializeSize(val uint64) int { - return varIntSerializeSize(val) -} - // TstReadVarString makes the internal readVarString function available to the // test package. func TstReadVarString(r io.Reader, pver uint32) (string, error) { diff --git a/msgtx.go b/msgtx.go index dc6f59e0..f7cd87d0 100644 --- a/msgtx.go +++ b/msgtx.go @@ -88,7 +88,7 @@ func (t *TxIn) SerializeSize() int { // Outpoint Hash 32 bytes + Outpoint Index 4 bytes + Sequence 4 bytes + // serialized varint size for the length of SignatureScript + // SignatureScript bytes. - return 40 + varIntSerializeSize(uint64(len(t.SignatureScript))) + + return 40 + VarIntSerializeSize(uint64(len(t.SignatureScript))) + len(t.SignatureScript) } @@ -114,7 +114,7 @@ type TxOut struct { func (t *TxOut) SerializeSize() int { // Value 8 bytes + serialized varint size for the length of PkScript + // PkScript bytes. - return 8 + varIntSerializeSize(uint64(len(t.PkScript))) + len(t.PkScript) + return 8 + VarIntSerializeSize(uint64(len(t.PkScript))) + len(t.PkScript) } // NewTxOut returns a new bitcoin transaction output with the provided @@ -388,8 +388,8 @@ func (msg *MsgTx) Serialize(w io.Writer) error { func (msg *MsgTx) SerializeSize() int { // Version 4 bytes + LockTime 4 bytes + Serialized varint size for the // number of transaction inputs and outputs. - n := 8 + varIntSerializeSize(uint64(len(msg.TxIn))) + - varIntSerializeSize(uint64(len(msg.TxOut))) + n := 8 + VarIntSerializeSize(uint64(len(msg.TxIn))) + + VarIntSerializeSize(uint64(len(msg.TxOut))) for _, txIn := range msg.TxIn { n += txIn.SerializeSize()