这是一个看起来复杂,实际也真复杂的模块。违法的,直接拒绝;违规的,进行处罚。

但我们可以用排除法,先将如下和数方法进行筛选:

数据查询、检查判断、逻辑判断、可重复操作,剩下各个方法才是我们的重点关注对象。

Blockchain_Start

启动软件

/* do initialisation , setup storage, put genesis block and chain in store
   This is the first component to get up
   Global parameters are picked up  from the config package
*/
checkpoints.LoadCheckPoints
Bolt_backend
Mempool
Generate_Genesis_Block
Store_BL/Store_Block_Topological_order/Store_TOPO_HEIGHT/Store_TOP_HEIGHT/store_TIPS
Initialise_Chain_From_DB
init_hard_forks
clean_up_valid_cache

Initialise_Chain_From_DB

从数据库初始化“链”数据

// this function is called to read blockchain state from DB
// It is callable at any point in time
Load_TOP_HEIGHT
load_TIPS

Shutdown

停止软件的运行

// before shutdown , make sure p2p is confirmed stopped
为了解耦,这里不直接操作 p2p
但直接操作 Mempool 和 store

Get_Height

// get top unstable height
// this is obtained by  getting the highest topo block and getting its height

先从系统得到 topo 高度,然后根据它找到区块,再根据区块得到其高度。

Get_Stable_Height

// get height where chain is now stable
找到指定的几个 TIP (当前未端区块)的共同祖先。
封装了 find_common_base

Get_TIPS

从数据库获取当前几个末端区块

Get_Top_ID

Get_Difficulty

Get_Cumulative_Difficulty

伪实现,始终返回 0

Get_Median_Block_Size

Get_Network_HashRate

Block_Exists

指定哈希,区块是否存在数据库?

// confirm whether the block exist in the data
// this only confirms whether the block has been downloaded
// a separate check is required, whether the block is valid ( satifies PoW and other conditions)
// we will not add a block to store, until it satisfies PoW

sort_descending_by_cumulative_difficulty

按累积难度降序排序,如果难度相同,则按区块ID排序。
它是下面 SortTips 的核心

// Heighest node weight is ordered first,  the condition is reverted see eg. at https://golang.org/pkg/sort/#Slice
//  if weights are equal, nodes are sorted by their block ids which will never collide , hopefullly
// block ids are sorted by lowest byte first diff

sort_ascending_by_height
SortTips

按"累积挖矿难度"进行排!

和所谓的“最重子树”有异曲同工之妙。

// this will sort the tips based on cumulative difficulty and/or block ids
// the tips will sorted in descending order

Load_Block_Cumulative_Difficulty
sort_descending_by_cumulative_difficulty

封装上面的按累积难度排序

isblock_SideBlock

主要根据高度,判断指定区块是不是副块。

既是程序需要,又是激励机制的一部分。

后续会影响奖励,之前是 67%,现在是 8%
(特有解决方案)

// side blocks are blocks which lost the race the to become part
// of main chain, but there transactions are honoured,
// they are given 67 % reward
// a block is a side block if it satisfies the following condition
// if  block height   is less than or equal to height of past 8 topographical blocks
// this is part of consensus rule
// this is the topoheight of this block itself

根据当前区块哈希,得到当前区块,得到其高度(记为 block_height)
根据当前链拓扑高度,逆序获取最后一个区块,得到其高度(记为 previous_height)
如果 block_height <= previous_height,则当前区块为“副块”。

因为,如果它是“主块”的话,它的高度理应比当前的高。

client_protocol

交易费用激励机制,重复交易、冲突交易不给手续费。

运行存储于交易上的智能合约代码。

主要作用是计算手续费!
重复交易不提供手续费。
还有就是运行合约代码。
(特有解决方案)

// runs the client protocol which includes the following operations
// if any TX are being duplicate or double-spend ignore them
// mark all the valid transactions as valid
// mark all invalid transactions  as invalid
// calculate total fees based on valid TX
// we need NOT check ranges/ring signatures here, as they have been done already by earlier steps

client_protocol_reverse

// this undoes everything that is done by client protocol
// NOTE: this will have any effect, only if client protocol has been run on this block earlier
revoke_keyimages
Store_TX_Height
mark_TX

只要还没有最终确定,操作是可以撤消、还原的,这也是协议的一部分,也是“共识”的一部分。

transaction_scavenger

// scavanger for transactions from rusty/stale tips to reinsert them into pool
做了太久的孤儿
主要是根据时间限制,如果区块老旧,应当把其打包的交易重新放入交易池

Is_Block_Orphan

通过计算得到,但不做标记(存储结果)。

// Finds whether a  block is orphan
// since we donot store any fields, we need to calculate/find the block as orphan
// using an algorithm
// if the block is NOT topo ordered , it is orphan/stale

Is_TX_Orphan

// this is used to find if a tx is orphan, YES orphan TX
// these can occur during  when they are detect to be double-spended on
// so the TX becomes orphan ( chances are less may be less that .000001 % but they are there)
// if a tx is not valid in any of the blocks, it has been mined it is orphan

BlockCheckSum

// this is used to for quick syncs as entire blocks as SHA1,
// entires block can skipped for verification, if checksum matches what the devs have stored

