您现在的位置:kastop>> Kas信息 Kaspa网络>>正文内容

难度调整与时间戳的关系

我本以为DAA窗口不依赖于时间戳,而是依赖于daa分数或蓝功或dag拓扑结构固有的东西(而不是依赖于矿工输入的时间戳)?

its simple: https://github.com/kaspanet/rusty-kaspa/blob/master/consensus/src/processes/difficulty.rs#L131

基本上,您获取最后n个块的时间戳,并使用它们的时间戳来查看我们是否必须调整难度以匹配块时间。采样的有点复杂。


难度代码:


    pub fn calculate_difficulty_bits(&self, window: &BlockWindowHeap) -> u32 {

        let mut difficulty_blocks = self.get_difficulty_blocks(window);


        // Until there are enough blocks for a valid calculation the difficulty should remain constant.

        if difficulty_blocks.len() < self.min_difficulty_window_len {

            return self.genesis_bits;

        }


        let (min_ts_index, max_ts_index) = difficulty_blocks.iter().position_minmax().into_option().unwrap();


        let min_ts = difficulty_blocks[min_ts_index].timestamp;

        let max_ts = difficulty_blocks[max_ts_index].timestamp;


        // We remove the minimal block because we want the average target for the internal window.

        difficulty_blocks.swap_remove(min_ts_index);


        // We need Uint320 to avoid overflow when summing and multiplying by the window size.

        let difficulty_blocks_len = difficulty_blocks.len() as u64;

        let targets_sum: Uint320 =

            difficulty_blocks.into_iter().map(|diff_block| Uint320::from(Uint256::from_compact_target_bits(diff_block.bits))).sum();

        let average_target = targets_sum / (difficulty_blocks_len);

        let new_target = average_target * max(max_ts - min_ts, 1) / (self.target_time_per_block * difficulty_blocks_len);

        Uint256::try_from(new_target.min(self.max_difficulty_target)).expect("max target < Uint256::MAX").compact_target_bits()

    }



感动 同情 无聊 愤怒 搞笑 难过 高兴 路过
【字体: 】【收藏】【打印文章】 【 打赏 】 【查看评论

相关文章

    没有相关内容