fn fetch_validator_set(
		relay_contract: Vec<u8>,
		appchain_id: u32,
		seq_num: u32,
	) -> Result<ValidatorSet<<T as frame_system::Config>::AccountId>, http::Error> {
		// We want to keep the offchain worker execution time reasonable, so we set a hard-coded
		// deadline to 2s to complete the external call.
		// You can also wait idefinitely for the response, however you may still get a timeout
		// coming from the host machine.
		let deadline = sp_io::offchain::timestamp().add(Duration::from_millis(2_000));
		// Initiate an external HTTP GET request.
		// This is using high-level wrappers from `sp_runtime`, for the low-level calls that
		// you can find in `sp_io`. The API is trying to be similar to `reqwest`, but
		// since we are running in a custom WASM execution environment we can't simply
		// import the library here.
		let args = Self::encode_args(appchain_id, seq_num).ok_or_else(|| {
			log::info!("🐙 Encode args error");
			http::Error::Unknown
		})?;

		let mut body = br#"
		{
			"jsonrpc": "2.0",
			"id": "dontcare",
			"method": "query",
			"params": {
				"request_type": "call_function",
				"finality": "final",
				"account_id": ""#
			.to_vec();
		body.extend(&relay_contract);
		body.extend(
			br#"",
				"method_name": "get_validator_set",
				"args_base64": ""#,
		);
		body.extend(&args);
		body.extend(
			br#""
			}
		}"#,
		);
		let request = http::Request::default()
			.method(http::Method::Post)
			.url("https://rpc.testnet.near.org")
			.body(vec![body])
			.add_header("Content-Type", "application/json");
		// We set the deadline for sending of the request, note that awaiting response can
		// have a separate deadline. Next we send the request, before that it's also possible
		// to alter request headers or stream body content in case of non-GET requests.
		let pending = request
			.deadline(deadline)
			.send()
			.map_err(|_| http::Error::IoError)?;

		// The request is already being processed by the host, we are free to do anything
		// else in the worker (we can send multiple concurrent requests too).
		// At some point however we probably want to check the response though,
		// so we can block current thread and wait for it to finish.
		// Note that since the request is being driven by the host, we don't have to wait
		// for the request to have it complete, we will just not read the response.
		let response = pending
			.try_wait(deadline)
			.map_err(|_| http::Error::DeadlineReached)??;
		// Let's check the status code before we proceed to reading the response.
		if response.code != 200 {
			log::info!("🐙 Unexpected status code: {}", response.code);
			return Err(http::Error::Unknown);
		}

		// Next we want to fully read the response body and collect it to a vector of bytes.
		// Note that the return object allows you to read the body in chunks as well
		// with a way to control the deadline.
		let body = response.body().collect::<Vec<u8>>();

		// Create a str slice from the body.
		let body_str = sp_std::str::from_utf8(&body).map_err(|_| {
			log::info!("🐙 No UTF8 body");
			http::Error::Unknown
		})?;
		log::info!("🐙 Got response: {:?}", body_str);

		let val_set = match Self::parse_validator_set(body_str) {
			Some(val_set) => Ok(val_set),
			None => {
				log::info!(
					"🐙 Unable to extract validator set from the response: {:?}",
					body_str
				);
				Err(http::Error::Unknown)
			}
		}?;

		log::info!("🐙 Got validator set: {:?}", val_set);

		Ok(val_set)
	}
/// Fetch the validator set of a specified appchain with seq_num from relay contract.
Fetch the validator set of a specified appchain with seq_num from relay contract.
Fetch the validator set of a specified appchain with seq_num from relay contract .
fn fetch_validator_set(
		relay_contract: Vec<u8>,
		appchain_id: u32,
		seq_num: u32,
	) -> Result<ValidatorSet<<T as frame_system::Config>::AccountId>, http::Error> {
		// We want to keep the offchain worker execution time reasonable, so we set a hard-coded
		// deadline to 2s to complete the external call.
		// You can also wait idefinitely for the response, however you may still get a timeout
		// coming from the host machine.
		let deadline = sp_io::offchain::timestamp().add(Duration::from_millis(2_000));
		// Initiate an external HTTP GET request.
		// This is using high-level wrappers from `sp_runtime`, for the low-level calls that
		// you can find in `sp_io`. The API is trying to be similar to `reqwest`, but
		// since we are running in a custom WASM execution environment we can't simply
		// import the library here.
		let args = Self::encode_args(appchain_id, seq_num).ok_or_else(|| {
			log::info!("🐙 Encode args error");
			http::Error::Unknown
		})?;
		let mut body = br#"
		{
			"jsonrpc": "2.0",
			"id": "dontcare",
			"method": "query",
			"params": {
				"request_type": "call_function",
				"finality": "final",
				"account_id": ""#
			.to_vec();
		body.extend(&relay_contract);
		body.extend(
			br#"",
				"method_name": "get_validator_set",
				"args_base64": ""#,
		);
		body.extend(&args);
		body.extend(
			br#""
			}
		}"#,
		);
		let request = http::Request::default()
			.method(http::Method::Post)
			.url("https://rpc.testnet.near.org")
			.body(vec![body])
			.add_header("Content-Type", "application/json");
		// We set the deadline for sending of the request, note that awaiting response can
		// have a separate deadline. Next we send the request, before that it's also possible
		// to alter request headers or stream body content in case of non-GET requests.
		let pending = request
			.deadline(deadline)
			.send()
			.map_err(|_| http::Error::IoError)?;
		// The request is already being processed by the host, we are free to do anything
		// else in the worker (we can send multiple concurrent requests too).
		// At some point however we probably want to check the response though,
		// so we can block current thread and wait for it to finish.
		// Note that since the request is being driven by the host, we don't have to wait
		// for the request to have it complete, we will just not read the response.
		let response = pending
			.try_wait(deadline)
			.map_err(|_| http::Error::DeadlineReached)??;
		// Let's check the status code before we proceed to reading the response.
		if response.code != 200 {
			log::info!("🐙 Unexpected status code: {}", response.code);
			return Err(http::Error::Unknown);
		}
		// Next we want to fully read the response body and collect it to a vector of bytes.
		// Note that the return object allows you to read the body in chunks as well
		// with a way to control the deadline.
		let body = response.body().collect::<Vec<u8>>();
		// Create a str slice from the body.
		let body_str = sp_std::str::from_utf8(&body).map_err(|_| {
			log::info!("🐙 No UTF8 body");
			http::Error::Unknown
		})?;
		log::info!("🐙 Got response: {:?}", body_str);
		let val_set = match Self::parse_validator_set(body_str) {
			Some(val_set) => Ok(val_set),
			None => {
				log::info!(
					"🐙 Unable to extract validator set from the response: {:?}",
					body_str
				);
				Err(http::Error::Unknown)
			}
		}?;
		log::info!("🐙 Got validator set: {:?}", val_set);
		Ok(val_set)
	}
