-
Notifications
You must be signed in to change notification settings - Fork 151
/
tangle.go
217 lines (182 loc) · 8.06 KB
/
tangle.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
package testsuite
import (
"bytes"
"context"
"fmt"
"github.com/stretchr/testify/require"
"github.com/iotaledger/hornet/pkg/dag"
"github.com/iotaledger/hornet/pkg/model/hornet"
"github.com/iotaledger/hornet/pkg/model/milestone"
"github.com/iotaledger/hornet/pkg/model/storage"
"github.com/iotaledger/hornet/pkg/model/utxo"
"github.com/iotaledger/hornet/pkg/tangle"
"github.com/iotaledger/hornet/pkg/testsuite/utils"
"github.com/iotaledger/hornet/pkg/whiteflag"
)
// StoreMessage adds the message to the storage layer and solidifies it.
// message +1
func (te *TestEnvironment) StoreMessage(msg *storage.Message) *storage.CachedMessage {
// Store message in the database
cachedMsg, alreadyAdded := tangle.AddMessageToStorage(te.storage, te.milestoneManager, msg, te.syncManager.LatestMilestoneIndex(), false, true)
require.NotNil(te.TestInterface, cachedMsg)
require.False(te.TestInterface, alreadyAdded)
// Solidify msg
cachedMsg.Metadata().SetSolid(true)
require.True(te.TestInterface, cachedMsg.Metadata().IsSolid())
te.cachedMessages = append(te.cachedMessages, cachedMsg)
return cachedMsg
}
// VerifyCMI checks if the confirmed milestone index is equal to the given milestone index.
func (te *TestEnvironment) VerifyCMI(index milestone.Index) {
cmi := te.syncManager.ConfirmedMilestoneIndex()
require.Equal(te.TestInterface, index, cmi)
}
// VerifyLMI checks if the latest milestone index is equal to the given milestone index.
func (te *TestEnvironment) VerifyLMI(index milestone.Index) {
lmi := te.syncManager.LatestMilestoneIndex()
require.Equal(te.TestInterface, index, lmi)
}
// AssertLedgerBalance generates an address for the given seed and index and checks correct balance.
func (te *TestEnvironment) AssertLedgerBalance(wallet *utils.HDWallet, expectedBalance uint64) {
addrBalance, _, _, err := te.storage.UTXOManager().AddressBalance(wallet.Address())
require.NoError(te.TestInterface, err)
computedAddrBalance, _, err := te.storage.UTXOManager().ComputeBalance(utxo.FilterAddress(wallet.Address()))
require.NoError(te.TestInterface, err)
var balanceStatus string
balanceStatus += fmt.Sprintf("Balance for %s:\n", wallet.Name())
balanceStatus += fmt.Sprintf("\tLedger:\t\t%d\n", computedAddrBalance)
balanceStatus += fmt.Sprintf("\tComputed:\t%d\n", computedAddrBalance)
balanceStatus += fmt.Sprintf("\tExpected:\t%d\n", expectedBalance)
fmt.Print(balanceStatus)
require.Exactly(te.TestInterface, expectedBalance, addrBalance)
require.Exactly(te.TestInterface, expectedBalance, computedAddrBalance)
}
// AssertWalletBalance generates an address for the given seed and index and checks correct balance.
func (te *TestEnvironment) AssertWalletBalance(wallet *utils.HDWallet, expectedBalance uint64) {
addrBalance, _, _, err := te.storage.UTXOManager().AddressBalance(wallet.Address())
require.NoError(te.TestInterface, err)
computedAddrBalance, _, err := te.storage.UTXOManager().ComputeBalance(utxo.FilterAddress(wallet.Address()))
require.NoError(te.TestInterface, err)
var balanceStatus string
balanceStatus += fmt.Sprintf("Balance for %s:\n", wallet.Name())
balanceStatus += fmt.Sprintf("\tLedger:\t\t%d\n", computedAddrBalance)
balanceStatus += fmt.Sprintf("\tComputed:\t%d\n", computedAddrBalance)
balanceStatus += fmt.Sprintf("\tWallet:\t\t%d\n", wallet.Balance())
balanceStatus += fmt.Sprintf("\tExpected:\t%d\n", expectedBalance)
fmt.Print(balanceStatus)
require.Exactly(te.TestInterface, expectedBalance, addrBalance)
require.Exactly(te.TestInterface, expectedBalance, computedAddrBalance)
require.Exactly(te.TestInterface, expectedBalance, wallet.Balance())
}
// AssertTotalSupplyStillValid checks if the total supply in the database is still correct.
func (te *TestEnvironment) AssertTotalSupplyStillValid() {
err := te.storage.UTXOManager().CheckLedgerState()
require.NoError(te.TestInterface, err)
}
func (te *TestEnvironment) AssertMessageConflictReason(messageID hornet.MessageID, conflict storage.Conflict) {
cachedMsgMeta := te.storage.CachedMessageMetadataOrNil(messageID)
require.NotNil(te.TestInterface, cachedMsgMeta)
defer cachedMsgMeta.Release(true) // meta -1
require.Equal(te.TestInterface, cachedMsgMeta.Metadata().Conflict(), conflict)
}
// generateDotFileFromConfirmation generates a dot file from a whiteflag confirmation cone.
func (te *TestEnvironment) generateDotFileFromConfirmation(conf *whiteflag.Confirmation) string {
indexOf := func(hash hornet.MessageID) int {
if conf == nil {
return -1
}
for i := 0; i < len(conf.Mutations.MessagesReferenced)-1; i++ {
if bytes.Equal(conf.Mutations.MessagesReferenced[i], hash) {
return i
}
}
return -1
}
visitedCachedMessages := make(map[string]*storage.CachedMessage)
err := dag.TraverseParentsOfMessage(
context.Background(),
te.storage,
conf.MilestoneMessageID,
// traversal stops if no more messages pass the given condition
// Caution: condition func is not in DFS order
func(cachedMsgMeta *storage.CachedMetadata) (bool, error) { // meta +1
defer cachedMsgMeta.Release(true) // meta -1
return true, nil
},
// consumer
func(cachedMsgMeta *storage.CachedMetadata) error { // meta +1
defer cachedMsgMeta.Release(true) // meta -1
if _, visited := visitedCachedMessages[cachedMsgMeta.Metadata().MessageID().ToMapKey()]; !visited {
cachedMsg := te.storage.CachedMessageOrNil(cachedMsgMeta.Metadata().MessageID()) // message +1
require.NotNil(te.TestInterface, cachedMsg)
visitedCachedMessages[cachedMsgMeta.Metadata().MessageID().ToMapKey()] = cachedMsg
}
return nil
},
// called on missing parents
nil,
// called on solid entry points
// Ignore solid entry points (snapshot milestone included)
nil,
false)
require.NoError(te.TestInterface, err)
var milestoneMsgs []string
var includedMsgs []string
var ignoredMsgs []string
var conflictingMsgs []string
dotFile := fmt.Sprintf("digraph %s\n{\n", te.TestInterface.Name())
for _, cachedMsg := range visitedCachedMessages {
message := cachedMsg.Message()
meta := cachedMsg.Metadata()
shortIndex := utils.ShortenedIndex(cachedMsg.Retain()) // message pass +1
if index := indexOf(message.MessageID()); index != -1 {
dotFile += fmt.Sprintf("\"%s\" [ label=\"[%d] %s\" ];\n", shortIndex, index, shortIndex)
}
for i, parent := range message.Parents() {
contains, err := te.storage.SolidEntryPointsContain(parent)
if err != nil {
panic(err)
}
if contains {
dotFile += fmt.Sprintf("\"%s\" -> \"%s\" [ label=\"Parent%d\" ];\n", shortIndex, utils.ShortenedHash(parent), i+1)
continue
}
cachedMsgParent := te.storage.CachedMessageOrNil(parent) // message +1
require.NotNil(te.TestInterface, cachedMsgParent)
dotFile += fmt.Sprintf("\"%s\" -> \"%s\" [ label=\"Parent%d\" ];\n", shortIndex, utils.ShortenedIndex(cachedMsgParent.Retain()), i+1) // message pass +1
cachedMsgParent.Release(true) // message -1
}
ms := message.Milestone()
if ms != nil {
if conf != nil && milestone.Index(ms.Index) == conf.MilestoneIndex {
dotFile += fmt.Sprintf("\"%s\" [style=filled,color=gold];\n", shortIndex)
}
milestoneMsgs = append(milestoneMsgs, shortIndex)
} else if meta.IsReferenced() {
if meta.IsConflictingTx() {
conflictingMsgs = append(conflictingMsgs, shortIndex)
} else if meta.IsNoTransaction() {
ignoredMsgs = append(ignoredMsgs, shortIndex)
} else if meta.IsIncludedTxInLedger() {
includedMsgs = append(includedMsgs, shortIndex)
} else {
panic("unknown msg state")
}
}
cachedMsg.Release(true) // message -1
}
for _, milestone := range milestoneMsgs {
dotFile += fmt.Sprintf("\"%s\" [shape=Msquare];\n", milestone)
}
for _, conflictingMsg := range conflictingMsgs {
dotFile += fmt.Sprintf("\"%s\" [style=filled,color=red];\n", conflictingMsg)
}
for _, ignoredMsg := range ignoredMsgs {
dotFile += fmt.Sprintf("\"%s\" [style=filled,color=gray];\n", ignoredMsg)
}
for _, includedMsg := range includedMsgs {
dotFile += fmt.Sprintf("\"%s\" [style=filled,color=green];\n", includedMsg)
}
dotFile += "}\n"
return dotFile
}