-
Notifications
You must be signed in to change notification settings - Fork 3
/
sizeest.go
91 lines (81 loc) · 3.16 KB
/
sizeest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) 2016 The btcsuite developers
// Copyright (c) 2016-2017 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"github.com/ltcsuite/ltcd/txscript"
"github.com/ltcsuite/ltcd/wire"
)
// Worst case script and input/output size estimates.
const (
// redeemAtomicSwapSigScriptSize is the worst case (largest) serialize size
// of a transaction input script to redeem the atomic swap contract. This
// does not include final push for the contract itself.
//
// - OP_DATA_73
// - 72 bytes DER signature + 1 byte sighash
// - OP_DATA_33
// - 33 bytes serialized compressed pubkey
// - OP_DATA_32
// - 32 bytes secret
// - OP_TRUE
redeemAtomicSwapSigScriptSize = 1 + 73 + 1 + 33 + 1 + 32 + 1
// refundAtomicSwapSigScriptSize is the worst case (largest) serialize size
// of a transaction input script that refunds a P2SH atomic swap output.
// This does not include final push for the contract itself.
//
// - OP_DATA_73
// - 72 bytes DER signature + 1 byte sighash
// - OP_DATA_33
// - 33 bytes serialized compressed pubkey
// - OP_FALSE
refundAtomicSwapSigScriptSize = 1 + 73 + 1 + 33 + 1
)
func sumOutputSerializeSizes(outputs []*wire.TxOut) (serializeSize int) {
for _, txOut := range outputs {
serializeSize += txOut.SerializeSize()
}
return serializeSize
}
// inputSize returns the size of the transaction input needed to include a
// signature script with size sigScriptSize. It is calculated as:
//
// - 32 bytes previous tx
// - 4 bytes output index
// - Compact int encoding sigScriptSize
// - sigScriptSize bytes signature script
// - 4 bytes sequence
func inputSize(sigScriptSize int) int {
return 32 + 4 + wire.VarIntSerializeSize(uint64(sigScriptSize)) + sigScriptSize + 4
}
// estimateRedeemSerializeSize returns a worst case serialize size estimates for
// a transaction that redeems an atomic swap P2SH output.
func estimateRedeemSerializeSize(contract []byte, txOuts []*wire.TxOut) int {
contractPush, err := txscript.NewScriptBuilder().AddData(contract).Script()
if err != nil {
// Should never be hit since this script does exceed the limits.
panic(err)
}
contractPushSize := len(contractPush)
// 12 additional bytes are for version, locktime and expiry.
return 12 + wire.VarIntSerializeSize(1) +
wire.VarIntSerializeSize(uint64(len(txOuts))) +
inputSize(redeemAtomicSwapSigScriptSize+contractPushSize) +
sumOutputSerializeSizes(txOuts)
}
// estimateRefundSerializeSize returns a worst case serialize size estimates for
// a transaction that refunds an atomic swap P2SH output.
func estimateRefundSerializeSize(contract []byte, txOuts []*wire.TxOut) int {
contractPush, err := txscript.NewScriptBuilder().AddData(contract).Script()
if err != nil {
// Should never be hit since this script does exceed the limits.
panic(err)
}
contractPushSize := len(contractPush)
// 12 additional bytes are for version, locktime and expiry.
return 12 + wire.VarIntSerializeSize(1) +
wire.VarIntSerializeSize(uint64(len(txOuts))) +
inputSize(refundAtomicSwapSigScriptSize+contractPushSize) +
sumOutputSerializeSizes(txOuts)
}