fn fetch_validator_set ( relay_contract : Vec < u8 > , appchain_id : u32 , seq_num : u32 , ) -> Result < ValidatorSet < < T as frame_system :: Config > :: AccountId > , http :: Error > { // We want to keep the offchain worker execution time reasonable, so we set a hard-coded // deadline to 2s to complete the external call. // You can also wait idefinitely for the response, however you may still get a timeout // coming from the host machine. let deadline = sp_io :: offchain :: timestamp ( ) . add ( Duration :: from_millis ( 2_000 ) ) ; // Initiate an external HTTP GET request. // This is using high-level wrappers from `sp_runtime`, for the low-level calls that // you can find in `sp_io`. The API is trying to be similar to `reqwest`, but // since we are running in a custom WASM execution environment we can't simply // import the library here. let args = Self :: encode_args ( appchain_id , seq_num ) . ok_or_else ( | | { log :: info ! ( " http :: Error :: Unknown } ) ? ; let mut body = br#"
		{
			"jsonrpc": "2.0",
			"id": "dontcare",
			"method": "query",
			"params": {
				"request_type": "call_function",
				"finality": "final",
				"account_id": ""# . to_vec ( ) ; body . extend ( & relay_contract ) ; body . extend ( br#"",
				"method_name": "get_validator_set",
				"args_base64": ""# , ) ; body . extend ( & args ) ; body . extend ( br#""
			}
		}"# , ) ; let request = http :: Request :: default ( ) . method ( http :: Method :: Post ) . url ( " " ) . body ( vec ! [ body ] ) . add_header ( " " , " " ) ; // We set the deadline for sending of the request, note that awaiting response can // have a separate deadline. Next we send the request, before that it's also possible // to alter request headers or stream body content in case of non-GET requests. let pending = request . deadline ( deadline ) . send ( ) . map_err ( | _ | http :: Error :: IoError ) ? ; // The request is already being processed by the host, we are free to do anything // else in the worker (we can send multiple concurrent requests too). // At some point however we probably want to check the response though, // so we can block current thread and wait for it to finish. // Note that since the request is being driven by the host, we don't have to wait // for the request to have it complete, we will just not read the response. let response = pending . try_wait ( deadline ) . map_err ( | _ | http :: Error :: DeadlineReached ) ? ? ; // Let's check the status code before we proceed to reading the response. if response . code != 200 { log :: info ! ( " r ponse.co e); return Err ( http :: Error :: Unknown ) ; } // Next we want to fully read the response body and collect it to a vector of bytes. // Note that the return object allows you to read the body in chunks as well // with a way to control the deadline. let body = response . body ( ) . collect :: < Vec < u8 > > ( ) ; // Create a str slice from the body. let body_str = sp_std :: str :: from_utf8 ( & body ) . map_err ( | _ | { log :: info ! ( " http :: Error :: Unknown } ) ? ; log :: info ! ( " b y_str); let val_set = match Self :: parse_validator_set ( body_str ) { Some ( val_set ) => Ok ( val_set ) , None => { log :: info ! ( " body_str ) ; Err ( http :: Error :: Unknown ) } } ? ; log :: info ! ( " v _set); Ok ( val_set ) }
Fetch the validator set of a specified appchain with seq_num from relay contract.
Fetch the validator set of a specified appchain with seq_num from relay contract .
relay_contract
Vec<u8>
appchain_id
u32
seq_num
u32
relay_contract
Vec<u8>
appchain_id
u32
seq_num
u32