func (tx *Transaction) SerializedSize() int { // Version(4) + LockTime(4) + Varint(len(In)) + Reduce(In, Size(TxIn)) + Reduce(Out, Size(TxOut)) return 8 + varint.Size(uint64(len(tx.In))) + ReduceTotalSerializedSize(iterator.New(tx.In)) + varint.Size(uint64(len(tx.Out))) + ReduceTotalSerializedSize(iterator.New(tx.Out)) }
func TestSize(t *testing.T) { tests := []struct { val uint64 // Value to get the serialized size for size int // Expected serialized size }{ // Single byte {0, 1}, // Max single byte {0xfc, 1}, // Min 2-byte {0xfd, 3}, // Max 2-byte {0xffff, 3}, // Min 4-byte {0x10000, 5}, // Max 4-byte {0xffffffff, 5}, // Min 8-byte {0x100000000, 9}, // Max 8-byte {0xffffffffffffffff, 9}, } for i, test := range tests { serializedSize := varint.Size(test.val) if serializedSize != test.size { t.Errorf("Size #%d got: %d, want: %d", i, serializedSize, test.size) continue } } }
func (tx *TxOut) SerializedSize() int { // Value(8) + Varint(len(PKScript)) return 8 + varint.Size(uint64(len(tx.PKScript))) }
func (tx *TxIn) SerializedSize() int { return tx.PreviousTxOut.SerializedSize() + varint.Size(uint64(len(tx.ScriptSignature))) + len(tx.ScriptSignature) + varint.Size(uint64(tx.Sequence)) }