|
| 1 | +package checks |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/big" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/ethereum/go-ethereum/common" |
| 8 | + "github.com/stackup-wallet/stackup-bundler/internal/testutils" |
| 9 | + "github.com/stackup-wallet/stackup-bundler/pkg/userop" |
| 10 | +) |
| 11 | + |
| 12 | +// TestNoPendingOps calls checks.ValidatePendingOps with no pending UserOperations. Expect nil. |
| 13 | +func TestNoPendingOps(t *testing.T) { |
| 14 | + penOps := []*userop.UserOperation{} |
| 15 | + op := testutils.MockValidInitUserOp() |
| 16 | + err := ValidatePendingOps(op, penOps, testutils.MockGetNotStakeZeroDeposit) |
| 17 | + |
| 18 | + if err != nil { |
| 19 | + t.Fatalf("got err %v, want nil", err) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +// TestPendingOpsNotStaked calls checks.ValidatePendingOps with pending UserOperations but sender is not |
| 24 | +// staked. Expect error. |
| 25 | +func TestPendingOpsNotStaked(t *testing.T) { |
| 26 | + penOp := testutils.MockValidInitUserOp() |
| 27 | + penOps := []*userop.UserOperation{penOp} |
| 28 | + op := testutils.MockValidInitUserOp() |
| 29 | + op.Nonce = big.NewInt(0).Add(penOp.Nonce, common.Big1) |
| 30 | + err := ValidatePendingOps(op, penOps, testutils.MockGetNotStakeZeroDeposit) |
| 31 | + |
| 32 | + if err == nil { |
| 33 | + t.Fatal("got nil, want err") |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// TestPendingOpsStaked calls checks.ValidatePendingOps with pending UserOperations but sender is staked. |
| 38 | +// Expect nil. |
| 39 | +func TestPendingOpsStaked(t *testing.T) { |
| 40 | + penOp := testutils.MockValidInitUserOp() |
| 41 | + penOps := []*userop.UserOperation{penOp} |
| 42 | + op := testutils.MockValidInitUserOp() |
| 43 | + op.Nonce = big.NewInt(0).Add(penOp.Nonce, common.Big1) |
| 44 | + err := ValidatePendingOps(op, penOps, testutils.MockGetStakeZeroDeposit) |
| 45 | + |
| 46 | + if err != nil { |
| 47 | + t.Fatalf("got err %v, want nil", err) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// TestReplaceOp calls checks.ValidatePendingOps with a valid UserOperation that replaces a pending |
| 52 | +// UserOperation. Expect nil. |
| 53 | +func TestReplaceOp(t *testing.T) { |
| 54 | + penOp := testutils.MockValidInitUserOp() |
| 55 | + penOps := []*userop.UserOperation{penOp} |
| 56 | + op := testutils.MockValidInitUserOp() |
| 57 | + op.MaxPriorityFeePerGas = big.NewInt(0).Add(penOp.MaxPriorityFeePerGas, common.Big1) |
| 58 | + op.MaxFeePerGas = big.NewInt(0).Add(penOp.MaxFeePerGas, common.Big1) |
| 59 | + err := ValidatePendingOps(op, penOps, testutils.MockGetNotStakeZeroDeposit) |
| 60 | + |
| 61 | + if err != nil { |
| 62 | + t.Fatalf("got err %v, want nil", err) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// TestReplaceOpLowerMPF calls checks.ValidatePendingOps with a UserOperation that replaces a pending |
| 67 | +// UserOperation but has a lower MaxPriorityFeePerGas. Expect error. |
| 68 | +func TestReplaceOpLowerMPF(t *testing.T) { |
| 69 | + penOp := testutils.MockValidInitUserOp() |
| 70 | + penOps := []*userop.UserOperation{penOp} |
| 71 | + op := testutils.MockValidInitUserOp() |
| 72 | + op.MaxPriorityFeePerGas = big.NewInt(0).Sub(penOp.MaxPriorityFeePerGas, common.Big1) |
| 73 | + err := ValidatePendingOps(op, penOps, testutils.MockGetNotStakeZeroDeposit) |
| 74 | + |
| 75 | + if err == nil { |
| 76 | + t.Fatal("got nil, want err") |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// TestReplaceOpEqualMPF calls checks.ValidatePendingOps with a UserOperation that replaces a pending |
| 81 | +// UserOperation but has an equal MaxPriorityFeePerGas. Expect error. |
| 82 | +func TestReplaceOpEqualMPF(t *testing.T) { |
| 83 | + penOp := testutils.MockValidInitUserOp() |
| 84 | + penOps := []*userop.UserOperation{penOp} |
| 85 | + op := testutils.MockValidInitUserOp() |
| 86 | + op.MaxPriorityFeePerGas = big.NewInt(0).Add(penOp.MaxPriorityFeePerGas, common.Big0) |
| 87 | + err := ValidatePendingOps(op, penOps, testutils.MockGetNotStakeZeroDeposit) |
| 88 | + |
| 89 | + if err == nil { |
| 90 | + t.Fatal("got nil, want err") |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// TestReplaceOpNotEqualIncMF calls checks.ValidatePendingOps with a UserOperation that replaces a pending |
| 95 | +// UserOperation but does not have an equally increasing MaxFeePerGas. Expect error. |
| 96 | +func TestReplaceOpNotEqualIncMF(t *testing.T) { |
| 97 | + penOp := testutils.MockValidInitUserOp() |
| 98 | + penOps := []*userop.UserOperation{penOp} |
| 99 | + op := testutils.MockValidInitUserOp() |
| 100 | + op.MaxPriorityFeePerGas = big.NewInt(0).Add(penOp.MaxPriorityFeePerGas, common.Big2) |
| 101 | + op.MaxFeePerGas = big.NewInt(0).Add(penOp.MaxFeePerGas, common.Big1) |
| 102 | + err := ValidatePendingOps(op, penOps, testutils.MockGetNotStakeZeroDeposit) |
| 103 | + |
| 104 | + if err == nil { |
| 105 | + t.Fatal("got nil, want err") |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +// TestReplaceOpSameMF calls checks.ValidatePendingOps with a UserOperation that replaces a pending |
| 110 | +// UserOperation but does not increase MaxFeePerGas. Expect error. |
| 111 | +func TestReplaceOpSameMF(t *testing.T) { |
| 112 | + penOp := testutils.MockValidInitUserOp() |
| 113 | + penOps := []*userop.UserOperation{penOp} |
| 114 | + op := testutils.MockValidInitUserOp() |
| 115 | + op.MaxPriorityFeePerGas = big.NewInt(0).Add(penOp.MaxPriorityFeePerGas, common.Big1) |
| 116 | + op.MaxFeePerGas = big.NewInt(0).Add(penOp.MaxFeePerGas, common.Big0) |
| 117 | + err := ValidatePendingOps(op, penOps, testutils.MockGetNotStakeZeroDeposit) |
| 118 | + |
| 119 | + if err == nil { |
| 120 | + t.Fatal("got nil, want err") |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// TestReplaceOpDecMF calls checks.ValidatePendingOps with a UserOperation that replaces a pending |
| 125 | +// UserOperation but has a decreasing MaxFeePerGas. Expect error. |
| 126 | +func TestReplaceOpDecMF(t *testing.T) { |
| 127 | + penOp := testutils.MockValidInitUserOp() |
| 128 | + penOps := []*userop.UserOperation{penOp} |
| 129 | + op := testutils.MockValidInitUserOp() |
| 130 | + op.MaxPriorityFeePerGas = big.NewInt(0).Add(penOp.MaxPriorityFeePerGas, common.Big1) |
| 131 | + op.MaxFeePerGas = big.NewInt(0).Sub(penOp.MaxFeePerGas, common.Big1) |
| 132 | + err := ValidatePendingOps(op, penOps, testutils.MockGetNotStakeZeroDeposit) |
| 133 | + |
| 134 | + if err == nil { |
| 135 | + t.Fatal("got nil, want err") |
| 136 | + } |
| 137 | +} |
0 commit comments