-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathvm_runner_contract_write.go
More file actions
2573 lines (2306 loc) · 100 KB
/
vm_runner_contract_write.go
File metadata and controls
2573 lines (2306 loc) · 100 KB
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 taskengine
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"math/big"
"regexp"
"strconv"
"strings"
"time"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/AvaProtocol/EigenLayer-AVS/core/chainio/aa"
"github.com/AvaProtocol/EigenLayer-AVS/core/config"
"github.com/AvaProtocol/EigenLayer-AVS/pkg/byte4"
"github.com/AvaProtocol/EigenLayer-AVS/pkg/eip1559"
"github.com/AvaProtocol/EigenLayer-AVS/pkg/erc4337/bundler"
"github.com/AvaProtocol/EigenLayer-AVS/pkg/erc4337/preset"
"github.com/AvaProtocol/EigenLayer-AVS/pkg/erc4337/userop"
"github.com/AvaProtocol/EigenLayer-AVS/pkg/logger"
avsproto "github.com/AvaProtocol/EigenLayer-AVS/protobuf"
"google.golang.org/protobuf/types/known/structpb"
)
type SendUserOpFunc func(
config *config.SmartWalletConfig,
owner common.Address,
callData []byte,
paymasterReq *preset.VerifyingPaymasterRequest,
senderOverride *common.Address,
saltOverride *big.Int,
executionFeeWei *big.Int,
lgr logger.Logger,
) (*userop.UserOperation, *types.Receipt, error)
type ContractWriteProcessor struct {
*CommonProcessor
client *ethclient.Client
smartWalletConfig *config.SmartWalletConfig
owner common.Address
sendUserOpFunc SendUserOpFunc
}
func NewContractWriteProcessor(vm *VM, client *ethclient.Client, smartWalletConfig *config.SmartWalletConfig, owner common.Address) *ContractWriteProcessor {
r := &ContractWriteProcessor{
client: client,
smartWalletConfig: smartWalletConfig,
owner: owner,
sendUserOpFunc: preset.SendUserOp, // Default to the real implementation
CommonProcessor: &CommonProcessor{
vm: vm,
},
}
return r
}
// resolveSimulationMode resolves the effective simulation mode for a contract write node.
// It returns the per-node is_simulated value if explicitly set, otherwise falls back to the VM's simulation flag.
func (r *ContractWriteProcessor) resolveSimulationMode(node *avsproto.ContractWriteNode, vmDefault bool) bool {
if node != nil && node.Config != nil && node.Config.IsSimulated != nil {
return node.Config.GetIsSimulated()
}
return vmDefault
}
func (r *ContractWriteProcessor) getInputData(node *avsproto.ContractWriteNode) (string, string, []*avsproto.ContractWriteNode_MethodCall, error) {
var contractAddress, callData string
var methodCalls []*avsproto.ContractWriteNode_MethodCall
// Priority 1: Use node.Config if available (static configuration)
if node.Config != nil {
contractAddress = node.Config.ContractAddress
callData = node.Config.CallData
// Note: ABI is handled directly from protobuf Values in Execute method for optimization
methodCalls = node.Config.MethodCalls
}
// Priority 2: Override with VM variables if set (dynamic runtime values)
r.vm.mu.Lock()
if addr, exists := r.vm.vars["contract_address"]; exists {
if addrStr, ok := addr.(string); ok {
contractAddress = addrStr
}
}
if data, exists := r.vm.vars["call_data"]; exists {
if dataStr, ok := data.(string); ok {
callData = dataStr
}
}
r.vm.mu.Unlock()
// Apply template variable preprocessing
contractAddress = r.vm.preprocessTextWithVariableMapping(contractAddress)
callData = r.vm.preprocessTextWithVariableMapping(callData)
// If we have method_calls from config but also call_data from variables, prefer method_calls
// If no method_calls but we have call_data, create a single method call
if len(methodCalls) == 0 && callData != "" {
methodCalls = []*avsproto.ContractWriteNode_MethodCall{
{
CallData: &callData,
MethodName: UnknownMethodName, // Will be resolved from ABI if available
},
}
}
if contractAddress == "" {
return "", "", nil, NewMissingRequiredFieldError("contractAddress")
}
// Validate contract address format
if !common.IsHexAddress(contractAddress) {
return "", "", nil, NewInvalidAddressError(contractAddress)
}
if len(methodCalls) == 0 {
return "", "", nil, NewMissingRequiredFieldError("methodCalls or callData")
}
return contractAddress, callData, methodCalls, nil
}
func (r *ContractWriteProcessor) executeMethodCall(
ctx context.Context,
parsedABI *abi.ABI,
originalAbiString string,
contractAddress common.Address,
methodCall *avsproto.ContractWriteNode_MethodCall,
shouldSimulate bool,
node *avsproto.ContractWriteNode,
) *avsproto.ContractWriteNode_MethodResult {
t0 := time.Now()
// VERY OBVIOUS DEBUG - use Info to avoid noisy error-level logs in normal flow
// Log method execution start at debug level for development/troubleshooting
r.vm.logger.Debug("ContractWriteProcessor: executeMethodCall started",
"method", methodCall.MethodName,
"contract", contractAddress.Hex(),
"timestamp", time.Now().Format("15:04:05.000"))
// CRITICAL: We NEVER have eoaAddress's private key and can NEVER send transactions from it
// eoaAddress (r.owner) is ONLY for ownership verification
// For ALL transactions (simulations AND real), we MUST use runner (smart wallet) address as sender
// The runner smart wallet corresponds to and is controlled by eoaAddress, but sender must ALWAYS be runner
var senderAddress common.Address
if aaSenderVar, ok := r.vm.vars["aa_sender"]; ok {
if aaSenderStr, ok := aaSenderVar.(string); ok && aaSenderStr != "" {
senderAddress = common.HexToAddress(aaSenderStr) // This is the runner (smart wallet)
r.vm.logger.Info("CONTRACT WRITE - Sender address resolved from aa_sender",
"sender_address_runner", senderAddress.Hex(),
"aa_sender_var", aaSenderStr)
} else {
return &avsproto.ContractWriteNode_MethodResult{
MethodName: methodCall.MethodName,
Success: false,
Error: fmt.Sprintf("aa_sender variable is set but invalid - must be a non-empty hex address string, got: %v", aaSenderVar),
}
}
} else {
// This should never happen because RunNodeImmediately validates settings.runner and sets aa_sender
// If we get here, it means validation was bypassed or there's a bug in the validation logic
return &avsproto.ContractWriteNode_MethodResult{
MethodName: methodCall.MethodName,
Success: false,
Error: "aa_sender variable not set - settings.runner is required for contractWrite",
}
}
// Substitute template variables in methodParams before generating calldata
// Use preprocessTextWithVariableMapping for each parameter to support dot notation like {{value.address}}
resolvedMethodParams := make([]string, len(methodCall.MethodParams))
for i, param := range methodCall.MethodParams {
// LANGUAGE ENFORCEMENT: Validate Handlebars template size before preprocessing
if err := ValidateInputByLanguage(param, avsproto.Lang_LANG_HANDLEBARS); err != nil {
return &avsproto.ContractWriteNode_MethodResult{
MethodName: methodCall.MethodName,
Success: false,
Error: err.Error(),
}
}
resolvedMethodParams[i] = r.vm.preprocessTextWithVariableMapping(param)
// Validate that template resolution didn't produce "undefined" values using common utility
contextName := fmt.Sprintf("method '%s'", methodCall.MethodName)
if err := ValidateTemplateVariableResolution(resolvedMethodParams[i], param, r.vm, contextName); err != nil {
if r.vm != nil && r.vm.logger != nil {
r.vm.logger.Error("❌ CONTRACT WRITE - Template variable failed to resolve",
"method", methodCall.MethodName,
"param_index", i,
"original_param", param,
"resolved_param", resolvedMethodParams[i],
"error", err)
}
// Return error result - validation failures should stop execution
return &avsproto.ContractWriteNode_MethodResult{
MethodName: methodCall.MethodName,
Success: false,
Error: err.Error(),
}
}
}
// Handle JSON objects/arrays: convert to appropriate format based on method signature
// This supports struct/tuple parameters where the client returns objects or arrays from custom code
if len(resolvedMethodParams) == 1 {
param := resolvedMethodParams[0]
// Check if this method expects a struct parameter by examining the ABI
if parsedABI != nil {
if method, exists := parsedABI.Methods[methodCall.MethodName]; exists {
if len(method.Inputs) == 1 && method.Inputs[0].Type.T == abi.TupleTy {
// Method expects a single struct/tuple parameter
tupleType := method.Inputs[0].Type
// Handle JSON object - convert to ordered array based on struct field order
if strings.HasPrefix(param, "{") && strings.HasSuffix(param, "}") {
var objData map[string]interface{}
if err := json.Unmarshal([]byte(param), &objData); err == nil {
// Convert object to ordered array based on ABI struct field order
orderedArray := make([]interface{}, len(tupleType.TupleElems))
for i := range tupleType.TupleElems {
fieldName := tupleType.TupleRawNames[i]
if value, exists := objData[fieldName]; exists {
orderedArray[i] = value
} else {
// Field missing - return error immediately
if r.vm != nil && r.vm.logger != nil {
r.vm.logger.Error("❌ CONTRACT WRITE - Missing field in struct object",
"method", methodCall.MethodName,
"missing_field", fieldName,
"available_fields", GetMapKeys(objData))
}
return &avsproto.ContractWriteNode_MethodResult{
MethodName: methodCall.MethodName,
Success: false,
Error: fmt.Sprintf("missing required field '%s' in struct parameter for method '%s'", fieldName, methodCall.MethodName),
}
}
}
// Convert back to JSON array string for ABI processing
if jsonBytes, err := json.Marshal(orderedArray); err == nil {
resolvedMethodParams[0] = string(jsonBytes)
if r.vm != nil && r.vm.logger != nil {
r.vm.logger.Info("🔄 CONTRACT WRITE - Converted object to ordered array for struct",
"method", methodCall.MethodName,
"struct_fields", tupleType.TupleRawNames,
"ordered_array", string(jsonBytes))
}
}
}
} else if strings.HasPrefix(param, "[") && strings.HasSuffix(param, "]") {
// Handle JSON array - already in correct format for struct processing
if r.vm != nil && r.vm.logger != nil {
r.vm.logger.Info("🔄 CONTRACT WRITE - Detected struct parameter with JSON array",
"method", methodCall.MethodName,
"param_type", tupleType.String())
}
}
} else if len(method.Inputs) > 1 {
// Method expects multiple parameters - expand JSON array if provided
if strings.HasPrefix(param, "[") && strings.HasSuffix(param, "]") {
var arrayElements []interface{}
if err := json.Unmarshal([]byte(param), &arrayElements); err == nil {
expandedParams := make([]string, len(arrayElements))
for j, element := range arrayElements {
expandedParams[j] = fmt.Sprintf("%v", element)
}
resolvedMethodParams = expandedParams
if r.vm != nil && r.vm.logger != nil {
r.vm.logger.Info("🔄 CONTRACT WRITE - Expanded JSON array into individual parameters",
"method", methodCall.MethodName,
"expanded_count", len(expandedParams))
}
}
}
}
}
}
}
// After JSON array expansion, validate that no expanded parameters contain "undefined"
// This catches cases where template variables inside JSON array strings failed to resolve
if err := ValidateResolvedParams(resolvedMethodParams, methodCall.MethodParams, r.vm, fmt.Sprintf("method '%s'", methodCall.MethodName)); err != nil {
if r.vm != nil && r.vm.logger != nil {
r.vm.logger.Error("❌ CONTRACT WRITE - Template variable failed to resolve in expanded parameters",
"method", methodCall.MethodName,
"error", err)
}
// Return error result - validation failures should stop execution
return &avsproto.ContractWriteNode_MethodResult{
MethodName: methodCall.MethodName,
Success: false,
Error: err.Error(),
}
}
// Use shared utility to generate or use existing calldata
var existingCallData string
if methodCall.CallData != nil {
existingCallData = *methodCall.CallData
}
callData, err := GenerateOrUseCallData(methodCall.MethodName, existingCallData, resolvedMethodParams, parsedABI)
if err != nil {
if r.vm != nil && r.vm.logger != nil {
r.vm.logger.Error("❌ Failed to get/generate calldata for contract write",
"methodName", methodCall.MethodName,
"providedCallData", methodCall.CallData,
"rawMethodParams", methodCall.MethodParams,
"resolvedMethodParams", resolvedMethodParams,
"error", err)
}
return &avsproto.ContractWriteNode_MethodResult{
Success: false,
Error: err.Error(),
MethodName: methodCall.MethodName,
}
}
// Log successful calldata generation if needed
if existingCallData == "" && callData != "" && r.vm != nil && r.vm.logger != nil {
r.vm.logger.Debug("✅ Generated calldata from methodName and methodParams for contract write",
"methodName", methodCall.MethodName,
"rawMethodParams", methodCall.MethodParams,
"resolvedMethodParams", resolvedMethodParams,
"generatedCallData", callData)
}
calldata := common.FromHex(callData)
// Resolve method name from ABI if not provided or if provided name is UnknownMethodName
methodName := methodCall.MethodName
if parsedABI != nil && (methodName == "" || methodName == UnknownMethodName) {
if method, err := byte4.GetMethodFromCalldata(*parsedABI, calldata); err == nil {
methodName = method.Name
}
}
// 🔍 DEBUG: Log all configuration details
r.vm.logger.Info("🔍 CONTRACT WRITE DEBUG - Configuration Analysis",
"has_smart_wallet_config", r.smartWalletConfig != nil,
"method_name", methodName,
"contract_address", contractAddress.Hex())
if r.smartWalletConfig != nil {
r.vm.logger.Info("🔍 CONTRACT WRITE DEBUG - Smart Wallet Config Details",
"bundler_url", r.smartWalletConfig.BundlerURL,
"factory_address", r.smartWalletConfig.FactoryAddress,
"entrypoint_address", r.smartWalletConfig.EntrypointAddress)
} else {
r.vm.logger.Warn("⚠️ CONTRACT WRITE DEBUG - Smart wallet config is NIL!")
}
// For logging, detect RNWI context
isRunNodeWithInputs := false
if taskTypeVar, ok := r.vm.vars["task_type"]; ok {
if taskTypeStr, ok := taskTypeVar.(string); ok && taskTypeStr == "run_node_with_inputs" {
isRunNodeWithInputs = true
}
}
// Log VM mode
r.vm.logger.Debug("🔧 CONTRACT WRITE - VM mode check",
"vm_is_simulation", r.vm.IsSimulation,
"rnwi", isRunNodeWithInputs,
"should_simulate", shouldSimulate,
"method", methodName,
"contract", contractAddress.Hex())
// Use simulation if flag resolves true; otherwise perform real execution
if shouldSimulate {
r.vm.logger.Info("🔮 CONTRACT WRITE DEBUG - Using Tenderly simulation path",
"contract", contractAddress.Hex(),
"method", methodName,
"reason", "vm_is_simulation")
// Use shared Tenderly client from VM
tenderlyClient := r.vm.tenderlyClient
if tenderlyClient == nil {
return &avsproto.ContractWriteNode_MethodResult{
MethodName: methodCall.MethodName,
Success: false,
Error: "tenderlyClient is nil - cannot simulate contract write",
Value: nil,
}
}
// Get chain ID for simulation from settings only
var chainID int64
foundChainID := false
// Get chain_id from settings (snake_case only)
if settingsIface, ok := r.vm.vars["settings"]; ok {
if settings, ok := settingsIface.(map[string]interface{}); ok {
if cid, ok := settings["chain_id"]; ok {
switch v := cid.(type) {
case int64:
chainID = v
foundChainID = true
case int:
chainID = int64(v)
foundChainID = true
case float64:
chainID = int64(v)
foundChainID = true
case string:
if strings.HasPrefix(strings.ToLower(v), "0x") {
if parsed, err := strconv.ParseInt(strings.TrimPrefix(strings.ToLower(v), "0x"), 16, 64); err == nil {
chainID = parsed
foundChainID = true
}
} else if parsed, err := strconv.ParseInt(v, 10, 64); err == nil {
chainID = parsed
foundChainID = true
}
}
r.vm.logger.Debug("ContractWrite: Found chainId in settings", "chain_id", chainID)
} else {
r.vm.logger.Debug("ContractWrite: chainId not found in settings")
}
} else {
r.vm.logger.Debug("ContractWrite: settings is not a valid object")
}
} else {
r.vm.logger.Debug("ContractWrite: settings not found in VM variables")
}
if !foundChainID {
return &avsproto.ContractWriteNode_MethodResult{
MethodName: methodName,
Success: false,
Error: "settings.chain_id is required for contractWrite",
}
}
r.vm.logger.Debug("ContractWrite: resolved chain id for simulation", "chain_id", chainID)
// Get contract ABI as string
var contractAbiStr string
if parsedABI != nil && originalAbiString != "" {
// Use the original ABI string that was successfully parsed
// Don't re-marshal the parsed ABI as it changes the structure
contractAbiStr = originalAbiString
r.vm.logger.Debug("✅ CONTRACT WRITE - Using original ABI string for Tenderly",
"method", methodName, "abi_length", len(contractAbiStr))
}
// Note: HTTP Simulation API automatically uses the latest block context
// Extract transaction value from node Config or VM variables (RNWI fallback)
transactionValue := r.extractTransactionValue(node)
// Simulate the contract write using Tenderly
simulationResult, err := tenderlyClient.SimulateContractWrite(
ctx,
contractAddress.Hex(),
callData,
contractAbiStr,
methodName,
chainID,
senderAddress.Hex(), // Use runner (smart wallet) address for simulation
transactionValue, // Pass the transaction value
r.vm.simulationState,
)
if err != nil {
r.vm.logger.Warn("🚫 Tenderly simulation failed", "error", err)
// Return failure result without mock data
return &avsproto.ContractWriteNode_MethodResult{
MethodName: methodName,
Success: false,
Error: fmt.Sprintf("tenderly simulation failed: %v", err),
}
}
// Merge state diffs from this simulation into the accumulated state map
// so subsequent simulation steps see a consistent view of on-chain state.
if r.vm.simulationState != nil && simulationResult != nil && simulationResult.Success && len(simulationResult.RawStateDiff) > 0 {
r.vm.simulationState.MergeRawStateDiff(simulationResult.RawStateDiff)
r.vm.logger.Debug("Merged simulation state diff into accumulator",
"method", methodName,
"diffEntries", len(simulationResult.RawStateDiff))
}
// Convert Tenderly simulation result to legacy protobuf format
mr := r.convertTenderlyResultToFlexibleFormat(simulationResult, parsedABI, callData)
// Try to stamp real latest block number/hash from our configured RPC
if mr != nil && mr.Receipt != nil && r.client != nil {
if header, herr := r.client.HeaderByNumber(ctx, nil); herr == nil && header != nil {
if recMap, ok := mr.Receipt.AsInterface().(map[string]interface{}); ok {
recMap["blockNumber"] = fmt.Sprintf("0x%x", header.Number.Uint64())
recMap["blockHash"] = header.Hash().Hex()
if newVal, err := structpb.NewValue(recMap); err == nil {
mr.Receipt = newVal
}
}
}
}
// Fallback: if Tenderly returned latest block number, override placeholder
if simulationResult != nil && simulationResult.LatestBlockHex != "" && mr != nil && mr.Receipt != nil {
if recMap, ok := mr.Receipt.AsInterface().(map[string]interface{}); ok {
if _, has := recMap["blockNumber"]; !has || recMap["blockNumber"] == "0x1" {
recMap["blockNumber"] = simulationResult.LatestBlockHex
recMap["blockHash"] = fmt.Sprintf("0x%064s", strings.TrimPrefix(simulationResult.LatestBlockHex, "0x"))
if newVal, err := structpb.NewValue(recMap); err == nil {
mr.Receipt = newVal
}
}
}
}
// Finally, attach Tenderly logs into the flexible receipt (do this last to avoid overwrites)
r.vm.logger.Debug("LOG ATTACHMENT CHECK",
"mr_nil", mr == nil,
"receipt_nil", mr == nil || mr.Receipt == nil,
"simulation_nil", simulationResult == nil,
"receipt_logs_count", func() int {
if simulationResult != nil {
return len(simulationResult.ReceiptLogs)
}
return -1
}())
if mr != nil && mr.Receipt != nil && simulationResult != nil && len(simulationResult.ReceiptLogs) > 0 {
r.vm.logger.Debug("About to attach Tenderly logs to receipt",
"logs_count", len(simulationResult.ReceiptLogs))
if recMap, ok := mr.Receipt.AsInterface().(map[string]interface{}); ok {
logsIface := make([]interface{}, 0, len(simulationResult.ReceiptLogs))
for _, m := range simulationResult.ReceiptLogs {
logsIface = append(logsIface, m)
}
recMap["logs"] = logsIface
r.vm.logger.Debug("Attached logs to receipt map",
"logs_count", len(simulationResult.ReceiptLogs))
if newVal, err := structpb.NewValue(recMap); err == nil {
mr.Receipt = newVal
r.vm.logger.Debug("Updated receipt with logs")
} else {
r.vm.logger.Error("Failed to create new receipt value", "error", err)
}
} else {
r.vm.logger.Error("Failed to cast receipt to map")
}
} else {
r.vm.logger.Debug("Not attaching logs",
"mr_nil", mr == nil,
"receipt_nil", mr == nil || mr.Receipt == nil,
"simulation_nil", simulationResult == nil,
"logs_count", func() int {
if simulationResult != nil {
return len(simulationResult.ReceiptLogs)
}
return -1
}())
}
return mr
}
// Deployed workflows (simulation flag is false): require smartWalletConfig and use real UserOp path
r.vm.logger.Debug("🚀 CONTRACT WRITE - Going down REAL transaction path",
"is_simulation", r.vm.IsSimulation,
"method", methodName,
"contract", contractAddress.Hex())
if r.smartWalletConfig == nil {
r.vm.logger.Error("Contract write in deployed mode without smart wallet config")
return &avsproto.ContractWriteNode_MethodResult{
Success: false,
Error: "smart wallet config is required for deployed contract write",
MethodName: methodName,
}
}
r.vm.logger.Info("🚀 CONTRACT WRITE DEBUG - Using real UserOp transaction path",
"contract", contractAddress.Hex(),
"method", methodName)
return r.executeRealUserOpTransaction(ctx, contractAddress, callData, methodName, parsedABI, t0)
}
// executeRealUserOpTransaction executes a real UserOp transaction for contract writes
func (r *ContractWriteProcessor) executeRealUserOpTransaction(ctx context.Context, contractAddress common.Address, callData string, methodName string, parsedABI *abi.ABI, startTime time.Time) *avsproto.ContractWriteNode_MethodResult {
r.vm.logger.Info("🔍 REAL USEROP DEBUG - Starting real UserOp transaction execution",
"contract_address", contractAddress.Hex(),
"method_name", methodName,
"calldata_length", len(callData),
"calldata", callData,
"owner_eoaAddress", r.owner.Hex())
// Initialize execution log builder to capture all details
var executionLogBuilder strings.Builder
executionLogBuilder.WriteString(fmt.Sprintf("UserOp Transaction Execution for %s\n", methodName))
executionLogBuilder.WriteString(fmt.Sprintf("Contract: %s\n", contractAddress.Hex()))
executionLogBuilder.WriteString(fmt.Sprintf("Owner EOA: %s\n", r.owner.Hex()))
// Convert hex calldata to bytes
callDataBytes := common.FromHex(callData)
// 🔍 PRE-FLIGHT VALIDATION: Check for common failure scenarios before gas estimation
if validationErr := r.validateTransactionBeforeGasEstimation(methodName, callData, callDataBytes, contractAddress); validationErr != nil {
executionLogBuilder.WriteString(fmt.Sprintf("PRE-FLIGHT VALIDATION FAILED: %v\n", validationErr))
executionLogBuilder.WriteString("Skipped gas estimation to avoid bundler error\n")
r.vm.logger.Error("🚫 PRE-FLIGHT VALIDATION FAILED - Skipping gas estimation to avoid bundler error",
"validation_error", validationErr.Error(),
"method", methodName,
"contract", contractAddress.Hex())
return &avsproto.ContractWriteNode_MethodResult{
Success: false,
Error: fmt.Sprintf("Pre-flight validation failed: %v", validationErr),
}
}
// Create smart wallet execute calldata: execute(target, value, data)
executionLogBuilder.WriteString(fmt.Sprintf("Packing smart wallet execute calldata...\n"))
executionLogBuilder.WriteString(fmt.Sprintf(" Target contract: %s\n", contractAddress.Hex()))
executionLogBuilder.WriteString(fmt.Sprintf(" ETH value: 0\n"))
executionLogBuilder.WriteString(fmt.Sprintf(" Method calldata: %d bytes\n", len(callDataBytes)))
smartWalletCallData, err := aa.PackExecute(
contractAddress, // target contract
big.NewInt(0), // ETH value (0 for contract calls)
callDataBytes, // contract method calldata
)
if err != nil {
executionLogBuilder.WriteString(fmt.Sprintf("CALLDATA PACKING FAILED: %v\n", err))
r.vm.logger.Error("🚨 DEPLOYED WORKFLOW ERROR: Failed to pack smart wallet execute calldata",
"error", err,
"contract_address", contractAddress.Hex(),
"method_name", methodName,
"calldata_bytes_length", len(callDataBytes))
// Return error result - workflow execution FAILS (no fallback for deployed workflows)
return &avsproto.ContractWriteNode_MethodResult{
Success: false,
Error: fmt.Sprintf("Failed to pack smart wallet execute calldata: %v", err),
}
}
executionLogBuilder.WriteString(fmt.Sprintf("Smart wallet calldata packed: %d bytes\n", len(smartWalletCallData)))
// Set up factory address for AA operations
aa.SetFactoryAddress(r.smartWalletConfig.FactoryAddress)
aa.SetEntrypointAddress(r.smartWalletConfig.EntrypointAddress)
// Optional runner validation: if task.SmartWalletAddress is set, ensure it matches
// one of the owner EOA's known smart wallets (authoritative). If wallet list cannot be checked,
// fall back to checking the derived salt:0 address as a best-effort sanity check.
if r.vm.task != nil && r.vm.task.Task != nil && r.vm.task.SmartWalletAddress != "" {
runnerStr := r.vm.task.SmartWalletAddress
client, err := ethclient.Dial(r.smartWalletConfig.EthRpcUrl)
if err == nil {
// derive sender at salt:0
sender, derr := aa.GetSenderAddress(client, r.owner, big.NewInt(0))
client.Close()
if derr == nil && sender != nil {
if !strings.EqualFold(sender.Hex(), runnerStr) {
// Do not fail solely on derived salt:0 mismatch; authoritative check is the wallet list in run_node path
r.vm.logger.Warn("runner does not match derived salt:0; proceeding (wallet list validation applies in run_node)", "expected", sender.Hex(), "runner", runnerStr)
}
}
}
}
// Determine if paymaster should be used based on transaction limits and whitelist
var paymasterReq *preset.VerifyingPaymasterRequest
if r.shouldUsePaymaster() {
paymasterReq = preset.GetVerifyingPaymasterRequestForDuration(
r.smartWalletConfig.PaymasterAddress,
15*time.Minute, // 15 minute validity window
)
r.vm.logger.Info("Using paymaster for sponsored transaction",
"paymaster", r.smartWalletConfig.PaymasterAddress.Hex(),
"owner", r.owner.Hex())
} else {
r.vm.logger.Info("Using regular transaction (no paymaster)",
"owner", r.owner.Hex())
}
// Determine AA overrides from VM vars: get smart wallet address and salt for senderOverride
var senderOverride *common.Address
var saltOverride *big.Int
r.vm.mu.Lock()
if v, ok := r.vm.vars["aa_sender"]; ok {
if s, ok2 := v.(string); ok2 && common.IsHexAddress(s) {
addr := common.HexToAddress(s)
senderOverride = &addr
r.vm.logger.Info("🔍 DEPLOYED WORKFLOW: UserOp sender configuration",
"owner_eoaAddress", r.owner.Hex(),
"senderOverride_smartWallet", addr.Hex(),
"aa_sender_var", s)
}
}
if v, ok := r.vm.vars["aa_salt"]; ok {
if s, ok2 := v.(*big.Int); ok2 {
saltOverride = s
}
}
r.vm.mu.Unlock()
if senderOverride == nil {
executionLogBuilder.WriteString("WARNING: aa_sender not found in VM vars\n")
r.vm.logger.Error("🚨 DEPLOYED WORKFLOW ERROR: aa_sender not found in VM vars",
"owner_eoaAddress", r.owner.Hex())
} else {
executionLogBuilder.WriteString(fmt.Sprintf("Smart wallet sender: %s\n", senderOverride.Hex()))
}
// Add paymaster information to execution log
if paymasterReq != nil {
executionLogBuilder.WriteString(fmt.Sprintf("Using paymaster: %s\n", r.smartWalletConfig.PaymasterAddress.Hex()))
} else {
executionLogBuilder.WriteString("No paymaster (self-funded transaction)\n")
}
// Pre-send gas estimation to capture in logs
executionLogBuilder.WriteString("Performing gas estimation...\n")
// Create a temporary UserOp for gas estimation
rpcClient, rpcErr := ethclient.Dial(r.smartWalletConfig.EthRpcUrl)
if rpcErr != nil {
executionLogBuilder.WriteString(fmt.Sprintf("Failed to connect to RPC: %v\n", rpcErr))
} else {
defer rpcClient.Close()
_, bundlerErr := bundler.NewBundlerClient(r.smartWalletConfig.BundlerURL)
if bundlerErr != nil {
executionLogBuilder.WriteString(fmt.Sprintf("Failed to create bundler client: %v\n", bundlerErr))
} else {
// Check smart wallet balance
smartWalletAddr := senderOverride
if smartWalletAddr != nil {
if balance, balErr := rpcClient.BalanceAt(ctx, *smartWalletAddr, nil); balErr == nil {
executionLogBuilder.WriteString(fmt.Sprintf("Smart wallet balance: %s wei\n", balance.String()))
} else {
executionLogBuilder.WriteString(fmt.Sprintf("Failed to check balance: %v\n", balErr))
}
}
// Try to get current gas prices
if maxFee, maxPriority, feeErr := eip1559.SuggestFee(rpcClient); feeErr == nil {
executionLogBuilder.WriteString(fmt.Sprintf("Current gas prices:\n"))
executionLogBuilder.WriteString(fmt.Sprintf(" MaxFeePerGas: %s wei\n", maxFee.String()))
executionLogBuilder.WriteString(fmt.Sprintf(" MaxPriorityFeePerGas: %s wei\n", maxPriority.String()))
} else {
executionLogBuilder.WriteString(fmt.Sprintf("Failed to get gas prices: %v\n", feeErr))
}
}
}
executionLogBuilder.WriteString("Sending packed User Operation to bundler\n")
// Send UserOp transaction with correct parameters:
// - owner: EOA address (r.owner) for smart wallet derivation
// - senderOverride: smart wallet address (aa_sender) for the actual transaction
userOp, receipt, err := r.sendUserOpFunc(
r.smartWalletConfig,
r.owner, // Use EOA address (owner) for smart wallet derivation
smartWalletCallData,
paymasterReq, // Use paymaster for wallet creation/sponsorship if shouldUsePaymaster() returned true
senderOverride, // Smart wallet address from aa_sender
saltOverride, // Salt for wallet auto-deployment (from aa_salt VM var)
r.vm.executionFeeWei, // Execution fee in Wei (nil = no fee)
r.vm.logger, // Pass logger for debug/verbose logging
)
// Increment transaction counter for this address (regardless of success/failure)
if r.vm.db != nil {
counterKey := ContractWriteCounterKey(r.owner)
if _, err := r.vm.db.IncCounter(counterKey, 0); err != nil {
r.vm.logger.Warn("Failed to increment transaction counter", "error", err)
}
}
if err != nil {
// Add detailed error information to execution log (internal)
executionLogBuilder.WriteString(fmt.Sprintf("BUNDLER FAILED: UserOp transaction could not be sent\n"))
executionLogBuilder.WriteString(fmt.Sprintf("Error: %v\n", err))
// Check if this is specifically the AA21 prefund error and add detailed explanation
if strings.Contains(err.Error(), "AA21") {
executionLogBuilder.WriteString("AA21 PREFUND ERROR DETECTED\n")
executionLogBuilder.WriteString("This indicates insufficient ETH balance for gas fees\n")
executionLogBuilder.WriteString("Solution: Fund the smart wallet with ETH for gas fees\n")
// Add gas estimation details if available from userOp
if userOp != nil {
executionLogBuilder.WriteString("Gas Requirements (if estimated):\n")
// Only show gas limits if they were actually estimated (not default values)
// Use the shared constant from preset package to avoid duplication
defaultCallGasLimit := preset.DEFAULT_CALL_GAS_LIMIT
defaultVerificationGasLimit := preset.DEFAULT_VERIFICATION_GAS_LIMIT
defaultPreVerificationGas := preset.DEFAULT_PREVERIFICATION_GAS
if userOp.CallGasLimit != nil && userOp.CallGasLimit.Cmp(defaultCallGasLimit) != 0 {
executionLogBuilder.WriteString(fmt.Sprintf(" CallGasLimit: %s\n", userOp.CallGasLimit.String()))
}
if userOp.VerificationGasLimit != nil && userOp.VerificationGasLimit.Cmp(defaultVerificationGasLimit) != 0 {
executionLogBuilder.WriteString(fmt.Sprintf(" VerificationGasLimit: %s\n", userOp.VerificationGasLimit.String()))
}
if userOp.PreVerificationGas != nil && userOp.PreVerificationGas.Cmp(defaultPreVerificationGas) != 0 {
executionLogBuilder.WriteString(fmt.Sprintf(" PreVerificationGas: %s\n", userOp.PreVerificationGas.String()))
}
if userOp.MaxFeePerGas != nil {
executionLogBuilder.WriteString(fmt.Sprintf(" MaxFeePerGas: %s wei\n", userOp.MaxFeePerGas.String()))
}
}
}
// preset.LogBundlerError picks Error vs Warn based on the error: on-chain
// reverts (expected user-workflow outcomes) log at Warn so they don't page
// Sentry; real infra/AA failures (bundler down, AA21/AA23/AA25, paymaster
// revert) stay at Error.
preset.LogBundlerError(r.vm.logger, err,
"bundler: UserOp transaction failed, workflow execution FAILED",
"bundler_error", err,
"bundler_url", r.smartWalletConfig.BundlerURL,
"method", methodName,
"contract", contractAddress.Hex(),
"sender_smart_wallet", func() string {
if senderOverride != nil {
return senderOverride.Hex()
}
return "not_set"
}(),
"owner_eoa", r.owner.Hex())
// Check if this is specifically the AA21 prefund error
if strings.Contains(err.Error(), "AA21") {
r.vm.logger.Error("🚨 AA21 PREFUND ERROR DETECTED - This indicates insufficient ETH balance for gas fees",
"error_code", "AA21",
"meaning", "didn't pay prefund",
"solution", "Fund the smart wallet with ETH for gas fees")
}
// Create simplified user-facing error message
var userErrorMsg string
if strings.Contains(err.Error(), "connection refused") || strings.Contains(err.Error(), "dial tcp") {
userErrorMsg = "Bundler service unavailable"
} else if strings.Contains(err.Error(), "AA21") {
userErrorMsg = "Insufficient ETH balance for gas fees"
} else if strings.Contains(err.Error(), "AA") {
// Parse standard AA error codes like AA10, AA21, AA23, etc.
// Avoid falsely matching 'AA' inside hex strings or addresses.
if code := regexp.MustCompile(`AA\d{2}`).FindString(err.Error()); code != "" {
userErrorMsg = code
} else if strings.Contains(strings.ToLower(err.Error()), "not deployed") ||
strings.Contains(strings.ToLower(err.Error()), "override is not deployed") ||
strings.Contains(strings.ToLower(err.Error()), "sender not deployed") {
userErrorMsg = "Smart wallet not deployed"
} else {
userErrorMsg = "Transaction validation failed"
}
} else {
// Extract first meaningful error line without verbose details
errStr := err.Error()
if idx := strings.Index(errStr, "\n"); idx > 0 {
userErrorMsg = errStr[:idx]
} else {
userErrorMsg = errStr
}
// Limit length to avoid extremely long messages
if len(userErrorMsg) > 200 {
userErrorMsg = userErrorMsg[:200] + "..."
}
}
// Return error result with simplified user message
// Detailed execution log is available in server logs for debugging
return &avsproto.ContractWriteNode_MethodResult{
Success: false,
Error: userErrorMsg,
MethodName: methodName,
}
}
// Add success information to execution log
executionLogBuilder.WriteString("BUNDLER SUCCESS: UserOp transaction sent successfully\n")
if userOp != nil {
executionLogBuilder.WriteString(fmt.Sprintf("UserOp Hash: %s\n", r.getUserOpHashOrPending(receipt)))
executionLogBuilder.WriteString(fmt.Sprintf("Sender: %s\n", userOp.Sender.Hex()))
executionLogBuilder.WriteString(fmt.Sprintf("Nonce: %s\n", userOp.Nonce.String()))
}
if receipt != nil {
executionLogBuilder.WriteString(fmt.Sprintf("Transaction Hash: %s\n", receipt.TxHash.Hex()))
executionLogBuilder.WriteString(fmt.Sprintf("Gas Used: %d\n", receipt.GasUsed))
executionLogBuilder.WriteString(fmt.Sprintf("Block Number: %d\n", receipt.BlockNumber.Uint64()))
}
// Create result from real transaction
result := r.createRealTransactionResult(methodName, contractAddress.Hex(), callData, parsedABI, userOp, receipt)
// Error field should contain only the summary (first sentence)
// Detailed logs are already in the execution log (log field)
// Do NOT append executionLogBuilder to the error field
return result
}
// createRealTransactionResult creates a result from a real UserOp transaction
func (r *ContractWriteProcessor) createRealTransactionResult(methodName, contractAddress, callData string, parsedABI *abi.ABI, userOp *userop.UserOperation, receipt *types.Receipt) *avsproto.ContractWriteNode_MethodResult {
r.vm.logger.Info("🔍 DEPLOYED WORKFLOW: Creating real transaction result",
"method_name", methodName,
"contract_address", contractAddress,
"has_receipt", receipt != nil,
"has_userop", userOp != nil)
if receipt != nil {
r.vm.logger.Info("📋 DEPLOYED WORKFLOW: Transaction receipt details",
"tx_hash", receipt.TxHash.Hex(),
"status", receipt.Status,
"gas_used", receipt.GasUsed,
"block_number", receipt.BlockNumber.Uint64(),
"logs_count", len(receipt.Logs))
} else {
r.vm.logger.Error("🚨 DEPLOYED WORKFLOW ERROR: No receipt available for transaction result")
}
// Extract methodABI from contract ABI if available
var methodABI *structpb.Value
if parsedABI != nil {
if method, exists := parsedABI.Methods[methodName]; exists {
if abiMap := r.extractMethodABI(&method); abiMap != nil {
if abiValue, err := structpb.NewValue(abiMap); err == nil {
methodABI = abiValue
}
}
}
}
// Create receipt data from real transaction
var receiptMap map[string]interface{}
if receipt != nil {
// Get transaction details for from/to fields
var fromAddr, toAddr string
// Get the actual sender (runner) from VM variables
actualSender := r.owner // Default to owner (eoaAddress)
if aaSenderVar, ok := r.vm.vars["aa_sender"]; ok {
if aaSenderStr, ok := aaSenderVar.(string); ok && aaSenderStr != "" {
actualSender = common.HexToAddress(aaSenderStr)
}
}
if r.smartWalletConfig != nil {
// For UserOp transactions, 'from' is the smart wallet address (runner)
fromAddr = actualSender.Hex()
toAddr = contractAddress // contractAddress is already a string
} else {
// Fallback for regular transactions
fromAddr = actualSender.Hex()
toAddr = contractAddress // contractAddress is already a string
}
// Real transaction receipt with standard Ethereum fields
receiptMap = map[string]interface{}{
"transactionHash": receipt.TxHash.Hex(),
"blockNumber": fmt.Sprintf("0x%x", receipt.BlockNumber.Uint64()),
"blockHash": receipt.BlockHash.Hex(),
"transactionIndex": fmt.Sprintf("0x%x", receipt.TransactionIndex),
"from": fromAddr,
"to": toAddr,
"gasUsed": fmt.Sprintf("0x%x", receipt.GasUsed),
"cumulativeGasUsed": fmt.Sprintf("0x%x", receipt.CumulativeGasUsed),
"effectiveGasPrice": fmt.Sprintf("0x%x", receipt.EffectiveGasPrice.Uint64()),
"status": fmt.Sprintf("0x%x", receipt.Status),
"type": fmt.Sprintf("0x%x", receipt.Type),
"logsBloom": fmt.Sprintf("0x%x", receipt.Bloom),
"logs": convertLogsToInterface(receipt.Logs),
}
} else if userOp != nil {
// UserOp submitted but receipt not available yet
receiptMap = map[string]interface{}{
"userOpHash": userOp.GetUserOpHash(r.smartWalletConfig.EntrypointAddress, big.NewInt(r.smartWalletConfig.ChainID)).Hex(),
"sender": userOp.Sender.Hex(),
"nonce": fmt.Sprintf("0x%x", userOp.Nonce.Uint64()),
"status": "pending",
"transactionHash": "pending", // Will be available once bundler processes the UserOp
}
} else {
// Neither receipt nor userOp available - this shouldn't happen but handle gracefully
receiptMap = map[string]interface{}{
"status": "unknown",
"transactionHash": "unknown",
"error": "Neither receipt nor UserOp available",
}
}
receiptValue, _ := structpb.NewValue(receiptMap)
// Initialize UserOp success tracking (for Account Abstraction)
userOpEventTopic := common.HexToHash("0x49628fd1471006c1482da88028e9ce4dbb080b815c9b0344d39e5a8e6ec1419f")
userOpInnerSuccess := true // Default to true for non-AA transactions