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,43 @@ 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
+ hash , err := c . HashString ()
394
395
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 )
396
+ return nil , toError (http .StatusBadRequest , err )
408
397
}
409
- tree , err := emulator . Run (ctx , m )
398
+ trace , version , _ , err := h . storage . GetTraceWithState (ctx , hash )
410
399
if err != nil {
411
- return nil , toProperEmulationError (err )
400
+ h . logger . Warn ( "get trace from storage: " , zap . Error (err ) )
412
401
}
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 )
402
+ if trace == nil || h .tongoVersion == 0 || version > h .tongoVersion {
403
+ configBase64 , err := h .storage .TrimmedConfigBase64 ()
404
+ if err != nil {
405
+ return nil , toError (http .StatusInternalServerError , err )
406
+ }
407
+ options := []txemulator.TraceOption {
408
+ txemulator .WithAccountsSource (h .storage ),
409
+ txemulator .WithConfigBase64 (configBase64 ),
410
+ txemulator .WithLimit (1100 ),
411
+ }
412
+ if ! params .IgnoreSignatureCheck .Value {
413
+ options = append (options , txemulator .WithSignatureCheck ())
414
+ }
415
+ emulator , err := txemulator .NewTraceBuilder (options ... )
416
+ if err != nil {
417
+ return nil , toError (http .StatusInternalServerError , err )
418
+ }
419
+ tree , err := emulator .Run (ctx , m )
420
+ if err != nil {
421
+ return nil , toProperEmulationError (err )
422
+ }
423
+ trace , err = emulatedTreeToTrace (ctx , h .executor , h .storage , tree , emulator .FinalStates (), nil , h .configPool )
424
+ if err != nil {
425
+ return nil , toError (http .StatusInternalServerError , err )
426
+ }
427
+ err = h .storage .SaveTraceWithState (ctx , hash , trace , h .tongoVersion , []abi.MethodInvocation {}, 24 * time .Hour )
428
+ if err != nil {
429
+ fmt .Println ("trace not saved: " , err )
430
+ }
416
431
}
417
432
actions , err := bath .FindActions (ctx , trace , bath .WithInformationSource (h .storage ))
418
433
if err != nil {
@@ -437,33 +452,47 @@ func (h *Handler) EmulateMessageToEvent(ctx context.Context, request *oas.Emulat
437
452
}
438
453
trace , prs := h .mempoolEmulate .traces .Get (hash )
439
454
if ! prs {
440
- var m tlb. Message
441
- if err := tlb . Unmarshal ( c , & m ); err != nil {
455
+ hs , err := c . HashString ()
456
+ if err != nil {
442
457
return nil , toError (http .StatusBadRequest , err )
443
458
}
444
- configBase64 , err := h .storage .TrimmedConfigBase64 ( )
459
+ trace , version , _ , err := h .storage .GetTraceWithState ( ctx , hs )
445
460
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 ())
461
+ h .logger .Warn ("get trace from storage: " , zap .Error (err ))
454
462
}
463
+ if trace == nil || h .tongoVersion == 0 || version > h .tongoVersion {
464
+ var m tlb.Message
465
+ if err := tlb .Unmarshal (c , & m ); err != nil {
466
+ return nil , toError (http .StatusBadRequest , err )
467
+ }
468
+ configBase64 , err := h .storage .TrimmedConfigBase64 ()
469
+ if err != nil {
470
+ return nil , toError (http .StatusInternalServerError , err )
471
+ }
472
+ options := []txemulator.TraceOption {
473
+ txemulator .WithAccountsSource (h .storage ),
474
+ txemulator .WithConfigBase64 (configBase64 ),
475
+ }
476
+ if ! params .IgnoreSignatureCheck .Value {
477
+ options = append (options , txemulator .WithSignatureCheck ())
478
+ }
455
479
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 )
480
+ emulator , err := txemulator .NewTraceBuilder (options ... )
481
+ if err != nil {
482
+ return nil , toError (http .StatusInternalServerError , err )
483
+ }
484
+ tree , err := emulator .Run (ctx , m )
485
+ if err != nil {
486
+ return nil , toProperEmulationError (err )
487
+ }
488
+ trace , err = emulatedTreeToTrace (ctx , h .executor , h .storage , tree , emulator .FinalStates (), nil , h .configPool )
489
+ if err != nil {
490
+ return nil , toError (http .StatusInternalServerError , err )
491
+ }
492
+ err = h .storage .SaveTraceWithState (ctx , hs , trace , h .tongoVersion , []abi.MethodInvocation {}, 24 * time .Hour )
493
+ if err != nil {
494
+ fmt .Println ("trace not saved: " , err )
495
+ }
467
496
}
468
497
}
469
498
actions , err := bath .FindActions (ctx , trace , bath .WithInformationSource (h .storage ))
@@ -489,34 +518,48 @@ func (h *Handler) EmulateMessageToTrace(ctx context.Context, request *oas.Emulat
489
518
}
490
519
trace , prs := h .mempoolEmulate .traces .Get (hash )
491
520
if ! prs {
492
- var m tlb.Message
493
- err = tlb .Unmarshal (c , & m )
521
+ hs , err := c .HashString ()
494
522
if err != nil {
495
523
return nil , toError (http .StatusBadRequest , err )
496
524
}
497
- configBase64 , err := h .storage .TrimmedConfigBase64 ( )
525
+ trace , version , _ , err := h .storage .GetTraceWithState ( ctx , hs )
498
526
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 ())
527
+ h .logger .Warn ("get trace from storage: " , zap .Error (err ))
507
528
}
529
+ if trace == nil || h .tongoVersion == 0 || version > h .tongoVersion {
530
+ var m tlb.Message
531
+ err = tlb .Unmarshal (c , & m )
532
+ if err != nil {
533
+ return nil , toError (http .StatusBadRequest , err )
534
+ }
535
+ configBase64 , err := h .storage .TrimmedConfigBase64 ()
536
+ if err != nil {
537
+ return nil , toError (http .StatusInternalServerError , err )
538
+ }
539
+ options := []txemulator.TraceOption {
540
+ txemulator .WithAccountsSource (h .storage ),
541
+ txemulator .WithConfigBase64 (configBase64 ),
542
+ }
543
+ if ! params .IgnoreSignatureCheck .Value {
544
+ options = append (options , txemulator .WithSignatureCheck ())
545
+ }
508
546
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 )
547
+ emulator , err := txemulator .NewTraceBuilder (options ... )
548
+ if err != nil {
549
+ return nil , toError (http .StatusInternalServerError , err )
550
+ }
551
+ tree , err := emulator .Run (ctx , m )
552
+ if err != nil {
553
+ return nil , toProperEmulationError (err )
554
+ }
555
+ trace , err = emulatedTreeToTrace (ctx , h .executor , h .storage , tree , emulator .FinalStates (), nil , h .configPool )
556
+ if err != nil {
557
+ return nil , toError (http .StatusInternalServerError , err )
558
+ }
559
+ err = h .storage .SaveTraceWithState (ctx , hs , trace , h .tongoVersion , []abi.MethodInvocation {}, 24 * time .Hour )
560
+ if err != nil {
561
+ fmt .Println ("trace not saved: " , err )
562
+ }
520
563
}
521
564
}
522
565
t := convertTrace (trace , h .addressBook )
@@ -603,44 +646,60 @@ func (h *Handler) EmulateMessageToWallet(ctx context.Context, request *oas.Emula
603
646
if err != nil {
604
647
return nil , toError (http .StatusInternalServerError , err )
605
648
}
606
- configBase64 , err := h .storage .TrimmedConfigBase64 ()
607
- if err != nil {
608
- return nil , toError (http .StatusInternalServerError , err )
609
- }
610
649
611
- options := []txemulator.TraceOption {
612
- txemulator .WithConfigBase64 (configBase64 ),
613
- txemulator .WithAccountsSource (h .storage ),
614
- txemulator .WithLimit (1100 ),
615
- }
616
- accounts , err := convertEmulationParameters (request .Params )
650
+ hash , err := msgCell .HashString ()
617
651
if err != nil {
618
652
return nil , toError (http .StatusBadRequest , err )
619
653
}
620
- var states []tlb.ShardAccount
621
- for accountID , balance := range accounts {
622
- originalState , err := h .storage .GetAccountState (ctx , accountID )
654
+ trace , version , _ , err := h .storage .GetTraceWithState (ctx , hash )
655
+ if err != nil {
656
+ h .logger .Warn ("get trace from storage: " , zap .Error (err ))
657
+ }
658
+ if trace == nil || h .tongoVersion == 0 || version > h .tongoVersion {
659
+ configBase64 , err := h .storage .TrimmedConfigBase64 ()
623
660
if err != nil {
624
661
return nil , toError (http .StatusInternalServerError , err )
625
662
}
626
- state , err := prepareAccountState (* walletAddress , originalState , balance )
663
+
664
+ options := []txemulator.TraceOption {
665
+ txemulator .WithConfigBase64 (configBase64 ),
666
+ txemulator .WithAccountsSource (h .storage ),
667
+ txemulator .WithLimit (1100 ),
668
+ }
669
+ accounts , err := convertEmulationParameters (request .Params )
670
+ if err != nil {
671
+ return nil , toError (http .StatusBadRequest , err )
672
+ }
673
+ var states []tlb.ShardAccount
674
+ for accountID , balance := range accounts {
675
+ originalState , err := h .storage .GetAccountState (ctx , accountID )
676
+ if err != nil {
677
+ return nil , toError (http .StatusInternalServerError , err )
678
+ }
679
+ state , err := prepareAccountState (* walletAddress , originalState , balance )
680
+ if err != nil {
681
+ return nil , toError (http .StatusInternalServerError , err )
682
+ }
683
+ states = append (states , state )
684
+ }
685
+
686
+ options = append (options , txemulator .WithAccounts (states ... ))
687
+ emulator , err := txemulator .NewTraceBuilder (options ... )
627
688
if err != nil {
628
689
return nil , toError (http .StatusInternalServerError , err )
629
690
}
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 )
691
+ tree , err := emulator .Run (ctx , m )
692
+ if err != nil {
693
+ return nil , toProperEmulationError (err )
694
+ }
695
+ trace , err = emulatedTreeToTrace (ctx , h .executor , h .storage , tree , emulator .FinalStates (), nil , h .configPool )
696
+ if err != nil {
697
+ return nil , toError (http .StatusInternalServerError , err )
698
+ }
699
+ err = h .storage .SaveTraceWithState (ctx , hash , trace , h .tongoVersion , []abi.MethodInvocation {}, 24 * time .Hour )
700
+ if err != nil {
701
+ fmt .Println ("trace not saved: " , err )
702
+ }
644
703
}
645
704
t := convertTrace (trace , h .addressBook )
646
705
actions , err := bath .FindActions (ctx , trace , bath .ForAccount (* walletAddress ), bath .WithInformationSource (h .storage ))
0 commit comments