Skip to content

Commit 38850e1

Browse files
committed
add skills
1 parent b418b41 commit 38850e1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+3429
-0
lines changed

.claude/skills/index.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# xrpld Codebase Skills Index
2+
3+
## Description
4+
This is the top-level guide for all best-practices skills in this repository. Use this to understand the codebase organization and find the right skill for any task.
5+
6+
## When to Use Skills
7+
Reference a skill whenever you are:
8+
- **Writing new code** in a module - check the skill first for established patterns
9+
- **Modifying existing code** - verify your changes follow module conventions
10+
- **Adding a new transaction type** - see `libxrpl/tx/transactors.md` for the full template
11+
- **Debugging** - skills list key files and common pitfalls per module
12+
- **Reviewing code** - skills document what "correct" looks like for each module
13+
14+
## Codebase Architecture
15+
16+
The codebase is split into two main areas:
17+
18+
### `src/libxrpl/` — The Library (skills in `.claude/skills/libxrpl/`)
19+
Reusable library code: data types, serialization, cryptography, ledger state, transaction processing, and storage. This is the **protocol layer**.
20+
21+
| Module | Responsibility |
22+
|--------|---------------|
23+
| `basics` | Foundational types: Buffer, Slice, base_uint, Number, logging, error contracts |
24+
| `beast` | Support layer: Journal logging, test framework, instrumentation, IP types |
25+
| `conditions` | Crypto-conditions (RFC): fulfillment validation, DER encoding |
26+
| `core` | Job queue, load monitoring, hash-based message dedup |
27+
| `crypto` | CSPRNG, secure erasure, RFC1751 encoding |
28+
| `json` | Json::Value, parsing, serialization, StaticString optimization |
29+
| `ledger` | ReadView/ApplyView, state tables, payment sandbox, credit ops |
30+
| `net` | HTTP/HTTPS client, SSL certs, async I/O |
31+
| `nodestore` | Persistent node storage: RocksDB, NuDB, Memory backends |
32+
| `protocol` | STObject hierarchy, SField, Serializer, TER codes, Features, Keylets |
33+
| `proto` | Protocol Buffer generated headers (gRPC API definitions) |
34+
| `rdb` | SOCI database wrapper, checkpointing |
35+
| `resource` | Rate limiting, endpoint tracking, abuse prevention |
36+
| `server` | Port config, SSL/TLS, WebSocket, admin networks |
37+
| `shamap` | SHA-256 Merkle radix tree (16-way branching, COW) |
38+
| `tx` | Transaction pipeline: Transactor base, preflight/preclaim/doApply |
39+
40+
### `src/xrpld/` — The Server Application (skills in `.claude/skills/xrpld/`)
41+
The running rippled server: application lifecycle, consensus, networking, RPC, and peer management. This is the **application layer**.
42+
43+
| Module | Responsibility |
44+
|--------|---------------|
45+
| `app` | Application singleton, ledger management, consensus adapters, services |
46+
| `app/main` | Application initialization and lifecycle |
47+
| `app/ledger` | Ledger storage, retrieval, immutable state management |
48+
| `app/consensus` | RCL consensus adapters (bridges generic algorithm to rippled) |
49+
| `app/misc` | Fee voting, amendments, SHAMapStore, TxQ, validators, NetworkOPs |
50+
| `app/paths` | Payment path finding algorithm, trust line caching |
51+
| `app/rdb` | Application-level database operations |
52+
| `app/tx` | Application-level transaction handling |
53+
| `consensus` | Generic consensus algorithm (CRTP-based, app-independent) |
54+
| `core` | Configuration (Config.h), time keeping, network ID |
55+
| `overlay` | P2P networking: peer connections, protocol buffers, clustering |
56+
| `peerfinder` | Network discovery: bootcache, livecache, slot management |
57+
| `perflog` | Performance logging and instrumentation |
58+
| `rpc` | RPC handler dispatch, coroutine suspension, 40+ command handlers |
59+
| `shamap` | Application-level SHAMap operations (NodeFamily) |
60+
61+
### `include/xrpl/` — Header Files
62+
Headers live in `include/xrpl/` and mirror the `src/libxrpl/` structure. Each skill already references its corresponding headers in the "Key Files" section.
63+
64+
## Cross-Cutting Conventions
65+
66+
### Error Handling
67+
- **Transaction errors**: Return `TER` enum (tesSUCCESS, tecFROZEN, temBAD_AMOUNT, etc.)
68+
- **Logic errors**: `Throw<std::runtime_error>()`, `LogicError()`, `XRPL_ASSERT()`
69+
- **I/O errors**: `Status` enum or `boost::system::error_code`
70+
- **RPC errors**: Inject via `context.params`
71+
72+
### Assertions
73+
```cpp
74+
XRPL_ASSERT(condition, "ClassName::method : description"); // Debug only
75+
XRPL_VERIFY(condition, "ClassName::method : description"); // Always enabled
76+
```
77+
78+
### Logging
79+
```cpp
80+
JLOG(j_.warn()) << "Message"; // Always wrap in JLOG macro
81+
```
82+
83+
### Memory Management
84+
- `IntrusiveRefCounts` + `SharedIntrusive` for shared ownership in libxrpl
85+
- `std::shared_ptr` for shared ownership in xrpld
86+
- `std::unique_ptr` for exclusive ownership
87+
- `CountedObject<T>` mixin for instance tracking
88+
89+
### Feature Gating
90+
```cpp
91+
if (ctx.rules.enabled(featureMyFeature)) { /* new behavior */ }
92+
```
93+
94+
### Code Organization
95+
- Headers in `include/xrpl/`, implementations in `src/libxrpl/` or `src/xrpld/`
96+
- `#pragma once` (never `#ifndef` guards)
97+
- `namespace xrpl { }` for all code
98+
- `detail/` namespace for internal helpers
99+
- Factory functions: `make_*()` returning `unique_ptr` or `shared_ptr`

