-
Notifications
You must be signed in to change notification settings - Fork 12
/
booker.go
275 lines (217 loc) · 9.13 KB
/
booker.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
package inmemorybooker
import (
"sync/atomic"
"github.com/iotaledger/hive.go/ds"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/runtime/module"
"github.com/iotaledger/hive.go/runtime/options"
"github.com/iotaledger/iota-core/pkg/model"
"github.com/iotaledger/iota-core/pkg/protocol/engine"
"github.com/iotaledger/iota-core/pkg/protocol/engine/blocks"
"github.com/iotaledger/iota-core/pkg/protocol/engine/booker"
"github.com/iotaledger/iota-core/pkg/protocol/engine/ledger"
"github.com/iotaledger/iota-core/pkg/protocol/engine/mempool"
"github.com/iotaledger/iota-core/pkg/protocol/engine/mempool/spenddag"
iotago "github.com/iotaledger/iota.go/v4"
)
type Booker struct {
events *booker.Events
blockCache *blocks.Blocks
ledger ledger.Ledger
spendDAG spenddag.SpendDAG[iotago.TransactionID, mempool.StateID, ledger.BlockVoteRank]
loadBlockFromStorage func(id iotago.BlockID) (*model.Block, bool)
errorHandler func(error)
apiProvider iotago.APIProvider
module.Module
}
func NewProvider(opts ...options.Option[Booker]) module.Provider[*engine.Engine, booker.Booker] {
return module.Provide(func(e *engine.Engine) booker.Booker {
b := New(e.NewSubModule("Booker"), e, e.BlockCache, e.ErrorHandler("booker"), opts...)
e.ConstructedEvent().OnTrigger(func() {
b.Init(e.Ledger, e.Block)
e.Events.SeatManager.BlockProcessed.Hook(func(block *blocks.Block) {
if err := b.Queue(block); err != nil {
b.errorHandler(err)
}
})
e.Events.Booker.LinkTo(b.events)
b.InitializedEvent().Trigger()
})
return b
})
}
func New(subModule module.Module, apiProvider iotago.APIProvider, blockCache *blocks.Blocks, errorHandler func(error), opts ...options.Option[Booker]) *Booker {
return options.Apply(&Booker{
Module: subModule,
events: booker.NewEvents(),
apiProvider: apiProvider,
blockCache: blockCache,
errorHandler: errorHandler,
}, opts, func(b *Booker) {
b.ShutdownEvent().OnTrigger(func() {
b.StoppedEvent().Trigger()
})
b.ConstructedEvent().Trigger()
})
}
func (b *Booker) Init(ledger ledger.Ledger, loadBlockFromStorage func(iotago.BlockID) (*model.Block, bool)) {
b.ledger = ledger
b.loadBlockFromStorage = loadBlockFromStorage
ledger.InitializedEvent().OnTrigger(func() {
b.spendDAG = ledger.SpendDAG()
ledger.MemPool().OnTransactionAttached(func(transaction mempool.TransactionMetadata) {
transaction.OnAccepted(func() {
b.events.TransactionAccepted.Trigger(transaction)
})
transaction.OnInvalid(func(err error) {
b.events.TransactionInvalid.Trigger(transaction, err)
})
})
b.InitializedEvent().Trigger()
})
}
// Queue checks if payload is solid and then sets up the block to react to its parents.
func (b *Booker) Queue(block *blocks.Block) error {
signedTransactionMetadata, containsTransaction := b.ledger.AttachTransaction(block)
if !containsTransaction {
b.setupBlock(block, nil)
return nil
} else if signedTransactionMetadata == nil {
return ierrors.Errorf("transaction in block %s was not attached", block.ID())
}
// Based on the assumption that we always fork and the UTXO and Tangle past cones are always fully known.
signedTransactionMetadata.OnSignaturesValid(func() {
transactionMetadata := signedTransactionMetadata.TransactionMetadata()
if orphanedSlot, isOrphaned := transactionMetadata.OrphanedSlot(); isOrphaned && orphanedSlot <= block.SlotCommitmentID().Slot() {
block.SetInvalid()
return
}
transactionMetadata.OnBooked(func() {
block.SetPayloadSpenderIDs(transactionMetadata.SpenderIDs())
b.setupBlock(block, signedTransactionMetadata)
})
transactionMetadata.OnInvalid(func(_ error) {
b.setupBlock(block, signedTransactionMetadata)
})
})
signedTransactionMetadata.OnSignaturesInvalid(func(_ error) {
b.setupBlock(block, signedTransactionMetadata)
})
return nil
}
// Reset resets the component to a clean state as if it was created at the last commitment.
func (b *Booker) Reset() { /* nothing to reset but comply with interface */ }
func (b *Booker) setupBlock(block *blocks.Block, signedTransactionMetadata mempool.SignedTransactionMetadata) {
var payloadDependencies, directlyReferencedPayloadDependencies ds.Set[mempool.StateMetadata]
if signedTransactionMetadata != nil && signedTransactionMetadata.SignaturesInvalid() == nil && !signedTransactionMetadata.TransactionMetadata().IsInvalid() {
payloadDependencies = signedTransactionMetadata.TransactionMetadata().Inputs()
directlyReferencedPayloadDependencies = ds.NewSet[mempool.StateMetadata]()
}
var unbookedParentsCount atomic.Int32
unbookedParentsCount.Store(int32(len(block.Parents())))
block.ForEachParent(func(parent iotago.Parent) {
parentBlock, exists := b.blockCache.Block(parent.ID)
if !exists {
b.errorHandler(ierrors.Errorf("cannot setup block %s without existing parent %s", block.ID(), parent.ID))
return
}
parentBlock.Booked().OnUpdateOnce(func(_ bool, _ bool) {
if directlyReferencedPayloadDependencies != nil {
if signedTx, hasTx := parentBlock.SignedTransaction(); hasTx {
if parentTransactionMetadata, exists := b.ledger.TransactionMetadata(signedTx.Transaction.MustID()); exists {
directlyReferencedPayloadDependencies.AddAll(parentTransactionMetadata.Outputs())
}
}
}
if unbookedParentsCount.Add(-1) == 0 {
block.ParentsBooked.Trigger()
}
})
parentBlock.Invalid().OnUpdateOnce(func(_ bool, _ bool) {
if block.SetInvalid() {
b.events.BlockInvalid.Trigger(block, ierrors.Errorf("block marked as invalid in Booker because parent block %s is invalid", parentBlock.ID()))
}
})
})
block.ParentsBooked.OnTrigger(func() {
if directlyReferencedPayloadDependencies != nil {
payloadDependencies.DeleteAll(directlyReferencedPayloadDependencies)
}
block.WaitForPayloadDependencies(payloadDependencies)
})
block.PayloadDependenciesAvailable.OnTrigger(func() {
if err := b.book(block); err != nil {
if block.SetInvalid() {
b.events.BlockInvalid.Trigger(block, ierrors.Wrap(err, "failed to book block"))
}
}
})
}
func (b *Booker) book(block *blocks.Block) error {
spendersToInherit, err := b.inheritSpenders(block)
if err != nil {
return ierrors.Wrapf(err, "failed to inherit spenders for block %s", block.ID())
}
// The block does not inherit spenders that have been orphaned with respect to its commitment.
for it := spendersToInherit.Iterator(); it.HasNext(); {
spenderID := it.Next()
txMetadata, exists := b.ledger.MemPool().TransactionMetadata(spenderID)
if !exists {
return ierrors.Errorf("failed to load transaction %s for block %s", spenderID.String(), block.ID())
}
if orphanedSlot, orphaned := txMetadata.OrphanedSlot(); orphaned && orphanedSlot <= block.SlotCommitmentID().Slot() {
// Merge-to-master orphaned spenders.
spendersToInherit.Delete(spenderID)
}
}
block.SetSpenderIDs(spendersToInherit)
block.SetBooked()
b.events.BlockBooked.Trigger(block)
return nil
}
func (b *Booker) inheritSpenders(block *blocks.Block) (spenderIDs ds.Set[iotago.TransactionID], err error) {
spenderIDsToInherit := ds.NewSet[iotago.TransactionID]()
// Inherit spenderIDs from parents based on the parent type.
for _, parent := range block.ParentsWithType() {
parentBlock, exists := b.blockCache.Block(parent.ID)
if !exists {
return nil, ierrors.Errorf("parent %s does not exist", parent.ID)
}
switch parent.Type {
case iotago.StrongParentType:
spenderIDsToInherit.AddAll(parentBlock.SpenderIDs())
case iotago.WeakParentType:
spenderIDsToInherit.AddAll(parentBlock.PayloadSpenderIDs())
case iotago.ShallowLikeParentType:
// If parent block is a RootBlock, then make sure that the block contains a transaction;
// otherwise, the reference is invalid.
if parentBlock.IsRootBlock() {
parentModelBlock, exists := b.loadBlockFromStorage(parent.ID)
if !exists {
return nil, ierrors.Wrapf(err, "shallow like parent %s does not exist in storage", parent.ID.String())
}
if _, hasTx := parentModelBlock.SignedTransaction(); !hasTx {
return nil, ierrors.Wrapf(err, "shallow like parent %s does not contain a conflicting transaction", parent.ID.String())
}
break
}
// Check whether the parent contains a conflicting TX,
// otherwise reference is invalid and the block should be marked as invalid as well.
if _, hasTx := parentBlock.SignedTransaction(); !hasTx {
return nil, ierrors.Wrapf(err, "shallow like parent %s does not contain a conflicting transaction", parent.ID.String())
}
spenderIDsToInherit.AddAll(parentBlock.PayloadSpenderIDs())
// remove all conflicting spenders from spenderIDsToInherit
for _, spenderID := range parentBlock.PayloadSpenderIDs().ToSlice() {
if conflictingSpends, exists := b.spendDAG.ConflictingSpenders(spenderID); exists {
spenderIDsToInherit.DeleteAll(b.spendDAG.FutureCone(conflictingSpends))
}
}
}
}
// Add all spenders from the block's payload itself.
// Forking on booking: we determine the block's PayloadSpenderIDs by treating each TX as a spender.
spenderIDsToInherit.AddAll(block.PayloadSpenderIDs())
// Only inherit spenders that are not yet accepted (aka merge to master).
return b.spendDAG.UnacceptedSpenders(spenderIDsToInherit), nil
}