Add event-based offchain indexer with Runtime API and RPC for pallet-robonomics-cps#488
Add event-based offchain indexer with Runtime API and RPC for pallet-robonomics-cps#488Copilot wants to merge 12 commits intorelease/4.1from
Conversation
Co-authored-by: akru <786394+akru@users.noreply.github.com>
…orker feature Co-authored-by: akru <786394+akru@users.noreply.github.com>
…ance notes Co-authored-by: akru <786394+akru@users.noreply.github.com>
Co-authored-by: akru <786394+akru@users.noreply.github.com>
akru
left a comment
There was a problem hiding this comment.
Add tests to coverage functionality.
frame/cps/src/offchain/mod.rs
Outdated
| // Get current timestamp | ||
| // Note: In a real implementation, you would get the timestamp from the block | ||
| // For now, we use block number as a simple timestamp approximation | ||
| let timestamp: u64 = block_number.unique_saturated_into(); |
There was a problem hiding this comment.
Lets use real timestamp, config provides time source
There was a problem hiding this comment.
Now using sp_io::offchain::timestamp().unix_millis() to get real system time instead of block number. (ce0d328)
There was a problem hiding this comment.
It probably not related to block timestamp, can we use it?
There was a problem hiding this comment.
Currently using sp_io::offchain::timestamp().unix_millis() which provides system time. If you'd prefer to use block timestamp from pallet-timestamp instead, we could add a Time associated type to Config (like pallet-datalog has). Which approach would you prefer?
…num, add serde, use system time Co-authored-by: akru <786394+akru@users.noreply.github.com>
I'll add test coverage for the indexer functionality. Could you clarify what specific aspects should be tested? Should I add:
|
akru
left a comment
There was a problem hiding this comment.
Add tests for: storage, indexer and runtime api; rpc integration tests can be added later.
frame/cps/rpc/runtime-api/src/lib.rs
Outdated
| /// | ||
| /// # Returns | ||
| /// Vector of MetaRecord structures | ||
| fn get_meta_records(from: u64, to: u64, node_id: Option<NodeId>) -> Vec<(u64, NodeId, Vec<u8>)>; |
There was a problem hiding this comment.
Lets return proper structure instead of Vec.
There was a problem hiding this comment.
Changed to return Vec<Vec<u8>> (encoded structures). RPC layer decodes them back to typed structures for JSON serialization. (7a0f707)
…, return structures, add tests Co-authored-by: akru <786394+akru@users.noreply.github.com>
Added 7 comprehensive tests covering:
All tests passing (44 total: 37 existing + 7 new). (7a0f707) |
…ber as timestamp Co-authored-by: akru <786394+akru@users.noreply.github.com>
akru
left a comment
There was a problem hiding this comment.
Use https://docs.rs/sp-io/latest/sp_io/offchain_index/index.html to fulfill data from pallet to offchain storage. Research better organization for dtorage to query all data for node ID or filter data for nodeid in range of timestamps
…ficient queries Co-authored-by: akru <786394+akru@users.noreply.github.com>
Implemented using
Storage keys: |
akru
left a comment
There was a problem hiding this comment.
Probably, pallet can write system events into storage using offchain indexed api, and then offchain worker will dispatch it when block finish. It looks efficient.
That's a good architectural pattern. The current implementation requires manual calls to
This would eliminate the need for manual runtime integration. Would you like me to implement this pattern? It would involve:
Let me know if you'd like this enhancement implemented. |
…er block Co-authored-by: akru <786394+akru@users.noreply.github.com>
Co-authored-by: akru <786394+akru@users.noreply.github.com>
Adds optional offchain indexer for CPS events with single-write-per-block efficiency and RPC query interface.
Architecture
Event collection: Events tracked in transient storage during block execution, written once in
on_finalizeusingsp_io::offchain_index::set().Offchain processing: Worker reads event queue and indexes into double-map storage (
node_id→block_number→ data).Query interface: Runtime API + JSON-RPC with optional filtering (
node_id,from,to).Key Design
sp_io::offchain_indexcalls throughout palletoffchain-indexerfeature - no manual integration requiredCpsEventenum with variants:NodeCreated,MetaUpdated,PayloadUpdated,NodeMoved,NodeDeletedStorage Structure
Usage
Enable feature:
Runtime API implementation:
RPC query:
Event Tracking
Pallet methods automatically track events:
create_node()→NodeCreatedset_meta()→MetaUpdatedset_payload()→PayloadUpdatedmove_node()→NodeMoveddelete_node()→NodeDeletedEvents accumulate in
ModifiedNodesThisBlock, written once inon_finalize, processed by offchain worker on next block.Original prompt
Objective
Add an offchain-worker based indexer to
pallet-robonomics-cpsthat collects historical data and exposes it via Runtime API and RPC extension.Requirements
1. Pallet Structure
Update/create
frame/cps/with the following structure:2. Feature Flag
Add optional
offchain-workerfeature toframe/cps/Cargo.toml:3. Historical Data Collection
The offchain worker should collect and index three types of historical data:
4. Offchain Worker Implementation (
frame/cps/src/offchain/)The offchain worker should:
offchain_workerhook (gated byoffchain-workerfeature)cps::meta::<timestamp>for meta recordscps::payload::<timestamp>for payload recordscps::operations::<timestamp>for node operationsData structure examples:
5. Runtime API (
frame/cps/rpc/runtime-api/src/lib.rs)Define Runtime API trait:
6. RPC Extension (
frame/cps/rpc/src/lib.rs)Implement JSON-RPC interface:
Return JSON-serializable structures using
serde.7. Pallet Integration
In
frame/cps/src/lib.rs:8. Dependencies
Add necessary dependencies to Cargo.toml files:
frame/cps/Cargo.toml:
sp-io(for offchain storage)sp-runtimeframe-supportframe-systemframe/cps/rpc/runtime-api/Cargo.toml:
sp-apisp-stdparity-scale-codecframe/cps/rpc/Cargo.toml:
jsonrpseeserdesp-apisp-blockchainsp-runtime9. Runtime Integration Example
Provide example of how to integrate into runtime (
runtime/robonomics/src/lib.rs):10. Documentation
Add comprehensive documentation:
11. Code Quality
log::macros#![cfg_attr(not(feature = "std"), no_std)]compatibilityImplementation Notes
pallet-robonomics-cpsdoesn't exist yet, create it following the pattern of existing pallets (d...This pull request was created from Copilot chat.
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.