.claude/skills/libxrpl/basics.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Basics Module Best Practices
2+
3+
## Description
4+
Use when working with foundational utilities in `src/libxrpl/basics/` or `include/xrpl/basics/`. Covers data structures, memory management, logging, numeric operations, error handling, and string utilities.
5+
6+
## Responsibility
7+
Provides fundamental building blocks for the entire codebase: memory types (Buffer, Slice, Blob), intrusive smart pointers, arbitrary-precision integers (base_uint), multi-precision decimal arithmetic (Number), logging infrastructure, exception handling contracts, and platform abstractions.
8+
9+
## Key Patterns
10+
11+
### Memory Types
12+
- **Slice** - Non-owning view into contiguous bytes (like std::span)
13+
- **Buffer** - Owning byte container (like std::vector<uint8_t>)
14+
- **Blob** - Alias for std::vector<unsigned char>
15+
- Prefer `Slice` for read-only parameters, `Buffer` for owned data
16+
17+
### Intrusive Smart Pointers
18+
```cpp
19+
// Prefer intrusive pointers over std::shared_ptr for minimal overhead
20+
// Object embeds its own reference count
21+
class MyObject : public IntrusiveRefCounts {
22+
// ...
23+
};
24+
// Use SharedIntrusive<MyObject> when weak pointers needed
25+
```
26+
27+
### Error Handling
28+
```cpp
29+
// Contract-based exceptions (logs call stack then throws)
30+
Throw<std::runtime_error>("Invalid source file");
31+
LogThrow("exception message");
32+
33+
// Logic errors for invariant violations
34+
LogicError("This should never happen");
35+
36+
// Assertions (fuzzing-aware)
37+
XRPL_ASSERT(condition, "function_name : description");
38+
XRPL_VERIFY(condition, "message"); // Always enabled, not just debug
39+
```
40+
41+
### Logging
42+
```cpp
43+
// Use JLOG macro - short-circuits if level disabled
44+
JLOG(debugLog().warn()) << "Message: " << value;
45+
JLOG(j_.trace()) << "Verbose detail";
46+
// Never: debugLog().warn() << "msg"; // Missing JLOG wastes formatting
47+
```
48+
49+
### Number Precision
50+
```cpp
51+
// Use Number for multi-precision decimal arithmetic
52+
// Always use RAII guard for rounding mode
53+
NumberRoundModeGuard mg(Number::towards_zero);
54+
auto result = amount * rate;
55+
// Guard restores previous mode on scope exit
56+
```
57+
58+
### base_uint Template
59+
```cpp
60+
// Typed big-endian integers: base_uint<Bits, Tag>
61+
// Tag prevents mixing unrelated types of same width
62+
using uint256 = base_uint<256, void>;
63+
using AccountID = base_uint<160, detail::AccountIDTag>;
64+
```
65+
66+
## Common Pitfalls
67+
- Never use `std::shared_ptr` when IntrusiveRefCounts is available - it adds a separate allocation
68+
- Never format log messages without JLOG wrapper - wastes CPU when level is disabled
69+
- Never use raw `assert()` - use `XRPL_ASSERT` for fuzzing instrumentation support
70+
- Never mix `Buffer` and `Slice` ownership semantics - Slice does NOT own its data
71+
72+
## Key Files
73+
- `include/xrpl/basics/IntrusiveRefCounts.h` - Atomic reference counting
74+
- `include/xrpl/basics/IntrusivePointer.h` - Smart pointer using intrusive refs
75+
- `include/xrpl/basics/base_uint.h` - Arbitrary-length big-endian integers
76+
- `include/xrpl/basics/Buffer.h`, `Blob.h`, `Slice.h` - Memory types
77+
- `include/xrpl/basics/Number.h` - Multi-precision decimal arithmetic
78+
- `include/xrpl/basics/Log.h` - Logging infrastructure
79+
- `include/xrpl/basics/contract.h` - Throw, LogicError, XRPL_ASSERT

