|
5 | 5 | "context" |
6 | 6 | "encoding/hex" |
7 | 7 | "encoding/json" |
| 8 | + "errors" |
8 | 9 | "fmt" |
9 | 10 | "math/big" |
10 | 11 | "reflect" |
@@ -1401,3 +1402,54 @@ func TestCheckNonceInRange(t *testing.T) { |
1401 | 1402 | require.NoError(t, checkNonceInRange(0, 0, 0, "solana:devnet")) |
1402 | 1403 | }) |
1403 | 1404 | } |
| 1405 | + |
| 1406 | +// nonceBuilder is a partial TxBuilder: only GetNextNonce is implemented, so any |
| 1407 | +// other call panics loudly rather than silently returning a zero value. |
| 1408 | +type nonceBuilder struct { |
| 1409 | + common.TxBuilder |
| 1410 | + finalized, pending uint64 |
| 1411 | + finalizedErr, pendingErr error |
| 1412 | +} |
| 1413 | + |
| 1414 | +func (b *nonceBuilder) GetNextNonce(_ context.Context, _ string, useFinalized bool) (uint64, error) { |
| 1415 | + if useFinalized { |
| 1416 | + return b.finalized, b.finalizedErr |
| 1417 | + } |
| 1418 | + return b.pending, b.pendingErr |
| 1419 | +} |
| 1420 | + |
| 1421 | +func TestNonceBounds(t *testing.T) { |
| 1422 | + ctx := context.Background() |
| 1423 | + |
| 1424 | + t.Run("pending above finalized becomes the ceiling base", func(t *testing.T) { |
| 1425 | + fin, base, err := nonceBounds(ctx, &nonceBuilder{finalized: 100, pending: 140}, "0xtss") |
| 1426 | + require.NoError(t, err) |
| 1427 | + assert.Equal(t, uint64(100), fin) |
| 1428 | + assert.Equal(t, uint64(140), base) |
| 1429 | + }) |
| 1430 | + |
| 1431 | + // Falling back to finalized is stricter, never wrong. |
| 1432 | + t.Run("pending lookup failure falls back to finalized", func(t *testing.T) { |
| 1433 | + fin, base, err := nonceBounds(ctx, &nonceBuilder{ |
| 1434 | + finalized: 100, |
| 1435 | + pendingErr: errors.New("rpc down"), |
| 1436 | + }, "0xtss") |
| 1437 | + require.NoError(t, err) |
| 1438 | + assert.Equal(t, uint64(100), fin) |
| 1439 | + assert.Equal(t, uint64(100), base) |
| 1440 | + }) |
| 1441 | + |
| 1442 | + // A stale pending read must never lower the ceiling below finalized. |
| 1443 | + t.Run("pending below finalized falls back to finalized", func(t *testing.T) { |
| 1444 | + fin, base, err := nonceBounds(ctx, &nonceBuilder{finalized: 100, pending: 60}, "0xtss") |
| 1445 | + require.NoError(t, err) |
| 1446 | + assert.Equal(t, uint64(100), fin) |
| 1447 | + assert.Equal(t, uint64(100), base) |
| 1448 | + }) |
| 1449 | + |
| 1450 | + // Callers skip the nonce check entirely when finalized is unavailable. |
| 1451 | + t.Run("finalized lookup failure errors", func(t *testing.T) { |
| 1452 | + _, _, err := nonceBounds(ctx, &nonceBuilder{finalizedErr: errors.New("rpc down")}, "0xtss") |
| 1453 | + require.Error(t, err) |
| 1454 | + }) |
| 1455 | +} |
0 commit comments