Skip to content
Snippets Groups Projects
Unverified Commit 282a01e5 authored by Levente Pap's avatar Levente Pap
Browse files

Continue refactoring codebase to use new message layout - WIP

parent 18ca0dae
No related branches found
No related tags found
No related merge requests found
......@@ -53,7 +53,7 @@ func FromMarshalUtil(marshalUtil *marshalutil.MarshalUtil) (payload Payload, err
return
}
marshalUtil.ReadSeek(-marshalutil.UINT32_SIZE * 2)
marshalUtil.ReadSeek(-marshalutil.Uint32Size * 2)
payloadBytes, err := marshalUtil.ReadBytes(int(payloadSize) + 8)
if err != nil {
err = xerrors.Errorf("failed to unmarshal payload bytes (%v): %w", err, cerrors.ErrParseBytesFailed)
......
......@@ -112,8 +112,25 @@ func TestTangle_MissingMessages(t *testing.T) {
badgerDB,
[]byte("sequenceKey"),
identity.GenerateLocalIdentity(),
TipSelectorFunc(func() (MessageID, MessageID) {
return tips.RandomEntry().(MessageID), tips.RandomEntry().(MessageID)
TipSelectorFunc(func(count int) (parents []MessageID) {
parents = make([]MessageID, 0, count)
tmp := tips.RandomUniqueEntries(count)
// count is not valid
if tmp == nil {
parents = append(parents, EmptyMessageID)
return
}
// count is valid, but there simply are no tips
if len(tmp) == 0 {
parents = append(parents, EmptyMessageID)
return
}
// at least one tip is returned
for _, tip := range tmp {
parents = append(parents, tip.(MessageID))
}
return
}),
)
defer msgFactory.Shutdown()
......@@ -232,9 +249,9 @@ func TestTangle_MissingMessages(t *testing.T) {
func TestRetrieveAllTips(t *testing.T) {
messageTangle := New(mapdb.NewMapDB())
messageA := newTestParentsDataMessage("A", EmptyMessageID, EmptyMessageID)
messageB := newTestParentsDataMessage("B", messageA.ID(), EmptyMessageID)
messageC := newTestParentsDataMessage("C", messageA.ID(), EmptyMessageID)
messageA := newTestParentsDataMessage("A", []MessageID{EmptyMessageID}, []MessageID{EmptyMessageID})
messageB := newTestParentsDataMessage("B", []MessageID{messageA.ID()}, []MessageID{EmptyMessageID})
messageC := newTestParentsDataMessage("C", []MessageID{messageA.ID()}, []MessageID{EmptyMessageID})
var wg sync.WaitGroup
......
......@@ -45,24 +45,30 @@ func (t *MessageTipSelector) AddTip(msg *Message) {
})
}
// Tips returns two tips.
// Tips returns count number of tips, maximum MaxParentsCount.
func (t *MessageTipSelector) Tips(count int) (parents []MessageID) {
if count > MaxParentsCount {
count = MaxParentsCount
}
if count < MinParentsCount {
count = MinParentsCount
}
parents = make([]MessageID, 0, count)
tip := t.tips.RandomEntry()
if tip == nil {
tips := t.tips.RandomUniqueEntries(count)
// count is not valid
if tips == nil {
parents = append(parents, EmptyMessageID)
return
}
tipMessageID := tip.(MessageID)
parents = append(parents, tipMessageID)
// TODO: adjust tip selection to select as many tips as count
// it is a bit tricky to not cause a deadlock if we don't allow duplicates
parent1MessageID = t.tips.RandomEntry().(MessageID)
for parent1MessageID == parent2MessageID && t.tips.Size() > 1 {
parent1MessageID = t.tips.RandomEntry().(MessageID)
// count is valid, but there simply are no tips
if len(tips) == 0 {
parents = append(parents, EmptyMessageID)
return
}
// at least one tip is returned
for _, tip := range tips {
parents = append(parents, tip.(MessageID))
}
return
......
......@@ -11,37 +11,40 @@ func TestMessageTipSelector(t *testing.T) {
tipSelector := NewMessageTipSelector()
// check if first tips point to genesis
parent11, parent21 := tipSelector.Tips()
assert.Equal(t, EmptyMessageID, parent11)
assert.Equal(t, EmptyMessageID, parent21)
parents := tipSelector.Tips(2)
// there has to be only one valid tip, the genesis message
assert.Equal(t, 1, len(parents))
assert.Equal(t, parents[0], EmptyMessageID)
// create a message and attach it
message1 := newTestParentsDataMessage("testmessage1", parent11, parent21)
message1 := newTestParentsDataMessage("testmessage1", parents, []MessageID{})
tipSelector.AddTip(message1)
// check if the tip shows up in the tip count
assert.Equal(t, 1, tipSelector.TipCount())
// check if next tips point to our first message
parent12, parent22 := tipSelector.Tips()
assert.Equal(t, message1.ID(), parent12)
assert.Equal(t, message1.ID(), parent22)
parents2 := tipSelector.Tips(2)
assert.Equal(t, 1, len(parents2))
assert.Contains(t, parents2, message1.ID())
// create a 2nd message and attach it
message2 := newTestParentsDataMessage("testmessage2", EmptyMessageID, EmptyMessageID)
message2 := newTestParentsDataMessage("testmessage2", []MessageID{EmptyMessageID}, []MessageID{EmptyMessageID})
tipSelector.AddTip(message2)
// check if the tip shows up in the tip count
assert.Equal(t, 2, tipSelector.TipCount())
// attach a message to our two tips
parent13, parent23 := tipSelector.Tips()
message3 := newTestParentsDataMessage("testmessage3", parent13, parent23)
parents3 := tipSelector.Tips(2)
message3 := newTestParentsDataMessage("testmessage3", parents3, []MessageID{})
tipSelector.AddTip(message3)
// check if the tip shows replaces the current tips
parent14, parent24 := tipSelector.Tips()
parents4 := tipSelector.Tips(2)
assert.Equal(t, 1, tipSelector.TipCount())
assert.Equal(t, message3.ID(), parent14)
assert.Equal(t, message3.ID(), parent24)
assert.Equal(t, 1, len(parents4))
assert.Contains(t, parents4, message3.ID())
assert.Contains(t, parents4, message3.ID())
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment