Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add Reason() method to VmError interface #8

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions x/evm/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ type VmError interface {
Error() string
VmError() string
Ret() []byte
Reason() string
}

type vmErrorWithRet struct {
Expand Down
51 changes: 51 additions & 0 deletions x/evm/types/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,54 @@ func TestNewExecErrorWithReason(t *testing.T) {
require.Equal(t, 3, errWithReason.ErrorCode())
}
}

func TestNewVmErrorWithRet(t *testing.T) {
testCases := []struct {
name string
vmErr string
reason string
ret []byte
hash string
}{
{
"Empty reason",
"execution reverted",
"",
nil,
"0x",
},
{
"With unpackable reason",
"execution reverted",
"",
[]byte("a"),
"0x61",
},
{
"With packable reason but empty reason",
"execution reverted",
"",
revertSelector,
"0x08c379a0",
},
{
"With packable reason with reason",
"execution reverted",
"COUNTER_TOO_LOW",
hexutils.HexToBytes("08C379A00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000F434F554E5445525F544F4F5F4C4F570000000000000000000000000000000000"),
"0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000f434f554e5445525f544f4f5f4c4f570000000000000000000000000000000000",
},
}

for _, tc := range testCases {
tc := tc
vmErrorWithRet := NewVmErrorWithRet(
tc.vmErr,
tc.ret,
tc.hash,
0,
)
require.Equal(t, tc.vmErr, vmErrorWithRet.VmError())
require.Equal(t, tc.reason, vmErrorWithRet.Reason())
}
}
Comment on lines +93 to +104
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance test robustness and organization.

Consider the following improvements to strengthen the test implementation:

  1. Move the gas value into the test case structure instead of hardcoding it
  2. Use t.Run for subtests to improve test organization and output
  3. Add assertions for complete error state (e.g., hash value)
 	testCases := []struct {
 		name   string
 		vmErr  string
 		reason string
 		ret    []byte
 		hash   string
+		gas    uint64
 	}{
 		{
 			"Empty reason",
 			"execution reverted",
 			"",
 			nil,
 			"0x",
+			0,
 		},
 		// ... other test cases
 	}

 	for _, tc := range testCases {
-		tc := tc
-		vmErrorWithRet := NewVmErrorWithRet(
-			tc.vmErr,
-			tc.ret,
-			tc.hash,
-			0,
-		)
-		require.Equal(t, tc.vmErr, vmErrorWithRet.VmError())
-		require.Equal(t, tc.reason, vmErrorWithRet.Reason())
+		tc := tc // capture range variable
+		t.Run(tc.name, func(t *testing.T) {
+			vmErrorWithRet := NewVmErrorWithRet(
+				tc.vmErr,
+				tc.ret,
+				tc.hash,
+				tc.gas,
+			)
+			require.Equal(t, tc.vmErr, vmErrorWithRet.VmError())
+			require.Equal(t, tc.reason, vmErrorWithRet.Reason())
+			require.Equal(t, tc.hash, vmErrorWithRet.ErrorData())
+		})
 	}

Committable suggestion was skipped due to low confidence.

Loading