/
validate.go
1235 lines (1096 loc) · 41.9 KB
/
validate.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
package blockchain
import (
"bytes"
"encoding/binary"
"encoding/hex"
"fmt"
"math"
"math/big"
"time"
"github.com/fatzero/mass-core/blockchain/state"
"github.com/fatzero/mass-core/config"
"github.com/fatzero/mass-core/consensus"
"github.com/fatzero/mass-core/consensus/forks"
"github.com/fatzero/mass-core/database"
"github.com/fatzero/mass-core/interfaces"
"github.com/fatzero/mass-core/logging"
"github.com/fatzero/mass-core/massutil"
"github.com/fatzero/mass-core/poc"
"github.com/fatzero/mass-core/poc/pocutil"
"github.com/fatzero/mass-core/txscript"
"github.com/fatzero/mass-core/wire"
)
const (
// MaxSigOpsPerBlock is the maximum number of signature operations
// allowed for a block. It is a fraction of the max block payload size.
MaxSigOpsPerBlock = wire.MaxBlockPayload / 150 * txscript.MaxPubKeysPerMultiSig
// MaxTimeOffsetSeconds is the maximum number of seconds a block time
// is allowed to be ahead of the current time. This is currently 2
// hours.
MaxTimeOffsetSeconds = 2 * 60 * 60
// medianTimeBlocks is the number of previous blocks which should be
// used to calculate the median time used to validate block timestamps.
medianTimeBlocks = 11
)
var (
// zeroHash is the zero value for a wire.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 = &wire.Hash{}
)
// isNullOutpoint determines whether or not a previous transaction output point
// is set.
func isNullOutpoint(outpoint *wire.OutPoint) bool {
if outpoint.Index == math.MaxUint32 && outpoint.Hash.IsEqual(zeroHash) {
return true
}
return false
}
// IsCoinBaseTx determines whether or not a transaction is a coinbase. A coinbase
// is a special transaction created by miners that has no inputs. This is
// represented in the block chain by a transaction with a single input that has
// a previous output transaction index set to the maximum value along with a
// zero hash.
//
// This function only differs from IsCoinBase in that it works with a raw wire
// transaction as opposed to a higher level util transaction.
func IsCoinBaseTx(msgTx *wire.MsgTx) bool {
// A coin base must only have one transaction input.
if len(msgTx.TxIn) < 1 {
return false
}
// The previous output of a coin base must have a max value index and
// a zero hash.
prevOut := &msgTx.TxIn[0].PreviousOutPoint
if prevOut.Index != math.MaxUint32 || !prevOut.Hash.IsEqual(zeroHash) {
return false
}
return true
}
// IsCoinBase determines whether or not a transaction is a coinbase. A coinbase
// is a special transaction created by miners that has no inputs. This is
// represented in the block chain by a transaction with a single input that has
// a previous output transaction index set to the maximum value along with a
// zero hash.
//
// This function only differs from IsCoinBaseTx in that it works with a higher
// level util transaction as opposed to a raw wire transaction.
func IsCoinBase(tx *massutil.Tx) bool {
return IsCoinBaseTx(tx.MsgTx())
}
func pkToScriptHash(pubKey []byte, net *config.Params) ([]byte, error) {
addressPubKeyHash, err := massutil.NewAddressPubKeyHash(massutil.Hash160(pubKey), net)
if err != nil {
return nil, err
}
return addressPubKeyHash.ScriptAddress(), nil
}
// for poc pk
func pkToRedeemScriptHash(pubkey []byte, net *config.Params) ([]byte, error) {
var addressPubKeyStructs []*massutil.AddressPubKey
addressPubKeyStruct, err := massutil.NewAddressPubKey(pubkey, net)
if err != nil {
return nil, err
}
addressPubKeyStructs = append(addressPubKeyStructs, addressPubKeyStruct)
redeemScript, err := txscript.MultiSigScript(addressPubKeyStructs, 1)
if err != nil {
return nil, err
}
scriptHash := massutil.Hash160(redeemScript)
return scriptHash, nil
}
// SequenceLockActive determines if a transaction's sequence locks have been
// met, meaning that all the inputs of a given transaction have reached a
// height or time sufficient for their relative lock-time maturity.
func SequenceLockActive(sequenceLock *SequenceLock, blockHeight uint64,
medianTimePast time.Time) bool {
// If either the seconds, or height relative-lock time has not yet
// reached, then the transaction is not yet mature according to its
// sequence locks.
if sequenceLock.Seconds >= medianTimePast.Unix() ||
sequenceLock.BlockHeight >= blockHeight {
return false
}
return true
}
// IsFinalizedTransaction determines whether or not a transaction is finalized.
func IsFinalizedTransaction(tx *massutil.Tx, blockHeight uint64, blockTime time.Time) bool {
msgTx := tx.MsgTx()
// Lock time of zero means the transaction is finalized.
lockTime := msgTx.LockTime
if lockTime == 0 {
return true
}
// The lock time field of a transaction is either a block height at
// which the transaction is finalized or a timestamp depending on if the
// value is before the txscript.LockTimeThreshold. When it is under the
// threshold it is a block height.
var blockTimeOrHeight int64
if lockTime < txscript.LockTimeThreshold {
blockTimeOrHeight = int64(blockHeight)
} else {
blockTimeOrHeight = blockTime.Unix()
}
if int64(lockTime) < blockTimeOrHeight {
return true
}
// At this point, the transaction's lock time hasn't occured yet, but
// the transaction might still be finalized if the sequence number
// for all transaction inputs is maxed out.
for _, txIn := range msgTx.TxIn {
if txIn.Sequence != wire.MaxTxInSequenceNum {
return false
}
}
return true
}
// CheckTransactionSanity performs some preliminary checks on a transaction to
// ensure it is sane. These checks are context free.
func CheckTransactionSanity(tx *massutil.Tx) error {
// A transaction must have at least one input.
msgTx := tx.MsgTx()
if len(msgTx.TxIn) == 0 {
return ErrNoTxInputs
}
// A transaction must have at least one output.
if len(msgTx.TxOut) == 0 {
return ErrNoTxOutputs
}
// A transaction must not exceed the maximum allowed block payload when
// serialized.
//witness
// serializedTxSize := tx.MsgTx().PlainSize()
serializedTxSize := tx.MsgTx().PlainSize()
// if serializedTxSize > wire.MaxBlockPayload
if serializedTxSize > wire.MaxBlockPayload {
logging.CPrint(logging.ERROR, "transaction size is too big",
logging.LogFormat{"txSize": serializedTxSize, "txSizeLimit": wire.MaxBlockPayload})
return ErrTxTooBig
}
// Ensure the transaction amounts are in range. Each transaction
// output must not be negative or more than the max allowed per
// transaction. Also, the total of all outputs must abide by the same
// restrictions. All amounts in a transaction are in a unit value known
// as a maxwell. One Mass is a quantity of maxwell as defined by the
// MaxwellPerMass constant.
var err error
totalMaxwell := massutil.ZeroAmount()
for i, txOut := range msgTx.TxOut {
totalMaxwell, err = totalMaxwell.AddInt(txOut.Value)
if err != nil {
logging.CPrint(logging.ERROR, "count total output failed",
logging.LogFormat{
"index": i,
"value": txOut.Value,
"total": totalMaxwell,
"limit": massutil.MaxAmount().Value(),
"err": err,
})
return ErrBadTxOutValue
}
}
// Check for duplicate transaction inputs.
existingTxOut := make(map[wire.OutPoint]struct{})
for _, txIn := range msgTx.TxIn {
if _, exists := existingTxOut[txIn.PreviousOutPoint]; exists {
return ErrDuplicateTxInputs
}
existingTxOut[txIn.PreviousOutPoint] = struct{}{}
}
// Coinbase script length must be between min and max length.
if IsCoinBase(tx) {
for _, txIn := range msgTx.TxIn[1:] {
prevOut := &txIn.PreviousOutPoint
if isNullOutpoint(prevOut) {
return ErrBadTxInput
}
}
} else {
// Previous transaction outputs referenced by the inputs to this
// transaction must not be null.
for _, txIn := range msgTx.TxIn {
prevOut := &txIn.PreviousOutPoint
if isNullOutpoint(prevOut) {
return ErrBadTxInput
}
}
}
return nil
}
// checkProofOfCapacity ensures the block header Target
// is in min/max range and that the block's proof quality is less than the
// Target difficulty as claimed.
func checkProofOfCapacity(header *wire.BlockHeader, pocLimit *big.Int) error {
// match proof type with header version
if !forks.EnforceMASSIP0002(header.Height) && header.Proof.Type() != poc.ProofTypeDefault {
return ErrInvalidProofType
}
// The Target difficulty must be larger than zero.
target := header.Target
if target.Sign() <= 0 {
logging.CPrint(logging.ERROR, "block Target difficulty is too low",
logging.LogFormat{"target": target})
return ErrUnexpectedDifficulty
}
// The Target difficulty must be less than the maximum allowed.
if target.Cmp(pocLimit) < 0 {
logging.CPrint(logging.ERROR, "block Target difficulty is lower than min of pocLimit",
logging.LogFormat{"target": target, "pocLimit": pocLimit})
return ErrUnexpectedDifficulty
}
logging.CPrint(logging.TRACE, "validate: check PoC", logging.LogFormat{
"timestamp": uint64(header.Timestamp.Unix()),
"height": header.Height,
"big_length": header.Proof.BitLength(),
"challenge": header.Challenge,
})
pubKeyHash := pocutil.PubKeyItfHash(header.PubKey)
slot := uint64(header.Timestamp.Unix()) / poc.PoCSlot
quality, err := header.Proof.VerifiedQuality(pubKeyHash, pocutil.Hash(header.Challenge), forks.EnforceMASSIP0002(header.Height), slot, header.Height)
if err != nil {
return err
}
if quality.Cmp(target) < 0 {
logging.CPrint(logging.ERROR, "block's proof quality is lower than expected min target",
logging.LogFormat{"quality": quality, "expected": target, "height": header.Height, "hash": header.BlockHash()})
return ErrLowQuality
}
return nil
}
// CheckProofOfWork ensures the block header bits which indicate the Target
// difficulty is in min/max range and that the block's proof quality is less than the
// Target difficulty as claimed.
func CheckProofOfCapacity(block *massutil.Block, pocLimit *big.Int) error {
return checkProofOfCapacity(&block.MsgBlock().Header, pocLimit)
}
func checkChainID(header *wire.BlockHeader, chainID wire.Hash) error {
if !header.ChainID.IsEqual(&chainID) {
logging.CPrint(logging.ERROR, "block's chainID is not equal to expected chainID",
logging.LogFormat{"block chainID": header.ChainID.String(), "expected": chainID.String()})
return ErrChainID
}
return nil
}
func checkVersion(header *wire.BlockHeader) error {
requiredVersion := forks.GetBlockVersion(header.Height)
if header.Version < requiredVersion {
logging.CPrint(logging.ERROR, "invalid block version",
logging.LogFormat{"err": ErrInvalidBlockVersion, "block_version": header.Version, "required_version": requiredVersion})
return ErrInvalidBlockVersion
}
return nil
}
func checkHeaderTimestamp(header *wire.BlockHeader) error {
// A block timestamp must not have a greater precision than one second.
// This check is necessary because Go time.Time values support
// nanosecond precision whereas the consensus rules only apply to
// seconds and it's much nicer to deal with standard Go time values
// instead of converting to seconds everywhere.
if !header.Timestamp.Equal(time.Unix(header.Timestamp.Unix(), 0)) {
logging.CPrint(logging.ERROR, "block timestamp has a higher precision the one second",
logging.LogFormat{"timestamp": header.Timestamp})
return ErrInvalidTime
}
allowed := time.Now().Add(3 * time.Second)
if allowed.Before(header.Timestamp) {
logging.CPrint(logging.ERROR, "block timestamp of unix is too far in the future",
logging.LogFormat{
"allowed": allowed.Unix(),
"timestamp_unix": header.Timestamp.Unix(),
"timestamp": header.Timestamp.Format(time.RFC3339),
"height": header.Height,
"block": header.BlockHash(),
})
return ErrTimeTooNew
}
return nil
}
func checkHeaderBanList(header *wire.BlockHeader) error {
dupPk := make(map[string]struct{})
hpk := header.PublicKey().SerializeCompressed()
for _, bpk := range header.BannedPublicKeys() {
if bytes.Equal(hpk, bpk.SerializeCompressed()) {
logging.CPrint(logging.ERROR, "block's pubKey is banned in header banList",
logging.LogFormat{"pubkey": hex.EncodeToString(hpk)})
return ErrBanSelfPk
}
strPk := hex.EncodeToString(bpk.SerializeCompressed())
if _, exists := dupPk[strPk]; exists {
logging.CPrint(logging.ERROR, "duplicate pubKey in header banList")
return ErrBanList
}
dupPk[strPk] = struct{}{}
}
return nil
}
// checkHeaderSignature checks the signature in blockHeader
func checkHeaderSignature(header *wire.BlockHeader) error {
if header.Signature == nil || header.PubKey == nil {
logging.CPrint(logging.ERROR, "block signature verify failed for nil sig or pk")
return ErrBlockSIG
}
correct, err := header.VerifySig()
if err != nil {
return err
}
if !correct {
logging.CPrint(logging.ERROR, "block signature verify failed")
return ErrBlockSIG
}
return nil
}
// CountSigOps returns the number of signature operations for all transaction
//// input and output scripts in the provided transaction. This uses the
//// quicker, but imprecise, signature operation counting mechanism from
//// txscript.
func CountSigOps(tx *massutil.Tx) int {
msgTx := tx.MsgTx()
if IsCoinBaseTx(msgTx) {
return 0
}
totalSigOps := 0
for _, txIn := range msgTx.TxIn {
numSigOps := txscript.GetSigOpCount(txIn.Witness[len(txIn.Witness)-1])
totalSigOps += numSigOps
}
//}
// Accumulate the number of signature operations in all transaction
// inputs.
// Accumulate the number of signature operations in all transaction
// outputs.
for _, txOut := range msgTx.TxOut {
numSigOps := txscript.GetSigOpCount(txOut.PkScript)
totalSigOps += numSigOps
//log.Warn("the numsig is :",totalSigOps)
}
return totalSigOps
}
// checkBlockHeaderSanity performs some preliminary checks on a block header to
// ensure it is sane before continuing with processing. These checks are
// context free.
//
// The flags do not modify the behavior of this function directly, however they
// are needed to pass along to checkProofOfWork.
func checkBlockHeaderSanity(header *wire.BlockHeader, chainID wire.Hash, pocLimit *big.Int, flags BehaviorFlags) (err error) {
err = checkChainID(header, chainID)
if err != nil {
return
}
err = checkVersion(header)
if err != nil {
return
}
err = checkHeaderTimestamp(header)
if err != nil {
return
}
err = checkHeaderBanList(header)
if err != nil {
return
}
err = checkProofOfCapacity(header, pocLimit)
if err != nil {
return err
}
err = checkHeaderSignature(header)
if err != nil {
return err
}
return nil
}
// checkBlockSanity performs some preliminary checks on a block to ensure it is
// sane before continuing with block processing. These checks are context free.
func checkBlockSanity(block *massutil.Block, chainID wire.Hash, pocLimit *big.Int, flags BehaviorFlags) error {
msgBlock := block.MsgBlock()
header := &msgBlock.Header
proposals := &msgBlock.Proposals
if !flags.isFlagSet(BFNoPoCCheck) {
if err := checkBlockHeaderSanity(header, chainID, pocLimit, flags); err != nil {
return err
}
}
if err := checkBlockProposalSanity(proposals, header, chainID); err != nil {
return err
}
// A block must have at least one transaction.
numTx := len(msgBlock.Transactions)
if numTx == 0 {
return errBlockNoTransactions
}
// Checks that coinbase height matches block header height.
if err := CheckCoinbaseHeight(block); err != nil {
return err
}
// A block must not have more transactions than the max block payload.
if numTx > wire.MaxTxPerBlock {
logging.CPrint(logging.ERROR, "block contains too many transactions",
logging.LogFormat{"numTx": numTx, "MaxTxPerBlock": wire.MaxTxPerBlock})
return ErrTooManyTransactions
}
// A block must not exceed the maximum allowed block payload when
// serialized.
//serializedSize := msgBlock.PlainSize()
serializedSize := msgBlock.PlainSize()
//if serializedSize > wire.MaxBlockPayload
if serializedSize > wire.MaxBlockPayload {
logging.CPrint(logging.ERROR, "serialized block is too big",
logging.LogFormat{"serializedSize": serializedSize, "MaxBlockPayload": wire.MaxBlockPayload})
return ErrBlockTooBig
}
// ProposalRoot check
proposalMerkles := wire.BuildMerkleTreeStoreForProposal(&block.MsgBlock().Proposals)
calculatedProposalRoot := proposalMerkles[len(proposalMerkles)-1]
if !header.ProposalRoot.IsEqual(calculatedProposalRoot) {
logging.CPrint(logging.ERROR, "block proposal root is invalid",
logging.LogFormat{"header.ProposalRoot": header.ProposalRoot, "calculate": calculatedProposalRoot})
return ErrInvalidProposalRoot
}
// The first transaction in a block must be a coinbase.
transactions := block.Transactions()
if !IsCoinBase(transactions[0]) {
return ErrFirstTxNotCoinbase
}
// A block must not have more than one coinbase.
for i, tx := range transactions[1:] {
if IsCoinBase(tx) {
logging.CPrint(logging.ERROR, "block contains other coinbase",
logging.LogFormat{"hindex": i})
return ErrMultipleCoinbases
}
}
// Do some preliminary checks on each transaction to ensure they are
// sane before continuing.
for _, tx := range transactions {
err := CheckTransactionSanity(tx)
if err != nil {
return err
}
}
// Build merkle tree and ensure the calculated merkle root matches the
// entry in the block header. This also has the effect of caching all
// of the transaction hashes in the block to speed up future hash
// checks. Massd builds the tree here and checks the merkle root
// after the following checks, but there is no reason not to check the
// merkle root matches here.
merkles := wire.BuildMerkleTreeStoreTransactions(block.MsgBlock().Transactions, false)
calculatedMerkleRoot := merkles[len(merkles)-1]
if !header.TransactionRoot.IsEqual(calculatedMerkleRoot) {
logging.CPrint(logging.ERROR, "block merkle root is invalid",
logging.LogFormat{"header.TransactionRoot": header.TransactionRoot, "calculate": calculatedMerkleRoot})
return ErrInvalidMerkleRoot
}
witnessMerkles := wire.BuildMerkleTreeStoreTransactions(block.MsgBlock().Transactions, true)
witnessMerkleRoot := witnessMerkles[len(witnessMerkles)-1]
if !header.WitnessRoot.IsEqual(witnessMerkleRoot) {
logging.CPrint(logging.ERROR, "block witness merkle root is invalid",
logging.LogFormat{"header.WitnessRoot": header.WitnessRoot, "calculate": witnessMerkleRoot})
return ErrInvalidMerkleRoot
}
// Check for duplicate transactions. This check will be fairly quick
// since the transaction hashes are already cached due to building the
// merkle tree above.
existingTxHashes := make(map[wire.Hash]struct{})
for i, tx := range transactions {
hash := tx.Hash()
if _, exists := existingTxHashes[*hash]; exists {
logging.CPrint(logging.ERROR, "block contains duplicate transaction",
logging.LogFormat{"transaction": hash, "index": i})
return ErrDuplicateTx
}
existingTxHashes[*hash] = struct{}{}
}
// The number of signature operations must be less than the maximum
// allowed per block.
totalSigOps := 0
for _, tx := range transactions {
// We could potentially overflow the accumulator so check for
// overflow.
lastSigOps := totalSigOps
//witness-totalSigOps += CountSigOps(tx)
totalSigOps += CountSigOps(tx)
if totalSigOps < lastSigOps || totalSigOps > MaxSigOpsPerBlock {
logging.CPrint(logging.ERROR, "block contains too many signature operations",
logging.LogFormat{"totalSigOps": totalSigOps, "maxSigOps": MaxSigOpsPerBlock})
return ErrTooManySigOps
}
}
return nil
}
// CheckBlockSanity performs some preliminary checks on a block to ensure it is
// sane before continuing with block processing. These checks are context free.
func CheckBlockSanity(block *massutil.Block, chainID wire.Hash, pocLimit *big.Int) error {
return checkBlockSanity(block, chainID, pocLimit, BFNone)
}
// checkBlockHeaderContext peforms several validation checks on the block header
// which depend on its position within the block chain.
func (chain *Blockchain) checkBlockHeaderContext(header *wire.BlockHeader, prevNode *BlockNode, flags BehaviorFlags) error {
// The genesis block is valid by definition.
if prevNode == nil {
return nil
}
// pk has been banned
isBanned, err := chain.dmd.isPubKeyBanned(prevNode, header.PublicKey())
if err != nil {
return err
}
if isBanned {
logging.CPrint(logging.ERROR, "block builder pubkey has been banned",
logging.LogFormat{"pubkey": hex.EncodeToString(header.PublicKey().SerializeCompressed())})
return ErrBannedPk
}
// Ensure Target
expectedTarget, err := calcNextTarget(prevNode, header.Timestamp, chain.chainParams)
if err != nil {
return err
}
blockDifficulty := header.Target
if blockDifficulty.Cmp(expectedTarget) != 0 {
logging.CPrint(logging.ERROR, "block difficulty is not the expected value",
logging.LogFormat{"difficulty": blockDifficulty, "expectedTarget": expectedTarget})
return ErrUnexpectedDifficulty
}
blockHeight := prevNode.Height + 1
// Ensure the header BlockHeight matches height calculated in BlockNode.
if blockHeight != header.Height {
logging.CPrint(logging.ERROR, "block height does not match the expected height",
logging.LogFormat{"block Height": header.Height, "expected Height": blockHeight})
return ErrBadBlockHeight
}
// Ensure chain matches up to predetermined checkpoints.
blockHash := header.BlockHash()
if !chain.verifyCheckpoint(blockHeight, &blockHash) {
return fmt.Errorf("%s: block at height %d does not match checkpoint hash", ErrBadCheckpoint, blockHeight)
}
// Find the previous checkpoint and prevent blocks which fork the main
// chain before it. This prevents storage of new, otherwise valid,
// blocks which build off of old blocks that are likely at a much easier
// difficulty and therefore could be used to waste cache and disk space.
checkpointNode, err := chain.findPreviousCheckpoint()
if err != nil {
return err
}
if checkpointNode != nil && blockHeight < checkpointNode.Height {
return fmt.Errorf("%s: block at height %d forks the main chain before the previous checkpoint at height %d",
ErrForkTooOld, blockHeight, checkpointNode.Height)
}
// Ensure the provided challenge in header is right.
// The calculated challenge based on some rules.
challenge, err := calcNextChallenge(prevNode)
if err != nil {
return err
}
if !challenge.IsEqual(&header.Challenge) {
logging.CPrint(logging.ERROR, "block challenge does not match the expected challenge",
logging.LogFormat{"block challenge": header.Challenge, "blockHeight": blockHeight, "expectedChallenge": challenge})
return ErrUnexpectedChallenge
}
// Ensure the timestamp for the block header is after its
// preNode's header timestamp
if header.Timestamp.Unix()/poc.PoCSlot <= prevNode.Timestamp.Unix()/poc.PoCSlot {
logging.CPrint(logging.ERROR, "block timestamp is not after expected prevNode",
logging.LogFormat{"header timestamp": header.Timestamp, "prevNode timestamp": prevNode.Timestamp})
return ErrTimeTooOld
}
return nil
}
// checkBlockContext peforms several validation checks on the block which depend
// on its position within the block chain.
//
// The flags modify the behavior of this function as follows:
// - BFFastAdd: The transaction are not checked to see if they are finalized
// and the somewhat expensive BIP0034 validation is not performed.
//
// The flags are also passed to checkBlockHeaderContext. See its documentation
// for how the flags modify its behavior.
func (chain *Blockchain) checkBlockContext(block *massutil.Block, prevNode *BlockNode, flags BehaviorFlags) error {
// The genesis block is valid by definition.
if prevNode == nil {
return nil
}
// Perform all block header related validation checks.
header := &block.MsgBlock().Header
if !flags.isFlagSet(BFNoPoCCheck) {
err := chain.checkBlockHeaderContext(header, prevNode, flags)
if err != nil {
return err
}
}
err := chain.checkProposalContext(header.BannedPublicKeys(), prevNode)
if err != nil {
return err
}
blockTime, err := chain.calcPastMedianTime(prevNode)
if err != nil {
return err
}
// Ensure all transactions in the block are finalized.
for _, tx := range block.Transactions() {
if !IsFinalizedTransaction(tx, block.Height(), blockTime) {
logging.CPrint(logging.ERROR, "block contains unfinalized transaction", logging.LogFormat{"tx": tx.Hash(), "block": block.Hash()})
return errUnFinalizedTx
}
}
return nil
}
// CheckCoinbaseHeight checks whether block height in coinbase matches block
// height in header. We do not check *block's existence because this func
// is called in another func that *block exists.
func CheckCoinbaseHeight(block *massutil.Block) error {
coinbaseTx := block.Transactions()[0]
blockHeight := block.MsgBlock().Header.Height
return checkSerializedHeight(coinbaseTx, blockHeight)
}
// extractCoinbaseHeight attempts to extract the height of the block from
// coinbase payload
func extractCoinbaseHeight(coinbaseTx *massutil.Tx) (uint64, error) {
payload := coinbaseTx.MsgTx().Payload
if len(payload) < 8 {
return 0, errIncompleteCoinbasePayload
}
return binary.LittleEndian.Uint64(payload[:8]), nil
}
// extractCoinbaseHeight attempts to extract the number of lock reward
// of current block from coinbase payload
func extractCoinbaseStakingRewardNumber(coinbaseTx *massutil.Tx) (uint32, error) {
payload := coinbaseTx.MsgTx().Payload
if len(payload) < 12 {
return 0, errIncompleteCoinbasePayload
}
return binary.LittleEndian.Uint32(payload[8:12]), nil
}
// checkSerializedHeight checks if the signature script in the passed
// transaction starts with the serialized block height of wantHeight.
func checkSerializedHeight(coinbaseTx *massutil.Tx, wantHeight uint64) error {
serializedHeight, err := extractCoinbaseHeight(coinbaseTx)
if err != nil {
return err
}
if serializedHeight != wantHeight {
logging.CPrint(logging.ERROR, "the coinbase payload serialized block height does not equal expected height",
logging.LogFormat{"serializedHeight": serializedHeight, "wantHeight": wantHeight})
return ErrBadCoinbaseHeight
}
return nil
}
// isTransactionSpent returns whether or not the provided transaction data
// describes a fully spent transaction. A fully spent transaction is one where
// all outputs have been spent.
func isTransactionSpent(txD *TxData) bool {
for _, isOutputSpent := range txD.Spent {
if !isOutputSpent {
return false
}
}
return true
}
// checkDupTx ensures blocks do not contain duplicate transactions which
// 'overwrite' older transactions that are not fully spent. This prevents an
// attack where a coinbase and all of its dependent transactions could be
// duplicated to effectively revert the overwritten transactions to a single
// confirmation thereby making them vulnerable to a double spend.
func (chain *Blockchain) checkDupTx(node *BlockNode, block *massutil.Block) error {
// Attempt to fetch duplicate transactions for all of the transactions
// in this block from the point of view of the Parent node.
fetchSet := make(map[wire.Hash]struct{})
for _, tx := range block.Transactions() {
fetchSet[*tx.Hash()] = struct{}{}
}
txResults, err := chain.fetchTxStore(node, fetchSet)
if err != nil {
return err
}
// Examine the resulting data about the requested transactions.
for _, txD := range txResults {
switch txD.Err {
// A duplicate transaction was not found. This is the most
// common case.
case database.ErrTxShaMissing:
continue
// A duplicate transaction was found. This is only allowed if
// the duplicate transaction is fully spent.
case nil:
if !isTransactionSpent(txD) {
logging.CPrint(logging.ERROR, "tried to overwrite not fully spent transaction",
logging.LogFormat{"transaction ": txD.Hash, "block height": txD.BlockHeight})
return ErrOverwriteTx
}
// Some other unexpected error occurred. Return it now.
default:
return txD.Err
}
}
return nil
}
func checkDupSpend(preOutPoint wire.OutPoint, spent []bool) error {
if preOutPoint.Index >= uint32(len(spent)) {
logging.CPrint(logging.ERROR, "out of bounds input index in referenced transaction",
logging.LogFormat{"originTx": preOutPoint.Hash, "input index": preOutPoint.Index, "spent length": len(spent)})
return ErrBadTxInput
}
if spent[preOutPoint.Index] {
logging.CPrint(logging.ERROR, "transaction tried to double spend output",
logging.LogFormat{"originTx": preOutPoint.Hash, "input index": preOutPoint.Index})
return ErrDoubleSpend
}
return nil
}
// checkTxInMaturity ensures the transaction is not spending coins which have not
// yet reached the required coinbase maturity.
func checkTxInMaturity(txData *TxData, txHeight uint64, preOutPoint wire.OutPoint, isCoinbase bool) error {
blocksSincePrev := uint64(0)
if txHeight > txData.BlockHeight {
blocksSincePrev = txHeight - txData.BlockHeight
}
if IsCoinBase(txData.Tx) {
if blocksSincePrev < consensus.CoinbaseMaturity {
logging.CPrint(logging.WARN, "try to spend immature coinbase",
logging.LogFormat{
"next block height": txHeight,
"txIn height": txData.BlockHeight,
"coinbase maturity": consensus.CoinbaseMaturity,
"txInHash": preOutPoint.Hash,
"txInIndex": preOutPoint.Index,
})
return ErrImmatureSpend
}
return nil
}
if isCoinbase {
if blocksSincePrev < consensus.TransactionMaturity {
logging.CPrint(logging.ERROR, "try to spend immature transaction",
logging.LogFormat{
"next block height": txHeight,
"txIn height": txData.BlockHeight,
"transactions maturity": consensus.TransactionMaturity,
"txInHash": preOutPoint.Hash,
"txInIndex": preOutPoint.Index,
})
return ErrImmatureSpend
}
}
return nil
}
// CheckTransactionInputs performs a series of checks on the inputs to a
// transaction to ensure they are valid. An example of some of the checks
// include verifying all inputs exist, ensuring the coinbase seasoning
// requirements are met, detecting double spends, validating all values and fees
// are in the legal range and the total output amount doesn't exceed the input
// amount, and verifying the signatures to prove the spender was the owner of
// the masses and therefore allowed to spend them. As it checks the inputs,
// it also calculates the total fees for the transaction and returns that value.
func CheckTransactionInputs(tx *massutil.Tx, txHeight uint64, txStore TxStore) (massutil.Amount, error) {
// Coinbase transactions have no inputs.
if IsCoinBase(tx) {
for i, txIn := range tx.MsgTx().TxIn {
if txIn.Witness.PlainSize() != 0 {
logging.CPrint(logging.ERROR, "coinbaseTx txIn`s witness size must be 0",
logging.LogFormat{"index ": i, "size": txIn.Witness.PlainSize()})
return massutil.ZeroAmount(), ErrCoinbaseTxInWitness
}
}
return massutil.ZeroAmount(), nil
}
txHash := tx.Hash()
totalMaxwellIn := massutil.ZeroAmount()
for _, txIn := range tx.MsgTx().TxIn {
// Ensure the input is available.
txInHash := &txIn.PreviousOutPoint.Hash
originTxIndex := txIn.PreviousOutPoint.Index
originTx, exists := txStore[*txInHash]
if !exists || originTx.Err != nil || originTx.Tx == nil {
logging.CPrint(logging.ERROR, "unable to find input transaction",
logging.LogFormat{"input transaction ": txInHash, "transaction": txHash})
return massutil.ZeroAmount(), ErrMissingTx
}
// Ensure the transaction is not spending coins which have not
// yet reached the required coinbase maturity.
err := checkTxInMaturity(originTx, txHeight, txIn.PreviousOutPoint, false)
if err != nil {
return massutil.ZeroAmount(), err
}
// Ensure the transaction is not double spending coins.
err = checkDupSpend(txIn.PreviousOutPoint, originTx.Spent)
if err != nil {
return massutil.ZeroAmount(), err
}
// Ensure the transaction amounts are in range. Each of the
// output values of the input transactions must not be negative
// or more than the max allowed per transaction. All amounts in
// a transaction are in a unit value known as a maxwell. One
// mass is a quantity of maxwell as defined by the
// MaxwellPerMass constant.
originTxMaxwell, err := massutil.NewAmountFromInt(originTx.Tx.MsgTx().TxOut[originTxIndex].Value)
if err != nil {
logging.CPrint(logging.ERROR, "invalid input value",
logging.LogFormat{
"prevTx": txInHash.String(),
"prevIndex": originTxIndex,
"value": originTx.Tx.MsgTx().TxOut[originTxIndex].Value,
"err": err,
})
return massutil.ZeroAmount(), err
}
totalMaxwellIn, err = totalMaxwellIn.Add(originTxMaxwell)
if err != nil {
logging.CPrint(logging.ERROR, "calc total input value error",
logging.LogFormat{
"tx": tx.MsgTx().TxHash().String(),
"height": txHeight,
"err": err,
})
return massutil.ZeroAmount(), err
}
// Mark the referenced output as spent.
originTx.Spent[originTxIndex] = true
}
// Calculate the total output amount for this transaction. It is safe
// to ignore overflow and out of range errors here because those error
// conditions would have already been caught by checkTransactionSanity.
totalMaxwellOut := massutil.ZeroAmount()
for _, txOut := range tx.MsgTx().TxOut {
v, err := massutil.NewAmountFromInt(txOut.Value)
if err != nil {
logging.CPrint(logging.ERROR, "invalid output value",
logging.LogFormat{
"tx": tx.MsgTx().TxHash().String(),
"height": txHeight,
"value": txOut.Value,
"err": err,
})
return massutil.ZeroAmount(), err
}
totalMaxwellOut, err = totalMaxwellOut.Add(v)
if err != nil {
logging.CPrint(logging.ERROR, "calc total output value error",
logging.LogFormat{
"tx": tx.MsgTx().TxHash().String(),
"height": txHeight,
"err": err,
})
return massutil.ZeroAmount(), err
}
}
return totalMaxwellIn.Sub(totalMaxwellOut)
}
func (chain *Blockchain) checkConnectBlock(node *BlockNode, block *massutil.Block, flags BehaviorFlags, reorgBindingState state.Trie) error {
// The coinbase for the Genesis block is not spendable, so just return
// an error now.
if node.Hash.IsEqual(chain.chainParams.GenesisHash) {
return ErrConnectGenesis
}
// Have to prevent blocks which contain duplicate