Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #32 from subspace/relayer-generic-metadata
Browse files Browse the repository at this point in the history
Relayer generic metadata
  • Loading branch information
isSerge authored Sep 26, 2021
2 parents 25b0aba + ec6e8f9 commit 8e09d6b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 32 deletions.
37 changes: 10 additions & 27 deletions crates/pallet-feeds/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
use frame_support::{dispatch::DispatchResult, pallet_prelude::*};
use frame_system::pallet_prelude::*;
pub use pallet::*;
use scale_info::TypeInfo;
use sp_core::H256;
use sp_std::vec::Vec;

#[frame_support::pallet]
Expand All @@ -20,20 +18,11 @@ pub mod pallet {

pub type PutDataObject = Vec<u8>;
pub type FeedId = u64;
pub type ObjectMetadata = Vec<u8>;

// TODO: make it more generic
#[derive(Encode, Decode, Debug, Clone, Eq, PartialEq, TypeInfo)]
pub struct ObjectMetadata {
pub feed_id: FeedId,
// last block hash
pub hash: H256,
// last block number
pub number: u32,
}

// TODO: probably change (H256, u32) to Vec<u8>
#[pallet::storage]
pub type Feeds<T: Config> = StorageMap<_, Blake2_128Concat, FeedId, (H256, u32), OptionQuery>;
pub type Feeds<T: Config> =
StorageMap<_, Blake2_128Concat, FeedId, ObjectMetadata, OptionQuery>;

#[pallet::storage]
#[pallet::getter(fn current_feed_id)]
Expand All @@ -58,25 +47,21 @@ pub mod pallet {
#[pallet::weight(10_000)]
pub fn put(
origin: OriginFor<T>,
feed_id: FeedId,
data: PutDataObject,
metadata: ObjectMetadata,
) -> DispatchResult {
let who = ensure_signed(origin)?;

log::info!("metadata: {:?}", metadata);

let ObjectMetadata {
feed_id,
hash,
number,
} = metadata;
// TODO: add data handling
log::debug!("metadata: {:?}", metadata);
log::debug!("data.len: {:?}", data.len());

ensure!(Feeds::<T>::contains_key(feed_id), Error::<T>::UknownFeedId);
let current_feed_id = Self::current_feed_id();

Feeds::<T>::mutate_exists(feed_id, |values| *values = Some((hash, number)));
ensure!(current_feed_id >= feed_id, Error::<T>::UknownFeedId);

// TODO: add data handling
log::info!("data.len: {:?}", data.len());
Feeds::<T>::insert(feed_id, metadata.clone());

Self::deposit_event(Event::DataSubmitted(metadata, who));

Expand All @@ -90,8 +75,6 @@ pub mod pallet {

let feed_id = Self::current_feed_id();

Feeds::<T>::insert(feed_id, (H256::default(), u32::default()));

CurrentFeedId::<T>::mutate(|feed_id| *feed_id = feed_id.saturating_add(1));

Self::deposit_event(Event::FeedCreated(feed_id, who));
Expand Down
3 changes: 1 addition & 2 deletions relayer/src/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ class Source {
console.log(`Chain ${this.chain}: Finalized block size: ${size / 1024} Kb`);

const metadata = {
feedId: this.feedId,
hash,
// TODO: probably there is a better way - investigate
number: this.api.createType("U32", number.toNumber()),
};

return { block: hex, metadata };
return { feedId: this.feedId, block: hex, metadata };
};

subscribeBlocks = (): Observable<TxData> =>
Expand Down
7 changes: 5 additions & 2 deletions relayer/src/target.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ class Target {
}

// TODO: signer should be proxy account per feed
private async sendBlockTx({ block, metadata }: TxData) {
private async sendBlockTx({ feedId, block, metadata }: TxData) {
// metadata is stored as Vec<u8>
// to decode: new TextDecoder().decode(new Uint8Array([...]))
const metadataPayload = JSON.stringify(metadata);
return (
this.api.rx.tx.feeds
.put(block, metadata)
.put(feedId, block, metadataPayload)
// it is required to specify nonce, otherwise transaction within same block will be rejected
// if nonce is -1 API will do the lookup for the right value
// https://polkadot.js.org/docs/api/cookbook/tx/#how-do-i-take-the-pending-tx-pool-into-account-in-my-nonce
Expand Down
2 changes: 1 addition & 1 deletion relayer/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { U64, U32 } from "@polkadot/types/primitive";
import { Hash } from "@polkadot/types/interfaces";

export type TxData = {
feedId: U64;
block: string;
metadata: Metadata;
};

type Metadata = {
feedId: U64;
hash: Hash;
number: U32;
};

0 comments on commit 8e09d6b

Please sign in to comment.