当前位置:网站首页>Near consensus mechanism
Near consensus mechanism
2022-06-29 01:30:00 【mutourend】
1. introduction
NEAR use PoS Consensus mechanism , Use the name NighShade The slice design of , Use the name DoomSlug Block generation mechanism .
2. Relevant definitions and symbols
To maintain consensus , The trade will be packaged into blocks . There is one. preconfigured block G G G, be called genesis block. except G G G outside , Every block There are a link pointing to the previous block p r e v ( B ) prev(B) prev(B), among B B B by block, Through the link Eventually connected to G G G( non-existent cycles).
- A < B A<B A<B: That means A ≠ B A\neq B A=B, And B B B It can be done by previous block link Connect to A A A.
- A ≤ B A\leq B A≤B: That means A < B A<B A<B or A = B A=B A=B.
- A ∼ B A\sim B A∼B: That means A < B A<B A<B or A = B A=B A=B or A > B A>B A>B.
- A ≁ B A\nsim B A≁B: That is, not A ∼ B A\sim B A∼B.
chain c h a i n ( T ) chain(T) chain(T): Refer to a set of blocks reachable from block T T T, be called tips, namely c h a i n ( T ) = { B ∣ B ≤ T } chain(T)=\{B|B\leq T\} chain(T)={ B∣B≤T}. For any block A , B A,B A,B, If and only if A ∼ B A\sim B A∼B when , A , B A,B A,B between a chain. here , Can become A , B A,B A,B On the same chain .
Each block has an integer height h ( B ) h(B) h(B). The block height is monotonically increasing , That is, for any block B ≠ G B\neq G B=G, Yes h ( B ) > h ( p r e v ( B ) ) h(B)>h(prev(B)) h(B)>h(prev(B)). However, the block height is not required to be continuous , Such as h ( G ) h(G) h(G) Not for 0. Every node keep track of a valid block with the largest height it knows about, which is called its head.
Blocks will be packed into epoch. On the chain , Belong to the same epoch A group of blocks forming a continuous range : If block A < B A<B A<B All belong to the same epoch, Then each one satisfies A < X < B A<X<B A<X<B Block of X X X All belong to this epoch.epoch can With sequential indices To mark : G G G Belong to index by 0 Of epoch; For each block B B B, It belongs to epoch index Or equal to p r e v ( B ) prev(B) prev(B) Of epoch index, Or greater than .
Every epoch relation a set of block producers that are validating blocks in that epoch, as well as an assignment of block heights to block producers that are responsible for producing a block at that height.
Responsible for production with a height of h h h block block producer be called block proposer at h h h.
epoch index i ≥ 2 i\geq 2 i≥2 Of validator set and block height assignment The information will be in epoch index by i − 2 i-2 i−2 The last block determine . about epoch index by 0 and 1 The situation of , Its validator set and block height assignment by preconfigured Of . therefore , if 2 The chain shares a epoch The last block of , Then its follow-up 2 individual epoch Have the same validator set And the same block height assignment, But for the later epoch There is no guarantee that .
curing : if block B B B Solidified , Then any future solidified block can only be based on B B B Built . therefore , stay B B B And previous block transactions will never be reverse. f i n a l ( B , T ) final(B,T) final(B,T), among B ≤ T B\leq T B≤T, signify B B B stay c h a i n ( T ) chain(T) chain(T) It's solidified . if f i n a l ( B , T ) final(B,T) final(B,T) It's true , Then for all T ′ ≥ T T'\geq T T′≥T, f i n a l ( B , T ′ ) final(B,T') final(B,T′) It's all true .
2. Data structures
NEAR The fields related to consensus in the block header are :
struct BlockHeader {
...
prev_hash: BlockHash,
height: BlockHeight,
epoch_id: EpochId,
last_final_block_hash: BlockHash,
approvals: Vec<Option<Signature>>
...
}
In specific epoch Of block producers Will exchange many types of messages , The news related to consensus is 2 Kind of :
- Blocks
- Approvals
The following fields are included :
enum ApprovalInner {
Endorsement(BlockHash),// by the hash of the approved block
Skip(BlockHeight),// by the height of the approved block
}
struct Approval {
inner: ApprovalInner,
target_height: BlockHeight,// by the specific height at which the approval can be used (an approval with a particular `target_height` can be only included in the `approvals` of a block that has `height = target_height`)
signature: Signature,// To be right `(inner, target_height)` The signature of the
account_id: AccountId // by the account of the block producer who created the approval
}
3. Approvals Requirements
except genesis block Every block outside B B B in , Both must logically contain information from block producers Of approvals, these block producers The accumulation of stake Must exceed this epoch President Zhong stake Of 2/3. stay epoch Switch when , requirement block producers The accumulation of stake Need to go beyond the next epoch total stake Of 2/3.
approval Yes 2 Types :
- If and only if h ( B ) = h ( p r e v ( B ) ) + 1 h(B)=h(prev(B))+1 h(B)=h(prev(B))+1 when , by
Endorsement. - Otherwise
Skipwith the height of p r e v ( B ) prev(B) prev(B).
Each... Stored in the block approval Logically for each block producer It's all the same ( except account_id and signature Different ), therefore , Store all approvals It's redundant . In fact, the physical values are stored signatures of the approvals, The specific storage method is :
- Get current epoch Of block producers ordered set.
If a block stay epoch The border , It also needs to include approvals form the next epoch, Therefore need add new accounts from the new epoch:
def get_accounts_for_block_ordered(h, prev_block):
cur_epoch = get_next_block_epoch(prev_block)
next_epoch = get_next_block_next_epoch(prev_block)
account_ids = get_epoch_block_producers_ordered(cur_epoch)
if next_block_needs_approvals_from_next_epoch(prev_block):
for account_id in get_epoch_block_producers_ordered(next_epoch):
if account_id not in account_ids:
account_ids.append(account_id)
return account_ids
therefore , This boundary block It contains a vector of optional signatures of the same or smaller size than the resulting set of account_ids, with each element being Noneif the approval for such account is absent, or the signature on the approval message if it is present. therefore , It is easy to reconstruct the signature from the information in the block approvals Of block producers, And verify the corresponding signature . if the vector of signature Shorter than account_ids The length of , Then the rest signatures Are assumed to be None.
4. Messages
When I received approval news , Participants deposit only the collection of approval messages in :
def on_approval(self, approval):
self.approvals.append(approval)
When participants receive a block, The operations related to consensus are :
- to update
head - initialization a timer To start sending the block Of approvals to the block producers at the consecutive
target_heights. The timer timeout depends on the height of the last solidified block , This information will also persisted.
def on_block(self, block):
header = block.header
if header.height <= self.head_height:
return
last_final_block = store.get_block(header.last_final_block_hash)
self.head_height = header.height
self.head_hash = block.hash()
self.largest_final_height = last_final_block.height
self.timer_height = self.head_height + 1
self.timer_started = time.time()
self.endorsement_pending = True
The timer will periodically check the following logic :
def get_delay(n):
min(MAX_DELAY, MIN_DELAY + DELAY_STEP * (n-2))
def process_timer(self):
now = time.time()
skip_delay = get_delay(self.timer_height - self.largest_final_height)
if self.endorsement_pending and now > self.timer_started + ENDORSEMENT_DELAY:
if self.head_height >= self.largest_target_height:
self.largest_target_height = self.head_height + 1
self.send_approval(head_height + 1)
self.endorsement_pending = False
if now > self.timer_started + skip_delay:
assert not self.endorsement_pending
self.largest_target_height = max(self.largest_target_height, self.timer_height + 1)
self.send_approval(self.timer_height + 1)
self.timer_started = now
self.timer_height += 1
def send_approval(self, target_height):
if target_height == self.head_height + 1:
inner = Endorsement(self.head_hash)
else:
inner = Skip(self.head_height)
approval = Approval(inner, target_height)
send(approval, to_whom = get_block_proposer(self.head_hash, target_height))
among get_block_proposer Return to the next block proposer.
Simultaneous requirements ENDORSEMENT_DELAY < MIN_DELAY, Although it does not affect the correctness ,NEAR In the request ENDORSEMENT_DELAY * 2 <= MIN_DELAY.
5. Block Production
It defines how to get a certain height in a block approvals Function of :
def get_approvals(self, target_height):
return [approval for approval
in self.approvals
if approval.target_height == target_height and
(isinstance(approval.inner, Skip) and approval.prev_height == self.head_height or
isinstance(approval.inner, Endorsement) and approval.prev_hash == self.head_hash)]
as long as get_approvals Back to approvals Corresponding block producers The accumulated pledge amount of exceeds% of the total pledge amount 2/3, The block producer assigned this height can produce a block .
6. Curing conditions
block B B B Solidify in c h a i n ( T ) chain(T) chain(T), among T ≥ B T\geq B T≥B, or B = G B=G B=G, or block X ≤ T X\leq T X≤T bring B = p r e v ( p r e v ( X ) ) And h ( X ) = h ( p r e v ( X ) ) + 1 = h ( B ) + 2 B=prev(prev(X)) And h(X)=h(prev(X))+1=h(B)+2 B=prev(prev(X)) And h(X)=h(prev(X))+1=h(B)+2. namely , or B B Bwei genesis block, or c h a i n ( T ) chain(T) chain(T) Include at least 2 individual blocks on top of B B B, And this 3 Block ( Contains blocks B B B Itself and its successors 2 Block ) With continuous block height .
7. epoch Switch
e p o c h l e n g t h ≥ 3 epoch_{length}\geq 3 epochlength≥3 Defined a epoch The minimum length of . Assuming a epoch e c u r e_{cur} ecur Start at height h h h, next epoch by e n e x t e_{next} enext.
B P ( e ) BP(e) BP(e) by epoch e e e A series of producers in .
l a s t f i n a l ( T ) last_{final(T)} lastfinal(T) by the highest final block in c h a i n ( T ) chain(T) chain(T).
Then the producer produces the approvals The rule is :
- 1) For a height of h ( p r e v ( B ) ) < h + e p o c h l e n g t h − 3 h(prev(B))< h+epoch_{length}-3 h(prev(B))<h+epochlength−3 Block of B B B Belong to epoch e c u r e_{cur} ecur, There must be more than 2/3 Of approvals of B P ( e c u r ) BP(e_{cur}) BP(ecur)(stake-weighted).
- 2) For a height of h ( p r e v ( B ) ) ≥ h + e p o c h l e n g t h − 3 h(prev(B))\geq h+epoch_{length}-3 h(prev(B))≥h+epochlength−3 And h ( l a s t f i n a l ( p r e v ( B ) ) ) < h + e p o c h l e n g t h − 3 h(last_{final(prev(B))})<h+epoch_{length}-3 h(lastfinal(prev(B)))<h+epochlength−3 Block of B B B Belong to epoch e c u r e_{cur} ecur, Must have more than at the same time 2/3 Of approvals of B P ( e c u r ) BP(e_{cur}) BP(ecur)(stake-weighted) as well as exceed 2/3 Of approvals of B P ( e n e x t ) BP(e_{next}) BP(enext)(stake-weighted).
- 3) For the first height h ( l a s t f i n a l ( p r e v ( B ) ) ) ≥ h + e p o c h l e n g t h − 3 h(last_{final(prev(B))})\geq h+epoch_{length}-3 h(lastfinal(prev(B)))≥h+epochlength−3 block B B B Belong to epoch e n e x t e_{next} enext, There must be more than 2/3 Of approvals of B P ( e n e x t ) BP(e_{next}) BP(enext)(stake-weighted).
8. Safety
honest block producer:
- Can never be the same
prev_heightGenerate 2 individual endorsement( That is, conflicting endorsements) - Can never generate a skip message
sand an endorsement e e e, brings.prev_height < e.prev_height and s.target_height >= e.target_height( That is, conflicting skip and endorsement)
There is a theorem :
Inferential :

