Skip to content

Commit

Permalink
feat: prompt for start block on rpc url networks in contract import (#4)
Browse files Browse the repository at this point in the history
* feat: prompt for start_block on rpc contract import indexers

* tests

* feat: use custom type to allow recoverability for invalid startblock

---------

Co-authored-by: denhampreen <[email protected]>
  • Loading branch information
JasoonS and DenhamPreen authored Oct 22, 2024
1 parent d252373 commit d5dfea9
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 33 deletions.
1 change: 1 addition & 0 deletions codegenerator/cli/CommandLineHelp.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ Initialize from a local json ABI file
* `--contract-name <CONTRACT_NAME>` — The name of the contract
* `-b`, `--blockchain <BLOCKCHAIN>` — Network from which contract address should be fetched for migration
* `-r`, `--rpc-url <RPC_URL>` — The rpc url to use if the network id used is unsupported by our hypersync
* `-s`, `--start-block <START_BLOCK>` — The start block to use on this network



Expand Down
6 changes: 4 additions & 2 deletions codegenerator/cli/npm/envio/evm.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,17 @@
"start_block": {
"description": "The block at which the indexer should start ingesting data",
"type": "integer",
"format": "int32"
"format": "uint64",
"minimum": 0
},
"end_block": {
"description": "The block at which the indexer should terminate.",
"type": [
"integer",
"null"
],
"format": "int32"
"format": "uint64",
"minimum": 0
},
"contracts": {
"description": "All the contracts that should be indexed on the given network",
Expand Down
6 changes: 4 additions & 2 deletions codegenerator/cli/npm/envio/fuel.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,17 @@
"start_block": {
"description": "The block at which the indexer should start ingesting data",
"type": "integer",
"format": "int32"
"format": "uint64",
"minimum": 0
},
"end_block": {
"description": "The block at which the indexer should terminate.",
"type": [
"integer",
"null"
],
"format": "int32"
"format": "uint64",
"minimum": 0
},
"hyperfuel_config": {
"description": "Optional HyperFuel Config for additional fine-tuning",
Expand Down
4 changes: 4 additions & 0 deletions codegenerator/cli/src/cli_args/clap_definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,10 @@ pub mod evm {
///The rpc url to use if the network id used is unsupported by our hypersync
#[arg(short, long)]
pub rpc_url: Option<String>,

///The start block to use on this network
#[arg(short, long)]
pub start_block: Option<u64>,
}
}

Expand Down
8 changes: 4 additions & 4 deletions codegenerator/cli/src/cli_args/init_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ pub mod evm {
.or_insert({
let rpc_config = match &selected_network.network {
NetworkKind::Supported(_) => None,
NetworkKind::Unsupported(_, url) => Some(RpcConfig {
url: url.clone().into(),
NetworkKind::Unsupported { rpc_url, .. } => Some(RpcConfig {
url: rpc_url.clone().into(),
sync_config: None,
}),
};
Expand All @@ -117,7 +117,7 @@ pub mod evm {
NetworkKind::Supported(network) => {
chain_helpers::Network::from(network).get_finite_end_block()
}
NetworkKind::Unsupported(network_id, _) => {
NetworkKind::Unsupported { network_id, .. } => {
chain_helpers::Network::from_network_id(network_id)
.ok()
.map(|network| network.get_finite_end_block())
Expand All @@ -129,7 +129,7 @@ pub mod evm {
id: selected_network.network.get_network_id(),
hypersync_config: None,
rpc_config,
start_block: 0,
start_block: selected_network.network.get_start_block(),
end_block,
confirmed_block_threshold: None,
contracts: Vec::new(),
Expand Down
39 changes: 33 additions & 6 deletions codegenerator/cli/src/cli_args/interactive_init/evm_prompts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ impl ContractImportArgs {
///To validate that a user is not double selecting a network id
fn prompt_for_network_id(
opt_rpc_url: &Option<String>,
opt_start_block: &Option<u64>,
already_selected_ids: Vec<u64>,
) -> Result<converters::NetworkKind> {
//The first option of the list, funnels the user to enter a u64
Expand Down Expand Up @@ -221,7 +222,7 @@ fn prompt_for_network_id(
//Convert the id into a supported or unsupported network.
//If unsupported, it will use the optional rpc url or prompt
//for an rpc url
get_converter_network_u64(network_id, opt_rpc_url)?
get_converter_network_u64(network_id, opt_rpc_url, opt_start_block)?
}
//If a supported network choice was selected. We should be able to
//parse it back to a supported network since it was serialized as a
Expand All @@ -241,6 +242,7 @@ fn prompt_for_network_id(
fn get_converter_network_u64(
network_id: u64,
rpc_url: &Option<String>,
start_block: &Option<u64>,
) -> Result<converters::NetworkKind> {
let maybe_supported_network =
Network::from_network_id(network_id).and_then(|n| Ok(HypersyncNetwork::try_from(n)?));
Expand All @@ -252,13 +254,36 @@ fn get_converter_network_u64(
Some(r) => r.clone(),
None => prompt_for_rpc_url()?,
};
converters::NetworkKind::Unsupported(network_id, rpc_url)
let start_block = match start_block {
Some(s) => s.clone(),
None => prompt_for_start_block()?,
};

converters::NetworkKind::Unsupported {
network_id,
rpc_url,
start_block,
}
}
};

Ok(network)
}

///Prompt the user to enter a starting block
///only prompt when used when using rpc as it could
///be very slow to have the startblock at 0 with rpc 🦶🔫
fn prompt_for_start_block() -> Result<u64> {
let start_block = CustomType::<u64>::new(
"Please provide a start block for this network \
(this can be edited later in config.yaml):",
)
.with_error_message("Invalid start block input, please enter a number 0 or greater")
.prompt()?;

Ok(start_block)
}

///Prompt the user to enter an rpc url
fn prompt_for_rpc_url() -> Result<String> {
Text::new(
Expand Down Expand Up @@ -349,9 +374,9 @@ impl LocalImportArgs {
match &self.blockchain {
Some(b) => {
let network_id: u64 = (b.clone()).into();
get_converter_network_u64(network_id, &self.rpc_url)
get_converter_network_u64(network_id, &self.rpc_url, &self.start_block)
}
None => prompt_for_network_id(&self.rpc_url, vec![]),
None => prompt_for_network_id(&self.rpc_url, &self.start_block, vec![]),
}
}

Expand Down Expand Up @@ -385,10 +410,12 @@ impl Contract for SelectedContract {
//In a new network case, no RPC url could be
//derived from CLI flags
const NO_RPC_URL: Option<String> = None;
const NO_START_BLOCK: Option<u64> = None;

//Select a new network (not from the list of existing network ids already added)
let selected_network = prompt_for_network_id(&NO_RPC_URL, self.get_network_ids())
.context("Failed selecting network")?;
let selected_network =
prompt_for_network_id(&NO_RPC_URL, &NO_START_BLOCK, self.get_network_ids())
.context("Failed selecting network")?;

//Instantiate a network_selection without any contract addresses
let network_selection =
Expand Down
2 changes: 1 addition & 1 deletion codegenerator/cli/src/config_parsing/chain_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ impl Network {
}

/// Returns the end block for this network if it is finite
pub fn get_finite_end_block(&self) -> Option<i32> {
pub fn get_finite_end_block(&self) -> Option<u64> {
match self {
Self::Goerli => Some(10_387_962),
Self::Mumbai => Some(47_002_303),
Expand Down
24 changes: 15 additions & 9 deletions codegenerator/cli/src/config_parsing/contract_import/converters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,33 @@ impl SelectedContract {
}
}

type NetworkId = u64;
type RpcUrl = String;

#[derive(Clone, Debug)]
pub enum NetworkKind {
Supported(HypersyncNetwork),
Unsupported(NetworkId, RpcUrl),
Unsupported {
network_id: u64,
rpc_url: String,
start_block: u64,
},
}

impl NetworkKind {
pub fn get_network_id(&self) -> NetworkId {
pub fn get_network_id(&self) -> u64 {
match self {
Self::Supported(n) => n.clone() as u64,
Self::Unsupported(n, _) => *n,
Self::Unsupported { network_id, .. } => *network_id,
}
}
pub fn get_start_block(&self) -> u64 {
match self {
Self::Supported(_) => 0,
Self::Unsupported { start_block, .. } => *start_block,
}
}

pub fn uses_hypersync(&self) -> bool {
match self {
Self::Supported(_) => true,
Self::Unsupported(_, _) => false,
Self::Unsupported { .. } => false,
}
}
}
Expand All @@ -88,7 +94,7 @@ impl Display for NetworkKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self {
Self::Supported(n) => write!(f, "{}", n),
Self::Unsupported(n, _) => write!(f, "{}", n),
Self::Unsupported { network_id, .. } => write!(f, "{}", network_id),
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions codegenerator/cli/src/config_parsing/human_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,10 @@ pub mod evm {
)]
pub confirmed_block_threshold: Option<i32>,
#[schemars(description = "The block at which the indexer should start ingesting data")]
pub start_block: i32,
pub start_block: u64,
#[serde(skip_serializing_if = "Option::is_none")]
#[schemars(description = "The block at which the indexer should terminate.")]
pub end_block: Option<i32>,
pub end_block: Option<u64>,
#[schemars(description = "All the contracts that should be indexed on the given network")]
pub contracts: Vec<NetworkContract<ContractConfig>>,
}
Expand Down Expand Up @@ -498,10 +498,10 @@ pub mod fuel {
#[schemars(description = "Public chain/network id")]
pub id: NetworkId,
#[schemars(description = "The block at which the indexer should start ingesting data")]
pub start_block: i32,
pub start_block: u64,
#[serde(skip_serializing_if = "Option::is_none")]
#[schemars(description = "The block at which the indexer should terminate.")]
pub end_block: Option<i32>,
pub end_block: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
#[schemars(description = "Optional HyperFuel Config for additional fine-tuning")]
pub hyperfuel_config: Option<HyperfuelConfig>,
Expand Down
4 changes: 2 additions & 2 deletions codegenerator/cli/src/config_parsing/system_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,8 @@ impl SyncSource {
pub struct Network {
pub id: u64,
pub sync_source: SyncSource,
pub start_block: i32,
pub end_block: Option<i32>,
pub start_block: u64,
pub end_block: Option<u64>,
pub confirmed_block_threshold: i32,
pub contracts: Vec<NetworkContract>,
}
Expand Down
2 changes: 1 addition & 1 deletion codegenerator/cli/src/config_parsing/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl human_config::evm::Network {
let is_unordered_multichain_mode = human_config.unordered_multichain_mode.unwrap_or(false);
let is_multichain_indexer = human_config.networks.len() > 1;
if !is_unordered_multichain_mode && is_multichain_indexer {
let make_err = |finite_end_block: i32| {
let make_err = |finite_end_block: u64| {
Err(anyhow!(
"Network {} has a finite end block of {}. Please set an end_block that is \
less than or equal to the finite end block in your config or set \
Expand Down
4 changes: 2 additions & 2 deletions codegenerator/cli/src/hbs_templating/codegen_templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,8 +783,8 @@ struct NetworkTemplate {
hypersync_config: Option<HypersyncConfig>,
hyperfuel_config: Option<HyperfuelConfig>,
confirmed_block_threshold: i32,
start_block: i32,
end_block: Option<i32>,
start_block: u64,
end_block: Option<u64>,
}

impl NetworkTemplate {
Expand Down

0 comments on commit d5dfea9

Please sign in to comment.