Skip to content
Open
Show file tree
Hide file tree
Changes from 58 commits
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
a4ec9b3
wip: relayer-rs
serrrfirat Sep 20, 2024
4c7582d
Remove near_sdk
serrrfirat Sep 20, 2024
1047caf
Latest relayer
serrrfirat Sep 20, 2024
cd0ad87
metrics
serrrfirat Sep 20, 2024
0eedfad
fix metrics and await
serrrfirat Sep 20, 2024
7e332f6
wip: alloy-rs
serrrfirat Sep 20, 2024
6307c17
wip: alloy-rs
serrrfirat Sep 20, 2024
9a97d5d
Merge pull request #1 from firatsertgoz/relayer_rust
serrrfirat Sep 20, 2024
9826a50
alloy-rs
serrrfirat Sep 21, 2024
bbc019d
clippy
serrrfirat Sep 21, 2024
e3bb9a8
config and main
serrrfirat Sep 21, 2024
d4a2229
feature parity
serrrfirat Sep 21, 2024
3d155f4
Dockerfile
serrrfirat Sep 22, 2024
4b52feb
docker-compose relayer-rs
serrrfirat Sep 22, 2024
ec0b13e
working relayer
serrrfirat Sep 22, 2024
9bbc73b
Operator scaffold
serrrfirat Sep 23, 2024
9560e6c
wip:near-lake
serrrfirat Sep 23, 2024
e5879da
types and stuff
serrrfirat Sep 23, 2024
9b8e6ea
Merge branch 'NethermindEth:main' into main
serrrfirat Sep 27, 2024
a79ead3
fastnear
serrrfirat Sep 27, 2024
4e36b32
types.rs
serrrfirat Sep 27, 2024
3b9760d
wip: fastnear block listener
serrrfirat Sep 28, 2024
09d1054
wip: tracing and get last final block
serrrfirat Sep 28, 2024
a02cbea
wip: makefile and cfg feature
serrrfirat Sep 28, 2024
ce3fce4
wip: add fastnear types
serrrfirat Sep 28, 2024
00d88c9
broken: indexstreamer type, block_listner edits
serrrfirat Sep 28, 2024
bc57e36
wip: fastnear_indexer single run func
serrrfirat Sep 29, 2024
e51f66a
wip: added main function cfg flag
serrrfirat Sep 29, 2024
ea020a0
wip: some documentation
serrrfirat Sep 29, 2024
7f43c80
wip: cfg fix and tracing
serrrfirat Sep 29, 2024
cf74716
wip: target name fix
serrrfirat Sep 29, 2024
e7c5d18
wip: docker compose setup
serrrfirat Sep 29, 2024
ed2dbe6
remove lake
serrrfirat Sep 29, 2024
f092561
Fixes compilation error in `block_listener` test suite
Fly-Style Oct 2, 2024
3e650df
Merge pull request #2 from Fly-Style/fix/indexer-tests
serrrfirat Oct 2, 2024
683f887
wip: changes on docker
serrrfirat Oct 5, 2024
e4919d9
wip: clippy
serrrfirat Oct 5, 2024
88227d8
wip: README
serrrfirat Oct 5, 2024
ccfc34b
wip: docker compose fix
serrrfirat Oct 5, 2024
916e42b
wip: building and workign fast near indexer for docker compose
serrrfirat Oct 5, 2024
1751c0c
wip: docker compose fix connect rmq
serrrfirat Oct 5, 2024
8c9161a
wip: indexer relayer integration docker compose
serrrfirat Oct 5, 2024
20b9bf2
wip: operator-rs attestor skeleton
serrrfirat Oct 5, 2024
01a7321
wip: attestor
serrrfirat Oct 6, 2024
97fe987
wip: broken attestor, fix Arc
serrrfirat Oct 6, 2024
85fc220
wip: compiles with missing functions
serrrfirat Oct 6, 2024
bed95b2
wip: consumer implementation
serrrfirat Oct 6, 2024
5766be1
wip: commented out operator, unit tests for notifier
serrrfirat Oct 10, 2024
48f162a
wip: some mocking and implementation
serrrfirat Oct 10, 2024
bfadaa8
wip: setup and operator-rs
serrrfirat Oct 13, 2024
bc2e407
wip: attestor setup, bls keypair, mock safeclient
serrrfirat Oct 13, 2024
bc93181
wip: safeclient in rust
serrrfirat Oct 13, 2024
5624c06
wip: safeclient in attestor
serrrfirat Oct 13, 2024
8dc05d7
wip: safeclient builds within docker compose
serrrfirat Oct 13, 2024
0d250e0
wip: fix dockerfile so it doesn't break current indexer
serrrfirat Oct 16, 2024
cf1b4a4
wip: fix docker compose for setups
serrrfirat Oct 16, 2024
b14dfa6
wip: big cargo clean up
serrrfirat Oct 16, 2024
016ebd0
wip: env.example
serrrfirat Oct 17, 2024
2a08d9d
wip: add comments for config validation
serrrfirat Oct 20, 2024
49db338
wip: add bookworm to dockerfile
serrrfirat Oct 20, 2024
392c1ce
wip: add bookworm to fastinedexer dockerfile
serrrfirat Oct 20, 2024
1e933b9
feat: reusable offchain workers
palozano Oct 9, 2024
9fc8be8
chore: add receive lib abi
palozano Oct 9, 2024
9d720c5
chore: fix overall flow
palozano Oct 10, 2024
9e1c1ee
chore: preparing for deployment, lacking correct header
palozano Oct 10, 2024
1b02a1e
chore: ordering
palozano Oct 11, 2024
d4b3c53
refactor: more modular for later executor
palozano Oct 15, 2024
a9508f8
chore: clean up
palozano Oct 15, 2024
af548f8
refactor: dotenv -> dotenvy
palozano Oct 15, 2024
f825036
chore: remove Cargo.lock
palozano Oct 15, 2024
131c351
refactor: move config to `config-rs` crate
palozano Oct 15, 2024
4a5cc5c
refactor: move to manual decoding
palozano Oct 16, 2024
4894f4a
chore: modularize
palozano Oct 17, 2024
290bb85
chore: address pr comments on the worker
palozano Oct 17, 2024
263cb0e
chore: move some files to lib imports
palozano Oct 17, 2024
b9b81fb
chore: remove unused tests for now
palozano Oct 17, 2024
62994a8
chore: reorder imports
palozano Oct 17, 2024
5da950a
wip: fix dockerfile so it doesn't break current indexer
serrrfirat Oct 16, 2024
58d2454
wip: fix docker compose for setups
serrrfirat Oct 16, 2024
c5359dc
wip: big cargo clean up
serrrfirat Oct 16, 2024
f1624fd
chore: remove unused
palozano Oct 17, 2024
5bf79ef
chore: add tests to manual decoding
palozano Oct 17, 2024
90c0e70
chore: use const instead of magic number
palozano Oct 17, 2024
1433aac
chore: comments
palozano Oct 17, 2024
9293854
chore: address pr comments
palozano Oct 17, 2024
ce2d6e6
chore: address pr comments
palozano Oct 18, 2024
2d9d590
Update indexer/Dockerfile
palozano Oct 18, 2024
ed43e71
chore: fix after refactor
palozano Oct 18, 2024
0ed696f
chore: bls bn254 signature takes two uint256
palozano Oct 18, 2024
ec06783
chore: remove v1 contract, leaving only v2 (without version suffix)
palozano Oct 18, 2024
20a711c
chore: address comments
palozano Oct 20, 2024
b19f68e
chore: address pr comments
palozano Oct 21, 2024
20ef139
chore: remove alloy-json-abi as external dep
palozano Oct 22, 2024
fbedff6
chore: broken test to share
palozano Oct 22, 2024
2931fee
chore: fix after rebase
palozano Oct 22, 2024
5cbfb64
refactor: manual encode/decode
palozano Oct 22, 2024
22af924
chore: remove unused
palozano Oct 23, 2024
bbba6d4
chore: address pr comments
palozano Oct 23, 2024
99ad04b
chore: solve conflicts
palozano Oct 23, 2024
9287a5c
Merge branch 'main' into palozano/feat/workers
palozano Oct 23, 2024
f288183
Merge pull request #3 from palozano/palozano/feat/workers
serrrfirat Oct 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8,784 changes: 7,443 additions & 1,341 deletions Cargo.lock

