From a5627ecfcc815ce8d046c3a60f0b6b875e48396b Mon Sep 17 00:00:00 2001 From: Tzu-Jung Lee Date: Wed, 11 Jul 2018 12:40:13 -0700 Subject: [PATCH] claimnode: Add the example on the whitepaper as a testcase. Note: all the test cases will be reworked to a more compact form. --- claimnode/node_test.go | 141 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) diff --git a/claimnode/node_test.go b/claimnode/node_test.go index 3111e1e..90b3303 100644 --- a/claimnode/node_test.go +++ b/claimnode/node_test.go @@ -65,6 +65,146 @@ func Test_calNodeHash(t *testing.T) { } } +// The example on the [whitepaper](https://beta.lbry.tech/whitepaper.html) +func Test_History0(t *testing.T) { + + proportionalDelayFactor = 32 + n := NewNode() + + n.AdjustTo(12) + claimA, _ := n.AddClaim(*newOutPoint(1), 10) // Claim A + n.AdjustTo(13) + // Claim A for 10LBC is accepted. It is the first claim, so it immediately becomes active and controlling. + assert.Equal(t, claimA, n.BestClaim()) // A(10) is controlling. + + n.AdjustTo(1000) + n.AddClaim(*newOutPoint(2), 20) // Claim B + n.AdjustTo(1001) + // Claim B for 20LBC is accepted. It’s activation height is 1001+min(4032,floor(1001−1332))=1001+30=1031 + assert.Equal(t, claimA, n.BestClaim()) // A(10) is controlling, B(20) is accepted. + + n.AdjustTo(1009) + n.AddSupport(*newOutPoint(1001), 14, claimA.ID) // Support X + n.AdjustTo(1010) + // Support X for 14LBC for claim A is accepted. Since it is a support for the controlling claim, it activates immediately. + assert.Equal(t, claimA, n.BestClaim()) // A(10+14) is controlling, B(20) is accepted. + + n.AdjustTo(1019) + n.AddClaim(*newOutPoint(3), 50) // Claim C + n.AdjustTo(1020) + // Claim C for 50LBC is accepted. The activation height is 1020+min(4032,floor(1020−1332))=1020+31=1051 + assert.Equal(t, claimA, n.BestClaim()) // A(10+14) is controlling, B(20) is accepted, C(50) is accepted. + + n.AdjustTo(1031) + // Claim B activates. It has 20LBC, while claim A has 24LBC (10 original + 14 from support X). There is no takeover, and claim A remains controlling. + assert.Equal(t, claimA, n.BestClaim()) // A(10+14) is controlling, B(20) is active, C(50) is accepted. + + n.AdjustTo(1039) + claimD, _ := n.AddClaim(*newOutPoint(4), 300) // Claim D + n.AdjustTo(1040) + // Claim D for 300LBC is accepted. The activation height is 1040+min(4032,floor(1040−1332))=1040+32=1072 + assert.Equal(t, claimA, n.BestClaim()) //A(10+14) is controlling, B(20) is active, C(50) is accepted, D(300) is accepted. + + n.AdjustTo(1051) + // Claim C activates. It has 50LBC, while claim A has 24LBC, so a takeover is initiated. + // The takeover height for this name is set to 1051, and therefore the activation delay for all the claims becomes min(4032, floor(1051−1051/32)) = 0. + // All the claims become active. + // The totals for each claim are recalculated, and claim D becomes controlling because it has the highest total. + assert.Equal(t, claimD, n.BestClaim()) // A(10+14) is active, B(20) is active, C(50) is active, D(300) is controlling. + + // The following are the corresponding commands in the CLI to reproduce the test. + // Note that when a Toakeover happens, the {TakeoverHeight, BestClaim(OutPoint:Indx)} is pushed to the BestClaims stack. + // This provides sufficient info to update and backtracking the BestClaim at any height. + + // claimtrie > c -ht 12 + // claimtrie > ac -a 10 + // claimtrie > c -ht 13 + // claimtrie > s + // + // + // Hello : Height 13, 6f5970c9c13f00c77054d98e5b2c50b1a1bb723d91676cc03f984fac763ec6c3 BestClaims: {13, 31}, + // + // C-2f9b2ca28b30c97122de584e8d784e784bc7bdfb43b3b4bf9de69fc31196571f:31 amt: 10 effamt: 10 accepted: 13 active: 13 id: ae8b3adc8c8b378c76eae12edf3878357b31c0eb + + // claimtrie > c -ht 1000 + // claimtrie > ac -a 20 + // claimtrie > c -ht 1001 + // claimtrie > s + // + // + // Hello : Height 1000, 6f5970c9c13f00c77054d98e5b2c50b1a1bb723d91676cc03f984fac763ec6c3 BestClaims: {13, 31}, + // + // C-2f9b2ca28b30c97122de584e8d784e784bc7bdfb43b3b4bf9de69fc31196571f:31 amt: 10 effamt: 10 accepted: 13 active: 13 id: ae8b3adc8c8b378c76eae12edf3878357b31c0eb + // C-74fd7c15b445e7ac2de49a8058ad7dbb070ef31838345feff168dd40c2ef422e:46 amt: 20 effamt: 0 accepted: 1001 active: 1031 id: 2edc6338e9f3654f8b2b878817e029a2b2ecfa9e + + // claimtrie > c -ht 1009 + // claimtrie > as -a 14 -id ae8b3adc8c8b378c76eae12edf3878357b31c0eb + // claimtrie > c -ht 1010 + // claimtrie > s + // + // + // Hello : Height 1010, 6f5970c9c13f00c77054d98e5b2c50b1a1bb723d91676cc03f984fac763ec6c3 BestClaims: {13, 31}, + // + // C-2f9b2ca28b30c97122de584e8d784e784bc7bdfb43b3b4bf9de69fc31196571f:31 amt: 10 effamt: 24 accepted: 13 active: 13 id: ae8b3adc8c8b378c76eae12edf3878357b31c0eb + // C-74fd7c15b445e7ac2de49a8058ad7dbb070ef31838345feff168dd40c2ef422e:46 amt: 20 effamt: 0 accepted: 1001 active: 1031 id: 2edc6338e9f3654f8b2b878817e029a2b2ecfa9e + // + // S-087acb4f22ab6eb2e6c827624deab7beb02c190056376e0b3a3c3546b79bf216:22 amt: 14 accepted: 1010 active: 1010 id: ae8b3adc8c8b378c76eae12edf3878357b31c0eb + + // claimtrie > c -ht 1019 + // claimtrie > ac -a 50 + // claimtrie > c -ht 1020 + // claimtrie > s + // + // + // Hello : Height 1019, 6f5970c9c13f00c77054d98e5b2c50b1a1bb723d91676cc03f984fac763ec6c3 BestClaims: {13, 31}, + // + // C-2f9b2ca28b30c97122de584e8d784e784bc7bdfb43b3b4bf9de69fc31196571f:31 amt: 10 effamt: 24 accepted: 13 active: 13 id: ae8b3adc8c8b378c76eae12edf3878357b31c0eb + // C-74fd7c15b445e7ac2de49a8058ad7dbb070ef31838345feff168dd40c2ef422e:46 amt: 20 effamt: 0 accepted: 1001 active: 1031 id: 2edc6338e9f3654f8b2b878817e029a2b2ecfa9e + // C-864dc683ed1fbe3c072c2387bca7d74e60c2505f1987054fd70955a2e1c9490b:11 amt: 50 effamt: 0 accepted: 1020 active: 1051 id: 0f2ba15a5c66b978df4a27f52525680a6009185e + // + // S-087acb4f22ab6eb2e6c827624deab7beb02c190056376e0b3a3c3546b79bf216:22 amt: 14 accepted: 1010 active: 1010 id: ae8b3adc8c8b378c76eae12edf3878357b31c0eb + + // claimtrie > c -ht 1031 + // claimtrie > s + // + // + // Hello : Height 1031, 6f5970c9c13f00c77054d98e5b2c50b1a1bb723d91676cc03f984fac763ec6c3 BestClaims: {13, 31}, + // + // C-2f9b2ca28b30c97122de584e8d784e784bc7bdfb43b3b4bf9de69fc31196571f:31 amt: 10 effamt: 24 accepted: 13 active: 13 id: ae8b3adc8c8b378c76eae12edf3878357b31c0eb + // C-74fd7c15b445e7ac2de49a8058ad7dbb070ef31838345feff168dd40c2ef422e:46 amt: 20 effamt: 20 accepted: 1001 active: 1031 id: 2edc6338e9f3654f8b2b878817e029a2b2ecfa9e + // C-864dc683ed1fbe3c072c2387bca7d74e60c2505f1987054fd70955a2e1c9490b:11 amt: 50 effamt: 0 accepted: 1020 active: 1051 id: 0f2ba15a5c66b978df4a27f52525680a6009185e + // + // S-087acb4f22ab6eb2e6c827624deab7beb02c190056376e0b3a3c3546b79bf216:22 amt: 14 accepted: 1010 active: 1010 id: ae8b3adc8c8b378c76eae12edf3878357b31c0eb + + // claimtrie > c -ht 1039 + // claimtrie > ac -a 300 + // claimtrie > c -ht 1040 + // claimtrie > s + // + // + // Hello : Height 1039, 6f5970c9c13f00c77054d98e5b2c50b1a1bb723d91676cc03f984fac763ec6c3 BestClaims: {13, 31}, + // + // C-2f9b2ca28b30c97122de584e8d784e784bc7bdfb43b3b4bf9de69fc31196571f:31 amt: 10 effamt: 24 accepted: 13 active: 13 id: ae8b3adc8c8b378c76eae12edf3878357b31c0eb + // C-74fd7c15b445e7ac2de49a8058ad7dbb070ef31838345feff168dd40c2ef422e:46 amt: 20 effamt: 20 accepted: 1001 active: 1031 id: 2edc6338e9f3654f8b2b878817e029a2b2ecfa9e + // C-864dc683ed1fbe3c072c2387bca7d74e60c2505f1987054fd70955a2e1c9490b:11 amt: 50 effamt: 0 accepted: 1020 active: 1051 id: 0f2ba15a5c66b978df4a27f52525680a6009185e + // C-26292b27122d04d08fee4e4cc5a5f94681832204cc29d61039c09af9a5298d16:22 amt: 300 effamt: 0 accepted: 1040 active: 1072 id: 270496c0710e525156510e60e4be2ffa6fe2f507 + // + // S-087acb4f22ab6eb2e6c827624deab7beb02c190056376e0b3a3c3546b79bf216:22 amt: 14 accepted: 1010 active: 1010 id: ae8b3adc8c8b378c76eae12edf3878357b31c0eb + + // claimtrie > c -ht 1051 + // claimtrie > s + // + // + // Hello : Height 1051, 68dff86c9450e3cf96570f31b6ad8f8d35ae0cbce6cdcb3761910e25a815ee8b BestClaims: {13, 31}, {1051, 22}, + // + // C-2f9b2ca28b30c97122de584e8d784e784bc7bdfb43b3b4bf9de69fc31196571f:31 amt: 10 effamt: 24 accepted: 13 active: 13 id: ae8b3adc8c8b378c76eae12edf3878357b31c0eb + // C-74fd7c15b445e7ac2de49a8058ad7dbb070ef31838345feff168dd40c2ef422e:46 amt: 20 effamt: 20 accepted: 1001 active: 1001 id: 2edc6338e9f3654f8b2b878817e029a2b2ecfa9e + // C-864dc683ed1fbe3c072c2387bca7d74e60c2505f1987054fd70955a2e1c9490b:11 amt: 50 effamt: 50 accepted: 1020 active: 1020 id: 0f2ba15a5c66b978df4a27f52525680a6009185e + // C-26292b27122d04d08fee4e4cc5a5f94681832204cc29d61039c09af9a5298d16:22 amt: 300 effamt: 300 accepted: 1040 active: 1040 id: 270496c0710e525156510e60e4be2ffa6fe2f507 + // + // S-087acb4f22ab6eb2e6c827624deab7beb02c190056376e0b3a3c3546b79bf216:22 amt: 14 accepted: 1010 active: 1010 id: ae8b3adc8c8b378c76eae12edf3878357b31c0eb +} + var c1, c2, c3, c4, c5, c6, c7, c8, c9, c10 *claim.Claim var s1, s2, s3, s4, s5, s6, s7, s8, s9, s10 *claim.Support @@ -279,6 +419,7 @@ func Test_History1(t *testing.T) { assert.Equal(t, c2, n.BestClaim()) } + tests := []func(){ test1, test2,