|
| 1 | +package evm |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/hex" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/ethereum/go-ethereum/common" |
| 9 | + "github.com/ethereum/go-ethereum/core/types" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | + |
| 13 | + coreexecution "github.com/evstack/ev-node/core/execution" |
| 14 | +) |
| 15 | + |
| 16 | +// TestExecuteTxs_ForceIncludedMask verifies that force-included transactions |
| 17 | +// are validated while mempool transactions skip validation (already validated on submission) |
| 18 | +func TestExecuteTxs_ForceIncludedMask(t *testing.T) { |
| 19 | + t.Parallel() |
| 20 | + |
| 21 | + // Create a valid Ethereum transaction for testing |
| 22 | + validTx := types.NewTransaction( |
| 23 | + 0, // nonce |
| 24 | + common.HexToAddress("0x1234567890123456789012345678901234567890"), // to |
| 25 | + common.Big0, // value |
| 26 | + 21000, // gas |
| 27 | + common.Big1, // gasPrice |
| 28 | + nil, // data |
| 29 | + ) |
| 30 | + validTxBytes, err := validTx.MarshalBinary() |
| 31 | + require.NoError(t, err) |
| 32 | + |
| 33 | + // Create invalid transaction bytes (gibberish) |
| 34 | + invalidTxBytes := []byte("this is not a valid transaction") |
| 35 | + |
| 36 | + tests := []struct { |
| 37 | + name string |
| 38 | + txs [][]byte |
| 39 | + mask []bool |
| 40 | + expectedValidTxs int |
| 41 | + expectedFilterMsg string |
| 42 | + }{ |
| 43 | + { |
| 44 | + name: "all transactions validated when no mask", |
| 45 | + txs: [][]byte{ |
| 46 | + validTxBytes, |
| 47 | + validTxBytes, |
| 48 | + invalidTxBytes, // Should be filtered |
| 49 | + validTxBytes, |
| 50 | + }, |
| 51 | + mask: nil, // No mask = validate all |
| 52 | + expectedValidTxs: 3, // 3 valid, 1 invalid filtered |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "force-included invalid tx must be validated (filtered out)", |
| 56 | + txs: [][]byte{ |
| 57 | + invalidTxBytes, // Force-included, MUST be validated - will be filtered |
| 58 | + validTxBytes, // Mempool tx, skips validation |
| 59 | + }, |
| 60 | + mask: []bool{true, false}, |
| 61 | + expectedValidTxs: 1, // Only mempool tx passes (force-included filtered) |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "mixed force-included and mempool transactions", |
| 65 | + txs: [][]byte{ |
| 66 | + validTxBytes, // Force-included, validated (valid) |
| 67 | + validTxBytes, // Force-included, validated (valid) |
| 68 | + invalidTxBytes, // Mempool tx, skips validation (passes through) |
| 69 | + validTxBytes, // Mempool tx, skips validation (passes through) |
| 70 | + }, |
| 71 | + mask: []bool{true, true, false, false}, |
| 72 | + expectedValidTxs: 4, // 2 valid force-included + 2 mempool txs |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "all force-included transactions must be validated", |
| 76 | + txs: [][]byte{ |
| 77 | + invalidTxBytes, // Force-included gibberish - filtered |
| 78 | + invalidTxBytes, // Force-included gibberish - filtered |
| 79 | + invalidTxBytes, // Force-included gibberish - filtered |
| 80 | + }, |
| 81 | + mask: []bool{true, true, true}, |
| 82 | + expectedValidTxs: 0, // All filtered out (invalid) |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "all mempool transactions skip validation", |
| 86 | + txs: [][]byte{ |
| 87 | + validTxBytes, |
| 88 | + validTxBytes, |
| 89 | + invalidTxBytes, // Skips validation, passes through |
| 90 | + }, |
| 91 | + mask: []bool{false, false, false}, |
| 92 | + expectedValidTxs: 3, // All pass (validation skipped) |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "empty mask same as no mask", |
| 96 | + txs: [][]byte{ |
| 97 | + validTxBytes, |
| 98 | + invalidTxBytes, // Should be filtered |
| 99 | + }, |
| 100 | + mask: []bool{}, |
| 101 | + expectedValidTxs: 1, // 1 valid, 1 filtered |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + for _, tt := range tests { |
| 106 | + t.Run(tt.name, func(t *testing.T) { |
| 107 | + t.Parallel() |
| 108 | + |
| 109 | + // Create context with or without mask |
| 110 | + ctx := context.Background() |
| 111 | + if tt.mask != nil { |
| 112 | + ctx = coreexecution.WithForceIncludedMask(ctx, tt.mask) |
| 113 | + } |
| 114 | + |
| 115 | + // Call the validation logic by inspecting how transactions are filtered |
| 116 | + // We'll extract the logic to count valid transactions |
| 117 | + forceIncludedMask := coreexecution.GetForceIncludedMask(ctx) |
| 118 | + validTxs := make([]string, 0, len(tt.txs)) |
| 119 | + skippedValidation := 0 |
| 120 | + |
| 121 | + for i, tx := range tt.txs { |
| 122 | + if len(tx) == 0 { |
| 123 | + continue |
| 124 | + } |
| 125 | + |
| 126 | + // Skip validation for mempool transactions (already validated when added to mempool) |
| 127 | + // Force-included transactions from DA MUST be validated |
| 128 | + if forceIncludedMask != nil && i < len(forceIncludedMask) && !forceIncludedMask[i] { |
| 129 | + validTxs = append(validTxs, "0x"+hex.EncodeToString(tx)) |
| 130 | + skippedValidation++ |
| 131 | + continue |
| 132 | + } |
| 133 | + |
| 134 | + // Validate force-included transactions (and all txs when no mask) |
| 135 | + var ethTx types.Transaction |
| 136 | + if err := ethTx.UnmarshalBinary(tx); err != nil { |
| 137 | + // Invalid transaction, skip it |
| 138 | + continue |
| 139 | + } |
| 140 | + |
| 141 | + validTxs = append(validTxs, "0x"+hex.EncodeToString(tx)) |
| 142 | + } |
| 143 | + |
| 144 | + // Verify expected number of valid transactions |
| 145 | + assert.Equal(t, tt.expectedValidTxs, len(validTxs), |
| 146 | + "unexpected number of valid transactions") |
| 147 | + |
| 148 | + // Verify mempool transactions were actually skipped |
| 149 | + if tt.mask != nil { |
| 150 | + expectedSkipped := 0 |
| 151 | + for i, isForceIncluded := range tt.mask { |
| 152 | + // Skip when NOT force-included (i.e., mempool tx) |
| 153 | + if !isForceIncluded && i < len(tt.txs) && len(tt.txs[i]) > 0 { |
| 154 | + expectedSkipped++ |
| 155 | + } |
| 156 | + } |
| 157 | + assert.Equal(t, expectedSkipped, skippedValidation, |
| 158 | + "unexpected number of skipped validations") |
| 159 | + } |
| 160 | + }) |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +// TestWithForceIncludedMask_ContextRoundtrip verifies that the mask |
| 165 | +// can be stored in and retrieved from context correctly |
| 166 | +func TestWithForceIncludedMask_ContextRoundtrip(t *testing.T) { |
| 167 | + t.Parallel() |
| 168 | + |
| 169 | + tests := []struct { |
| 170 | + name string |
| 171 | + mask []bool |
| 172 | + }{ |
| 173 | + { |
| 174 | + name: "nil mask", |
| 175 | + mask: nil, |
| 176 | + }, |
| 177 | + { |
| 178 | + name: "empty mask", |
| 179 | + mask: []bool{}, |
| 180 | + }, |
| 181 | + { |
| 182 | + name: "single element", |
| 183 | + mask: []bool{true}, |
| 184 | + }, |
| 185 | + { |
| 186 | + name: "multiple elements", |
| 187 | + mask: []bool{true, false, true, false, false}, |
| 188 | + }, |
| 189 | + { |
| 190 | + name: "all true", |
| 191 | + mask: []bool{true, true, true}, |
| 192 | + }, |
| 193 | + { |
| 194 | + name: "all false", |
| 195 | + mask: []bool{false, false, false}, |
| 196 | + }, |
| 197 | + } |
| 198 | + |
| 199 | + for _, tt := range tests { |
| 200 | + t.Run(tt.name, func(t *testing.T) { |
| 201 | + t.Parallel() |
| 202 | + |
| 203 | + ctx := context.Background() |
| 204 | + |
| 205 | + // Add mask to context |
| 206 | + ctxWithMask := coreexecution.WithForceIncludedMask(ctx, tt.mask) |
| 207 | + |
| 208 | + // Retrieve mask from context |
| 209 | + retrieved := coreexecution.GetForceIncludedMask(ctxWithMask) |
| 210 | + |
| 211 | + // Verify it matches |
| 212 | + assert.Equal(t, tt.mask, retrieved) |
| 213 | + }) |
| 214 | + } |
| 215 | +} |
| 216 | + |
| 217 | +// TestGetForceIncludedMask_NoMask verifies that getting a mask from |
| 218 | +// a context without one returns nil |
| 219 | +func TestGetForceIncludedMask_NoMask(t *testing.T) { |
| 220 | + t.Parallel() |
| 221 | + |
| 222 | + ctx := context.Background() |
| 223 | + mask := coreexecution.GetForceIncludedMask(ctx) |
| 224 | + |
| 225 | + assert.Nil(t, mask, "expected nil mask from context without mask") |
| 226 | +} |
| 227 | + |
| 228 | +// TestGetForceIncludedMask_WrongType verifies that wrong types in context |
| 229 | +// are handled gracefully |
| 230 | +func TestGetForceIncludedMask_WrongType(t *testing.T) { |
| 231 | + t.Parallel() |
| 232 | + |
| 233 | + // Create context with wrong type |
| 234 | + ctx := context.WithValue(context.Background(), "force_included_mask", "wrong type") |
| 235 | + |
| 236 | + mask := coreexecution.GetForceIncludedMask(ctx) |
| 237 | + |
| 238 | + assert.Nil(t, mask, "expected nil mask when context has wrong type") |
| 239 | +} |
0 commit comments