|
3 | 3 | package eth |
4 | 4 |
|
5 | 5 | import ( |
| 6 | + "math" |
6 | 7 | "testing" |
7 | 8 | "time" |
8 | 9 |
|
@@ -70,6 +71,54 @@ func TestEthereumTypeGetNonces_PrivatePendingHint_RaisesPrimaryFallback(t *testi |
70 | 71 | } |
71 | 72 | } |
72 | 73 |
|
| 74 | +// TestEthereumTypeGetNonces_PrivatePendingHint_WithConfirmedNonce exercises the exact production |
| 75 | +// combination (api/worker.go passes WithConfirmedNonce together with PrivatePendingNonces...): the |
| 76 | +// declared floor must raise only the PENDING nonce and leave the confirmed (latest) nonce untouched. |
| 77 | +func TestEthereumTypeGetNonces_PrivatePendingHint_WithConfirmedNonce(t *testing.T) { |
| 78 | + server := newNonceRPCServer(t, map[string]string{"pending": "0x9", "latest": "0x5"}, nil) |
| 79 | + stub := &nonceBatchStub{results: map[string]string{"pending": "0x4", "latest": "0x2"}} |
| 80 | + // no recent senders → routed purely by the declared floor |
| 81 | + b := &EthereumRPC{RPC: stub, Timeout: time.Second, alternativeSendTxProvider: newRecentSenderProvider(server)} |
| 82 | + |
| 83 | + // declared nonce 42 → pending floor 43, above the provider's pending answer of 9 |
| 84 | + pending, confirmed, confirmedOK, err := b.EthereumTypeGetNonces(nonceTestAddr, true, 42) |
| 85 | + if err != nil { |
| 86 | + t.Fatalf("unexpected error: %v", err) |
| 87 | + } |
| 88 | + if pending != 43 { |
| 89 | + t.Errorf("pending = %d, want 43 (declared floor over the provider answer)", pending) |
| 90 | + } |
| 91 | + if confirmed != 5 || !confirmedOK { |
| 92 | + t.Errorf("confirmed = (%d, ok=%v), want (5, true) — the floor must not touch the confirmed nonce", confirmed, confirmedOK) |
| 93 | + } |
| 94 | + if len(stub.queried) != 0 { |
| 95 | + t.Errorf("primary RPC queried tags %v, want none once routed to the provider", stub.queried) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// TestEthereumTypeGetNonces_PrivatePendingHint_RoutesOnDeclaredZero confirms a declared nonce of 0 |
| 100 | +// (a wallet's very first tx) still trips the routing guard (declaredFloor 1 > 0) and raises the |
| 101 | +// pending nonce to 1 — the boundary the routing tests above (nonce 42) do not exercise. |
| 102 | +func TestEthereumTypeGetNonces_PrivatePendingHint_RoutesOnDeclaredZero(t *testing.T) { |
| 103 | + server := newNonceRPCServer(t, map[string]string{"pending": "0x0"}, nil) |
| 104 | + stub := &nonceBatchStub{results: map[string]string{"pending": "0x0"}} |
| 105 | + b := &EthereumRPC{RPC: stub, Timeout: time.Second, alternativeSendTxProvider: newRecentSenderProvider(server)} |
| 106 | + |
| 107 | + pending, _, _, err := b.EthereumTypeGetNonces(nonceTestAddr, false, 0) |
| 108 | + if err != nil { |
| 109 | + t.Fatalf("unexpected error: %v", err) |
| 110 | + } |
| 111 | + if pending != 1 { |
| 112 | + t.Errorf("pending = %d, want 1 (declared nonce 0 → floor 1)", pending) |
| 113 | + } |
| 114 | + if got := server.callCount("pending"); got != 1 { |
| 115 | + t.Errorf("alternative provider queried %d times, want 1 (declared 0 must still route)", got) |
| 116 | + } |
| 117 | + if len(stub.queried) != 0 { |
| 118 | + t.Errorf("primary RPC queried tags %v, want none once routed to the provider", stub.queried) |
| 119 | + } |
| 120 | +} |
| 121 | + |
73 | 122 | // TestEthereumTypeGetNonces_PrivatePendingHint_IgnoredWithoutProvider confirms the hint is a |
74 | 123 | // relay-deployment feature: with no alternative provider configured it is ignored and the primary |
75 | 124 | // answer stands unchanged. |
@@ -124,6 +173,11 @@ func TestDeclaredPendingFloor(t *testing.T) { |
124 | 173 | {[]uint64{0}, 1}, |
125 | 174 | {[]uint64{5}, 6}, |
126 | 175 | {[]uint64{5, 42, 7}, 43}, |
| 176 | + // n+1 wraps to 0 at MaxUint64; the entry is silently ignored (benign: the floor is only |
| 177 | + // ever a max() operand, so a spurious 0 can never lower the reported nonce). |
| 178 | + {[]uint64{math.MaxUint64}, 0}, |
| 179 | + // a physically-unreachable max value co-declared with a real nonce must not corrupt it. |
| 180 | + {[]uint64{math.MaxUint64, 43}, 44}, |
127 | 181 | } |
128 | 182 | for _, c := range cases { |
129 | 183 | if got := declaredPendingFloor(c.in); got != c.want { |
|
0 commit comments