forked from decred/dcrd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
staketx.go
1189 lines (1029 loc) · 40.1 KB
/
staketx.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2015-2016 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
//
// Contains a collection of functions that determine what type of stake tx a
// given tx is and does a cursory check for sanity.
package stake
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"math"
"math/big"
"github.com/decred/dcrd/chaincfg"
"github.com/decred/dcrd/chaincfg/chainec"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/txscript"
"github.com/decred/dcrd/wire"
"github.com/decred/dcrutil"
)
// TxType indicates the type of tx (regular or stake type).
type TxType int
// Possible TxTypes. Statically declare these so that they might be used in
// consensus code.
const (
TxTypeRegular TxType = iota
TxTypeSStx
TxTypeSSGen
TxTypeSSRtx
)
const (
// MaxInputsPerSStx is the maximum number of inputs allowed in an SStx.
MaxInputsPerSStx = 64
// MaxOutputsPerSStx is the maximum number of outputs allowed in an SStx;
// you need +1 for the tagged SStx output.
MaxOutputsPerSStx = MaxInputsPerSStx*2 + 1
// NumInputsPerSSGen is the exact number of inputs for an SSGen
// (stakebase) tx. Inputs are a tagged SStx output and a stakebase (null)
// input.
NumInputsPerSSGen = 2 // SStx and stakebase
// MaxOutputsPerSSGen is the maximum number of outputs in an SSGen tx,
// which are all outputs to the addresses specified in the OP_RETURNs of
// the original SStx referenced as input plus reference and vote
// OP_RETURN outputs in the zeroeth and first position.
MaxOutputsPerSSGen = MaxInputsPerSStx + 2
// NumInputsPerSSRtx is the exact number of inputs for an SSRtx (stake
// revocation tx); the only input should be the SStx output.
NumInputsPerSSRtx = 1
// MaxOutputsPerSSRtx is the maximum number of outputs in an SSRtx, which
// are all outputs to the addresses specified in the OP_RETURNs of the
// original SStx referenced as input plus a reference to the block header
// hash of the block in which voting was missed.
MaxOutputsPerSSRtx = MaxInputsPerSStx
// SStxPKHMinOutSize is the minimum size of of an OP_RETURN commitment output
// for an SStx tx.
// 20 bytes P2SH/P2PKH + 8 byte amount + 4 byte fee range limits
SStxPKHMinOutSize = 32
// SStxPKHMaxOutSize is the maximum size of of an OP_RETURN commitment output
// for an SStx tx.
SStxPKHMaxOutSize = 77
// SSGenBlockReferenceOutSize is the size of a block reference OP_RETURN
// output for an SSGen tx.
SSGenBlockReferenceOutSize = 38
// SSGenVoteBitsOutputMinSize is the minimum size for a VoteBits push
// in an SSGen.
SSGenVoteBitsOutputMinSize = 4
// SSGenVoteBitsOutputMaxSize is the maximum size for a VoteBits push
// in an SSGen.
SSGenVoteBitsOutputMaxSize = 77
// MaxSingleBytePushLength is the largest maximum push for an
// SStx commitment or VoteBits push.
MaxSingleBytePushLength = 75
// SSGenVoteBitsExtendedMaxSize is the maximum size for a VoteBitsExtended
// push in an SSGen.
//
// The final vote transaction includes a single data push for all vote
// bits concatenated. The non-extended vote bits occupy the first 2
// bytes, thus the max number of extended vote bits is the maximum
// allow length for a single byte data push minus the 2 bytes required
// by the non-extended vote bits.
SSGenVoteBitsExtendedMaxSize = MaxSingleBytePushLength - 2
// SStxVoteReturnFractionMask extracts the return fraction from a
// commitment output version.
// If after applying this mask &0x003f is given, the entire amount of
// the output is allowed to be spent as fees if the flag to allow fees
// is set.
SStxVoteReturnFractionMask = 0x003f
// SStxRevReturnFractionMask extracts the return fraction from a
// commitment output version.
// If after applying this mask &0x3f00 is given, the entire amount of
// the output is allowed to be spent as fees if the flag to allow fees
// is set.
SStxRevReturnFractionMask = 0x3f00
// SStxVoteFractionFlag is a bitflag mask specifying whether or not to
// apply a fractional limit to the amount used for fees in a vote.
// 00000000 00000000 = No fees allowed
// 00000000 01000000 = Apply fees rule
SStxVoteFractionFlag = 0x0040
// SStxRevFractionFlag is a bitflag mask specifying whether or not to
// apply a fractional limit to the amount used for fees in a vote.
// 00000000 00000000 = No fees allowed
// 01000000 00000000 = Apply fees rule
SStxRevFractionFlag = 0x4000
// VoteConsensusVersionAbsent is the value of the consensus version
// for a short read of the voteBits.
VoteConsensusVersionAbsent = 0
)
var (
// validSStxAddressOutPrefix is the valid prefix for a 30-byte
// minimum OP_RETURN push for a commitment for an SStx.
// Example SStx address out:
// 0x6a (OP_RETURN)
// 0x1e (OP_DATA_30, push length: 30 bytes)
//
// 0x?? 0x?? 0x?? 0x?? (20 byte public key hash)
// 0x?? 0x?? 0x?? 0x??
// 0x?? 0x?? 0x?? 0x??
// 0x?? 0x?? 0x?? 0x??
// 0x?? 0x??
//
// 0x?? 0x?? 0x?? 0x?? (8 byte amount)
// 0x?? 0x?? 0x?? 0x??
//
// 0x?? 0x?? (2 byte range limits)
validSStxAddressOutMinPrefix = []byte{0x6a, 0x1e}
// validSSGenReferenceOutPrefix is the valid prefix for a block
// reference output for an SSGen tx.
// Example SStx address out:
// 0x6a (OP_RETURN)
// 0x28 (OP_DATA_40, push length: 40 bytes)
//
// 0x?? 0x?? 0x?? 0x?? (32 byte block header hash for the block
// 0x?? 0x?? 0x?? 0x?? you wish to vote on)
// 0x?? 0x?? 0x?? 0x??
// 0x?? 0x?? 0x?? 0x??
// 0x?? 0x?? 0x?? 0x??
// 0x?? 0x?? 0x?? 0x??
// 0x?? 0x?? 0x?? 0x??
// 0x?? 0x?? 0x?? 0x??
//
// 0x?? 0x?? 0x?? 0x?? (4 byte uint32 for the height of the block
// that you wish to vote on)
validSSGenReferenceOutPrefix = []byte{0x6a, 0x24}
// validSSGenVoteOutMinPrefix is the valid prefix for a vote output for an
// SSGen tx.
// 0x6a (OP_RETURN)
// 0x02 (OP_DATA_2 to OP_DATA_75, push length: 2-75 bytes)
//
// 0x?? 0x?? (VoteBits) ... 0x??
validSSGenVoteOutMinPrefix = []byte{0x6a, 0x02}
// zeroHash is the zero value for a chainhash.Hash and is defined as
// a package level variable to avoid the need to create a new instance
// every time a check is needed.
zeroHash = &chainhash.Hash{}
// rangeLimitMax is the maximum bitshift for a fees limit on an
// sstx commitment output.
rangeLimitMax = uint16(63)
)
// VoteBits is a field representing the mandatory 2-byte field of voteBits along
// with the optional 73-byte extended field for votes.
type VoteBits struct {
Bits uint16
ExtendedBits []byte
}
// --------------------------------------------------------------------------------
// Accessory Stake Functions
// --------------------------------------------------------------------------------
// isNullOutpoint determines whether or not a previous transaction output point
// is set.
func isNullOutpoint(tx *wire.MsgTx) bool {
nullInOP := tx.TxIn[0].PreviousOutPoint
if nullInOP.Index == math.MaxUint32 && nullInOP.Hash.IsEqual(zeroHash) &&
nullInOP.Tree == wire.TxTreeRegular {
return true
}
return false
}
// isNullFraudProof determines whether or not a previous transaction fraud proof
// is set.
func isNullFraudProof(tx *wire.MsgTx) bool {
txIn := tx.TxIn[0]
switch {
case txIn.BlockHeight != wire.NullBlockHeight:
return false
case txIn.BlockIndex != wire.NullBlockIndex:
return false
}
return true
}
// IsStakeBase returns whether or not a tx could be considered as having a
// topically valid stake base present.
func IsStakeBase(tx *wire.MsgTx) bool {
// A stake base (SSGen) must only have two transaction inputs.
if len(tx.TxIn) != 2 {
return false
}
// The previous output of a coin base must have a max value index and
// a zero hash, as well as null fraud proofs.
if !isNullOutpoint(tx) {
return false
}
if !isNullFraudProof(tx) {
return false
}
return true
}
// MinimalOutput is a struct encoding a minimally sized output for use in parsing
// stake related information.
type MinimalOutput struct {
PkScript []byte
Value int64
Version uint16
}
// ConvertToMinimalOutputs converts a transaction to its minimal outputs
// derivative.
func ConvertToMinimalOutputs(tx *wire.MsgTx) []*MinimalOutput {
minOuts := make([]*MinimalOutput, len(tx.TxOut))
for i, txOut := range tx.TxOut {
minOuts[i] = &MinimalOutput{
PkScript: txOut.PkScript,
Value: txOut.Value,
Version: txOut.Version,
}
}
return minOuts
}
// SStxStakeOutputInfo takes an SStx as input and scans through its outputs,
// returning the pubkeyhashs and amounts for any NullDataTy's (future
// commitments to stake generation rewards).
func SStxStakeOutputInfo(outs []*MinimalOutput) ([]bool, [][]byte, []int64,
[]int64, [][]bool, [][]uint16) {
expectedInLen := len(outs) / 2
isP2SH := make([]bool, expectedInLen)
addresses := make([][]byte, expectedInLen)
amounts := make([]int64, expectedInLen)
changeAmounts := make([]int64, expectedInLen)
allSpendRules := make([][]bool, expectedInLen)
allSpendLimits := make([][]uint16, expectedInLen)
// Cycle through the inputs and pull the proportional amounts
// and commit to PKHs/SHs.
for idx, out := range outs {
// We only care about the outputs where we get proportional
// amounts and the PKHs/SHs to send rewards to, which is all
// the odd numbered output indexes.
if (idx > 0) && (idx%2 != 0) {
// The MSB (sign), not used ever normally, encodes whether
// or not it is a P2PKH or P2SH for the input.
amtEncoded := make([]byte, 8, 8)
copy(amtEncoded, out.PkScript[22:30])
isP2SH[idx/2] = !(amtEncoded[7]&(1<<7) == 0) // MSB set?
amtEncoded[7] &= ^uint8(1 << 7) // Clear bit
addresses[idx/2] = out.PkScript[2:22]
amounts[idx/2] = int64(binary.LittleEndian.Uint64(amtEncoded))
// Get flags and restrictions for the outputs to be
// make in either a vote or revocation.
spendRules := make([]bool, 2, 2)
spendLimits := make([]uint16, 2, 2)
// This bitflag is true/false.
feeLimitUint16 := binary.LittleEndian.Uint16(out.PkScript[30:32])
spendRules[0] = (feeLimitUint16 & SStxVoteFractionFlag) ==
SStxVoteFractionFlag
spendRules[1] = (feeLimitUint16 & SStxRevFractionFlag) ==
SStxRevFractionFlag
allSpendRules[idx/2] = spendRules
// This is the fraction to use out of 64.
spendLimits[0] = feeLimitUint16 & SStxVoteReturnFractionMask
spendLimits[1] = feeLimitUint16 & SStxRevReturnFractionMask
spendLimits[1] >>= 8
allSpendLimits[idx/2] = spendLimits
}
// Here we only care about the change amounts, so scan
// the change outputs (even indices) and save their
// amounts.
if (idx > 0) && (idx%2 == 0) {
changeAmounts[(idx/2)-1] = out.Value
}
}
return isP2SH, addresses, amounts, changeAmounts, allSpendRules,
allSpendLimits
}
// TxSStxStakeOutputInfo takes an SStx as input and scans through its outputs,
// returning the pubkeyhashs and amounts for any NullDataTy's (future
// commitments to stake generation rewards).
func TxSStxStakeOutputInfo(tx *wire.MsgTx) ([]bool, [][]byte, []int64, []int64,
[][]bool, [][]uint16) {
return SStxStakeOutputInfo(ConvertToMinimalOutputs(tx))
}
// AddrFromSStxPkScrCommitment extracts a P2SH or P2PKH address from a
// ticket commitment pkScript.
func AddrFromSStxPkScrCommitment(pkScript []byte,
params *chaincfg.Params) (dcrutil.Address, error) {
if len(pkScript) < SStxPKHMinOutSize {
return nil, stakeRuleError(ErrSStxBadCommitAmount, "short read "+
"of sstx commit pkscript")
}
// The MSB (sign), not used ever normally, encodes whether
// or not it is a P2PKH or P2SH for the input.
amtEncoded := make([]byte, 8, 8)
copy(amtEncoded, pkScript[22:30])
isP2SH := !(amtEncoded[7]&(1<<7) == 0) // MSB set?
// The 20 byte PKH or SH.
hashBytes := pkScript[2:22]
var err error
var addr dcrutil.Address
if isP2SH {
addr, err = dcrutil.NewAddressScriptHashFromHash(hashBytes, params)
} else {
addr, err = dcrutil.NewAddressPubKeyHash(hashBytes, params,
chainec.ECTypeSecp256k1)
}
return addr, err
}
// AmountFromSStxPkScrCommitment extracts a commitment amount from a
// ticket commitment pkScript.
func AmountFromSStxPkScrCommitment(pkScript []byte) (dcrutil.Amount, error) {
if len(pkScript) < SStxPKHMinOutSize {
return 0, stakeRuleError(ErrSStxBadCommitAmount, "short read "+
"of sstx commit pkscript")
}
// The MSB (sign), not used ever normally, encodes whether
// or not it is a P2PKH or P2SH for the input.
amtEncoded := make([]byte, 8, 8)
copy(amtEncoded, pkScript[22:30])
amtEncoded[7] &= ^uint8(1 << 7) // Clear bit for P2SH flag
return dcrutil.Amount(binary.LittleEndian.Uint64(amtEncoded)), nil
}
// TxSSGenStakeOutputInfo takes an SSGen tx as input and scans through its
// outputs, returning the amount of the output and the PKH or SH that it was
// sent to.
func TxSSGenStakeOutputInfo(tx *wire.MsgTx, params *chaincfg.Params) ([]bool,
[][]byte, []int64, error) {
numOutputsInSSGen := len(tx.TxOut)
isP2SH := make([]bool, numOutputsInSSGen-2)
addresses := make([][]byte, numOutputsInSSGen-2)
amounts := make([]int64, numOutputsInSSGen-2)
// Cycle through the inputs and generate
for idx, out := range tx.TxOut {
// We only care about the outputs where we get proportional
// amounts and the PKHs they were sent to.
if (idx > 1) && (idx < numOutputsInSSGen) {
// Get the PKH or SH it's going to, and what type of
// script it is.
class, addr, _, err :=
txscript.ExtractPkScriptAddrs(out.Version, out.PkScript, params)
if err != nil {
return nil, nil, nil, err
}
if class != txscript.StakeGenTy {
return nil, nil, nil, fmt.Errorf("ssgen output included non "+
"ssgen tagged output in idx %v", idx)
}
subClass, err := txscript.GetStakeOutSubclass(out.PkScript)
if !(subClass == txscript.PubKeyHashTy ||
subClass == txscript.ScriptHashTy) {
return nil, nil, nil, fmt.Errorf("bad script type")
}
isP2SH[idx-2] = false
if subClass == txscript.ScriptHashTy {
isP2SH[idx-2] = true
}
// Get the amount that was sent.
amt := out.Value
addresses[idx-2] = addr[0].ScriptAddress()
amounts[idx-2] = amt
}
}
return isP2SH, addresses, amounts, nil
}
// SSGenBlockVotedOn takes an SSGen tx and returns the block voted on in the
// first OP_RETURN by hash and height.
//
// This function is only safe to be called on a transaction that
// has passed IsSSGen.
func SSGenBlockVotedOn(tx *wire.MsgTx) (chainhash.Hash, uint32, error) {
// Get the block header hash.
blockHash, err := chainhash.NewHash(tx.TxOut[0].PkScript[2:34])
if err != nil {
return chainhash.Hash{}, 0, err
}
// Get the block height.
height := binary.LittleEndian.Uint32(tx.TxOut[0].PkScript[34:38])
return *blockHash, height, nil
}
// SSGenVoteBits takes an SSGen tx as input and scans through its
// outputs, returning the VoteBits of the index 1 output.
//
// This function is only safe to be called on a transaction that
// has passed IsSSGen.
func SSGenVoteBits(tx *wire.MsgTx) uint16 {
return binary.LittleEndian.Uint16(tx.TxOut[1].PkScript[2:4])
}
// SSGenVersion takes an SSGen tx as input and returns the network
// consensus version from the VoteBits output. If there is a short
// read, the network consensus version is considered 0 or "unset".
//
// This function is only safe to be called on a transaction that
// has passed IsSSGen.
func SSGenVersion(tx *wire.MsgTx) uint32 {
if len(tx.TxOut[1].PkScript) < 8 {
return VoteConsensusVersionAbsent
}
return binary.LittleEndian.Uint32(tx.TxOut[1].PkScript[4:8])
}
// TxSSRtxStakeOutputInfo takes an SSRtx tx as input and scans through its
// outputs, returning the amount of the output and the pkh that it was sent to.
func TxSSRtxStakeOutputInfo(tx *wire.MsgTx, params *chaincfg.Params) ([]bool,
[][]byte, []int64, error) {
numOutputsInSSRtx := len(tx.TxOut)
isP2SH := make([]bool, numOutputsInSSRtx)
addresses := make([][]byte, numOutputsInSSRtx)
amounts := make([]int64, numOutputsInSSRtx)
// Cycle through the inputs and generate
for idx, out := range tx.TxOut {
// Get the PKH or SH it's going to, and what type of
// script it is.
class, addr, _, err :=
txscript.ExtractPkScriptAddrs(out.Version, out.PkScript, params)
if err != nil {
return nil, nil, nil, err
}
if class != txscript.StakeRevocationTy {
return nil, nil, nil, fmt.Errorf("ssrtx output included non "+
"ssrtx tagged output in idx %v", idx)
}
subClass, err := txscript.GetStakeOutSubclass(out.PkScript)
if !(subClass == txscript.PubKeyHashTy ||
subClass == txscript.ScriptHashTy) {
return nil, nil, nil, fmt.Errorf("bad script type")
}
isP2SH[idx] = false
if subClass == txscript.ScriptHashTy {
isP2SH[idx] = true
}
// Get the amount that was sent.
amt := out.Value
addresses[idx] = addr[0].ScriptAddress()
amounts[idx] = amt
}
return isP2SH, addresses, amounts, nil
}
// SStxNullOutputAmounts takes an array of input amounts, change amounts, and a
// ticket purchase amount, calculates the adjusted proportion from the purchase
// amount, stores it in an array, then returns the array. That is, for any given
// SStx, this function calculates the proportional outputs that any single user
// should receive.
// Returns: (1) Fees (2) Output Amounts (3) Error
func SStxNullOutputAmounts(amounts []int64,
changeAmounts []int64,
amountTicket int64) (int64, []int64, error) {
lengthAmounts := len(amounts)
if lengthAmounts != len(changeAmounts) {
errStr := fmt.Sprintf("amounts was not equal in length " +
"to change amounts!")
return 0, nil, errors.New(errStr)
}
if amountTicket <= 0 {
errStr := fmt.Sprintf("committed amount was too small!")
return 0, nil, stakeRuleError(ErrSStxBadCommitAmount, errStr)
}
contribAmounts := make([]int64, lengthAmounts)
sum := int64(0)
// Now we want to get the adjusted amounts. The algorithm is like this:
// 1 foreach amount
// 2 subtract change from input, store
// 3 add this amount to sum
// 4 check sum against the total committed amount
for i := 0; i < lengthAmounts; i++ {
contribAmounts[i] = amounts[i] - changeAmounts[i]
if contribAmounts[i] < 0 {
errStr := fmt.Sprintf("change at idx %v spent more coins than "+
"allowed (have: %v, spent: %v)", i, amounts[i], changeAmounts[i])
return 0, nil, stakeRuleError(ErrSStxBadChangeAmts, errStr)
}
sum += contribAmounts[i]
}
fees := sum - amountTicket
return fees, contribAmounts, nil
}
// CalculateRewards takes a list of SStx adjusted output amounts, the amount used
// to purchase that ticket, and the reward for an SSGen tx and subsequently
// generates what the outputs should be in the SSGen tx. If used for calculating
// the outputs for an SSRtx, pass 0 for subsidy.
func CalculateRewards(amounts []int64, amountTicket int64,
subsidy int64) []int64 {
outputsAmounts := make([]int64, len(amounts))
// SSGen handling
amountWithStakebase := amountTicket + subsidy
// Get the sum of the amounts contributed between both fees
// and contributions to the ticket.
totalContrib := int64(0)
for _, amount := range amounts {
totalContrib += amount
}
// Now we want to get the adjusted amounts including the reward.
// The algorithm is like this:
// 1 foreach amount
// 2 amount *= 2^32
// 3 amount /= amountTicket
// 4 amount *= amountWithStakebase
// 5 amount /= 2^32
amountWithStakebaseBig := big.NewInt(amountWithStakebase)
totalContribBig := big.NewInt(totalContrib)
for idx, amount := range amounts {
amountBig := big.NewInt(amount) // We need > 64 bits
// mul amountWithStakebase
amountBig.Mul(amountBig, amountWithStakebaseBig)
// mul 2^32
amountBig.Lsh(amountBig, 32)
// div totalContrib
amountBig.Div(amountBig, totalContribBig)
// div 2^32
amountBig.Rsh(amountBig, 32)
// make int64
amountFinal := int64(amountBig.Uint64())
outputsAmounts[idx] = amountFinal
}
return outputsAmounts
}
// VerifySStxAmounts compares a list of calculated amounts for ticket commitments
// to the list of commitment amounts from the actual SStx.
func VerifySStxAmounts(sstxAmts []int64, sstxCalcAmts []int64) error {
if len(sstxCalcAmts) != len(sstxAmts) {
errStr := fmt.Sprintf("SStx verify error: number of calculated " +
"sstx output values was not equivalent to the number of sstx " +
"commitment outputs")
return stakeRuleError(ErrVerSStxAmts, errStr)
}
for idx, amt := range sstxCalcAmts {
if !(amt == sstxAmts[idx]) {
errStr := fmt.Sprintf("SStx verify error: at index %v incongruent "+
"amt %v in SStx calculated reward and amt %v in "+
"SStx", idx, amt, sstxAmts[idx])
return stakeRuleError(ErrVerSStxAmts, errStr)
}
}
return nil
}
// VerifyStakingPkhsAndAmounts takes the following:
// 1. sstxTypes: A list of types for what the output should be (P2PK or P2SH).
// 2. sstxPkhs: A list of payee PKHs from NullDataTy outputs of an input SStx.
// 3. ssSpendAmts: What the payouts in an SSGen/SSRtx tx actually were.
// 4. ssSpendTypes: A list of types for what the outputs actually were.
// 5. ssSpendPkhs: A list of payee PKHs from OP_SSGEN tagged outputs of the SSGen
// or SSRtx.
// 6. ssSpendCalcAmts: A list of payee amounts that was calculated based on
// the input SStx. These are the maximum possible amounts that can be
// transacted from this output.
// 7. isVote: Whether this is a vote (true) or revocation (false).
// 8. spendRules: Spending rules for each output in terms of fees allowable
// as extracted from the origin output Version.
// 9. spendLimits: Spending limits for each output in terms of fees allowable
// as extracted from the origin output Version.
//
// and determines if the two pairs of slices are congruent or not.
func VerifyStakingPkhsAndAmounts(
sstxTypes []bool,
sstxPkhs [][]byte,
ssSpendAmts []int64,
ssSpendTypes []bool,
ssSpendPkhs [][]byte,
ssSpendCalcAmts []int64,
isVote bool,
spendRules [][]bool,
spendLimits [][]uint16) error {
if len(sstxTypes) != len(ssSpendTypes) {
errStr := fmt.Sprintf("Staking verify error: number of " +
"sstx type values was not equivalent to the number " +
"of ss*** type values")
return stakeRuleError(ErrVerifyInput, errStr)
}
if len(ssSpendAmts) != len(ssSpendCalcAmts) {
errStr := fmt.Sprintf("Staking verify error: number of " +
"sstx output values was not equivalent to the number " +
"of ss*** output values")
return stakeRuleError(ErrVerifyInput, errStr)
}
if len(sstxPkhs) != len(ssSpendPkhs) {
errStr := fmt.Sprintf("Staking verify error: number of " +
"sstx output pks was not equivalent to the number " +
"of ss*** output pkhs")
return stakeRuleError(ErrVerifyInput, errStr)
}
for idx, typ := range sstxTypes {
if typ != ssSpendTypes[idx] {
errStr := fmt.Sprintf("SStx in/SS*** out verify error: at index %v "+
"non-equivalent type %v in SStx and type %v in SS***", idx, typ,
ssSpendTypes[idx])
return stakeRuleError(ErrVerifyOutType, errStr)
}
}
for idx, amt := range ssSpendAmts {
rule := false
limit := uint16(0)
if isVote {
// Vote.
rule = spendRules[idx][0]
limit = spendLimits[idx][0]
} else {
// Revocation.
rule = spendRules[idx][1]
limit = spendLimits[idx][1]
}
// Apply the spending rules and see if the transaction is within
// the specified limits if it asks us to.
if rule {
// If 63 is given, the entire amount may be used as a fee.
// Obviously we can't allow shifting 1 63 places because
// we'd get a negative number.
feeAllowance := ssSpendCalcAmts[idx]
if limit < rangeLimitMax {
if int64(1<<uint64(limit)) < ssSpendCalcAmts[idx] {
feeAllowance = int64(1 << uint64(limit))
}
}
amtLimitLow := ssSpendCalcAmts[idx] - feeAllowance
amtLimitHigh := ssSpendCalcAmts[idx]
// Our new output is not allowed to be below this minimum
// amount.
if amt < amtLimitLow {
errStr := fmt.Sprintf("SStx in/SS*** out verify error: "+
"at index %v amt min limit was calculated to be %v yet "+
"the actual amount output was %v", idx, amtLimitLow,
amt)
return stakeRuleError(ErrVerifyTooMuchFees, errStr)
}
// It also isn't allowed to spend more than the maximum
// amount.
if amt > amtLimitHigh {
errStr := fmt.Sprintf("at index %v amt max limit was "+
"calculated to be %v yet the actual amount output "+
"was %v", idx, amtLimitHigh, amt)
return stakeRuleError(ErrVerifySpendTooMuch, errStr)
}
} else {
// Fees are disabled.
if amt != ssSpendCalcAmts[idx] {
errStr := fmt.Sprintf("SStx in/SS*** out verify error: "+
"at index %v non-equivalent amt %v in SStx and amt "+
"%v in SS***", idx, amt, ssSpendCalcAmts[idx])
return stakeRuleError(ErrVerifyOutputAmt, errStr)
}
}
}
for idx, pkh := range sstxPkhs {
if !bytes.Equal(pkh, ssSpendPkhs[idx]) {
errStr := fmt.Sprintf("SStx in/SS*** out verify error: at index %v "+
"non-equivalent pkh %x in SStx and pkh %x in SS***", idx, pkh,
ssSpendPkhs[idx])
return stakeRuleError(ErrVerifyOutPkhs, errStr)
}
}
return nil
}
// --------------------------------------------------------------------------------
// Stake Transaction Identification Functions
// --------------------------------------------------------------------------------
// IsSStx returns whether or not a transaction is an SStx. It does some
// simple validation steps to make sure the number of inputs, number of
// outputs, and the input/output scripts are valid.
//
// SStx transactions are specified as below.
// Inputs:
// untagged output 1 [index 0]
// untagged output 2 [index 1]
// ...
// untagged output MaxInputsPerSStx [index MaxInputsPerSStx-1]
//
// Outputs:
// OP_SSTX tagged output [index 0]
// OP_RETURN push of input 1's address for reward receiving [index 1]
// OP_SSTXCHANGE tagged output for input 1 [index 2]
// OP_RETURN push of input 2's address for reward receiving [index 3]
// OP_SSTXCHANGE tagged output for input 2 [index 4]
// ...
// OP_RETURN push of input MaxInputsPerSStx's address for reward receiving
// [index (MaxInputsPerSStx*2)-2]
// OP_SSTXCHANGE tagged output [index (MaxInputsPerSStx*2)-1]
//
// The output OP_RETURN pushes should be of size 20 bytes (standard address).
//
// The errors in this function can be ignored if you want to use it in to
// identify SStx from a list of stake tx.
func IsSStx(tx *wire.MsgTx) (bool, error) {
// Check to make sure there aren't too many inputs.
// CheckTransactionSanity already makes sure that number of inputs is
// greater than 0, so no need to check that.
if len(tx.TxIn) > MaxInputsPerSStx {
return false, stakeRuleError(ErrSStxTooManyInputs, "SStx has too many "+
"inputs")
}
// Check to make sure there aren't too many outputs.
if len(tx.TxOut) > MaxOutputsPerSStx {
return false, stakeRuleError(ErrSStxTooManyOutputs, "SStx has too many "+
"outputs")
}
// Check to make sure there are some outputs.
if len(tx.TxOut) == 0 {
return false, stakeRuleError(ErrSStxNoOutputs, "SStx has no "+
"outputs")
}
// Check to make sure that all output scripts are the default version.
for idx, txOut := range tx.TxOut {
if txOut.Version != txscript.DefaultScriptVersion {
errStr := fmt.Sprintf("invalid script version found in "+
"txOut idx %v", idx)
return false, stakeRuleError(ErrSStxInvalidOutputs, errStr)
}
}
// Ensure that the first output is tagged OP_SSTX.
if txscript.GetScriptClass(tx.TxOut[0].Version, tx.TxOut[0].PkScript) !=
txscript.StakeSubmissionTy {
return false, stakeRuleError(ErrSStxInvalidOutputs, "First SStx output "+
"should have been OP_SSTX tagged, but it was not")
}
// Ensure that the number of outputs is equal to the number of inputs
// + 1.
if (len(tx.TxIn)*2 + 1) != len(tx.TxOut) {
return false, stakeRuleError(ErrSStxInOutProportions, "The number of "+
"inputs in the SStx tx was not the number of outputs/2 - 1")
}
// Ensure that the rest of the odd outputs are 28-byte OP_RETURN pushes that
// contain putative pubkeyhashes, and that the rest of the odd outputs are
// OP_SSTXCHANGE tagged.
for outTxIndex := 1; outTxIndex < len(tx.TxOut); outTxIndex++ {
scrVersion := tx.TxOut[outTxIndex].Version
rawScript := tx.TxOut[outTxIndex].PkScript
// Check change outputs.
if outTxIndex%2 == 0 {
if txscript.GetScriptClass(scrVersion, rawScript) !=
txscript.StakeSubChangeTy {
str := fmt.Sprintf("SStx output at output index %d was not "+
"an sstx change output", outTxIndex)
return false, stakeRuleError(ErrSStxInvalidOutputs, str)
}
continue
}
// Else (odd) check commitment outputs. The script should be a
// NullDataTy output.
if txscript.GetScriptClass(scrVersion, rawScript) !=
txscript.NullDataTy {
str := fmt.Sprintf("SStx output at output index %d was not "+
"a NullData (OP_RETURN) push", outTxIndex)
return false, stakeRuleError(ErrSStxInvalidOutputs, str)
}
// The length of the output script should be between 32 and 77 bytes long.
if len(rawScript) < SStxPKHMinOutSize ||
len(rawScript) > SStxPKHMaxOutSize {
str := fmt.Sprintf("SStx output at output index %d was a "+
"NullData (OP_RETURN) push of the wrong size", outTxIndex)
return false, stakeRuleError(ErrSStxInvalidOutputs, str)
}
// The OP_RETURN output script prefix should conform to the standard.
outputScriptBuffer := bytes.NewBuffer(rawScript)
outputScriptPrefix := outputScriptBuffer.Next(2)
minPush := validSStxAddressOutMinPrefix[1]
maxPush := validSStxAddressOutMinPrefix[1] +
(MaxSingleBytePushLength - minPush)
pushLen := outputScriptPrefix[1]
pushLengthValid := (pushLen >= minPush) && (pushLen <= maxPush)
// The first byte should be OP_RETURN, while the second byte should be a
// valid push length.
if !(outputScriptPrefix[0] == validSStxAddressOutMinPrefix[0]) ||
!pushLengthValid {
errStr := fmt.Sprintf("sstx commitment at output idx %v had "+
"an invalid prefix", outTxIndex)
return false, stakeRuleError(ErrSStxInvalidOutputs,
errStr)
}
}
return true, nil
}
// IsSSGen returns whether or not a transaction is an SSGen tx. It does some
// simple validation steps to make sure the number of inputs, number of
// outputs, and the input/output scripts are valid.
//
// This does NOT check to see if the subsidy is valid or whether or not the
// value of input[0] + subsidy = value of the outputs.
//
// SSGen transactions are specified as below.
// Inputs:
// Stakebase null input [index 0]
// SStx-tagged output [index 1]
//
// Outputs:
// OP_RETURN push of 40 bytes containing: [index 0]
// i. 32-byte block header of block being voted on.
// ii. 8-byte int of this block's height.
// OP_RETURN push of 2 bytes containing votebits [index 1]
// SSGen-tagged output to address from SStx-tagged output's tx index output 1
// [index 2]
// SSGen-tagged output to address from SStx-tagged output's tx index output 2
// [index 3]
// ...
// SSGen-tagged output to address from SStx-tagged output's tx index output
// MaxInputsPerSStx [index MaxOutputsPerSSgen - 1]
//
// The errors in this function can be ignored if you want to use it in to
// identify SSGen from a list of stake tx.
func IsSSGen(tx *wire.MsgTx) (bool, error) {
// Check to make sure there aren't too many inputs.
// CheckTransactionSanity already makes sure that number of inputs is
// greater than 0, so no need to check that.
if len(tx.TxIn) != NumInputsPerSSGen {
return false, stakeRuleError(ErrSSGenWrongNumInputs, "SSgen tx has an "+
"invalid number of inputs")
}
// Check to make sure there aren't too many outputs.
if len(tx.TxOut) > MaxOutputsPerSSGen {
return false, stakeRuleError(ErrSSGenTooManyOutputs, "SSgen tx has too "+
"many outputs")
}
// Check to make sure there are some outputs.
if len(tx.TxOut) == 0 {
return false, stakeRuleError(ErrSSGenNoOutputs, "SSgen tx no "+
"many outputs")
}
// Ensure that the first input is a stake base null input.
// Also checks to make sure that there aren't too many or too few inputs.
if !IsStakeBase(tx) {
return false, stakeRuleError(ErrSSGenNoStakebase, "SSGen tx did not "+
"include a stakebase in the zeroeth input position")
}
// Check to make sure that the output used as input came from TxTreeStake.
for i, txin := range tx.TxIn {
// Skip the stakebase
if i == 0 {
continue
}
if txin.PreviousOutPoint.Index != 0 {
errStr := fmt.Sprintf("SSGen used an invalid input idx (got %v, "+
"want 0)", txin.PreviousOutPoint.Index)
return false, stakeRuleError(ErrSSGenWrongIndex, errStr)
}
if txin.PreviousOutPoint.Tree != wire.TxTreeStake {
return false, stakeRuleError(ErrSSGenWrongTxTree, "SSGen used "+
"a non-stake input")
}
}
// Check to make sure that all output scripts are the default version.
for _, txOut := range tx.TxOut {
if txOut.Version != txscript.DefaultScriptVersion {
return false, stakeRuleError(ErrSSGenBadGenOuts, "invalid "+
"script version found in txOut")
}
}
// Ensure the number of outputs is equal to the number of inputs found in
// the original SStx + 2.
// TODO: Do this in validate, requires DB and valid chain.
// Ensure that the second input is an SStx tagged output.
// TODO: Do this in validate, as we don't want to actually lookup
// old tx here. This function is for more general sorting.
// Ensure that the first output is an OP_RETURN push.
zeroethOutputVersion := tx.TxOut[0].Version
zeroethOutputScript := tx.TxOut[0].PkScript
if txscript.GetScriptClass(zeroethOutputVersion, zeroethOutputScript) !=
txscript.NullDataTy {
return false, stakeRuleError(ErrSSGenNoReference, "First SSGen output "+
"should have been an OP_RETURN data push, but was not")
}
// Ensure that the first output is the correct size.
if len(zeroethOutputScript) != SSGenBlockReferenceOutSize {
return false, stakeRuleError(ErrSSGenBadReference, "First SSGen output "+
"should have been 43 bytes long, but was not")
}
// The OP_RETURN output script prefix for block referencing should