Burrow 藉助 Tendermint 管理 p2p 網絡和區塊共識,在 Tendermint 之外自己維護了一個 StateDB 存儲 Burrow 中各種交易的狀態數據。
1. 存儲結構
Burrow 在 KV DB
之上通過前綴的形式劃分出了多個用途的空間,主要有三大類
1.1 Plain
這一類簡單直接,直接操作 DB 存儲 KV
。
包含兩種數據:
-
Tx 存儲的存儲位置
存儲 Tx 的區塊高度和在Event Tree
中的區塊數據中的偏移
前綴 + Tx Hash 爲key,存儲區塊高度和偏移值hth
+<tx_hash>
->
{height, offset}
type TxExecutionKey struct { // The block height Height uint64 `protobuf:"varint,1,opt,name=Height,proto3" json:"Height,omitempty"` // The offset of the TxExecution in bytes Offset uint64 `protobuf:"varint,2,opt,name=Offset,proto3" json:"Offset,omitempty"` }
-
智能合約的元數據
存儲智能合約相關 abi 的元數據,與存放在Account Tree
下的智能合約賬號中ContractMeta
通過codehash
和metahash
進行關聯habi
+<metahash>
->
<abi meta>
1.2 Tree
這一類是大部分狀態數據的存儲形式,Burrow 使用 IAVL+ Tree 來組織各前綴空間下的數據。
iavl
是一個平衡二叉樹實現,只在葉子節點存放數據
存儲結構:
-
版本對應的
root hash
ft<type>r
+version
->
<root hash>
-
hash
對應的Node
ft<type>n
+hash
->
{Node}
// Node represents a node in a Tree. type Node struct { key []byte value []byte hash []byte leftHash []byte rightHash []byte version int64 size int64 leftNode *Node rightNode *Node height int8 saved bool // saved to memory or disk persisted bool // persisted to disk }
<type>
爲各種類別的前綴
主要有以下的狀態樹:
類別 | 前綴 | 描述 | Node Key | Node Value |
---|---|---|---|---|
Account |
a |
存儲賬號狀態,包含智能合約賬號 | address | Account |
Storage |
s |
智能合約中字段的狀態數據 | address + Field Key | Field Value |
Name |
n |
NameReg提供一個基於名稱,數據對的全局鍵值存儲,這些數據對受帳戶的到期和所有權約束 | name | Entry |
Proposal |
p |
存儲 ProposalTx 信息 | proposalHash | Ballot |
Validator |
v |
存儲參與 Tendermint Voting 的節點 Power | address | power |
Event |
e |
存儲區塊執行的狀態信息 | Height | []StreamEvent |
Registry |
r |
存儲 Validator 節點信息 | address | NodeIdentity |
1.3 Commit
Commit 類狀態與 1.2 一樣也使用 IAVL+ Tree
來組織數據,不同的是,Commit 記錄的是 1.2 中所有 Tree
的保存狀態也就是賬本的 Commit 狀態
存儲結構:
-
版本對應的
root hash
fcr
+version
->
<root hash>
-
hash
對應的Node
fcn
+hash
->
{Node}
node key:<type prefix>
node value: CommitID
// This is the object that is stored in the leaves of the commitsTree - it captures the sub-tree hashes so that the
// commitsTree's hash becomes a mixture of the hashes of all the sub-trees.
type CommitID struct {
Version int64 `protobuf:"varint,1,opt,name=Version,proto3" json:"Version,omitempty"`
Hash []byte `protobuf:"bytes,2,opt,name=Hash,proto3" json:"Hash,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
}
2. 代碼結構
Burrow 抽出 Forest
來管理各種狀態樹(包括Commit),State
封裝 Forest
對外提供狀態查詢、更新功能