2

在substrate的pow共识模块中,矿工不通过RPC访问挖矿,如何访问?

我不知道。

fn mine(
        &self,
        parent: &BlockId<B>,
        pre_hash: &H256,
        difficulty: Difficulty,
        round: u32,
    ) -> Result<Option<RawSeal>, String> {
        let mut rng = SmallRng::from_rng(&mut thread_rng())
            .map_err(|e| format!("Initialize RNG failed for mining: {:?}", e))?;
        let key_hash = key_hash(self.client.as_ref(), parent)?;

        for _ in 0..round {
            let nonce = H256::random_using(&mut rng);

            let compute = Compute {
                key_hash,
                difficulty,
                pre_hash: *pre_hash,
                nonce,
            };

            let seal = compute.compute();

            if is_valid_hash(&seal.work, difficulty) {
                return Ok(Some(seal.encode()))
            }
        }
        Ok(None)
    }
4

1 回答 1

3

我建议您遵循类似于Kulupu的模式来创建 PoW 基板区块链。

在 Kulupu 中,如果 Substrate 服务检测到您是“权威”,它似乎会开始挖掘:

/// Builds a new service for a full client.
pub fn new_full<C: Send + Default + 'static>(config: Configuration<C, GenesisConfig>, author: Option<&str>, threads: usize, round: u32)
    -> Result<impl AbstractService, ServiceError>
{
    let is_authority = config.roles.is_authority();

    let (builder, inherent_data_providers) = new_full_start!(config, author);

    let service = builder
        .with_network_protocol(|_| Ok(NodeProtocol::new()))?
        .with_finality_proof_provider(|_client, _backend| {
            Ok(Arc::new(()) as _)
        })?
        .build()?;

    if is_authority {
        for _ in 0..threads {
            let proposer = basic_authorship::ProposerFactory {
                client: service.client(),
                transaction_pool: service.transaction_pool(),
            };

            consensus_pow::start_mine(
                Box::new(service.client().clone()),
                service.client(),
                kulupu_pow::RandomXAlgorithm::new(service.client()),
                proposer,
                None,
                round,
                service.network(),
                std::time::Duration::new(2, 0),
                service.select_chain().map(|v| v.clone()),
                inherent_data_providers.clone(),
            );
        }
    }

    Ok(service)
}

在这种情况下,您只需要使用--validator标志和--author带有地址的标志来启动您的节点:

cargo run --release -- --validator --author 0x7e946b7dd192307b4538d664ead95474062ac3738e04b5f3084998b76bc5122d
于 2019-11-12T11:40:42.950 回答