github.com/lbryio/lbcd@v0.22.119/claimtrie/node/manager_test.go (about) 1 package node 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/lbryio/lbcd/claimtrie/change" 8 "github.com/lbryio/lbcd/claimtrie/node/noderepo" 9 "github.com/lbryio/lbcd/claimtrie/param" 10 "github.com/lbryio/lbcd/wire" 11 12 "github.com/stretchr/testify/require" 13 ) 14 15 var ( 16 out1 = NewOutPointFromString("0000000000000000000000000000000000000000000000000000000000000000:1") 17 out2 = NewOutPointFromString("0000000000000000000000000000000000000000000000000000000000000000:2") 18 out3 = NewOutPointFromString("0100000000000000000000000000000000000000000000000000000000000000:1") 19 out4 = NewOutPointFromString("0100000000000000000000000000000000000000000000000000000000000000:2") 20 name1 = []byte("name1") 21 name2 = []byte("name2") 22 ) 23 24 // verify that we can round-trip bytes to strings 25 func TestStringRoundTrip(t *testing.T) { 26 27 r := require.New(t) 28 29 data := [][]byte{ 30 {97, 98, 99, 0, 100, 255}, 31 {0xc3, 0x28}, 32 {0xa0, 0xa1}, 33 {0xe2, 0x28, 0xa1}, 34 {0xf0, 0x28, 0x8c, 0x28}, 35 } 36 for _, d := range data { 37 s := string(d) 38 r.Equal(s, fmt.Sprintf("%s", d)) // nolint 39 d2 := []byte(s) 40 r.Equal(len(d), len(s)) 41 r.Equal(d, d2) 42 } 43 } 44 45 func TestSimpleAddClaim(t *testing.T) { 46 47 r := require.New(t) 48 49 param.SetNetwork(wire.TestNet) 50 repo, err := noderepo.NewPebble(t.TempDir()) 51 r.NoError(err) 52 53 m, err := NewBaseManager(repo) 54 r.NoError(err) 55 defer m.Close() 56 57 _, err = m.IncrementHeightTo(10, false) 58 r.NoError(err) 59 60 chg := change.NewChange(change.AddClaim).SetName(name1).SetOutPoint(out1).SetHeight(11) 61 m.AppendChange(chg) 62 _, err = m.IncrementHeightTo(11, false) 63 r.NoError(err) 64 65 chg = chg.SetName(name2).SetOutPoint(out2).SetHeight(12) 66 m.AppendChange(chg) 67 _, err = m.IncrementHeightTo(12, false) 68 r.NoError(err) 69 70 n1, err := m.node(name1) 71 r.NoError(err) 72 r.Equal(1, len(n1.Claims)) 73 r.NotNil(n1.Claims.find(byOut(*out1))) 74 75 n2, err := m.node(name2) 76 r.NoError(err) 77 r.Equal(1, len(n2.Claims)) 78 r.NotNil(n2.Claims.find(byOut(*out2))) 79 80 _, err = m.DecrementHeightTo([][]byte{name2}, 11) 81 r.NoError(err) 82 n2, err = m.node(name2) 83 r.NoError(err) 84 r.Nil(n2) 85 86 _, err = m.DecrementHeightTo([][]byte{name1}, 1) 87 r.NoError(err) 88 n2, err = m.node(name1) 89 r.NoError(err) 90 r.Nil(n2) 91 } 92 93 func TestSupportAmounts(t *testing.T) { 94 95 r := require.New(t) 96 97 param.SetNetwork(wire.TestNet) 98 repo, err := noderepo.NewPebble(t.TempDir()) 99 r.NoError(err) 100 101 m, err := NewBaseManager(repo) 102 r.NoError(err) 103 defer m.Close() 104 105 _, err = m.IncrementHeightTo(10, false) 106 r.NoError(err) 107 108 chg := change.NewChange(change.AddClaim).SetName(name1).SetOutPoint(out1).SetHeight(11).SetAmount(3) 109 chg.ClaimID = change.NewClaimID(*out1) 110 m.AppendChange(chg) 111 112 chg = change.NewChange(change.AddClaim).SetName(name1).SetOutPoint(out2).SetHeight(11).SetAmount(4) 113 chg.ClaimID = change.NewClaimID(*out2) 114 m.AppendChange(chg) 115 116 _, err = m.IncrementHeightTo(11, false) 117 r.NoError(err) 118 119 chg = change.NewChange(change.AddSupport).SetName(name1).SetOutPoint(out3).SetHeight(12).SetAmount(2) 120 chg.ClaimID = change.NewClaimID(*out1) 121 m.AppendChange(chg) 122 123 chg = change.NewChange(change.AddSupport).SetName(name1).SetOutPoint(out4).SetHeight(12).SetAmount(2) 124 chg.ClaimID = change.NewClaimID(*out2) 125 m.AppendChange(chg) 126 127 chg = change.NewChange(change.SpendSupport).SetName(name1).SetOutPoint(out4).SetHeight(12).SetAmount(2) 128 chg.ClaimID = change.NewClaimID(*out2) 129 m.AppendChange(chg) 130 131 _, err = m.IncrementHeightTo(20, false) 132 r.NoError(err) 133 134 n1, err := m.node(name1) 135 r.NoError(err) 136 r.Equal(2, len(n1.Claims)) 137 r.Equal(int64(5), n1.BestClaim.Amount+n1.SupportSums[n1.BestClaim.ClaimID.Key()]) 138 } 139 140 func TestNodeSort(t *testing.T) { 141 142 r := require.New(t) 143 144 param.ActiveParams.ExtendedClaimExpirationTime = 1000 145 146 r.True(OutPointLess(*out1, *out2)) 147 r.True(OutPointLess(*out1, *out3)) 148 149 n := New() 150 n.Claims = append(n.Claims, &Claim{OutPoint: *out1, AcceptedAt: 3, Amount: 3, ClaimID: change.ClaimID{1}}) 151 n.Claims = append(n.Claims, &Claim{OutPoint: *out2, AcceptedAt: 3, Amount: 3, ClaimID: change.ClaimID{2}}) 152 n.handleExpiredAndActivated(3) 153 n.updateTakeoverHeight(3, []byte{}, true) 154 155 r.Equal(n.Claims.find(byOut(*out1)).OutPoint.String(), n.BestClaim.OutPoint.String()) 156 157 n.Claims = append(n.Claims, &Claim{OutPoint: *out3, AcceptedAt: 3, Amount: 3, ClaimID: change.ClaimID{3}}) 158 n.handleExpiredAndActivated(3) 159 n.updateTakeoverHeight(3, []byte{}, true) 160 r.Equal(n.Claims.find(byOut(*out1)).OutPoint.String(), n.BestClaim.OutPoint.String()) 161 } 162 163 func TestClaimSort(t *testing.T) { 164 165 r := require.New(t) 166 167 param.ActiveParams.ExtendedClaimExpirationTime = 1000 168 169 n := New() 170 n.Claims = append(n.Claims, &Claim{OutPoint: *out2, AcceptedAt: 3, Amount: 3, ClaimID: change.ClaimID{2}, Status: Activated}) 171 n.Claims = append(n.Claims, &Claim{OutPoint: *out3, AcceptedAt: 3, Amount: 2, ClaimID: change.ClaimID{3}, Status: Activated}) 172 n.Claims = append(n.Claims, &Claim{OutPoint: *out3, AcceptedAt: 4, Amount: 2, ClaimID: change.ClaimID{4}, Status: Activated}) 173 n.Claims = append(n.Claims, &Claim{OutPoint: *out1, AcceptedAt: 3, Amount: 4, ClaimID: change.ClaimID{1}, Status: Activated}) 174 n.Claims = append(n.Claims, &Claim{OutPoint: *out1, AcceptedAt: 1, Amount: 9, ClaimID: change.ClaimID{5}, Status: Accepted}) 175 n.SortClaimsByBid() 176 177 r.Equal(int64(4), n.Claims[0].Amount) 178 r.Equal(int64(3), n.Claims[1].Amount) 179 r.Equal(int64(2), n.Claims[2].Amount) 180 r.Equal(int32(4), n.Claims[3].AcceptedAt) 181 } 182 183 func TestHasChildren(t *testing.T) { 184 r := require.New(t) 185 186 param.SetNetwork(wire.TestNet) 187 repo, err := noderepo.NewPebble(t.TempDir()) 188 r.NoError(err) 189 190 m, err := NewBaseManager(repo) 191 r.NoError(err) 192 defer m.Close() 193 194 chg := change.NewChange(change.AddClaim).SetName([]byte("a")).SetOutPoint(out1).SetHeight(1).SetAmount(2) 195 chg.ClaimID = change.NewClaimID(*out1) 196 m.AppendChange(chg) 197 _, err = m.IncrementHeightTo(1, false) 198 r.NoError(err) 199 r.False(m.hasChildren([]byte("a"), 1, nil, 1)) 200 201 chg = change.NewChange(change.AddClaim).SetName([]byte("ab")).SetOutPoint(out2).SetHeight(2).SetAmount(2) 202 chg.ClaimID = change.NewClaimID(*out2) 203 m.AppendChange(chg) 204 _, err = m.IncrementHeightTo(2, false) 205 r.NoError(err) 206 r.False(m.hasChildren([]byte("a"), 2, nil, 2)) 207 r.True(m.hasChildren([]byte("a"), 2, nil, 1)) 208 209 chg = change.NewChange(change.AddClaim).SetName([]byte("abc")).SetOutPoint(out3).SetHeight(3).SetAmount(2) 210 chg.ClaimID = change.NewClaimID(*out3) 211 m.AppendChange(chg) 212 _, err = m.IncrementHeightTo(3, false) 213 r.NoError(err) 214 r.False(m.hasChildren([]byte("a"), 3, nil, 2)) 215 216 chg = change.NewChange(change.AddClaim).SetName([]byte("ac")).SetOutPoint(out1).SetHeight(4).SetAmount(2) 217 chg.ClaimID = change.NewClaimID(*out4) 218 m.AppendChange(chg) 219 _, err = m.IncrementHeightTo(4, false) 220 r.NoError(err) 221 r.True(m.hasChildren([]byte("a"), 4, nil, 2)) 222 } 223 224 func TestCollectChildren(t *testing.T) { 225 r := require.New(t) 226 227 c1 := change.Change{Name: []byte("ba"), Type: change.SpendClaim} 228 c2 := change.Change{Name: []byte("ba"), Type: change.UpdateClaim} 229 c3 := change.Change{Name: []byte("ac"), Type: change.SpendClaim} 230 c4 := change.Change{Name: []byte("ac"), Type: change.UpdateClaim} 231 c5 := change.Change{Name: []byte("a"), Type: change.SpendClaim} 232 c6 := change.Change{Name: []byte("a"), Type: change.UpdateClaim} 233 c7 := change.Change{Name: []byte("ab"), Type: change.SpendClaim} 234 c8 := change.Change{Name: []byte("ab"), Type: change.UpdateClaim} 235 c := []change.Change{c1, c2, c3, c4, c5, c6, c7, c8} 236 237 collectChildNames(c) 238 239 r.Empty(c[0].SpentChildren) 240 r.Empty(c[2].SpentChildren) 241 r.Empty(c[4].SpentChildren) 242 r.Empty(c[6].SpentChildren) 243 244 r.Len(c[1].SpentChildren, 0) 245 r.Len(c[3].SpentChildren, 0) 246 r.Len(c[5].SpentChildren, 1) 247 r.True(c[5].SpentChildren["ac"]) 248 249 r.Len(c[7].SpentChildren, 0) 250 } 251 252 func TestTemporaryAddClaim(t *testing.T) { 253 254 r := require.New(t) 255 256 param.SetNetwork(wire.TestNet) 257 repo, err := noderepo.NewPebble(t.TempDir()) 258 r.NoError(err) 259 260 m, err := NewBaseManager(repo) 261 r.NoError(err) 262 defer m.Close() 263 264 _, err = m.IncrementHeightTo(10, false) 265 r.NoError(err) 266 267 chg := change.NewChange(change.AddClaim).SetName(name1).SetOutPoint(out1).SetHeight(11) 268 m.AppendChange(chg) 269 _, err = m.IncrementHeightTo(11, false) 270 r.NoError(err) 271 272 chg = chg.SetName(name2).SetOutPoint(out2).SetHeight(12) 273 m.AppendChange(chg) 274 _, err = m.IncrementHeightTo(12, true) 275 r.NoError(err) 276 277 n1, err := m.node(name1) 278 r.NoError(err) 279 r.Equal(1, len(n1.Claims)) 280 r.NotNil(n1.Claims.find(byOut(*out1))) 281 282 n2, err := m.node(name2) 283 r.NoError(err) 284 r.Equal(1, len(n2.Claims)) 285 r.NotNil(n2.Claims.find(byOut(*out2))) 286 287 names, err := m.DecrementHeightTo([][]byte{name2}, 11) 288 r.Equal(names[0], name2) 289 r.NoError(err) 290 n2, err = m.node(name2) 291 r.NoError(err) 292 r.Nil(n2) 293 294 _, err = m.DecrementHeightTo([][]byte{name1}, 1) 295 r.NoError(err) 296 n2, err = m.node(name1) 297 r.NoError(err) 298 r.Nil(n2) 299 }