-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rules.go
executable file
·118 lines (85 loc) · 3.33 KB
/
rules.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (c) 2016 The btcsuite developers
// Package txrules provides transaction rules that should be followed by
// transaction authors for wide mempool acceptance and quick mining.
package txrules
import (
"errors"
txscript "git.parallelcoin.io/dev/9/pkg/chain/tx/script"
"git.parallelcoin.io/dev/9/pkg/chain/wire"
"git.parallelcoin.io/dev/9/pkg/util"
)
// DefaultRelayFeePerKb is the default minimum relay fee policy for a mempool.
const DefaultRelayFeePerKb util.Amount = 1e3
// GetDustThreshold is used to define the amount below which output will be
// determined as dust. Threshold is determined as 3 times the relay fee.
func GetDustThreshold(
scriptSize int, relayFeePerKb util.Amount) util.Amount {
// Calculate the total (estimated) cost to the network. This is
// calculated using the serialize size of the output plus the serial
// size of a transaction input which redeems it. The output is assumed
// to be compressed P2PKH as this is the most common script type. Use
// the average size of a compressed P2PKH redeem input (148) rather than
// the largest possible (txsizes.RedeemP2PKHInputSize).
totalSize := 8 + wire.VarIntSerializeSize(uint64(scriptSize)) +
scriptSize + 148
byteFee := relayFeePerKb / 1000
relayFee := util.Amount(totalSize) * byteFee
return 3 * relayFee
}
// IsDustAmount determines whether a transaction output value and script length would
// cause the output to be considered dust. Transactions with dust outputs are
// not standard and are rejected by mempools with default policies.
func IsDustAmount(
amount util.Amount, scriptSize int, relayFeePerKb util.Amount) bool {
return amount < GetDustThreshold(scriptSize, relayFeePerKb)
}
// IsDustOutput determines whether a transaction output is considered dust.
// Transactions with dust outputs are not standard and are rejected by mempools
// with default policies.
func IsDustOutput(
output *wire.TxOut, relayFeePerKb util.Amount) bool {
// Unspendable outputs which solely carry data are not checked for dust.
if txscript.GetScriptClass(output.PkScript) == txscript.NullDataTy {
return false
}
// All other unspendable outputs are considered dust.
if txscript.IsUnspendable(output.PkScript) {
return true
}
return IsDustAmount(util.Amount(output.Value), len(output.PkScript),
relayFeePerKb)
}
// Transaction rule violations
var (
ErrAmountNegative = errors.New("transaction output amount is negative")
ErrAmountExceedsMax = errors.New("transaction output amount exceeds maximum value")
ErrOutputIsDust = errors.New("transaction output is dust")
)
// CheckOutput performs simple consensus and policy tests on a transaction
// output.
func CheckOutput(
output *wire.TxOut, relayFeePerKb util.Amount) error {
if output.Value < 0 {
return ErrAmountNegative
}
if output.Value > util.MaxSatoshi {
return ErrAmountExceedsMax
}
if IsDustOutput(output, relayFeePerKb) {
return ErrOutputIsDust
}
return nil
}
// FeeForSerializeSize calculates the required fee for a transaction of some
// arbitrary size given a mempool's relay fee policy.
func FeeForSerializeSize(
relayFeePerKb util.Amount, txSerializeSize int) util.Amount {
fee := relayFeePerKb * util.Amount(txSerializeSize) / 1000
if fee == 0 && relayFeePerKb > 0 {
fee = relayFeePerKb
}
if fee < 0 || fee > util.MaxSatoshi {
fee = util.MaxSatoshi
}
return fee
}