From 5711a15cda6b715bb3d7428943a2c5ad53174635 Mon Sep 17 00:00:00 2001 From: Harri Lainio Date: Sat, 20 Nov 2021 17:53:11 +0200 Subject: [PATCH] First working long invition chains with multiple roots --- node/node_test.go | 53 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/node/node_test.go b/node/node_test.go index 1b102fb..afb774b 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -38,23 +38,23 @@ func setup() { dave.Key = crypto.NewKey() eve.Key = crypto.NewKey() - root1.Node = NewRootNode(root1.Key.PubKey) - root2.Node = NewRootNode(root2.Key.PubKey) + root1.Node = NewRootNode(root1.PubKey) + root2.Node = NewRootNode(root2.PubKey) } func TestNewNode(t *testing.T) { - aliceNode := NewRootNode(alice.Key.PubKey) + aliceNode := NewRootNode(alice.PubKey) assert.Len(t, aliceNode.Chains, 1) assert.Len(t, aliceNode.Chains[0].Blocks, 1) - bobNode := NewRootNode(bob.Key.PubKey) + bobNode := NewRootNode(bob.PubKey) assert.Len(t, bobNode.Chains, 1) } func TestInvite(t *testing.T) { - alice.Node = root1.Invite(alice.Node, root1.Key, alice.Key.PubKey, 1) + // root1 chains start here: + alice.Node = root1.Invite(alice.Node, root1.Key, alice.PubKey, 1) assert.Len(t, alice.Node.Chains, 1) - for { c := alice.Node.Chains[0] assert.Len(t, c.Blocks, 2) @@ -62,9 +62,8 @@ func TestInvite(t *testing.T) { break } - bob.Node = alice.Invite(bob.Node, alice.Key, bob.Key.PubKey, 1) + bob.Node = alice.Invite(bob.Node, alice.Key, bob.PubKey, 1) assert.Len(t, bob.Node.Chains, 1) - for { c := bob.Node.Chains[0] assert.Len(t, c.Blocks, 3) @@ -75,9 +74,9 @@ func TestInvite(t *testing.T) { common := alice.CommonChain(bob.Node) assert.NotNil(t, common.Blocks) - carol.Node = root1.Invite(carol.Node, root1.Key, carol.Key.PubKey, 1) + // root2 invites carol here + carol.Node = root2.Invite(carol.Node, root2.Key, carol.PubKey, 1) assert.Len(t, carol.Node.Chains, 1) - for { c := carol.Node.Chains[0] assert.Len(t, c.Blocks, 2) @@ -85,17 +84,45 @@ func TestInvite(t *testing.T) { break } - dave.Node = NewRootNode(dave.Key.PubKey) - dave.Node = root2.Invite(dave.Node, root2.Key, dave.Key.PubKey, 1) - assert.Len(t, dave.Node.Chains, 2) + // alice is root1 and carol root2 chain, so no common ground. + common = alice.CommonChain(carol.Node) + assert.Nil(t, common.Blocks) + + // dave is one of the roots as well and we build it here: + dave.Node = NewRootNode(dave.PubKey) + eve.Node = dave.Invite(eve.Node, dave.Key, eve.PubKey, 1) + assert.Len(t, eve.Node.Chains, 1) + for { + c := eve.Node.Chains[0] + assert.Len(t, c.Blocks, 2) + assert.True(t, c.Verify()) + break + } + dave.Node = root2.Invite(dave.Node, root2.Key, dave.PubKey, 1) + assert.Len(t, dave.Node.Chains, 2) for { c := dave.Node.Chains[1] assert.Len(t, c.Blocks, 2) assert.True(t, c.Verify()) break } + // dave joins to root2 but until now, that's why eve is not member of root2 + common = root2.CommonChain(eve.Node) + assert.Nil(t, common.Blocks) + // carol and eve doesn't have common chains + common = carol.CommonChain(eve.Node) + assert.Nil(t, common.Blocks) + + // .. so carol can invite eve + eve.Node = carol.Invite(eve.Node, carol.Key, eve.PubKey, 1) + assert.Len(t, eve.Node.Chains, 2) + + // now eve has common chain with root1 as well + common = eve.CommonChain(root2.Node) + assert.NotNil(t, common.Blocks) + } // func TestXXX(t *testing.T) {