【问题标题】:How to include a json-rpc in the pow consensus building on Substrate?如何在 Substrate 的 pow 共识中包含一个 json-rpc?
【发布时间】:2020-03-07 23:39:56
【问题描述】:

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)
    }

【问题讨论】:

    标签: json-rpc pow consensus substrate


    【解决方案1】:

    我建议你遵循类似于Kulupu 的模式来创建 PoW Substrate 区块链。

    在 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
    

    【讨论】:

    • 是的,太棒了,但我不希望在运行时或节点中进行挖掘过程。我希望矿工进行挖掘,然后通过 rpc 将挖掘信息发送到节点。你能分享一下你的想法吗?
    • PoW 模块目前不支持——我们确实需要 RPC 方法 getWorksubmitWork 以便人们可以实现矿池,但现在你必须实现这些你自己。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-18
    • 1970-01-01
    • 2021-02-16
    • 2018-04-27
    • 2019-12-09
    • 1970-01-01
    • 2017-01-04
    相关资源
    最近更新 更多