7
7
"encoding/hex"
8
8
"errors"
9
9
"fmt"
10
+ "github.com/tonkeeper/tongo/abi"
10
11
"go.uber.org/zap"
11
12
"golang.org/x/exp/slices"
12
13
"net/http"
@@ -390,29 +391,37 @@ func (h *Handler) EmulateMessageToAccountEvent(ctx context.Context, request *oas
390
391
if err != nil {
391
392
return nil , toError (http .StatusBadRequest , err )
392
393
}
393
- configBase64 , err := h .storage .TrimmedConfigBase64 ()
394
- if err != nil {
395
- return nil , toError (http .StatusInternalServerError , err )
396
- }
397
- options := []txemulator.TraceOption {
398
- txemulator .WithAccountsSource (h .storage ),
399
- txemulator .WithConfigBase64 (configBase64 ),
400
- txemulator .WithLimit (1100 ),
401
- }
402
- if ! params .IgnoreSignatureCheck .Value {
403
- options = append (options , txemulator .WithSignatureCheck ())
404
- }
405
- emulator , err := txemulator .NewTraceBuilder (options ... )
406
- if err != nil {
407
- return nil , toError (http .StatusInternalServerError , err )
408
- }
409
- tree , err := emulator .Run (ctx , m )
410
- if err != nil {
411
- return nil , toProperEmulationError (err )
412
- }
413
- trace , err := emulatedTreeToTrace (ctx , h .executor , h .storage , tree , emulator .FinalStates (), nil , h .configPool )
414
- if err != nil {
415
- return nil , toError (http .StatusInternalServerError , err )
394
+ hash , err := c .HashString ()
395
+ trace , _ , err := h .storage .GetTraceWithState (ctx , hash )
396
+ if trace == nil {
397
+ configBase64 , err := h .storage .TrimmedConfigBase64 ()
398
+ if err != nil {
399
+ return nil , toError (http .StatusInternalServerError , err )
400
+ }
401
+ options := []txemulator.TraceOption {
402
+ txemulator .WithAccountsSource (h .storage ),
403
+ txemulator .WithConfigBase64 (configBase64 ),
404
+ txemulator .WithLimit (1100 ),
405
+ }
406
+ if ! params .IgnoreSignatureCheck .Value {
407
+ options = append (options , txemulator .WithSignatureCheck ())
408
+ }
409
+ emulator , err := txemulator .NewTraceBuilder (options ... )
410
+ if err != nil {
411
+ return nil , toError (http .StatusInternalServerError , err )
412
+ }
413
+ tree , err := emulator .Run (ctx , m )
414
+ if err != nil {
415
+ return nil , toProperEmulationError (err )
416
+ }
417
+ trace , err = emulatedTreeToTrace (ctx , h .executor , h .storage , tree , emulator .FinalStates (), nil , h .configPool )
418
+ if err != nil {
419
+ return nil , toError (http .StatusInternalServerError , err )
420
+ }
421
+ err = h .storage .SaveTraceWithState (hash , trace , []abi.MethodInvocation {}, 24 * time .Hour )
422
+ if err != nil {
423
+ fmt .Println ("not saved to scylla: " , err )
424
+ }
416
425
}
417
426
actions , err := bath .FindActions (ctx , trace , bath .WithInformationSource (h .storage ))
418
427
if err != nil {
@@ -437,33 +446,41 @@ func (h *Handler) EmulateMessageToEvent(ctx context.Context, request *oas.Emulat
437
446
}
438
447
trace , prs := h .mempoolEmulate .traces .Get (hash )
439
448
if ! prs {
440
- var m tlb.Message
441
- if err := tlb .Unmarshal (c , & m ); err != nil {
442
- return nil , toError (http .StatusBadRequest , err )
443
- }
444
- configBase64 , err := h .storage .TrimmedConfigBase64 ()
445
- if err != nil {
446
- return nil , toError (http .StatusInternalServerError , err )
447
- }
448
- options := []txemulator.TraceOption {
449
- txemulator .WithAccountsSource (h .storage ),
450
- txemulator .WithConfigBase64 (configBase64 ),
451
- }
452
- if ! params .IgnoreSignatureCheck .Value {
453
- options = append (options , txemulator .WithSignatureCheck ())
454
- }
449
+ hs , _ := c .HashString ()
450
+ trace , _ , err = h .storage .GetTraceWithState (ctx , hs )
451
+ if trace == nil {
452
+ var m tlb.Message
453
+ if err := tlb .Unmarshal (c , & m ); err != nil {
454
+ return nil , toError (http .StatusBadRequest , err )
455
+ }
456
+ configBase64 , err := h .storage .TrimmedConfigBase64 ()
457
+ if err != nil {
458
+ return nil , toError (http .StatusInternalServerError , err )
459
+ }
460
+ options := []txemulator.TraceOption {
461
+ txemulator .WithAccountsSource (h .storage ),
462
+ txemulator .WithConfigBase64 (configBase64 ),
463
+ }
464
+ if ! params .IgnoreSignatureCheck .Value {
465
+ options = append (options , txemulator .WithSignatureCheck ())
466
+ }
455
467
456
- emulator , err := txemulator .NewTraceBuilder (options ... )
457
- if err != nil {
458
- return nil , toError (http .StatusInternalServerError , err )
459
- }
460
- tree , err := emulator .Run (ctx , m )
461
- if err != nil {
462
- return nil , toProperEmulationError (err )
463
- }
464
- trace , err = emulatedTreeToTrace (ctx , h .executor , h .storage , tree , emulator .FinalStates (), nil , h .configPool )
465
- if err != nil {
466
- return nil , toError (http .StatusInternalServerError , err )
468
+ emulator , err := txemulator .NewTraceBuilder (options ... )
469
+ if err != nil {
470
+ return nil , toError (http .StatusInternalServerError , err )
471
+ }
472
+ tree , err := emulator .Run (ctx , m )
473
+ if err != nil {
474
+ return nil , toProperEmulationError (err )
475
+ }
476
+ trace , err = emulatedTreeToTrace (ctx , h .executor , h .storage , tree , emulator .FinalStates (), nil , h .configPool )
477
+ if err != nil {
478
+ return nil , toError (http .StatusInternalServerError , err )
479
+ }
480
+ err = h .storage .SaveTraceWithState (hs , trace , []abi.MethodInvocation {}, 24 * time .Hour )
481
+ if err != nil {
482
+ fmt .Println ("not saved to scylla: " , err )
483
+ }
467
484
}
468
485
}
469
486
actions , err := bath .FindActions (ctx , trace , bath .WithInformationSource (h .storage ))
@@ -489,34 +506,42 @@ func (h *Handler) EmulateMessageToTrace(ctx context.Context, request *oas.Emulat
489
506
}
490
507
trace , prs := h .mempoolEmulate .traces .Get (hash )
491
508
if ! prs {
492
- var m tlb.Message
493
- err = tlb .Unmarshal (c , & m )
494
- if err != nil {
495
- return nil , toError (http .StatusBadRequest , err )
496
- }
497
- configBase64 , err := h .storage .TrimmedConfigBase64 ()
498
- if err != nil {
499
- return nil , toError (http .StatusInternalServerError , err )
500
- }
501
- options := []txemulator.TraceOption {
502
- txemulator .WithAccountsSource (h .storage ),
503
- txemulator .WithConfigBase64 (configBase64 ),
504
- }
505
- if ! params .IgnoreSignatureCheck .Value {
506
- options = append (options , txemulator .WithSignatureCheck ())
507
- }
509
+ hs , err := c .HashString ()
510
+ trace , _ , err = h .storage .GetTraceWithState (ctx , hs )
511
+ if trace == nil {
512
+ var m tlb.Message
513
+ err = tlb .Unmarshal (c , & m )
514
+ if err != nil {
515
+ return nil , toError (http .StatusBadRequest , err )
516
+ }
517
+ configBase64 , err := h .storage .TrimmedConfigBase64 ()
518
+ if err != nil {
519
+ return nil , toError (http .StatusInternalServerError , err )
520
+ }
521
+ options := []txemulator.TraceOption {
522
+ txemulator .WithAccountsSource (h .storage ),
523
+ txemulator .WithConfigBase64 (configBase64 ),
524
+ }
525
+ if ! params .IgnoreSignatureCheck .Value {
526
+ options = append (options , txemulator .WithSignatureCheck ())
527
+ }
508
528
509
- emulator , err := txemulator .NewTraceBuilder (options ... )
510
- if err != nil {
511
- return nil , toError (http .StatusInternalServerError , err )
512
- }
513
- tree , err := emulator .Run (ctx , m )
514
- if err != nil {
515
- return nil , toProperEmulationError (err )
516
- }
517
- trace , err = emulatedTreeToTrace (ctx , h .executor , h .storage , tree , emulator .FinalStates (), nil , h .configPool )
518
- if err != nil {
519
- return nil , toError (http .StatusInternalServerError , err )
529
+ emulator , err := txemulator .NewTraceBuilder (options ... )
530
+ if err != nil {
531
+ return nil , toError (http .StatusInternalServerError , err )
532
+ }
533
+ tree , err := emulator .Run (ctx , m )
534
+ if err != nil {
535
+ return nil , toProperEmulationError (err )
536
+ }
537
+ trace , err = emulatedTreeToTrace (ctx , h .executor , h .storage , tree , emulator .FinalStates (), nil , h .configPool )
538
+ if err != nil {
539
+ return nil , toError (http .StatusInternalServerError , err )
540
+ }
541
+ err = h .storage .SaveTraceWithState (hs , trace , []abi.MethodInvocation {}, 24 * time .Hour )
542
+ if err != nil {
543
+ fmt .Println ("not saved to scylla: " , err )
544
+ }
520
545
}
521
546
}
522
547
t := convertTrace (trace , h .addressBook )
@@ -603,44 +628,54 @@ func (h *Handler) EmulateMessageToWallet(ctx context.Context, request *oas.Emula
603
628
if err != nil {
604
629
return nil , toError (http .StatusInternalServerError , err )
605
630
}
606
- configBase64 , err := h .storage .TrimmedConfigBase64 ()
607
- if err != nil {
608
- return nil , toError (http .StatusInternalServerError , err )
609
- }
610
631
611
- options := []txemulator.TraceOption {
612
- txemulator .WithConfigBase64 (configBase64 ),
613
- txemulator .WithAccountsSource (h .storage ),
614
- txemulator .WithLimit (1100 ),
615
- }
616
- accounts , err := convertEmulationParameters (request .Params )
617
- if err != nil {
618
- return nil , toError (http .StatusBadRequest , err )
619
- }
620
- var states []tlb.ShardAccount
621
- for accountID , balance := range accounts {
622
- originalState , err := h .storage .GetAccountState (ctx , accountID )
632
+ hash , err := msgCell .HashString ()
633
+ trace , _ , err := h .storage .GetTraceWithState (ctx , hash )
634
+ if trace == nil {
635
+ configBase64 , err := h .storage .TrimmedConfigBase64 ()
636
+ if err != nil {
637
+ return nil , toError (http .StatusInternalServerError , err )
638
+ }
639
+
640
+ options := []txemulator.TraceOption {
641
+ txemulator .WithConfigBase64 (configBase64 ),
642
+ txemulator .WithAccountsSource (h .storage ),
643
+ txemulator .WithLimit (1100 ),
644
+ }
645
+ accounts , err := convertEmulationParameters (request .Params )
646
+ if err != nil {
647
+ return nil , toError (http .StatusBadRequest , err )
648
+ }
649
+ var states []tlb.ShardAccount
650
+ for accountID , balance := range accounts {
651
+ originalState , err := h .storage .GetAccountState (ctx , accountID )
652
+ if err != nil {
653
+ return nil , toError (http .StatusInternalServerError , err )
654
+ }
655
+ state , err := prepareAccountState (* walletAddress , originalState , balance )
656
+ if err != nil {
657
+ return nil , toError (http .StatusInternalServerError , err )
658
+ }
659
+ states = append (states , state )
660
+ }
661
+
662
+ options = append (options , txemulator .WithAccounts (states ... ))
663
+ emulator , err := txemulator .NewTraceBuilder (options ... )
623
664
if err != nil {
624
665
return nil , toError (http .StatusInternalServerError , err )
625
666
}
626
- state , err := prepareAccountState (* walletAddress , originalState , balance )
667
+ tree , err := emulator .Run (ctx , m )
668
+ if err != nil {
669
+ return nil , toProperEmulationError (err )
670
+ }
671
+ trace , err = emulatedTreeToTrace (ctx , h .executor , h .storage , tree , emulator .FinalStates (), nil , h .configPool )
627
672
if err != nil {
628
673
return nil , toError (http .StatusInternalServerError , err )
629
674
}
630
- states = append (states , state )
631
- }
632
- options = append (options , txemulator .WithAccounts (states ... ))
633
- emulator , err := txemulator .NewTraceBuilder (options ... )
634
- if err != nil {
635
- return nil , toError (http .StatusInternalServerError , err )
636
- }
637
- tree , err := emulator .Run (ctx , m )
638
- if err != nil {
639
- return nil , toProperEmulationError (err )
640
- }
641
- trace , err := emulatedTreeToTrace (ctx , h .executor , h .storage , tree , emulator .FinalStates (), nil , h .configPool )
642
- if err != nil {
643
- return nil , toError (http .StatusInternalServerError , err )
675
+ err = h .storage .SaveTraceWithState (hash , trace , []abi.MethodInvocation {}, 24 * time .Hour )
676
+ if err != nil {
677
+ fmt .Println ("not saved to scylla: " , err )
678
+ }
644
679
}
645
680
t := convertTrace (trace , h .addressBook )
646
681
actions , err := bath .FindActions (ctx , trace , bath .ForAccount (* walletAddress ), bath .WithInformationSource (h .storage ))
0 commit comments