-
Notifications
You must be signed in to change notification settings - Fork 33
/
appender.go
1188 lines (1114 loc) · 43.3 KB
/
appender.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 state
import (
"fmt"
"github.com/mr-tron/base58/base58"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/wavesplatform/gowaves/pkg/crypto"
"github.com/wavesplatform/gowaves/pkg/errs"
"github.com/wavesplatform/gowaves/pkg/proto"
"github.com/wavesplatform/gowaves/pkg/settings"
"github.com/wavesplatform/gowaves/pkg/types"
)
var errBlockSnapshotStateHashMismatch = errors.New("block snapshot state hash differs from the calculated one")
type blockInfoProvider interface {
NewestBlockInfoByHeight(height proto.Height) (*proto.BlockInfo, error)
}
type txAppender struct {
sc *scriptCaller
ia *invokeApplier
ethTxKindResolver proto.EthereumTransactionKindResolver
rw *blockReadWriter
blockInfoProvider blockInfoProvider
atx *addressTransactions
stor *blockchainEntitiesStorage
settings *settings.BlockchainSettings
// TransactionHandler is handler for any operations on transactions.
txHandler *transactionHandler
// Block differ is used to create diffs from blocks.
blockDiffer *blockDiffer
// Storage for diffs of incoming transactions (from added blocks or UTX).
// It will be used for validation and applying diffs to existing balances.
diffStor *diffStorage
// diffStorInvoke is storage for partial diffs generated by Invoke transactions.
// It is used to calculate balances that take into account intermediate invoke changes for RIDE.
diffStorInvoke *diffStorageWrapped
// Ids of all transactions whose diffs are currently in diffStor.
// This is needed to check that transaction ids are unique.
recentTxIds map[string]struct{}
// diffApplier is used to both validate and apply balance diffs.
diffApplier *diffApplier
// totalScriptsRuns counts script runs in block / UTX.
totalScriptsRuns uint64
// buildApiData flag indicates that additional data for API is built when
// appending transactions.
buildApiData bool
}
func newTxAppender(
state types.SmartState,
rw *blockReadWriter,
stor *blockchainEntitiesStorage,
settings *settings.BlockchainSettings,
stateDB *stateDB,
atx *addressTransactions,
snapshotApplier *blockSnapshotsApplier,
) (*txAppender, error) {
buildAPIData, err := stateDB.stateStoresApiData()
if err != nil {
return nil, err
}
sc, err := newScriptCaller(state, stor, settings)
if err != nil {
return nil, err
}
genesis := settings.Genesis
txHandler, err := newTransactionHandler(genesis.BlockID(), stor, settings, snapshotApplier, buildAPIData)
if err != nil {
return nil, err
}
blockDiffer, err := newBlockDiffer(txHandler, stor, settings)
if err != nil {
return nil, err
}
diffStor, err := newDiffStorage()
if err != nil {
return nil, err
}
diffStorInvoke, err := newDiffStorageWrapped(diffStor)
if err != nil {
return nil, err
}
diffApplier, err := newDiffApplier(stor.balances, settings.AddressSchemeCharacter)
if err != nil {
return nil, err
}
ia := newInvokeApplier(state, sc, txHandler, stor, settings, blockDiffer, diffStorInvoke, diffApplier)
ethKindResolver := proto.NewEthereumTransactionKindResolver(state, settings.AddressSchemeCharacter)
return &txAppender{
sc: sc,
ia: ia,
rw: rw,
blockInfoProvider: state,
atx: atx,
stor: stor,
settings: settings,
txHandler: txHandler,
blockDiffer: blockDiffer,
recentTxIds: make(map[string]struct{}),
diffStor: diffStor,
diffStorInvoke: diffStorInvoke,
diffApplier: diffApplier,
buildApiData: buildAPIData,
ethTxKindResolver: ethKindResolver,
}, nil
}
func (a *txAppender) checkDuplicateTxIdsImpl(id []byte, recentIds map[string]struct{}) error {
// Check recent.
if _, ok := recentIds[string(id)]; ok {
return proto.NewInfoMsg(errors.Errorf("transaction with ID %s already in state", base58.Encode(id)))
}
// Check DB.
if _, _, err := a.rw.readTransaction(id); err == nil {
return proto.NewInfoMsg(errors.Errorf("transaction with ID %s already in state", base58.Encode(id)))
}
return nil
}
func (a *txAppender) checkDuplicateTxIds(tx proto.Transaction, recentIds map[string]struct{}, timestamp uint64) error {
if tx.GetTypeInfo().Type == proto.PaymentTransaction {
// Payment transactions are deprecated.
return nil
}
if tx.GetTypeInfo().Type == proto.CreateAliasTransaction {
if (timestamp >= a.settings.StolenAliasesWindowTimeStart) && (timestamp <= a.settings.StolenAliasesWindowTimeEnd) {
// At this period alias transactions might have duplicate IDs due to bugs in historical blockchain.
return nil
}
}
txID, err := tx.GetID(a.settings.AddressSchemeCharacter)
if err != nil {
return err
}
err = a.checkDuplicateTxIdsImpl(txID, recentIds)
if err != nil {
if tx.GetTypeInfo().Type == proto.CreateAliasTransaction {
return errs.NewAliasTaken(err.Error())
}
}
return err
}
type appendBlockParams struct {
transactions []proto.Transaction
chans *verifierChans
block, parent *proto.BlockHeader
blockchainHeight proto.Height
fixSnapshotsToInitialHash []proto.AtomicSnapshot
lastSnapshotStateHash crypto.Digest
optionalSnapshot *proto.BlockSnapshot
}
func (a *txAppender) orderIsScripted(order proto.Order) (bool, error) {
return a.txHandler.tc.orderScriptedAccount(order)
}
// For UTX validation, this returns the last stable block, which is in fact current block.
func (a *txAppender) currentBlock() (*proto.BlockHeader, error) {
curBlockHeight := a.rw.addingBlockHeight()
curHeader, err := a.rw.readNewestBlockHeaderByHeight(curBlockHeight)
if err != nil {
return nil, err
}
return curHeader, nil
}
func (a *txAppender) currentBlockInfo() (*proto.BlockInfo, error) {
curBlockHeight := a.rw.addingBlockHeight()
return a.blockInfoProvider.NewestBlockInfoByHeight(curBlockHeight)
}
func (a *txAppender) checkProtobufVersion(tx proto.Transaction, blockV5Activated bool) error {
if !proto.IsProtobufTx(tx) {
return nil
}
if !blockV5Activated {
return errors.Errorf("bad transaction version %v before blockV5 activation", tx.GetVersion())
}
return nil
}
func (a *txAppender) checkTxFees(tx proto.Transaction, info *fallibleValidationParams) error {
var (
feeChanges txBalanceChanges
err error
)
di := newDifferInfo(info.blockInfo)
switch tx.GetTypeInfo().Type {
case proto.ExchangeTransaction:
feeChanges, err = a.txHandler.td.createDiffForExchangeFeeValidation(tx, di)
if err != nil {
return err
}
case proto.InvokeScriptTransaction:
feeChanges, err = a.txHandler.td.createFeeDiffInvokeScriptWithProofs(tx, di)
if err != nil {
return err
}
case proto.InvokeExpressionTransaction:
feeChanges, err = a.txHandler.td.createFeeDiffInvokeExpressionWithProofs(tx, di)
if err != nil {
return err
}
// TODO handle ethereum invoke expression tx
case proto.EthereumMetamaskTransaction:
feeChanges, err = a.txHandler.td.createFeeDiffEthereumInvokeScriptWithProofs(tx, di)
if err != nil {
return err
}
default:
return errors.Errorf("failed to check tx fees: wrong tx type=%d (%T)", tx.GetTypeInfo().Type, tx)
}
return a.diffApplier.validateTxDiff(feeChanges.diff, a.diffStor)
}
// This function is used for script validation of transaction that can't fail.
func (a *txAppender) checkTransactionScripts(tx proto.Transaction, accountScripted bool, params *appendTxParams) (uint64, txCheckerData, error) {
scriptsRuns := uint64(0)
if accountScripted {
// Check script.
if err := a.sc.callAccountScriptWithTx(tx, params); err != nil {
return 0, txCheckerData{}, errs.Extend(err, "callAccountScriptWithTx")
}
scriptsRuns++
}
// Check against state.
checkerData, err := a.txHandler.checkTx(tx, params.checkerInfo)
if err != nil {
return 0, txCheckerData{}, err
}
txSmartAssets := checkerData.smartAssets
ride4DAppsActivated, err := a.stor.features.newestIsActivated(int16(settings.Ride4DApps))
if err != nil {
return 0, txCheckerData{}, errs.Extend(err, "isActivated")
}
for _, smartAsset := range txSmartAssets {
// Check smart asset's script.
r, err := a.sc.callAssetScript(tx, smartAsset, params)
if err != nil {
return 0, txCheckerData{}, errs.Extend(err, "callAssetScript")
}
if !r.Result() {
return 0, txCheckerData{}, errs.Extend(errors.New("negative asset script result"), "callAssetScript")
}
if tx.GetTypeInfo().Type == proto.SetAssetScriptTransaction && !ride4DAppsActivated {
// Exception: don't count before Ride4DApps activation.
continue
}
scriptsRuns++
}
return scriptsRuns, checkerData, nil
}
func (a *txAppender) checkScriptsLimits(scriptsRuns uint64, blockID proto.BlockID) error {
smartAccountsActivated, err := a.stor.features.newestIsActivated(int16(settings.SmartAccounts))
if err != nil {
return err
}
ride4DAppsActivated, err := a.stor.features.newestIsActivated(int16(settings.Ride4DApps))
if err != nil {
return err
}
if ride4DAppsActivated {
rideV5Activated, err := a.stor.features.newestIsActivated(int16(settings.RideV5))
if err != nil {
return errors.Wrapf(err, "failed to check if feature %d is activated", settings.RideV5)
}
maxBlockComplexity := NewMaxScriptsComplexityInBlock().GetMaxScriptsComplexityInBlock(rideV5Activated)
if a.sc.getTotalComplexity() > uint64(maxBlockComplexity) {
rideV6Activated, err := a.stor.features.newestIsActivated(int16(settings.RideV6))
if err != nil {
return errors.Wrapf(err, "failed to check if feature %d is activated", settings.RideV6)
}
if rideV6Activated {
return errors.Errorf("complexity of scripts (%d) in block '%s' exceeds limit of %d",
a.sc.getTotalComplexity(), blockID.String(), maxBlockComplexity,
)
}
zap.S().Warnf("Complexity of scripts (%d) in block '%s' exceeds limit of %d",
a.sc.getTotalComplexity(), blockID.String(), maxBlockComplexity,
)
}
return nil
} else if smartAccountsActivated {
if scriptsRuns > maxScriptsRunsInBlock {
return errors.Errorf("more scripts runs in block than allowed: %d > %d", scriptsRuns, maxScriptsRunsInBlock)
}
}
return nil
}
func (a *txAppender) needToCheckOrdersSignatures(transaction proto.Transaction) (bool, bool, error) {
tx, ok := transaction.(proto.Exchange)
if !ok {
return false, false, nil
}
o1Scripted, err := a.orderIsScripted(tx.GetOrder1())
if err != nil {
return false, false, err
}
o2Scripted, err := a.orderIsScripted(tx.GetOrder2())
if err != nil {
return false, false, err
}
return !o1Scripted, !o2Scripted, nil
}
func (a *txAppender) saveTransactionIdByAddresses(addresses []proto.WavesAddress, txID []byte, blockID proto.BlockID) error {
for _, addr := range addresses {
if err := a.atx.saveTxIdByAddress(addr, txID, blockID); err != nil {
return err
}
}
return nil
}
func (a *txAppender) commitTxApplication(
tx proto.Transaction,
params *appendTxParams,
invocationRes *invocationResult,
applicationRes *applicationResult) (txSnapshot, error) {
// Add transaction ID to recent IDs.
txID, err := tx.GetID(a.settings.AddressSchemeCharacter)
if err != nil {
return txSnapshot{}, wrapErr(TxCommitmentError, errors.Errorf("failed to get tx id: %v", err))
}
a.recentTxIds[string(txID)] = empty
// Update script runs.
a.totalScriptsRuns += applicationRes.totalScriptsRuns
// Update complexity.
a.sc.addRecentTxComplexity()
// Save balance diff.
if err = a.diffStor.saveTxDiff(applicationRes.changes.diff); err != nil {
return txSnapshot{}, wrapErr(TxCommitmentError, errors.Errorf("failed to save balance diff: %v", err))
}
var (
pi = newPerformerInfo(
params.checkerInfo.blockchainHeight,
params.checkerInfo.blockID,
proto.MustAddressFromPublicKey(a.settings.AddressSchemeCharacter, params.currentMinerPK),
applicationRes.checkerData,
)
applicationStatus = applicationRes.status
)
balanceChanges := applicationRes.changes.diff.balancesChanges()
err = a.diffApplier.validateBalancesChanges(balanceChanges)
if err != nil {
return txSnapshot{}, err
}
a.diffStor.reset()
snapshot, err := a.txHandler.performTx(tx, pi, params.validatingUtx, invocationRes, applicationStatus, balanceChanges)
if err != nil {
return txSnapshot{}, wrapErr(TxCommitmentError,
errors.Wrapf(err, "failed to perform transaction %q", base58.Encode(txID)),
)
}
if !params.validatingUtx {
// Count tx fee. This should not affect transaction execution. It only accumulates miner fee.
if err := a.blockDiffer.countMinerFee(tx); err != nil {
return txSnapshot{}, wrapErr(TxCommitmentError, errors.Errorf("failed to count miner fee: %v", err))
}
}
return snapshot, nil
}
func (a *txAppender) verifyWavesTxSigAndData(tx proto.Transaction, params *appendTxParams, accountHasVerifierScript bool) error {
// Detect what signatures must be checked for this transaction.
// For transaction with SmartAccount we don't check signature.
checkTxSig := !accountHasVerifierScript
checkOrder1, checkOrder2, err := a.needToCheckOrdersSignatures(tx)
if err != nil {
return err
}
if checkSequentially := params.validatingUtx; checkSequentially {
vp := proto.TransactionValidationParams{
Scheme: a.settings.AddressSchemeCharacter,
CheckVersion: params.lightNodeActivated,
}
// In UTX it is not very useful to check signatures in separate goroutines,
// because they have to be checked in each validateNextTx() anyway.
return checkTx(tx, checkTxSig, checkOrder1, checkOrder2, vp)
}
// Send transaction for validation of transaction's data correctness (using tx.Validate() method)
// and simple cryptographic signature verification (using tx.Verify() and PK).
task := &verifyTask{
taskType: verifyTx,
tx: tx,
checkTxSig: checkTxSig,
checkOrder1: checkOrder1,
checkOrder2: checkOrder2,
checkVersion: params.lightNodeActivated,
}
return params.chans.trySend(task)
}
// appendTxParams contains params which are necessary for tx or block appending
// TODO: create features provider instead of passing new params
type appendTxParams struct {
chans *verifierChans // can be nil if validatingUtx == true
checkerInfo *checkerInfo
blockInfo *proto.BlockInfo
block *proto.BlockHeader
acceptFailed bool
blockV5Activated bool
rideV5Activated bool
rideV6Activated bool
consensusImprovementsActivated bool
blockRewardDistributionActivated bool
lightNodeActivated bool
validatingUtx bool // if validatingUtx == false then chans MUST be initialized with non nil value
currentMinerPK crypto.PublicKey
}
func (a *txAppender) handleInvokeOrExchangeTransaction(
tx proto.Transaction,
fallibleInfo *fallibleValidationParams) (*invocationResult, *applicationResult, error) {
invocationRes, applicationRes, err := a.handleFallible(tx, fallibleInfo)
if err != nil {
msg := "fallible validation failed"
if txID, err2 := tx.GetID(a.settings.AddressSchemeCharacter); err2 == nil {
msg = fmt.Sprintf("fallible validation failed for transaction '%s'", base58.Encode(txID))
}
return nil, nil, errs.Extend(err, msg)
}
return invocationRes, applicationRes, nil
}
func (a *txAppender) handleDefaultTransaction(tx proto.Transaction, params *appendTxParams, accountHasVerifierScript bool) (*applicationResult, error) {
// Execute transaction's scripts, check against state.
txScriptsRuns, checkerData, err := a.checkTransactionScripts(tx, accountHasVerifierScript, params)
if err != nil {
return nil, err
}
// Create balance diff of this tx.
txChanges, err := a.blockDiffer.createTransactionDiff(tx, params.block, newDifferInfo(params.blockInfo))
if err != nil {
return nil, errs.Extend(err, "create transaction diff")
}
return newApplicationResult(true, txScriptsRuns, txChanges, checkerData), nil
}
func (a *txAppender) handleEthTx(
tx proto.Transaction,
params *appendTxParams,
accountHasVerifierScript bool,
senderAddr proto.Address,
) (*applicationResult, *invocationResult, bool, error) {
var applicationRes *applicationResult
var invocationRes *invocationResult
needToValidateBalanceDiff := false
ethTx, ok := tx.(*proto.EthereumTransaction)
if !ok {
return nil, nil, false, errors.New("failed to cast interface transaction to ethereum transaction structure")
}
kind, errResolveTx := a.ethTxKindResolver.ResolveTxKind(ethTx, params.blockRewardDistributionActivated)
if errResolveTx != nil {
return nil, nil, false, errors.Wrap(errResolveTx, "failed to guess ethereum transaction kind")
}
ethTx.TxKind = kind
var err error
switch ethTx.TxKind.(type) {
case *proto.EthereumTransferWavesTxKind, *proto.EthereumTransferAssetsErc20TxKind:
applicationRes, err = a.handleDefaultTransaction(tx, params, accountHasVerifierScript)
if err != nil {
return nil, nil, false, errors.Wrapf(err,
"failed to handle ethereum transaction (type %s) with id %s, on height %d",
ethTx.TxKind.String(), ethTx.ID.String(), params.checkerInfo.blockchainHeight+1)
}
// In UTX balances are always validated.
needToValidateBalanceDiff = params.validatingUtx
case *proto.EthereumInvokeScriptTxKind:
fallibleInfo := &fallibleValidationParams{
appendTxParams: params,
senderScripted: accountHasVerifierScript,
senderAddress: senderAddr,
}
invocationRes, applicationRes, err = a.handleInvokeOrExchangeTransaction(tx, fallibleInfo)
if err != nil {
return nil, nil, false, errors.Wrapf(err,
"failed to handle ethereum invoke script transaction (type %s) with id %s, on height %d",
ethTx.TxKind.String(), ethTx.ID.String(), params.checkerInfo.blockchainHeight+1)
}
default:
return nil, nil, false, errors.Errorf("Undefined ethereum transaction kind %T", ethTx.TxKind)
}
return applicationRes, invocationRes, needToValidateBalanceDiff, nil
}
func (a *txAppender) handleTxAndScripts(
tx proto.Transaction,
params *appendTxParams,
accountHasVerifierScript bool,
senderAddr proto.Address,
) (*applicationResult, *invocationResult, bool, error) {
switch tx.GetTypeInfo().Type {
case proto.InvokeScriptTransaction, proto.InvokeExpressionTransaction, proto.ExchangeTransaction:
// Invoke and Exchange transactions should be handled differently.
// They may fail, and will be saved to blockchain anyway.
fallibleInfo := &fallibleValidationParams{
appendTxParams: params,
senderScripted: accountHasVerifierScript,
senderAddress: senderAddr,
}
invocationRes, applicationRes, err := a.handleInvokeOrExchangeTransaction(tx, fallibleInfo)
if err != nil {
return nil, nil, false, errors.Wrap(err, "failed to handle invoke or exchange transaction")
}
// Exchange and Invoke balances are validated in UTX when acceptFailed is false.
// When acceptFailed is true, balances are validated inside handleFallible().
needToValidateBalanceDiff := params.validatingUtx && !params.acceptFailed
return applicationRes, invocationRes, needToValidateBalanceDiff, nil
case proto.EthereumMetamaskTransaction:
return a.handleEthTx(tx, params, accountHasVerifierScript, senderAddr)
case proto.GenesisTransaction, proto.PaymentTransaction, proto.IssueTransaction, proto.TransferTransaction,
proto.ReissueTransaction, proto.BurnTransaction, proto.LeaseTransaction, proto.LeaseCancelTransaction,
proto.CreateAliasTransaction, proto.MassTransferTransaction, proto.DataTransaction, proto.SetScriptTransaction,
proto.SponsorshipTransaction, proto.SetAssetScriptTransaction, proto.UpdateAssetInfoTransaction:
applicationRes, err := a.handleDefaultTransaction(tx, params, accountHasVerifierScript)
if err != nil {
id, idErr := tx.GetID(a.settings.AddressSchemeCharacter)
if idErr != nil {
return nil, nil, false, errors.Wrap(err, "failed to generate transaction ID")
}
return nil, nil, false, errors.Wrapf(err, "failed to handle transaction '%s'", base58.Encode(id))
}
// In UTX balances are always validated.
return applicationRes, nil, params.validatingUtx, nil
default:
return nil, nil, false, errors.Errorf("Undefined transaction type %d", tx.GetTypeInfo())
}
}
func (a *txAppender) appendTx(tx proto.Transaction, params *appendTxParams) (txSnapshot, error) {
defer func() {
a.sc.resetRecentTxComplexity()
a.stor.dropUncertain()
}()
blockID := params.checkerInfo.blockID
// Check that Protobuf transactions are accepted.
if err := a.checkProtobufVersion(tx, params.blockV5Activated); err != nil {
return txSnapshot{}, err
}
// Check transaction for duplication of its ID.
if err := a.checkDuplicateTxIds(tx, a.recentTxIds, params.block.Timestamp); err != nil {
return txSnapshot{}, errs.Extend(err, "check duplicate tx ids")
}
// Verify tx signature and internal data correctness.
senderAddr, err := tx.GetSender(a.settings.AddressSchemeCharacter)
if err != nil {
return txSnapshot{}, errs.Extend(err, "failed to get sender addr by pk")
}
// senderWavesAddr needs only for newestAccountHasVerifier check
senderWavesAddr, err := senderAddr.ToWavesAddress(a.settings.AddressSchemeCharacter)
if err != nil {
return txSnapshot{}, errors.Wrapf(err, "failed to transform (%T) address type to WavesAddress type", senderAddr)
}
accountHasVerifierScript, err := a.stor.scriptsStorage.newestAccountHasVerifier(senderWavesAddr)
if err != nil {
return txSnapshot{}, errs.Extend(err, "account has verifier")
}
if err = a.verifyWavesTxSigAndData(tx, params, accountHasVerifierScript); err != nil {
return txSnapshot{}, errs.Extend(err, "tx signature or data verification failed")
}
// Check tx against state, check tx scripts, calculate balance changes.
applicationRes, invocationResult, needToValidateBalanceDiff, err :=
a.handleTxAndScripts(tx, params, accountHasVerifierScript, senderAddr)
if err != nil {
return txSnapshot{}, err
}
if needToValidateBalanceDiff {
// Validate balance diff for negative balances.
if err = a.diffApplier.validateTxDiff(applicationRes.changes.diff, a.diffStor); err != nil {
return txSnapshot{}, errs.Extend(err, "validate transaction diff")
}
}
// Check complexity limits and scripts runs limits.
if err := a.checkScriptsLimits(a.totalScriptsRuns+applicationRes.totalScriptsRuns, blockID); err != nil {
return txSnapshot{}, errs.Extend(errors.Errorf("%s: %v", blockID.String(), err), "check scripts limits")
}
// Perform state changes, save balance changes, write tx to storage.
txID, err := tx.GetID(a.settings.AddressSchemeCharacter)
if err != nil {
return txSnapshot{}, errs.Extend(err, "get transaction id")
}
// invocationResult may be empty if it was not an Invoke Transaction
snapshot, err := a.commitTxApplication(tx, params, invocationResult, applicationRes)
if err != nil {
zap.S().Errorf("failed to commit transaction (id %s) after successful validation; this should NEVER happen", base58.Encode(txID))
return txSnapshot{}, err
}
// Store additional data for API: transaction by address.
if !params.validatingUtx && a.buildApiData {
if err = a.saveTransactionIdByAddresses(applicationRes.changes.addresses(), txID, blockID); err != nil {
return txSnapshot{}, errs.Extend(err, "save transaction id by addresses")
}
}
return snapshot, nil
}
func (a *txAppender) applySnapshotInLightNode(
params *appendBlockParams,
blockInfo *proto.BlockInfo,
snapshot proto.BlockSnapshot,
stateHash crypto.Digest,
hasher *txSnapshotHasher,
) (crypto.Digest, error) {
if len(snapshot.TxSnapshots) != len(params.transactions) { // sanity check
return crypto.Digest{}, errors.New("number of tx snapshots doesn't match number of transactions")
}
for i, txs := range snapshot.TxSnapshots {
tx := params.transactions[i]
txID, idErr := tx.GetID(a.settings.AddressSchemeCharacter)
if idErr != nil {
return crypto.Digest{}, idErr
}
if len(txs) == 0 { // sanity check
return crypto.Digest{}, errors.Errorf("snapshot of txID %q cannot be empty", base58.Encode(txID))
}
txSh, shErr := calculateTxSnapshotStateHash(hasher, txID, blockInfo.Height, stateHash, txs)
if shErr != nil {
return crypto.Digest{}, errors.Wrapf(shErr, "failed to calculate tx snapshot hash for txID %q at height %d",
base58.Encode(txID), blockInfo.Height,
)
}
stateHash = txSh
regSnapshots := txSnapshot{regular: txs}
if err := regSnapshots.Apply(a.txHandler.sa, tx, false); err != nil {
return crypto.Digest{}, errors.Wrap(err, "failed to apply tx snapshot")
}
if fErr := a.blockDiffer.countMinerFee(tx); fErr != nil {
return crypto.Digest{}, errors.Wrapf(fErr, "failed to count miner fee for tx %d", i+1)
}
// TODO: In future we have to store the list of affected addresses for each transaction here.
}
return stateHash, nil
}
func (a *txAppender) appendTxs(
params *appendBlockParams,
info *checkerInfo,
blockInfo *proto.BlockInfo,
stateHash crypto.Digest,
hasher *txSnapshotHasher,
) (proto.BlockSnapshot, crypto.Digest, error) {
blockV5Activated, err := a.stor.features.newestIsActivated(int16(settings.BlockV5))
if err != nil {
return proto.BlockSnapshot{}, crypto.Digest{}, err
}
consensusImprovementsActivated, err := a.stor.features.newestIsActivated(int16(settings.ConsensusImprovements))
if err != nil {
return proto.BlockSnapshot{}, crypto.Digest{}, err
}
blockRewardDistributionActivated, err := a.stor.features.newestIsActivated(int16(settings.BlockRewardDistribution))
if err != nil {
return proto.BlockSnapshot{}, crypto.Digest{}, err
}
lightNodeActivated, err := a.stor.features.newestIsActivated(int16(settings.LightNode))
if err != nil {
return proto.BlockSnapshot{}, crypto.Digest{}, err
}
// Check and append transactions.
var bs proto.BlockSnapshot
appendTxArgs := &appendTxParams{
chans: params.chans,
checkerInfo: info,
blockInfo: blockInfo,
block: params.block,
acceptFailed: blockV5Activated,
blockV5Activated: blockV5Activated,
rideV5Activated: info.rideV5Activated,
rideV6Activated: info.rideV6Activated,
consensusImprovementsActivated: consensusImprovementsActivated,
blockRewardDistributionActivated: blockRewardDistributionActivated,
lightNodeActivated: lightNodeActivated,
validatingUtx: false,
currentMinerPK: params.block.GeneratorPublicKey,
}
for _, tx := range params.transactions {
txSnapshots, errAppendTx := a.appendTx(tx, appendTxArgs)
if errAppendTx != nil {
return proto.BlockSnapshot{}, crypto.Digest{}, errAppendTx
}
bs.AppendTxSnapshot(txSnapshots.regular)
txID, idErr := tx.GetID(a.settings.AddressSchemeCharacter)
if idErr != nil {
return proto.BlockSnapshot{}, crypto.Digest{}, idErr
}
if len(txSnapshots.regular) == 0 { // sanity check
return proto.BlockSnapshot{}, crypto.Digest{},
errors.Errorf("snapshot of txID %q cannot be empty", base58.Encode(txID))
}
txSh, shErr := calculateTxSnapshotStateHash(hasher, txID, blockInfo.Height, stateHash, txSnapshots.regular)
if shErr != nil {
return proto.BlockSnapshot{}, crypto.Digest{}, errors.Wrapf(shErr,
"failed to calculate tx snapshot hash for txID %q at height %d", base58.Encode(txID), blockInfo.Height,
)
}
stateHash = txSh // update stateHash in order to accumulate state hashes into block snapshot hash
}
return bs, stateHash, nil
}
func calculateInitialSnapshotStateHash(
h *txSnapshotHasher,
blockHasParent bool,
blockHeight proto.Height,
prevHash crypto.Digest,
txSnapshot []proto.AtomicSnapshot,
) (crypto.Digest, error) {
if !blockHasParent { // processing genesis block
if len(txSnapshot) != 0 { // sanity check
return crypto.Digest{}, errors.New("initial block snapshot for genesis block must be empty")
}
return prevHash, nil // return prevHash state hash as initial state hash
}
if len(txSnapshot) == 0 {
return prevHash, nil // return prevHash state hash as initial state hash
}
var txID []byte // txID is necessary only for txStatus atomic snapshot; init snapshot can't have such message
return calculateTxSnapshotStateHash(h, txID, blockHeight, prevHash, txSnapshot)
}
func (a *txAppender) appendFixSnapshots(fixSnapshots []proto.AtomicSnapshot, blockID proto.BlockID) error {
if len(fixSnapshots) == 0 { // no changes, nothing to do
return nil
}
ai := a.txHandler.sa.ApplierInfo()
if ai == nil { // sanity check
return errors.New("applier info is not set")
}
if ai.BlockID() != blockID { // sanity check
return errors.Errorf(
"applier info block ID doesn't match the block ID: expected %s, actual %s",
blockID.String(), ai.BlockID().String(),
)
}
fix := txSnapshot{regular: fixSnapshots}
if err := fix.ApplyFixSnapshot(a.txHandler.sa); err != nil {
return errors.Wrapf(err, "failed to apply fix snapshot at block %s", blockID)
}
return nil
}
func (a *txAppender) appendBlock(params *appendBlockParams) error {
// Reset block complexity counter.
defer func() {
a.sc.resetComplexity()
a.totalScriptsRuns = 0
}()
checkerInfo, err := a.createCheckerInfo(params)
if err != nil {
return err
}
hasParent := params.parent != nil
if hasParent {
checkerInfo.parentTimestamp = params.parent.Timestamp
}
snapshotApplierInfo := newBlockSnapshotsApplierInfo(checkerInfo, a.settings.AddressSchemeCharacter)
a.txHandler.sa.SetApplierInfo(snapshotApplierInfo)
blockInfo, err := a.currentBlockInfo()
if err != nil {
return errors.Wrapf(err, "failed to get current block info, blockchain height is %d", params.blockchainHeight)
}
currentBlockHeight := blockInfo.Height
hasher, err := newTxSnapshotHasherDefault()
if err != nil {
return errors.Wrapf(err, "failed to create tx snapshot default hasher, block height is %d", currentBlockHeight)
}
defer hasher.Release()
stateHash, err := a.createInitialDiffAndStateHash(params, hasParent, blockInfo, hasher)
if err != nil {
return err
}
var blockSnapshot proto.BlockSnapshot
if params.optionalSnapshot != nil {
blockSnapshot = *params.optionalSnapshot
stateHash, err = a.applySnapshotInLightNode(params, blockInfo, blockSnapshot, stateHash, hasher)
} else {
blockSnapshot, stateHash, err = a.appendTxs(params, checkerInfo, blockInfo, stateHash, hasher)
}
if err != nil {
return err
}
// check whether the calculated snapshot state hash equals with the provided one
if blockStateHash, present := params.block.GetStateHash(); present && blockStateHash != stateHash {
return errors.Wrapf(errBlockSnapshotStateHashMismatch, "state hash mismatch; provided '%s', caluclated '%s'",
blockStateHash.String(), stateHash.String(),
)
}
blockID := params.block.BlockID()
if ssErr := a.stor.snapshots.saveSnapshots(blockID, currentBlockHeight, blockSnapshot); ssErr != nil {
return ssErr
}
// clean up legacy state hash records with zero diffs
a.txHandler.sa.filterZeroDiffsSHOut(blockID)
if shErr := a.stor.stateHashes.saveSnapshotStateHash(stateHash, currentBlockHeight, blockID); shErr != nil {
return errors.Wrapf(shErr, "failed to save block shasnpt hash at height %d", currentBlockHeight)
}
// Save fee distribution of this block.
// This will be needed for createMinerAndRewardDiff() of next block due to NG.
return a.blockDiffer.saveCurFeeDistr(params.block)
}
func (a *txAppender) createCheckerInfo(params *appendBlockParams) (*checkerInfo, error) {
rideV5Activated, err := a.stor.features.newestIsActivated(int16(settings.RideV5))
if err != nil {
return nil, err
}
rideV6Activated, err := a.stor.features.newestIsActivated(int16(settings.RideV6))
if err != nil {
return nil, err
}
blockRewardDistribution, err := a.stor.features.newestIsActivated(int16(settings.BlockRewardDistribution))
if err != nil {
return nil, err
}
checkerInfo := &checkerInfo{
currentTimestamp: params.block.Timestamp,
blockID: params.block.BlockID(),
blockVersion: params.block.Version,
blockchainHeight: params.blockchainHeight,
rideV5Activated: rideV5Activated,
rideV6Activated: rideV6Activated,
blockRewardDistribution: blockRewardDistribution,
}
return checkerInfo, nil
}
func (a *txAppender) createInitialDiffAndStateHash(
params *appendBlockParams,
hasParent bool,
blockInfo *proto.BlockInfo,
hasher *txSnapshotHasher,
) (crypto.Digest, error) {
// Create miner balance diff.
// This adds 60% of prev block fees as very first balance diff of the current block in case NG is activated.
// Before NG activation it adds all transactions fees to the miner's balance.
minerAndRewardDiff, err := a.blockDiffer.createMinerAndRewardDiff(params.block, hasParent, params.transactions)
if err != nil {
return crypto.Digest{}, err
}
// create the initial snapshot
initialSnapshot, err := a.txHandler.tp.createInitialBlockSnapshot(minerAndRewardDiff.balancesChanges())
if err != nil {
return crypto.Digest{}, errors.Wrap(err, "failed to create initial snapshot")
}
currentBlockHeight := blockInfo.Height
// Save miner diff first (for validation)
if err = a.diffStor.saveTxDiff(minerAndRewardDiff); err != nil {
return crypto.Digest{}, err
}
err = a.diffApplier.validateBalancesChanges(minerAndRewardDiff.balancesChanges())
if err != nil {
return crypto.Digest{}, errors.Wrap(err, "failed to validate miner reward changes")
}
a.diffStor.reset()
err = initialSnapshot.ApplyInitialSnapshot(a.txHandler.sa)
if err != nil {
return crypto.Digest{}, errors.Wrap(err, "failed to apply an initial snapshot")
}
// hash block initial snapshot and fix snapshot in the context of the applying block
snapshotsToHash := initialSnapshot.regular
if len(params.fixSnapshotsToInitialHash) > 0 {
snapshotsToHash = append(snapshotsToHash, params.fixSnapshotsToInitialHash...)
}
// get initial snapshot hash for block
stateHash, err := calculateInitialSnapshotStateHash(
hasher,
hasParent,
currentBlockHeight,
params.lastSnapshotStateHash, // previous block state hash
snapshotsToHash,
)
if err != nil {
return crypto.Digest{}, errors.Wrapf(err, "failed to calculate initial snapshot hash for blockID %q at height %d",
params.block.BlockID(), currentBlockHeight,
)
}
return stateHash, nil
}
// used only in tests now. All diffs are applied in snapshotApplier.
func (a *txAppender) applyAllDiffs() error {
a.recentTxIds = make(map[string]struct{})
return a.moveChangesToHistoryStorage()
}
func (a *txAppender) moveChangesToHistoryStorage() error {
changes := a.diffStor.allChanges()
a.diffStor.reset()
return a.diffApplier.applyBalancesChanges(changes)
}
type fallibleValidationParams struct {
*appendTxParams
senderScripted bool
senderAddress proto.Address
}
type applicationResult struct {
status bool
totalScriptsRuns uint64
changes txBalanceChanges
checkerData txCheckerData
}
func newApplicationResult(status bool, totalScriptsRuns uint64, changes txBalanceChanges, checkerData txCheckerData) *applicationResult {
return &applicationResult{status, totalScriptsRuns, changes, checkerData} // all fields must be initialized
}
func (a *txAppender) handleInvoke(
tx proto.Transaction,
info *fallibleValidationParams) (*invocationResult, *applicationResult, error) {
var ID crypto.Digest
switch t := tx.(type) {
case *proto.InvokeScriptWithProofs:
ID = *t.ID
case *proto.InvokeExpressionTransactionWithProofs:
ID = *t.ID
case *proto.EthereumTransaction:
switch t.TxKind.(type) {
case *proto.EthereumInvokeScriptTxKind:
ID = *t.ID
default:
return nil, nil, errors.Errorf("unexpected ethereum tx kind (%T)", tx)
}
default:
return nil, nil, errors.Errorf("failed to handle invoke: wrong type of transaction (%T)", tx)
}
invocationRes, applicationRes, err := a.ia.applyInvokeScript(tx, info)
if err != nil {
zap.S().Debugf("failed to apply InvokeScript transaction %s to state: %v", ID.String(), err)
return nil, nil, err
}
return invocationRes, applicationRes, nil
}
func (a *txAppender) countExchangeScriptsRuns(scriptsRuns uint64) (uint64, error) {
// Some bug in historical blockchain, no logic here.
ride4DAppsActivated, err := a.stor.features.newestIsActivated(int16(settings.Ride4DApps))
if err != nil {
return 0, err
}
if !ride4DAppsActivated {
// Don't count before Ride4DApps activation.
return 0, nil
}
return scriptsRuns, nil
}
func (a *txAppender) handleExchange(tx proto.Transaction, info *fallibleValidationParams) (*applicationResult, error) {
exchange, ok := tx.(proto.Exchange)
if !ok {
return nil, errors.New("failed to convert transaction to Exchange")
}
// If BlockV5 feature is not activated, we never accept failed transactions.
info.acceptFailed = info.blockV5Activated && info.acceptFailed
scriptsRuns := uint64(0)
// At first, we call accounts and orders scripts which must not fail.
if info.senderScripted {
// Check script on account.
err := a.sc.callAccountScriptWithTx(tx, info.appendTxParams)
if err != nil {
return nil, err
}
scriptsRuns++
}