Large diffs are not rendered by default.

27 changes: 27 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,39 @@ resolver = "2"
members = [
"indexer",
"contracts/evm/test/ffi/bls-utils",
"relayer-rs",
"operator-rs",
"core-rs",
]

[workspace.package]
version = "0.0.1"
edition = "2021"
repository = "https://github.com/NethermindEth/near-sffl"

[workspace.dependencies]
alloy = { version = "0.4.2", features = ["full", "node-bindings", "rpc-types-debug", "rpc-types-trace", "json-rpc", "rpc-client", "serde"] }
alloy-primitives = "0.8.3"
alloy-rpc-client = "0.4.2"
alloy-rpc-types = "0.4.2"
alloy-transport-ws = "0.4.2"
alloy-rlp = "0.3.8"
tokio = { version = "1.0", features = ["full"] }
prometheus = "0.13"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
anyhow = "1.0"
tracing = "0.1"
tracing-subscriber = "0.3"
futures-util = "0.3.30"
eyre = "0.6"
bincode = "1.3"
serde_yaml = "0.9"
clap = "4.5.18"
tempfile = "3.10.0"
async-trait = "0.1"
log = "0.4"
core-rs = { path = "core-rs" }

[patch.crates-io]
parity-secp256k1 = { git = 'https://github.com/paritytech/rust-secp256k1.git' }
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ start-indexer: ##
cargo run -p indexer --release -- --home-dir ~/.near/localnet init --chain-id localnet
cargo run -p indexer --release -- --home-dir ~/.near/localnet run --da-contract-ids da.test.near --rollup-ids 2 --rmq-address "amqp://127.0.0.1:5672"

