一.DAG的算法逻辑
假设有网络中有4个节点(A,B,C,D),每个节点都发送一笔交易,交易被包含在一个event里gossip到其他节点,一次gossip会把本节点的所知道的对方不知道的交易随机发送给其他节点,每个节点维护一个完整的图谱,通过投票算法,最后对每个event打一个时间戳,讲解具体逻辑前,我们先看一下event的数据结构:
type Event struct {
Transactions [][]byte //the payload
selfParent string
otherParent string
Creator []byte //creator's public key
Timestamp time.Time //creator's claimed timestamp of the event's creation
roundReceived *int
consensusTimestamp time.Time
}
Transactions字段是Event里包含的所有交易,selfParent和otherParent是该Event父Event的hash,包括了自己创建的父Event和其他节点创建的父Event,Creator是创建者的公钥,Timestamp是创建Event时的时间戳,roundRecevied是Event被第几层round里的famous witnesses 所共识的,consensusTimestamp是Event被共识时的时间戳。
接下来我们来看看具体的共识过程:
1.A,B,C,D在初始化时,各自创建一个rootEvent,然后B随机选择一个节点(假设选中的是D),然后B把自己所知道的D不知道的所有Events发送给D(这里只有B在一开始创建的rootEvent),D创建一个新的Event(该Event的selfParent为D的rootEvent,otherParent是B的rootEvent)