-
Notifications
You must be signed in to change notification settings - Fork 232
/
multisets.go
109 lines (88 loc) · 3.59 KB
/
multisets.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
package consensusstatemanager
import (
"github.com/kaspanet/kaspad/domain/consensus/model"
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
"github.com/kaspanet/kaspad/domain/consensus/utils/utxo"
)
func (csm *consensusStateManager) calculateMultiset(stagingArea *model.StagingArea,
blockHash *externalapi.DomainHash,
acceptanceData externalapi.AcceptanceData,
blockGHOSTDAGData *externalapi.BlockGHOSTDAGData,
daaScore uint64) (model.Multiset, error) {
log.Debugf("calculateMultiset start for block with selected parent %s", blockGHOSTDAGData.SelectedParent())
defer log.Debugf("calculateMultiset end for block with selected parent %s", blockGHOSTDAGData.SelectedParent())
if blockHash.Equal(csm.genesisHash) {
log.Debugf("Selected parent is nil, which could only happen for the genesis. " +
"The genesis has a predefined multiset")
return csm.multisetStore.Get(csm.databaseContext, stagingArea, blockHash)
}
ms, err := csm.multisetStore.Get(csm.databaseContext, stagingArea, blockGHOSTDAGData.SelectedParent())
if err != nil {
return nil, err
}
log.Debugf("The multiset for the selected parent %s is: %s", blockGHOSTDAGData.SelectedParent(), ms.Hash())
for _, blockAcceptanceData := range acceptanceData {
for i, transactionAcceptanceData := range blockAcceptanceData.TransactionAcceptanceData {
transaction := transactionAcceptanceData.Transaction
transactionID := consensushashing.TransactionID(transaction)
if !transactionAcceptanceData.IsAccepted {
log.Tracef("Skipping transaction %s because it was not accepted", transactionID)
continue
}
isCoinbase := i == 0
log.Tracef("Is transaction %s a coinbase transaction: %t", transactionID, isCoinbase)
err := addTransactionToMultiset(ms, transaction, daaScore, isCoinbase)
if err != nil {
return nil, err
}
log.Tracef("Added transaction %s to the multiset", transactionID)
}
}
return ms, nil
}
func addTransactionToMultiset(multiset model.Multiset, transaction *externalapi.DomainTransaction,
blockDAAScore uint64, isCoinbase bool) error {
transactionID := consensushashing.TransactionID(transaction)
log.Tracef("addTransactionToMultiset start for transaction %s", transactionID)
defer log.Tracef("addTransactionToMultiset end for transaction %s", transactionID)
for _, input := range transaction.Inputs {
log.Tracef("Removing input %s at index %d from the multiset",
input.PreviousOutpoint.TransactionID, input.PreviousOutpoint.Index)
err := removeUTXOFromMultiset(multiset, input.UTXOEntry, &input.PreviousOutpoint)
if err != nil {
return err
}
}
for i, output := range transaction.Outputs {
outpoint := &externalapi.DomainOutpoint{
TransactionID: *transactionID,
Index: uint32(i),
}
utxoEntry := utxo.NewUTXOEntry(output.Value, output.ScriptPublicKey, isCoinbase, blockDAAScore)
log.Tracef("Adding input %s at index %d from the multiset", transactionID, i)
err := addUTXOToMultiset(multiset, utxoEntry, outpoint)
if err != nil {
return err
}
}
return nil
}
func addUTXOToMultiset(multiset model.Multiset, entry externalapi.UTXOEntry,
outpoint *externalapi.DomainOutpoint) error {
serializedUTXO, err := utxo.SerializeUTXO(entry, outpoint)
if err != nil {
return err
}
multiset.Add(serializedUTXO)
return nil
}
func removeUTXOFromMultiset(multiset model.Multiset, entry externalapi.UTXOEntry,
outpoint *externalapi.DomainOutpoint) error {
serializedUTXO, err := utxo.SerializeUTXO(entry, outpoint)
if err != nil {
return err
}
multiset.Remove(serializedUTXO)
return nil
}