9. Liveness
Specifically proof of liveness see NEAR The team 2020 Year paper 《Doomslug: block confirmation with single round of communication, and a finality gadget with guaranteed liveness》. The consensus here differs in that it requires 2 Consecutive have endorsements Block of . And in the paper proof It has been extended , The place of , once the delay is sufficiently long for a honest block producer to collect enough endorsements, the next block producer ought to have enough time to collect all the endorsements too.
10. Approval condition
approval condition by :
- Within any valid block , Must contain approvals from block producers, The accumulated pledge amount exceeds this amount epoch Of the total pledge amount 2/3. For blocks B B B And the previous block B ′ B' B′, If and only if
B.height == B'.height + 1when , B B B Each of the approval It has to be for anEndorsementwith the hash of B ′ B' B′, Otherwise, it must be aSkipwith the height of B ′ B' B′.
this 2 Two conditions cannot be combined . about approval Medium endorsement, Its prev_hash Must be equal to the hash of the previous block, Otherwise, No 7 Chaste safety proof Will not pass .
about skip messages Come on , Does not require approval Medium hash matching the hash of the previous block, Because if there is malicious actor Can be created at the same height 2 Block , And distribute them to half of the block producers. Each half of block producers Will be given to the same prev_height Sending has different prev_hash Of skip messages To the future producers . If... Is required at this time skip Medium prev_hash Must strictly match the prev_hash, Then no block producer can create a new block .
Reference material
[1] NEAR Consensus mechanism
[2] What is NEAR protocol?
[3] Doomslug vs PBFT, Tendermint, and Hotstuff
[4] NEAR The team 2020 Year paper 《Doomslug: block confirmation with single round of communication, and a finality gadget with guaranteed liveness》
边栏推荐
- Battle drag method 1: moderately optimistic and build self-confidence (2)
- Introduction to UE gameplay 44 (animation import FBX and production standard)
- [eight part essay] MySQL
- EasyCVR服务private.pem文件被清空,导致无法正常启动该如何处理?
- [Fire Detection] forest fire detection system based on Matlab GUI (with panel) [including Matlab source code phase 1921]
- Qt est basé sur le système de gestion RFID (peut être appliqué à la plupart des systèmes de gestion RFID)
- [SV basics] some usage of queue
- [Architect (Part 38)] locally install the latest version of MySQL database developed by the server
- [solution] longest common subsequence
- Niuke.com Huawei question bank (41~50)
猜你喜欢

