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.
This commit is contained in:
parent
1623818c12
commit
b9c21bd518
4 changed files with 8 additions and 14 deletions
|
@ -325,9 +325,9 @@ func writeVarInt(w io.Writer, pver uint32, val uint64) error {
|
||||||
return err
|
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.
|
// 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
|
// The value is small enough to be represented by itself, so it's
|
||||||
// just 1 byte.
|
// just 1 byte.
|
||||||
if val < 0xfd {
|
if val < 0xfd {
|
||||||
|
|
|
@ -352,9 +352,9 @@ func TestVarIntSerializeSize(t *testing.T) {
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
t.Logf("Running %d tests", len(tests))
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
serializedSize := btcwire.TstVarIntSerializeSize(test.val)
|
serializedSize := btcwire.VarIntSerializeSize(test.val)
|
||||||
if serializedSize != test.size {
|
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)
|
serializedSize, test.size)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,12 +53,6 @@ func TstWriteVarInt(w io.Writer, pver uint32, val uint64) error {
|
||||||
return writeVarInt(w, pver, val)
|
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
|
// TstReadVarString makes the internal readVarString function available to the
|
||||||
// test package.
|
// test package.
|
||||||
func TstReadVarString(r io.Reader, pver uint32) (string, error) {
|
func TstReadVarString(r io.Reader, pver uint32) (string, error) {
|
||||||
|
|
8
msgtx.go
8
msgtx.go
|
@ -88,7 +88,7 @@ func (t *TxIn) SerializeSize() int {
|
||||||
// Outpoint Hash 32 bytes + Outpoint Index 4 bytes + Sequence 4 bytes +
|
// Outpoint Hash 32 bytes + Outpoint Index 4 bytes + Sequence 4 bytes +
|
||||||
// serialized varint size for the length of SignatureScript +
|
// serialized varint size for the length of SignatureScript +
|
||||||
// SignatureScript bytes.
|
// SignatureScript bytes.
|
||||||
return 40 + varIntSerializeSize(uint64(len(t.SignatureScript))) +
|
return 40 + VarIntSerializeSize(uint64(len(t.SignatureScript))) +
|
||||||
len(t.SignatureScript)
|
len(t.SignatureScript)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,7 +114,7 @@ type TxOut struct {
|
||||||
func (t *TxOut) SerializeSize() int {
|
func (t *TxOut) SerializeSize() int {
|
||||||
// Value 8 bytes + serialized varint size for the length of PkScript +
|
// Value 8 bytes + serialized varint size for the length of PkScript +
|
||||||
// PkScript bytes.
|
// 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
|
// 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 {
|
func (msg *MsgTx) SerializeSize() int {
|
||||||
// Version 4 bytes + LockTime 4 bytes + Serialized varint size for the
|
// Version 4 bytes + LockTime 4 bytes + Serialized varint size for the
|
||||||
// number of transaction inputs and outputs.
|
// number of transaction inputs and outputs.
|
||||||
n := 8 + varIntSerializeSize(uint64(len(msg.TxIn))) +
|
n := 8 + VarIntSerializeSize(uint64(len(msg.TxIn))) +
|
||||||
varIntSerializeSize(uint64(len(msg.TxOut)))
|
VarIntSerializeSize(uint64(len(msg.TxOut)))
|
||||||
|
|
||||||
for _, txIn := range msg.TxIn {
|
for _, txIn := range msg.TxIn {
|
||||||
n += txIn.SerializeSize()
|
n += txIn.SerializeSize()
|
||||||
|
|
Loading…
Reference in a new issue