-
Notifications
You must be signed in to change notification settings - Fork 1
/
staketx.go
1474 lines (1291 loc) · 52.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-2021 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"
"fmt"
"math"
"math/big"
"github.com/EXCCoin/exccd/chaincfg/chainhash"
"github.com/EXCCoin/exccd/chaincfg/v3"
"github.com/EXCCoin/exccd/dcrutil/v4"
"github.com/EXCCoin/exccd/txscript/v4"
"github.com/EXCCoin/exccd/txscript/v4/stdaddr"
"github.com/EXCCoin/exccd/wire"
)
// TxType indicates the type of tx (regular or stake type).
type TxType int
// Possible TxTypes.
const (
TxTypeRegular TxType = iota
TxTypeSStx
TxTypeSSGen
TxTypeSSRtx
TxTypeTAdd
TxTypeTSpend
TxTypeTreasuryBase
)
const (
// consensusVersion = txscript.consensusVersion
consensusVersion = 0
// TxVersionAutoRevocations is the revocation transaction version that enables
// automatic ticket revocations.
TxVersionAutoRevocations uint16 = 2
// 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.
//
// NOTE: If the treasury agenda is active, an OP_RETURN TV (treasury vote)
// output is additionally included in the last position when voting on a
// treasury spend. The MaxOutputsPerSSGen value does NOT account for this
// output, and should technically be increased by 1, but since the treasury
// agenda is already active this cannot be updated without a consensus vote.
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.
MaxOutputsPerSSRtx = MaxInputsPerSStx
// SStxPKHMinOutSize is the minimum size 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 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
// MaxDataCarrierSize is the maximum number of bytes allowed in pushed
// data in the various stake transactions.
MaxDataCarrierSize = 256
)
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{txscript.OP_RETURN, txscript.OP_DATA_30}
// validSSGenReferenceOutPrefix is the valid prefix for a block
// reference output for an SSGen tx.
// Example SStx address out:
// 0x6a (OP_RETURN)
// 0x24 (OP_DATA_36, push length: 36 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{txscript.OP_RETURN, txscript.OP_DATA_36}
// 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{txscript.OP_RETURN, txscript.OP_DATA_2}
// 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{}
)
// 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
}
// VoteVersionTuple contains the extracted vote bits and version from votes
// (SSGen).
type VoteVersionTuple struct {
Version uint32
Bits uint16
}
// SpentTicketsInBlock stores the hashes of the spent (both voted and revoked)
// tickets of a given block, along with the vote information.
type SpentTicketsInBlock struct {
VotedTickets []chainhash.Hash
RevokedTickets []chainhash.Hash
Votes []VoteVersionTuple
}
// --------------------------------------------------------------------------------
// 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
}
// isSmallInt returns whether or not the opcode is considered a small integer,
// which is an OP_0, or OP_1 through OP_16.
//
// NOTE: This function is only valid for version 0 opcodes.
func isSmallInt(op byte) bool {
return op == txscript.OP_0 || (op >= txscript.OP_1 && op <= txscript.OP_16)
}
// IsNullDataScript returns whether or not the passed script is a null
// data script.
//
// NOTE: This function is only valid for version 0 scripts. It will always
// return false for other script versions.
func IsNullDataScript(scriptVersion uint16, script []byte) bool {
// The only supported script version is 0.
if scriptVersion != 0 {
return false
}
// A null script is of the form:
// OP_RETURN <optional data>
//
// Thus, it can either be a single OP_RETURN or an OP_RETURN followed by a
// data push up to MaxDataCarrierSize bytes.
// The script can't possibly be a null data script if it doesn't start
// with OP_RETURN. Fail fast to avoid more work below.
if len(script) < 1 || script[0] != txscript.OP_RETURN {
return false
}
// Single OP_RETURN.
if len(script) == 1 {
return true
}
// OP_RETURN followed by data push up to MaxDataCarrierSize bytes.
tokenizer := txscript.MakeScriptTokenizer(scriptVersion, script[1:])
return tokenizer.Next() && tokenizer.Done() &&
(isSmallInt(tokenizer.Opcode()) ||
tokenizer.Opcode() <= txscript.OP_PUSHDATA4) &&
len(tokenizer.Data()) <= MaxDataCarrierSize
}
// 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 null data pushes (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)
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)
spendLimits := make([]uint16, 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 null data pushes (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 stdaddr.AddressParams) (stdaddr.StakeAddress, error) {
if len(pkScript) < SStxPKHMinOutSize {
str := "short read of sstx commit pkscript"
return nil, stakeRuleError(ErrSStxBadCommitAmount, str)
}
// The MSB of the encoded amount specifies if the output is P2SH. Since
// it is encoded with little endian, the MSB is in final byte in the encoded
// amount.
//
// This is a faster equivalent of:
//
// amtBytes := script[22:30]
// amtEncoded := binary.LittleEndian.Uint64(amtBytes)
// isP2SH := (amtEncoded & uint64(1<<63)) != 0
isP2SH := pkScript[29]&0x80 != 0
// The 20 byte PKH or SH.
hashBytes := pkScript[2:22]
// Return the correct address type.
if isP2SH {
return stdaddr.NewAddressScriptHashV0FromHash(hashBytes, params)
}
return stdaddr.NewAddressPubKeyHashEcdsaSecp256k1V0(hashBytes, params)
}
// AmountFromSStxPkScrCommitment extracts a commitment amount from a
// ticket commitment pkScript.
func AmountFromSStxPkScrCommitment(pkScript []byte) (dcrutil.Amount, error) {
if len(pkScript) < SStxPKHMinOutSize {
str := "short read of sstx commit pkscript"
return 0, stakeRuleError(ErrSStxBadCommitAmount, str)
}
// The MSB (sign), not used ever normally, encodes whether
// or not it is a P2PKH or P2SH for the input.
amtEncoded := make([]byte, 8)
copy(amtEncoded, pkScript[22:30])
amtEncoded[7] &= ^uint8(1 << 7) // Clear bit for P2SH flag
return dcrutil.Amount(binary.LittleEndian.Uint64(amtEncoded)), 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) {
// Get the block header hash. Note that the actual number of bytes is
// specified here over using chainhash.HashSize in order to statically
// assert hash sizes have not changed.
var blockHash [32]byte
copy(blockHash[:], tx.TxOut[0].PkScript[2:34])
// Get the block height.
height := binary.LittleEndian.Uint32(tx.TxOut[0].PkScript[34:38])
return chainhash.Hash(blockHash), height
}
// 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])
}
// 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) {
str := "amounts was not equal in length to change amounts!"
return 0, nil, fmt.Errorf(str)
}
if amountTicket <= 0 {
str := "committed amount was too small!"
return 0, nil, stakeRuleError(ErrSStxBadCommitAmount, str)
}
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 {
str := 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, str)
}
sum += contribAmounts[i]
}
fees := sum - amountTicket
return fees, contribAmounts, nil
}
// calculateTicketReturnAmounts calculates the required amounts to return from a
// ticket for the given original contribution amounts, the price the ticket was
// purchased for, and the vote subsidy (if any) to include.
//
// Since multiple inputs can be used to purchase a ticket, each one contributes
// a portion of the overall ticket purchase, including the transaction fee.
// Thus, when claiming the ticket, either due to it being selected to vote, or
// being revoked, each output must receive the same proportion of the total
// amount returned.
//
// The vote subsidy must be 0 for revocations since, unlike votes, they do not
// produce any additional subsidy.
//
// Note: This function is intended to only be a helper function, and when
// calculating rewards CalculateRewards or CalculateRevocationRewards should be
// used.
func calculateTicketReturnAmounts(contribAmounts []int64, ticketPurchaseAmount,
voteSubsidy int64) []int64 {
// Get the sum of the amounts contributed between both fees
// and contributions to the ticket.
totalContrib := int64(0)
for _, amount := range contribAmounts {
totalContrib += amount
}
totalContribBig := big.NewInt(totalContrib)
// Create a slice for the return amounts.
numReturnAmounts := uint32(len(contribAmounts))
returnAmounts := make([]int64, numReturnAmounts)
// Calculate the return amounts.
totalOutputAmt := ticketPurchaseAmount + voteSubsidy
totalOutputAmtBig := big.NewInt(totalOutputAmt)
for i, contribAmount := range contribAmounts {
// This is effectively equivalent to:
//
// total output amount
// return amount = ------------------- * contribution amount
// total contributions
//
// However, in order to avoid floating point math, it uses 64.32 fixed point
// integer division to perform:
//
// -- --
// | total output amount * contribution amount * 2^32 |
// | ------------------------------------------------ |
// return amount = | total contributions |
// -- --
// ----------------------------------------------------
// 2^32
//
returnAmtBig := big.NewInt(contribAmount)
returnAmtBig.Mul(returnAmtBig, totalOutputAmtBig)
returnAmtBig.Lsh(returnAmtBig, 32)
returnAmtBig.Div(returnAmtBig, totalContribBig)
returnAmtBig.Rsh(returnAmtBig, 32)
returnAmounts[i] = returnAmtBig.Int64()
}
return returnAmounts
}
// CalculateRewards calculates the required amounts to return for a vote given
// the original contribution amounts for the ticket, the price the ticket was
// purchased for, and the vote subsidy to include.
//
// Since multiple inputs can be used to purchase a ticket, each one contributes
// a portion of the overall ticket purchase, including the transaction fee.
// Thus, when claiming the ticket, each output must receive the same proportion
// of the total amount returned.
//
// After the original contribution amounts are evenly distributed to each
// output, there may be a remainder left over of 1 to numOutputs - 1 atoms.
// This remainder ends up as part of the transaction fee.
//
// Note: If calculating rewards for a revocation, CalculateRevocationRewards
// should be used instead.
func CalculateRewards(contribAmounts []int64, ticketPurchaseAmount,
voteSubsidy int64) []int64 {
return calculateTicketReturnAmounts(contribAmounts, ticketPurchaseAmount,
voteSubsidy)
}
// CalculateRevocationRewards calculates the required amounts to return for a
// revocation given the original contribution amounts for the ticket and the
// price the ticket was purchased for.
//
// Since multiple inputs can be used to purchase a ticket, each one contributes
// a portion of the overall ticket purchase, including the transaction fee.
// Thus, when claiming the ticket, each output must receive the same proportion
// of the total amount returned.
//
// After the original contribution amounts are evenly distributed to each
// output, there may be a remainder left over of 1 to numOutputs - 1 atoms.
// If the automatic ticket revocations agenda is not active, this remainder ends
// up as part of the transaction fee. If the agenda is active, each atom in the
// remainder is added to an output index that is selected in a uniformly random
// manner, where Hash256PRNG seeded with the previous header bytes is used for
// the deterministic pseudorandom selection.
func CalculateRevocationRewards(contribAmounts []int64, ticketPurchaseAmount int64,
prevHeaderBytes []byte, isAutoRevocationsEnabled bool) []int64 {
// Calculate the evenly distributed return amounts from the original
// contribution amounts.
returnAmounts := calculateTicketReturnAmounts(contribAmounts,
ticketPurchaseAmount, 0)
// Return now if the automatic ticket revocations agenda is not active, and
// any remainder will end up as part of the transaction fee.
if !isAutoRevocationsEnabled {
return returnAmounts
}
// Calculate the total return amount.
totalReturnAmount := int64(0)
for _, amount := range returnAmounts {
totalReturnAmount += amount
}
// If the automatic ticket revocations agenda is active, and there is a
// remainder after distributing the returns, then select a uniformly
// pseudorandom output index to receive each remaining atom.
if totalReturnAmount < ticketPurchaseAmount {
numReturnAmounts := uint32(len(returnAmounts))
remainder := ticketPurchaseAmount - totalReturnAmount
prng := NewHash256PRNG(prevHeaderBytes)
for i := int64(0); i < remainder; i++ {
returnIndex := prng.UniformRandom(numReturnAmounts)
returnAmounts[returnIndex] += 1
}
}
return returnAmounts
}
// --------------------------------------------------------------------------------
// Stake Transaction Identification Functions
// --------------------------------------------------------------------------------
// CheckSStx returns an error if a transaction is not a stake submission
// transaction. 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).
func CheckSStx(tx *wire.MsgTx) 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 {
str := "SStx has too many inputs"
return stakeRuleError(ErrSStxTooManyInputs, str)
}
// Check to make sure there aren't too many outputs.
if len(tx.TxOut) > MaxOutputsPerSStx {
str := "SStx has too many outputs"
return stakeRuleError(ErrSStxTooManyOutputs, str)
}
// Check to make sure there are some outputs.
if len(tx.TxOut) == 0 {
str := "SStx has no outputs"
return stakeRuleError(ErrSStxNoOutputs, str)
}
// Check to make sure that all output scripts are the consensus version.
for idx, txOut := range tx.TxOut {
if txOut.Version != consensusVersion {
str := fmt.Sprintf("invalid script version found in "+
"txOut idx %v", idx)
return stakeRuleError(ErrSStxInvalidOutputs, str)
}
}
// Ensure that the first output is tagged OP_SSTX.
if !IsTicketPurchaseScript(tx.TxOut[0].Version, tx.TxOut[0].PkScript) {
str := "first SStx output should have been OP_SSTX tagged, " +
"but it was not"
return stakeRuleError(ErrSStxInvalidOutputs, str)
}
// Ensure that the number of outputs is equal to the number of inputs
// + 1.
if (len(tx.TxIn)*2 + 1) != len(tx.TxOut) {
str := "the number of inputs in the SStx tx was not the number " +
"of outputs/2 - 1"
return stakeRuleError(ErrSStxInOutProportions, str)
}
// 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 !IsStakeChangeScript(scrVersion, rawScript) {
str := fmt.Sprintf("SStx output at output index %d was not "+
"an sstx change output", outTxIndex)
return stakeRuleError(ErrSStxInvalidOutputs, str)
}
continue
}
// Else (odd) check commitment outputs. The script should be a
// null data output.
if !IsNullDataScript(scrVersion, rawScript) {
str := fmt.Sprintf("SStx output at output index %d was not "+
"a null data (OP_RETURN) push", outTxIndex)
return 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 "+
"null data (OP_RETURN) push of the wrong size", outTxIndex)
return 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 {
str := fmt.Sprintf("sstx commitment at output idx %v had "+
"an invalid prefix", outTxIndex)
return stakeRuleError(ErrSStxInvalidOutputs, str)
}
}
return nil
}
// IsSStx returns whether or not a transaction is a stake submission transaction.
// These are also known as tickets.
func IsSStx(tx *wire.MsgTx) bool {
return CheckSStx(tx) == nil
}
// TreasuryVoteT is the type that designates a treasury vote. There are two
// valid bits that may be set, although not simultaneously. 0x01 and 0x02. Any
// other (or lack therefore) bits are considered invalid.
type TreasuryVoteT byte
const (
// TreasuryVoteInvalid identifies an invalid vote for a treasury spend
// transaction.
TreasuryVoteInvalid TreasuryVoteT = 0x00
// TreasuryVoteYes identifies a vote in favor of a specific treasury spend
// transaction.
TreasuryVoteYes TreasuryVoteT = 0x01
// TreasuryVoteNo identifies a vote against a specific treasury spend
// transaction.
TreasuryVoteNo TreasuryVoteT = 0x02
)
// CheckTreasuryVote ensures that the provided treasury vote is valid. If the
// vote is valid the proper value is returned.
func CheckTreasuryVote(vote TreasuryVoteT) (TreasuryVoteT, error) {
switch vote {
case TreasuryVoteYes:
case TreasuryVoteNo:
default:
return TreasuryVoteInvalid,
fmt.Errorf("invalid treasury vote: 0x%x", vote)
}
return vote, nil
}
// IsTreasuryVote returns true if the provided vote is valid.
func IsTreasuryVote(vote TreasuryVoteT) bool {
_, err := CheckTreasuryVote(vote)
return err == nil
}
// TreasuryVoteTuple is a tuple that groups a TSpend hash and its associated
// vote bits.
type TreasuryVoteTuple struct {
Hash chainhash.Hash
Vote TreasuryVoteT
}
// GetSSGenTreasuryVotes pulls out treasury votes for TSpend transactions. This
// function is a convenience function to discourage rolling new versions that
// pull out votes and may be therefore different from consensus.
// This function verifies that the passed in script is of the following form:
// OP_RETURN DATA_PUSH []byte{'T', 'V'} N-votes
// and returns a slice of hashes which represent the TSpend transactions they
// are supposed to vote on.
func GetSSGenTreasuryVotes(PkScript []byte) ([]TreasuryVoteTuple, error) {
// Verify that there is enough length to contain a discriminator.
// Expect at least: OP_RETURN DATA_PUSH Y Z
// The final check that is parenthesis is to prevent crashing when
// using an illegal script. This cannot be hit but it is verified
// anyway.
if len(PkScript) < 4 || PkScript[1] > txscript.OP_PUSHDATA1 {
str := fmt.Sprintf("final output of a SSGen does not " +
"contain a valid type discriminator")
return nil, stakeRuleError(ErrSSGenInvalidDiscriminatorLength,
str)
}
// Determine start of discriminator based on the opcode in [1].
start := 2
if PkScript[1] == txscript.OP_PUSHDATA1 {
start = 3
}
if start+2 > len(PkScript) {
str := fmt.Sprintf("final output of a SSGen is not a valid " +
"nullscript")
return nil, stakeRuleError(ErrSSGenInvalidNullScript,
str)
}
// Ensure discriminator is TV.
if !bytes.Equal(PkScript[start:start+2], []byte{'T', 'V'}) {
str := fmt.Sprintf("last SSGen unknown type discriminator: "+
"0x%x 0x%x", PkScript[start], PkScript[start+1])
return nil, stakeRuleError(ErrSSGenUnknownDiscriminator, str)
}
// Since this is a 'T','V' we expect N hashes and their vote bits.
const size = chainhash.HashSize + 1
if len(PkScript[start+2:]) < size ||
len(PkScript[start+2:])%size != 0 {
str := fmt.Sprintf("SSGen 'T','V' invalid " +
"length")
return nil, stakeRuleError(ErrSSGenInvalidTVLength,
str)
}
// Return hashes, this is the success path.
const maxVotes = 7
votes := make([]TreasuryVoteTuple, 0, maxVotes)
vmap := make(map[chainhash.Hash]struct{}, maxVotes) // Collision detection
for i := start + 2; ; i += size {
if len(PkScript[i:]) < size {
break
}
var hash chainhash.Hash
copy(hash[:], PkScript[i:size+i-1])
vote := TreasuryVoteT(PkScript[size+i-1])
if !IsTreasuryVote(vote) {
str := fmt.Sprintf("SSGen invalid treasury "+
"vote bits 0x%0x", vote)
return nil,
stakeRuleError(ErrSSGenInvalidTreasuryVote,
str)
}
// Ensure there are no duplicate TSpend votes.
if _, ok := vmap[hash]; ok {
str := fmt.Sprintf("SSGen duplicate treasury "+
"vote %v", hash)
return nil,
stakeRuleError(ErrSSGenDuplicateTreasuryVote,
str)
}
vmap[hash] = struct{}{}
// Store votes in order of appearance.
votes = append(votes, TreasuryVoteTuple{
Hash: hash,
Vote: vote,
})
}
return votes, nil
}
// CheckSSGenVotes returns an error if a transaction is not a stake submission
// generation transaction. It does some simple validation steps to make sure
// the number of inputs, number of outputs, and the input/output scripts are
// valid. In addition it returns treasury votes to avoid subsequent expensive
// calls.
//
// 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:
// [index 0] Stakebase null input
// [index 1] SStx-tagged output
//
// Outputs:
// [index 0] OP_RETURN push of 40 bytes containing:
// i. 32-byte block header of block being voted on.
// ii. 8-byte int of this block's height.
// [index 1] OP_RETURN push of 2 bytes containing votebits
// [index 2] SSGen-tagged output to the first payment commitment address from
// SStx-tagged output's tx (output index 1)
// [index 3] SSGen-tagged output to the second payment commitment address from
// SStx-tagged output's tx (output index 3)
// ...
// [index maxOuts - 2] SSGen-tagged output to the last payment
// commitment address from SStx-tagged output's tx index output (output
// index MaxInputsPerSStx - 1)
// [index maxOuts - 1] OP_RETURN push of 2 bytes containing opcode
// designating what the remaining data that is pushed is. In the case of
// 'TV' (Treasury Vote) it checks for a <hash><vote> tuple. For example:
// OP_RETURN OP_DATA_X 'T','V' <N hashvote_tuple>
// NOTE: This output is only appended when the treasury agenda is active
// and a treasury spend is being voted on.
func CheckSSGenVotes(tx *wire.MsgTx, isTreasuryEnabled bool) ([]TreasuryVoteTuple, error) {
// Check to make sure there aren't too many inputs.
if len(tx.TxIn) != NumInputsPerSSGen {
str := "SSgen tx has an invalid number of inputs"
return nil, stakeRuleError(ErrSSGenWrongNumInputs, str)
}
// Check to make sure there aren't too many outputs.
if len(tx.TxOut) > MaxOutputsPerSSGen {
str := "SSgen tx has too many outputs"
return nil, stakeRuleError(ErrSSGenTooManyOutputs, str)
}
// Check to make sure there are enough outputs.
if len(tx.TxOut) < 2 {
str := "SSgen tx does not have enough outputs"
return nil, stakeRuleError(ErrSSGenNoOutputs, str)
}