Advanced installer architect authoring tool

【图像增强】基于matlab人工多重曝光融合AMEF图像去雾【含Matlab源码 1916期】

PAT甲级真题1165

How can multidimensional analysis pre summary work?

ASP. Net based on LAN

To the interface problems we have encountered

机构加密资产产品上周流出4.23亿美元资金,创历史新高

Day 8 script and audio

Design and development of VB mine sweeping game

分享自己平时使用的socket多客户端通信的代码技术点和软件使用
随机推荐
NOIP2006-2018 提高组 初赛试题完善程序题 CSP-S 2019 2020 初赛试题完善程序题
After easycvr creates a new user, the video access page cannot be clicked. Fix the problem
EasyCVR接入Ehome协议的设备,无法观看设备录像是什么原因?
Learning notes of Lichuang EDA: Copper laying dead zone? isolated island? Dead copper?
大厂裁员潮下,测试人员路在何方?
Pat grade a real problem 1165
测试只能干到35岁?35岁+的测试就会失业?
Teach you how to understand the test environment project deployment
What is the difference between the histology and western blotting 两种方法都是进行组织染色的
有了这款工具,自动化识别验证码再也不是问题
IPFS简述
Introduction to UE gameplay 44 (animation import FBX and production standard)
Seven mistakes in IT Governance and how to avoid them
Flask-SQLAlchemy的基本使用
Vulnerability mining | routine in password retrieval
[solution] longest common subsequence
mysql数据库修改密码
多维分析预汇总应该怎样做才管用?
免疫组化和免疫组学之间的区别是啥?
QT基于RFID管理系统(可应用于大多数RFID管理系统)