start-indexer-with-fastnear: ##
cargo run --features use_fastnear -p indexer --release -- --home-dir ~/.near/localnet init --chain-id localnet
cargo run --features use_fastnear -p indexer --release -- --home-dir ~/.near/localnet run --da-contract-ids da.test.near --rollup-ids 2 --rmq-address "amqp://127.0.0.1:5672"

start-test-relayer: ##
CGO_LDFLAGS="-L ./relayer/libs ${CGO_LDFLAGS}" go run relayer/cmd/main.go run-args --rpc-url ws://127.0.0.1:8546 --da-account-id da.test.near --key-path ~/.near-credentials/localnet/da.test.near.json

Expand Down
23 changes: 23 additions & 0 deletions core-rs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "core-rs"
version = "0.1.0"
edition = "2021"

[dependencies]
tokio = { workspace = true }
async-trait = { workspace = true }
thiserror = "1.0"
anyhow = { workspace = true }

alloy = { workspace = true }
mockall = "0.13.0"

serde = { workspace = true }
serde_json = { workspace = true }
sha2 = "0.10.6"

log = { workspace = true }
futures-util = { workspace = true }

[dev-dependencies]
tokio-test = "0.4"
6 changes: 6 additions & 0 deletions core-rs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// pub mod chainio;
// pub mod config;
// pub mod metricable;
// pub mod smt;
// pub mod types;
pub mod safeclient;
156 changes: 156 additions & 0 deletions core-rs/src/safeclient/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
use alloy::eips::BlockNumberOrTag;
use alloy::primitives::{B256, U256};
use alloy::providers::{Provider, ProviderBuilder, RootProvider, WsConnect};
use alloy::pubsub::PubSubFrontend;
use alloy::rpc::types::{Block, Filter, Header, Log, Transaction, TransactionReceipt};
use anyhow::Result;
use async_trait::async_trait;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::broadcast;
use futures_util::StreamExt;
use log::{error, warn};


#[async_trait]
pub trait SafeClient: Send + Sync {
async fn block_number(&self) -> Result<U256>;
async fn get_block(&self, block_number: BlockNumberOrTag) -> Result<Option<Block>>;
async fn get_transaction(&self, tx_hash: B256) -> Result<Option<Transaction>>;
async fn get_transaction_receipt(&self, tx_hash: B256) -> Result<Option<TransactionReceipt>>;
async fn get_logs(&self, filter: Filter) -> Result<Vec<Log>>;
async fn subscribe_logs(&self, filter: Filter) -> Result<broadcast::Receiver<Log>>;
async fn subscribe_new_heads(&self) -> Result<broadcast::Receiver<Header>>;
fn close(&self);
}

pub struct SafeEthClient {
provider: Arc<RootProvider<PubSubFrontend>>,
log_resub_interval: Duration,
header_timeout: Duration,
block_chunk_size: u64,
block_max_range: u64,
close_sender: broadcast::Sender<()>,
}

pub struct SafeEthClientOptions {
pub log_resub_interval: Duration,
pub header_timeout: Duration,
pub block_chunk_size: u64,
pub block_max_range: u64,
}

impl Default for SafeEthClientOptions {
fn default() -> Self {
Self {
log_resub_interval: Duration::from_secs(300),
header_timeout: Duration::from_secs(30),
block_chunk_size: 100,
block_max_range: 100,
}
}
}

