This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
simpleblock.go
239 lines (212 loc) · 7.16 KB
/
simpleblock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package lazyledger
import (
"bytes"
"crypto/sha256"
"gitlab.com/NebulousLabs/merkletree"
)
// SimpleBlock represents a block designed for the Simple Validity Rule.
type SimpleBlock struct {
prevHash []byte
messages []Message
messagesRoot []byte
provenDependencies map[string]bool
}
// NewSimpleBlock returns a new simple block.
func NewSimpleBlock(prevHash []byte) Block {
return &SimpleBlock{
prevHash: prevHash,
provenDependencies: make(map[string]bool),
}
}
// ImportSimpleBlockHeader imports a received simple block without the messages.
func ImportSimpleBlockHeader(prevHash []byte, messagesRoot []byte) Block {
return &SimpleBlock{
prevHash: prevHash,
messagesRoot: messagesRoot,
provenDependencies: make(map[string]bool),
}
}
// ImportSimpleBlock imports a received simple block.
func ImportSimpleBlock(prevHash []byte, messages []Message) Block {
return &SimpleBlock{
prevHash: prevHash,
messages: messages,
provenDependencies: make(map[string]bool),
}
}
// AddMessage adds a message to the block.
func (sb *SimpleBlock) AddMessage(message Message) {
sb.messages = append(sb.messages, message)
// Force recompututation of messagesRoot
sb.messagesRoot = nil
}
// MessagesRoot returns the Merkle root of the messages in the block.
func (sb *SimpleBlock) MessagesRoot() []byte {
if sb.messagesRoot == nil {
ndf := NewNamespaceDummyFlagger()
fh := NewFlagHasher(ndf, sha256.New())
tree := merkletree.New(fh)
for _, message := range sb.messages {
tree.Push(message.Marshal())
}
sb.messagesRoot = tree.Root()
}
return sb.messagesRoot
}
// Digest computes the hash of the block.
func (sb *SimpleBlock) Digest() []byte {
hasher := sha256.New()
hasher.Write(sb.prevHash)
hasher.Write(sb.MessagesRoot())
return hasher.Sum(nil)
}
// Valid returns true if the block is valid.
func (sb *SimpleBlock) Valid() bool {
if sb.messages == nil {
// Cannot validate block without messages.
return false
}
ndf := NewNamespaceDummyFlagger()
fh := NewFlagHasher(ndf, sha256.New())
tree := merkletree.New(fh)
for _, message := range sb.messages {
tree.Push(message.Marshal())
}
if bytes.Compare(tree.Root(), sb.MessagesRoot()) == 0 {
return true
}
return false
}
// PrevHash returns the hash of the previous block.
func (sb *SimpleBlock) PrevHash() []byte {
return sb.prevHash
}
// Messages returns the block's messages.
func (sb *SimpleBlock) Messages() []Message {
return sb.messages
}
// ApplicationProof creates a Merkle proof for all of the messages in a block for an application namespace.
func (sb *SimpleBlock) ApplicationProof(namespace [namespaceSize]byte) (int, int, [][]byte, *[]Message, [][]byte) {
var proofStart int
var proofEnd int
var found bool
for index, message := range sb.messages {
if message.Namespace() == namespace {
if !found {
found = true
proofStart = index
}
proofEnd = index + 1
}
}
var inRange bool
if !found {
var prevMessage Message
// We need to generate a proof for an absence of relevant messages.
for index, message := range sb.messages {
if index != 0 {
prevNs := prevMessage.Namespace()
currentNs := message.Namespace()
if ((bytes.Compare(prevNs[:], namespace[:]) < 0 && bytes.Compare(namespace[:], currentNs[:]) < 0) ||
(bytes.Compare(prevNs[:], namespace[:]) > 0 && bytes.Compare(namespace[:], currentNs[:]) > 0)) {
if !inRange {
inRange = true
proofStart = index
}
proofEnd = index + 1
}
}
prevMessage = message
}
}
ndf := NewNamespaceDummyFlagger()
fh := NewFlagHasher(ndf, sha256.New())
var proof [][]byte
if found || inRange {
proof, _ = merkletree.BuildRangeProof(proofStart, proofEnd, NewMessageSubtreeHasher(&sb.messages, fh))
}
proofMessages := sb.messages[proofStart:proofEnd]
if found {
return proofStart, proofEnd, proof, &proofMessages, nil
}
var hashes [][]byte
for _, message := range proofMessages {
ndf := NewNamespaceDummyFlagger()
fh := NewFlagHasher(ndf, sha256.New())
hashes = append(hashes, leafSum(fh, message.Marshal()))
fh.Reset()
}
return proofStart, proofEnd, proof, nil, hashes
}
// VerifyApplicationProof verifies a Merkle proof for all of the messages in a block for an application namespace.
func (sb *SimpleBlock) VerifyApplicationProof(namespace [namespaceSize]byte, proofStart int, proofEnd int, proof [][]byte, messages *[]Message, hashes [][]byte) bool {
// Verify Merkle proof
ndf := NewNamespaceDummyFlagger()
fh := NewFlagHasher(ndf, sha256.New())
var lh merkletree.LeafHasher
if messages != nil {
lh = NewMessageLeafHasher(messages, fh)
} else {
lh = NewHashLeafHasher(hashes)
}
result, err := merkletree.VerifyRangeProof(lh, fh, proofStart, proofEnd, proof, sb.MessagesRoot())
if !result || err != nil {
return false
}
// Verify proof completeness
var leafIndex uint64
var leftSubtrees [][]byte
var rightSubtrees [][]byte
consumeUntil := func(end uint64) error {
for leafIndex != end && len(proof) > 0 {
subtreeSize := nextSubtreeSize(leafIndex, end)
leftSubtrees = append(leftSubtrees, proof[0])
proof = proof[1:]
leafIndex += uint64(subtreeSize)
}
return nil
}
if err := consumeUntil(uint64(proofStart)); err != nil {
return false
}
rightSubtrees = proof
for _, subtree := range leftSubtrees {
_, max := dummyNamespacesFromFlag(subtree)
if bytes.Compare(max, namespace[:]) >= 0 {
return false
}
}
for _, subtree := range rightSubtrees {
min, _ := dummyNamespacesFromFlag(subtree)
if bytes.Compare(min, namespace[:]) <= 0 {
return false
}
}
return true
}
func (sb *SimpleBlock) ProveDependency(index int) ([]byte, [][]byte, error) {
ndf := NewNamespaceDummyFlagger()
fh := NewFlagHasher(ndf, sha256.New())
proof, err := merkletree.BuildRangeProof(index, index + 1, NewMessageSubtreeHasher(&sb.messages, fh))
if err != nil {
return nil, nil, err
}
return leafSum(fh, sb.messages[index].Marshal()), proof, nil
}
func (sb *SimpleBlock) VerifyDependency(index int, hash []byte, proof [][]byte) bool {
ndf := NewNamespaceDummyFlagger()
fh := NewFlagHasher(ndf, sha256.New())
lh := NewHashLeafHasher([][]byte{hash})
result, err := merkletree.VerifyRangeProof(lh, fh, index, index + 1, proof, sb.MessagesRoot())
if result && err == nil {
sb.provenDependencies[string(hash)] = true
return true
}
return false
}
func (sb *SimpleBlock) DependencyProven(hash []byte) bool {
if value, ok := sb.provenDependencies[string(hash)]; ok {
return value
}
return false
}