-
Notifications
You must be signed in to change notification settings - Fork 10
/
default_validator.go
296 lines (243 loc) · 9.23 KB
/
default_validator.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package defaultvalidator
import (
"encoding/hex"
"fmt"
"math"
"github.com/bitcoin-sv/arc/api"
"github.com/bitcoin-sv/arc/validator"
"github.com/libsv/go-bt/v2"
"github.com/libsv/go-bt/v2/bscript"
"github.com/libsv/go-bt/v2/bscript/interpreter"
"github.com/ordishs/go-bitcoin"
)
// MaxBlockSize is set dynamically in a node, and should be gotten from the policy
const MaxBlockSize = 4 * 1024 * 1024 * 1024
const MaxSatoshis = 21_000_000_00_000_000
const coinbaseTxID = "0000000000000000000000000000000000000000000000000000000000000000"
const MaxTxSigopsCountPolicyAfterGenesis = ^uint32(0) // UINT32_MAX
type DefaultValidator struct {
policy *bitcoin.Settings
}
func New(policy *bitcoin.Settings) validator.Validator {
return &DefaultValidator{
policy: policy,
}
}
func (v *DefaultValidator) ValidateTransaction(tx *bt.Tx, skipFeeValidation bool, skipScriptValidation bool) error { //nolint:funlen - mostly comments
//
// Each node will verify every transaction against a long checklist of criteria:
//
txSize := tx.Size()
// fmt.Println(hex.EncodeToString(tx.ExtendedBytes()))
// 0) Check whether we have a complete transaction in extended format, with all input information
// we cannot check the satoshi input, OP_RETURN is allowed 0 satoshis
if !v.IsExtended(tx) {
return validator.NewError(fmt.Errorf("transaction is not in extended format"), api.ErrStatusTxFormat)
}
// 1) Neither lists of inputs or outputs are empty
if len(tx.Inputs) == 0 || len(tx.Outputs) == 0 {
return validator.NewError(fmt.Errorf("transaction has no inputs or outputs"), api.ErrStatusInputs)
}
// 2) The transaction size in bytes is less than maxtxsizepolicy.
if err := checkTxSize(txSize, v.policy); err != nil {
return validator.NewError(err, api.ErrStatusTxFormat)
}
// 3) check that each input value, as well as the sum, are in the allowed range of values (less than 21m coins)
// 5) None of the inputs have hash=0, N=–1 (coinbase transactions should not be relayed)
if err := checkInputs(tx); err != nil {
return validator.NewError(err, api.ErrStatusInputs)
}
// 4) Each output value, as well as the total, must be within the allowed range of values (less than 21m coins,
// more than the dust threshold if 1 unless it's OP_RETURN, which is allowed to be 0)
if err := checkOutputs(tx); err != nil {
return validator.NewError(err, api.ErrStatusOutputs)
}
// 6) nLocktime is equal to INT_MAX, or nLocktime and nSequence values are satisfied according to MedianTimePast
// => checked by the node, we do not want to have to know the current block height
// 7) The transaction size in bytes is greater than or equal to 100
if txSize < 100 {
return validator.NewError(fmt.Errorf("transaction size in bytes is less than 100 bytes"), api.ErrStatusMalformed)
}
// 8) The number of signature operations (SIGOPS) contained in the transaction is less than the signature operation limit
if err := sigOpsCheck(tx, v.policy); err != nil {
return validator.NewError(err, api.ErrStatusMalformed)
}
// 9) The unlocking script (scriptSig) can only push numbers on the stack
if err := pushDataCheck(tx); err != nil {
return validator.NewError(err, api.ErrStatusMalformed)
}
// 10) Reject if the sum of input values is less than sum of output values
// 11) Reject if transaction fee would be too low (minRelayTxFee) to get into an empty block.
if !skipFeeValidation {
if err := checkFees(tx, api.FeesToBtFeeQuote(v.policy.MinMiningTxFee)); err != nil {
return validator.NewError(err, api.ErrStatusFees)
}
}
// 12) The unlocking scripts for each input must validate against the corresponding output locking scripts
if !skipScriptValidation {
if err := checkScripts(tx); err != nil {
return validator.NewError(err, api.ErrStatusUnlockingScripts)
}
}
// everything checks out
return nil
}
func (v *DefaultValidator) IsExtended(tx *bt.Tx) bool {
if tx == nil || tx.Inputs == nil {
return false
}
for _, input := range tx.Inputs {
if input.PreviousTxScript == nil || (input.PreviousTxSatoshis == 0 && !input.PreviousTxScript.IsData()) {
return false
}
}
return true
}
func checkTxSize(txSize int, policy *bitcoin.Settings) error {
maxTxSizePolicy := policy.MaxTxSizePolicy
if maxTxSizePolicy == 0 {
// no policy found for tx size, use max block size
maxTxSizePolicy = MaxBlockSize
}
if txSize > maxTxSizePolicy {
return fmt.Errorf("transaction size in bytes is greater than max tx size policy %d", maxTxSizePolicy)
}
return nil
}
func checkOutputs(tx *bt.Tx) error {
total := uint64(0)
for index, output := range tx.Outputs {
isData := output.LockingScript.IsData()
switch {
case !isData && (output.Satoshis > MaxSatoshis || output.Satoshis < bt.DustLimit):
return validator.NewError(fmt.Errorf("transaction output %d satoshis is invalid", index), api.ErrStatusOutputs)
case isData && output.Satoshis != 0:
return validator.NewError(fmt.Errorf("transaction output %d has non 0 value op return", index), api.ErrStatusOutputs)
}
total += output.Satoshis
}
if total > MaxSatoshis {
return validator.NewError(fmt.Errorf("transaction output total satoshis is too high"), api.ErrStatusOutputs)
}
return nil
}
func checkInputs(tx *bt.Tx) error {
total := uint64(0)
for index, input := range tx.Inputs {
if hex.EncodeToString(input.PreviousTxID()) == coinbaseTxID {
return validator.NewError(fmt.Errorf("transaction input %d is a coinbase input", index), api.ErrStatusInputs)
}
/* lots of our valid test transactions have this sequence number, is this not allowed?
if input.SequenceNumber == 0xffffffff {
fmt.Printf("input %d has sequence number 0xffffffff, txid = %s", index, tx.TxID())
return validator.NewError(fmt.Errorf("transaction input %d sequence number is invalid", index), arc.ErrStatusInputs)
}
*/
if input.PreviousTxSatoshis > MaxSatoshis {
return validator.NewError(fmt.Errorf("transaction input %d satoshis is too high", index), api.ErrStatusInputs)
}
total += input.PreviousTxSatoshis
}
if total > MaxSatoshis {
return validator.NewError(fmt.Errorf("transaction input total satoshis is too high"), api.ErrStatusInputs)
}
return nil
}
func checkFees(tx *bt.Tx, feeQuote *bt.FeeQuote) error {
feesOK, expFeesPaid, actualFeePaid, err := isFeePaidEnough(feeQuote, tx)
if err != nil {
return err
}
if !feesOK {
return fmt.Errorf("transaction fee of %d sat is too low - minimum expected fee is %d sat", actualFeePaid, expFeesPaid)
}
return nil
}
func isFeePaidEnough(fees *bt.FeeQuote, tx *bt.Tx) (bool, uint64, uint64, error) {
expFeesPaid, err := calculateMiningFeesRequired(tx.SizeWithTypes(), fees)
if err != nil {
return false, 0, 0, err
}
totalInputSatoshis := tx.TotalInputSatoshis()
totalOutputSatoshis := tx.TotalOutputSatoshis()
if totalInputSatoshis < totalOutputSatoshis {
return false, 0, 0, nil
}
actualFeePaid := totalInputSatoshis - totalOutputSatoshis
return actualFeePaid >= expFeesPaid, expFeesPaid, actualFeePaid, nil
}
func calculateMiningFeesRequired(size *bt.TxSize, fees *bt.FeeQuote) (uint64, error) {
var feesRequired float64
feeStandard, err := fees.Fee(bt.FeeTypeStandard)
if err != nil {
return 0, err
}
feesRequired += float64(size.TotalStdBytes) * float64(feeStandard.MiningFee.Satoshis) / float64(feeStandard.MiningFee.Bytes)
feeData, err := fees.Fee(bt.FeeTypeData)
if err != nil {
return 0, err
}
feesRequired += float64(size.TotalDataBytes) * float64(feeData.MiningFee.Satoshis) / float64(feeData.MiningFee.Bytes)
// the minimum fees required is 1 satoshi
feesRequiredRounded := uint64(math.Round(feesRequired))
if feesRequiredRounded < 1 {
feesRequiredRounded = 1
}
return feesRequiredRounded, nil
}
func sigOpsCheck(tx *bt.Tx, policy *bitcoin.Settings) error {
maxSigOps := policy.MaxTxSigopsCountsPolicy
if maxSigOps == 0 {
maxSigOps = int64(MaxTxSigopsCountPolicyAfterGenesis)
}
numSigOps := int64(0)
for _, input := range tx.Inputs {
parser := interpreter.DefaultOpcodeParser{}
parsedUnlockingScript, err := parser.Parse(input.PreviousTxScript)
if err != nil {
return err
}
for _, op := range parsedUnlockingScript {
if op.Value() == bscript.OpCHECKSIG || op.Value() == bscript.OpCHECKSIGVERIFY {
numSigOps++
if numSigOps > maxSigOps {
return fmt.Errorf("transaction unlocking scripts have too many sigops (%d)", numSigOps)
}
}
}
}
return nil
}
func pushDataCheck(tx *bt.Tx) error {
for index, input := range tx.Inputs {
if input.UnlockingScript == nil {
return fmt.Errorf("transaction input %d unlocking script is empty", index)
}
parser := interpreter.DefaultOpcodeParser{}
parsedUnlockingScript, err := parser.Parse(input.UnlockingScript)
if err != nil {
return err
}
if !parsedUnlockingScript.IsPushOnly() {
return fmt.Errorf("transaction input %d unlocking script is not push only", index)
}
}
return nil
}
func checkScripts(tx *bt.Tx) error {
for i, in := range tx.Inputs {
prevOutput := &bt.Output{
Satoshis: in.PreviousTxSatoshis,
LockingScript: in.PreviousTxScript,
}
if err := interpreter.NewEngine().Execute(
interpreter.WithTx(tx, i, prevOutput),
interpreter.WithForkID(),
interpreter.WithAfterGenesis(),
// interpreter.WithDebugger(&LogDebugger{}),
); err != nil {
return fmt.Errorf("script execution failed: %w", err)
}
}
return nil
}