mark_keyimages

// this function will mark all the key images present in the tx as requested
// this is done so as they cannot be respent
// mark is int64  height

consume_keyimages

//this will mark all the keyimages present in this TX as spent
//this is done so as an input cannot be spent twice

标记“密钥镜像”已被使用(单独存储)

revoke_keyimages

//this will mark all the keyimages present in this TX as unspent
//this is required during  client protocol runs

Get_TX

/* this will only give you access to transactions which have been mined */

从数据库加载已被打包的区块

Calculate_Height_At_Tips

根据几个 Tip 获得高度(取最大值)
(特有解决方案)

// This will get the biggest height of tip for hardfork version and other calculations
// get biggest height of parent, add 1

IsLagging

时钟,我们是否滞后?

// verifies whether we are lagging
// return true if we need resync
// returns false if we are good and resync is not required

Expand_Transaction_v2

对交易补充数据,数据是重复的,非必须的。

数据:指的密钥镜像、环成员数据,在其它地方已经有体现。

作用:更全面的安全考虑,以及后续使用方便。

补充 RctSignature.MlsagSigs.II 和 RctSignature.MixRing
并保证通过验证
(特有解决方案)

// This function will expand a transaction with all the missing info being reconstitued from the blockchain
// this also increases security since data is coming from the chain or being calculated
// basically this places data for ring signature verification
// REMEMBER to expand key images from the blockchain

Block_Count_Vout

统计该区块所有“输出”的总数

// this function count all the vouts of the block,
// this function exists here because  only the chain knows the tx

Rewind_Chain

// this function will rewind the chain from the topo height one block at a time
// this function also runs the client protocol in reverse and also deletes the block from the storage
由客户端操作
重新组织“链”的拓扑结构
自己扩展,不上链,自己可修改

我们向其它节点发送请求,尝试同步数据。
结果发现我们反而领先了?!
这种情况下,先进行自我检查,并“尝试回滚本地数据”。有则改之,无则加勉。
然后重新发送请求,等待响应。

buildReachability_internal

// build reachability graph upto 2*config deeps to answer reachability queries

buildReachability

// build reachability graph upto 2*limit  deeps to answer reachability queries

VerifyNonReachability

// this is part of consensus rule, 2 tips cannot refer to their common parent

不能恶意引用末端区块,会被直接拒绝。

BuildReachableBlocks

// used in the difficulty calculation for consensus and while scavenging

BuildReachabilityKeyImages

// this is part of consensus rule, reachable blocks cannot have keyimages collision with new blocks
// this is to avoid dishonest miners including dead transactions

IsBlockSyncBlockHeight

// sync blocks have the following specific property
// 1) the block is singleton at this height
// basically the condition allow us to confirm weight of future blocks with reference to sync blocks
// these are the one who settle the chain and guarantee it

IsBlockSyncBlockHeightSpecific

FindTipBase

// base of a tip is last known sync point
// weight of bases in mentioned in term of height
// this must not employ any cache

FindTipWorkScore_internal

// this will find the sum of  work done ( skipping any repetive nodes )
// all the information is privided in unique_map

FindTipWorkScore

// find the score of the tip  in reference to  a base (NOTE: base is always a sync block otherwise results will be wrong )

find_common_base

找到几个 Tip 共有的祖先,也就是其分歧点
(特有解决该当,涉及到算法)

// this function finds a common base  which can be used to compare tips
// weight is replace by height

find_best_tip

核心是“累积挖矿难度”!(注意,叔块对应的难度已经考虑进来了)

GHOST,贪婪最重可见子树协议,这是其一种实现方式。

理论上:大算力,会影响主链的选择(确定主链)。这也符合直觉。

找到最佳 Tip
最佳 Tip 可做为父块,其余 Tip 可做为叔块
(特有解决方案,涉及到算法)

// this function finds a common base  which can be used to compare tips based on cumulative difficulty

calculate_mainchain_distance_internal_recursive

按累积难度降序排序
(特有解决方案,涉及算法)

// Heighest node weight is ordered first,  the condition is reverted see eg. at https://golang.org/pkg/sort/#Slice
//  if weights are equal, nodes are sorted by their block ids which will never collide , hopefullly
// block ids are sorted by lowest byte first diff

calculate_mainchain_distance

Generate_Full_Order

BlockDAG 的全排序!
先获得几个 tips 的后后一个共同祖先,然后找出当前最优解。
BlockDAG 核心技术之一。
(特有解决方案)

// converts a DAG's partial order into a full order, this function is recursive
// generate full order should be only callled on the basis of base blocks which satisfy sync block properties as follows
// generate full order is called on maximum weight tip at every tip change
// blocks are ordered recursively, till we find a find a block  which is already in the chain

Load_Block_Topological_order

获得区块在全排序里的排序序号!
先获得几个 tips 的后后一个共同祖先,然后找出当前最优解。

WriteBlockChainTree

其它:

除了 blockchain.go 文件,其它文件下,也有很多 chain *Blockchain 实例方法。

PoW 应用最长链规则(Longest Chain Rule)或者最重链规则(GHOST)选择出有效的区块链。

results matching ""

    No results matching ""