-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexecution_error.go
More file actions
968 lines (809 loc) · 29.3 KB
/
Copy pathexecution_error.go
File metadata and controls
968 lines (809 loc) · 29.3 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
package evm
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"math/big"
"reflect"
"regexp"
"strconv"
"strings"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
gethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/smartcontractkit/mcms/sdk/evm/bindings"
)
var (
// CallRevertedSelector is the 4-byte selector for the CallReverted error from MCMS 0x70de1b4b
CallRevertedSelector = [4]byte{0x70, 0xde, 0x1b, 0x4b}
// hexPattern matches "0x" followed by one or more hex characters
hexPattern = regexp.MustCompile(`0x[0-9a-fA-F]+`)
// bytesArrayPattern matches "[[...]]" and captures the content between brackets
bytesArrayPattern = regexp.MustCompile(`(?s)\[\[(.*)\]\]`)
// customErrorPattern matches "custom error 0x<8-hex-chars>: <hex-data>"
// Captures: group 1 = selector (8 hex chars), group 2 = data (hex chars with optional spaces)
customErrorPattern = regexp.MustCompile(`custom error 0x([0-9a-fA-F]{8}):\s*([0-9a-fA-F\s]+)`)
// callProxyRuntimePrefix and callProxyRuntimeSuffix fingerprint the deployed
// runtime of bindings.CallProxy (immutable target embedded between them).
// If CallProxy is recompiled, update these and TestCallProxyBytecodeFingerprintMatchesBinding.
callProxyRuntimePrefix = common.FromHex("0x60806040527f")
callProxyRuntimeSuffix = common.FromHex("0x366000803760008036600034855af13d6000803e80156043573d6000f35b503d6000fd")
)
const (
selectorSize = 4
revertPrefix = "revert:"
)
// CustomErrorData contains the error selector and its arguments separately.
type CustomErrorData struct {
Selector [4]byte // 4-byte error selector
Data []byte // Error arguments (ABI-encoded)
}
// MarshalJSON renders the selector/data as hex strings for readability.
func (c *CustomErrorData) MarshalJSON() ([]byte, error) {
if c == nil {
return []byte("null"), nil
}
payload := struct {
Selector string `json:"selector"`
Data string `json:"data,omitempty"`
}{
Selector: "0x" + common.Bytes2Hex(c.Selector[:]),
}
if len(c.Data) > 0 {
payload.Data = "0x" + common.Bytes2Hex(c.Data)
}
return json.Marshal(payload)
}
// UnmarshalJSON unmarshals JSON into CustomErrorData, handling hex string fields.
func (c *CustomErrorData) UnmarshalJSON(data []byte) error {
if c == nil {
return nil
}
var payload struct {
Selector string `json:"selector"`
Data string `json:"data,omitempty"`
}
if err := json.Unmarshal(data, &payload); err != nil {
return fmt.Errorf("failed to unmarshal CustomErrorData: %w", err)
}
if payload.Selector != "" {
selectorBytes, err := hexutil.Decode(payload.Selector)
if err == nil && len(selectorBytes) >= 4 {
copy(c.Selector[:], selectorBytes[:4])
}
}
if payload.Data != "" {
dataBytes, err := hexutil.Decode(payload.Data)
if err != nil {
return fmt.Errorf("failed to unmarshal CustomErrorData: %w", err)
}
c.Data = dataBytes
}
return nil
}
// Combined returns the full revert data (selector + data) as a byte slice.
func (c *CustomErrorData) Combined() []byte {
if c == nil {
return nil
}
return append(c.Selector[:], c.Data...)
}
// HexSelector returns the hex-encoded string representation of the selector (e.g., "0x70de1b4b").
func (c *CustomErrorData) HexSelector() string {
if c == nil {
return ""
}
return common.Bytes2Hex(c.Selector[:])
}
// ExecutionError represents an error that occurred during transaction execution.
// It includes the transaction data and revert reason for debugging and retry purposes.
type ExecutionError struct {
// Transaction is the pre-packed transaction data that can be used to retry or simulate
Transaction *gethtypes.Transaction
// RevertReasonRaw contains the error selector and raw data from the contract
// This allows for easy comparison of error selectors (e.g., to check if it's CallReverted)
RevertReasonRaw *CustomErrorData
// RevertReasonDecoded is the human-readable decoded revert reason from the contract (e.g., "InsufficientSigners()")
// This is only populated if the revert data could be decoded using contract ABIs
RevertReasonDecoded string
// UnderlyingReasonRaw contains the raw revert reason returned by a nested contract call (plain string or hex)
UnderlyingReasonRaw string
// UnderlyingReasonDecoded is the human-readable decoded underlying revert reason (if decoding succeeded)
UnderlyingReasonDecoded string
// OriginalError is the original error from the contract binding
OriginalError error `json:"-"`
}
func (e *ExecutionError) Error() string {
if e.UnderlyingReasonDecoded != "" {
return fmt.Sprintf("execution failed: %v (underlying reason: %s)", e.OriginalError, e.UnderlyingReasonDecoded)
}
if e.UnderlyingReasonRaw != "" {
return fmt.Sprintf("execution failed: %v (underlying reason: %s)", e.OriginalError, e.UnderlyingReasonRaw)
}
if e.RevertReasonDecoded != "" {
return fmt.Sprintf("execution failed: %v (revert reason: %s)", e.OriginalError, e.RevertReasonDecoded)
}
if e.RevertReasonRaw != nil && len(e.RevertReasonRaw.Combined()) > 0 {
return fmt.Sprintf("execution failed: %v (raw revert data: %s)", e.OriginalError, common.Bytes2Hex(e.RevertReasonRaw.Combined()))
}
return fmt.Sprintf("execution failed: %v", e.OriginalError)
}
func (e *ExecutionError) Unwrap() error {
return e.OriginalError
}
// MarshalJSON renders the execution error with a stringified OriginalError for portability.
func (e *ExecutionError) MarshalJSON() ([]byte, error) {
if e == nil {
return []byte("null"), nil
}
type alias ExecutionError
copyAlias := alias(*e)
copyAlias.OriginalError = nil
payload := struct {
*alias
OriginalError string `json:"OriginalError,omitempty"`
}{
alias: ©Alias,
}
if e.OriginalError != nil {
payload.OriginalError = e.OriginalError.Error()
}
return json.Marshal(payload)
}
// UnmarshalJSON unmarshals JSON into ExecutionError, handling OriginalError as a string.
func (e *ExecutionError) UnmarshalJSON(data []byte) error {
if e == nil {
return nil
}
var jsonMap map[string]any
if err := json.Unmarshal(data, &jsonMap); err != nil {
return fmt.Errorf("error unmarshaling ExecutionError: %w", err)
}
var originalErr error
if origErrVal, ok := jsonMap["OriginalError"]; ok {
if origErrStr, ok := origErrVal.(string); ok && origErrStr != "" {
originalErr = errors.New(origErrStr)
} else if origErrMap, ok := origErrVal.(map[string]any); ok && len(origErrMap) == 0 {
originalErr = nil
}
delete(jsonMap, "OriginalError")
}
cleanData, err := json.Marshal(jsonMap)
if err != nil {
return fmt.Errorf("error re-marshaling ExecutionError data: %w", err)
}
type executionErrorAlias struct {
Transaction *gethtypes.Transaction `json:"Transaction"`
RawRevertReason *CustomErrorData `json:"RevertReasonRaw"`
DecodedRevertReason string `json:"RevertReasonDecoded"`
UnderlyingReason string `json:"UnderlyingReasonRaw"`
DecodedUnderlyingReason string `json:"UnderlyingReasonDecoded"`
}
var temp executionErrorAlias
if err := json.Unmarshal(cleanData, &temp); err != nil {
return fmt.Errorf("error unmarshaling ExecutionError fields: %w", err)
}
e.Transaction = temp.Transaction
e.RevertReasonRaw = temp.RawRevertReason
e.RevertReasonDecoded = temp.DecodedRevertReason
e.UnderlyingReasonRaw = temp.UnderlyingReason
e.UnderlyingReasonDecoded = temp.DecodedUnderlyingReason
if originalErr != nil {
e.OriginalError = originalErr
}
return nil
}
// BuildExecutionError creates an ExecutionError from a contract execution error.
// It extracts revert reasons and handles special cases like RBACTimelock underlying transaction reverts.
// timelockAddr and timelockCallData are only needed for timelock execute cases (bypass or regular).
func BuildExecutionError(
ctx context.Context,
err error,
txPreview *gethtypes.Transaction,
opts *bind.TransactOpts,
contractAddr common.Address,
client ContractDeployBackend,
timelockAddr common.Address,
timelockCallData []byte,
) *ExecutionError {
if err == nil {
return nil
}
errStr := err.Error()
execErr := &ExecutionError{
Transaction: txPreview,
OriginalError: err,
}
// Extract both raw revert data and decoded revert reason from the error (best-effort)
revertData := extractRevertReasonFromError(err)
// If we have CustomErrorData directly, use it; otherwise construct from RawData
if revertData.CustomError != nil {
execErr.RevertReasonRaw = revertData.CustomError
} else if len(revertData.RawData) > 0 {
// Extract selector and data separately from raw data
if len(revertData.RawData) >= selectorSize {
var selector [selectorSize]byte
copy(selector[:], revertData.RawData[:selectorSize])
execErr.RevertReasonRaw = &CustomErrorData{
Selector: selector,
Data: revertData.RawData[selectorSize:],
}
} else {
execErr.RevertReasonRaw = &CustomErrorData{
Selector: [selectorSize]byte{},
Data: revertData.RawData,
}
}
}
execErr.RevertReasonDecoded = revertData.Decoded
// Check if this is an RBACTimelock underlying transaction revert
// This can appear in multiple ways:
// 1. As a plain string in the error: "RBACTimelock: underlying transaction reverted"
// 2. As a decoded custom error that contains this message
// 3. As a CallReverted error (selector 0x70de1b4b) which wraps the timelock revert
// In this case, we need to use CallContract to get the actual underlying revert reason.
// If extraction fails, just leave UnderlyingReasonRaw empty
rawRevertReasonHasCallRevertedSelector := execErr.RevertReasonRaw != nil && execErr.RevertReasonRaw.Selector == CallRevertedSelector
rawDataHasCallRevertedSelector := len(revertData.RawData) >= selectorSize && string(revertData.RawData[:4]) == string(CallRevertedSelector[:])
isCallReverted := strings.Contains(errStr, "CallReverted") ||
strings.Contains(execErr.RevertReasonDecoded, "CallReverted") ||
rawRevertReasonHasCallRevertedSelector ||
rawDataHasCallRevertedSelector
if strings.Contains(errStr, "RBACTimelock: underlying transaction reverted") ||
strings.Contains(execErr.RevertReasonDecoded, "RBACTimelock: underlying transaction reverted") ||
(isCallReverted && timelockAddr != (common.Address{}) && len(timelockCallData) > 0) {
underlyingCallSender := resolveUnderlyingCallSender(ctx, timelockAddr, client)
rawUnderlyingReason, decodedUnderlyingReason := getUnderlyingRevertReason(ctx, underlyingCallSender, timelockCallData, opts, client)
execErr.UnderlyingReasonRaw = rawUnderlyingReason
execErr.UnderlyingReasonDecoded = decodedUnderlyingReason
}
return execErr
}
func resolveUnderlyingCallSender(ctx context.Context, executionAddr common.Address, client ContractDeployBackend) common.Address {
if executionAddr != (common.Address{}) && client != nil {
if code, err := client.CodeAt(ctx, executionAddr, nil); err == nil {
if targetAddress, ok := callProxyTargetFromRuntime(code); ok {
return targetAddress
}
}
}
return executionAddr
}
// callProxyTargetFromRuntime checks whether code matches bindings.CallProxy runtime
// (fixed prefix + suffix with a 32-byte immutable target between them) and returns that target.
func callProxyTargetFromRuntime(code []byte) (common.Address, bool) {
targetOffset := len(callProxyRuntimePrefix)
suffixOffset := targetOffset + common.HashLength
if len(code) < suffixOffset+len(callProxyRuntimeSuffix) {
return common.Address{}, false
}
if !bytes.Equal(code[:targetOffset], callProxyRuntimePrefix) {
return common.Address{}, false
}
if !bytes.Equal(code[suffixOffset:suffixOffset+len(callProxyRuntimeSuffix)], callProxyRuntimeSuffix) {
return common.Address{}, false
}
return common.BytesToAddress(code[targetOffset:suffixOffset]), true
}
// extractHexEncodedRevertData extracts hex-encoded revert data (0x...) from an error string.
// Returns the extracted bytes if found, nil otherwise.
func extractHexEncodedRevertData(errStr string) []byte {
hexStr := hexPattern.FindString(errStr)
if hexStr == "" {
return nil
}
if data := common.FromHex(hexStr); len(data) > 0 {
return data
}
return nil
}
// extractBytesArrayRevertData extracts bytes array format revert data from an error string.
// Format: "[[...bytes...]]" where bytes are space-separated decimal values.
// Returns the extracted bytes if found, nil otherwise.
func extractBytesArrayRevertData(errStr string) []byte {
matches := bytesArrayPattern.FindStringSubmatch(errStr)
if len(matches) < 2 { //nolint
return nil
}
bytesStr := matches[1] // First capture group contains the content between brackets
data := parseBytesFromString(bytesStr)
if len(data) > 0 {
return data
}
return nil
}
// extractCustomErrorRevertData extracts revert data from the "custom error 0x...: <hex data>" format.
// Format: "execution reverted: custom error 0x70de1b4b: 00000000000000000000000000000000…00000000000000000000000000000000"
// Returns the error selector and data separately, or nil if not found.
func extractCustomErrorRevertData(errStr string) *CustomErrorData {
matches := customErrorPattern.FindStringSubmatch(errStr)
if len(matches) < 3 { //nolint
return nil
}
// Extract selector (8 hex chars = 4 bytes) using shared hex extractor
selectorBytes := extractHexEncodedRevertData("0x" + matches[1])
if len(selectorBytes) != selectorSize {
return nil
}
// Extract and clean data (remove spaces) before reusing the hex extractor
dataHex := strings.ReplaceAll(matches[2], " ", "")
data := extractHexEncodedRevertData("0x" + dataHex)
if len(data) == 0 {
return nil
}
var selector [4]byte
copy(selector[:], selectorBytes)
return &CustomErrorData{
Selector: selector,
Data: data,
}
}
// revertReasonData contains both the raw revert data and the decoded revert reason
type revertReasonData struct {
RawData []byte // Raw hex-encoded revert data (full data including selector)
Decoded string // Human-readable decoded revert reason
CustomError *CustomErrorData // CustomErrorData if extracted directly (selector + data separately)
}
// extractRevertReasonFromError extracts both raw revert data and decoded revert reason from a bind error.
// Returns the raw data (hex-encoded) and the decoded reason (if decoding was successful).
func extractRevertReasonFromError(err error) revertReasonData {
if err == nil {
return revertReasonData{}
}
errStr := err.Error()
// Check for plain string reverts first
// (e.g., "execution reverted: revert: Ownable: caller is not the owner")
if strings.Contains(errStr, revertPrefix) {
// Try to extract the plain string revert reason
// Format: "execution reverted: revert: <reason>" or "revert: <reason>"
revertIdx := strings.Index(errStr, revertPrefix)
if revertIdx != -1 {
// Extract everything after "revert: "
reason := strings.TrimSpace(errStr[revertIdx+len(revertPrefix):])
if reason != "" {
return revertReasonData{
Decoded: reason,
}
}
}
}
// Try to extract from "custom error 0x...: <hex data>" format
if strings.Contains(errStr, "custom error") {
customErr := extractCustomErrorRevertData(errStr)
if customErr != nil {
rawData := customErr.Combined()
decoded := decodeRevertReasonFromCustomError(customErr)
return revertReasonData{
RawData: rawData,
Decoded: decoded,
CustomError: customErr, // Store CustomErrorData directly for easy selector access
}
}
}
// Try to extract revert data from error string using array, hex or bytesArray formats
if strings.Contains(errStr, "CallReverted") {
if rawData := extractBytesArrayRevertData(errStr); len(rawData) > 0 {
return revertReasonData{
RawData: rawData,
Decoded: decodeRevertReason(rawData),
}
}
}
if rawData := extractHexEncodedRevertData(errStr); len(rawData) > 0 {
return revertReasonData{
RawData: rawData,
Decoded: decodeRevertReason(rawData),
}
}
if rawData := extractBytesArrayRevertData(errStr); len(rawData) > 0 {
return revertReasonData{
RawData: rawData,
Decoded: decodeRevertReason(rawData),
}
}
return revertReasonData{}
}
// parseBytesFromString parses a string representation of bytes like "8 195 121 160 ..."
func parseBytesFromString(s string) []byte {
parts := strings.Fields(s)
if len(parts) == 0 {
return nil
}
parsed := make([]byte, 0, len(parts))
for _, part := range parts {
if val, err := strconv.ParseUint(part, 10, 8); err == nil {
parsed = append(parsed, byte(val))
}
}
return parsed
}
// decodeRevertReasonFromCustomError decodes the revert reason using the error selector to find
// the matching error definition in the contract ABIs, then decodes the data part.
// Prioritizes selector matching for efficient error identification.
func decodeRevertReasonFromCustomError(customErr *CustomErrorData) string {
if customErr == nil {
return ""
}
selector := customErr.Selector[:]
// Try to decode using contract ABIs (MCMS first, then Timelock)
if decoded := tryDecodeWithContractABIs(selector, customErr.Data); decoded != "" {
return decoded
}
// Fallback: try standard Solidity Error(string) or Panic(uint256) using go-ethereum's helper
fullData := customErr.Combined()
if reason, err := abi.UnpackRevert(fullData); err == nil {
return reason
}
return ""
}
// decodeRevertReason decodes the revert reason from ABI-encoded data.
// First tries to decode using MCMS and RBACTimelock ABIs for custom errors.
// If that fails, falls back to Error(string) decoding.
// Returns empty string if all decoding fails (so we can return the original error).
func decodeRevertReason(data []byte) string {
if len(data) < selectorSize {
return ""
}
// Try to decode using contract ABIs (MCMS first, then Timelock)
selector := data[:selectorSize]
if decoded := tryDecodeWithContractABIs(selector, data[4:]); decoded != "" {
return decoded
}
// Fallback to standard Solidity Error(string) or Panic(uint256) using go-ethereum's helper
if reason, err := abi.UnpackRevert(data); err == nil {
return reason
}
return ""
}
// tryDecodeWithContractABIs attempts to decode an error using MCMS and RBACTimelock ABIs.
// Returns the first successful decode, or empty string if all attempts fail.
func tryDecodeWithContractABIs(selector []byte, data []byte) string {
if len(selector) != selectorSize {
return ""
}
// Try MCMS ABI first
if mcmsABI, err := bindings.ManyChainMultiSigMetaData.GetAbi(); err == nil && mcmsABI != nil {
if decoded := decodeErrorBySelector(selector, data, mcmsABI); decoded != "" {
return decoded
}
}
// Try RBACTimelock ABI
if timelockABI, err := bindings.RBACTimelockMetaData.GetAbi(); err == nil && timelockABI != nil {
if decoded := decodeErrorBySelector(selector, data, timelockABI); decoded != "" {
return decoded
}
}
return ""
}
// decodeErrorBySelector finds an error in the ABI by matching the selector, then decodes the data part.
// The selector is used to identify the error, and only the data part (without selector) is decoded.
func decodeErrorBySelector(selector []byte, data []byte, contractABI *abi.ABI) string {
if len(selector) != 4 || contractABI == nil {
return ""
}
// Find the error in the ABI by matching the selector
for name, errDef := range contractABI.Errors {
if len(errDef.ID) < selectorSize {
continue
}
errSelector := errDef.ID[:selectorSize]
// Match the selector
if string(errSelector) != string(selector) {
continue
}
// Decode only the data part (without selector) using the error definition
// The error definition expects the full data (selector + data), so we need to reconstruct it
fullData := append(selector, data...)
decoded, err := errDef.Unpack(fullData)
if err != nil {
if name == "CallReverted" && strings.Contains(err.Error(), "length insufficient") {
return "CallReverted(truncated)"
}
continue
}
// Format the decoded error nicely
var values []any
if decodedSlice, ok := decoded.([]any); ok {
values = decodedSlice
} else {
// If it's a single value or tuple, wrap it
values = []any{decoded}
}
// Best-effort: if formatting fails, return empty string
if formatted := formatDecodedError(name, values); formatted != "" {
return formatted
}
}
return ""
}
// formatDecodedError formats a decoded error into a readable string.
// Format as "ErrorName(arg1, arg2, ...)"
func formatDecodedError(errorName string, decodedValues []any) string {
if errorName == "" {
return ""
}
if len(decodedValues) == 0 {
return errorName
}
var parts []string
for _, val := range decodedValues {
if val != nil {
parts = append(parts, fmt.Sprintf("%v", val))
}
}
if len(parts) > 0 {
return fmt.Sprintf("%s(%s)", errorName, strings.Join(parts, ", "))
}
return errorName
}
// getUnderlyingRevertReason extracts both the raw and decoded underlying revert reasons when MCMS calls
// RBACTimelock's bypasserExecuteBatch or executeBatch and one of the underlying transactions reverts.
// This function:
// 1. Extracts the underlying call from the timelock execute calls array
// 2. Simulates the underlying call using the timelock address as From (since that's msg.sender)
// 3. Returns the raw revert data/message and the decoded revert reason (if decoding succeeds)
func getUnderlyingRevertReason(
ctx context.Context,
timelockAddr common.Address,
timelockCallData []byte,
opts *bind.TransactOpts,
client ContractDeployBackend,
) (string, string) {
if timelockAddr == (common.Address{}) || len(timelockCallData) == 0 || client == nil || opts == nil {
return "", ""
}
// Extract the underlying call from timelock execute calls array
underlyingCall := extractUnderlyingCall(timelockCallData)
if underlyingCall == nil {
return "", ""
}
// Simulate the underlying transaction using CallContract (best-effort).
// timelockAddr is the underlying-call sender (RBACTimelock), including after CallProxy resolution.
_, err := client.CallContract(ctx, ethereum.CallMsg{
From: timelockAddr,
To: &underlyingCall.Target,
Value: underlyingCall.Value,
Data: underlyingCall.Data,
Gas: opts.GasLimit,
}, nil)
if err == nil {
return "", ""
}
errStr := err.Error()
// Check if error contains revert data
if !strings.Contains(errStr, "execution reverted") && !strings.Contains(errStr, "revert") {
return "", ""
}
var rawReason string
var decodedReason string
// Try to extract revert data from error string (best-effort)
// First try direct extraction, then fall back to full error parsing
if revertDataBytes := extractRevertDataFromCallError(err); len(revertDataBytes) > 0 {
rawReason = "0x" + common.Bytes2Hex(revertDataBytes)
if reason := decodeRevertReason(revertDataBytes); reason != "" {
decodedReason = reason
}
}
// Fall back to full error parsing
revertData := extractRevertReasonFromError(err)
if len(revertData.RawData) > 0 && rawReason == "" {
rawReason = "0x" + common.Bytes2Hex(revertData.RawData)
}
if decodedReason == "" && revertData.Decoded != "" {
decodedReason = revertData.Decoded
}
if rawReason == "" && revertData.Decoded != "" {
rawReason = revertData.Decoded
}
if rawReason == "" {
if idx := strings.Index(errStr, revertPrefix); idx != -1 {
rawReason = strings.TrimSpace(errStr[idx+len(revertPrefix):])
}
}
if rawReason == "" {
rawReason = errStr
}
return rawReason, decodedReason
}
// extractRevertDataFromCallError extracts revert data from a CallContract error.
// This is a best-effort extraction as the format may vary.
func extractRevertDataFromCallError(err error) []byte {
if err == nil {
return nil
}
errStr := err.Error()
// Try to find hex-encoded revert data (0x...)
if data := extractHexEncodedRevertData(errStr); len(data) > 0 {
return data
}
// Try to extract bytes array format
if data := extractBytesArrayRevertData(errStr); len(data) > 0 {
return data
}
return nil
}
// extractUnderlyingCall extracts the first underlying call from timelock execute calldata.
// Works with both bypasserExecuteBatch(calls RBACTimelockCall[]) and
// executeBatch(calls RBACTimelockCall[], predecessor, salt).
// This is a modular helper that can be reused for both bypass and regular timelock execute cases.
func extractUnderlyingCall(timelockCallData []byte) *bindings.RBACTimelockCall {
if len(timelockCallData) < selectorSize {
return nil
}
// Get the RBACTimelock ABI (best-effort)
timelockABI, err := bindings.RBACTimelockMetaData.GetAbi()
if err != nil || timelockABI == nil {
return nil
}
// Try bypasserExecuteBatch first
if method, exists := timelockABI.Methods["bypasserExecuteBatch"]; exists {
if len(method.ID) >= 4 && string(timelockCallData[:4]) == string(method.ID[:4]) {
if result := extractCallFromMethod(timelockCallData, &method); result != nil {
return result
}
}
}
// Try executeBatch
if method, exists := timelockABI.Methods["executeBatch"]; exists {
if len(method.ID) >= 4 && string(timelockCallData[:4]) == string(method.ID[:4]) {
return extractCallFromMethod(timelockCallData, &method)
}
}
return nil
}
// extractCallFromMethod extracts the first call from a timelock execute method's calldata.
// Both bypasserExecuteBatch and executeBatch have calls as their first parameter.
// Best-effort: returns nil if extraction fails.
func extractCallFromMethod(callData []byte, method *abi.Method) *bindings.RBACTimelockCall {
firstCall := firstTimelockCallArgument(callData, method)
if firstCall == nil {
return nil
}
if call, ok := firstCall.(bindings.RBACTimelockCall); ok {
return &call
}
return extractCallFields(firstCall)
}
// firstTimelockCallArgument returns the first element of the timelock calls slice extracted from calldata.
func firstTimelockCallArgument(callData []byte, method *abi.Method) any {
if method == nil || len(callData) < selectorSize {
return nil
}
args, err := method.Inputs.UnpackValues(callData[selectorSize:])
if err != nil || len(args) == 0 {
return nil
}
argValue := reflect.ValueOf(args[0])
if !argValue.IsValid() || argValue.Kind() != reflect.Slice || argValue.Len() == 0 {
return nil
}
return argValue.Index(0).Interface()
}
// extractCallFields extracts target, value, and data from a call structure.
// Handles the formats that go-ethereum's ABI unpacking may return:
// - Direct struct type (most common)
// - Anonymous struct with same fields (from UnpackValues)
// - Map format (from UnpackIntoMap or named tuples)
// - Tuple slice format (positional tuples with unnamed fields)
func extractCallFields(call any) *bindings.RBACTimelockCall {
if callStruct, ok := call.(bindings.RBACTimelockCall); ok {
return &callStruct
}
if callStruct := extractCallFromStruct(call); callStruct != nil {
return callStruct
}
if callStruct := extractCallFromMap(call); callStruct != nil {
return callStruct
}
return extractCallFromTuple(call)
}
func extractCallFromStruct(call any) *bindings.RBACTimelockCall {
callValue := reflect.ValueOf(call)
if !callValue.IsValid() || callValue.Kind() != reflect.Struct {
return nil
}
target := addressFromField(callValue, "Target")
if target == (common.Address{}) {
return nil
}
value := bigIntFromField(callValue, "Value")
data := bytesFromField(callValue, "Data")
return newTimelockCall(target, value, data)
}
func extractCallFromMap(call any) *bindings.RBACTimelockCall {
callMap, ok := call.(map[string]any)
if !ok {
return nil
}
target := addressFromMap(callMap, "target", "Target")
if target == (common.Address{}) {
return nil
}
value := bigIntFromMap(callMap, "value", "Value")
data := bytesFromMap(callMap, "data", "Data")
return newTimelockCall(target, value, data)
}
func extractCallFromTuple(call any) *bindings.RBACTimelockCall {
callSlice, ok := call.([]any)
if !ok || len(callSlice) < 3 {
return nil
}
target, _ := callSlice[0].(common.Address)
if target == (common.Address{}) {
return nil
}
value, _ := callSlice[1].(*big.Int)
data := bytesFromValue(callSlice[2])
return newTimelockCall(target, value, data)
}
func addressFromField(value reflect.Value, fieldName string) common.Address {
field := value.FieldByName(fieldName)
if !field.IsValid() || !field.CanInterface() {
return common.Address{}
}
addr, _ := field.Interface().(common.Address)
return addr
}
func bigIntFromField(value reflect.Value, fieldName string) *big.Int {
field := value.FieldByName(fieldName)
if !field.IsValid() || !field.CanInterface() {
return nil
}
bigVal, _ := field.Interface().(*big.Int)
return bigVal
}
func bytesFromField(value reflect.Value, fieldName string) []byte {
field := value.FieldByName(fieldName)
if !field.IsValid() || !field.CanInterface() {
return nil
}
return bytesFromValue(field.Interface())
}
func addressFromMap(m map[string]any, keys ...string) common.Address {
for _, key := range keys {
if addr, ok := m[key].(common.Address); ok {
return addr
}
}
return common.Address{}
}
func bigIntFromMap(m map[string]any, keys ...string) *big.Int {
for _, key := range keys {
if val, ok := m[key].(*big.Int); ok {
return val
}
}
return nil
}
func bytesFromMap(m map[string]any, keys ...string) []byte {
for _, key := range keys {
if data := bytesFromValue(m[key]); len(data) > 0 {
return data
}
}
return nil
}
func bytesFromValue(value any) []byte {
data, ok := value.([]byte)
if !ok || len(data) == 0 {
return data
}
copied := make([]byte, len(data))
copy(copied, data)
return copied
}
func newTimelockCall(target common.Address, value *big.Int, data []byte) *bindings.RBACTimelockCall {
if target == (common.Address{}) {
return nil
}
return &bindings.RBACTimelockCall{
Target: target,
Value: value,
Data: data,
}
}