impl SafeEthClient {
pub async fn new(ws_url: &str, options: SafeEthClientOptions) -> Result<Self> {
let ws = WsConnect::new(ws_url);
let provider = ProviderBuilder::new().on_ws(ws).await?;
let (close_sender, _) = broadcast::channel(1);

Ok(Self {
provider: Arc::new(provider),
log_resub_interval: options.log_resub_interval,
header_timeout: options.header_timeout,
block_chunk_size: options.block_chunk_size,
block_max_range: options.block_max_range,
close_sender,
})
}
}

#[async_trait]
impl SafeClient for SafeEthClient {
async fn block_number(&self) -> Result<U256> {
Ok(U256::from(self.provider.get_block_number().await?))
}

async fn get_block(&self, block_number: BlockNumberOrTag) -> Result<Option<Block>> {
Ok(self.provider.get_block(alloy::eips::BlockId::Number(block_number), alloy::rpc::types::BlockTransactionsKind::Hashes).await?)
}

async fn get_transaction(&self, tx_hash: B256) -> Result<Option<Transaction>> {
Ok(self.provider.get_transaction_by_hash(tx_hash).await?)
}

async fn get_transaction_receipt(&self, tx_hash: B256) -> Result<Option<TransactionReceipt>> {
Ok(self.provider.get_transaction_receipt(tx_hash).await?)
}

async fn get_logs(&self, filter: Filter) -> Result<Vec<Log>> {
Ok(self.provider.get_logs(&filter).await?)
}

async fn subscribe_logs(&self, filter: Filter) -> Result<broadcast::Receiver<Log>> {
let (tx, rx) = broadcast::channel(100);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a likelihood of more than 100 here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good one - depends on how successful NFFL is really :) Maybe I'll make this into a runtime config.

let log_resub_interval = self.log_resub_interval;
let subscription = self.provider.subscribe_logs(&filter).await?;
let mut stream = subscription.into_stream();

tokio::spawn(async move {
loop {
tokio::select! {
Some(log) = stream.next() => {
if tx.send(log).is_err() {
error!("Error sending log: channel closed");
break;
}
}
_ = tokio::time::sleep(log_resub_interval) => {
warn!("Timeout waiting for new log");
break;
}
else => {
error!("Log stream ended unexpectedly");
break;
}
}
}
});

Ok(rx)
}

async fn subscribe_new_heads(&self) -> Result<broadcast::Receiver<Header>> {
let (tx, rx) = broadcast::channel(100);
let header_timeout = self.header_timeout;
let subscription = self.provider.subscribe_blocks().await?;
let mut stream = subscription.into_stream();

tokio::spawn(async move {
loop {
tokio::select! {
Some(block) = stream.next() => {
if tx.send(block.header).is_err() {
error!("Error sending header: channel closed");
break;
}
}
_ = tokio::time::sleep(header_timeout) => {
warn!("Timeout waiting for new header");
break;
}
else => {
error!("Header stream ended unexpectedly");
break;
}
}
}
});

Ok(rx)
}

fn close(&self) {
let _ = self.close_sender.send(());
}
}
5 changes: 5 additions & 0 deletions core-rs/src/safeclient/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod client;
mod utils;

pub use client::{SafeClient, SafeEthClient, SafeEthClientOptions};
pub use utils::*;
26 changes: 26 additions & 0 deletions core-rs/src/safeclient/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use alloy::{primitives::B256, rpc::types::Log};
use sha2::{Digest, Sha256};

pub fn hash_log(log: &Log) -> B256 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used? Are logs hashed with SHA256 or Keccak?

let mut hasher = Sha256::new();

// Hash the log fields
hasher.update(log.address().as_slice());
for topic in log.topics() {
hasher.update(topic.as_slice());
}
hasher.update(log.data().data.clone());

// Hash additional block and tx info
if let Some(block_hash) = log.block_hash {
hasher.update(block_hash.as_slice());
}
if let Some(transaction_hash) = log.transaction_hash {
hasher.update(transaction_hash.as_slice());
}
if let Some(log_index) = log.log_index {
hasher.update(&log_index.to_be_bytes());
}

B256::from_slice(&hasher.finalize())
}
20 changes: 12 additions & 8 deletions indexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,33 @@ edition = "2021"
actix = "0.13.1"
actix-web = "4.5.1"
futures = "0.3.5"
tokio = { version = "1.28.2", features = ["sync", "time"] }
tokio = { workspace = true }
deadpool = "0.10.0"
lapin = "2.3.1"
deadpool-lapin = "0.11.0"
tokio-executor-trait = "2.1.0"
tokio-reactor-trait = "1.1.0"
prometheus = "0.13.3"
prometheus = { workspace = true }