.claude/skills/libxrpl/beast.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Beast Module Best Practices
2+
3+
## Description
4+
Use when working with the Beast support layer in `src/libxrpl/beast/` or `include/xrpl/beast/`. Covers networking types, unit testing, Journal logging, and instrumentation.
5+
6+
## Responsibility
7+
Library support layer providing network types (IP addresses, endpoints), unit testing framework, Journal/logging abstractions, instrumentation/assertion macros, and type introspection utilities.
8+
9+
## Key Patterns
10+
11+
### Journal Logging
12+
```cpp
13+
// Journal wraps a Sink pointer - copy by value is cheap
14+
beast::Journal const j_;
15+
16+
// Severity levels: kTrace, kDebug, kInfo, kWarning, kError, kFatal
17+
JLOG(j_.warn()) << "User-facing issue";
18+
JLOG(j_.debug()) << "Implementation detail";
19+
JLOG(j_.trace()) << "Very verbose diagnostic";
20+
JLOG(j_.error()) << "Unexpected failure";
21+
```
22+
23+
### Unit Testing
24+
```cpp
25+
class MyTest : public beast::unit_test::suite {
26+
void run() override {
27+
testcase("feature description");
28+
BEAST_EXPECT(value == expected); // Non-fatal assertion
29+
BEAST_REQUIRE(value == expected); // Fatal - aborts test on failure
30+
}
31+
};
32+
BEAST_DEFINE_TESTSUITE(MyTest, module, ripple);
33+
```
34+
35+
### Instrumentation
36+
```cpp
37+
// Use XRPL_ASSERT for debug-only assertions (fuzzing-aware)
38+
XRPL_ASSERT(ptr != nullptr, "MyClass::method : null pointer");
39+
40+
// Format: "ClassName::methodName : description"
41+
// The string format is important for fuzzing instrumentation
42+
```
43+
44+
### IP Endpoint Types
45+
```cpp
46+
// Use beast::IP::Endpoint for network addresses
47+
beast::IP::Endpoint endpoint;
48+
// Supports both IPv4 and IPv6
49+
// Includes port information
50+
```
51+
52+
## Common Pitfalls
53+
- Never use `BEAST_EXPECT` when the test cannot continue on failure - use `BEAST_REQUIRE` instead
54+
- Never create a Journal with a dangling Sink pointer - ensure Sink outlives Journal
55+
- Always include "ClassName::methodName" prefix in XRPL_ASSERT messages for traceability
56+
57+
## Key Files
58+
- `include/xrpl/beast/utility/Journal.h` - Log sink abstraction
59+
- `include/xrpl/beast/unit_test/suite.h` - Test framework
60+
- `include/xrpl/beast/utility/instrumentation.h` - XRPL_ASSERT macros
61+
- `include/xrpl/beast/net/IPEndpoint.h` - Network endpoint types
62+
- `include/xrpl/beast/core/SemanticVersion.h` - Version parsing
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Beast Clock Module Best Practices
2+
3+
## Description
4+
Use when working with clock abstractions in `src/libxrpl/beast/clock/` or `include/xrpl/beast/clock/`. Covers abstract clock interfaces and manual clock implementations for testing.
5+
6+
## Responsibility
7+
Provides clock abstractions that decouple code from system time, enabling deterministic testing with manual clocks and supporting different time granularities.
8+
9+
## Key Patterns
10+
11+
### Abstract Clock Interface
12+
```cpp
13+
// Use abstract_clock<T> for testable time-dependent code
14+
// Production: uses system clock
15+
// Testing: uses manual_clock for deterministic control
16+
```
17+
18+
### Manual Clock for Testing
19+
```cpp
20+
// Advance time manually in tests
21+
manual_clock<std::chrono::steady_clock> clock;
22+
clock.advance(std::chrono::seconds(30));
23+
// Deterministic - no flaky tests from timing issues
24+
```
25+
26+
## Common Pitfalls
27+
- Never use `std::chrono::system_clock::now()` directly - inject clock dependency
28+
- Always use manual_clock in unit tests for deterministic behavior
29+
- Be aware of Ripple Epoch vs Unix Epoch when working with ledger timestamps
30+
31+
## Key Files
32+
- `include/xrpl/beast/clock/abstract_clock.h` - Abstract clock interface
33+
- `include/xrpl/beast/clock/manual_clock.h` - Testable clock implementation
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Beast Core Module Best Practices
2+
3+
## Description
4+
Use when working with core beast utilities in `src/libxrpl/beast/core/` or `include/xrpl/beast/core/`. Covers semantic versioning and core type utilities.
5+
6+
## Responsibility
7+
Provides core utilities including semantic version parsing/comparison, system abstractions, and foundational type support.
8+
9+
## Key Patterns
10+
11+
### Semantic Version Parsing
12+
```cpp
13+
// Parse version strings following semver spec
14+
SemanticVersion version;
15+
if (version.parse("1.2.3-beta")) {
16+
auto major = version.majorVersion;
17+
auto minor = version.minorVersion;
18+
auto patch = version.patchVersion;
19+
}
20+
// Supports comparison operators for version ordering
21+
```
22+
23+
## Common Pitfalls
24+
- Always validate version strings before using - parse() returns false on invalid input
25+
- Remember that pre-release versions have lower precedence than release versions
26+
27+
## Key Files
28+
- `include/xrpl/beast/core/SemanticVersion.h` - Version parsing and comparison
29+
- `src/libxrpl/beast/core/SemanticVersion.cpp` - Implementation
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Beast Insight Module Best Practices
2+
3+
## Description
4+
Use when working with metrics and monitoring in `src/libxrpl/beast/insight/` or `include/xrpl/beast/insight/`. Covers counters, gauges, and metrics collection.
5+
6+
## Responsibility
7+
Provides metrics collection infrastructure for monitoring application performance: counters for cumulative values, gauges for point-in-time measurements, and hooks for metrics export.
8+
9+
## Key Patterns
10+
11+
### Metrics Types
12+
```cpp
13+
// Counter: Monotonically increasing value (e.g., total requests)
14+
insight::Counter requests;
15+
++requests;
16+
17+
// Gauge: Point-in-time value (e.g., current connections)
18+
insight::Gauge connections;
19+
connections = currentCount;
20+
21+
// Event: Timed operation tracking
22+
insight::Event latency;
23+
```
24+
25+
### Null Metrics
26+
```cpp
27+
// Use NullCollector when metrics disabled
28+
// All operations become no-ops with zero overhead
29+
auto collector = insight::NullCollector::New();
30+
```
31+
32+
## Common Pitfalls
33+
- Use Counter for cumulative values, Gauge for current state - don't mix them
34+
- Always use NullCollector for tests - avoids metrics overhead
35+
- Never store raw metric values in long-lived objects - use the insight types
36+
37+
## Key Files
38+
- `include/xrpl/beast/insight/Counter.h` - Cumulative counter
39+
- `include/xrpl/beast/insight/Gauge.h` - Point-in-time gauge
40+
- `include/xrpl/beast/insight/Collector.h` - Metrics collector interface
41+
- `include/xrpl/beast/insight/NullCollector.h` - No-op collector
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Beast Net Module Best Practices
2+
3+
## Description
4+
Use when working with network types in `src/libxrpl/beast/net/` or `include/xrpl/beast/net/`. Covers IP address and endpoint representations.
5+
6+
## Responsibility
7+
Provides network type abstractions for IP addresses (v4 and v6) and endpoints (address + port), with parsing utilities and comparison operators.
8+
9+
## Key Patterns
10+
11+
### IP Endpoint Usage
12+
```cpp
13+
// beast::IP::Endpoint combines address + port
14+
beast::IP::Endpoint endpoint(
15+
beast::IP::Address::from_string("127.0.0.1"), 51235);
16+
17+
// Supports both IPv4 and IPv6
18+
beast::IP::Endpoint v6endpoint(
19+
beast::IP::Address::from_string("::1"), 51235);
20+
```
21+
22+
### Address Parsing
23+
```cpp
24+
// Parse from string with error handling
25+
boost::system::error_code ec;
26+
auto address = beast::IP::Address::from_string(input, ec);
27+
if (ec) { /* handle invalid address */ }
28+
```
29+
30+
### Comparison and Ordering
31+
```cpp
32+
// Endpoints support full comparison for use in containers
33+
std::set<beast::IP::Endpoint> endpoints;
34+
std::map<beast::IP::Endpoint, Consumer> consumers;
35+
```
36+
37+
## Common Pitfalls
38+
- Always handle both IPv4 and IPv6 when working with endpoints
39+
- Use error_code overload of from_string to handle invalid input gracefully
40+
- Never assume address format - always parse and validate
41+
42+
## Key Files
43+
- `include/xrpl/beast/net/IPEndpoint.h` - Combined address + port
44+
- `include/xrpl/beast/net/IPAddress.h` - IP address types
45+
- `include/xrpl/beast/net/IPAddressV4.h` - IPv4 specific
46+
- `include/xrpl/beast/net/IPAddressV6.h` - IPv6 specific

0 commit comments

Comments
 (0)