/
coordinator.go
383 lines (307 loc) · 12.5 KB
/
coordinator.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package coordinator
import (
"crypto"
"fmt"
"os"
"strings"
"time"
_ "golang.org/x/crypto/blake2b" // import implementation
"github.com/pkg/errors"
"github.com/iotaledger/hive.go/events"
"github.com/iotaledger/hive.go/syncutils"
"github.com/iotaledger/iota.go/consts"
"github.com/iotaledger/iota.go/merkle"
"github.com/iotaledger/iota.go/pow"
"github.com/iotaledger/iota.go/transaction"
"github.com/iotaledger/iota.go/trinary"
"github.com/gohornet/hornet/pkg/model/hornet"
"github.com/gohornet/hornet/pkg/model/milestone"
"github.com/gohornet/hornet/pkg/model/tangle"
"github.com/gohornet/hornet/pkg/whiteflag"
)
// Bundle represents grouped together transactions forming a transfer.
type Bundle = []*transaction.Transaction
// SendBundleFunc is a function which sends a bundle to the network.
type SendBundleFunc = func(b Bundle) error
// CheckpointTipSelectionFunc is a function which performs a tipselection and returns several tips for a checkpoint.
type CheckpointTipSelectionFunc = func(minRequiredTips int) (hornet.Hashes, error)
var (
// ErrNetworkBootstrapped is returned when the flag for bootstrap network was given, but a state file already exists.
ErrNetworkBootstrapped = errors.New("network already bootstrapped")
)
// CoordinatorEvents are the events issued by the coordinator.
type CoordinatorEvents struct {
// Fired when a checkpoint transaction is issued.
IssuedCheckpointTransaction *events.Event
// Fired when a milestone is issued.
IssuedMilestone *events.Event
}
// Coordinator is used to issue signed transactions, called "milestones" to secure an IOTA network and prevent double spends.
type Coordinator struct {
milestoneLock syncutils.Mutex
// config options
seed trinary.Hash
securityLvl consts.SecurityLevel
merkleTreeDepth int
minWeightMagnitude int
stateFilePath string
milestoneIntervalSec int
powFunc pow.ProofOfWorkFunc
checkpointTipselFunc CheckpointTipSelectionFunc
sendBundleFunc SendBundleFunc
milestoneMerkleHashFunc crypto.Hash
// internal state
state *State
merkleTree *merkle.MerkleTree
lastCheckpointIndex int
lastCheckpointHash hornet.Hash
lastMilestoneHash hornet.Hash
bootstrapped bool
// events of the coordinator
Events *CoordinatorEvents
}
// MilestoneMerkleTreeHashFuncWithName maps the passed name to one of the supported crypto.Hash hashing functions.
// Also verifies that the available function is available or else panics.
func MilestoneMerkleTreeHashFuncWithName(name string) crypto.Hash {
//TODO: golang 1.15 will include a String() method to get the string from the crypto.Hash, so we could iterate over them instead
var hashFunc crypto.Hash
switch strings.ToLower(name) {
case "blake2b-512":
hashFunc = crypto.BLAKE2b_512
case "blake2b-384":
hashFunc = crypto.BLAKE2b_384
case "blake2b-256":
hashFunc = crypto.BLAKE2b_256
case "blake2s-256":
hashFunc = crypto.BLAKE2s_256
default:
panic(fmt.Sprintf("Unsupported merkle tree hash func '%s'", name))
}
if !hashFunc.Available() {
panic(fmt.Sprintf("Merkle tree hash func '%s' not available. Please check the package imports", name))
}
return hashFunc
}
// New creates a new coordinator instance.
func New(seed trinary.Hash, securityLvl consts.SecurityLevel, merkleTreeDepth int, minWeightMagnitude int, stateFilePath string, milestoneIntervalSec int, powFunc pow.ProofOfWorkFunc, checkpointTipselFunc CheckpointTipSelectionFunc, sendBundleFunc SendBundleFunc, milestoneMerkleHashFunc crypto.Hash) *Coordinator {
result := &Coordinator{
seed: seed,
securityLvl: securityLvl,
merkleTreeDepth: merkleTreeDepth,
minWeightMagnitude: minWeightMagnitude,
stateFilePath: stateFilePath,
milestoneIntervalSec: milestoneIntervalSec,
powFunc: powFunc,
checkpointTipselFunc: checkpointTipselFunc,
sendBundleFunc: sendBundleFunc,
milestoneMerkleHashFunc: milestoneMerkleHashFunc,
Events: &CoordinatorEvents{
IssuedCheckpointTransaction: events.NewEvent(CheckpointCaller),
IssuedMilestone: events.NewEvent(MilestoneCaller),
},
}
return result
}
// InitMerkleTree loads the Merkle tree file and checks the coordinator address.
func (coo *Coordinator) InitMerkleTree(filePath string, cooAddress trinary.Hash) error {
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return fmt.Errorf("Merkle tree file not found: %v", filePath)
}
var err error
coo.merkleTree, err = merkle.LoadMerkleTreeFile(filePath)
if err != nil {
return err
}
if cooAddress != coo.merkleTree.Root {
return fmt.Errorf("coordinator address does not match Merkle tree root: %v != %v", cooAddress, coo.merkleTree.Root)
}
return nil
}
// InitState loads an existing state file or bootstraps the network.
func (coo *Coordinator) InitState(bootstrap bool, startIndex milestone.Index) error {
_, err := os.Stat(coo.stateFilePath)
stateFileExists := !os.IsNotExist(err)
latestMilestoneFromDatabase := tangle.SearchLatestMilestoneIndexInStore()
if bootstrap {
if stateFileExists {
return ErrNetworkBootstrapped
}
if startIndex == 0 {
// start with milestone 1 at least
startIndex = 1
}
if latestMilestoneFromDatabase != startIndex-1 {
return fmt.Errorf("previous milestone does not match latest milestone in database: %d != %d", startIndex-1, latestMilestoneFromDatabase)
}
// if we bootstrap a network, NullHash has to be set as a solid entry point
tangle.SolidEntryPointsAdd(hornet.NullHashBytes, startIndex)
latestMilestoneHash := hornet.NullHashBytes
if startIndex != 1 {
// If we don't start a new network, the last milestone has to be referenced
cachedBndl := tangle.GetMilestoneOrNil(latestMilestoneFromDatabase)
if cachedBndl == nil {
return fmt.Errorf("latest milestone (%d) not found in database. database is corrupt", latestMilestoneFromDatabase)
}
latestMilestoneHash = cachedBndl.GetBundle().GetTailHash()
cachedBndl.Release()
}
// create a new coordinator state to bootstrap the network
state := &State{}
state.LatestMilestoneHash = latestMilestoneHash
state.LatestMilestoneIndex = startIndex
state.LatestMilestoneTime = 0
state.LatestMilestoneTransactions = hornet.Hashes{hornet.NullHashBytes}
coo.state = state
coo.lastCheckpointHash = coo.state.LatestMilestoneHash
coo.bootstrapped = false
return nil
}
if !stateFileExists {
return fmt.Errorf("state file not found: %v", coo.stateFilePath)
}
coo.state, err = loadStateFile(coo.stateFilePath)
if err != nil {
return err
}
if latestMilestoneFromDatabase != coo.state.LatestMilestoneIndex {
return fmt.Errorf("previous milestone does not match latest milestone in database: %d != %d", coo.state.LatestMilestoneIndex, latestMilestoneFromDatabase)
}
cachedBndl := tangle.GetMilestoneOrNil(latestMilestoneFromDatabase)
if cachedBndl == nil {
return fmt.Errorf("latest milestone (%d) not found in database. database is corrupt", latestMilestoneFromDatabase)
}
cachedBndl.Release()
coo.lastCheckpointHash = coo.state.LatestMilestoneHash
coo.lastMilestoneHash = coo.state.LatestMilestoneHash
coo.bootstrapped = true
return nil
}
// issueCheckpointWithoutLocking tries to create and send a "checkpoint" to the network.
// a checkpoint can contain multiple chained transactions to reference big parts of the unconfirmed cone.
// this is done to keep the confirmation rate as high as possible, even if there is an attack ongoing.
// new checkpoints always reference the last checkpoint or the last milestone if it is the first checkpoint after a new milestone.
func (coo *Coordinator) issueCheckpointWithoutLocking(minRequiredTips int) error {
if !tangle.IsNodeSynced() {
return tangle.ErrNodeNotSynced
}
tips, err := coo.checkpointTipselFunc(minRequiredTips)
if err != nil {
return err
}
var lastCheckpointHash hornet.Hash
if coo.lastCheckpointIndex == 0 {
// reference the last milestone
lastCheckpointHash = coo.lastMilestoneHash
} else {
// reference the last checkpoint
lastCheckpointHash = coo.lastCheckpointHash
}
for i, tip := range tips {
b, err := createCheckpoint(tip, lastCheckpointHash, coo.minWeightMagnitude, coo.powFunc)
if err != nil {
return err
}
if err := coo.sendBundleFunc(b); err != nil {
return err
}
lastCheckpointHash = hornet.HashFromHashTrytes(b[0].Hash)
coo.Events.IssuedCheckpointTransaction.Trigger(coo.lastCheckpointIndex, i, len(tips), lastCheckpointHash)
}
coo.lastCheckpointIndex++
coo.lastCheckpointHash = lastCheckpointHash
return nil
}
// createAndSendMilestone creates a milestone, sends it to the network and stores a new coordinator state file.
func (coo *Coordinator) createAndSendMilestone(trunkHash hornet.Hash, branchHash hornet.Hash, newMilestoneIndex milestone.Index) error {
// compute merkle tree root
byteEncodedMerkleTreeRootHash, err := whiteflag.ComputeMerkleTreeRootHash(coo.milestoneMerkleHashFunc, trunkHash, branchHash)
if err != nil {
return err
}
b, err := createMilestone(coo.seed, newMilestoneIndex, coo.securityLvl, trunkHash, branchHash, coo.minWeightMagnitude, coo.merkleTree, byteEncodedMerkleTreeRootHash, coo.powFunc)
if err != nil {
return err
}
if err := coo.sendBundleFunc(b); err != nil {
return err
}
txHashes := hornet.Hashes{}
for _, tx := range b {
txHashes = append(txHashes, hornet.HashFromHashTrytes(tx.Hash))
}
tailTx := b[0]
// reset checkpoint count
coo.lastCheckpointIndex = 0
// always reference the last milestone directly to speed up syncing
latestMilestoneHash := hornet.HashFromHashTrytes(tailTx.Hash)
coo.lastMilestoneHash = latestMilestoneHash
coo.state.LatestMilestoneHash = latestMilestoneHash
coo.state.LatestMilestoneIndex = newMilestoneIndex
coo.state.LatestMilestoneTime = int64(tailTx.Timestamp)
coo.state.LatestMilestoneTransactions = txHashes
if err := coo.state.storeStateFile(coo.stateFilePath); err != nil {
return err
}
coo.Events.IssuedMilestone.Trigger(coo.state.LatestMilestoneIndex, coo.state.LatestMilestoneHash)
return nil
}
// Bootstrap creates the first milestone, if the network was not bootstrapped yet.
// Returns critical errors.
func (coo *Coordinator) Bootstrap() error {
coo.milestoneLock.Lock()
defer coo.milestoneLock.Unlock()
if !coo.bootstrapped {
// create first milestone to bootstrap the network
if err := coo.createAndSendMilestone(hornet.Hash(hornet.NullHashBytes), hornet.Hash(hornet.NullHashBytes), coo.state.LatestMilestoneIndex); err != nil {
// creating milestone failed => critical error
return err
}
coo.bootstrapped = true
}
return nil
}
// IssueCheckpoint tries to create and send a "checkpoint" to the network.
// a checkpoint can contain multiple chained transactions to reference big parts of the unconfirmed cone.
// this is done to keep the confirmation rate as high as possible, even if there is an attack ongoing.
// new checkpoints always reference the last checkpoint or the last milestone if it is the first checkpoint after a new milestone.
func (coo *Coordinator) IssueCheckpoint() error {
coo.milestoneLock.Lock()
defer coo.milestoneLock.Unlock()
return coo.issueCheckpointWithoutLocking(0)
}
// IssueMilestone creates the next milestone.
// a new checkpoint is created right in front of the milestone to raise confirmation rate.
// Returns non-critical and critical errors.
func (coo *Coordinator) IssueMilestone() (error, error) {
coo.milestoneLock.Lock()
defer coo.milestoneLock.Unlock()
if !tangle.IsNodeSynced() {
// return a non-critical error to not kill the database
return tangle.ErrNodeNotSynced, nil
}
lastCheckpointHash := coo.lastCheckpointHash
// issue a new checkpoint right in front of the milestone
if err := coo.issueCheckpointWithoutLocking(1); err != nil {
// creating checkpoint failed => not critical
if coo.lastCheckpointIndex == 0 {
// no checkpoint created => use the last milestone hash
lastCheckpointHash = coo.lastMilestoneHash
}
} else {
// use the new checkpoint hash
lastCheckpointHash = coo.lastCheckpointHash
}
if err := coo.createAndSendMilestone(coo.lastMilestoneHash, lastCheckpointHash, coo.state.LatestMilestoneIndex+1); err != nil {
// creating milestone failed => critical error
return nil, err
}
return nil, nil
}
// GetInterval returns the interval milestones should be issued.
func (coo *Coordinator) GetInterval() time.Duration {
return time.Second * time.Duration(coo.milestoneIntervalSec)
}
// State returns the current state of the coordinator.
func (coo *Coordinator) State() *State {
return coo.state
}