clap = { version = "4.4.11", features = ["color", "derive", "env"] }
clap = { workspace = true }
openssl-probe = "0.1.4"
serde = { version = "1", features = ["derive"] }
serde_json = "1.0.68"
serde = { workspace = true }
serde_json = { workspace = true }

tracing = { version = "0.1.36", features = ["std"] }
tracing = { workspace = true }
thiserror = "1.0.56"
anyhow = "1.0.79"
anyhow = { workspace = true }

near-indexer = { git = "https://github.com/near/nearcore", rev = "b3d767e7664d8e123a35313ccc66c8ac1afb2058" }
near-client = { git = "https://github.com/near/nearcore", rev = "b3d767e7664d8e123a35313ccc66c8ac1afb2058" }
near-o11y = { git = "https://github.com/near/nearcore", rev = "b3d767e7664d8e123a35313ccc66c8ac1afb2058" }
near-client-primitives = { git = "https://github.com/near/nearcore", rev = "b3d767e7664d8e123a35313ccc66c8ac1afb2058" }
reqwest = { version = "0.12.7", features = ["json"] }
borsh = { version = "1.0.0", features = ["derive", "rc"] }
serde_yaml = "0.9.34"
serde_yaml = { workspace = true }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gud


[dev-dependencies]
near-crypto = { git = "https://github.com/near/nearcore", rev = "b3d767e7664d8e123a35313ccc66c8ac1afb2058" }

[features]
use_fastnear = []
6 changes: 3 additions & 3 deletions indexer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ RUN apt-get update -qq && \
llvm \
clang

COPY ./indexer/Cargo.toml .
COPY ./Cargo.toml .
RUN mkdir ./src && echo "fn main() {}" > ./src/main.rs
RUN cargo build --release -p indexer

COPY ./indexer .
COPY . .
RUN touch ./src/main.rs

RUN cargo build --release -p indexer
Expand All @@ -29,7 +29,7 @@ WORKDIR /indexer-app
RUN apt update && apt install -yy openssl ca-certificates jq curl

COPY --from=builder /tmp/indexer/target/release/indexer .
COPY ./indexer/entrypoint.sh ./entrypoint.sh
COPY entrypoint.sh ./entrypoint.sh
RUN chmod +x ./entrypoint.sh

EXPOSE 3030
Expand Down
47 changes: 47 additions & 0 deletions indexer/FastIndexer.dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
FROM rust:1.79 AS builder
WORKDIR /tmp/indexer
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
FROM rust:1.79 AS builder
WORKDIR /tmp/indexer
FROM rust:1.79.0-bookworm AS builder
WORKDIR /tmp/indexer

This will make sure the builder doesn't break since it's using bookworm. You could also elect to use bookworm-slim


# Copy from nearcore:
# https://github.com/near/nearcore/blob/master/Dockerfile
RUN apt-get update -qq && \
apt-get install -y \
git \
cmake \
g++ \
pkg-config \
libssl-dev \
curl \
llvm \
clang

COPY ./Cargo.toml .
RUN mkdir ./src && echo "fn main() {}" > ./src/main.rs
RUN cargo build --release -p indexer --features use_fastnear

COPY . .
RUN touch ./src/main.rs

RUN cargo build --release -p indexer --features use_fastnear

FROM debian:bookworm-slim as runtime
WORKDIR /indexer-app

RUN apt update && apt install -yy openssl ca-certificates jq curl

COPY --from=builder /tmp/indexer/target/release/indexer .
COPY entrypoint.sh ./entrypoint.sh
RUN chmod +x ./entrypoint.sh

EXPOSE 3030

HEALTHCHECK --interval=20s --timeout=30s --retries=10000 \
CMD (curl -f -s -X POST -H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"block","params":{"finality":"optimistic"},"id":"dontcare"}' \
http://localhost:3030 | \
jq -es 'if . == [] then null else .[] | (now - (.result.header.timestamp / 1000000000)) < 10 end') && \
(curl -f -s -X POST -H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"status","params":[],"id":"dontcare"}' \
http://localhost:3030 | \
jq -es 'if . == [] then null else .[] | .result.sync_info.syncing == false end')

ENTRYPOINT [ "./entrypoint.sh" ]
Loading