mirror of
https://github.com/kaspanet/kaspad.git
synced 2026-03-22 16:13:45 +00:00
Compare commits
552 Commits
bignet-deb
...
v0.10.1-de
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9e0b50c0dd | ||
|
|
b405ea50e5 | ||
|
|
ccfe8a45dd | ||
|
|
9dd8136e4b | ||
|
|
eb1703b948 | ||
|
|
a6da3251d0 | ||
|
|
bf198948c4 | ||
|
|
dfd8b3423d | ||
|
|
28bfc0fb9c | ||
|
|
83beae4463 | ||
|
|
a6ebe83198 | ||
|
|
acdc59b565 | ||
|
|
15811b0bcb | ||
|
|
a8a7e3dd9b | ||
|
|
3f193e9219 | ||
|
|
dfa24d8353 | ||
|
|
3c3ad1425d | ||
|
|
9bb8123391 | ||
|
|
347dd8fc4b | ||
|
|
d2cccd2829 | ||
|
|
7186f83095 | ||
|
|
5c394c2951 | ||
|
|
a786cdc15e | ||
|
|
6dd3d4a9e7 | ||
|
|
73b36f12f0 | ||
|
|
a795a9e619 | ||
|
|
0be1bba408 | ||
|
|
6afc06ce58 | ||
|
|
d01a213f3d | ||
|
|
7ad8ce521c | ||
|
|
86ba80a091 | ||
|
|
088e2114c2 | ||
|
|
2854d91688 | ||
|
|
af10b59181 | ||
|
|
c5b0394bbc | ||
|
|
9266d179a9 | ||
|
|
321792778e | ||
|
|
70f3fa9893 | ||
|
|
4e18031483 | ||
|
|
2abc284e3b | ||
|
|
f1451406f7 | ||
|
|
c12e180873 | ||
|
|
3959bc1e7c | ||
|
|
6ec0a8a559 | ||
|
|
6824be9216 | ||
|
|
d0511c1636 | ||
|
|
7d69b66c7c | ||
|
|
cebcab7f5c | ||
|
|
caf251b7a8 | ||
|
|
1a4161ffc0 | ||
|
|
b84080f3d9 | ||
|
|
cbd0bb6d14 | ||
|
|
d9449a32b8 | ||
|
|
0ee8f2b631 | ||
|
|
ff1c96c149 | ||
|
|
5e335be5ab | ||
|
|
032eda4604 | ||
|
|
e4e3541a30 | ||
|
|
b5933bc4fe | ||
|
|
ec446ac511 | ||
|
|
3d668cc1bd | ||
|
|
1486a6312c | ||
|
|
cd27f2850e | ||
|
|
14cf7f81f3 | ||
|
|
74539f8f0b | ||
|
|
a7299c1b87 | ||
|
|
27c1e4611e | ||
|
|
b8413fcecb | ||
|
|
c084c69771 | ||
|
|
53781eed4d | ||
|
|
837fa65735 | ||
|
|
dd3b2cf7d1 | ||
|
|
3fd324ca28 | ||
|
|
0271858f25 | ||
|
|
7909480757 | ||
|
|
18274c236b | ||
|
|
7829a9fd76 | ||
|
|
1548ed9629 | ||
|
|
32cd643e8b | ||
|
|
05df6e3e4e | ||
|
|
ce326b3c7d | ||
|
|
a4ae263ed5 | ||
|
|
79be1edaa5 | ||
|
|
c1ef5f0c56 | ||
|
|
458e409654 | ||
|
|
df19bdfaf3 | ||
|
|
103edf97d0 | ||
|
|
1f69f9eed9 | ||
|
|
6a12428504 | ||
|
|
63b1d2a05a | ||
|
|
089115c389 | ||
|
|
24a12cf2a1 | ||
|
|
6597f24bab | ||
|
|
dc84913214 | ||
|
|
201646282b | ||
|
|
3f08bf87a8 | ||
|
|
581a12db96 | ||
|
|
fb6c9c8f21 | ||
|
|
2adb4f5d0f | ||
|
|
9ffda2b1da | ||
|
|
bee0893660 | ||
|
|
a7bb1853f9 | ||
|
|
35e555e959 | ||
|
|
a250f697ee | ||
|
|
f66708b3c6 | ||
|
|
8fbea5d239 | ||
|
|
5fa06fe7d7 | ||
|
|
06fd6f1b95 | ||
|
|
d2f4ed660c | ||
|
|
19878aa062 | ||
|
|
6415e525c3 | ||
|
|
995e526dae | ||
|
|
00a023620d | ||
|
|
2908a46441 | ||
|
|
e78cdff3d0 | ||
|
|
2a31074fc4 | ||
|
|
d835f72e74 | ||
|
|
a581dea127 | ||
|
|
7b4b5668e2 | ||
|
|
0e2061d838 | ||
|
|
0a579e7f78 | ||
|
|
1ed6c4c086 | ||
|
|
fea83e5c6c | ||
|
|
7c3beb526e | ||
|
|
171deded4e | ||
|
|
94cdc77481 | ||
|
|
1222a555f2 | ||
|
|
f13fc35b9e | ||
|
|
2d61a67592 | ||
|
|
3a4fa6e0e1 | ||
|
|
2edf6bfd07 | ||
|
|
8225f7fb3c | ||
|
|
3d0a2a47b2 | ||
|
|
4a354cd538 | ||
|
|
1a3b16aaa3 | ||
|
|
5b5a7e60af | ||
|
|
d30f05b250 | ||
|
|
6bc7a4eb85 | ||
|
|
608d1f8ef9 | ||
|
|
a792d4a19e | ||
|
|
8941c518fc | ||
|
|
6f53da18b1 | ||
|
|
44280b9006 | ||
|
|
dbababb978 | ||
|
|
238950cb98 | ||
|
|
ee8fa32ff8 | ||
|
|
e7f9606683 | ||
|
|
97be133cee | ||
|
|
aeb8e9d2cd | ||
|
|
b636ae234e | ||
|
|
a3913dbf80 | ||
|
|
2871a6a527 | ||
|
|
d5a3a96bde | ||
|
|
12c438d389 | ||
|
|
280fa3de46 | ||
|
|
d281dabdb4 | ||
|
|
331042edf1 | ||
|
|
669a9ab4c3 | ||
|
|
65e149b2bb | ||
|
|
7c1495ba65 | ||
|
|
13ffa5093c | ||
|
|
a9a810a2b2 | ||
|
|
c9b591f2d3 | ||
|
|
8d6e71d490 | ||
|
|
2823461fe2 | ||
|
|
2075c585da | ||
|
|
01aee62cb0 | ||
|
|
a6ee871f7e | ||
|
|
6393a8186a | ||
|
|
3916534a7e | ||
|
|
0561347ff1 | ||
|
|
fb11981da1 | ||
|
|
1742e76af7 | ||
|
|
ddfe376388 | ||
|
|
52da65077a | ||
|
|
ed85f09742 | ||
|
|
819ec9f2a7 | ||
|
|
7ea8a72a9e | ||
|
|
ca04c049ab | ||
|
|
9a17198e7d | ||
|
|
756f40c59a | ||
|
|
6a03d31f98 | ||
|
|
319ab6cfcd | ||
|
|
abef96e3de | ||
|
|
2e0bc0f8c4 | ||
|
|
acf5423c63 | ||
|
|
effb545d20 | ||
|
|
ad9c213a06 | ||
|
|
a4adbabf96 | ||
|
|
799eb7515c | ||
|
|
0769705b37 | ||
|
|
189e3b6be9 | ||
|
|
e8dfbc8367 | ||
|
|
d70740331a | ||
|
|
9a81b1328a | ||
|
|
d4f3a252ff | ||
|
|
f14527de4c | ||
|
|
dd57e6abe6 | ||
|
|
67be4d82bf | ||
|
|
a1381d6768 | ||
|
|
10b519a3e2 | ||
|
|
a35f8269ea | ||
|
|
15af6641fc | ||
|
|
1b97cfb302 | ||
|
|
61be80a60c | ||
|
|
83134cc2b5 | ||
|
|
4988817da1 | ||
|
|
68bd8330ac | ||
|
|
192dd2ba8f | ||
|
|
cc49b1826a | ||
|
|
ce348373c6 | ||
|
|
8ad5725421 | ||
|
|
23a2fbf401 | ||
|
|
c1361e5b3e | ||
|
|
53744ceb45 | ||
|
|
bcf2302460 | ||
|
|
6101e6bdb6 | ||
|
|
d9b97afb92 | ||
|
|
b8ca33d91d | ||
|
|
c7deda41c6 | ||
|
|
434cf45112 | ||
|
|
2cc0bf1639 | ||
|
|
0f2d0d45b5 | ||
|
|
09e1a73340 | ||
|
|
c6d20c1f6f | ||
|
|
49e0a2a2e7 | ||
|
|
285ae5cd40 | ||
|
|
541205904e | ||
|
|
256b7f25f1 | ||
|
|
82d95c59a3 | ||
|
|
79c5d4595e | ||
|
|
b195301a99 | ||
|
|
4ad89056c2 | ||
|
|
64f6cd2178 | ||
|
|
26368cd674 | ||
|
|
9ea4c0fa38 | ||
|
|
a04a5462ae | ||
|
|
4577023e44 | ||
|
|
d8293ef635 | ||
|
|
2059d6ba56 | ||
|
|
3ec1cbe236 | ||
|
|
741e0962be | ||
|
|
6279db2bf1 | ||
|
|
e24bc527f3 | ||
|
|
8a309a7d2a | ||
|
|
70d515a5a9 | ||
|
|
72a7ca53e6 | ||
|
|
119e7374e1 | ||
|
|
e509cb1597 | ||
|
|
0fb97a4f37 | ||
|
|
789a7379bd | ||
|
|
778375c4af | ||
|
|
acef311fb4 | ||
|
|
e8cad2b2f3 | ||
|
|
97fddeff4b | ||
|
|
d6fe9a3017 | ||
|
|
1abffd472c | ||
|
|
8c8da3b01f | ||
|
|
51625e7967 | ||
|
|
6fa3aa1dca | ||
|
|
23304a4977 | ||
|
|
b473a09111 | ||
|
|
dd35669861 | ||
|
|
9401b77a4f | ||
|
|
e87368157d | ||
|
|
7dd0188838 | ||
|
|
6172e48adc | ||
|
|
739cffd918 | ||
|
|
bd89ca2125 | ||
|
|
d917a1fc1e | ||
|
|
9b12b9c58a | ||
|
|
533fa8c00e | ||
|
|
0f93189c16 | ||
|
|
52427cb953 | ||
|
|
d72f70fabe | ||
|
|
49b6cc6038 | ||
|
|
c10a087696 | ||
|
|
02d5fb29cf | ||
|
|
48278bd1c0 | ||
|
|
d91afbfe3b | ||
|
|
5f22632836 | ||
|
|
4aafe8a630 | ||
|
|
7e379028f3 | ||
|
|
af1b8c8490 | ||
|
|
c7c8b25c09 | ||
|
|
b0251fe1a6 | ||
|
|
cfe013eca7 | ||
|
|
50e74bf412 | ||
|
|
12f1c3dfab | ||
|
|
a231ec7214 | ||
|
|
8aecf961bc | ||
|
|
0dea766373 | ||
|
|
830ddf4735 | ||
|
|
9d0f513e49 | ||
|
|
bd97075e07 | ||
|
|
05941a76e7 | ||
|
|
7cbda3b018 | ||
|
|
a0b93e1230 | ||
|
|
b749b2db0b | ||
|
|
717914319a | ||
|
|
6ef8eaf133 | ||
|
|
273c271771 | ||
|
|
729e3db145 | ||
|
|
43c00f5e7f | ||
|
|
90d4dbcba1 | ||
|
|
cb9d7e313d | ||
|
|
c2cec2f170 | ||
|
|
e7edfaceb7 | ||
|
|
5632bee49d | ||
|
|
21a459c0f4 | ||
|
|
45edacfbfa | ||
|
|
9f8f0fd747 | ||
|
|
053bb351b5 | ||
|
|
843edc4ba5 | ||
|
|
bd5f4e8c6a | ||
|
|
6b1e691a57 | ||
|
|
bf67c6351e | ||
|
|
99a14c5999 | ||
|
|
b510fc08a7 | ||
|
|
dc3ae4d3ac | ||
|
|
1ebda36b17 | ||
|
|
12379bedb6 | ||
|
|
f90d7d796a | ||
|
|
fddda46d4f | ||
|
|
77adb6c99f | ||
|
|
48e1a2c396 | ||
|
|
6926a7ab81 | ||
|
|
a9e0c33e5c | ||
|
|
2c1688909d | ||
|
|
6714e084e9 | ||
|
|
3354ac67c8 | ||
|
|
0d8f7bba40 | ||
|
|
e04f76b800 | ||
|
|
82fa8e6831 | ||
|
|
b7ca3f4461 | ||
|
|
37bf261da1 | ||
|
|
d90e18ec51 | ||
|
|
9962527793 | ||
|
|
78550d3639 | ||
|
|
7f899b0d09 | ||
|
|
4886425caf | ||
|
|
c3902ed7a8 | ||
|
|
33eaf9edac | ||
|
|
f97b8f7580 | ||
|
|
05979de705 | ||
|
|
32a04d1811 | ||
|
|
a585f32763 | ||
|
|
9866abb75a | ||
|
|
ab3c81c552 | ||
|
|
9756d64f28 | ||
|
|
21fc2d4219 | ||
|
|
f1c6df48c9 | ||
|
|
80c445c78b | ||
|
|
3b6eb73e53 | ||
|
|
f407c44a8d | ||
|
|
a1af992d15 | ||
|
|
048caebda3 | ||
|
|
baa4311a34 | ||
|
|
f6dfce8180 | ||
|
|
0e91b44fc6 | ||
|
|
f7fa823f17 | ||
|
|
546ea83123 | ||
|
|
f9c2137344 | ||
|
|
0fa13357c3 | ||
|
|
5b2fae0457 | ||
|
|
3bad9ec1eb | ||
|
|
45d9b63572 | ||
|
|
afc634d871 | ||
|
|
2334f8b4eb | ||
|
|
d65f382c80 | ||
|
|
2096a28d1c | ||
|
|
96d9e5800f | ||
|
|
8264369c81 | ||
|
|
bb2d7f72ac | ||
|
|
c1505b4748 | ||
|
|
dec9ef5f75 | ||
|
|
5211727206 | ||
|
|
fafe1d534f | ||
|
|
c56a5336f3 | ||
|
|
b3a3121725 | ||
|
|
950dd0cc8d | ||
|
|
bb244706ea | ||
|
|
ed386bbc8f | ||
|
|
75d21d39cc | ||
|
|
3f92ddd827 | ||
|
|
8500acd86b | ||
|
|
5b037950d8 | ||
|
|
184911f76e | ||
|
|
7479f5f5e8 | ||
|
|
891095563e | ||
|
|
60c24d8dea | ||
|
|
d4993c1d06 | ||
|
|
c785ca0e52 | ||
|
|
9eb5c4a0ed | ||
|
|
9d5d1b02dc | ||
|
|
213be67c47 | ||
|
|
14d7ab5fc6 | ||
|
|
7224d58940 | ||
|
|
b2188f5993 | ||
|
|
dbd15aecf5 | ||
|
|
66f5a5bd7d | ||
|
|
c994200878 | ||
|
|
7050ebeac9 | ||
|
|
f2df48139f | ||
|
|
48d8137604 | ||
|
|
310cf0bb9b | ||
|
|
b6c47fdd21 | ||
|
|
e6a2b7366f | ||
|
|
d8f72e2b27 | ||
|
|
08749deaeb | ||
|
|
83a88d9989 | ||
|
|
151910c27a | ||
|
|
fca8ed57bd | ||
|
|
56679818be | ||
|
|
f07f2edad2 | ||
|
|
2dcfe90850 | ||
|
|
dc80a39c54 | ||
|
|
34be898491 | ||
|
|
f4a2fbf64f | ||
|
|
a0c6076ccc | ||
|
|
fddce00d08 | ||
|
|
ae682d59f7 | ||
|
|
347f3de15c | ||
|
|
a34091991a | ||
|
|
efe1986a56 | ||
|
|
3ab507b66f | ||
|
|
afbad73c0b | ||
|
|
a1fa17d872 | ||
|
|
b50421beee | ||
|
|
aeded07815 | ||
|
|
7d14f24b84 | ||
|
|
c52b8100c6 | ||
|
|
f52cddc25c | ||
|
|
fc5e39f6cc | ||
|
|
8ccf381fc7 | ||
|
|
f320887bff | ||
|
|
eef5e3768c | ||
|
|
7a7821e1c8 | ||
|
|
37fbdcb453 | ||
|
|
135ffbd4f2 | ||
|
|
4736213ba4 | ||
|
|
8290fadd3a | ||
|
|
23c1ea6c31 | ||
|
|
31c5264430 | ||
|
|
32da4440ba | ||
|
|
e7a61c7edf | ||
|
|
6db337c8c5 | ||
|
|
2282e36196 | ||
|
|
72b5832f30 | ||
|
|
9a344152aa | ||
|
|
281944762d | ||
|
|
e3be67c3c7 | ||
|
|
215ab512cd | ||
|
|
d0fc728c23 | ||
|
|
1c710daf98 | ||
|
|
5566aaf95a | ||
|
|
52c73d3a08 | ||
|
|
baf8d25656 | ||
|
|
2eb0d946e5 | ||
|
|
ce95c6dc9d | ||
|
|
6a46cb2be6 | ||
|
|
f06dc7ea90 | ||
|
|
7f2ef708a6 | ||
|
|
4ab2e0d498 | ||
|
|
ca9161024f | ||
|
|
8dc246a2a7 | ||
|
|
c7f2de73df | ||
|
|
3f979399b1 | ||
|
|
2abd4a274b | ||
|
|
c5707f64dc | ||
|
|
62bb841e89 | ||
|
|
23cccb6396 | ||
|
|
87ad9dfc59 | ||
|
|
c59adaa4db | ||
|
|
14fbe50636 | ||
|
|
d3ede3a46f | ||
|
|
01c7c67aed | ||
|
|
971d50b684 | ||
|
|
9cf1557c37 | ||
|
|
126e2e49bb | ||
|
|
c88266afed | ||
|
|
7402f3fb0e | ||
|
|
eae8bce941 | ||
|
|
8c0275421a | ||
|
|
a436b30ebf | ||
|
|
a132f55302 | ||
|
|
be56fb7e8b | ||
|
|
4fbe130592 | ||
|
|
ed6d8243ef | ||
|
|
03790ad8a2 | ||
|
|
97b5b0b875 | ||
|
|
f62183473c | ||
|
|
aeb4b96560 | ||
|
|
b413760136 | ||
|
|
45882343e6 | ||
|
|
4c1f24da82 | ||
|
|
8c63835971 | ||
|
|
a96a5fd2ef | ||
|
|
9a62fae012 | ||
|
|
81a10e9f89 | ||
|
|
db475bd511 | ||
|
|
eef5f27a87 | ||
|
|
790dc74581 | ||
|
|
4f36accd81 | ||
|
|
04ead57731 | ||
|
|
e9951bc34a | ||
|
|
74d13e271e | ||
|
|
9181481fc8 | ||
|
|
62ddd8fe1c | ||
|
|
7891f73cb0 | ||
|
|
a359e2248b | ||
|
|
513ffa7e0c | ||
|
|
ef6c46a231 | ||
|
|
22237a4a8d | ||
|
|
6ab8ada9ff | ||
|
|
9a756939d8 | ||
|
|
aea3baf897 | ||
|
|
f8d0f7f67a | ||
|
|
fed34273a1 | ||
|
|
34a1b30006 | ||
|
|
798abf2103 | ||
|
|
75e539f4d2 | ||
|
|
946e65d1c6 | ||
|
|
b8e36eacfd | ||
|
|
cd49c1dac7 | ||
|
|
86411a5ca5 | ||
|
|
1186cad9ca | ||
|
|
e66de86a82 | ||
|
|
1e08bfca9c | ||
|
|
64f5b96295 | ||
|
|
8d38a28b82 | ||
|
|
1743dc694a | ||
|
|
8fb30a5895 | ||
|
|
d59ed71465 | ||
|
|
ea0f5ca60e | ||
|
|
bf74341257 | ||
|
|
c4e6dee1e6 | ||
|
|
34ab661cde | ||
|
|
26b3ce4eb7 | ||
|
|
e10e418971 | ||
|
|
4c915f12b7 | ||
|
|
3c454eefe9 | ||
|
|
3839767aed | ||
|
|
fc0a7ca7e3 | ||
|
|
a361d62945 | ||
|
|
aac173ed72 | ||
|
|
5f3fb0bf9f | ||
|
|
61f383a713 | ||
|
|
c62bdb2fa1 | ||
|
|
c88869778d | ||
|
|
3fd647b291 | ||
|
|
2f255952b7 |
9
.codecov.yml
Normal file
9
.codecov.yml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
coverage:
|
||||||
|
status:
|
||||||
|
patch: off
|
||||||
|
|
||||||
|
project:
|
||||||
|
default:
|
||||||
|
informational: true
|
||||||
|
ignore:
|
||||||
|
- "**/*.pb.go" # Ignore all auto generated protobuf structures.
|
||||||
196
.github/workflows/SetPageFileSize.ps1
vendored
Normal file
196
.github/workflows/SetPageFileSize.ps1
vendored
Normal file
@@ -0,0 +1,196 @@
|
|||||||
|
<#
|
||||||
|
# MIT License (MIT) Copyright (c) 2020 Maxim Lobanov and contributors
|
||||||
|
# Source: https://github.com/al-cheb/configure-pagefile-action/blob/master/scripts/SetPageFileSize.ps1
|
||||||
|
.SYNOPSIS
|
||||||
|
Configure Pagefile on Windows machine
|
||||||
|
.NOTES
|
||||||
|
Author: Aleksandr Chebotov
|
||||||
|
|
||||||
|
.EXAMPLE
|
||||||
|
SetPageFileSize.ps1 -MinimumSize 4GB -MaximumSize 8GB -DiskRoot "D:"
|
||||||
|
#>
|
||||||
|
|
||||||
|
param(
|
||||||
|
[System.UInt64] $MinimumSize = 8gb ,
|
||||||
|
[System.UInt64] $MaximumSize = 8gb ,
|
||||||
|
[System.String] $DiskRoot = "D:"
|
||||||
|
)
|
||||||
|
|
||||||
|
# https://referencesource.microsoft.com/#System.IdentityModel/System/IdentityModel/NativeMethods.cs,619688d876febbe1
|
||||||
|
# https://www.geoffchappell.com/studies/windows/km/ntoskrnl/api/mm/modwrite/create.htm
|
||||||
|
# https://referencesource.microsoft.com/#mscorlib/microsoft/win32/safehandles/safefilehandle.cs,9b08210f3be75520
|
||||||
|
# https://referencesource.microsoft.com/#mscorlib/system/security/principal/tokenaccesslevels.cs,6eda91f498a38586
|
||||||
|
# https://www.autoitscript.com/forum/topic/117993-api-ntcreatepagingfile/
|
||||||
|
|
||||||
|
$source = @'
|
||||||
|
using System;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Security.Principal;
|
||||||
|
using System.Text;
|
||||||
|
using Microsoft.Win32;
|
||||||
|
using Microsoft.Win32.SafeHandles;
|
||||||
|
|
||||||
|
namespace Util
|
||||||
|
{
|
||||||
|
class NativeMethods
|
||||||
|
{
|
||||||
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
|
internal struct LUID
|
||||||
|
{
|
||||||
|
internal uint LowPart;
|
||||||
|
internal uint HighPart;
|
||||||
|
}
|
||||||
|
|
||||||
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
|
internal struct LUID_AND_ATTRIBUTES
|
||||||
|
{
|
||||||
|
internal LUID Luid;
|
||||||
|
internal uint Attributes;
|
||||||
|
}
|
||||||
|
|
||||||
|
[StructLayout(LayoutKind.Sequential)]
|
||||||
|
internal struct TOKEN_PRIVILEGE
|
||||||
|
{
|
||||||
|
internal uint PrivilegeCount;
|
||||||
|
internal LUID_AND_ATTRIBUTES Privilege;
|
||||||
|
|
||||||
|
internal static readonly uint Size = (uint)Marshal.SizeOf(typeof(TOKEN_PRIVILEGE));
|
||||||
|
}
|
||||||
|
|
||||||
|
[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
||||||
|
internal struct UNICODE_STRING
|
||||||
|
{
|
||||||
|
internal UInt16 length;
|
||||||
|
internal UInt16 maximumLength;
|
||||||
|
internal string buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
[DllImport("kernel32.dll", SetLastError=true)]
|
||||||
|
internal static extern IntPtr LocalFree(IntPtr handle);
|
||||||
|
|
||||||
|
[DllImport("advapi32.dll", ExactSpelling = true, CharSet = CharSet.Unicode, SetLastError = true, PreserveSig = false)]
|
||||||
|
internal static extern bool LookupPrivilegeValueW(
|
||||||
|
[In] string lpSystemName,
|
||||||
|
[In] string lpName,
|
||||||
|
[Out] out LUID luid
|
||||||
|
);
|
||||||
|
|
||||||
|
[DllImport("advapi32.dll", SetLastError = true, PreserveSig = false)]
|
||||||
|
internal static extern bool AdjustTokenPrivileges(
|
||||||
|
[In] SafeCloseHandle tokenHandle,
|
||||||
|
[In] bool disableAllPrivileges,
|
||||||
|
[In] ref TOKEN_PRIVILEGE newState,
|
||||||
|
[In] uint bufferLength,
|
||||||
|
[Out] out TOKEN_PRIVILEGE previousState,
|
||||||
|
[Out] out uint returnLength
|
||||||
|
);
|
||||||
|
|
||||||
|
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true, PreserveSig = false)]
|
||||||
|
internal static extern bool OpenProcessToken(
|
||||||
|
[In] IntPtr processToken,
|
||||||
|
[In] int desiredAccess,
|
||||||
|
[Out] out SafeCloseHandle tokenHandle
|
||||||
|
);
|
||||||
|
|
||||||
|
[DllImport("ntdll.dll", CharSet = CharSet.Unicode, SetLastError = true, CallingConvention = CallingConvention.StdCall)]
|
||||||
|
internal static extern Int32 NtCreatePagingFile(
|
||||||
|
[In] ref UNICODE_STRING pageFileName,
|
||||||
|
[In] ref Int64 minimumSize,
|
||||||
|
[In] ref Int64 maximumSize,
|
||||||
|
[In] UInt32 flags
|
||||||
|
);
|
||||||
|
|
||||||
|
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
||||||
|
internal static extern uint QueryDosDeviceW(
|
||||||
|
string lpDeviceName,
|
||||||
|
StringBuilder lpTargetPath,
|
||||||
|
int ucchMax
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class SafeCloseHandle: SafeHandleZeroOrMinusOneIsInvalid
|
||||||
|
{
|
||||||
|
[DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true)]
|
||||||
|
internal extern static bool CloseHandle(IntPtr handle);
|
||||||
|
|
||||||
|
private SafeCloseHandle() : base(true)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public SafeCloseHandle(IntPtr preexistingHandle, bool ownsHandle) : base(ownsHandle)
|
||||||
|
{
|
||||||
|
SetHandle(preexistingHandle);
|
||||||
|
}
|
||||||
|
|
||||||
|
override protected bool ReleaseHandle()
|
||||||
|
{
|
||||||
|
return CloseHandle(handle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class PageFile
|
||||||
|
{
|
||||||
|
public static void SetPageFileSize(long minimumValue, long maximumValue, string lpDeviceName)
|
||||||
|
{
|
||||||
|
SetPageFilePrivilege();
|
||||||
|
StringBuilder lpTargetPath = new StringBuilder(260);
|
||||||
|
|
||||||
|
UInt32 resultQueryDosDevice = NativeMethods.QueryDosDeviceW(lpDeviceName, lpTargetPath, lpTargetPath.Capacity);
|
||||||
|
if (resultQueryDosDevice == 0)
|
||||||
|
{
|
||||||
|
throw new Win32Exception(Marshal.GetLastWin32Error());
|
||||||
|
}
|
||||||
|
|
||||||
|
string pageFilePath = lpTargetPath.ToString() + "\\pagefile.sys";
|
||||||
|
|
||||||
|
NativeMethods.UNICODE_STRING pageFileName = new NativeMethods.UNICODE_STRING
|
||||||
|
{
|
||||||
|
length = (ushort)(pageFilePath.Length * 2),
|
||||||
|
maximumLength = (ushort)(2 * (pageFilePath.Length + 1)),
|
||||||
|
buffer = pageFilePath
|
||||||
|
};
|
||||||
|
|
||||||
|
Int32 resultNtCreatePagingFile = NativeMethods.NtCreatePagingFile(ref pageFileName, ref minimumValue, ref maximumValue, 0);
|
||||||
|
if (resultNtCreatePagingFile != 0)
|
||||||
|
{
|
||||||
|
throw new Win32Exception(Marshal.GetLastWin32Error());
|
||||||
|
}
|
||||||
|
|
||||||
|
Console.WriteLine("PageFile: {0} / {1} bytes for {2}", minimumValue, maximumValue, pageFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void SetPageFilePrivilege()
|
||||||
|
{
|
||||||
|
const int SE_PRIVILEGE_ENABLED = 0x00000002;
|
||||||
|
const int AdjustPrivileges = 0x00000020;
|
||||||
|
const int Query = 0x00000008;
|
||||||
|
|
||||||
|
NativeMethods.LUID luid;
|
||||||
|
NativeMethods.LookupPrivilegeValueW(null, "SeCreatePagefilePrivilege", out luid);
|
||||||
|
|
||||||
|
SafeCloseHandle hToken;
|
||||||
|
NativeMethods.OpenProcessToken(
|
||||||
|
Process.GetCurrentProcess().Handle,
|
||||||
|
AdjustPrivileges | Query,
|
||||||
|
out hToken
|
||||||
|
);
|
||||||
|
|
||||||
|
NativeMethods.TOKEN_PRIVILEGE previousState;
|
||||||
|
NativeMethods.TOKEN_PRIVILEGE newState;
|
||||||
|
uint previousSize = 0;
|
||||||
|
newState.PrivilegeCount = 1;
|
||||||
|
newState.Privilege.Luid = luid;
|
||||||
|
newState.Privilege.Attributes = SE_PRIVILEGE_ENABLED;
|
||||||
|
|
||||||
|
NativeMethods.AdjustTokenPrivileges(hToken, false, ref newState, NativeMethods.TOKEN_PRIVILEGE.Size, out previousState, out previousSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
'@
|
||||||
|
|
||||||
|
Add-Type -TypeDefinition $source
|
||||||
|
|
||||||
|
# Set SetPageFileSize
|
||||||
|
[Util.PageFile]::SetPageFileSize($minimumSize, $maximumSize, $diskRoot)
|
||||||
77
.github/workflows/go-deploy.yml
vendored
Normal file
77
.github/workflows/go-deploy.yml
vendored
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
name: Build and Upload assets
|
||||||
|
on:
|
||||||
|
release:
|
||||||
|
types: [published]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
os: [ ubuntu-latest, windows-latest, macos-latest ]
|
||||||
|
name: Building For ${{ matrix.os }}
|
||||||
|
steps:
|
||||||
|
- name: Fix windows CRLF
|
||||||
|
run: git config --global core.autocrlf false
|
||||||
|
|
||||||
|
- name: Check out code into the Go module directory
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
|
# We need to increase the page size because the tests run out of memory on github CI windows.
|
||||||
|
# Use the powershell script from this github action: https://github.com/al-cheb/configure-pagefile-action/blob/master/scripts/SetPageFileSize.ps1
|
||||||
|
# MIT License (MIT) Copyright (c) 2020 Maxim Lobanov and contributors
|
||||||
|
- name: Increase page size on windows
|
||||||
|
if: runner.os == 'Windows'
|
||||||
|
shell: powershell
|
||||||
|
run: powershell -command .\.github\workflows\SetPageFileSize.ps1
|
||||||
|
|
||||||
|
|
||||||
|
- name: Set up Go 1.x
|
||||||
|
uses: actions/setup-go@v2
|
||||||
|
with:
|
||||||
|
go-version: 1.16
|
||||||
|
|
||||||
|
- name: Build on linux
|
||||||
|
if: runner.os == 'Linux'
|
||||||
|
# `-extldflags=-static` - means static link everything, `-tags netgo,osusergo` means use pure go replacements for "os/user" and "net"
|
||||||
|
# `-s -w` strips the binary to produce smaller size binaries
|
||||||
|
run: |
|
||||||
|
go build -v -ldflags="-s -w -extldflags=-static" -tags netgo,osusergo -o ./bin/ ./...
|
||||||
|
archive="bin/kaspad-${{ github.event.release.tag_name }}-linux.zip"
|
||||||
|
asset_name="kaspad-${{ github.event.release.tag_name }}-linux.zip"
|
||||||
|
zip -r "${archive}" ./bin/*
|
||||||
|
echo "archive=${archive}" >> $GITHUB_ENV
|
||||||
|
echo "asset_name=${asset_name}" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
- name: Build on Windows
|
||||||
|
if: runner.os == 'Windows'
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
go build -v -ldflags="-s -w" -o bin/ ./...
|
||||||
|
archive="bin/kaspad-${{ github.event.release.tag_name }}-win64.zip"
|
||||||
|
asset_name="kaspad-${{ github.event.release.tag_name }}-win64.zip"
|
||||||
|
powershell "Compress-Archive bin/* \"${archive}\""
|
||||||
|
echo "archive=${archive}" >> $GITHUB_ENV
|
||||||
|
echo "asset_name=${asset_name}" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
- name: Build on MacOS
|
||||||
|
if: runner.os == 'macOS'
|
||||||
|
run: |
|
||||||
|
go build -v -ldflags="-s -w" -o ./bin/ ./...
|
||||||
|
archive="bin/kaspad-${{ github.event.release.tag_name }}-osx.zip"
|
||||||
|
asset_name="kaspad-${{ github.event.release.tag_name }}-osx.zip"
|
||||||
|
zip -r "${archive}" ./bin/*
|
||||||
|
echo "archive=${archive}" >> $GITHUB_ENV
|
||||||
|
echo "asset_name=${asset_name}" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
|
||||||
|
- name: Upload Release Asset
|
||||||
|
uses: actions/upload-release-asset@v1
|
||||||
|
env:
|
||||||
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
with:
|
||||||
|
upload_url: ${{ github.event.release.upload_url }}
|
||||||
|
asset_path: "./${{ env.archive }}"
|
||||||
|
asset_name: "${{ env.asset_name }}"
|
||||||
|
asset_content_type: application/zip
|
||||||
49
.github/workflows/go-race.yml
vendored
Normal file
49
.github/workflows/go-race.yml
vendored
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
name: Go-Race
|
||||||
|
|
||||||
|
on:
|
||||||
|
schedule:
|
||||||
|
- cron: "0 0 * * *"
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
race_test:
|
||||||
|
runs-on: ubuntu-20.04
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
branch: [ master, latest ]
|
||||||
|
name: Race detection on ${{ matrix.branch }}
|
||||||
|
steps:
|
||||||
|
- name: Check out code into the Go module directory
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Set up Go 1.x
|
||||||
|
uses: actions/setup-go@v2
|
||||||
|
with:
|
||||||
|
go-version: 1.15
|
||||||
|
|
||||||
|
- name: Set scheduled branch name
|
||||||
|
shell: bash
|
||||||
|
if: github.event_name == 'schedule'
|
||||||
|
run: |
|
||||||
|
if [ "${{ matrix.branch }}" == "master" ]; then
|
||||||
|
echo "run_on=master" >> $GITHUB_ENV
|
||||||
|
fi
|
||||||
|
if [ "${{ matrix.branch }}" == "latest" ]; then
|
||||||
|
branch=$(git branch -r | grep 'v\([0-9]\+\.\)\([0-9]\+\.\)\([0-9]\+\)-dev' | sort -Vr | head -1 | xargs)
|
||||||
|
echo "run_on=${branch}" >> $GITHUB_ENV
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Set manual branch name
|
||||||
|
shell: bash
|
||||||
|
if: github.event_name == 'workflow_dispatch'
|
||||||
|
run: echo "run_on=${{ github.ref }}" >> $GITHUB_ENV
|
||||||
|
|
||||||
|
- name: Test with race detector
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
git checkout "${{ env.run_on }}"
|
||||||
|
git status
|
||||||
|
go test -race ./...
|
||||||
73
.github/workflows/go.yml
vendored
Normal file
73
.github/workflows/go.yml
vendored
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
name: Go
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
pull_request:
|
||||||
|
# edtited - "title, body, or the base branch of the PR is modified"
|
||||||
|
# synchronize - "commit(s) pushed to the pull request"
|
||||||
|
types: [opened, synchronize, edited, reopened]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
os: [ ubuntu-16.04, macos-10.15, windows-2019 ]
|
||||||
|
name: Testing on on ${{ matrix.os }}
|
||||||
|
steps:
|
||||||
|
|
||||||
|
- name: Fix windows CRLF
|
||||||
|
run: git config --global core.autocrlf false
|
||||||
|
|
||||||
|
- name: Check out code into the Go module directory
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
|
# We need to increase the page size because the tests run out of memory on github CI windows.
|
||||||
|
# Use the powershell script from this github action: https://github.com/al-cheb/configure-pagefile-action/blob/master/scripts/SetPageFileSize.ps1
|
||||||
|
# MIT License (MIT) Copyright (c) 2020 Maxim Lobanov and contributors
|
||||||
|
- name: Increase page size on windows
|
||||||
|
if: runner.os == 'Windows'
|
||||||
|
shell: powershell
|
||||||
|
run: powershell -command .\.github\workflows\SetPageFileSize.ps1
|
||||||
|
|
||||||
|
|
||||||
|
- name: Set up Go 1.x
|
||||||
|
uses: actions/setup-go@v2
|
||||||
|
with:
|
||||||
|
go-version: 1.16
|
||||||
|
|
||||||
|
|
||||||
|
# Source: https://github.com/actions/cache/blob/main/examples.md#go---modules
|
||||||
|
- name: Go Cache
|
||||||
|
uses: actions/cache@v2
|
||||||
|
with:
|
||||||
|
path: ~/go/pkg/mod
|
||||||
|
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
|
||||||
|
restore-keys: |
|
||||||
|
${{ runner.os }}-go-
|
||||||
|
|
||||||
|
- name: Test
|
||||||
|
shell: bash
|
||||||
|
run: ./build_and_test.sh -v
|
||||||
|
|
||||||
|
coverage:
|
||||||
|
runs-on: ubuntu-20.04
|
||||||
|
name: Produce code coverage
|
||||||
|
steps:
|
||||||
|
- name: Check out code into the Go module directory
|
||||||
|
uses: actions/checkout@v2
|
||||||
|
|
||||||
|
- name: Set up Go 1.x
|
||||||
|
uses: actions/setup-go@v2
|
||||||
|
with:
|
||||||
|
go-version: 1.16
|
||||||
|
|
||||||
|
- name: Delete the stability tests from coverage
|
||||||
|
run: rm -r stability-tests
|
||||||
|
|
||||||
|
- name: Create coverage file
|
||||||
|
run: go test -v -covermode=atomic -coverpkg=./... -coverprofile coverage.txt ./...
|
||||||
|
|
||||||
|
- name: Upload coverage file
|
||||||
|
run: bash <(curl -s https://codecov.io/bash)
|
||||||
18
.gitignore
vendored
18
.gitignore
vendored
@@ -13,6 +13,21 @@ kaspad.db
|
|||||||
*.o
|
*.o
|
||||||
*.a
|
*.a
|
||||||
*.so
|
*.so
|
||||||
|
*.dylib
|
||||||
|
|
||||||
|
# Test binary, built with `go test -c`
|
||||||
|
*.test
|
||||||
|
|
||||||
|
# Real binaries, build with `go build .`
|
||||||
|
kaspad
|
||||||
|
cmd/gencerts/gencerts
|
||||||
|
cmd/kaspactl/kaspactl
|
||||||
|
cmd/kasminer/kaspaminer
|
||||||
|
*.exe
|
||||||
|
*.exe~
|
||||||
|
|
||||||
|
# Output of the go coverage tool
|
||||||
|
*.out
|
||||||
|
|
||||||
# Folders
|
# Folders
|
||||||
_obj
|
_obj
|
||||||
@@ -31,8 +46,7 @@ _cgo_export.*
|
|||||||
|
|
||||||
_testmain.go
|
_testmain.go
|
||||||
|
|
||||||
*.exe
|
|
||||||
|
|
||||||
# IDE
|
# IDE
|
||||||
.idea
|
.idea
|
||||||
.vscode
|
.vscode
|
||||||
|
|||||||
19
CONTRIBUTING.md
Normal file
19
CONTRIBUTING.md
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
# Contributing to Kaspad
|
||||||
|
|
||||||
|
Any contribution to Kaspad is very welcome.
|
||||||
|
|
||||||
|
## Getting started
|
||||||
|
|
||||||
|
If you want to start contributing to Kaspad and don't know where to start, you can pick an issue from
|
||||||
|
the [list](https://github.com/kaspanet/kaspad/issues).
|
||||||
|
|
||||||
|
If you want to make a big change it's better to discuss it first by opening an issue or talk about it in
|
||||||
|
[Discord](https://discord.gg/WmGhhzk) to avoid duplicate work.
|
||||||
|
|
||||||
|
## Pull Request process
|
||||||
|
|
||||||
|
Any pull request should be opened against the development branch of the target version. The development branch format is
|
||||||
|
as follows: `vx.y.z-dev`, for example: `v0.8.5-dev`.
|
||||||
|
|
||||||
|
All pull requests should pass the checks written in `build_and_test.sh`, so it's recommended to run this script before
|
||||||
|
submitting your PR.
|
||||||
29
README.md
29
README.md
@@ -9,12 +9,16 @@ Warning: This is pre-alpha software. There's no guarantee anything works.
|
|||||||
|
|
||||||
Kaspad is the reference full node Kaspa implementation written in Go (golang).
|
Kaspad is the reference full node Kaspa implementation written in Go (golang).
|
||||||
|
|
||||||
This project is currently under active development and is in a pre-Alpha state.
|
This project is currently under active development and is in a pre-Alpha state.
|
||||||
Some things still don't work and APIs are far from finalized. The code is provided for reference only.
|
Some things still don't work and APIs are far from finalized. The code is provided for reference only.
|
||||||
|
|
||||||
|
## What is kaspa
|
||||||
|
|
||||||
|
Kaspa is an attempt at a proof-of-work cryptocurrency with instant confirmations and sub-second block times. It is based on [the PHANTOM protocol](https://eprint.iacr.org/2018/104.pdf), a generalization of Nakamoto consensus.
|
||||||
|
|
||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
Latest version of [Go](http://golang.org) (currently 1.13).
|
Go 1.16 or later.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@@ -27,23 +31,17 @@ Latest version of [Go](http://golang.org) (currently 1.13).
|
|||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ go version
|
$ go version
|
||||||
$ go env GOROOT GOPATH
|
|
||||||
```
|
```
|
||||||
|
|
||||||
NOTE: The `GOROOT` and `GOPATH` above must not be the same path. It is
|
|
||||||
recommended that `GOPATH` is set to a directory in your home directory such as
|
|
||||||
`~/dev/go` to avoid write permission issues. It is also recommended to add
|
|
||||||
`$GOPATH/bin` to your `PATH` at this point.
|
|
||||||
|
|
||||||
- Run the following commands to obtain and install kaspad including all dependencies:
|
- Run the following commands to obtain and install kaspad including all dependencies:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ git clone https://github.com/kaspanet/kaspad $GOPATH/src/github.com/kaspanet/kaspad
|
$ git clone https://github.com/kaspanet/kaspad
|
||||||
$ cd $GOPATH/src/github.com/kaspanet/kaspad
|
$ cd kaspad
|
||||||
$ go install . ./cmd/...
|
$ go install . ./cmd/...
|
||||||
```
|
```
|
||||||
|
|
||||||
- Kaspad (and utilities) should now be installed in `$GOPATH/bin`. If you did
|
- Kaspad (and utilities) should now be installed in `$(go env GOPATH)/bin`. If you did
|
||||||
not already add the bin directory to your system path during Go installation,
|
not already add the bin directory to your system path during Go installation,
|
||||||
you are encouraged to do so now.
|
you are encouraged to do so now.
|
||||||
|
|
||||||
@@ -53,14 +51,12 @@ $ go install . ./cmd/...
|
|||||||
Kaspad has several configuration options available to tweak how it runs, but all
|
Kaspad has several configuration options available to tweak how it runs, but all
|
||||||
of the basic operations work with zero configuration.
|
of the basic operations work with zero configuration.
|
||||||
|
|
||||||
#### Linux/BSD/POSIX/Source
|
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ ./kaspad
|
$ kaspad
|
||||||
```
|
```
|
||||||
|
|
||||||
## Discord
|
## Discord
|
||||||
Join our discord server using the following link: https://discord.gg/WmGhhzk
|
Join our discord server using the following link: https://discord.gg/YNYnNN5Pf2
|
||||||
|
|
||||||
## Issue Tracker
|
## Issue Tracker
|
||||||
|
|
||||||
@@ -69,9 +65,8 @@ is used for this project.
|
|||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
The documentation is a work-in-progress. It is located in the [docs](https://github.com/kaspanet/kaspad/tree/master/docs) folder.
|
The [documentation](https://github.com/kaspanet/docs) is a work-in-progress
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
Kaspad is licensed under the copyfree [ISC License](https://choosealicense.com/licenses/isc/).
|
Kaspad is licensed under the copyfree [ISC License](https://choosealicense.com/licenses/isc/).
|
||||||
|
|
||||||
|
|||||||
353
app/app.go
353
app/app.go
@@ -2,247 +2,184 @@ package app
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync/atomic"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/kaspanet/kaspad/infrastructure/network/addressmanager"
|
|
||||||
|
|
||||||
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/id"
|
|
||||||
|
|
||||||
"github.com/kaspanet/kaspad/app/appmessage"
|
|
||||||
"github.com/kaspanet/kaspad/app/protocol"
|
|
||||||
"github.com/kaspanet/kaspad/domain/blockdag"
|
|
||||||
"github.com/kaspanet/kaspad/domain/blockdag/indexers"
|
|
||||||
"github.com/kaspanet/kaspad/domain/mempool"
|
|
||||||
"github.com/kaspanet/kaspad/domain/mining"
|
|
||||||
"github.com/kaspanet/kaspad/domain/txscript"
|
|
||||||
"github.com/kaspanet/kaspad/infrastructure/config"
|
"github.com/kaspanet/kaspad/infrastructure/config"
|
||||||
"github.com/kaspanet/kaspad/infrastructure/db/dbaccess"
|
"github.com/kaspanet/kaspad/infrastructure/db/database"
|
||||||
"github.com/kaspanet/kaspad/infrastructure/network/connmanager"
|
"github.com/kaspanet/kaspad/infrastructure/db/database/ldb"
|
||||||
"github.com/kaspanet/kaspad/infrastructure/network/dnsseed"
|
"github.com/kaspanet/kaspad/infrastructure/logger"
|
||||||
"github.com/kaspanet/kaspad/infrastructure/network/netadapter"
|
"github.com/kaspanet/kaspad/infrastructure/os/execenv"
|
||||||
"github.com/kaspanet/kaspad/infrastructure/network/rpc"
|
"github.com/kaspanet/kaspad/infrastructure/os/limits"
|
||||||
"github.com/kaspanet/kaspad/infrastructure/os/signal"
|
"github.com/kaspanet/kaspad/infrastructure/os/signal"
|
||||||
"github.com/kaspanet/kaspad/util"
|
"github.com/kaspanet/kaspad/infrastructure/os/winservice"
|
||||||
"github.com/kaspanet/kaspad/util/panics"
|
"github.com/kaspanet/kaspad/util/panics"
|
||||||
|
"github.com/kaspanet/kaspad/util/profiling"
|
||||||
|
"github.com/kaspanet/kaspad/version"
|
||||||
)
|
)
|
||||||
|
|
||||||
// App is a wrapper for all the kaspad services
|
const leveldbCacheSizeMiB = 256
|
||||||
type App struct {
|
|
||||||
cfg *config.Config
|
|
||||||
rpcServer *rpc.Server
|
|
||||||
addressManager *addressmanager.AddressManager
|
|
||||||
protocolManager *protocol.Manager
|
|
||||||
connectionManager *connmanager.ConnectionManager
|
|
||||||
netAdapter *netadapter.NetAdapter
|
|
||||||
|
|
||||||
started, shutdown int32
|
var desiredLimits = &limits.DesiredLimits{
|
||||||
|
FileLimitWant: 2048,
|
||||||
|
FileLimitMin: 1024,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start launches all the kaspad services.
|
var serviceDescription = &winservice.ServiceDescription{
|
||||||
func (a *App) Start() {
|
Name: "kaspadsvc",
|
||||||
// Already started?
|
DisplayName: "Kaspad Service",
|
||||||
if atomic.AddInt32(&a.started, 1) != 1 {
|
Description: "Downloads and stays synchronized with the Kaspa blockDAG and " +
|
||||||
return
|
"provides DAG services to applications.",
|
||||||
}
|
|
||||||
|
|
||||||
log.Trace("Starting kaspad")
|
|
||||||
|
|
||||||
err := a.protocolManager.Start()
|
|
||||||
if err != nil {
|
|
||||||
panics.Exit(log, fmt.Sprintf("Error starting the p2p protocol: %+v", err))
|
|
||||||
}
|
|
||||||
|
|
||||||
a.maybeSeedFromDNS()
|
|
||||||
|
|
||||||
a.connectionManager.Start()
|
|
||||||
|
|
||||||
if !a.cfg.DisableRPC {
|
|
||||||
a.rpcServer.Start()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stop gracefully shuts down all the kaspad services.
|
type kaspadApp struct {
|
||||||
func (a *App) Stop() {
|
cfg *config.Config
|
||||||
// Make sure this only happens once.
|
}
|
||||||
if atomic.AddInt32(&a.shutdown, 1) != 1 {
|
|
||||||
log.Infof("Kaspad is already in the process of shutting down")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Warnf("Kaspad shutting down")
|
// StartApp starts the kaspad app, and blocks until it finishes running
|
||||||
|
func StartApp() error {
|
||||||
|
execenv.Initialize(desiredLimits)
|
||||||
|
|
||||||
a.connectionManager.Stop()
|
// Load configuration and parse command line. This function also
|
||||||
|
// initializes logging and configures it accordingly.
|
||||||
err := a.protocolManager.Stop()
|
cfg, err := config.LoadConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error stopping the p2p protocol: %+v", err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
defer logger.BackendLog.Close()
|
||||||
|
defer panics.HandlePanic(log, "MAIN", nil)
|
||||||
|
|
||||||
// Shutdown the RPC server if it's not disabled.
|
app := &kaspadApp{cfg: cfg}
|
||||||
if !a.cfg.DisableRPC {
|
|
||||||
err := a.rpcServer.Stop()
|
// Call serviceMain on Windows to handle running as a service. When
|
||||||
|
// the return isService flag is true, exit now since we ran as a
|
||||||
|
// service. Otherwise, just fall through to normal operation.
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
isService, err := winservice.WinServiceMain(app.main, serviceDescription, cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error stopping rpcServer: %+v", err)
|
return err
|
||||||
|
}
|
||||||
|
if isService {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
err = a.addressManager.Stop()
|
return app.main(nil)
|
||||||
if err != nil {
|
|
||||||
log.Errorf("Error stopping address manager: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a new App instance configured to listen on addr for the
|
func (app *kaspadApp) main(startedChan chan<- struct{}) error {
|
||||||
// kaspa network type specified by dagParams. Use start to begin accepting
|
// Get a channel that will be closed when a shutdown signal has been
|
||||||
// connections from peers.
|
// triggered either from an OS signal such as SIGINT (Ctrl+C) or from
|
||||||
func New(cfg *config.Config, databaseContext *dbaccess.DatabaseContext, interrupt <-chan struct{}) (*App, error) {
|
// another subsystem such as the RPC server.
|
||||||
indexManager, acceptanceIndex := setupIndexes(cfg)
|
interrupt := signal.InterruptListener()
|
||||||
|
defer log.Info("Shutdown complete")
|
||||||
|
|
||||||
sigCache := txscript.NewSigCache(cfg.SigCacheMaxSize)
|
// Show version at startup.
|
||||||
|
log.Infof("Version %s", version.Version())
|
||||||
|
|
||||||
// Create a new block DAG instance with the appropriate configuration.
|
// Enable http profiling server if requested.
|
||||||
dag, err := setupDAG(cfg, databaseContext, interrupt, sigCache, indexManager)
|
if app.cfg.Profile != "" {
|
||||||
if err != nil {
|
profiling.Start(app.cfg.Profile, log)
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
txMempool := setupMempool(cfg, dag, sigCache)
|
// Return now if an interrupt signal was triggered.
|
||||||
|
if signal.InterruptRequested(interrupt) {
|
||||||
netAdapter, err := netadapter.NewNetAdapter(cfg)
|
return nil
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
addressManager, err := addressmanager.New(cfg, databaseContext)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
connectionManager, err := connmanager.New(cfg, netAdapter, addressManager)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
protocolManager, err := protocol.NewManager(cfg, dag, netAdapter, addressManager, txMempool, connectionManager)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
rpcServer, err := setupRPC(
|
|
||||||
cfg, dag, txMempool, sigCache, acceptanceIndex, connectionManager, addressManager, protocolManager)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &App{
|
if app.cfg.ResetDatabase {
|
||||||
cfg: cfg,
|
err := removeDatabase(app.cfg)
|
||||||
rpcServer: rpcServer,
|
|
||||||
protocolManager: protocolManager,
|
|
||||||
connectionManager: connectionManager,
|
|
||||||
netAdapter: netAdapter,
|
|
||||||
addressManager: addressManager,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *App) maybeSeedFromDNS() {
|
|
||||||
if !a.cfg.DisableDNSSeed {
|
|
||||||
dnsseed.SeedFromDNS(a.cfg.NetParams(), a.cfg.DNSSeed, appmessage.SFNodeNetwork, false, nil,
|
|
||||||
a.cfg.Lookup, func(addresses []*appmessage.NetAddress) {
|
|
||||||
// Kaspad uses a lookup of the dns seeder here. Since seeder returns
|
|
||||||
// IPs of nodes and not its own IP, we can not know real IP of
|
|
||||||
// source. So we'll take first returned address as source.
|
|
||||||
a.addressManager.AddAddresses(addresses, addresses[0], nil)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
func setupDAG(cfg *config.Config, databaseContext *dbaccess.DatabaseContext, interrupt <-chan struct{},
|
|
||||||
sigCache *txscript.SigCache, indexManager blockdag.IndexManager) (*blockdag.BlockDAG, error) {
|
|
||||||
|
|
||||||
dag, err := blockdag.New(&blockdag.Config{
|
|
||||||
Interrupt: interrupt,
|
|
||||||
DatabaseContext: databaseContext,
|
|
||||||
DAGParams: cfg.NetParams(),
|
|
||||||
TimeSource: blockdag.NewTimeSource(),
|
|
||||||
SigCache: sigCache,
|
|
||||||
IndexManager: indexManager,
|
|
||||||
SubnetworkID: cfg.SubnetworkID,
|
|
||||||
})
|
|
||||||
return dag, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func setupIndexes(cfg *config.Config) (blockdag.IndexManager, *indexers.AcceptanceIndex) {
|
|
||||||
// Create indexes if needed.
|
|
||||||
var indexes []indexers.Indexer
|
|
||||||
var acceptanceIndex *indexers.AcceptanceIndex
|
|
||||||
if cfg.AcceptanceIndex {
|
|
||||||
log.Info("acceptance index is enabled")
|
|
||||||
acceptanceIndex = indexers.NewAcceptanceIndex()
|
|
||||||
indexes = append(indexes, acceptanceIndex)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create an index manager if any of the optional indexes are enabled.
|
|
||||||
if len(indexes) < 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
indexManager := indexers.NewManager(indexes)
|
|
||||||
return indexManager, acceptanceIndex
|
|
||||||
}
|
|
||||||
|
|
||||||
func setupMempool(cfg *config.Config, dag *blockdag.BlockDAG, sigCache *txscript.SigCache) *mempool.TxPool {
|
|
||||||
mempoolConfig := mempool.Config{
|
|
||||||
Policy: mempool.Policy{
|
|
||||||
AcceptNonStd: cfg.RelayNonStd,
|
|
||||||
MaxOrphanTxs: cfg.MaxOrphanTxs,
|
|
||||||
MaxOrphanTxSize: config.DefaultMaxOrphanTxSize,
|
|
||||||
MinRelayTxFee: cfg.MinRelayTxFee,
|
|
||||||
MaxTxVersion: 1,
|
|
||||||
},
|
|
||||||
CalcSequenceLockNoLock: func(tx *util.Tx, utxoSet blockdag.UTXOSet) (*blockdag.SequenceLock, error) {
|
|
||||||
return dag.CalcSequenceLockNoLock(tx, utxoSet)
|
|
||||||
},
|
|
||||||
SigCache: sigCache,
|
|
||||||
DAG: dag,
|
|
||||||
}
|
|
||||||
|
|
||||||
return mempool.New(&mempoolConfig)
|
|
||||||
}
|
|
||||||
|
|
||||||
func setupRPC(cfg *config.Config,
|
|
||||||
dag *blockdag.BlockDAG,
|
|
||||||
txMempool *mempool.TxPool,
|
|
||||||
sigCache *txscript.SigCache,
|
|
||||||
acceptanceIndex *indexers.AcceptanceIndex,
|
|
||||||
connectionManager *connmanager.ConnectionManager,
|
|
||||||
addressManager *addressmanager.AddressManager,
|
|
||||||
protocolManager *protocol.Manager) (*rpc.Server, error) {
|
|
||||||
|
|
||||||
if !cfg.DisableRPC {
|
|
||||||
policy := mining.Policy{
|
|
||||||
BlockMaxMass: cfg.BlockMaxMass,
|
|
||||||
}
|
|
||||||
blockTemplateGenerator := mining.NewBlkTmplGenerator(&policy, txMempool, dag, sigCache)
|
|
||||||
|
|
||||||
rpcServer, err := rpc.NewRPCServer(cfg, dag, txMempool, acceptanceIndex, blockTemplateGenerator,
|
|
||||||
connectionManager, addressManager, protocolManager)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
log.Error(err)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Signal process shutdown when the RPC server requests it.
|
|
||||||
spawn("setupRPC-handleShutdownRequest", func() {
|
|
||||||
<-rpcServer.RequestedProcessShutdown()
|
|
||||||
signal.ShutdownRequestChannel <- struct{}{}
|
|
||||||
})
|
|
||||||
|
|
||||||
return rpcServer, nil
|
|
||||||
}
|
}
|
||||||
return nil, nil
|
|
||||||
|
// Open the database
|
||||||
|
databaseContext, err := openDB(app.cfg)
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
log.Infof("Gracefully shutting down the database...")
|
||||||
|
err := databaseContext.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Failed to close the database: %s", err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Return now if an interrupt signal was triggered.
|
||||||
|
if signal.InterruptRequested(interrupt) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create componentManager and start it.
|
||||||
|
componentManager, err := NewComponentManager(app.cfg, databaseContext, interrupt)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("Unable to start kaspad: %+v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
log.Infof("Gracefully shutting down kaspad...")
|
||||||
|
|
||||||
|
shutdownDone := make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
componentManager.Stop()
|
||||||
|
shutdownDone <- struct{}{}
|
||||||
|
}()
|
||||||
|
|
||||||
|
const shutdownTimeout = 2 * time.Minute
|
||||||
|
|
||||||
|
select {
|
||||||
|
case <-shutdownDone:
|
||||||
|
case <-time.After(shutdownTimeout):
|
||||||
|
log.Criticalf("Graceful shutdown timed out %s. Terminating...", shutdownTimeout)
|
||||||
|
}
|
||||||
|
log.Infof("Kaspad shutdown complete")
|
||||||
|
}()
|
||||||
|
|
||||||
|
componentManager.Start()
|
||||||
|
|
||||||
|
if startedChan != nil {
|
||||||
|
startedChan <- struct{}{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait until the interrupt signal is received from an OS signal or
|
||||||
|
// shutdown is requested through one of the subsystems such as the RPC
|
||||||
|
// server.
|
||||||
|
<-interrupt
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// P2PNodeID returns the network ID associated with this App
|
// dbPath returns the path to the block database given a database type.
|
||||||
func (a *App) P2PNodeID() *id.ID {
|
func databasePath(cfg *config.Config) string {
|
||||||
return a.netAdapter.ID()
|
return filepath.Join(cfg.AppDir, "data")
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddressManager returns the AddressManager associated with this App
|
func removeDatabase(cfg *config.Config) error {
|
||||||
func (a *App) AddressManager() *addressmanager.AddressManager {
|
dbPath := databasePath(cfg)
|
||||||
return a.addressManager
|
return os.RemoveAll(dbPath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func openDB(cfg *config.Config) (database.Database, error) {
|
||||||
|
dbPath := databasePath(cfg)
|
||||||
|
|
||||||
|
err := checkDatabaseVersion(dbPath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Infof("Loading database from '%s'", dbPath)
|
||||||
|
db, err := ldb.NewLevelDB(dbPath, leveldbCacheSizeMiB)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return db, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,431 +0,0 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package appmessage
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"compress/bzip2"
|
|
||||||
"io/ioutil"
|
|
||||||
"math"
|
|
||||||
"os"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
|
||||||
)
|
|
||||||
|
|
||||||
// genesisCoinbaseTx is the coinbase transaction for the genesis blocks for
|
|
||||||
// the main network and test network.
|
|
||||||
var genesisCoinbaseTxIns = []*TxIn{
|
|
||||||
{
|
|
||||||
PreviousOutpoint: Outpoint{
|
|
||||||
TxID: daghash.TxID{},
|
|
||||||
Index: 0xffffffff,
|
|
||||||
},
|
|
||||||
SignatureScript: []byte{
|
|
||||||
0x04, 0xff, 0xff, 0x00, 0x1d, 0x01, 0x04, 0x45, /* |.......E| */
|
|
||||||
0x54, 0x68, 0x65, 0x20, 0x54, 0x69, 0x6d, 0x65, /* |The Time| */
|
|
||||||
0x73, 0x20, 0x30, 0x33, 0x2f, 0x4a, 0x61, 0x6e, /* |s 03/Jan| */
|
|
||||||
0x2f, 0x32, 0x30, 0x30, 0x39, 0x20, 0x43, 0x68, /* |/2009 Ch| */
|
|
||||||
0x61, 0x6e, 0x63, 0x65, 0x6c, 0x6c, 0x6f, 0x72, /* |ancellor| */
|
|
||||||
0x20, 0x6f, 0x6e, 0x20, 0x62, 0x72, 0x69, 0x6e, /* | on brin| */
|
|
||||||
0x6b, 0x20, 0x6f, 0x66, 0x20, 0x73, 0x65, 0x63, /* |k of sec|*/
|
|
||||||
0x6f, 0x6e, 0x64, 0x20, 0x62, 0x61, 0x69, 0x6c, /* |ond bail| */
|
|
||||||
0x6f, 0x75, 0x74, 0x20, 0x66, 0x6f, 0x72, 0x20, /* |out for |*/
|
|
||||||
0x62, 0x61, 0x6e, 0x6b, 0x73, /* |banks| */
|
|
||||||
},
|
|
||||||
Sequence: math.MaxUint64,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
var genesisCoinbaseTxOuts = []*TxOut{
|
|
||||||
{
|
|
||||||
Value: 0x12a05f200,
|
|
||||||
ScriptPubKey: []byte{
|
|
||||||
0x41, 0x04, 0x67, 0x8a, 0xfd, 0xb0, 0xfe, 0x55, /* |A.g....U| */
|
|
||||||
0x48, 0x27, 0x19, 0x67, 0xf1, 0xa6, 0x71, 0x30, /* |H'.g..q0| */
|
|
||||||
0xb7, 0x10, 0x5c, 0xd6, 0xa8, 0x28, 0xe0, 0x39, /* |..\..(.9| */
|
|
||||||
0x09, 0xa6, 0x79, 0x62, 0xe0, 0xea, 0x1f, 0x61, /* |..yb...a| */
|
|
||||||
0xde, 0xb6, 0x49, 0xf6, 0xbc, 0x3f, 0x4c, 0xef, /* |..I..?L.| */
|
|
||||||
0x38, 0xc4, 0xf3, 0x55, 0x04, 0xe5, 0x1e, 0xc1, /* |8..U....| */
|
|
||||||
0x12, 0xde, 0x5c, 0x38, 0x4d, 0xf7, 0xba, 0x0b, /* |..\8M...| */
|
|
||||||
0x8d, 0x57, 0x8a, 0x4c, 0x70, 0x2b, 0x6b, 0xf1, /* |.W.Lp+k.| */
|
|
||||||
0x1d, 0x5f, 0xac, /* |._.| */
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
var genesisCoinbaseTx = NewNativeMsgTx(1, genesisCoinbaseTxIns, genesisCoinbaseTxOuts)
|
|
||||||
|
|
||||||
// BenchmarkWriteVarInt1 performs a benchmark on how long it takes to write
|
|
||||||
// a single byte variable length integer.
|
|
||||||
func BenchmarkWriteVarInt1(b *testing.B) {
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
WriteVarInt(ioutil.Discard, 1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkWriteVarInt3 performs a benchmark on how long it takes to write
|
|
||||||
// a three byte variable length integer.
|
|
||||||
func BenchmarkWriteVarInt3(b *testing.B) {
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
WriteVarInt(ioutil.Discard, 65535)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkWriteVarInt5 performs a benchmark on how long it takes to write
|
|
||||||
// a five byte variable length integer.
|
|
||||||
func BenchmarkWriteVarInt5(b *testing.B) {
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
WriteVarInt(ioutil.Discard, 4294967295)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkWriteVarInt9 performs a benchmark on how long it takes to write
|
|
||||||
// a nine byte variable length integer.
|
|
||||||
func BenchmarkWriteVarInt9(b *testing.B) {
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
WriteVarInt(ioutil.Discard, 18446744073709551615)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkReadVarInt1 performs a benchmark on how long it takes to read
|
|
||||||
// a single byte variable length integer.
|
|
||||||
func BenchmarkReadVarInt1(b *testing.B) {
|
|
||||||
buf := []byte{0x01}
|
|
||||||
r := bytes.NewReader(buf)
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
r.Seek(0, 0)
|
|
||||||
ReadVarInt(r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkReadVarInt3 performs a benchmark on how long it takes to read
|
|
||||||
// a three byte variable length integer.
|
|
||||||
func BenchmarkReadVarInt3(b *testing.B) {
|
|
||||||
buf := []byte{0x0fd, 0xff, 0xff}
|
|
||||||
r := bytes.NewReader(buf)
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
r.Seek(0, 0)
|
|
||||||
ReadVarInt(r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkReadVarInt5 performs a benchmark on how long it takes to read
|
|
||||||
// a five byte variable length integer.
|
|
||||||
func BenchmarkReadVarInt5(b *testing.B) {
|
|
||||||
buf := []byte{0xfe, 0xff, 0xff, 0xff, 0xff}
|
|
||||||
r := bytes.NewReader(buf)
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
r.Seek(0, 0)
|
|
||||||
ReadVarInt(r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkReadVarInt9 performs a benchmark on how long it takes to read
|
|
||||||
// a nine byte variable length integer.
|
|
||||||
func BenchmarkReadVarInt9(b *testing.B) {
|
|
||||||
buf := []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
|
|
||||||
r := bytes.NewReader(buf)
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
r.Seek(0, 0)
|
|
||||||
ReadVarInt(r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkReadVarStr4 performs a benchmark on how long it takes to read a
|
|
||||||
// four byte variable length string.
|
|
||||||
func BenchmarkReadVarStr4(b *testing.B) {
|
|
||||||
buf := []byte{0x04, 't', 'e', 's', 't'}
|
|
||||||
r := bytes.NewReader(buf)
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
r.Seek(0, 0)
|
|
||||||
ReadVarString(r, 0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkReadVarStr10 performs a benchmark on how long it takes to read a
|
|
||||||
// ten byte variable length string.
|
|
||||||
func BenchmarkReadVarStr10(b *testing.B) {
|
|
||||||
buf := []byte{0x0a, 't', 'e', 's', 't', '0', '1', '2', '3', '4', '5'}
|
|
||||||
r := bytes.NewReader(buf)
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
r.Seek(0, 0)
|
|
||||||
ReadVarString(r, 0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkWriteVarStr4 performs a benchmark on how long it takes to write a
|
|
||||||
// four byte variable length string.
|
|
||||||
func BenchmarkWriteVarStr4(b *testing.B) {
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
WriteVarString(ioutil.Discard, "test")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkWriteVarStr10 performs a benchmark on how long it takes to write a
|
|
||||||
// ten byte variable length string.
|
|
||||||
func BenchmarkWriteVarStr10(b *testing.B) {
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
WriteVarString(ioutil.Discard, "test012345")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkReadOutpoint performs a benchmark on how long it takes to read a
|
|
||||||
// transaction outpoint.
|
|
||||||
func BenchmarkReadOutpoint(b *testing.B) {
|
|
||||||
buf := []byte{
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash
|
|
||||||
0xff, 0xff, 0xff, 0xff, // Previous output index
|
|
||||||
}
|
|
||||||
r := bytes.NewReader(buf)
|
|
||||||
var op Outpoint
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
r.Seek(0, 0)
|
|
||||||
readOutpoint(r, 0, 0, &op)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkWriteOutpoint performs a benchmark on how long it takes to write a
|
|
||||||
// transaction outpoint.
|
|
||||||
func BenchmarkWriteOutpoint(b *testing.B) {
|
|
||||||
op := &Outpoint{
|
|
||||||
TxID: daghash.TxID{},
|
|
||||||
Index: 0,
|
|
||||||
}
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
writeOutpoint(ioutil.Discard, 0, 0, op)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkReadTxOut performs a benchmark on how long it takes to read a
|
|
||||||
// transaction output.
|
|
||||||
func BenchmarkReadTxOut(b *testing.B) {
|
|
||||||
buf := []byte{
|
|
||||||
0x00, 0xf2, 0x05, 0x2a, 0x01, 0x00, 0x00, 0x00, // Transaction amount
|
|
||||||
0x43, // Varint for length of scriptPubKey
|
|
||||||
0x41, // OP_DATA_65
|
|
||||||
0x04, 0x96, 0xb5, 0x38, 0xe8, 0x53, 0x51, 0x9c,
|
|
||||||
0x72, 0x6a, 0x2c, 0x91, 0xe6, 0x1e, 0xc1, 0x16,
|
|
||||||
0x00, 0xae, 0x13, 0x90, 0x81, 0x3a, 0x62, 0x7c,
|
|
||||||
0x66, 0xfb, 0x8b, 0xe7, 0x94, 0x7b, 0xe6, 0x3c,
|
|
||||||
0x52, 0xda, 0x75, 0x89, 0x37, 0x95, 0x15, 0xd4,
|
|
||||||
0xe0, 0xa6, 0x04, 0xf8, 0x14, 0x17, 0x81, 0xe6,
|
|
||||||
0x22, 0x94, 0x72, 0x11, 0x66, 0xbf, 0x62, 0x1e,
|
|
||||||
0x73, 0xa8, 0x2c, 0xbf, 0x23, 0x42, 0xc8, 0x58,
|
|
||||||
0xee, // 65-byte signature
|
|
||||||
0xac, // OP_CHECKSIG
|
|
||||||
}
|
|
||||||
r := bytes.NewReader(buf)
|
|
||||||
var txOut TxOut
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
r.Seek(0, 0)
|
|
||||||
readTxOut(r, 0, 0, &txOut)
|
|
||||||
scriptPool.Return(txOut.ScriptPubKey)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkWriteTxOut performs a benchmark on how long it takes to write
|
|
||||||
// a transaction output.
|
|
||||||
func BenchmarkWriteTxOut(b *testing.B) {
|
|
||||||
txOut := blockOne.Transactions[0].TxOut[0]
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
WriteTxOut(ioutil.Discard, 0, 0, txOut)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkReadTxIn performs a benchmark on how long it takes to read a
|
|
||||||
// transaction input.
|
|
||||||
func BenchmarkReadTxIn(b *testing.B) {
|
|
||||||
buf := []byte{
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash
|
|
||||||
0xff, 0xff, 0xff, 0xff, // Previous output index
|
|
||||||
0x07, // Varint for length of signature script
|
|
||||||
0x04, 0xff, 0xff, 0x00, 0x1d, 0x01, 0x04, // Signature script
|
|
||||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // Sequence
|
|
||||||
}
|
|
||||||
r := bytes.NewReader(buf)
|
|
||||||
var txIn TxIn
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
r.Seek(0, 0)
|
|
||||||
readTxIn(r, 0, 0, &txIn)
|
|
||||||
scriptPool.Return(txIn.SignatureScript)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkWriteTxIn performs a benchmark on how long it takes to write
|
|
||||||
// a transaction input.
|
|
||||||
func BenchmarkWriteTxIn(b *testing.B) {
|
|
||||||
txIn := blockOne.Transactions[0].TxIn[0]
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
writeTxIn(ioutil.Discard, 0, 0, txIn, txEncodingFull)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkDeserializeTx performs a benchmark on how long it takes to
|
|
||||||
// deserialize a small transaction.
|
|
||||||
func BenchmarkDeserializeTxSmall(b *testing.B) {
|
|
||||||
buf := []byte{
|
|
||||||
0x01, 0x00, 0x00, 0x00, // Version
|
|
||||||
0x01, // Varint for number of input transactions
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // // Previous output hash
|
|
||||||
0xff, 0xff, 0xff, 0xff, // Prevous output index
|
|
||||||
0x07, // Varint for length of signature script
|
|
||||||
0x04, 0xff, 0xff, 0x00, 0x1d, 0x01, 0x04, // Signature script
|
|
||||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // Sequence
|
|
||||||
0x01, // Varint for number of output transactions
|
|
||||||
0x00, 0xf2, 0x05, 0x2a, 0x01, 0x00, 0x00, 0x00, // Transaction amount
|
|
||||||
0x43, // Varint for length of scriptPubKey
|
|
||||||
0x41, // OP_DATA_65
|
|
||||||
0x04, 0x96, 0xb5, 0x38, 0xe8, 0x53, 0x51, 0x9c,
|
|
||||||
0x72, 0x6a, 0x2c, 0x91, 0xe6, 0x1e, 0xc1, 0x16,
|
|
||||||
0x00, 0xae, 0x13, 0x90, 0x81, 0x3a, 0x62, 0x7c,
|
|
||||||
0x66, 0xfb, 0x8b, 0xe7, 0x94, 0x7b, 0xe6, 0x3c,
|
|
||||||
0x52, 0xda, 0x75, 0x89, 0x37, 0x95, 0x15, 0xd4,
|
|
||||||
0xe0, 0xa6, 0x04, 0xf8, 0x14, 0x17, 0x81, 0xe6,
|
|
||||||
0x22, 0x94, 0x72, 0x11, 0x66, 0xbf, 0x62, 0x1e,
|
|
||||||
0x73, 0xa8, 0x2c, 0xbf, 0x23, 0x42, 0xc8, 0x58,
|
|
||||||
0xee, // 65-byte signature
|
|
||||||
0xac, // OP_CHECKSIG
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Lock time
|
|
||||||
}
|
|
||||||
|
|
||||||
r := bytes.NewReader(buf)
|
|
||||||
var tx MsgTx
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
r.Seek(0, 0)
|
|
||||||
tx.Deserialize(r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkDeserializeTxLarge performs a benchmark on how long it takes to
|
|
||||||
// deserialize a very large transaction.
|
|
||||||
func BenchmarkDeserializeTxLarge(b *testing.B) {
|
|
||||||
fi, err := os.Open("testdata/megatx.bin.bz2")
|
|
||||||
if err != nil {
|
|
||||||
b.Fatalf("Failed to read transaction data: %v", err)
|
|
||||||
}
|
|
||||||
defer fi.Close()
|
|
||||||
buf, err := ioutil.ReadAll(bzip2.NewReader(fi))
|
|
||||||
if err != nil {
|
|
||||||
b.Fatalf("Failed to read transaction data: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
r := bytes.NewReader(buf)
|
|
||||||
var tx MsgTx
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
r.Seek(0, 0)
|
|
||||||
tx.Deserialize(r)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkSerializeTx performs a benchmark on how long it takes to serialize
|
|
||||||
// a transaction.
|
|
||||||
func BenchmarkSerializeTx(b *testing.B) {
|
|
||||||
tx := blockOne.Transactions[0]
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
tx.Serialize(ioutil.Discard)
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkReadBlockHeader performs a benchmark on how long it takes to
|
|
||||||
// deserialize a block header.
|
|
||||||
func BenchmarkReadBlockHeader(b *testing.B) {
|
|
||||||
buf := []byte{
|
|
||||||
0x01, 0x00, 0x00, 0x00, // Version 1
|
|
||||||
0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72,
|
|
||||||
0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
|
|
||||||
0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
|
|
||||||
0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00, // PrevBlock
|
|
||||||
0x3b, 0xa3, 0xed, 0xfd, 0x7a, 0x7b, 0x12, 0xb2,
|
|
||||||
0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76, 0x8f, 0x61,
|
|
||||||
0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32,
|
|
||||||
0x3a, 0x9f, 0xb8, 0xaa, 0x4b, 0x1e, 0x5e, 0x4a, // MerkleRoot
|
|
||||||
0x29, 0xab, 0x5f, 0x49, 0x00, 0x00, 0x00, 0x00, // Timestamp
|
|
||||||
0xff, 0xff, 0x00, 0x1d, // Bits
|
|
||||||
0xf3, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, // Fake Nonce
|
|
||||||
0x00, // TxnCount Varint
|
|
||||||
}
|
|
||||||
r := bytes.NewReader(buf)
|
|
||||||
var header BlockHeader
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
r.Seek(0, 0)
|
|
||||||
readBlockHeader(r, 0, &header)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkWriteBlockHeader performs a benchmark on how long it takes to
|
|
||||||
// serialize a block header.
|
|
||||||
func BenchmarkWriteBlockHeader(b *testing.B) {
|
|
||||||
header := blockOne.Header
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
writeBlockHeader(ioutil.Discard, 0, &header)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkTxHash performs a benchmark on how long it takes to hash a
|
|
||||||
// transaction.
|
|
||||||
func BenchmarkTxHash(b *testing.B) {
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
genesisCoinbaseTx.TxHash()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkDoubleHashB performs a benchmark on how long it takes to perform a
|
|
||||||
// double hash returning a byte slice.
|
|
||||||
func BenchmarkDoubleHashB(b *testing.B) {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
if err := genesisCoinbaseTx.Serialize(&buf); err != nil {
|
|
||||||
b.Errorf("Serialize: unexpected error: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
txBytes := buf.Bytes()
|
|
||||||
|
|
||||||
b.ResetTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
_ = daghash.DoubleHashB(txBytes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkDoubleHashH performs a benchmark on how long it takes to perform
|
|
||||||
// a double hash returning a daghash.Hash.
|
|
||||||
func BenchmarkDoubleHashH(b *testing.B) {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
if err := genesisCoinbaseTx.Serialize(&buf); err != nil {
|
|
||||||
b.Errorf("Serialize: unexpected error: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
txBytes := buf.Bytes()
|
|
||||||
|
|
||||||
b.ResetTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
_ = daghash.DoubleHashH(txBytes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// BenchmarkDoubleHashWriter performs a benchmark on how long it takes to perform
|
|
||||||
// a double hash via the writer returning a daghash.Hash.
|
|
||||||
func BenchmarkDoubleHashWriter(b *testing.B) {
|
|
||||||
var buf bytes.Buffer
|
|
||||||
err := genesisCoinbaseTx.Serialize(&buf)
|
|
||||||
if err != nil {
|
|
||||||
b.Fatalf("Serialize: unexpected error: %+v", err)
|
|
||||||
}
|
|
||||||
txBytes := buf.Bytes()
|
|
||||||
|
|
||||||
b.ResetTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
writer := daghash.NewDoubleHashWriter()
|
|
||||||
_, _ = writer.Write(txBytes)
|
|
||||||
writer.Finalize()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,190 +0,0 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package appmessage
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
|
||||||
"github.com/kaspanet/kaspad/util/mstime"
|
|
||||||
)
|
|
||||||
|
|
||||||
// BaseBlockHeaderPayload is the base number of bytes a block header can be,
|
|
||||||
// not including the list of parent block headers.
|
|
||||||
// Version 4 bytes + Timestamp 8 bytes + Bits 4 bytes + Nonce 8 bytes +
|
|
||||||
// + NumParentBlocks 1 byte + HashMerkleRoot hash +
|
|
||||||
// + AcceptedIDMerkleRoot hash + UTXOCommitment hash.
|
|
||||||
// To get total size of block header len(ParentHashes) * daghash.HashSize should be
|
|
||||||
// added to this value
|
|
||||||
const BaseBlockHeaderPayload = 25 + 3*(daghash.HashSize)
|
|
||||||
|
|
||||||
// MaxNumParentBlocks is the maximum number of parent blocks a block can reference.
|
|
||||||
// Currently set to 255 as the maximum number NumParentBlocks can be due to it being a byte
|
|
||||||
const MaxNumParentBlocks = 255
|
|
||||||
|
|
||||||
// MaxBlockHeaderPayload is the maximum number of bytes a block header can be.
|
|
||||||
// BaseBlockHeaderPayload + up to MaxNumParentBlocks hashes of parent blocks
|
|
||||||
const MaxBlockHeaderPayload = BaseBlockHeaderPayload + (MaxNumParentBlocks * daghash.HashSize)
|
|
||||||
|
|
||||||
// BlockHeader defines information about a block and is used in the kaspa
|
|
||||||
// block (MsgBlock) and headers (MsgHeader) messages.
|
|
||||||
type BlockHeader struct {
|
|
||||||
// Version of the block. This is not the same as the protocol version.
|
|
||||||
Version int32
|
|
||||||
|
|
||||||
// Hashes of the parent block headers in the blockDAG.
|
|
||||||
ParentHashes []*daghash.Hash
|
|
||||||
|
|
||||||
// HashMerkleRoot is the merkle tree reference to hash of all transactions for the block.
|
|
||||||
HashMerkleRoot *daghash.Hash
|
|
||||||
|
|
||||||
// AcceptedIDMerkleRoot is merkle tree reference to hash all transactions
|
|
||||||
// accepted form the block.Blues
|
|
||||||
AcceptedIDMerkleRoot *daghash.Hash
|
|
||||||
|
|
||||||
// UTXOCommitment is an ECMH UTXO commitment to the block UTXO.
|
|
||||||
UTXOCommitment *daghash.Hash
|
|
||||||
|
|
||||||
// Time the block was created.
|
|
||||||
Timestamp mstime.Time
|
|
||||||
|
|
||||||
// Difficulty target for the block.
|
|
||||||
Bits uint32
|
|
||||||
|
|
||||||
// Nonce used to generate the block.
|
|
||||||
Nonce uint64
|
|
||||||
}
|
|
||||||
|
|
||||||
// NumParentBlocks return the number of entries in ParentHashes
|
|
||||||
func (h *BlockHeader) NumParentBlocks() byte {
|
|
||||||
return byte(len(h.ParentHashes))
|
|
||||||
}
|
|
||||||
|
|
||||||
// BlockHash computes the block identifier hash for the given block header.
|
|
||||||
func (h *BlockHeader) BlockHash() *daghash.Hash {
|
|
||||||
// Encode the header and double sha256 everything prior to the number of
|
|
||||||
// transactions.
|
|
||||||
writer := daghash.NewDoubleHashWriter()
|
|
||||||
err := writeBlockHeader(writer, 0, h)
|
|
||||||
if err != nil {
|
|
||||||
// It seems like this could only happen if the writer returned an error.
|
|
||||||
// and this writer should never return an error (no allocations or possible failures)
|
|
||||||
// the only non-writer error path here is unknown types in `WriteElement`
|
|
||||||
panic(fmt.Sprintf("BlockHash() failed. this should never fail unless BlockHeader was changed. err: %+v", err))
|
|
||||||
}
|
|
||||||
|
|
||||||
res := writer.Finalize()
|
|
||||||
return &res
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsGenesis returns true iff this block is a genesis block
|
|
||||||
func (h *BlockHeader) IsGenesis() bool {
|
|
||||||
return h.NumParentBlocks() == 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// KaspaDecode decodes r using the kaspa protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
// See Deserialize for decoding block headers stored to disk, such as in a
|
|
||||||
// database, as opposed to decoding block headers from the appmessage.
|
|
||||||
func (h *BlockHeader) KaspaDecode(r io.Reader, pver uint32) error {
|
|
||||||
return readBlockHeader(r, pver, h)
|
|
||||||
}
|
|
||||||
|
|
||||||
// KaspaEncode encodes the receiver to w using the kaspa protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
// See Serialize for encoding block headers to be stored to disk, such as in a
|
|
||||||
// database, as opposed to encoding block headers for the appmessage.
|
|
||||||
func (h *BlockHeader) KaspaEncode(w io.Writer, pver uint32) error {
|
|
||||||
return writeBlockHeader(w, pver, h)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deserialize decodes a block header from r into the receiver using a format
|
|
||||||
// that is suitable for long-term storage such as a database while respecting
|
|
||||||
// the Version field.
|
|
||||||
func (h *BlockHeader) Deserialize(r io.Reader) error {
|
|
||||||
// At the current time, there is no difference between the appmessage encoding
|
|
||||||
// at protocol version 0 and the stable long-term storage format. As
|
|
||||||
// a result, make use of readBlockHeader.
|
|
||||||
return readBlockHeader(r, 0, h)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Serialize encodes a block header from r into the receiver using a format
|
|
||||||
// that is suitable for long-term storage such as a database while respecting
|
|
||||||
// the Version field.
|
|
||||||
func (h *BlockHeader) Serialize(w io.Writer) error {
|
|
||||||
// At the current time, there is no difference between the appmessage encoding
|
|
||||||
// at protocol version 0 and the stable long-term storage format. As
|
|
||||||
// a result, make use of writeBlockHeader.
|
|
||||||
return writeBlockHeader(w, 0, h)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SerializeSize returns the number of bytes it would take to serialize the
|
|
||||||
// block header.
|
|
||||||
func (h *BlockHeader) SerializeSize() int {
|
|
||||||
return BaseBlockHeaderPayload + int(h.NumParentBlocks())*daghash.HashSize
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewBlockHeader returns a new BlockHeader using the provided version, previous
|
|
||||||
// block hash, hash merkle root, accepted ID merkle root, difficulty bits, and nonce used to generate the
|
|
||||||
// block with defaults or calclulated values for the remaining fields.
|
|
||||||
func NewBlockHeader(version int32, parentHashes []*daghash.Hash, hashMerkleRoot *daghash.Hash,
|
|
||||||
acceptedIDMerkleRoot *daghash.Hash, utxoCommitment *daghash.Hash, bits uint32, nonce uint64) *BlockHeader {
|
|
||||||
|
|
||||||
// Limit the timestamp to one millisecond precision since the protocol
|
|
||||||
// doesn't support better.
|
|
||||||
return &BlockHeader{
|
|
||||||
Version: version,
|
|
||||||
ParentHashes: parentHashes,
|
|
||||||
HashMerkleRoot: hashMerkleRoot,
|
|
||||||
AcceptedIDMerkleRoot: acceptedIDMerkleRoot,
|
|
||||||
UTXOCommitment: utxoCommitment,
|
|
||||||
Timestamp: mstime.Now(),
|
|
||||||
Bits: bits,
|
|
||||||
Nonce: nonce,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// readBlockHeader reads a kaspa block header from r. See Deserialize for
|
|
||||||
// decoding block headers stored to disk, such as in a database, as opposed to
|
|
||||||
// decoding from the appmessage.
|
|
||||||
func readBlockHeader(r io.Reader, pver uint32, bh *BlockHeader) error {
|
|
||||||
var numParentBlocks byte
|
|
||||||
err := readElements(r, &bh.Version, &numParentBlocks)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
bh.ParentHashes = make([]*daghash.Hash, numParentBlocks)
|
|
||||||
for i := byte(0); i < numParentBlocks; i++ {
|
|
||||||
hash := &daghash.Hash{}
|
|
||||||
err := ReadElement(r, hash)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
bh.ParentHashes[i] = hash
|
|
||||||
}
|
|
||||||
bh.HashMerkleRoot = &daghash.Hash{}
|
|
||||||
bh.AcceptedIDMerkleRoot = &daghash.Hash{}
|
|
||||||
bh.UTXOCommitment = &daghash.Hash{}
|
|
||||||
return readElements(r, bh.HashMerkleRoot, bh.AcceptedIDMerkleRoot, bh.UTXOCommitment,
|
|
||||||
(*int64Time)(&bh.Timestamp), &bh.Bits, &bh.Nonce)
|
|
||||||
}
|
|
||||||
|
|
||||||
// writeBlockHeader writes a kaspa block header to w. See Serialize for
|
|
||||||
// encoding block headers to be stored to disk, such as in a database, as
|
|
||||||
// opposed to encoding for the appmessage.
|
|
||||||
func writeBlockHeader(w io.Writer, pver uint32, bh *BlockHeader) error {
|
|
||||||
timestamp := bh.Timestamp.UnixMilliseconds()
|
|
||||||
if err := writeElements(w, bh.Version, bh.NumParentBlocks()); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for _, hash := range bh.ParentHashes {
|
|
||||||
if err := WriteElement(w, hash); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return writeElements(w, bh.HashMerkleRoot, bh.AcceptedIDMerkleRoot, bh.UTXOCommitment, timestamp, bh.Bits, bh.Nonce)
|
|
||||||
}
|
|
||||||
@@ -1,345 +0,0 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package appmessage
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
|
||||||
"github.com/kaspanet/kaspad/util/mstime"
|
|
||||||
"github.com/kaspanet/kaspad/util/random"
|
|
||||||
"reflect"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TestBlockHeader tests the BlockHeader API.
|
|
||||||
func TestBlockHeader(t *testing.T) {
|
|
||||||
nonce, err := random.Uint64()
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("random.Uint64: Error generating nonce: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
hashes := []*daghash.Hash{mainnetGenesisHash, simnetGenesisHash}
|
|
||||||
|
|
||||||
merkleHash := mainnetGenesisMerkleRoot
|
|
||||||
acceptedIDMerkleRoot := exampleAcceptedIDMerkleRoot
|
|
||||||
bits := uint32(0x1d00ffff)
|
|
||||||
bh := NewBlockHeader(1, hashes, merkleHash, acceptedIDMerkleRoot, exampleUTXOCommitment, bits, nonce)
|
|
||||||
|
|
||||||
// Ensure we get the same data back out.
|
|
||||||
if !reflect.DeepEqual(bh.ParentHashes, hashes) {
|
|
||||||
t.Errorf("NewBlockHeader: wrong prev hashes - got %v, want %v",
|
|
||||||
spew.Sprint(bh.ParentHashes), spew.Sprint(hashes))
|
|
||||||
}
|
|
||||||
if !bh.HashMerkleRoot.IsEqual(merkleHash) {
|
|
||||||
t.Errorf("NewBlockHeader: wrong merkle root - got %v, want %v",
|
|
||||||
spew.Sprint(bh.HashMerkleRoot), spew.Sprint(merkleHash))
|
|
||||||
}
|
|
||||||
if bh.Bits != bits {
|
|
||||||
t.Errorf("NewBlockHeader: wrong bits - got %v, want %v",
|
|
||||||
bh.Bits, bits)
|
|
||||||
}
|
|
||||||
if bh.Nonce != nonce {
|
|
||||||
t.Errorf("NewBlockHeader: wrong nonce - got %v, want %v",
|
|
||||||
bh.Nonce, nonce)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestBlockHeaderEncoding tests the BlockHeader appmessage encode and decode for various
|
|
||||||
// protocol versions.
|
|
||||||
func TestBlockHeaderEncoding(t *testing.T) {
|
|
||||||
nonce := uint64(123123) // 0x000000000001e0f3
|
|
||||||
pver := ProtocolVersion
|
|
||||||
|
|
||||||
// baseBlockHdr is used in the various tests as a baseline BlockHeader.
|
|
||||||
bits := uint32(0x1d00ffff)
|
|
||||||
baseBlockHdr := &BlockHeader{
|
|
||||||
Version: 1,
|
|
||||||
ParentHashes: []*daghash.Hash{mainnetGenesisHash, simnetGenesisHash},
|
|
||||||
HashMerkleRoot: mainnetGenesisMerkleRoot,
|
|
||||||
AcceptedIDMerkleRoot: exampleAcceptedIDMerkleRoot,
|
|
||||||
UTXOCommitment: exampleUTXOCommitment,
|
|
||||||
Timestamp: mstime.UnixMilliseconds(0x17315ed0f99),
|
|
||||||
Bits: bits,
|
|
||||||
Nonce: nonce,
|
|
||||||
}
|
|
||||||
|
|
||||||
// baseBlockHdrEncoded is the appmessage encoded bytes of baseBlockHdr.
|
|
||||||
baseBlockHdrEncoded := []byte{
|
|
||||||
0x01, 0x00, 0x00, 0x00, // Version 1
|
|
||||||
0x02, // NumParentBlocks
|
|
||||||
0xdc, 0x5f, 0x5b, 0x5b, 0x1d, 0xc2, 0xa7, 0x25, // mainnetGenesisHash
|
|
||||||
0x49, 0xd5, 0x1d, 0x4d, 0xee, 0xd7, 0xa4, 0x8b,
|
|
||||||
0xaf, 0xd3, 0x14, 0x4b, 0x56, 0x78, 0x98, 0xb1,
|
|
||||||
0x8c, 0xfd, 0x9f, 0x69, 0xdd, 0xcf, 0xbb, 0x63,
|
|
||||||
0xf6, 0x7a, 0xd7, 0x69, 0x5d, 0x9b, 0x66, 0x2a, // simnetGenesisHash
|
|
||||||
0x72, 0xff, 0x3d, 0x8e, 0xdb, 0xbb, 0x2d, 0xe0,
|
|
||||||
0xbf, 0xa6, 0x7b, 0x13, 0x97, 0x4b, 0xb9, 0x91,
|
|
||||||
0x0d, 0x11, 0x6d, 0x5c, 0xbd, 0x86, 0x3e, 0x68,
|
|
||||||
0x4a, 0x5e, 0x1e, 0x4b, 0xaa, 0xb8, 0x9f, 0x3a, // HashMerkleRoot
|
|
||||||
0x32, 0x51, 0x8a, 0x88, 0xc3, 0x1b, 0xc8, 0x7f,
|
|
||||||
0x61, 0x8f, 0x76, 0x67, 0x3e, 0x2c, 0xc7, 0x7a,
|
|
||||||
0xb2, 0x12, 0x7b, 0x7a, 0xfd, 0xed, 0xa3, 0x3b,
|
|
||||||
0x09, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C, // AcceptedIDMerkleRoot
|
|
||||||
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
|
|
||||||
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
|
|
||||||
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
|
|
||||||
0x10, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C, // UTXOCommitment
|
|
||||||
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
|
|
||||||
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
|
|
||||||
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
|
|
||||||
0x99, 0x0f, 0xed, 0x15, 0x73, 0x01, 0x00, 0x00, // Timestamp
|
|
||||||
0xff, 0xff, 0x00, 0x1d, // Bits
|
|
||||||
0xf3, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, // Fake Nonce
|
|
||||||
}
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
in *BlockHeader // Data to encode
|
|
||||||
out *BlockHeader // Expected decoded data
|
|
||||||
buf []byte // Encoded data
|
|
||||||
pver uint32 // Protocol version for appmessage encoding
|
|
||||||
}{
|
|
||||||
// Latest protocol version.
|
|
||||||
{
|
|
||||||
baseBlockHdr,
|
|
||||||
baseBlockHdr,
|
|
||||||
baseBlockHdrEncoded,
|
|
||||||
ProtocolVersion,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Encode to appmessage format.
|
|
||||||
var buf bytes.Buffer
|
|
||||||
err := writeBlockHeader(&buf, test.pver, test.in)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("writeBlockHeader #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !bytes.Equal(buf.Bytes(), test.buf) {
|
|
||||||
t.Errorf("writeBlockHeader #%d\n got: %s want: %s", i,
|
|
||||||
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
buf.Reset()
|
|
||||||
err = test.in.KaspaEncode(&buf, pver)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("KaspaEncode #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !bytes.Equal(buf.Bytes(), test.buf) {
|
|
||||||
t.Errorf("KaspaEncode #%d\n got: %s want: %s", i,
|
|
||||||
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode the block header from appmessage format.
|
|
||||||
var bh BlockHeader
|
|
||||||
rbuf := bytes.NewReader(test.buf)
|
|
||||||
err = readBlockHeader(rbuf, test.pver, &bh)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("readBlockHeader #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(&bh, test.out) {
|
|
||||||
t.Errorf("readBlockHeader #%d\n got: %s want: %s", i,
|
|
||||||
spew.Sdump(&bh), spew.Sdump(test.out))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
rbuf = bytes.NewReader(test.buf)
|
|
||||||
err = bh.KaspaDecode(rbuf, pver)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("KaspaDecode #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(&bh, test.out) {
|
|
||||||
t.Errorf("KaspaDecode #%d\n got: %s want: %s", i,
|
|
||||||
spew.Sdump(&bh), spew.Sdump(test.out))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestBlockHeaderSerialize tests BlockHeader serialize and deserialize.
|
|
||||||
func TestBlockHeaderSerialize(t *testing.T) {
|
|
||||||
nonce := uint64(123123) // 0x01e0f3
|
|
||||||
|
|
||||||
// baseBlockHdr is used in the various tests as a baseline BlockHeader.
|
|
||||||
bits := uint32(0x1d00ffff)
|
|
||||||
baseBlockHdr := &BlockHeader{
|
|
||||||
Version: 1,
|
|
||||||
ParentHashes: []*daghash.Hash{mainnetGenesisHash, simnetGenesisHash},
|
|
||||||
HashMerkleRoot: mainnetGenesisMerkleRoot,
|
|
||||||
AcceptedIDMerkleRoot: exampleAcceptedIDMerkleRoot,
|
|
||||||
UTXOCommitment: exampleUTXOCommitment,
|
|
||||||
Timestamp: mstime.UnixMilliseconds(0x17315ed0f99),
|
|
||||||
Bits: bits,
|
|
||||||
Nonce: nonce,
|
|
||||||
}
|
|
||||||
|
|
||||||
// baseBlockHdrEncoded is the appmessage encoded bytes of baseBlockHdr.
|
|
||||||
baseBlockHdrEncoded := []byte{
|
|
||||||
0x01, 0x00, 0x00, 0x00, // Version 1
|
|
||||||
0x02, // NumParentBlocks
|
|
||||||
0xdc, 0x5f, 0x5b, 0x5b, 0x1d, 0xc2, 0xa7, 0x25, // mainnetGenesisHash
|
|
||||||
0x49, 0xd5, 0x1d, 0x4d, 0xee, 0xd7, 0xa4, 0x8b,
|
|
||||||
0xaf, 0xd3, 0x14, 0x4b, 0x56, 0x78, 0x98, 0xb1,
|
|
||||||
0x8c, 0xfd, 0x9f, 0x69, 0xdd, 0xcf, 0xbb, 0x63,
|
|
||||||
0xf6, 0x7a, 0xd7, 0x69, 0x5d, 0x9b, 0x66, 0x2a, // simnetGenesisHash
|
|
||||||
0x72, 0xff, 0x3d, 0x8e, 0xdb, 0xbb, 0x2d, 0xe0,
|
|
||||||
0xbf, 0xa6, 0x7b, 0x13, 0x97, 0x4b, 0xb9, 0x91,
|
|
||||||
0x0d, 0x11, 0x6d, 0x5c, 0xbd, 0x86, 0x3e, 0x68,
|
|
||||||
0x4a, 0x5e, 0x1e, 0x4b, 0xaa, 0xb8, 0x9f, 0x3a, // HashMerkleRoot
|
|
||||||
0x32, 0x51, 0x8a, 0x88, 0xc3, 0x1b, 0xc8, 0x7f,
|
|
||||||
0x61, 0x8f, 0x76, 0x67, 0x3e, 0x2c, 0xc7, 0x7a,
|
|
||||||
0xb2, 0x12, 0x7b, 0x7a, 0xfd, 0xed, 0xa3, 0x3b,
|
|
||||||
0x09, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C, // AcceptedIDMerkleRoot
|
|
||||||
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
|
|
||||||
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
|
|
||||||
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
|
|
||||||
0x10, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C, // UTXOCommitment
|
|
||||||
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
|
|
||||||
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
|
|
||||||
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
|
|
||||||
0x99, 0x0f, 0xed, 0x15, 0x73, 0x01, 0x00, 0x00, // Timestamp
|
|
||||||
0xff, 0xff, 0x00, 0x1d, // Bits
|
|
||||||
0xf3, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, // Fake Nonce
|
|
||||||
}
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
in *BlockHeader // Data to encode
|
|
||||||
out *BlockHeader // Expected decoded data
|
|
||||||
buf []byte // Serialized data
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
baseBlockHdr,
|
|
||||||
baseBlockHdr,
|
|
||||||
baseBlockHdrEncoded,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Serialize the block header.
|
|
||||||
var buf bytes.Buffer
|
|
||||||
err := test.in.Serialize(&buf)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Serialize #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !bytes.Equal(buf.Bytes(), test.buf) {
|
|
||||||
t.Errorf("Serialize #%d\n got: %s want: %s", i,
|
|
||||||
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deserialize the block header.
|
|
||||||
var bh BlockHeader
|
|
||||||
rbuf := bytes.NewReader(test.buf)
|
|
||||||
err = bh.Deserialize(rbuf)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Deserialize #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(&bh, test.out) {
|
|
||||||
t.Errorf("Deserialize #%d\n got: %s want: %s", i,
|
|
||||||
spew.Sdump(&bh), spew.Sdump(test.out))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestBlockHeaderSerializeSize performs tests to ensure the serialize size for
|
|
||||||
// various block headers is accurate.
|
|
||||||
func TestBlockHeaderSerializeSize(t *testing.T) {
|
|
||||||
nonce := uint64(123123) // 0x1e0f3
|
|
||||||
bits := uint32(0x1d00ffff)
|
|
||||||
timestamp := mstime.UnixMilliseconds(0x495fab29000)
|
|
||||||
baseBlockHdr := &BlockHeader{
|
|
||||||
Version: 1,
|
|
||||||
ParentHashes: []*daghash.Hash{mainnetGenesisHash, simnetGenesisHash},
|
|
||||||
HashMerkleRoot: mainnetGenesisMerkleRoot,
|
|
||||||
AcceptedIDMerkleRoot: &daghash.ZeroHash,
|
|
||||||
UTXOCommitment: &daghash.ZeroHash,
|
|
||||||
Timestamp: timestamp,
|
|
||||||
Bits: bits,
|
|
||||||
Nonce: nonce,
|
|
||||||
}
|
|
||||||
|
|
||||||
genesisBlockHdr := &BlockHeader{
|
|
||||||
Version: 1,
|
|
||||||
ParentHashes: []*daghash.Hash{},
|
|
||||||
HashMerkleRoot: mainnetGenesisMerkleRoot,
|
|
||||||
AcceptedIDMerkleRoot: &daghash.ZeroHash,
|
|
||||||
UTXOCommitment: &daghash.ZeroHash,
|
|
||||||
Timestamp: timestamp,
|
|
||||||
Bits: bits,
|
|
||||||
Nonce: nonce,
|
|
||||||
}
|
|
||||||
tests := []struct {
|
|
||||||
in *BlockHeader // Block header to encode
|
|
||||||
size int // Expected serialized size
|
|
||||||
}{
|
|
||||||
// Block with no transactions.
|
|
||||||
{genesisBlockHdr, 121},
|
|
||||||
|
|
||||||
// First block in the mainnet block DAG.
|
|
||||||
{baseBlockHdr, 185},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
serializedSize := test.in.SerializeSize()
|
|
||||||
if serializedSize != test.size {
|
|
||||||
t.Errorf("BlockHeader.SerializeSize: #%d got: %d, want: "+
|
|
||||||
"%d", i, serializedSize, test.size)
|
|
||||||
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIsGenesis(t *testing.T) {
|
|
||||||
nonce := uint64(123123) // 0x1e0f3
|
|
||||||
bits := uint32(0x1d00ffff)
|
|
||||||
timestamp := mstime.UnixMilliseconds(0x495fab29000)
|
|
||||||
|
|
||||||
baseBlockHdr := &BlockHeader{
|
|
||||||
Version: 1,
|
|
||||||
ParentHashes: []*daghash.Hash{mainnetGenesisHash, simnetGenesisHash},
|
|
||||||
HashMerkleRoot: mainnetGenesisMerkleRoot,
|
|
||||||
Timestamp: timestamp,
|
|
||||||
Bits: bits,
|
|
||||||
Nonce: nonce,
|
|
||||||
}
|
|
||||||
genesisBlockHdr := &BlockHeader{
|
|
||||||
Version: 1,
|
|
||||||
ParentHashes: []*daghash.Hash{},
|
|
||||||
HashMerkleRoot: mainnetGenesisMerkleRoot,
|
|
||||||
Timestamp: timestamp,
|
|
||||||
Bits: bits,
|
|
||||||
Nonce: nonce,
|
|
||||||
}
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
in *BlockHeader // Block header to encode
|
|
||||||
isGenesis bool // Expected result for call of .IsGenesis
|
|
||||||
}{
|
|
||||||
{genesisBlockHdr, true},
|
|
||||||
{baseBlockHdr, false},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
isGenesis := test.in.IsGenesis()
|
|
||||||
if isGenesis != test.isGenesis {
|
|
||||||
t.Errorf("BlockHeader.IsGenesis: #%d got: %t, want: %t",
|
|
||||||
i, isGenesis, test.isGenesis)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -5,34 +5,12 @@
|
|||||||
package appmessage
|
package appmessage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"fmt"
|
|
||||||
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/id"
|
|
||||||
"github.com/kaspanet/kaspad/util/binaryserializer"
|
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
|
||||||
"github.com/kaspanet/kaspad/util/mstime"
|
|
||||||
"github.com/kaspanet/kaspad/util/subnetworkid"
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"io"
|
|
||||||
"math"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// MaxVarIntPayload is the maximum payload size for a variable length integer.
|
|
||||||
const MaxVarIntPayload = 9
|
|
||||||
|
|
||||||
// MaxInvPerMsg is the maximum number of inventory vectors that can be in any type of kaspa inv message.
|
// MaxInvPerMsg is the maximum number of inventory vectors that can be in any type of kaspa inv message.
|
||||||
const MaxInvPerMsg = 1 << 17
|
const MaxInvPerMsg = 1 << 17
|
||||||
|
|
||||||
var (
|
|
||||||
// littleEndian is a convenience variable since binary.LittleEndian is
|
|
||||||
// quite long.
|
|
||||||
littleEndian = binary.LittleEndian
|
|
||||||
|
|
||||||
// bigEndian is a convenience variable since binary.BigEndian is quite
|
|
||||||
// long.
|
|
||||||
bigEndian = binary.BigEndian
|
|
||||||
)
|
|
||||||
|
|
||||||
// errNonCanonicalVarInt is the common format string used for non-canonically
|
// errNonCanonicalVarInt is the common format string used for non-canonically
|
||||||
// encoded variable length integer errors.
|
// encoded variable length integer errors.
|
||||||
var errNonCanonicalVarInt = "non-canonical varint %x - discriminant %x must " +
|
var errNonCanonicalVarInt = "non-canonical varint %x - discriminant %x must " +
|
||||||
@@ -40,472 +18,3 @@ var errNonCanonicalVarInt = "non-canonical varint %x - discriminant %x must " +
|
|||||||
|
|
||||||
// errNoEncodingForType signifies that there's no encoding for the given type.
|
// errNoEncodingForType signifies that there's no encoding for the given type.
|
||||||
var errNoEncodingForType = errors.New("there's no encoding for this type")
|
var errNoEncodingForType = errors.New("there's no encoding for this type")
|
||||||
|
|
||||||
// int64Time represents a unix timestamp with milliseconds precision encoded with
|
|
||||||
// an int64. It is used as a way to signal the readElement function how to decode
|
|
||||||
// a timestamp into a Go mstime.Time since it is otherwise ambiguous.
|
|
||||||
type int64Time mstime.Time
|
|
||||||
|
|
||||||
// ReadElement reads the next sequence of bytes from r using little endian
|
|
||||||
// depending on the concrete type of element pointed to.
|
|
||||||
func ReadElement(r io.Reader, element interface{}) error {
|
|
||||||
// Attempt to read the element based on the concrete type via fast
|
|
||||||
// type assertions first.
|
|
||||||
switch e := element.(type) {
|
|
||||||
case *int32:
|
|
||||||
rv, err := binaryserializer.Uint32(r, littleEndian)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*e = int32(rv)
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case *uint32:
|
|
||||||
rv, err := binaryserializer.Uint32(r, littleEndian)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*e = rv
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case *int64:
|
|
||||||
rv, err := binaryserializer.Uint64(r, littleEndian)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*e = int64(rv)
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case *uint64:
|
|
||||||
rv, err := binaryserializer.Uint64(r, littleEndian)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*e = rv
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case *uint8:
|
|
||||||
rv, err := binaryserializer.Uint8(r)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*e = rv
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case *bool:
|
|
||||||
rv, err := binaryserializer.Uint8(r)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if rv == 0x00 {
|
|
||||||
*e = false
|
|
||||||
} else {
|
|
||||||
*e = true
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
// Unix timestamp encoded as an int64.
|
|
||||||
case *int64Time:
|
|
||||||
rv, err := binaryserializer.Uint64(r, binary.LittleEndian)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*e = int64Time(mstime.UnixMilliseconds(int64(rv)))
|
|
||||||
return nil
|
|
||||||
|
|
||||||
// Message header checksum.
|
|
||||||
case *[4]byte:
|
|
||||||
_, err := io.ReadFull(r, e[:])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
// Message header command.
|
|
||||||
case *MessageCommand:
|
|
||||||
rv, err := binaryserializer.Uint32(r, littleEndian)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*e = MessageCommand(rv)
|
|
||||||
return nil
|
|
||||||
|
|
||||||
// IP address.
|
|
||||||
case *[16]byte:
|
|
||||||
_, err := io.ReadFull(r, e[:])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case *daghash.Hash:
|
|
||||||
_, err := io.ReadFull(r, e[:])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case *id.ID:
|
|
||||||
return e.Deserialize(r)
|
|
||||||
|
|
||||||
case *subnetworkid.SubnetworkID:
|
|
||||||
_, err := io.ReadFull(r, e[:])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case *ServiceFlag:
|
|
||||||
rv, err := binaryserializer.Uint64(r, littleEndian)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*e = ServiceFlag(rv)
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case *KaspaNet:
|
|
||||||
rv, err := binaryserializer.Uint32(r, littleEndian)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
*e = KaspaNet(rv)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return errors.Wrapf(errNoEncodingForType, "couldn't find a way to read type %T", element)
|
|
||||||
}
|
|
||||||
|
|
||||||
// readElements reads multiple items from r. It is equivalent to multiple
|
|
||||||
// calls to readElement.
|
|
||||||
func readElements(r io.Reader, elements ...interface{}) error {
|
|
||||||
for _, element := range elements {
|
|
||||||
err := ReadElement(r, element)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteElement writes the little endian representation of element to w.
|
|
||||||
func WriteElement(w io.Writer, element interface{}) error {
|
|
||||||
// Attempt to write the element based on the concrete type via fast
|
|
||||||
// type assertions first.
|
|
||||||
switch e := element.(type) {
|
|
||||||
case int32:
|
|
||||||
err := binaryserializer.PutUint32(w, littleEndian, uint32(e))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case uint32:
|
|
||||||
err := binaryserializer.PutUint32(w, littleEndian, e)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case int64:
|
|
||||||
err := binaryserializer.PutUint64(w, littleEndian, uint64(e))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case uint64:
|
|
||||||
err := binaryserializer.PutUint64(w, littleEndian, e)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case uint8:
|
|
||||||
err := binaryserializer.PutUint8(w, e)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case bool:
|
|
||||||
var err error
|
|
||||||
if e {
|
|
||||||
err = binaryserializer.PutUint8(w, 0x01)
|
|
||||||
} else {
|
|
||||||
err = binaryserializer.PutUint8(w, 0x00)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
// Message header checksum.
|
|
||||||
case [4]byte:
|
|
||||||
_, err := w.Write(e[:])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
// Message header command.
|
|
||||||
case MessageCommand:
|
|
||||||
err := binaryserializer.PutUint32(w, littleEndian, uint32(e))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
// IP address.
|
|
||||||
case [16]byte:
|
|
||||||
_, err := w.Write(e[:])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case *daghash.Hash:
|
|
||||||
_, err := w.Write(e[:])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case *id.ID:
|
|
||||||
return e.Serialize(w)
|
|
||||||
|
|
||||||
case *subnetworkid.SubnetworkID:
|
|
||||||
_, err := w.Write(e[:])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case ServiceFlag:
|
|
||||||
err := binaryserializer.PutUint64(w, littleEndian, uint64(e))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
|
|
||||||
case KaspaNet:
|
|
||||||
err := binaryserializer.PutUint32(w, littleEndian, uint32(e))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return errors.Wrapf(errNoEncodingForType, "couldn't find a way to write type %T", element)
|
|
||||||
}
|
|
||||||
|
|
||||||
// writeElements writes multiple items to w. It is equivalent to multiple
|
|
||||||
// calls to writeElement.
|
|
||||||
func writeElements(w io.Writer, elements ...interface{}) error {
|
|
||||||
for _, element := range elements {
|
|
||||||
err := WriteElement(w, element)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadVarInt reads a variable length integer from r and returns it as a uint64.
|
|
||||||
func ReadVarInt(r io.Reader) (uint64, error) {
|
|
||||||
discriminant, err := binaryserializer.Uint8(r)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var rv uint64
|
|
||||||
switch discriminant {
|
|
||||||
case 0xff:
|
|
||||||
sv, err := binaryserializer.Uint64(r, littleEndian)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
rv = sv
|
|
||||||
|
|
||||||
// The encoding is not canonical if the value could have been
|
|
||||||
// encoded using fewer bytes.
|
|
||||||
min := uint64(0x100000000)
|
|
||||||
if rv < min {
|
|
||||||
return 0, messageError("readVarInt", fmt.Sprintf(
|
|
||||||
errNonCanonicalVarInt, rv, discriminant, min))
|
|
||||||
}
|
|
||||||
|
|
||||||
case 0xfe:
|
|
||||||
sv, err := binaryserializer.Uint32(r, littleEndian)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
rv = uint64(sv)
|
|
||||||
|
|
||||||
// The encoding is not canonical if the value could have been
|
|
||||||
// encoded using fewer bytes.
|
|
||||||
min := uint64(0x10000)
|
|
||||||
if rv < min {
|
|
||||||
return 0, messageError("readVarInt", fmt.Sprintf(
|
|
||||||
errNonCanonicalVarInt, rv, discriminant, min))
|
|
||||||
}
|
|
||||||
|
|
||||||
case 0xfd:
|
|
||||||
sv, err := binaryserializer.Uint16(r, littleEndian)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
rv = uint64(sv)
|
|
||||||
|
|
||||||
// The encoding is not canonical if the value could have been
|
|
||||||
// encoded using fewer bytes.
|
|
||||||
min := uint64(0xfd)
|
|
||||||
if rv < min {
|
|
||||||
return 0, messageError("readVarInt", fmt.Sprintf(
|
|
||||||
errNonCanonicalVarInt, rv, discriminant, min))
|
|
||||||
}
|
|
||||||
|
|
||||||
default:
|
|
||||||
rv = uint64(discriminant)
|
|
||||||
}
|
|
||||||
|
|
||||||
return rv, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteVarInt serializes val to w using a variable number of bytes depending
|
|
||||||
// on its value.
|
|
||||||
func WriteVarInt(w io.Writer, val uint64) error {
|
|
||||||
if val < 0xfd {
|
|
||||||
return binaryserializer.PutUint8(w, uint8(val))
|
|
||||||
}
|
|
||||||
|
|
||||||
if val <= math.MaxUint16 {
|
|
||||||
err := binaryserializer.PutUint8(w, 0xfd)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return binaryserializer.PutUint16(w, littleEndian, uint16(val))
|
|
||||||
}
|
|
||||||
|
|
||||||
if val <= math.MaxUint32 {
|
|
||||||
err := binaryserializer.PutUint8(w, 0xfe)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return binaryserializer.PutUint32(w, littleEndian, uint32(val))
|
|
||||||
}
|
|
||||||
|
|
||||||
err := binaryserializer.PutUint8(w, 0xff)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return binaryserializer.PutUint64(w, littleEndian, val)
|
|
||||||
}
|
|
||||||
|
|
||||||
// VarIntSerializeSize returns the number of bytes it would take to serialize
|
|
||||||
// val as a variable length integer.
|
|
||||||
func VarIntSerializeSize(val uint64) int {
|
|
||||||
// The value is small enough to be represented by itself, so it's
|
|
||||||
// just 1 byte.
|
|
||||||
if val < 0xfd {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
// Discriminant 1 byte plus 2 bytes for the uint16.
|
|
||||||
if val <= math.MaxUint16 {
|
|
||||||
return 3
|
|
||||||
}
|
|
||||||
|
|
||||||
// Discriminant 1 byte plus 4 bytes for the uint32.
|
|
||||||
if val <= math.MaxUint32 {
|
|
||||||
return 5
|
|
||||||
}
|
|
||||||
|
|
||||||
// Discriminant 1 byte plus 8 bytes for the uint64.
|
|
||||||
return 9
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadVarString reads a variable length string from r and returns it as a Go
|
|
||||||
// string. A variable length string is encoded as a variable length integer
|
|
||||||
// containing the length of the string followed by the bytes that represent the
|
|
||||||
// string itself. An error is returned if the length is greater than the
|
|
||||||
// maximum block payload size since it helps protect against memory exhaustion
|
|
||||||
// attacks and forced panics through malformed messages.
|
|
||||||
func ReadVarString(r io.Reader, pver uint32) (string, error) {
|
|
||||||
count, err := ReadVarInt(r)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prevent variable length strings that are larger than the maximum
|
|
||||||
// message size. It would be possible to cause memory exhaustion and
|
|
||||||
// panics without a sane upper bound on this count.
|
|
||||||
if count > MaxMessagePayload {
|
|
||||||
str := fmt.Sprintf("variable length string is too long "+
|
|
||||||
"[count %d, max %d]", count, MaxMessagePayload)
|
|
||||||
return "", messageError("ReadVarString", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
buf := make([]byte, count)
|
|
||||||
_, err = io.ReadFull(r, buf)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return string(buf), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteVarString serializes str to w as a variable length integer containing
|
|
||||||
// the length of the string followed by the bytes that represent the string
|
|
||||||
// itself.
|
|
||||||
func WriteVarString(w io.Writer, str string) error {
|
|
||||||
err := WriteVarInt(w, uint64(len(str)))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
_, err = w.Write([]byte(str))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReadVarBytes reads a variable length byte array. A byte array is encoded
|
|
||||||
// as a varInt containing the length of the array followed by the bytes
|
|
||||||
// themselves. An error is returned if the length is greater than the
|
|
||||||
// passed maxAllowed parameter which helps protect against memory exhaustion
|
|
||||||
// attacks and forced panics through malformed messages. The fieldName
|
|
||||||
// parameter is only used for the error message so it provides more context in
|
|
||||||
// the error.
|
|
||||||
func ReadVarBytes(r io.Reader, pver uint32, maxAllowed uint32,
|
|
||||||
fieldName string) ([]byte, error) {
|
|
||||||
|
|
||||||
count, err := ReadVarInt(r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prevent byte array larger than the max message size. It would
|
|
||||||
// be possible to cause memory exhaustion and panics without a sane
|
|
||||||
// upper bound on this count.
|
|
||||||
if count > uint64(maxAllowed) {
|
|
||||||
str := fmt.Sprintf("%s is larger than the max allowed size "+
|
|
||||||
"[count %d, max %d]", fieldName, count, maxAllowed)
|
|
||||||
return nil, messageError("ReadVarBytes", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
b := make([]byte, count)
|
|
||||||
_, err = io.ReadFull(r, b)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return b, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteVarBytes serializes a variable length byte array to w as a varInt
|
|
||||||
// containing the number of bytes, followed by the bytes themselves.
|
|
||||||
func WriteVarBytes(w io.Writer, pver uint32, bytes []byte) error {
|
|
||||||
slen := uint64(len(bytes))
|
|
||||||
err := WriteVarInt(w, slen)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = w.Write(bytes)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,695 +1,44 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package appmessage
|
package appmessage
|
||||||
|
|
||||||
import (
|
import "github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
"bytes"
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"io"
|
|
||||||
"reflect"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
|
||||||
)
|
|
||||||
|
|
||||||
// mainnetGenesisHash is the hash of the first block in the block DAG for the
|
// mainnetGenesisHash is the hash of the first block in the block DAG for the
|
||||||
// main network (genesis block).
|
// main network (genesis block).
|
||||||
var mainnetGenesisHash = &daghash.Hash{
|
var mainnetGenesisHash = externalapi.NewDomainHashFromByteArray(&[externalapi.DomainHashSize]byte{
|
||||||
0xdc, 0x5f, 0x5b, 0x5b, 0x1d, 0xc2, 0xa7, 0x25,
|
0xdc, 0x5f, 0x5b, 0x5b, 0x1d, 0xc2, 0xa7, 0x25,
|
||||||
0x49, 0xd5, 0x1d, 0x4d, 0xee, 0xd7, 0xa4, 0x8b,
|
0x49, 0xd5, 0x1d, 0x4d, 0xee, 0xd7, 0xa4, 0x8b,
|
||||||
0xaf, 0xd3, 0x14, 0x4b, 0x56, 0x78, 0x98, 0xb1,
|
0xaf, 0xd3, 0x14, 0x4b, 0x56, 0x78, 0x98, 0xb1,
|
||||||
0x8c, 0xfd, 0x9f, 0x69, 0xdd, 0xcf, 0xbb, 0x63,
|
0x8c, 0xfd, 0x9f, 0x69, 0xdd, 0xcf, 0xbb, 0x63,
|
||||||
}
|
})
|
||||||
|
|
||||||
// simnetGenesisHash is the hash of the first block in the block DAG for the
|
// simnetGenesisHash is the hash of the first block in the block DAG for the
|
||||||
// simulation test network.
|
// simulation test network.
|
||||||
var simnetGenesisHash = &daghash.Hash{
|
var simnetGenesisHash = externalapi.NewDomainHashFromByteArray(&[externalapi.DomainHashSize]byte{
|
||||||
0xf6, 0x7a, 0xd7, 0x69, 0x5d, 0x9b, 0x66, 0x2a,
|
0x9d, 0x89, 0xb0, 0x6e, 0xb3, 0x47, 0xb5, 0x6e,
|
||||||
0x72, 0xff, 0x3d, 0x8e, 0xdb, 0xbb, 0x2d, 0xe0,
|
0xcd, 0x6c, 0x63, 0x99, 0x45, 0x91, 0xd5, 0xce,
|
||||||
0xbf, 0xa6, 0x7b, 0x13, 0x97, 0x4b, 0xb9, 0x91,
|
0x9b, 0x43, 0x05, 0xc1, 0xa5, 0x5e, 0x2a, 0xda,
|
||||||
0x0d, 0x11, 0x6d, 0x5c, 0xbd, 0x86, 0x3e, 0x68,
|
0x90, 0x4c, 0xf0, 0x6c, 0x4d, 0x5f, 0xd3, 0x62,
|
||||||
}
|
})
|
||||||
|
|
||||||
// mainnetGenesisMerkleRoot is the hash of the first transaction in the genesis
|
// mainnetGenesisMerkleRoot is the hash of the first transaction in the genesis
|
||||||
// block for the main network.
|
// block for the main network.
|
||||||
var mainnetGenesisMerkleRoot = &daghash.Hash{
|
var mainnetGenesisMerkleRoot = externalapi.NewDomainHashFromByteArray(&[externalapi.DomainHashSize]byte{
|
||||||
0x4a, 0x5e, 0x1e, 0x4b, 0xaa, 0xb8, 0x9f, 0x3a,
|
0x4a, 0x5e, 0x1e, 0x4b, 0xaa, 0xb8, 0x9f, 0x3a,
|
||||||
0x32, 0x51, 0x8a, 0x88, 0xc3, 0x1b, 0xc8, 0x7f,
|
0x32, 0x51, 0x8a, 0x88, 0xc3, 0x1b, 0xc8, 0x7f,
|
||||||
0x61, 0x8f, 0x76, 0x67, 0x3e, 0x2c, 0xc7, 0x7a,
|
0x61, 0x8f, 0x76, 0x67, 0x3e, 0x2c, 0xc7, 0x7a,
|
||||||
0xb2, 0x12, 0x7b, 0x7a, 0xfd, 0xed, 0xa3, 0x3b,
|
0xb2, 0x12, 0x7b, 0x7a, 0xfd, 0xed, 0xa3, 0x3b,
|
||||||
}
|
})
|
||||||
|
|
||||||
var exampleAcceptedIDMerkleRoot = &daghash.Hash{
|
var exampleAcceptedIDMerkleRoot = externalapi.NewDomainHashFromByteArray(&[externalapi.DomainHashSize]byte{
|
||||||
0x09, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C,
|
0x09, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C,
|
||||||
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
|
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
|
||||||
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
|
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
|
||||||
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
|
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
|
||||||
}
|
})
|
||||||
|
|
||||||
var exampleUTXOCommitment = &daghash.Hash{
|
var exampleUTXOCommitment = externalapi.NewDomainHashFromByteArray(&[externalapi.DomainHashSize]byte{
|
||||||
0x10, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C,
|
0x10, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C,
|
||||||
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
|
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
|
||||||
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
|
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
|
||||||
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
|
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
|
||||||
}
|
})
|
||||||
|
|
||||||
// TestElementEncoding tests appmessage encode and decode for various element types. This
|
|
||||||
// is mainly to test the "fast" paths in readElement and writeElement which use
|
|
||||||
// type assertions to avoid reflection when possible.
|
|
||||||
func TestElementEncoding(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
in interface{} // Value to encode
|
|
||||||
buf []byte // Encoded value
|
|
||||||
}{
|
|
||||||
{int32(1), []byte{0x01, 0x00, 0x00, 0x00}},
|
|
||||||
{uint32(256), []byte{0x00, 0x01, 0x00, 0x00}},
|
|
||||||
{
|
|
||||||
int64(65536),
|
|
||||||
[]byte{0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
uint64(4294967296),
|
|
||||||
[]byte{0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
true,
|
|
||||||
[]byte{0x01},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
false,
|
|
||||||
[]byte{0x00},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
[4]byte{0x01, 0x02, 0x03, 0x04},
|
|
||||||
[]byte{0x01, 0x02, 0x03, 0x04},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
MessageCommand(0x10),
|
|
||||||
[]byte{
|
|
||||||
0x10, 0x00, 0x00, 0x00,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
[16]byte{
|
|
||||||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
|
||||||
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
|
|
||||||
},
|
|
||||||
[]byte{
|
|
||||||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
|
||||||
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
(*daghash.Hash)(&[daghash.HashSize]byte{
|
|
||||||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
|
||||||
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
|
|
||||||
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
|
|
||||||
0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
|
|
||||||
}),
|
|
||||||
[]byte{
|
|
||||||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
|
||||||
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
|
|
||||||
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
|
|
||||||
0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ServiceFlag(SFNodeNetwork),
|
|
||||||
[]byte{0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
KaspaNet(Mainnet),
|
|
||||||
[]byte{0x1d, 0xf7, 0xdc, 0x3d},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Write to appmessage format.
|
|
||||||
var buf bytes.Buffer
|
|
||||||
err := WriteElement(&buf, test.in)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("writeElement #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !bytes.Equal(buf.Bytes(), test.buf) {
|
|
||||||
t.Errorf("writeElement #%d\n got: %s want: %s", i,
|
|
||||||
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read from appmessage format.
|
|
||||||
rbuf := bytes.NewReader(test.buf)
|
|
||||||
val := test.in
|
|
||||||
if reflect.ValueOf(test.in).Kind() != reflect.Ptr {
|
|
||||||
val = reflect.New(reflect.TypeOf(test.in)).Interface()
|
|
||||||
}
|
|
||||||
err = ReadElement(rbuf, val)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("readElement #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
ival := val
|
|
||||||
if reflect.ValueOf(test.in).Kind() != reflect.Ptr {
|
|
||||||
ival = reflect.Indirect(reflect.ValueOf(val)).Interface()
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(ival, test.in) {
|
|
||||||
t.Errorf("readElement #%d\n got: %s want: %s", i,
|
|
||||||
spew.Sdump(ival), spew.Sdump(test.in))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestElementEncodingErrors performs negative tests against appmessage encode and decode
|
|
||||||
// of various element types to confirm error paths work correctly.
|
|
||||||
func TestElementEncodingErrors(t *testing.T) {
|
|
||||||
type writeElementReflect int32
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
in interface{} // Value to encode
|
|
||||||
max int // Max size of fixed buffer to induce errors
|
|
||||||
writeErr error // Expected write error
|
|
||||||
readErr error // Expected read error
|
|
||||||
}{
|
|
||||||
{int32(1), 0, io.ErrShortWrite, io.EOF},
|
|
||||||
{uint32(256), 0, io.ErrShortWrite, io.EOF},
|
|
||||||
{int64(65536), 0, io.ErrShortWrite, io.EOF},
|
|
||||||
{true, 0, io.ErrShortWrite, io.EOF},
|
|
||||||
{[4]byte{0x01, 0x02, 0x03, 0x04}, 0, io.ErrShortWrite, io.EOF},
|
|
||||||
{
|
|
||||||
MessageCommand(10),
|
|
||||||
0, io.ErrShortWrite, io.EOF,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
[16]byte{
|
|
||||||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
|
||||||
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
|
|
||||||
},
|
|
||||||
0, io.ErrShortWrite, io.EOF,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
(*daghash.Hash)(&[daghash.HashSize]byte{
|
|
||||||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
|
||||||
0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
|
|
||||||
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
|
|
||||||
0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
|
|
||||||
}),
|
|
||||||
0, io.ErrShortWrite, io.EOF,
|
|
||||||
},
|
|
||||||
{ServiceFlag(SFNodeNetwork), 0, io.ErrShortWrite, io.EOF},
|
|
||||||
{KaspaNet(Mainnet), 0, io.ErrShortWrite, io.EOF},
|
|
||||||
// Type with no supported encoding.
|
|
||||||
{writeElementReflect(0), 0, errNoEncodingForType, errNoEncodingForType},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Encode to appmessage format.
|
|
||||||
w := newFixedWriter(test.max)
|
|
||||||
err := WriteElement(w, test.in)
|
|
||||||
if !errors.Is(err, test.writeErr) {
|
|
||||||
t.Errorf("writeElement #%d wrong error got: %v, want: %v",
|
|
||||||
i, err, test.writeErr)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode from appmessage format.
|
|
||||||
r := newFixedReader(test.max, nil)
|
|
||||||
val := test.in
|
|
||||||
if reflect.ValueOf(test.in).Kind() != reflect.Ptr {
|
|
||||||
val = reflect.New(reflect.TypeOf(test.in)).Interface()
|
|
||||||
}
|
|
||||||
err = ReadElement(r, val)
|
|
||||||
if !errors.Is(err, test.readErr) {
|
|
||||||
t.Errorf("readElement #%d wrong error got: %v, want: %v",
|
|
||||||
i, err, test.readErr)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestVarIntEncoding tests appmessage encode and decode for variable length integers.
|
|
||||||
func TestVarIntEncoding(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
value uint64 // Value to encode
|
|
||||||
buf []byte // Encoded value
|
|
||||||
}{
|
|
||||||
// Latest protocol version.
|
|
||||||
// Single byte
|
|
||||||
{0, []byte{0x00}},
|
|
||||||
// Max single byte
|
|
||||||
{0xfc, []byte{0xfc}},
|
|
||||||
// Min 2-byte
|
|
||||||
{0xfd, []byte{0xfd, 0x0fd, 0x00}},
|
|
||||||
// Max 2-byte
|
|
||||||
{0xffff, []byte{0xfd, 0xff, 0xff}},
|
|
||||||
// Min 4-byte
|
|
||||||
{0x10000, []byte{0xfe, 0x00, 0x00, 0x01, 0x00}},
|
|
||||||
// Max 4-byte
|
|
||||||
{0xffffffff, []byte{0xfe, 0xff, 0xff, 0xff, 0xff}},
|
|
||||||
// Min 8-byte
|
|
||||||
{
|
|
||||||
0x100000000,
|
|
||||||
[]byte{0xff, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00},
|
|
||||||
},
|
|
||||||
// Max 8-byte
|
|
||||||
{
|
|
||||||
0xffffffffffffffff,
|
|
||||||
[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Encode to appmessage format.
|
|
||||||
buf := &bytes.Buffer{}
|
|
||||||
err := WriteVarInt(buf, test.value)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("WriteVarInt #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !bytes.Equal(buf.Bytes(), test.buf) {
|
|
||||||
t.Errorf("WriteVarInt #%d\n got: %s want: %s", i,
|
|
||||||
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode from appmessage format.
|
|
||||||
rbuf := bytes.NewReader(test.buf)
|
|
||||||
val, err := ReadVarInt(rbuf)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("ReadVarInt #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if val != test.value {
|
|
||||||
t.Errorf("ReadVarInt #%d\n got: %x want: %x", i,
|
|
||||||
val, test.value)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestVarIntEncodingErrors performs negative tests against appmessage encode and decode
|
|
||||||
// of variable length integers to confirm error paths work correctly.
|
|
||||||
func TestVarIntEncodingErrors(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
in uint64 // Value to encode
|
|
||||||
buf []byte // Encoded value
|
|
||||||
max int // Max size of fixed buffer to induce errors
|
|
||||||
writeErr error // Expected write error
|
|
||||||
readErr error // Expected read error
|
|
||||||
}{
|
|
||||||
// Force errors on discriminant.
|
|
||||||
{0, []byte{0x00}, 0, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force errors on 2-byte read/write.
|
|
||||||
{0xfd, []byte{0xfd}, 0, io.ErrShortWrite, io.EOF}, // error on writing length
|
|
||||||
{0xfd, []byte{0xfd}, 2, io.ErrShortWrite, io.ErrUnexpectedEOF}, // error on writing actual data
|
|
||||||
// Force errors on 4-byte read/write.
|
|
||||||
{0x10000, []byte{0xfe}, 0, io.ErrShortWrite, io.EOF}, // error on writing length
|
|
||||||
{0x10000, []byte{0xfe}, 2, io.ErrShortWrite, io.ErrUnexpectedEOF}, // error on writing actual data
|
|
||||||
// Force errors on 8-byte read/write.
|
|
||||||
{0x100000000, []byte{0xff}, 0, io.ErrShortWrite, io.EOF}, // error on writing length
|
|
||||||
{0x100000000, []byte{0xff}, 2, io.ErrShortWrite, io.ErrUnexpectedEOF}, // error on writing actual data
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Encode to appmessage format.
|
|
||||||
w := newFixedWriter(test.max)
|
|
||||||
err := WriteVarInt(w, test.in)
|
|
||||||
if !errors.Is(err, test.writeErr) {
|
|
||||||
t.Errorf("WriteVarInt #%d wrong error got: %v, want: %v",
|
|
||||||
i, err, test.writeErr)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode from appmessage format.
|
|
||||||
r := newFixedReader(test.max, test.buf)
|
|
||||||
_, err = ReadVarInt(r)
|
|
||||||
if !errors.Is(err, test.readErr) {
|
|
||||||
t.Errorf("ReadVarInt #%d wrong error got: %v, want: %v",
|
|
||||||
i, err, test.readErr)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestVarIntNonCanonical ensures variable length integers that are not encoded
|
|
||||||
// canonically return the expected error.
|
|
||||||
func TestVarIntNonCanonical(t *testing.T) {
|
|
||||||
pver := ProtocolVersion
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
name string // Test name for easier identification
|
|
||||||
in []byte // Value to decode
|
|
||||||
pver uint32 // Protocol version for appmessage encoding
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
"0 encoded with 3 bytes", []byte{0xfd, 0x00, 0x00},
|
|
||||||
pver,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"max single-byte value encoded with 3 bytes",
|
|
||||||
[]byte{0xfd, 0xfc, 0x00}, pver,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"0 encoded with 5 bytes",
|
|
||||||
[]byte{0xfe, 0x00, 0x00, 0x00, 0x00}, pver,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"max three-byte value encoded with 5 bytes",
|
|
||||||
[]byte{0xfe, 0xff, 0xff, 0x00, 0x00}, pver,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"0 encoded with 9 bytes",
|
|
||||||
[]byte{0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
|
||||||
pver,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"max five-byte value encoded with 9 bytes",
|
|
||||||
[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00},
|
|
||||||
pver,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Decode from appmessage format.
|
|
||||||
rbuf := bytes.NewReader(test.in)
|
|
||||||
val, err := ReadVarInt(rbuf)
|
|
||||||
if msgErr := &(MessageError{}); !errors.As(err, &msgErr) {
|
|
||||||
t.Errorf("ReadVarInt #%d (%s) unexpected error %v", i,
|
|
||||||
test.name, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if val != 0 {
|
|
||||||
t.Errorf("ReadVarInt #%d (%s)\n got: %d want: 0", i,
|
|
||||||
test.name, val)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestVarIntEncoding tests the serialize size for variable length integers.
|
|
||||||
func TestVarIntSerializeSize(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
val uint64 // Value to get the serialized size for
|
|
||||||
size int // Expected serialized size
|
|
||||||
}{
|
|
||||||
// Single byte
|
|
||||||
{0, 1},
|
|
||||||
// Max single byte
|
|
||||||
{0xfc, 1},
|
|
||||||
// Min 2-byte
|
|
||||||
{0xfd, 3},
|
|
||||||
// Max 2-byte
|
|
||||||
{0xffff, 3},
|
|
||||||
// Min 4-byte
|
|
||||||
{0x10000, 5},
|
|
||||||
// Max 4-byte
|
|
||||||
{0xffffffff, 5},
|
|
||||||
// Min 8-byte
|
|
||||||
{0x100000000, 9},
|
|
||||||
// Max 8-byte
|
|
||||||
{0xffffffffffffffff, 9},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
serializedSize := VarIntSerializeSize(test.val)
|
|
||||||
if serializedSize != test.size {
|
|
||||||
t.Errorf("VarIntSerializeSize #%d got: %d, want: %d", i,
|
|
||||||
serializedSize, test.size)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestVarStringEncoding tests appmessage encode and decode for variable length strings.
|
|
||||||
func TestVarStringEncoding(t *testing.T) {
|
|
||||||
pver := ProtocolVersion
|
|
||||||
|
|
||||||
// str256 is a string that takes a 2-byte varint to encode.
|
|
||||||
str256 := strings.Repeat("test", 64)
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
in string // String to encode
|
|
||||||
out string // String to decoded value
|
|
||||||
buf []byte // Encoded value
|
|
||||||
pver uint32 // Protocol version for appmessage encoding
|
|
||||||
}{
|
|
||||||
// Latest protocol version.
|
|
||||||
// Empty string
|
|
||||||
{"", "", []byte{0x00}, pver},
|
|
||||||
// Single byte varint + string
|
|
||||||
{"Test", "Test", append([]byte{0x04}, []byte("Test")...), pver},
|
|
||||||
// 2-byte varint + string
|
|
||||||
{str256, str256, append([]byte{0xfd, 0x00, 0x01}, []byte(str256)...), pver},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Encode to appmessage format.
|
|
||||||
var buf bytes.Buffer
|
|
||||||
err := WriteVarString(&buf, test.in)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("WriteVarString #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !bytes.Equal(buf.Bytes(), test.buf) {
|
|
||||||
t.Errorf("WriteVarString #%d\n got: %s want: %s", i,
|
|
||||||
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode from appmessage format.
|
|
||||||
rbuf := bytes.NewReader(test.buf)
|
|
||||||
val, err := ReadVarString(rbuf, test.pver)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("ReadVarString #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if val != test.out {
|
|
||||||
t.Errorf("ReadVarString #%d\n got: %s want: %s", i,
|
|
||||||
val, test.out)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestVarStringEncodingErrors performs negative tests against appmessage encode and
|
|
||||||
// decode of variable length strings to confirm error paths work correctly.
|
|
||||||
func TestVarStringEncodingErrors(t *testing.T) {
|
|
||||||
pver := ProtocolVersion
|
|
||||||
|
|
||||||
// str256 is a string that takes a 2-byte varint to encode.
|
|
||||||
str256 := strings.Repeat("test", 64)
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
in string // Value to encode
|
|
||||||
buf []byte // Encoded value
|
|
||||||
pver uint32 // Protocol version for appmessage encoding
|
|
||||||
max int // Max size of fixed buffer to induce errors
|
|
||||||
writeErr error // Expected write error
|
|
||||||
readErr error // Expected read error
|
|
||||||
}{
|
|
||||||
// Latest protocol version with intentional read/write errors.
|
|
||||||
// Force errors on empty string.
|
|
||||||
{"", []byte{0x00}, pver, 0, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error on single byte varint + string.
|
|
||||||
{"Test", []byte{0x04}, pver, 2, io.ErrShortWrite, io.ErrUnexpectedEOF},
|
|
||||||
// Force errors on 2-byte varint + string.
|
|
||||||
{str256, []byte{0xfd}, pver, 2, io.ErrShortWrite, io.ErrUnexpectedEOF},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Encode to appmessage format.
|
|
||||||
w := newFixedWriter(test.max)
|
|
||||||
err := WriteVarString(w, test.in)
|
|
||||||
if !errors.Is(err, test.writeErr) {
|
|
||||||
t.Errorf("WriteVarString #%d wrong error got: %v, want: %v",
|
|
||||||
i, err, test.writeErr)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode from appmessage format.
|
|
||||||
r := newFixedReader(test.max, test.buf)
|
|
||||||
_, err = ReadVarString(r, test.pver)
|
|
||||||
if !errors.Is(err, test.readErr) {
|
|
||||||
t.Errorf("ReadVarString #%d wrong error got: %v, want: %v",
|
|
||||||
i, err, test.readErr)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestVarStringOverflowErrors performs tests to ensure deserializing variable
|
|
||||||
// length strings intentionally crafted to use large values for the string
|
|
||||||
// length are handled properly. This could otherwise potentially be used as an
|
|
||||||
// attack vector.
|
|
||||||
func TestVarStringOverflowErrors(t *testing.T) {
|
|
||||||
pver := ProtocolVersion
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
buf []byte // Encoded value
|
|
||||||
pver uint32 // Protocol version for appmessage encoding
|
|
||||||
err error // Expected error
|
|
||||||
}{
|
|
||||||
{[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
|
||||||
pver, &MessageError{}},
|
|
||||||
{[]byte{0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
|
|
||||||
pver, &MessageError{}},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Decode from appmessage format.
|
|
||||||
rbuf := bytes.NewReader(test.buf)
|
|
||||||
_, err := ReadVarString(rbuf, test.pver)
|
|
||||||
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
|
|
||||||
t.Errorf("ReadVarString #%d wrong error got: %v, "+
|
|
||||||
"want: %v", i, err, reflect.TypeOf(test.err))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestVarBytesEncoding tests appmessage encode and decode for variable length byte array.
|
|
||||||
func TestVarBytesEncoding(t *testing.T) {
|
|
||||||
pver := ProtocolVersion
|
|
||||||
|
|
||||||
// bytes256 is a byte array that takes a 2-byte varint to encode.
|
|
||||||
bytes256 := bytes.Repeat([]byte{0x01}, 256)
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
in []byte // Byte Array to write
|
|
||||||
buf []byte // Encoded value
|
|
||||||
pver uint32 // Protocol version for appmessage encoding
|
|
||||||
}{
|
|
||||||
// Latest protocol version.
|
|
||||||
// Empty byte array
|
|
||||||
{[]byte{}, []byte{0x00}, pver},
|
|
||||||
// Single byte varint + byte array
|
|
||||||
{[]byte{0x01}, []byte{0x01, 0x01}, pver},
|
|
||||||
// 2-byte varint + byte array
|
|
||||||
{bytes256, append([]byte{0xfd, 0x00, 0x01}, bytes256...), pver},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Encode to appmessage format.
|
|
||||||
var buf bytes.Buffer
|
|
||||||
err := WriteVarBytes(&buf, test.pver, test.in)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("WriteVarBytes #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !bytes.Equal(buf.Bytes(), test.buf) {
|
|
||||||
t.Errorf("WriteVarBytes #%d\n got: %s want: %s", i,
|
|
||||||
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode from appmessage format.
|
|
||||||
rbuf := bytes.NewReader(test.buf)
|
|
||||||
val, err := ReadVarBytes(rbuf, test.pver, MaxMessagePayload,
|
|
||||||
"test payload")
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("ReadVarBytes #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !bytes.Equal(buf.Bytes(), test.buf) {
|
|
||||||
t.Errorf("ReadVarBytes #%d\n got: %s want: %s", i,
|
|
||||||
val, test.buf)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestVarBytesEncodingErrors performs negative tests against appmessage encode and
|
|
||||||
// decode of variable length byte arrays to confirm error paths work correctly.
|
|
||||||
func TestVarBytesEncodingErrors(t *testing.T) {
|
|
||||||
pver := ProtocolVersion
|
|
||||||
|
|
||||||
// bytes256 is a byte array that takes a 2-byte varint to encode.
|
|
||||||
bytes256 := bytes.Repeat([]byte{0x01}, 256)
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
in []byte // Byte Array to write
|
|
||||||
buf []byte // Encoded value
|
|
||||||
pver uint32 // Protocol version for appmessage encoding
|
|
||||||
max int // Max size of fixed buffer to induce errors
|
|
||||||
writeErr error // Expected write error
|
|
||||||
readErr error // Expected read error
|
|
||||||
}{
|
|
||||||
// Latest protocol version with intentional read/write errors.
|
|
||||||
// Force errors on empty byte array.
|
|
||||||
{[]byte{}, []byte{0x00}, pver, 0, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error on single byte varint + byte array.
|
|
||||||
{[]byte{0x01, 0x02, 0x03}, []byte{0x04}, pver, 2, io.ErrShortWrite, io.ErrUnexpectedEOF},
|
|
||||||
// Force errors on 2-byte varint + byte array.
|
|
||||||
{bytes256, []byte{0xfd}, pver, 2, io.ErrShortWrite, io.ErrUnexpectedEOF},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Encode to appmessage format.
|
|
||||||
w := newFixedWriter(test.max)
|
|
||||||
err := WriteVarBytes(w, test.pver, test.in)
|
|
||||||
if !errors.Is(err, test.writeErr) {
|
|
||||||
t.Errorf("WriteVarBytes #%d wrong error got: %v, want: %v",
|
|
||||||
i, err, test.writeErr)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode from appmessage format.
|
|
||||||
r := newFixedReader(test.max, test.buf)
|
|
||||||
_, err = ReadVarBytes(r, test.pver, MaxMessagePayload,
|
|
||||||
"test payload")
|
|
||||||
if !errors.Is(err, test.readErr) {
|
|
||||||
t.Errorf("ReadVarBytes #%d wrong error got: %v, want: %v",
|
|
||||||
i, err, test.readErr)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestVarBytesOverflowErrors performs tests to ensure deserializing variable
|
|
||||||
// length byte arrays intentionally crafted to use large values for the array
|
|
||||||
// length are handled properly. This could otherwise potentially be used as an
|
|
||||||
// attack vector.
|
|
||||||
func TestVarBytesOverflowErrors(t *testing.T) {
|
|
||||||
pver := ProtocolVersion
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
buf []byte // Encoded value
|
|
||||||
pver uint32 // Protocol version for appmessage encoding
|
|
||||||
err error // Expected error
|
|
||||||
}{
|
|
||||||
{[]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
|
||||||
pver, &MessageError{}},
|
|
||||||
{[]byte{0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01},
|
|
||||||
pver, &MessageError{}},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Decode from appmessage format.
|
|
||||||
rbuf := bytes.NewReader(test.buf)
|
|
||||||
_, err := ReadVarBytes(rbuf, test.pver, MaxMessagePayload,
|
|
||||||
"test payload")
|
|
||||||
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
|
|
||||||
t.Errorf("ReadVarBytes #%d wrong error got: %v, "+
|
|
||||||
"want: %v", i, err, reflect.TypeOf(test.err))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|||||||
364
app/appmessage/domainconverters.go
Normal file
364
app/appmessage/domainconverters.go
Normal file
@@ -0,0 +1,364 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/hex"
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/utils/blockheader"
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/utils/hashes"
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/utils/utxo"
|
||||||
|
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/utils/subnetworks"
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/utils/transactionid"
|
||||||
|
"github.com/kaspanet/kaspad/util/mstime"
|
||||||
|
)
|
||||||
|
|
||||||
|
// DomainBlockToMsgBlock converts an externalapi.DomainBlock to MsgBlock
|
||||||
|
func DomainBlockToMsgBlock(domainBlock *externalapi.DomainBlock) *MsgBlock {
|
||||||
|
msgTxs := make([]*MsgTx, 0, len(domainBlock.Transactions))
|
||||||
|
for _, domainTransaction := range domainBlock.Transactions {
|
||||||
|
msgTxs = append(msgTxs, DomainTransactionToMsgTx(domainTransaction))
|
||||||
|
}
|
||||||
|
return &MsgBlock{
|
||||||
|
Header: *DomainBlockHeaderToBlockHeader(domainBlock.Header),
|
||||||
|
Transactions: msgTxs,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DomainBlockHeaderToBlockHeader converts an externalapi.BlockHeader to MsgBlockHeader
|
||||||
|
func DomainBlockHeaderToBlockHeader(domainBlockHeader externalapi.BlockHeader) *MsgBlockHeader {
|
||||||
|
return &MsgBlockHeader{
|
||||||
|
Version: domainBlockHeader.Version(),
|
||||||
|
ParentHashes: domainBlockHeader.ParentHashes(),
|
||||||
|
HashMerkleRoot: domainBlockHeader.HashMerkleRoot(),
|
||||||
|
AcceptedIDMerkleRoot: domainBlockHeader.AcceptedIDMerkleRoot(),
|
||||||
|
UTXOCommitment: domainBlockHeader.UTXOCommitment(),
|
||||||
|
Timestamp: mstime.UnixMilliseconds(domainBlockHeader.TimeInMilliseconds()),
|
||||||
|
Bits: domainBlockHeader.Bits(),
|
||||||
|
Nonce: domainBlockHeader.Nonce(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MsgBlockToDomainBlock converts a MsgBlock to externalapi.DomainBlock
|
||||||
|
func MsgBlockToDomainBlock(msgBlock *MsgBlock) *externalapi.DomainBlock {
|
||||||
|
transactions := make([]*externalapi.DomainTransaction, 0, len(msgBlock.Transactions))
|
||||||
|
for _, msgTx := range msgBlock.Transactions {
|
||||||
|
transactions = append(transactions, MsgTxToDomainTransaction(msgTx))
|
||||||
|
}
|
||||||
|
|
||||||
|
return &externalapi.DomainBlock{
|
||||||
|
Header: BlockHeaderToDomainBlockHeader(&msgBlock.Header),
|
||||||
|
Transactions: transactions,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BlockHeaderToDomainBlockHeader converts a MsgBlockHeader to externalapi.BlockHeader
|
||||||
|
func BlockHeaderToDomainBlockHeader(blockHeader *MsgBlockHeader) externalapi.BlockHeader {
|
||||||
|
return blockheader.NewImmutableBlockHeader(
|
||||||
|
blockHeader.Version,
|
||||||
|
blockHeader.ParentHashes,
|
||||||
|
blockHeader.HashMerkleRoot,
|
||||||
|
blockHeader.AcceptedIDMerkleRoot,
|
||||||
|
blockHeader.UTXOCommitment,
|
||||||
|
blockHeader.Timestamp.UnixMilliseconds(),
|
||||||
|
blockHeader.Bits,
|
||||||
|
blockHeader.Nonce,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DomainTransactionToMsgTx converts an externalapi.DomainTransaction into an MsgTx
|
||||||
|
func DomainTransactionToMsgTx(domainTransaction *externalapi.DomainTransaction) *MsgTx {
|
||||||
|
txIns := make([]*TxIn, 0, len(domainTransaction.Inputs))
|
||||||
|
for _, input := range domainTransaction.Inputs {
|
||||||
|
txIns = append(txIns, domainTransactionInputToTxIn(input))
|
||||||
|
}
|
||||||
|
|
||||||
|
txOuts := make([]*TxOut, 0, len(domainTransaction.Outputs))
|
||||||
|
for _, output := range domainTransaction.Outputs {
|
||||||
|
txOuts = append(txOuts, domainTransactionOutputToTxOut(output))
|
||||||
|
}
|
||||||
|
|
||||||
|
return &MsgTx{
|
||||||
|
Version: domainTransaction.Version,
|
||||||
|
TxIn: txIns,
|
||||||
|
TxOut: txOuts,
|
||||||
|
LockTime: domainTransaction.LockTime,
|
||||||
|
SubnetworkID: domainTransaction.SubnetworkID,
|
||||||
|
Gas: domainTransaction.Gas,
|
||||||
|
Payload: domainTransaction.Payload,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func domainTransactionOutputToTxOut(domainTransactionOutput *externalapi.DomainTransactionOutput) *TxOut {
|
||||||
|
return &TxOut{
|
||||||
|
Value: domainTransactionOutput.Value,
|
||||||
|
ScriptPubKey: domainTransactionOutput.ScriptPublicKey,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func domainTransactionInputToTxIn(domainTransactionInput *externalapi.DomainTransactionInput) *TxIn {
|
||||||
|
return &TxIn{
|
||||||
|
PreviousOutpoint: *domainOutpointToOutpoint(domainTransactionInput.PreviousOutpoint),
|
||||||
|
SignatureScript: domainTransactionInput.SignatureScript,
|
||||||
|
Sequence: domainTransactionInput.Sequence,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func domainOutpointToOutpoint(domainOutpoint externalapi.DomainOutpoint) *Outpoint {
|
||||||
|
return NewOutpoint(
|
||||||
|
&domainOutpoint.TransactionID,
|
||||||
|
domainOutpoint.Index)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MsgTxToDomainTransaction converts an MsgTx into externalapi.DomainTransaction
|
||||||
|
func MsgTxToDomainTransaction(msgTx *MsgTx) *externalapi.DomainTransaction {
|
||||||
|
transactionInputs := make([]*externalapi.DomainTransactionInput, 0, len(msgTx.TxIn))
|
||||||
|
for _, txIn := range msgTx.TxIn {
|
||||||
|
transactionInputs = append(transactionInputs, txInToDomainTransactionInput(txIn))
|
||||||
|
}
|
||||||
|
|
||||||
|
transactionOutputs := make([]*externalapi.DomainTransactionOutput, 0, len(msgTx.TxOut))
|
||||||
|
for _, txOut := range msgTx.TxOut {
|
||||||
|
transactionOutputs = append(transactionOutputs, txOutToDomainTransactionOutput(txOut))
|
||||||
|
}
|
||||||
|
|
||||||
|
payload := make([]byte, 0)
|
||||||
|
if msgTx.Payload != nil {
|
||||||
|
payload = msgTx.Payload
|
||||||
|
}
|
||||||
|
|
||||||
|
return &externalapi.DomainTransaction{
|
||||||
|
Version: msgTx.Version,
|
||||||
|
Inputs: transactionInputs,
|
||||||
|
Outputs: transactionOutputs,
|
||||||
|
LockTime: msgTx.LockTime,
|
||||||
|
SubnetworkID: msgTx.SubnetworkID,
|
||||||
|
Gas: msgTx.Gas,
|
||||||
|
Payload: payload,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func txOutToDomainTransactionOutput(txOut *TxOut) *externalapi.DomainTransactionOutput {
|
||||||
|
return &externalapi.DomainTransactionOutput{
|
||||||
|
Value: txOut.Value,
|
||||||
|
ScriptPublicKey: txOut.ScriptPubKey,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func txInToDomainTransactionInput(txIn *TxIn) *externalapi.DomainTransactionInput {
|
||||||
|
return &externalapi.DomainTransactionInput{
|
||||||
|
PreviousOutpoint: *outpointToDomainOutpoint(&txIn.PreviousOutpoint), //TODO
|
||||||
|
SignatureScript: txIn.SignatureScript,
|
||||||
|
Sequence: txIn.Sequence,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func outpointToDomainOutpoint(outpoint *Outpoint) *externalapi.DomainOutpoint {
|
||||||
|
return &externalapi.DomainOutpoint{
|
||||||
|
TransactionID: outpoint.TxID,
|
||||||
|
Index: outpoint.Index,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// RPCTransactionToDomainTransaction converts RPCTransactions to DomainTransactions
|
||||||
|
func RPCTransactionToDomainTransaction(rpcTransaction *RPCTransaction) (*externalapi.DomainTransaction, error) {
|
||||||
|
inputs := make([]*externalapi.DomainTransactionInput, len(rpcTransaction.Inputs))
|
||||||
|
for i, input := range rpcTransaction.Inputs {
|
||||||
|
transactionID, err := transactionid.FromString(input.PreviousOutpoint.TransactionID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
previousOutpoint := &externalapi.DomainOutpoint{
|
||||||
|
TransactionID: *transactionID,
|
||||||
|
Index: input.PreviousOutpoint.Index,
|
||||||
|
}
|
||||||
|
signatureScript, err := hex.DecodeString(input.SignatureScript)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
inputs[i] = &externalapi.DomainTransactionInput{
|
||||||
|
PreviousOutpoint: *previousOutpoint,
|
||||||
|
SignatureScript: signatureScript,
|
||||||
|
Sequence: input.Sequence,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
outputs := make([]*externalapi.DomainTransactionOutput, len(rpcTransaction.Outputs))
|
||||||
|
for i, output := range rpcTransaction.Outputs {
|
||||||
|
scriptPublicKey, err := hex.DecodeString(output.ScriptPublicKey.Script)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
outputs[i] = &externalapi.DomainTransactionOutput{
|
||||||
|
Value: output.Amount,
|
||||||
|
ScriptPublicKey: &externalapi.ScriptPublicKey{Script: scriptPublicKey, Version: output.ScriptPublicKey.Version},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
subnetworkID, err := subnetworks.FromString(rpcTransaction.SubnetworkID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
payload, err := hex.DecodeString(rpcTransaction.Payload)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &externalapi.DomainTransaction{
|
||||||
|
Version: rpcTransaction.Version,
|
||||||
|
Inputs: inputs,
|
||||||
|
Outputs: outputs,
|
||||||
|
LockTime: rpcTransaction.LockTime,
|
||||||
|
SubnetworkID: *subnetworkID,
|
||||||
|
Gas: rpcTransaction.LockTime,
|
||||||
|
Payload: payload,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DomainTransactionToRPCTransaction converts DomainTransactions to RPCTransactions
|
||||||
|
func DomainTransactionToRPCTransaction(transaction *externalapi.DomainTransaction) *RPCTransaction {
|
||||||
|
inputs := make([]*RPCTransactionInput, len(transaction.Inputs))
|
||||||
|
for i, input := range transaction.Inputs {
|
||||||
|
transactionID := input.PreviousOutpoint.TransactionID.String()
|
||||||
|
previousOutpoint := &RPCOutpoint{
|
||||||
|
TransactionID: transactionID,
|
||||||
|
Index: input.PreviousOutpoint.Index,
|
||||||
|
}
|
||||||
|
signatureScript := hex.EncodeToString(input.SignatureScript)
|
||||||
|
inputs[i] = &RPCTransactionInput{
|
||||||
|
PreviousOutpoint: previousOutpoint,
|
||||||
|
SignatureScript: signatureScript,
|
||||||
|
Sequence: input.Sequence,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
outputs := make([]*RPCTransactionOutput, len(transaction.Outputs))
|
||||||
|
for i, output := range transaction.Outputs {
|
||||||
|
scriptPublicKey := hex.EncodeToString(output.ScriptPublicKey.Script)
|
||||||
|
outputs[i] = &RPCTransactionOutput{
|
||||||
|
Amount: output.Value,
|
||||||
|
ScriptPublicKey: &RPCScriptPublicKey{Script: scriptPublicKey, Version: output.ScriptPublicKey.Version},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
subnetworkID := transaction.SubnetworkID.String()
|
||||||
|
payload := hex.EncodeToString(transaction.Payload)
|
||||||
|
return &RPCTransaction{
|
||||||
|
Version: transaction.Version,
|
||||||
|
Inputs: inputs,
|
||||||
|
Outputs: outputs,
|
||||||
|
LockTime: transaction.LockTime,
|
||||||
|
SubnetworkID: subnetworkID,
|
||||||
|
Gas: transaction.LockTime,
|
||||||
|
Payload: payload,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// OutpointAndUTXOEntryPairsToDomainOutpointAndUTXOEntryPairs converts
|
||||||
|
// OutpointAndUTXOEntryPairs to domain OutpointAndUTXOEntryPairs
|
||||||
|
func OutpointAndUTXOEntryPairsToDomainOutpointAndUTXOEntryPairs(
|
||||||
|
outpointAndUTXOEntryPairs []*OutpointAndUTXOEntryPair) []*externalapi.OutpointAndUTXOEntryPair {
|
||||||
|
|
||||||
|
domainOutpointAndUTXOEntryPairs := make([]*externalapi.OutpointAndUTXOEntryPair, len(outpointAndUTXOEntryPairs))
|
||||||
|
for i, outpointAndUTXOEntryPair := range outpointAndUTXOEntryPairs {
|
||||||
|
domainOutpointAndUTXOEntryPairs[i] = &externalapi.OutpointAndUTXOEntryPair{
|
||||||
|
Outpoint: &externalapi.DomainOutpoint{
|
||||||
|
TransactionID: outpointAndUTXOEntryPair.Outpoint.TxID,
|
||||||
|
Index: outpointAndUTXOEntryPair.Outpoint.Index,
|
||||||
|
},
|
||||||
|
UTXOEntry: utxo.NewUTXOEntry(
|
||||||
|
outpointAndUTXOEntryPair.UTXOEntry.Amount,
|
||||||
|
outpointAndUTXOEntryPair.UTXOEntry.ScriptPublicKey,
|
||||||
|
outpointAndUTXOEntryPair.UTXOEntry.IsCoinbase,
|
||||||
|
outpointAndUTXOEntryPair.UTXOEntry.BlockDAAScore,
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return domainOutpointAndUTXOEntryPairs
|
||||||
|
}
|
||||||
|
|
||||||
|
// DomainOutpointAndUTXOEntryPairsToOutpointAndUTXOEntryPairs converts
|
||||||
|
// domain OutpointAndUTXOEntryPairs to OutpointAndUTXOEntryPairs
|
||||||
|
func DomainOutpointAndUTXOEntryPairsToOutpointAndUTXOEntryPairs(
|
||||||
|
outpointAndUTXOEntryPairs []*externalapi.OutpointAndUTXOEntryPair) []*OutpointAndUTXOEntryPair {
|
||||||
|
|
||||||
|
domainOutpointAndUTXOEntryPairs := make([]*OutpointAndUTXOEntryPair, len(outpointAndUTXOEntryPairs))
|
||||||
|
for i, outpointAndUTXOEntryPair := range outpointAndUTXOEntryPairs {
|
||||||
|
domainOutpointAndUTXOEntryPairs[i] = &OutpointAndUTXOEntryPair{
|
||||||
|
Outpoint: &Outpoint{
|
||||||
|
TxID: outpointAndUTXOEntryPair.Outpoint.TransactionID,
|
||||||
|
Index: outpointAndUTXOEntryPair.Outpoint.Index,
|
||||||
|
},
|
||||||
|
UTXOEntry: &UTXOEntry{
|
||||||
|
Amount: outpointAndUTXOEntryPair.UTXOEntry.Amount(),
|
||||||
|
ScriptPublicKey: outpointAndUTXOEntryPair.UTXOEntry.ScriptPublicKey(),
|
||||||
|
IsCoinbase: outpointAndUTXOEntryPair.UTXOEntry.IsCoinbase(),
|
||||||
|
BlockDAAScore: outpointAndUTXOEntryPair.UTXOEntry.BlockDAAScore(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return domainOutpointAndUTXOEntryPairs
|
||||||
|
}
|
||||||
|
|
||||||
|
// DomainBlockToRPCBlock converts DomainBlocks to RPCBlocks
|
||||||
|
func DomainBlockToRPCBlock(block *externalapi.DomainBlock) *RPCBlock {
|
||||||
|
header := &RPCBlockHeader{
|
||||||
|
Version: uint32(block.Header.Version()),
|
||||||
|
ParentHashes: hashes.ToStrings(block.Header.ParentHashes()),
|
||||||
|
HashMerkleRoot: block.Header.HashMerkleRoot().String(),
|
||||||
|
AcceptedIDMerkleRoot: block.Header.AcceptedIDMerkleRoot().String(),
|
||||||
|
UTXOCommitment: block.Header.UTXOCommitment().String(),
|
||||||
|
Timestamp: block.Header.TimeInMilliseconds(),
|
||||||
|
Bits: block.Header.Bits(),
|
||||||
|
Nonce: block.Header.Nonce(),
|
||||||
|
}
|
||||||
|
transactions := make([]*RPCTransaction, len(block.Transactions))
|
||||||
|
for i, transaction := range block.Transactions {
|
||||||
|
transactions[i] = DomainTransactionToRPCTransaction(transaction)
|
||||||
|
}
|
||||||
|
return &RPCBlock{
|
||||||
|
Header: header,
|
||||||
|
Transactions: transactions,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// RPCBlockToDomainBlock converts `block` into a DomainBlock
|
||||||
|
func RPCBlockToDomainBlock(block *RPCBlock) (*externalapi.DomainBlock, error) {
|
||||||
|
parentHashes := make([]*externalapi.DomainHash, len(block.Header.ParentHashes))
|
||||||
|
for i, parentHash := range block.Header.ParentHashes {
|
||||||
|
domainParentHashes, err := externalapi.NewDomainHashFromString(parentHash)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
parentHashes[i] = domainParentHashes
|
||||||
|
}
|
||||||
|
hashMerkleRoot, err := externalapi.NewDomainHashFromString(block.Header.HashMerkleRoot)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
acceptedIDMerkleRoot, err := externalapi.NewDomainHashFromString(block.Header.AcceptedIDMerkleRoot)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
utxoCommitment, err := externalapi.NewDomainHashFromString(block.Header.UTXOCommitment)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
header := blockheader.NewImmutableBlockHeader(
|
||||||
|
uint16(block.Header.Version),
|
||||||
|
parentHashes,
|
||||||
|
hashMerkleRoot,
|
||||||
|
acceptedIDMerkleRoot,
|
||||||
|
utxoCommitment,
|
||||||
|
block.Header.Timestamp,
|
||||||
|
block.Header.Bits,
|
||||||
|
block.Header.Nonce)
|
||||||
|
transactions := make([]*externalapi.DomainTransaction, len(block.Transactions))
|
||||||
|
for i, transaction := range block.Transactions {
|
||||||
|
domainTransaction, err := RPCTransactionToDomainTransaction(transaction)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
transactions[i] = domainTransaction
|
||||||
|
}
|
||||||
|
return &externalapi.DomainBlock{
|
||||||
|
Header: header,
|
||||||
|
Transactions: transactions,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
@@ -32,3 +32,16 @@ func (e *MessageError) Error() string {
|
|||||||
func messageError(f string, desc string) *MessageError {
|
func messageError(f string, desc string) *MessageError {
|
||||||
return &MessageError{Func: f, Description: desc}
|
return &MessageError{Func: f, Description: desc}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RPCError represents an error arriving from the RPC
|
||||||
|
type RPCError struct {
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
|
||||||
|
// RPCErrorf formats according to a format specifier and returns the string
|
||||||
|
// as an RPCError.
|
||||||
|
func RPCErrorf(format string, args ...interface{}) *RPCError {
|
||||||
|
return &RPCError{
|
||||||
|
Message: fmt.Sprintf(format, args...),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,56 +0,0 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package appmessage
|
|
||||||
|
|
||||||
import "io"
|
|
||||||
|
|
||||||
// fakeMessage implements the Message interface and is used to force encode
|
|
||||||
// errors in messages.
|
|
||||||
type fakeMessage struct {
|
|
||||||
command MessageCommand
|
|
||||||
payload []byte
|
|
||||||
forceEncodeErr bool
|
|
||||||
forceLenErr bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// KaspaDecode doesn't do anything. It just satisfies the appmessage.Message
|
|
||||||
// interface.
|
|
||||||
func (msg *fakeMessage) KaspaDecode(r io.Reader, pver uint32) error {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// KaspaEncode writes the payload field of the fake message or forces an error
|
|
||||||
// if the forceEncodeErr flag of the fake message is set. It also satisfies the
|
|
||||||
// appmessage.Message interface.
|
|
||||||
func (msg *fakeMessage) KaspaEncode(w io.Writer, pver uint32) error {
|
|
||||||
if msg.forceEncodeErr {
|
|
||||||
err := &MessageError{
|
|
||||||
Func: "fakeMessage.KaspaEncode",
|
|
||||||
Description: "intentional error",
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := w.Write(msg.payload)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the command field of the fake message and satisfies the
|
|
||||||
// Message interface.
|
|
||||||
func (msg *fakeMessage) Command() MessageCommand {
|
|
||||||
return msg.command
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the length of the payload field of fake message
|
|
||||||
// or a smaller value if the forceLenErr flag of the fake message is set. It
|
|
||||||
// satisfies the Message interface.
|
|
||||||
func (msg *fakeMessage) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
lenp := uint32(len(msg.payload))
|
|
||||||
if msg.forceLenErr {
|
|
||||||
return lenp - 1
|
|
||||||
}
|
|
||||||
|
|
||||||
return lenp
|
|
||||||
}
|
|
||||||
@@ -11,13 +11,16 @@ import (
|
|||||||
|
|
||||||
// MaxMessagePayload is the maximum bytes a message can be regardless of other
|
// MaxMessagePayload is the maximum bytes a message can be regardless of other
|
||||||
// individual limits imposed by messages themselves.
|
// individual limits imposed by messages themselves.
|
||||||
const MaxMessagePayload = (1024 * 1024 * 32) // 32MB
|
const MaxMessagePayload = 1024 * 1024 * 32 // 32MB
|
||||||
|
|
||||||
// MessageCommand is a number in the header of a message that represents its type.
|
// MessageCommand is a number in the header of a message that represents its type.
|
||||||
type MessageCommand uint32
|
type MessageCommand uint32
|
||||||
|
|
||||||
func (cmd MessageCommand) String() string {
|
func (cmd MessageCommand) String() string {
|
||||||
cmdString, ok := MessageCommandToString[cmd]
|
cmdString, ok := ProtocolMessageCommandToString[cmd]
|
||||||
|
if !ok {
|
||||||
|
cmdString, ok = RPCMessageCommandToString[cmd]
|
||||||
|
}
|
||||||
if !ok {
|
if !ok {
|
||||||
cmdString = "unknown command"
|
cmdString = "unknown command"
|
||||||
}
|
}
|
||||||
@@ -26,54 +29,225 @@ func (cmd MessageCommand) String() string {
|
|||||||
|
|
||||||
// Commands used in kaspa message headers which describe the type of message.
|
// Commands used in kaspa message headers which describe the type of message.
|
||||||
const (
|
const (
|
||||||
|
// protocol
|
||||||
CmdVersion MessageCommand = iota
|
CmdVersion MessageCommand = iota
|
||||||
CmdVerAck
|
CmdVerAck
|
||||||
CmdRequestAddresses
|
CmdRequestAddresses
|
||||||
CmdAddresses
|
CmdAddresses
|
||||||
CmdRequestIBDBlocks
|
CmdRequestHeaders
|
||||||
CmdBlock
|
CmdBlock
|
||||||
CmdTx
|
CmdTx
|
||||||
CmdPing
|
CmdPing
|
||||||
CmdPong
|
CmdPong
|
||||||
CmdRequestBlockLocator
|
CmdRequestBlockLocator
|
||||||
CmdBlockLocator
|
CmdBlockLocator
|
||||||
CmdSelectedTip
|
|
||||||
CmdRequestSelectedTip
|
|
||||||
CmdInvRelayBlock
|
CmdInvRelayBlock
|
||||||
CmdRequestRelayBlocks
|
CmdRequestRelayBlocks
|
||||||
CmdInvTransaction
|
CmdInvTransaction
|
||||||
CmdRequestTransactions
|
CmdRequestTransactions
|
||||||
CmdIBDBlock
|
CmdIBDBlock
|
||||||
CmdRequestNextIBDBlocks
|
CmdDoneHeaders
|
||||||
CmdDoneIBDBlocks
|
|
||||||
CmdTransactionNotFound
|
CmdTransactionNotFound
|
||||||
CmdReject
|
CmdReject
|
||||||
|
CmdHeader
|
||||||
|
CmdRequestNextHeaders
|
||||||
|
CmdRequestPruningPointUTXOSetAndBlock
|
||||||
|
CmdPruningPointUTXOSetChunk
|
||||||
|
CmdRequestIBDBlocks
|
||||||
|
CmdUnexpectedPruningPoint
|
||||||
|
CmdRequestPruningPointHash
|
||||||
|
CmdPruningPointHash
|
||||||
|
CmdIBDBlockLocator
|
||||||
|
CmdIBDBlockLocatorHighestHash
|
||||||
|
CmdIBDBlockLocatorHighestHashNotFound
|
||||||
|
CmdBlockHeaders
|
||||||
|
CmdRequestNextPruningPointUTXOSetChunk
|
||||||
|
CmdDonePruningPointUTXOSetChunks
|
||||||
|
|
||||||
|
// rpc
|
||||||
|
CmdGetCurrentNetworkRequestMessage
|
||||||
|
CmdGetCurrentNetworkResponseMessage
|
||||||
|
CmdSubmitBlockRequestMessage
|
||||||
|
CmdSubmitBlockResponseMessage
|
||||||
|
CmdGetBlockTemplateRequestMessage
|
||||||
|
CmdGetBlockTemplateResponseMessage
|
||||||
|
CmdGetBlockTemplateTransactionMessage
|
||||||
|
CmdNotifyBlockAddedRequestMessage
|
||||||
|
CmdNotifyBlockAddedResponseMessage
|
||||||
|
CmdBlockAddedNotificationMessage
|
||||||
|
CmdGetPeerAddressesRequestMessage
|
||||||
|
CmdGetPeerAddressesResponseMessage
|
||||||
|
CmdGetSelectedTipHashRequestMessage
|
||||||
|
CmdGetSelectedTipHashResponseMessage
|
||||||
|
CmdGetMempoolEntryRequestMessage
|
||||||
|
CmdGetMempoolEntryResponseMessage
|
||||||
|
CmdGetConnectedPeerInfoRequestMessage
|
||||||
|
CmdGetConnectedPeerInfoResponseMessage
|
||||||
|
CmdAddPeerRequestMessage
|
||||||
|
CmdAddPeerResponseMessage
|
||||||
|
CmdSubmitTransactionRequestMessage
|
||||||
|
CmdSubmitTransactionResponseMessage
|
||||||
|
CmdNotifyVirtualSelectedParentChainChangedRequestMessage
|
||||||
|
CmdNotifyVirtualSelectedParentChainChangedResponseMessage
|
||||||
|
CmdVirtualSelectedParentChainChangedNotificationMessage
|
||||||
|
CmdGetBlockRequestMessage
|
||||||
|
CmdGetBlockResponseMessage
|
||||||
|
CmdGetSubnetworkRequestMessage
|
||||||
|
CmdGetSubnetworkResponseMessage
|
||||||
|
CmdGetVirtualSelectedParentChainFromBlockRequestMessage
|
||||||
|
CmdGetVirtualSelectedParentChainFromBlockResponseMessage
|
||||||
|
CmdGetBlocksRequestMessage
|
||||||
|
CmdGetBlocksResponseMessage
|
||||||
|
CmdGetBlockCountRequestMessage
|
||||||
|
CmdGetBlockCountResponseMessage
|
||||||
|
CmdGetBlockDAGInfoRequestMessage
|
||||||
|
CmdGetBlockDAGInfoResponseMessage
|
||||||
|
CmdResolveFinalityConflictRequestMessage
|
||||||
|
CmdResolveFinalityConflictResponseMessage
|
||||||
|
CmdNotifyFinalityConflictsRequestMessage
|
||||||
|
CmdNotifyFinalityConflictsResponseMessage
|
||||||
|
CmdFinalityConflictNotificationMessage
|
||||||
|
CmdFinalityConflictResolvedNotificationMessage
|
||||||
|
CmdGetMempoolEntriesRequestMessage
|
||||||
|
CmdGetMempoolEntriesResponseMessage
|
||||||
|
CmdShutDownRequestMessage
|
||||||
|
CmdShutDownResponseMessage
|
||||||
|
CmdGetHeadersRequestMessage
|
||||||
|
CmdGetHeadersResponseMessage
|
||||||
|
CmdNotifyUTXOsChangedRequestMessage
|
||||||
|
CmdNotifyUTXOsChangedResponseMessage
|
||||||
|
CmdUTXOsChangedNotificationMessage
|
||||||
|
CmdStopNotifyingUTXOsChangedRequestMessage
|
||||||
|
CmdStopNotifyingUTXOsChangedResponseMessage
|
||||||
|
CmdGetUTXOsByAddressesRequestMessage
|
||||||
|
CmdGetUTXOsByAddressesResponseMessage
|
||||||
|
CmdGetVirtualSelectedParentBlueScoreRequestMessage
|
||||||
|
CmdGetVirtualSelectedParentBlueScoreResponseMessage
|
||||||
|
CmdNotifyVirtualSelectedParentBlueScoreChangedRequestMessage
|
||||||
|
CmdNotifyVirtualSelectedParentBlueScoreChangedResponseMessage
|
||||||
|
CmdVirtualSelectedParentBlueScoreChangedNotificationMessage
|
||||||
|
CmdBanRequestMessage
|
||||||
|
CmdBanResponseMessage
|
||||||
|
CmdUnbanRequestMessage
|
||||||
|
CmdUnbanResponseMessage
|
||||||
|
CmdGetInfoRequestMessage
|
||||||
|
CmdGetInfoResponseMessage
|
||||||
|
CmdNotifyPruningPointUTXOSetOverrideRequestMessage
|
||||||
|
CmdNotifyPruningPointUTXOSetOverrideResponseMessage
|
||||||
|
CmdPruningPointUTXOSetOverrideNotificationMessage
|
||||||
|
CmdStopNotifyingPruningPointUTXOSetOverrideRequestMessage
|
||||||
|
CmdStopNotifyingPruningPointUTXOSetOverrideResponseMessage
|
||||||
)
|
)
|
||||||
|
|
||||||
// MessageCommandToString maps all MessageCommands to their string representation
|
// ProtocolMessageCommandToString maps all MessageCommands to their string representation
|
||||||
var MessageCommandToString = map[MessageCommand]string{
|
var ProtocolMessageCommandToString = map[MessageCommand]string{
|
||||||
CmdVersion: "Version",
|
CmdVersion: "Version",
|
||||||
CmdVerAck: "VerAck",
|
CmdVerAck: "VerAck",
|
||||||
CmdRequestAddresses: "RequestAddresses",
|
CmdRequestAddresses: "RequestAddresses",
|
||||||
CmdAddresses: "Addresses",
|
CmdAddresses: "Addresses",
|
||||||
CmdRequestIBDBlocks: "RequestBlocks",
|
CmdRequestHeaders: "RequestHeaders",
|
||||||
CmdBlock: "Block",
|
CmdBlock: "Block",
|
||||||
CmdTx: "Tx",
|
CmdTx: "Tx",
|
||||||
CmdPing: "Ping",
|
CmdPing: "Ping",
|
||||||
CmdPong: "Pong",
|
CmdPong: "Pong",
|
||||||
CmdRequestBlockLocator: "RequestBlockLocator",
|
CmdRequestBlockLocator: "RequestBlockLocator",
|
||||||
CmdBlockLocator: "BlockLocator",
|
CmdBlockLocator: "BlockLocator",
|
||||||
CmdSelectedTip: "SelectedTip",
|
CmdInvRelayBlock: "InvRelayBlock",
|
||||||
CmdRequestSelectedTip: "RequestSelectedTip",
|
CmdRequestRelayBlocks: "RequestRelayBlocks",
|
||||||
CmdInvRelayBlock: "InvRelayBlock",
|
CmdInvTransaction: "InvTransaction",
|
||||||
CmdRequestRelayBlocks: "RequestRelayBlocks",
|
CmdRequestTransactions: "RequestTransactions",
|
||||||
CmdInvTransaction: "InvTransaction",
|
CmdIBDBlock: "IBDBlock",
|
||||||
CmdRequestTransactions: "RequestTransactions",
|
CmdDoneHeaders: "DoneHeaders",
|
||||||
CmdIBDBlock: "IBDBlock",
|
CmdTransactionNotFound: "TransactionNotFound",
|
||||||
CmdRequestNextIBDBlocks: "RequestNextIBDBlocks",
|
CmdReject: "Reject",
|
||||||
CmdDoneIBDBlocks: "DoneIBDBlocks",
|
CmdHeader: "Header",
|
||||||
CmdTransactionNotFound: "TransactionNotFound",
|
CmdRequestNextHeaders: "RequestNextHeaders",
|
||||||
CmdReject: "Reject",
|
CmdRequestPruningPointUTXOSetAndBlock: "RequestPruningPointUTXOSetAndBlock",
|
||||||
|
CmdPruningPointUTXOSetChunk: "PruningPointUTXOSetChunk",
|
||||||
|
CmdRequestIBDBlocks: "RequestIBDBlocks",
|
||||||
|
CmdUnexpectedPruningPoint: "UnexpectedPruningPoint",
|
||||||
|
CmdRequestPruningPointHash: "RequestPruningPointHashHash",
|
||||||
|
CmdPruningPointHash: "PruningPointHash",
|
||||||
|
CmdIBDBlockLocator: "IBDBlockLocator",
|
||||||
|
CmdIBDBlockLocatorHighestHash: "IBDBlockLocatorHighestHash",
|
||||||
|
CmdIBDBlockLocatorHighestHashNotFound: "IBDBlockLocatorHighestHashNotFound",
|
||||||
|
CmdBlockHeaders: "BlockHeaders",
|
||||||
|
CmdRequestNextPruningPointUTXOSetChunk: "RequestNextPruningPointUTXOSetChunk",
|
||||||
|
CmdDonePruningPointUTXOSetChunks: "DonePruningPointUTXOSetChunks",
|
||||||
|
}
|
||||||
|
|
||||||
|
// RPCMessageCommandToString maps all MessageCommands to their string representation
|
||||||
|
var RPCMessageCommandToString = map[MessageCommand]string{
|
||||||
|
CmdGetCurrentNetworkRequestMessage: "GetCurrentNetworkRequest",
|
||||||
|
CmdGetCurrentNetworkResponseMessage: "GetCurrentNetworkResponse",
|
||||||
|
CmdSubmitBlockRequestMessage: "SubmitBlockRequest",
|
||||||
|
CmdSubmitBlockResponseMessage: "SubmitBlockResponse",
|
||||||
|
CmdGetBlockTemplateRequestMessage: "GetBlockTemplateRequest",
|
||||||
|
CmdGetBlockTemplateResponseMessage: "GetBlockTemplateResponse",
|
||||||
|
CmdGetBlockTemplateTransactionMessage: "CmdGetBlockTemplateTransaction",
|
||||||
|
CmdNotifyBlockAddedRequestMessage: "NotifyBlockAddedRequest",
|
||||||
|
CmdNotifyBlockAddedResponseMessage: "NotifyBlockAddedResponse",
|
||||||
|
CmdBlockAddedNotificationMessage: "BlockAddedNotification",
|
||||||
|
CmdGetPeerAddressesRequestMessage: "GetPeerAddressesRequest",
|
||||||
|
CmdGetPeerAddressesResponseMessage: "GetPeerAddressesResponse",
|
||||||
|
CmdGetSelectedTipHashRequestMessage: "GetSelectedTipHashRequest",
|
||||||
|
CmdGetSelectedTipHashResponseMessage: "GetSelectedTipHashResponse",
|
||||||
|
CmdGetMempoolEntryRequestMessage: "GetMempoolEntryRequest",
|
||||||
|
CmdGetMempoolEntryResponseMessage: "GetMempoolEntryResponse",
|
||||||
|
CmdGetConnectedPeerInfoRequestMessage: "GetConnectedPeerInfoRequest",
|
||||||
|
CmdGetConnectedPeerInfoResponseMessage: "GetConnectedPeerInfoResponse",
|
||||||
|
CmdAddPeerRequestMessage: "AddPeerRequest",
|
||||||
|
CmdAddPeerResponseMessage: "AddPeerResponse",
|
||||||
|
CmdSubmitTransactionRequestMessage: "SubmitTransactionRequest",
|
||||||
|
CmdSubmitTransactionResponseMessage: "SubmitTransactionResponse",
|
||||||
|
CmdNotifyVirtualSelectedParentChainChangedRequestMessage: "NotifyVirtualSelectedParentChainChangedRequest",
|
||||||
|
CmdNotifyVirtualSelectedParentChainChangedResponseMessage: "NotifyVirtualSelectedParentChainChangedResponse",
|
||||||
|
CmdVirtualSelectedParentChainChangedNotificationMessage: "VirtualSelectedParentChainChangedNotification",
|
||||||
|
CmdGetBlockRequestMessage: "GetBlockRequest",
|
||||||
|
CmdGetBlockResponseMessage: "GetBlockResponse",
|
||||||
|
CmdGetSubnetworkRequestMessage: "GetSubnetworkRequest",
|
||||||
|
CmdGetSubnetworkResponseMessage: "GetSubnetworkResponse",
|
||||||
|
CmdGetVirtualSelectedParentChainFromBlockRequestMessage: "GetVirtualSelectedParentChainFromBlockRequest",
|
||||||
|
CmdGetVirtualSelectedParentChainFromBlockResponseMessage: "GetVirtualSelectedParentChainFromBlockResponse",
|
||||||
|
CmdGetBlocksRequestMessage: "GetBlocksRequest",
|
||||||
|
CmdGetBlocksResponseMessage: "GetBlocksResponse",
|
||||||
|
CmdGetBlockCountRequestMessage: "GetBlockCountRequest",
|
||||||
|
CmdGetBlockCountResponseMessage: "GetBlockCountResponse",
|
||||||
|
CmdGetBlockDAGInfoRequestMessage: "GetBlockDAGInfoRequest",
|
||||||
|
CmdGetBlockDAGInfoResponseMessage: "GetBlockDAGInfoResponse",
|
||||||
|
CmdResolveFinalityConflictRequestMessage: "ResolveFinalityConflictRequest",
|
||||||
|
CmdResolveFinalityConflictResponseMessage: "ResolveFinalityConflictResponse",
|
||||||
|
CmdNotifyFinalityConflictsRequestMessage: "NotifyFinalityConflictsRequest",
|
||||||
|
CmdNotifyFinalityConflictsResponseMessage: "NotifyFinalityConflictsResponse",
|
||||||
|
CmdFinalityConflictNotificationMessage: "FinalityConflictNotification",
|
||||||
|
CmdFinalityConflictResolvedNotificationMessage: "FinalityConflictResolvedNotification",
|
||||||
|
CmdGetMempoolEntriesRequestMessage: "GetMempoolEntriesRequest",
|
||||||
|
CmdGetMempoolEntriesResponseMessage: "GetMempoolEntriesResponse",
|
||||||
|
CmdGetHeadersRequestMessage: "GetHeadersRequest",
|
||||||
|
CmdGetHeadersResponseMessage: "GetHeadersResponse",
|
||||||
|
CmdNotifyUTXOsChangedRequestMessage: "NotifyUTXOsChangedRequest",
|
||||||
|
CmdNotifyUTXOsChangedResponseMessage: "NotifyUTXOsChangedResponse",
|
||||||
|
CmdUTXOsChangedNotificationMessage: "UTXOsChangedNotification",
|
||||||
|
CmdStopNotifyingUTXOsChangedRequestMessage: "StopNotifyingUTXOsChangedRequest",
|
||||||
|
CmdStopNotifyingUTXOsChangedResponseMessage: "StopNotifyingUTXOsChangedResponse",
|
||||||
|
CmdGetUTXOsByAddressesRequestMessage: "GetUTXOsByAddressesRequest",
|
||||||
|
CmdGetUTXOsByAddressesResponseMessage: "GetUTXOsByAddressesResponse",
|
||||||
|
CmdGetVirtualSelectedParentBlueScoreRequestMessage: "GetVirtualSelectedParentBlueScoreRequest",
|
||||||
|
CmdGetVirtualSelectedParentBlueScoreResponseMessage: "GetVirtualSelectedParentBlueScoreResponse",
|
||||||
|
CmdNotifyVirtualSelectedParentBlueScoreChangedRequestMessage: "NotifyVirtualSelectedParentBlueScoreChangedRequest",
|
||||||
|
CmdNotifyVirtualSelectedParentBlueScoreChangedResponseMessage: "NotifyVirtualSelectedParentBlueScoreChangedResponse",
|
||||||
|
CmdVirtualSelectedParentBlueScoreChangedNotificationMessage: "VirtualSelectedParentBlueScoreChangedNotification",
|
||||||
|
CmdBanRequestMessage: "BanRequest",
|
||||||
|
CmdBanResponseMessage: "BanResponse",
|
||||||
|
CmdUnbanRequestMessage: "UnbanRequest",
|
||||||
|
CmdUnbanResponseMessage: "UnbanResponse",
|
||||||
|
CmdGetInfoRequestMessage: "GetInfoRequest",
|
||||||
|
CmdGetInfoResponseMessage: "GeInfoResponse",
|
||||||
|
CmdNotifyPruningPointUTXOSetOverrideRequestMessage: "NotifyPruningPointUTXOSetOverrideRequest",
|
||||||
|
CmdNotifyPruningPointUTXOSetOverrideResponseMessage: "NotifyPruningPointUTXOSetOverrideResponse",
|
||||||
|
CmdPruningPointUTXOSetOverrideNotificationMessage: "PruningPointUTXOSetOverrideNotification",
|
||||||
|
CmdStopNotifyingPruningPointUTXOSetOverrideRequestMessage: "StopNotifyingPruningPointUTXOSetOverrideRequest",
|
||||||
|
CmdStopNotifyingPruningPointUTXOSetOverrideResponseMessage: "StopNotifyingPruningPointUTXOSetOverrideResponse",
|
||||||
}
|
}
|
||||||
|
|
||||||
// Message is an interface that describes a kaspa message. A type that
|
// Message is an interface that describes a kaspa message. A type that
|
||||||
|
|||||||
@@ -1,75 +0,0 @@
|
|||||||
// Copyright (c) 2013-2015 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package appmessage
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/kaspanet/kaspad/util/subnetworkid"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MaxAddressesPerMsg is the maximum number of addresses that can be in a single
|
|
||||||
// kaspa Addresses message (MsgAddresses).
|
|
||||||
const MaxAddressesPerMsg = 1000
|
|
||||||
|
|
||||||
// MsgAddresses implements the Message interface and represents a kaspa
|
|
||||||
// Addresses message. It is used to provide a list of known active peers on the
|
|
||||||
// network. An active peer is considered one that has transmitted a message
|
|
||||||
// within the last 3 hours. Nodes which have not transmitted in that time
|
|
||||||
// frame should be forgotten. Each message is limited to a maximum number of
|
|
||||||
// addresses, which is currently 1000. As a result, multiple messages must
|
|
||||||
// be used to relay the full list.
|
|
||||||
//
|
|
||||||
// Use the AddAddress function to build up the list of known addresses when
|
|
||||||
// sending an Addresses message to another peer.
|
|
||||||
type MsgAddresses struct {
|
|
||||||
baseMessage
|
|
||||||
IncludeAllSubnetworks bool
|
|
||||||
SubnetworkID *subnetworkid.SubnetworkID
|
|
||||||
AddrList []*NetAddress
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddAddress adds a known active peer to the message.
|
|
||||||
func (msg *MsgAddresses) AddAddress(na *NetAddress) error {
|
|
||||||
if len(msg.AddrList)+1 > MaxAddressesPerMsg {
|
|
||||||
str := fmt.Sprintf("too many addresses in message [max %d]",
|
|
||||||
MaxAddressesPerMsg)
|
|
||||||
return messageError("MsgAddresses.AddAddress", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
msg.AddrList = append(msg.AddrList, na)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddAddresses adds multiple known active peers to the message.
|
|
||||||
func (msg *MsgAddresses) AddAddresses(netAddrs ...*NetAddress) error {
|
|
||||||
for _, na := range netAddrs {
|
|
||||||
err := msg.AddAddress(na)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ClearAddresses removes all addresses from the message.
|
|
||||||
func (msg *MsgAddresses) ClearAddresses() {
|
|
||||||
msg.AddrList = []*NetAddress{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgAddresses) Command() MessageCommand {
|
|
||||||
return CmdAddresses
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgAddresses returns a new kaspa Addresses message that conforms to the
|
|
||||||
// Message interface. See MsgAddresses for details.
|
|
||||||
func NewMsgAddresses(includeAllSubnetworks bool, subnetworkID *subnetworkid.SubnetworkID) *MsgAddresses {
|
|
||||||
return &MsgAddresses{
|
|
||||||
IncludeAllSubnetworks: includeAllSubnetworks,
|
|
||||||
SubnetworkID: subnetworkID,
|
|
||||||
AddrList: make([]*NetAddress, 0, MaxAddressesPerMsg),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package appmessage
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TestAddresses tests the MsgAddresses API.
|
|
||||||
func TestAddresses(t *testing.T) {
|
|
||||||
// Ensure the command is expected value.
|
|
||||||
wantCmd := MessageCommand(3)
|
|
||||||
msg := NewMsgAddresses(false, nil)
|
|
||||||
if cmd := msg.Command(); cmd != wantCmd {
|
|
||||||
t.Errorf("NewMsgAddresses: wrong command - got %v want %v",
|
|
||||||
cmd, wantCmd)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure NetAddresses are added properly.
|
|
||||||
tcpAddr := &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 16111}
|
|
||||||
na := NewNetAddress(tcpAddr, SFNodeNetwork)
|
|
||||||
err := msg.AddAddress(na)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("AddAddress: %v", err)
|
|
||||||
}
|
|
||||||
if msg.AddrList[0] != na {
|
|
||||||
t.Errorf("AddAddress: wrong address added - got %v, want %v",
|
|
||||||
spew.Sprint(msg.AddrList[0]), spew.Sprint(na))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure the address list is cleared properly.
|
|
||||||
msg.ClearAddresses()
|
|
||||||
if len(msg.AddrList) != 0 {
|
|
||||||
t.Errorf("ClearAddresses: address list is not empty - "+
|
|
||||||
"got %v [%v], want %v", len(msg.AddrList),
|
|
||||||
spew.Sprint(msg.AddrList[0]), 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure adding more than the max allowed addresses per message returns
|
|
||||||
// error.
|
|
||||||
for i := 0; i < MaxAddressesPerMsg+1; i++ {
|
|
||||||
err = msg.AddAddress(na)
|
|
||||||
}
|
|
||||||
if err == nil {
|
|
||||||
t.Errorf("AddAddress: expected error on too many addresses " +
|
|
||||||
"not received")
|
|
||||||
}
|
|
||||||
err = msg.AddAddresses(na)
|
|
||||||
if err == nil {
|
|
||||||
t.Errorf("AddAddresses: expected error on too many addresses " +
|
|
||||||
"not received")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,250 +0,0 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package appmessage
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/kaspanet/kaspad/util/subnetworkid"
|
|
||||||
|
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
|
||||||
)
|
|
||||||
|
|
||||||
// defaultTransactionAlloc is the default size used for the backing array
|
|
||||||
// for transactions. The transaction array will dynamically grow as needed, but
|
|
||||||
// this figure is intended to provide enough space for the number of
|
|
||||||
// transactions in the vast majority of blocks without needing to grow the
|
|
||||||
// backing array multiple times.
|
|
||||||
const defaultTransactionAlloc = 2048
|
|
||||||
|
|
||||||
// MaxMassPerBlock is the maximum total transaction mass a block may have.
|
|
||||||
const MaxMassPerBlock = 10000000
|
|
||||||
|
|
||||||
// MaxMassPerTx is the maximum total mass a transaction may have.
|
|
||||||
const MaxMassPerTx = MaxMassPerBlock / 2
|
|
||||||
|
|
||||||
// MaxTxPerBlock is the maximum number of transactions that could
|
|
||||||
// possibly fit into a block.
|
|
||||||
const MaxTxPerBlock = (MaxMassPerBlock / minTxPayload) + 1
|
|
||||||
|
|
||||||
// TxLoc holds locator data for the offset and length of where a transaction is
|
|
||||||
// located within a MsgBlock data buffer.
|
|
||||||
type TxLoc struct {
|
|
||||||
TxStart int
|
|
||||||
TxLen int
|
|
||||||
}
|
|
||||||
|
|
||||||
// MsgBlock implements the Message interface and represents a kaspa
|
|
||||||
// block message. It is used to deliver block and transaction information in
|
|
||||||
// response to a getdata message (MsgGetData) for a given block hash.
|
|
||||||
type MsgBlock struct {
|
|
||||||
baseMessage
|
|
||||||
Header BlockHeader
|
|
||||||
Transactions []*MsgTx
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddTransaction adds a transaction to the message.
|
|
||||||
func (msg *MsgBlock) AddTransaction(tx *MsgTx) {
|
|
||||||
msg.Transactions = append(msg.Transactions, tx)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ClearTransactions removes all transactions from the message.
|
|
||||||
func (msg *MsgBlock) ClearTransactions() {
|
|
||||||
msg.Transactions = make([]*MsgTx, 0, defaultTransactionAlloc)
|
|
||||||
}
|
|
||||||
|
|
||||||
// KaspaDecode decodes r using the kaspa protocol encoding into the receiver.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
// See Deserialize for decoding blocks stored to disk, such as in a database, as
|
|
||||||
// opposed to decoding blocks from the appmessage.
|
|
||||||
func (msg *MsgBlock) KaspaDecode(r io.Reader, pver uint32) error {
|
|
||||||
err := readBlockHeader(r, pver, &msg.Header)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
txCount, err := ReadVarInt(r)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prevent more transactions than could possibly fit into a block.
|
|
||||||
// It would be possible to cause memory exhaustion and panics without
|
|
||||||
// a sane upper bound on this count.
|
|
||||||
if txCount > MaxTxPerBlock {
|
|
||||||
str := fmt.Sprintf("too many transactions to fit into a block "+
|
|
||||||
"[count %d, max %d]", txCount, MaxTxPerBlock)
|
|
||||||
return messageError("MsgBlock.KaspaDecode", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
msg.Transactions = make([]*MsgTx, 0, txCount)
|
|
||||||
for i := uint64(0); i < txCount; i++ {
|
|
||||||
tx := MsgTx{}
|
|
||||||
err := tx.KaspaDecode(r, pver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
msg.Transactions = append(msg.Transactions, &tx)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deserialize decodes a block from r into the receiver using a format that is
|
|
||||||
// suitable for long-term storage such as a database while respecting the
|
|
||||||
// Version field in the block. This function differs from KaspaDecode in that
|
|
||||||
// KaspaDecode decodes from the kaspa appmessage protocol as it was sent across the
|
|
||||||
// network. The appmessage encoding can technically differ depending on the protocol
|
|
||||||
// version and doesn't even really need to match the format of a stored block at
|
|
||||||
// all. As of the time this comment was written, the encoded block is the same
|
|
||||||
// in both instances, but there is a distinct difference and separating the two
|
|
||||||
// allows the API to be flexible enough to deal with changes.
|
|
||||||
func (msg *MsgBlock) Deserialize(r io.Reader) error {
|
|
||||||
// At the current time, there is no difference between the appmessage encoding
|
|
||||||
// at protocol version 0 and the stable long-term storage format. As
|
|
||||||
// a result, make use of KaspaDecode.
|
|
||||||
return msg.KaspaDecode(r, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeserializeTxLoc decodes r in the same manner Deserialize does, but it takes
|
|
||||||
// a byte buffer instead of a generic reader and returns a slice containing the
|
|
||||||
// start and length of each transaction within the raw data that is being
|
|
||||||
// deserialized.
|
|
||||||
func (msg *MsgBlock) DeserializeTxLoc(r *bytes.Buffer) ([]TxLoc, error) {
|
|
||||||
fullLen := r.Len()
|
|
||||||
|
|
||||||
// At the current time, there is no difference between the appmessage encoding
|
|
||||||
// at protocol version 0 and the stable long-term storage format. As
|
|
||||||
// a result, make use of existing appmessage protocol functions.
|
|
||||||
err := readBlockHeader(r, 0, &msg.Header)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
txCount, err := ReadVarInt(r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prevent more transactions than could possibly fit into a block.
|
|
||||||
// It would be possible to cause memory exhaustion and panics without
|
|
||||||
// a sane upper bound on this count.
|
|
||||||
if txCount > MaxTxPerBlock {
|
|
||||||
str := fmt.Sprintf("too many transactions to fit into a block "+
|
|
||||||
"[count %d, max %d]", txCount, MaxTxPerBlock)
|
|
||||||
return nil, messageError("MsgBlock.DeserializeTxLoc", str)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deserialize each transaction while keeping track of its location
|
|
||||||
// within the byte stream.
|
|
||||||
msg.Transactions = make([]*MsgTx, 0, txCount)
|
|
||||||
txLocs := make([]TxLoc, txCount)
|
|
||||||
for i := uint64(0); i < txCount; i++ {
|
|
||||||
txLocs[i].TxStart = fullLen - r.Len()
|
|
||||||
tx := MsgTx{}
|
|
||||||
err := tx.Deserialize(r)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
msg.Transactions = append(msg.Transactions, &tx)
|
|
||||||
txLocs[i].TxLen = (fullLen - r.Len()) - txLocs[i].TxStart
|
|
||||||
}
|
|
||||||
|
|
||||||
return txLocs, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// KaspaEncode encodes the receiver to w using the kaspa protocol encoding.
|
|
||||||
// This is part of the Message interface implementation.
|
|
||||||
// See Serialize for encoding blocks to be stored to disk, such as in a
|
|
||||||
// database, as opposed to encoding blocks for the appmessage.
|
|
||||||
func (msg *MsgBlock) KaspaEncode(w io.Writer, pver uint32) error {
|
|
||||||
err := writeBlockHeader(w, pver, &msg.Header)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = WriteVarInt(w, uint64(len(msg.Transactions)))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tx := range msg.Transactions {
|
|
||||||
err = tx.KaspaEncode(w, pver)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Serialize encodes the block to w using a format that suitable for long-term
|
|
||||||
// storage such as a database while respecting the Version field in the block.
|
|
||||||
// This function differs from KaspaEncode in that KaspaEncode encodes the block to
|
|
||||||
// the kaspa appmessage protocol in order to be sent across the network. The appmessage
|
|
||||||
// encoding can technically differ depending on the protocol version and doesn't
|
|
||||||
// even really need to match the format of a stored block at all. As of the
|
|
||||||
// time this comment was written, the encoded block is the same in both
|
|
||||||
// instances, but there is a distinct difference and separating the two allows
|
|
||||||
// the API to be flexible enough to deal with changes.
|
|
||||||
func (msg *MsgBlock) Serialize(w io.Writer) error {
|
|
||||||
// At the current time, there is no difference between the appmessage encoding
|
|
||||||
// at protocol version 0 and the stable long-term storage format. As
|
|
||||||
// a result, make use of KaspaEncode.
|
|
||||||
return msg.KaspaEncode(w, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SerializeSize returns the number of bytes it would take to serialize the
|
|
||||||
// block.
|
|
||||||
func (msg *MsgBlock) SerializeSize() int {
|
|
||||||
// Block header bytes + Serialized varint size for the number of
|
|
||||||
// transactions.
|
|
||||||
n := msg.Header.SerializeSize() + VarIntSerializeSize(uint64(len(msg.Transactions)))
|
|
||||||
|
|
||||||
for _, tx := range msg.Transactions {
|
|
||||||
n += tx.SerializeSize()
|
|
||||||
}
|
|
||||||
|
|
||||||
return n
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgBlock) Command() MessageCommand {
|
|
||||||
return CmdBlock
|
|
||||||
}
|
|
||||||
|
|
||||||
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
||||||
// receiver. This is part of the Message interface implementation.
|
|
||||||
func (msg *MsgBlock) MaxPayloadLength(pver uint32) uint32 {
|
|
||||||
return MaxMessagePayload
|
|
||||||
}
|
|
||||||
|
|
||||||
// BlockHash computes the block identifier hash for this block.
|
|
||||||
func (msg *MsgBlock) BlockHash() *daghash.Hash {
|
|
||||||
return msg.Header.BlockHash()
|
|
||||||
}
|
|
||||||
|
|
||||||
// ConvertToPartial clears out all the payloads of the subnetworks that are
|
|
||||||
// incompatible with the given subnetwork ID.
|
|
||||||
// Note: this operation modifies the block in place.
|
|
||||||
func (msg *MsgBlock) ConvertToPartial(subnetworkID *subnetworkid.SubnetworkID) {
|
|
||||||
for _, tx := range msg.Transactions {
|
|
||||||
if !tx.SubnetworkID.IsEqual(subnetworkID) {
|
|
||||||
tx.Payload = []byte{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgBlock returns a new kaspa block message that conforms to the
|
|
||||||
// Message interface. See MsgBlock for details.
|
|
||||||
func NewMsgBlock(blockHeader *BlockHeader) *MsgBlock {
|
|
||||||
return &MsgBlock{
|
|
||||||
Header: *blockHeader,
|
|
||||||
Transactions: make([]*MsgTx, 0, defaultTransactionAlloc),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,610 +0,0 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package appmessage
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"github.com/kaspanet/kaspad/util/mstime"
|
|
||||||
"github.com/kaspanet/kaspad/util/subnetworkid"
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"io"
|
|
||||||
"math"
|
|
||||||
"reflect"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TestBlock tests the MsgBlock API.
|
|
||||||
func TestBlock(t *testing.T) {
|
|
||||||
pver := ProtocolVersion
|
|
||||||
|
|
||||||
// Block 1 header.
|
|
||||||
parentHashes := blockOne.Header.ParentHashes
|
|
||||||
hashMerkleRoot := blockOne.Header.HashMerkleRoot
|
|
||||||
acceptedIDMerkleRoot := blockOne.Header.AcceptedIDMerkleRoot
|
|
||||||
utxoCommitment := blockOne.Header.UTXOCommitment
|
|
||||||
bits := blockOne.Header.Bits
|
|
||||||
nonce := blockOne.Header.Nonce
|
|
||||||
bh := NewBlockHeader(1, parentHashes, hashMerkleRoot, acceptedIDMerkleRoot, utxoCommitment, bits, nonce)
|
|
||||||
|
|
||||||
// Ensure the command is expected value.
|
|
||||||
wantCmd := MessageCommand(5)
|
|
||||||
msg := NewMsgBlock(bh)
|
|
||||||
if cmd := msg.Command(); cmd != wantCmd {
|
|
||||||
t.Errorf("NewMsgBlock: wrong command - got %v want %v",
|
|
||||||
cmd, wantCmd)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure max payload is expected value for latest protocol version.
|
|
||||||
wantPayload := uint32(1024 * 1024 * 32)
|
|
||||||
maxPayload := msg.MaxPayloadLength(pver)
|
|
||||||
if maxPayload != wantPayload {
|
|
||||||
t.Errorf("MaxPayloadLength: wrong max payload length for "+
|
|
||||||
"protocol version %d - got %v, want %v", pver,
|
|
||||||
maxPayload, wantPayload)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure we get the same block header data back out.
|
|
||||||
if !reflect.DeepEqual(&msg.Header, bh) {
|
|
||||||
t.Errorf("NewMsgBlock: wrong block header - got %v, want %v",
|
|
||||||
spew.Sdump(&msg.Header), spew.Sdump(bh))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure transactions are added properly.
|
|
||||||
tx := blockOne.Transactions[0].Copy()
|
|
||||||
msg.AddTransaction(tx)
|
|
||||||
if !reflect.DeepEqual(msg.Transactions, blockOne.Transactions) {
|
|
||||||
t.Errorf("AddTransaction: wrong transactions - got %v, want %v",
|
|
||||||
spew.Sdump(msg.Transactions),
|
|
||||||
spew.Sdump(blockOne.Transactions))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure transactions are properly cleared.
|
|
||||||
msg.ClearTransactions()
|
|
||||||
if len(msg.Transactions) != 0 {
|
|
||||||
t.Errorf("ClearTransactions: wrong transactions - got %v, want %v",
|
|
||||||
len(msg.Transactions), 0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestBlockHash tests the ability to generate the hash of a block accurately.
|
|
||||||
func TestBlockHash(t *testing.T) {
|
|
||||||
// Block 1 hash.
|
|
||||||
hashStr := "55d71bd49a8233bc9f0edbcbd0ad5d3eaebffe1fc6a6443a1c1f310fd02c11a5"
|
|
||||||
wantHash, err := daghash.NewHashFromStr(hashStr)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("NewHashFromStr: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure the hash produced is expected.
|
|
||||||
blockHash := blockOne.BlockHash()
|
|
||||||
if !blockHash.IsEqual(wantHash) {
|
|
||||||
t.Errorf("BlockHash: wrong hash - got %v, want %v",
|
|
||||||
spew.Sprint(blockHash), spew.Sprint(wantHash))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestConvertToPartial(t *testing.T) {
|
|
||||||
transactions := []struct {
|
|
||||||
subnetworkID *subnetworkid.SubnetworkID
|
|
||||||
payload []byte
|
|
||||||
expectedPayloadLength int
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
subnetworkID: subnetworkid.SubnetworkIDNative,
|
|
||||||
payload: []byte{},
|
|
||||||
expectedPayloadLength: 0,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
subnetworkID: subnetworkid.SubnetworkIDRegistry,
|
|
||||||
payload: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08},
|
|
||||||
expectedPayloadLength: 0,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
subnetworkID: &subnetworkid.SubnetworkID{123},
|
|
||||||
payload: []byte{0x01},
|
|
||||||
expectedPayloadLength: 1,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
subnetworkID: &subnetworkid.SubnetworkID{234},
|
|
||||||
payload: []byte{0x02},
|
|
||||||
expectedPayloadLength: 0,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
block := MsgBlock{}
|
|
||||||
payload := []byte{1}
|
|
||||||
for _, transaction := range transactions {
|
|
||||||
block.Transactions = append(block.Transactions, NewSubnetworkMsgTx(1, nil, nil, transaction.subnetworkID, 0, payload))
|
|
||||||
}
|
|
||||||
|
|
||||||
block.ConvertToPartial(&subnetworkid.SubnetworkID{123})
|
|
||||||
|
|
||||||
for _, transaction := range transactions {
|
|
||||||
var subnetworkTx *MsgTx
|
|
||||||
for _, tx := range block.Transactions {
|
|
||||||
if tx.SubnetworkID.IsEqual(transaction.subnetworkID) {
|
|
||||||
subnetworkTx = tx
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if subnetworkTx == nil {
|
|
||||||
t.Errorf("ConvertToPartial: subnetworkID '%s' not found in block!", transaction.subnetworkID)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
payloadLength := len(subnetworkTx.Payload)
|
|
||||||
if payloadLength != transaction.expectedPayloadLength {
|
|
||||||
t.Errorf("ConvertToPartial: unexpected payload length for subnetwork '%s': expected: %d, got: %d",
|
|
||||||
transaction.subnetworkID, transaction.expectedPayloadLength, payloadLength)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestBlockEncoding tests the MsgBlock appmessage encode and decode for various numbers
|
|
||||||
// of transaction inputs and outputs and protocol versions.
|
|
||||||
func TestBlockEncoding(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
in *MsgBlock // Message to encode
|
|
||||||
out *MsgBlock // Expected decoded message
|
|
||||||
buf []byte // Encoded value
|
|
||||||
txLocs []TxLoc // Expected transaction locations
|
|
||||||
pver uint32 // Protocol version for appmessage encoding
|
|
||||||
}{
|
|
||||||
// Latest protocol version.
|
|
||||||
{
|
|
||||||
&blockOne,
|
|
||||||
&blockOne,
|
|
||||||
blockOneBytes,
|
|
||||||
blockOneTxLocs,
|
|
||||||
ProtocolVersion,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Encode the message to appmessage format.
|
|
||||||
var buf bytes.Buffer
|
|
||||||
err := test.in.KaspaEncode(&buf, test.pver)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("KaspaEncode #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !bytes.Equal(buf.Bytes(), test.buf) {
|
|
||||||
t.Errorf("KaspaEncode #%d\n got: %s want: %s", i,
|
|
||||||
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode the message from appmessage format.
|
|
||||||
var msg MsgBlock
|
|
||||||
rbuf := bytes.NewReader(test.buf)
|
|
||||||
err = msg.KaspaDecode(rbuf, test.pver)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("KaspaDecode #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(&msg, test.out) {
|
|
||||||
t.Errorf("KaspaDecode #%d\n got: %s want: %s", i,
|
|
||||||
spew.Sdump(&msg), spew.Sdump(test.out))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestBlockEncodingErrors performs negative tests against appmessage encode and decode
|
|
||||||
// of MsgBlock to confirm error paths work correctly.
|
|
||||||
func TestBlockEncodingErrors(t *testing.T) {
|
|
||||||
pver := ProtocolVersion
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
in *MsgBlock // Value to encode
|
|
||||||
buf []byte // Encoded value
|
|
||||||
pver uint32 // Protocol version for appmessage encoding
|
|
||||||
max int // Max size of fixed buffer to induce errors
|
|
||||||
writeErr error // Expected write error
|
|
||||||
readErr error // Expected read error
|
|
||||||
}{
|
|
||||||
// Force error in version.
|
|
||||||
{&blockOne, blockOneBytes, pver, 0, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in num block hashes.
|
|
||||||
{&blockOne, blockOneBytes, pver, 4, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in prev block hash #1.
|
|
||||||
{&blockOne, blockOneBytes, pver, 5, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in prev block hash #2.
|
|
||||||
{&blockOne, blockOneBytes, pver, 37, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in hash merkle root.
|
|
||||||
{&blockOne, blockOneBytes, pver, 69, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in accepted ID merkle root.
|
|
||||||
{&blockOne, blockOneBytes, pver, 101, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in utxo commitment.
|
|
||||||
{&blockOne, blockOneBytes, pver, 133, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in timestamp.
|
|
||||||
{&blockOne, blockOneBytes, pver, 165, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in difficulty bits.
|
|
||||||
{&blockOne, blockOneBytes, pver, 173, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in header nonce.
|
|
||||||
{&blockOne, blockOneBytes, pver, 177, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in transaction count.
|
|
||||||
{&blockOne, blockOneBytes, pver, 185, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in transactions.
|
|
||||||
{&blockOne, blockOneBytes, pver, 186, io.ErrShortWrite, io.EOF},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Encode to appmessage format.
|
|
||||||
w := newFixedWriter(test.max)
|
|
||||||
err := test.in.KaspaEncode(w, test.pver)
|
|
||||||
if !errors.Is(err, test.writeErr) {
|
|
||||||
t.Errorf("KaspaEncode #%d wrong error got: %v, want: %v",
|
|
||||||
i, err, test.writeErr)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode from appmessage format.
|
|
||||||
var msg MsgBlock
|
|
||||||
r := newFixedReader(test.max, test.buf)
|
|
||||||
err = msg.KaspaDecode(r, test.pver)
|
|
||||||
if !errors.Is(err, test.readErr) {
|
|
||||||
t.Errorf("KaspaDecode #%d wrong error got: %v, want: %v",
|
|
||||||
i, err, test.readErr)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestBlockSerialize tests MsgBlock serialize and deserialize.
|
|
||||||
func TestBlockSerialize(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
in *MsgBlock // Message to encode
|
|
||||||
out *MsgBlock // Expected decoded message
|
|
||||||
buf []byte // Serialized data
|
|
||||||
txLocs []TxLoc // Expected transaction locations
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
&blockOne,
|
|
||||||
&blockOne,
|
|
||||||
blockOneBytes,
|
|
||||||
blockOneTxLocs,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Serialize the block.
|
|
||||||
var buf bytes.Buffer
|
|
||||||
err := test.in.Serialize(&buf)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Serialize #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !bytes.Equal(buf.Bytes(), test.buf) {
|
|
||||||
t.Errorf("Serialize #%d\n got: %s want: %s", i,
|
|
||||||
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deserialize the block.
|
|
||||||
var block MsgBlock
|
|
||||||
rbuf := bytes.NewReader(test.buf)
|
|
||||||
err = block.Deserialize(rbuf)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Deserialize #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(&block, test.out) {
|
|
||||||
t.Errorf("Deserialize #%d\n got: %s want: %s", i,
|
|
||||||
spew.Sdump(&block), spew.Sdump(test.out))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deserialize the block while gathering transaction location
|
|
||||||
// information.
|
|
||||||
var txLocBlock MsgBlock
|
|
||||||
br := bytes.NewBuffer(test.buf)
|
|
||||||
txLocs, err := txLocBlock.DeserializeTxLoc(br)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("DeserializeTxLoc #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(&txLocBlock, test.out) {
|
|
||||||
t.Errorf("DeserializeTxLoc #%d\n got: %s want: %s", i,
|
|
||||||
spew.Sdump(&txLocBlock), spew.Sdump(test.out))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(txLocs, test.txLocs) {
|
|
||||||
t.Errorf("DeserializeTxLoc #%d\n got: %s want: %s", i,
|
|
||||||
spew.Sdump(txLocs), spew.Sdump(test.txLocs))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestBlockSerializeErrors performs negative tests against appmessage encode and
|
|
||||||
// decode of MsgBlock to confirm error paths work correctly.
|
|
||||||
func TestBlockSerializeErrors(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
in *MsgBlock // Value to encode
|
|
||||||
buf []byte // Serialized data
|
|
||||||
max int // Max size of fixed buffer to induce errors
|
|
||||||
writeErr error // Expected write error
|
|
||||||
readErr error // Expected read error
|
|
||||||
}{
|
|
||||||
// Force error in version.
|
|
||||||
{&blockOne, blockOneBytes, 0, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in numParentBlocks.
|
|
||||||
{&blockOne, blockOneBytes, 4, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in prev block hash #1.
|
|
||||||
{&blockOne, blockOneBytes, 5, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in prev block hash #2.
|
|
||||||
{&blockOne, blockOneBytes, 37, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in hash merkle root.
|
|
||||||
{&blockOne, blockOneBytes, 69, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in accepted ID merkle root.
|
|
||||||
{&blockOne, blockOneBytes, 101, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in utxo commitment.
|
|
||||||
{&blockOne, blockOneBytes, 133, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in timestamp.
|
|
||||||
{&blockOne, blockOneBytes, 165, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in difficulty bits.
|
|
||||||
{&blockOne, blockOneBytes, 173, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in header nonce.
|
|
||||||
{&blockOne, blockOneBytes, 177, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in transaction count.
|
|
||||||
{&blockOne, blockOneBytes, 185, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in transactions.
|
|
||||||
{&blockOne, blockOneBytes, 186, io.ErrShortWrite, io.EOF},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Serialize the block.
|
|
||||||
w := newFixedWriter(test.max)
|
|
||||||
err := test.in.Serialize(w)
|
|
||||||
if !errors.Is(err, test.writeErr) {
|
|
||||||
t.Errorf("Serialize #%d wrong error got: %v, want: %v",
|
|
||||||
i, err, test.writeErr)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deserialize the block.
|
|
||||||
var block MsgBlock
|
|
||||||
r := newFixedReader(test.max, test.buf)
|
|
||||||
err = block.Deserialize(r)
|
|
||||||
if !errors.Is(err, test.readErr) {
|
|
||||||
t.Errorf("Deserialize #%d wrong error got: %v, want: %v",
|
|
||||||
i, err, test.readErr)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
var txLocBlock MsgBlock
|
|
||||||
br := bytes.NewBuffer(test.buf[0:test.max])
|
|
||||||
_, err = txLocBlock.DeserializeTxLoc(br)
|
|
||||||
if !errors.Is(err, test.readErr) {
|
|
||||||
t.Errorf("DeserializeTxLoc #%d wrong error got: %v, want: %v",
|
|
||||||
i, err, test.readErr)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestBlockOverflowErrors performs tests to ensure deserializing blocks which
|
|
||||||
// are intentionally crafted to use large values for the number of transactions
|
|
||||||
// are handled properly. This could otherwise potentially be used as an attack
|
|
||||||
// vector.
|
|
||||||
func TestBlockOverflowErrors(t *testing.T) {
|
|
||||||
pver := ProtocolVersion
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
buf []byte // Encoded value
|
|
||||||
pver uint32 // Protocol version for appmessage encoding
|
|
||||||
err error // Expected error
|
|
||||||
}{
|
|
||||||
// Block that claims to have ~uint64(0) transactions.
|
|
||||||
{
|
|
||||||
[]byte{
|
|
||||||
0x01, 0x00, 0x00, 0x00, // Version 1
|
|
||||||
0x02, // NumParentBlocks
|
|
||||||
0x6f, 0xe2, 0x8c, 0x0a, 0xb6, 0xf1, 0xb3, 0x72, // mainnetGenesisHash
|
|
||||||
0xc1, 0xa6, 0xa2, 0x46, 0xae, 0x63, 0xf7, 0x4f,
|
|
||||||
0x93, 0x1e, 0x83, 0x65, 0xe1, 0x5a, 0x08, 0x9c,
|
|
||||||
0x68, 0xd6, 0x19, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0xf6, 0x7a, 0xd7, 0x69, 0x5d, 0x9b, 0x66, 0x2a, // simnetGenesisHash
|
|
||||||
0x72, 0xff, 0x3d, 0x8e, 0xdb, 0xbb, 0x2d, 0xe0,
|
|
||||||
0xbf, 0xa6, 0x7b, 0x13, 0x97, 0x4b, 0xb9, 0x91,
|
|
||||||
0x0d, 0x11, 0x6d, 0x5c, 0xbd, 0x86, 0x3e, 0x68,
|
|
||||||
0x3b, 0xa3, 0xed, 0xfd, 0x7a, 0x7b, 0x12, 0xb2, // HashMerkleRoot
|
|
||||||
0x7a, 0xc7, 0x2c, 0x3e, 0x67, 0x76, 0x8f, 0x61,
|
|
||||||
0x7f, 0xc8, 0x1b, 0xc3, 0x88, 0x8a, 0x51, 0x32,
|
|
||||||
0x3a, 0x9f, 0xb8, 0xaa, 0x4b, 0x1e, 0x5e, 0x4a,
|
|
||||||
0x09, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C, // AcceptedIDMerkleRoot
|
|
||||||
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
|
|
||||||
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
|
|
||||||
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
|
|
||||||
0x10, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C, // UTXOCommitment
|
|
||||||
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
|
|
||||||
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
|
|
||||||
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
|
|
||||||
0x61, 0xbc, 0x66, 0x49, 0x00, 0x00, 0x00, 0x00, // Timestamp
|
|
||||||
0xff, 0xff, 0x00, 0x1d, // Bits
|
|
||||||
0x01, 0xe3, 0x62, 0x99, 0x00, 0x00, 0x00, 0x00, // Fake Nonce
|
|
||||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
||||||
0xff, // TxnCount
|
|
||||||
}, pver, &MessageError{},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Decode from appmessage format.
|
|
||||||
var msg MsgBlock
|
|
||||||
r := bytes.NewReader(test.buf)
|
|
||||||
err := msg.KaspaDecode(r, test.pver)
|
|
||||||
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
|
|
||||||
t.Errorf("KaspaDecode #%d wrong error got: %v, want: %v",
|
|
||||||
i, err, reflect.TypeOf(test.err))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deserialize from appmessage format.
|
|
||||||
r = bytes.NewReader(test.buf)
|
|
||||||
err = msg.Deserialize(r)
|
|
||||||
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
|
|
||||||
t.Errorf("Deserialize #%d wrong error got: %v, want: %v",
|
|
||||||
i, err, reflect.TypeOf(test.err))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deserialize with transaction location info from appmessage format.
|
|
||||||
br := bytes.NewBuffer(test.buf)
|
|
||||||
_, err = msg.DeserializeTxLoc(br)
|
|
||||||
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
|
|
||||||
t.Errorf("DeserializeTxLoc #%d wrong error got: %v, "+
|
|
||||||
"want: %v", i, err, reflect.TypeOf(test.err))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestBlockSerializeSize performs tests to ensure the serialize size for
|
|
||||||
// various blocks is accurate.
|
|
||||||
func TestBlockSerializeSize(t *testing.T) {
|
|
||||||
// Block with no transactions.
|
|
||||||
noTxBlock := NewMsgBlock(&blockOne.Header)
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
in *MsgBlock // Block to encode
|
|
||||||
size int // Expected serialized size
|
|
||||||
}{
|
|
||||||
// Block with no transactions.
|
|
||||||
{noTxBlock, 186},
|
|
||||||
|
|
||||||
// First block in the mainnet block DAG.
|
|
||||||
{&blockOne, len(blockOneBytes)},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
serializedSize := test.in.SerializeSize()
|
|
||||||
if serializedSize != test.size {
|
|
||||||
t.Errorf("MsgBlock.SerializeSize: #%d got: %d, want: "+
|
|
||||||
"%d", i, serializedSize, test.size)
|
|
||||||
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// blockOne is the first block in the mainnet block DAG.
|
|
||||||
var blockOne = MsgBlock{
|
|
||||||
Header: BlockHeader{
|
|
||||||
Version: 1,
|
|
||||||
ParentHashes: []*daghash.Hash{mainnetGenesisHash, simnetGenesisHash},
|
|
||||||
HashMerkleRoot: mainnetGenesisMerkleRoot,
|
|
||||||
AcceptedIDMerkleRoot: exampleAcceptedIDMerkleRoot,
|
|
||||||
UTXOCommitment: exampleUTXOCommitment,
|
|
||||||
Timestamp: mstime.UnixMilliseconds(0x17315ed0f99),
|
|
||||||
Bits: 0x1d00ffff, // 486604799
|
|
||||||
Nonce: 0x9962e301, // 2573394689
|
|
||||||
},
|
|
||||||
Transactions: []*MsgTx{
|
|
||||||
NewNativeMsgTx(1,
|
|
||||||
[]*TxIn{
|
|
||||||
{
|
|
||||||
PreviousOutpoint: Outpoint{
|
|
||||||
TxID: daghash.TxID{},
|
|
||||||
Index: 0xffffffff,
|
|
||||||
},
|
|
||||||
SignatureScript: []byte{
|
|
||||||
0x04, 0xff, 0xff, 0x00, 0x1d, 0x01, 0x04,
|
|
||||||
},
|
|
||||||
Sequence: math.MaxUint64,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
[]*TxOut{
|
|
||||||
{
|
|
||||||
Value: 0x12a05f200,
|
|
||||||
ScriptPubKey: []byte{
|
|
||||||
0x41, // OP_DATA_65
|
|
||||||
0x04, 0x96, 0xb5, 0x38, 0xe8, 0x53, 0x51, 0x9c,
|
|
||||||
0x72, 0x6a, 0x2c, 0x91, 0xe6, 0x1e, 0xc1, 0x16,
|
|
||||||
0x00, 0xae, 0x13, 0x90, 0x81, 0x3a, 0x62, 0x7c,
|
|
||||||
0x66, 0xfb, 0x8b, 0xe7, 0x94, 0x7b, 0xe6, 0x3c,
|
|
||||||
0x52, 0xda, 0x75, 0x89, 0x37, 0x95, 0x15, 0xd4,
|
|
||||||
0xe0, 0xa6, 0x04, 0xf8, 0x14, 0x17, 0x81, 0xe6,
|
|
||||||
0x22, 0x94, 0x72, 0x11, 0x66, 0xbf, 0x62, 0x1e,
|
|
||||||
0x73, 0xa8, 0x2c, 0xbf, 0x23, 0x42, 0xc8, 0x58,
|
|
||||||
0xee, // 65-byte signature
|
|
||||||
0xac, // OP_CHECKSIG
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// Block one serialized bytes.
|
|
||||||
var blockOneBytes = []byte{
|
|
||||||
0x01, 0x00, 0x00, 0x00, // Version 1
|
|
||||||
0x02, // NumParentBlocks
|
|
||||||
0xdc, 0x5f, 0x5b, 0x5b, 0x1d, 0xc2, 0xa7, 0x25, // mainnetGenesisHash
|
|
||||||
0x49, 0xd5, 0x1d, 0x4d, 0xee, 0xd7, 0xa4, 0x8b,
|
|
||||||
0xaf, 0xd3, 0x14, 0x4b, 0x56, 0x78, 0x98, 0xb1,
|
|
||||||
0x8c, 0xfd, 0x9f, 0x69, 0xdd, 0xcf, 0xbb, 0x63,
|
|
||||||
0xf6, 0x7a, 0xd7, 0x69, 0x5d, 0x9b, 0x66, 0x2a, // simnetGenesisHash
|
|
||||||
0x72, 0xff, 0x3d, 0x8e, 0xdb, 0xbb, 0x2d, 0xe0,
|
|
||||||
0xbf, 0xa6, 0x7b, 0x13, 0x97, 0x4b, 0xb9, 0x91,
|
|
||||||
0x0d, 0x11, 0x6d, 0x5c, 0xbd, 0x86, 0x3e, 0x68,
|
|
||||||
0x4a, 0x5e, 0x1e, 0x4b, 0xaa, 0xb8, 0x9f, 0x3a, // HashMerkleRoot
|
|
||||||
0x32, 0x51, 0x8a, 0x88, 0xc3, 0x1b, 0xc8, 0x7f,
|
|
||||||
0x61, 0x8f, 0x76, 0x67, 0x3e, 0x2c, 0xc7, 0x7a,
|
|
||||||
0xb2, 0x12, 0x7b, 0x7a, 0xfd, 0xed, 0xa3, 0x3b,
|
|
||||||
0x09, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C, // AcceptedIDMerkleRoot
|
|
||||||
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
|
|
||||||
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
|
|
||||||
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
|
|
||||||
0x10, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C, // UTXOCommitment
|
|
||||||
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
|
|
||||||
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
|
|
||||||
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
|
|
||||||
0x99, 0x0f, 0xed, 0x15, 0x73, 0x01, 0x00, 0x00, // Timestamp
|
|
||||||
0xff, 0xff, 0x00, 0x1d, // Bits
|
|
||||||
0x01, 0xe3, 0x62, 0x99, 0x00, 0x00, 0x00, 0x00, // Fake Nonce
|
|
||||||
0x01, // TxnCount
|
|
||||||
0x01, 0x00, 0x00, 0x00, // Version
|
|
||||||
0x01, // Varint for number of transaction inputs
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash
|
|
||||||
0xff, 0xff, 0xff, 0xff, // Prevous output index
|
|
||||||
0x07, // Varint for length of signature script
|
|
||||||
0x04, 0xff, 0xff, 0x00, 0x1d, 0x01, 0x04, // Signature script (coinbase)
|
|
||||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // Sequence
|
|
||||||
0x01, // Varint for number of transaction outputs
|
|
||||||
0x00, 0xf2, 0x05, 0x2a, 0x01, 0x00, 0x00, 0x00, // Transaction amount
|
|
||||||
0x43, // Varint for length of scriptPubKey
|
|
||||||
0x41, // OP_DATA_65
|
|
||||||
0x04, 0x96, 0xb5, 0x38, 0xe8, 0x53, 0x51, 0x9c,
|
|
||||||
0x72, 0x6a, 0x2c, 0x91, 0xe6, 0x1e, 0xc1, 0x16,
|
|
||||||
0x00, 0xae, 0x13, 0x90, 0x81, 0x3a, 0x62, 0x7c,
|
|
||||||
0x66, 0xfb, 0x8b, 0xe7, 0x94, 0x7b, 0xe6, 0x3c,
|
|
||||||
0x52, 0xda, 0x75, 0x89, 0x37, 0x95, 0x15, 0xd4,
|
|
||||||
0xe0, 0xa6, 0x04, 0xf8, 0x14, 0x17, 0x81, 0xe6,
|
|
||||||
0x22, 0x94, 0x72, 0x11, 0x66, 0xbf, 0x62, 0x1e,
|
|
||||||
0x73, 0xa8, 0x2c, 0xbf, 0x23, 0x42, 0xc8, 0x58,
|
|
||||||
0xee, // 65-byte uncompressed public key
|
|
||||||
0xac, // OP_CHECKSIG
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Lock time
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, // SubnetworkID
|
|
||||||
}
|
|
||||||
|
|
||||||
// Transaction location information for block one transactions.
|
|
||||||
var blockOneTxLocs = []TxLoc{
|
|
||||||
{TxStart: 186, TxLen: 162},
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
package appmessage
|
|
||||||
|
|
||||||
// MsgDoneIBDBlocks implements the Message interface and represents a kaspa
|
|
||||||
// DoneIBDBlocks message. It is used to notify the IBD syncing peer that the
|
|
||||||
// syncer sent all the requested blocks.
|
|
||||||
//
|
|
||||||
// This message has no payload.
|
|
||||||
type MsgDoneIBDBlocks struct {
|
|
||||||
baseMessage
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgDoneIBDBlocks) Command() MessageCommand {
|
|
||||||
return CmdDoneIBDBlocks
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgDoneIBDBlocks returns a new kaspa DoneIBDBlocks message that conforms to the
|
|
||||||
// Message interface.
|
|
||||||
func NewMsgDoneIBDBlocks() *MsgDoneIBDBlocks {
|
|
||||||
return &MsgDoneIBDBlocks{}
|
|
||||||
}
|
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package appmessage
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MsgRequestIBDBlocks implements the Message interface and represents a kaspa
|
|
||||||
// RequestIBDBlocks message. It is used to request a list of blocks starting after the
|
|
||||||
// low hash and until the high hash.
|
|
||||||
type MsgRequestIBDBlocks struct {
|
|
||||||
baseMessage
|
|
||||||
LowHash *daghash.Hash
|
|
||||||
HighHash *daghash.Hash
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgRequestIBDBlocks) Command() MessageCommand {
|
|
||||||
return CmdRequestIBDBlocks
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgRequstIBDBlocks returns a new kaspa RequestIBDBlocks message that conforms to the
|
|
||||||
// Message interface using the passed parameters and defaults for the remaining
|
|
||||||
// fields.
|
|
||||||
func NewMsgRequstIBDBlocks(lowHash, highHash *daghash.Hash) *MsgRequestIBDBlocks {
|
|
||||||
return &MsgRequestIBDBlocks{
|
|
||||||
LowHash: lowHash,
|
|
||||||
HighHash: highHash,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
package appmessage
|
|
||||||
|
|
||||||
// MsgRequestNextIBDBlocks implements the Message interface and represents a kaspa
|
|
||||||
// RequestNextIBDBlocks message. It is used to notify the IBD syncer peer to send
|
|
||||||
// more blocks.
|
|
||||||
//
|
|
||||||
// This message has no payload.
|
|
||||||
type MsgRequestNextIBDBlocks struct {
|
|
||||||
baseMessage
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgRequestNextIBDBlocks) Command() MessageCommand {
|
|
||||||
return CmdRequestNextIBDBlocks
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgRequestNextIBDBlocks returns a new kaspa RequestNextIBDBlocks message that conforms to the
|
|
||||||
// Message interface.
|
|
||||||
func NewMsgRequestNextIBDBlocks() *MsgRequestNextIBDBlocks {
|
|
||||||
return &MsgRequestNextIBDBlocks{}
|
|
||||||
}
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
package appmessage
|
|
||||||
|
|
||||||
// MsgRequestSelectedTip implements the Message interface and represents a kaspa
|
|
||||||
// RequestSelectedTip message. It is used to request the selected tip of another peer.
|
|
||||||
//
|
|
||||||
// This message has no payload.
|
|
||||||
type MsgRequestSelectedTip struct {
|
|
||||||
baseMessage
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgRequestSelectedTip) Command() MessageCommand {
|
|
||||||
return CmdRequestSelectedTip
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgRequestSelectedTip returns a new kaspa RequestSelectedTip message that conforms to the
|
|
||||||
// Message interface.
|
|
||||||
func NewMsgRequestSelectedTip() *MsgRequestSelectedTip {
|
|
||||||
return &MsgRequestSelectedTip{}
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package appmessage
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TestRequestSelectedTip tests the MsgRequestSelectedTip API.
|
|
||||||
func TestRequestSelectedTip(t *testing.T) {
|
|
||||||
// Ensure the command is expected value.
|
|
||||||
wantCmd := MessageCommand(12)
|
|
||||||
msg := NewMsgRequestSelectedTip()
|
|
||||||
if cmd := msg.Command(); cmd != wantCmd {
|
|
||||||
t.Errorf("NewMsgRequestSelectedTip: wrong command - got %v want %v",
|
|
||||||
cmd, wantCmd)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
package appmessage
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MsgSelectedTip implements the Message interface and represents a kaspa
|
|
||||||
// selectedtip message. It is used to answer getseltip messages and tell
|
|
||||||
// the asking peer what is the selected tip of this peer.
|
|
||||||
type MsgSelectedTip struct {
|
|
||||||
baseMessage
|
|
||||||
// The selected tip hash of the generator of the message.
|
|
||||||
SelectedTipHash *daghash.Hash
|
|
||||||
}
|
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
|
||||||
// of the Message interface implementation.
|
|
||||||
func (msg *MsgSelectedTip) Command() MessageCommand {
|
|
||||||
return CmdSelectedTip
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMsgSelectedTip returns a new kaspa selectedtip message that conforms to the
|
|
||||||
// Message interface.
|
|
||||||
func NewMsgSelectedTip(selectedTipHash *daghash.Hash) *MsgSelectedTip {
|
|
||||||
return &MsgSelectedTip{
|
|
||||||
SelectedTipHash: selectedTipHash,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
package appmessage
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TestSelectedTip tests the MsgSelectedTip API.
|
|
||||||
func TestSelectedTip(t *testing.T) {
|
|
||||||
|
|
||||||
// Ensure the command is expected value.
|
|
||||||
wantCmd := MessageCommand(11)
|
|
||||||
msg := NewMsgSelectedTip(&daghash.ZeroHash)
|
|
||||||
if cmd := msg.Command(); cmd != wantCmd {
|
|
||||||
t.Errorf("NewMsgSelectedTip: wrong command - got %v want %v",
|
|
||||||
cmd, wantCmd)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,942 +0,0 @@
|
|||||||
// Copyright (c) 2013-2016 The btcsuite developers
|
|
||||||
// Use of this source code is governed by an ISC
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
package appmessage
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"io"
|
|
||||||
"math"
|
|
||||||
"reflect"
|
|
||||||
"testing"
|
|
||||||
"unsafe"
|
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
|
||||||
"github.com/kaspanet/kaspad/util/subnetworkid"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TestTx tests the MsgTx API.
|
|
||||||
func TestTx(t *testing.T) {
|
|
||||||
pver := ProtocolVersion
|
|
||||||
|
|
||||||
txIDStr := "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
|
|
||||||
txID, err := daghash.NewTxIDFromStr(txIDStr)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("NewTxIDFromStr: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure the command is expected value.
|
|
||||||
wantCmd := MessageCommand(6)
|
|
||||||
msg := NewNativeMsgTx(1, nil, nil)
|
|
||||||
if cmd := msg.Command(); cmd != wantCmd {
|
|
||||||
t.Errorf("NewMsgAddresses: wrong command - got %v want %v",
|
|
||||||
cmd, wantCmd)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure max payload is expected value for latest protocol version.
|
|
||||||
wantPayload := uint32(1024 * 1024 * 32)
|
|
||||||
maxPayload := msg.MaxPayloadLength(pver)
|
|
||||||
if maxPayload != wantPayload {
|
|
||||||
t.Errorf("MaxPayloadLength: wrong max payload length for "+
|
|
||||||
"protocol version %d - got %v, want %v", pver,
|
|
||||||
maxPayload, wantPayload)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure we get the same transaction outpoint data back out.
|
|
||||||
// NOTE: This is a block hash and made up index, but we're only
|
|
||||||
// testing package functionality.
|
|
||||||
prevOutIndex := uint32(1)
|
|
||||||
prevOut := NewOutpoint(txID, prevOutIndex)
|
|
||||||
if !prevOut.TxID.IsEqual(txID) {
|
|
||||||
t.Errorf("NewOutpoint: wrong ID - got %v, want %v",
|
|
||||||
spew.Sprint(&prevOut.TxID), spew.Sprint(txID))
|
|
||||||
}
|
|
||||||
if prevOut.Index != prevOutIndex {
|
|
||||||
t.Errorf("NewOutpoint: wrong index - got %v, want %v",
|
|
||||||
prevOut.Index, prevOutIndex)
|
|
||||||
}
|
|
||||||
prevOutStr := fmt.Sprintf("%s:%d", txID.String(), prevOutIndex)
|
|
||||||
if s := prevOut.String(); s != prevOutStr {
|
|
||||||
t.Errorf("Outpoint.String: unexpected result - got %v, "+
|
|
||||||
"want %v", s, prevOutStr)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure we get the same transaction input back out.
|
|
||||||
sigScript := []byte{0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62}
|
|
||||||
txIn := NewTxIn(prevOut, sigScript)
|
|
||||||
if !reflect.DeepEqual(&txIn.PreviousOutpoint, prevOut) {
|
|
||||||
t.Errorf("NewTxIn: wrong prev outpoint - got %v, want %v",
|
|
||||||
spew.Sprint(&txIn.PreviousOutpoint),
|
|
||||||
spew.Sprint(prevOut))
|
|
||||||
}
|
|
||||||
if !bytes.Equal(txIn.SignatureScript, sigScript) {
|
|
||||||
t.Errorf("NewTxIn: wrong signature script - got %v, want %v",
|
|
||||||
spew.Sdump(txIn.SignatureScript),
|
|
||||||
spew.Sdump(sigScript))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure we get the same transaction output back out.
|
|
||||||
txValue := uint64(5000000000)
|
|
||||||
scriptPubKey := []byte{
|
|
||||||
0x41, // OP_DATA_65
|
|
||||||
0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
|
|
||||||
0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
|
|
||||||
0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
|
|
||||||
0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
|
|
||||||
0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
|
|
||||||
0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
|
|
||||||
0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
|
|
||||||
0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
|
|
||||||
0xa6, // 65-byte signature
|
|
||||||
0xac, // OP_CHECKSIG
|
|
||||||
}
|
|
||||||
txOut := NewTxOut(txValue, scriptPubKey)
|
|
||||||
if txOut.Value != txValue {
|
|
||||||
t.Errorf("NewTxOut: wrong scriptPubKey - got %v, want %v",
|
|
||||||
txOut.Value, txValue)
|
|
||||||
|
|
||||||
}
|
|
||||||
if !bytes.Equal(txOut.ScriptPubKey, scriptPubKey) {
|
|
||||||
t.Errorf("NewTxOut: wrong scriptPubKey - got %v, want %v",
|
|
||||||
spew.Sdump(txOut.ScriptPubKey),
|
|
||||||
spew.Sdump(scriptPubKey))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure transaction inputs are added properly.
|
|
||||||
msg.AddTxIn(txIn)
|
|
||||||
if !reflect.DeepEqual(msg.TxIn[0], txIn) {
|
|
||||||
t.Errorf("AddTxIn: wrong transaction input added - got %v, want %v",
|
|
||||||
spew.Sprint(msg.TxIn[0]), spew.Sprint(txIn))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure transaction outputs are added properly.
|
|
||||||
msg.AddTxOut(txOut)
|
|
||||||
if !reflect.DeepEqual(msg.TxOut[0], txOut) {
|
|
||||||
t.Errorf("AddTxIn: wrong transaction output added - got %v, want %v",
|
|
||||||
spew.Sprint(msg.TxOut[0]), spew.Sprint(txOut))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure the copy produced an identical transaction message.
|
|
||||||
newMsg := msg.Copy()
|
|
||||||
if !reflect.DeepEqual(newMsg, msg) {
|
|
||||||
t.Errorf("Copy: mismatched tx messages - got %v, want %v",
|
|
||||||
spew.Sdump(newMsg), spew.Sdump(msg))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestTxHash tests the ability to generate the hash of a transaction accurately.
|
|
||||||
func TestTxHashAndID(t *testing.T) {
|
|
||||||
txID1Str := "edca872f27279674c7a52192b32fd68b8b8be714bfea52d98b2c3c86c30e85c6"
|
|
||||||
wantTxID1, err := daghash.NewTxIDFromStr(txID1Str)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("NewTxIDFromStr: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// A coinbase transaction
|
|
||||||
txIn := &TxIn{
|
|
||||||
PreviousOutpoint: Outpoint{
|
|
||||||
TxID: daghash.TxID{},
|
|
||||||
Index: math.MaxUint32,
|
|
||||||
},
|
|
||||||
SignatureScript: []byte{0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62},
|
|
||||||
Sequence: math.MaxUint64,
|
|
||||||
}
|
|
||||||
txOut := &TxOut{
|
|
||||||
Value: 5000000000,
|
|
||||||
ScriptPubKey: []byte{
|
|
||||||
0x41, // OP_DATA_65
|
|
||||||
0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
|
|
||||||
0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
|
|
||||||
0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
|
|
||||||
0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
|
|
||||||
0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
|
|
||||||
0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
|
|
||||||
0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
|
|
||||||
0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
|
|
||||||
0xa6, // 65-byte signature
|
|
||||||
0xac, // OP_CHECKSIG
|
|
||||||
},
|
|
||||||
}
|
|
||||||
tx1 := NewSubnetworkMsgTx(1, []*TxIn{txIn}, []*TxOut{txOut}, subnetworkid.SubnetworkIDCoinbase, 0, nil)
|
|
||||||
|
|
||||||
// Ensure the hash produced is expected.
|
|
||||||
tx1Hash := tx1.TxHash()
|
|
||||||
if !tx1Hash.IsEqual((*daghash.Hash)(wantTxID1)) {
|
|
||||||
t.Errorf("TxHash: wrong hash - got %v, want %v",
|
|
||||||
spew.Sprint(tx1Hash), spew.Sprint(wantTxID1))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure the TxID for coinbase transaction is the same as TxHash.
|
|
||||||
tx1ID := tx1.TxID()
|
|
||||||
if !tx1ID.IsEqual(wantTxID1) {
|
|
||||||
t.Errorf("TxID: wrong ID - got %v, want %v",
|
|
||||||
spew.Sprint(tx1ID), spew.Sprint(wantTxID1))
|
|
||||||
}
|
|
||||||
|
|
||||||
hash2Str := "b11924b7eeffea821522222576c53dc5b8ddd97602f81e5e124d2626646d74ca"
|
|
||||||
wantHash2, err := daghash.NewHashFromStr(hash2Str)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("NewTxIDFromStr: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
id2Str := "750499ae9e6d44961ef8bad8af27a44dd4bcbea166b71baf181e8d3997e1ff72"
|
|
||||||
wantID2, err := daghash.NewTxIDFromStr(id2Str)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("NewTxIDFromStr: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
payload := []byte{1, 2, 3}
|
|
||||||
txIns := []*TxIn{{
|
|
||||||
PreviousOutpoint: Outpoint{
|
|
||||||
Index: 0,
|
|
||||||
TxID: daghash.TxID{1, 2, 3},
|
|
||||||
},
|
|
||||||
SignatureScript: []byte{
|
|
||||||
0x49, 0x30, 0x46, 0x02, 0x21, 0x00, 0xDA, 0x0D, 0xC6, 0xAE, 0xCE, 0xFE, 0x1E, 0x06, 0xEF, 0xDF,
|
|
||||||
0x05, 0x77, 0x37, 0x57, 0xDE, 0xB1, 0x68, 0x82, 0x09, 0x30, 0xE3, 0xB0, 0xD0, 0x3F, 0x46, 0xF5,
|
|
||||||
0xFC, 0xF1, 0x50, 0xBF, 0x99, 0x0C, 0x02, 0x21, 0x00, 0xD2, 0x5B, 0x5C, 0x87, 0x04, 0x00, 0x76,
|
|
||||||
0xE4, 0xF2, 0x53, 0xF8, 0x26, 0x2E, 0x76, 0x3E, 0x2D, 0xD5, 0x1E, 0x7F, 0xF0, 0xBE, 0x15, 0x77,
|
|
||||||
0x27, 0xC4, 0xBC, 0x42, 0x80, 0x7F, 0x17, 0xBD, 0x39, 0x01, 0x41, 0x04, 0xE6, 0xC2, 0x6E, 0xF6,
|
|
||||||
0x7D, 0xC6, 0x10, 0xD2, 0xCD, 0x19, 0x24, 0x84, 0x78, 0x9A, 0x6C, 0xF9, 0xAE, 0xA9, 0x93, 0x0B,
|
|
||||||
0x94, 0x4B, 0x7E, 0x2D, 0xB5, 0x34, 0x2B, 0x9D, 0x9E, 0x5B, 0x9F, 0xF7, 0x9A, 0xFF, 0x9A, 0x2E,
|
|
||||||
0xE1, 0x97, 0x8D, 0xD7, 0xFD, 0x01, 0xDF, 0xC5, 0x22, 0xEE, 0x02, 0x28, 0x3D, 0x3B, 0x06, 0xA9,
|
|
||||||
0xD0, 0x3A, 0xCF, 0x80, 0x96, 0x96, 0x8D, 0x7D, 0xBB, 0x0F, 0x91, 0x78,
|
|
||||||
},
|
|
||||||
Sequence: math.MaxUint64,
|
|
||||||
}}
|
|
||||||
txOuts := []*TxOut{
|
|
||||||
{
|
|
||||||
Value: 244623243,
|
|
||||||
ScriptPubKey: []byte{
|
|
||||||
0x76, 0xA9, 0x14, 0xBA, 0xDE, 0xEC, 0xFD, 0xEF, 0x05, 0x07, 0x24, 0x7F, 0xC8, 0xF7, 0x42, 0x41,
|
|
||||||
0xD7, 0x3B, 0xC0, 0x39, 0x97, 0x2D, 0x7B, 0x88, 0xAC,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Value: 44602432,
|
|
||||||
ScriptPubKey: []byte{
|
|
||||||
0x76, 0xA9, 0x14, 0xC1, 0x09, 0x32, 0x48, 0x3F, 0xEC, 0x93, 0xED, 0x51, 0xF5, 0xFE, 0x95, 0xE7,
|
|
||||||
0x25, 0x59, 0xF2, 0xCC, 0x70, 0x43, 0xF9, 0x88, 0xAC,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
tx2 := NewSubnetworkMsgTx(1, txIns, txOuts, &subnetworkid.SubnetworkID{1, 2, 3}, 0, payload)
|
|
||||||
|
|
||||||
// Ensure the hash produced is expected.
|
|
||||||
tx2Hash := tx2.TxHash()
|
|
||||||
if !tx2Hash.IsEqual(wantHash2) {
|
|
||||||
t.Errorf("TxHash: wrong hash - got %v, want %v",
|
|
||||||
spew.Sprint(tx2Hash), spew.Sprint(wantHash2))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure the TxID for coinbase transaction is the same as TxHash.
|
|
||||||
tx2ID := tx2.TxID()
|
|
||||||
if !tx2ID.IsEqual(wantID2) {
|
|
||||||
t.Errorf("TxID: wrong ID - got %v, want %v",
|
|
||||||
spew.Sprint(tx2ID), spew.Sprint(wantID2))
|
|
||||||
}
|
|
||||||
|
|
||||||
if tx2ID.IsEqual((*daghash.TxID)(tx2Hash)) {
|
|
||||||
t.Errorf("tx2ID and tx2Hash shouldn't be the same for non-coinbase transaction with signature and/or payload")
|
|
||||||
}
|
|
||||||
|
|
||||||
tx2.TxIn[0].SignatureScript = []byte{}
|
|
||||||
newTx2Hash := tx2.TxHash()
|
|
||||||
if !tx2ID.IsEqual((*daghash.TxID)(newTx2Hash)) {
|
|
||||||
t.Errorf("tx2ID and newTx2Hash should be the same for transaction with an empty signature")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestTxEncoding tests the MsgTx appmessage encode and decode for various numbers
|
|
||||||
// of transaction inputs and outputs and protocol versions.
|
|
||||||
func TestTxEncoding(t *testing.T) {
|
|
||||||
// Empty tx message.
|
|
||||||
noTx := NewNativeMsgTx(1, nil, nil)
|
|
||||||
noTxEncoded := []byte{
|
|
||||||
0x01, 0x00, 0x00, 0x00, // Version
|
|
||||||
0x00, // Varint for number of input transactions
|
|
||||||
0x00, // Varint for number of output transactions
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Lock time
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, // Sub Network ID
|
|
||||||
}
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
in *MsgTx // Message to encode
|
|
||||||
out *MsgTx // Expected decoded message
|
|
||||||
buf []byte // Encoded value
|
|
||||||
pver uint32 // Protocol version for appmessage encoding
|
|
||||||
}{
|
|
||||||
// Latest protocol version with no transactions.
|
|
||||||
{
|
|
||||||
noTx,
|
|
||||||
noTx,
|
|
||||||
noTxEncoded,
|
|
||||||
ProtocolVersion,
|
|
||||||
},
|
|
||||||
|
|
||||||
// Latest protocol version with multiple transactions.
|
|
||||||
{
|
|
||||||
multiTx,
|
|
||||||
multiTx,
|
|
||||||
multiTxEncoded,
|
|
||||||
ProtocolVersion,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Encode the message to appmessage format.
|
|
||||||
var buf bytes.Buffer
|
|
||||||
err := test.in.KaspaEncode(&buf, test.pver)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("KaspaEncode #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !bytes.Equal(buf.Bytes(), test.buf) {
|
|
||||||
t.Errorf("KaspaEncode #%d\n got: %s want: %s", i,
|
|
||||||
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode the message from appmessage format.
|
|
||||||
var msg MsgTx
|
|
||||||
rbuf := bytes.NewReader(test.buf)
|
|
||||||
err = msg.KaspaDecode(rbuf, test.pver)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("KaspaDecode #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(&msg, test.out) {
|
|
||||||
t.Errorf("KaspaDecode #%d\n got: %s want: %s", i,
|
|
||||||
spew.Sdump(&msg), spew.Sdump(test.out))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestTxEncodingErrors performs negative tests against appmessage encode and decode
|
|
||||||
// of MsgTx to confirm error paths work correctly.
|
|
||||||
func TestTxEncodingErrors(t *testing.T) {
|
|
||||||
pver := ProtocolVersion
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
in *MsgTx // Value to encode
|
|
||||||
buf []byte // Encoded value
|
|
||||||
pver uint32 // Protocol version for appmessage encoding
|
|
||||||
max int // Max size of fixed buffer to induce errors
|
|
||||||
writeErr error // Expected write error
|
|
||||||
readErr error // Expected read error
|
|
||||||
}{
|
|
||||||
// Force error in version.
|
|
||||||
{multiTx, multiTxEncoded, pver, 0, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in number of transaction inputs.
|
|
||||||
{multiTx, multiTxEncoded, pver, 4, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in transaction input previous block hash.
|
|
||||||
{multiTx, multiTxEncoded, pver, 5, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in transaction input previous block output index.
|
|
||||||
{multiTx, multiTxEncoded, pver, 37, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in transaction input signature script length.
|
|
||||||
{multiTx, multiTxEncoded, pver, 41, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in transaction input signature script.
|
|
||||||
{multiTx, multiTxEncoded, pver, 42, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in transaction input sequence.
|
|
||||||
{multiTx, multiTxEncoded, pver, 49, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in number of transaction outputs.
|
|
||||||
{multiTx, multiTxEncoded, pver, 57, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in transaction output value.
|
|
||||||
{multiTx, multiTxEncoded, pver, 58, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in transaction output scriptPubKey length.
|
|
||||||
{multiTx, multiTxEncoded, pver, 66, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in transaction output scriptPubKey.
|
|
||||||
{multiTx, multiTxEncoded, pver, 67, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in transaction output lock time.
|
|
||||||
{multiTx, multiTxEncoded, pver, 210, io.ErrShortWrite, io.EOF},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Encode to appmessage format.
|
|
||||||
w := newFixedWriter(test.max)
|
|
||||||
err := test.in.KaspaEncode(w, test.pver)
|
|
||||||
if !errors.Is(err, test.writeErr) {
|
|
||||||
t.Errorf("KaspaEncode #%d wrong error got: %v, want: %v",
|
|
||||||
i, err, test.writeErr)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode from appmessage format.
|
|
||||||
var msg MsgTx
|
|
||||||
r := newFixedReader(test.max, test.buf)
|
|
||||||
err = msg.KaspaDecode(r, test.pver)
|
|
||||||
if !errors.Is(err, test.readErr) {
|
|
||||||
t.Errorf("KaspaDecode #%d wrong error got: %v, want: %v",
|
|
||||||
i, err, test.readErr)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestTxSerialize tests MsgTx serialize and deserialize.
|
|
||||||
func TestTxSerialize(t *testing.T) {
|
|
||||||
noTx := NewNativeMsgTx(1, nil, nil)
|
|
||||||
noTxEncoded := []byte{
|
|
||||||
0x01, 0x00, 0x00, 0x00, // Version
|
|
||||||
0x00, // Varint for number of input transactions
|
|
||||||
0x00, // Varint for number of output transactions
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Lock time
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, // Sub Network ID
|
|
||||||
}
|
|
||||||
|
|
||||||
registryTx := NewRegistryMsgTx(1, nil, nil, 16)
|
|
||||||
registryTxEncoded := []byte{
|
|
||||||
0x01, 0x00, 0x00, 0x00, // Version
|
|
||||||
0x00, // Varint for number of input transactions
|
|
||||||
0x00, // Varint for number of output transactions
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Lock time
|
|
||||||
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, // Sub Network ID
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Gas
|
|
||||||
0x77, 0x56, 0x36, 0xb4, 0x89, 0x32, 0xe9, 0xa8,
|
|
||||||
0xbb, 0x67, 0xe6, 0x54, 0x84, 0x36, 0x93, 0x8d,
|
|
||||||
0x9f, 0xc5, 0x62, 0x49, 0x79, 0x5c, 0x0d, 0x0a,
|
|
||||||
0x86, 0xaf, 0x7c, 0x5d, 0x54, 0x45, 0x4c, 0x4b, // Payload hash
|
|
||||||
0x08, // Payload length varint
|
|
||||||
0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Payload / Gas limit
|
|
||||||
}
|
|
||||||
|
|
||||||
subnetworkTx := NewSubnetworkMsgTx(1, nil, nil, &subnetworkid.SubnetworkID{0xff}, 5, []byte{0, 1, 2})
|
|
||||||
|
|
||||||
subnetworkTxEncoded := []byte{
|
|
||||||
0x01, 0x00, 0x00, 0x00, // Version
|
|
||||||
0x00, // Varint for number of input transactions
|
|
||||||
0x00, // Varint for number of output transactions
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Lock time
|
|
||||||
0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, // Sub Network ID
|
|
||||||
0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Gas
|
|
||||||
0x35, 0xf9, 0xf2, 0x93, 0x0e, 0xa3, 0x44, 0x61,
|
|
||||||
0x88, 0x22, 0x79, 0x5e, 0xee, 0xc5, 0x68, 0xae,
|
|
||||||
0x67, 0xab, 0x29, 0x87, 0xd8, 0xb1, 0x9e, 0x45,
|
|
||||||
0x91, 0xe1, 0x05, 0x27, 0xba, 0xa1, 0xdf, 0x3d, // Payload hash
|
|
||||||
0x03, // Payload length varint
|
|
||||||
0x00, 0x01, 0x02, // Payload
|
|
||||||
}
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
in *MsgTx // Message to encode
|
|
||||||
out *MsgTx // Expected decoded message
|
|
||||||
buf []byte // Serialized data
|
|
||||||
scriptPubKeyLocs []int // Expected output script locations
|
|
||||||
}{
|
|
||||||
// No transactions.
|
|
||||||
{
|
|
||||||
"noTx",
|
|
||||||
noTx,
|
|
||||||
noTx,
|
|
||||||
noTxEncoded,
|
|
||||||
nil,
|
|
||||||
},
|
|
||||||
|
|
||||||
// Registry Transaction.
|
|
||||||
{
|
|
||||||
"registryTx",
|
|
||||||
registryTx,
|
|
||||||
registryTx,
|
|
||||||
registryTxEncoded,
|
|
||||||
nil,
|
|
||||||
},
|
|
||||||
|
|
||||||
// Sub Network Transaction.
|
|
||||||
{
|
|
||||||
"subnetworkTx",
|
|
||||||
subnetworkTx,
|
|
||||||
subnetworkTx,
|
|
||||||
subnetworkTxEncoded,
|
|
||||||
nil,
|
|
||||||
},
|
|
||||||
|
|
||||||
// Multiple transactions.
|
|
||||||
{
|
|
||||||
"multiTx",
|
|
||||||
multiTx,
|
|
||||||
multiTx,
|
|
||||||
multiTxEncoded,
|
|
||||||
multiTxScriptPubKeyLocs,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Serialize the transaction.
|
|
||||||
var buf bytes.Buffer
|
|
||||||
err := test.in.Serialize(&buf)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Serialize %s: error %v", test.name, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !bytes.Equal(buf.Bytes(), test.buf) {
|
|
||||||
t.Errorf("Serialize %s:\n got: %s want: %s", test.name,
|
|
||||||
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deserialize the transaction.
|
|
||||||
var tx MsgTx
|
|
||||||
rbuf := bytes.NewReader(test.buf)
|
|
||||||
err = tx.Deserialize(rbuf)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("Deserialize #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(&tx, test.out) {
|
|
||||||
t.Errorf("Deserialize #%d\n got: %s want: %s", i,
|
|
||||||
spew.Sdump(&tx), spew.Sdump(test.out))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure the public key script locations are accurate.
|
|
||||||
scriptPubKeyLocs := test.in.ScriptPubKeyLocs()
|
|
||||||
if !reflect.DeepEqual(scriptPubKeyLocs, test.scriptPubKeyLocs) {
|
|
||||||
t.Errorf("ScriptPubKeyLocs #%d\n got: %s want: %s", i,
|
|
||||||
spew.Sdump(scriptPubKeyLocs),
|
|
||||||
spew.Sdump(test.scriptPubKeyLocs))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
for j, loc := range scriptPubKeyLocs {
|
|
||||||
wantScriptPubKey := test.in.TxOut[j].ScriptPubKey
|
|
||||||
gotScriptPubKey := test.buf[loc : loc+len(wantScriptPubKey)]
|
|
||||||
if !bytes.Equal(gotScriptPubKey, wantScriptPubKey) {
|
|
||||||
t.Errorf("ScriptPubKeyLocs #%d:%d\n unexpected "+
|
|
||||||
"script got: %s want: %s", i, j,
|
|
||||||
spew.Sdump(gotScriptPubKey),
|
|
||||||
spew.Sdump(wantScriptPubKey))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestTxSerializeErrors performs negative tests against appmessage encode and decode
|
|
||||||
// of MsgTx to confirm error paths work correctly.
|
|
||||||
func TestTxSerializeErrors(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
in *MsgTx // Value to encode
|
|
||||||
buf []byte // Serialized data
|
|
||||||
max int // Max size of fixed buffer to induce errors
|
|
||||||
writeErr error // Expected write error
|
|
||||||
readErr error // Expected read error
|
|
||||||
}{
|
|
||||||
// Force error in version.
|
|
||||||
{multiTx, multiTxEncoded, 0, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in number of transaction inputs.
|
|
||||||
{multiTx, multiTxEncoded, 4, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in transaction input previous block hash.
|
|
||||||
{multiTx, multiTxEncoded, 5, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in transaction input previous block output index.
|
|
||||||
{multiTx, multiTxEncoded, 37, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in transaction input signature script length.
|
|
||||||
{multiTx, multiTxEncoded, 41, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in transaction input signature script.
|
|
||||||
{multiTx, multiTxEncoded, 42, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in transaction input sequence.
|
|
||||||
{multiTx, multiTxEncoded, 49, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in number of transaction outputs.
|
|
||||||
{multiTx, multiTxEncoded, 57, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in transaction output value.
|
|
||||||
{multiTx, multiTxEncoded, 58, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in transaction output scriptPubKey length.
|
|
||||||
{multiTx, multiTxEncoded, 66, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in transaction output scriptPubKey.
|
|
||||||
{multiTx, multiTxEncoded, 67, io.ErrShortWrite, io.EOF},
|
|
||||||
// Force error in transaction output lock time.
|
|
||||||
{multiTx, multiTxEncoded, 210, io.ErrShortWrite, io.EOF},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Serialize the transaction.
|
|
||||||
w := newFixedWriter(test.max)
|
|
||||||
err := test.in.Serialize(w)
|
|
||||||
if !errors.Is(err, test.writeErr) {
|
|
||||||
t.Errorf("Serialize #%d wrong error got: %v, want: %v",
|
|
||||||
i, err, test.writeErr)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deserialize the transaction.
|
|
||||||
var tx MsgTx
|
|
||||||
r := newFixedReader(test.max, test.buf)
|
|
||||||
err = tx.Deserialize(r)
|
|
||||||
if !errors.Is(err, test.readErr) {
|
|
||||||
t.Errorf("Deserialize #%d wrong error got: %v, want: %v",
|
|
||||||
i, err, test.readErr)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
registryTx := NewSubnetworkMsgTx(1, nil, nil, subnetworkid.SubnetworkIDRegistry, 1, nil)
|
|
||||||
|
|
||||||
w := bytes.NewBuffer(make([]byte, 0, registryTx.SerializeSize()))
|
|
||||||
err := registryTx.Serialize(w)
|
|
||||||
str := "Transactions from built-in should have 0 gas"
|
|
||||||
expectedErr := messageError("MsgTx.KaspaEncode", str)
|
|
||||||
if err == nil || err.Error() != expectedErr.Error() {
|
|
||||||
t.Errorf("TestTxSerializeErrors: expected error %v but got %v", expectedErr, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
nativeTx := NewSubnetworkMsgTx(1, nil, nil, subnetworkid.SubnetworkIDNative, 1, nil)
|
|
||||||
w = bytes.NewBuffer(make([]byte, 0, registryTx.SerializeSize()))
|
|
||||||
err = nativeTx.Serialize(w)
|
|
||||||
|
|
||||||
str = "Transactions from native subnetwork should have 0 gas"
|
|
||||||
expectedErr = messageError("MsgTx.KaspaEncode", str)
|
|
||||||
if err == nil || err.Error() != expectedErr.Error() {
|
|
||||||
t.Errorf("TestTxSerializeErrors: expected error %v but got %v", expectedErr, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
nativeTx.Gas = 0
|
|
||||||
nativeTx.Payload = []byte{1, 2, 3}
|
|
||||||
nativeTx.PayloadHash = daghash.DoubleHashP(nativeTx.Payload)
|
|
||||||
w = bytes.NewBuffer(make([]byte, 0, registryTx.SerializeSize()))
|
|
||||||
err = nativeTx.Serialize(w)
|
|
||||||
|
|
||||||
str = "Transactions from native subnetwork should have <nil> payload"
|
|
||||||
expectedErr = messageError("MsgTx.KaspaEncode", str)
|
|
||||||
if err == nil || err.Error() != expectedErr.Error() {
|
|
||||||
t.Errorf("TestTxSerializeErrors: expected error %v but got %v", expectedErr, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestTxOverflowErrors performs tests to ensure deserializing transactions
|
|
||||||
// which are intentionally crafted to use large values for the variable number
|
|
||||||
// of inputs and outputs are handled properly. This could otherwise potentially
|
|
||||||
// be used as an attack vector.
|
|
||||||
func TestTxOverflowErrors(t *testing.T) {
|
|
||||||
pver := ProtocolVersion
|
|
||||||
txVer := uint32(1)
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
buf []byte // Encoded value
|
|
||||||
pver uint32 // Protocol version for appmessage encoding
|
|
||||||
version uint32 // Transaction version
|
|
||||||
err error // Expected error
|
|
||||||
}{
|
|
||||||
// Transaction that claims to have ~uint64(0) inputs.
|
|
||||||
{
|
|
||||||
[]byte{
|
|
||||||
0x00, 0x00, 0x00, 0x01, // Version
|
|
||||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
||||||
0xff, // Varint for number of input transactions
|
|
||||||
}, pver, txVer, &MessageError{},
|
|
||||||
},
|
|
||||||
|
|
||||||
// Transaction that claims to have ~uint64(0) outputs.
|
|
||||||
{
|
|
||||||
[]byte{
|
|
||||||
0x00, 0x00, 0x00, 0x01, // Version
|
|
||||||
0x00, // Varint for number of input transactions
|
|
||||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
||||||
0xff, // Varint for number of output transactions
|
|
||||||
}, pver, txVer, &MessageError{},
|
|
||||||
},
|
|
||||||
|
|
||||||
// Transaction that has an input with a signature script that
|
|
||||||
// claims to have ~uint64(0) length.
|
|
||||||
{
|
|
||||||
[]byte{
|
|
||||||
0x00, 0x00, 0x00, 0x01, // Version
|
|
||||||
0x01, // Varint for number of input transactions
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash
|
|
||||||
0xff, 0xff, 0xff, 0xff, // Prevous output index
|
|
||||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
||||||
0xff, // Varint for length of signature script
|
|
||||||
}, pver, txVer, &MessageError{},
|
|
||||||
},
|
|
||||||
|
|
||||||
// Transaction that has an output with a public key script
|
|
||||||
// that claims to have ~uint64(0) length.
|
|
||||||
{
|
|
||||||
[]byte{
|
|
||||||
0x00, 0x00, 0x00, 0x01, // Version
|
|
||||||
0x01, // Varint for number of input transactions
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash
|
|
||||||
0xff, 0xff, 0xff, 0xff, // Prevous output index
|
|
||||||
0x00, // Varint for length of signature script
|
|
||||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // Sequence
|
|
||||||
0x01, // Varint for number of output transactions
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Transaction amount
|
|
||||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
||||||
0xff, // Varint for length of public key script
|
|
||||||
}, pver, txVer, &MessageError{},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Decode from appmessage format.
|
|
||||||
var msg MsgTx
|
|
||||||
r := bytes.NewReader(test.buf)
|
|
||||||
err := msg.KaspaDecode(r, test.pver)
|
|
||||||
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
|
|
||||||
t.Errorf("KaspaDecode #%d wrong error got: %v, want: %v",
|
|
||||||
i, err, reflect.TypeOf(test.err))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode from appmessage format.
|
|
||||||
r = bytes.NewReader(test.buf)
|
|
||||||
err = msg.Deserialize(r)
|
|
||||||
if reflect.TypeOf(err) != reflect.TypeOf(test.err) {
|
|
||||||
t.Errorf("Deserialize #%d wrong error got: %v, want: %v",
|
|
||||||
i, err, reflect.TypeOf(test.err))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestTxSerializeSize performs tests to ensure the serialize size for
|
|
||||||
// various transactions is accurate.
|
|
||||||
func TestTxSerializeSize(t *testing.T) {
|
|
||||||
// Empty tx message.
|
|
||||||
noTx := NewNativeMsgTx(1, nil, nil)
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
in *MsgTx // Tx to encode
|
|
||||||
size int // Expected serialized size
|
|
||||||
}{
|
|
||||||
// No inputs or outpus.
|
|
||||||
{noTx, 34},
|
|
||||||
|
|
||||||
// Transcaction with an input and an output.
|
|
||||||
{multiTx, 238},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
serializedSize := test.in.SerializeSize()
|
|
||||||
if serializedSize != test.size {
|
|
||||||
t.Errorf("MsgTx.SerializeSize: #%d got: %d, want: %d", i,
|
|
||||||
serializedSize, test.size)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIsSubnetworkCompatible(t *testing.T) {
|
|
||||||
testTx := NewSubnetworkMsgTx(1, nil, nil, &subnetworkid.SubnetworkID{123}, 0, []byte{})
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
subnetworkID *subnetworkid.SubnetworkID
|
|
||||||
expectedResult bool
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
name: "Native subnetwork",
|
|
||||||
subnetworkID: subnetworkid.SubnetworkIDNative,
|
|
||||||
expectedResult: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "same subnetwork as test tx",
|
|
||||||
subnetworkID: &subnetworkid.SubnetworkID{123},
|
|
||||||
expectedResult: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "other subnetwork",
|
|
||||||
subnetworkID: &subnetworkid.SubnetworkID{234},
|
|
||||||
expectedResult: false,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
result := testTx.IsSubnetworkCompatible(test.subnetworkID)
|
|
||||||
if result != test.expectedResult {
|
|
||||||
t.Errorf("IsSubnetworkCompatible got unexpected result in test '%s': "+
|
|
||||||
"expected: %t, want: %t", test.name, test.expectedResult, result)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestScriptFreeList(t *testing.T) {
|
|
||||||
var list scriptFreeList = make(chan []byte, freeListMaxItems)
|
|
||||||
|
|
||||||
expectedCapacity := 512
|
|
||||||
expectedLengthFirst := 12
|
|
||||||
expectedLengthSecond := 13
|
|
||||||
|
|
||||||
first := list.Borrow(uint64(expectedLengthFirst))
|
|
||||||
if cap(first) != expectedCapacity {
|
|
||||||
t.Errorf("MsgTx.TestScriptFreeList: Expected capacity for first %d, but got %d",
|
|
||||||
expectedCapacity, cap(first))
|
|
||||||
}
|
|
||||||
if len(first) != expectedLengthFirst {
|
|
||||||
t.Errorf("MsgTx.TestScriptFreeList: Expected length for first %d, but got %d",
|
|
||||||
expectedLengthFirst, len(first))
|
|
||||||
}
|
|
||||||
list.Return(first)
|
|
||||||
|
|
||||||
// Borrow again, and check that the underlying array is re-used for second
|
|
||||||
second := list.Borrow(uint64(expectedLengthSecond))
|
|
||||||
if cap(second) != expectedCapacity {
|
|
||||||
t.Errorf("MsgTx.TestScriptFreeList: Expected capacity for second %d, but got %d",
|
|
||||||
expectedCapacity, cap(second))
|
|
||||||
}
|
|
||||||
if len(second) != expectedLengthSecond {
|
|
||||||
t.Errorf("MsgTx.TestScriptFreeList: Expected length for second %d, but got %d",
|
|
||||||
expectedLengthSecond, len(second))
|
|
||||||
}
|
|
||||||
|
|
||||||
firstArrayAddress := underlyingArrayAddress(first)
|
|
||||||
secondArrayAddress := underlyingArrayAddress(second)
|
|
||||||
|
|
||||||
if firstArrayAddress != secondArrayAddress {
|
|
||||||
t.Errorf("First underlying array is at address %d and second at address %d, "+
|
|
||||||
"which means memory was not re-used", firstArrayAddress, secondArrayAddress)
|
|
||||||
}
|
|
||||||
|
|
||||||
list.Return(second)
|
|
||||||
|
|
||||||
// test for buffers bigger than freeListMaxScriptSize
|
|
||||||
expectedCapacityBig := freeListMaxScriptSize + 1
|
|
||||||
expectedLengthBig := expectedCapacityBig
|
|
||||||
big := list.Borrow(uint64(expectedCapacityBig))
|
|
||||||
|
|
||||||
if cap(big) != expectedCapacityBig {
|
|
||||||
t.Errorf("MsgTx.TestScriptFreeList: Expected capacity for second %d, but got %d",
|
|
||||||
expectedCapacityBig, cap(big))
|
|
||||||
}
|
|
||||||
if len(big) != expectedLengthBig {
|
|
||||||
t.Errorf("MsgTx.TestScriptFreeList: Expected length for second %d, but got %d",
|
|
||||||
expectedLengthBig, len(big))
|
|
||||||
}
|
|
||||||
|
|
||||||
list.Return(big)
|
|
||||||
|
|
||||||
// test there's no crash when channel is full because borrowed too much
|
|
||||||
buffers := make([][]byte, freeListMaxItems+1)
|
|
||||||
for i := 0; i < freeListMaxItems+1; i++ {
|
|
||||||
buffers[i] = list.Borrow(1)
|
|
||||||
}
|
|
||||||
for i := 0; i < freeListMaxItems+1; i++ {
|
|
||||||
list.Return(buffers[i])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func underlyingArrayAddress(buf []byte) uint64 {
|
|
||||||
return uint64((*reflect.SliceHeader)(unsafe.Pointer(&buf)).Data)
|
|
||||||
}
|
|
||||||
|
|
||||||
// multiTx is a MsgTx with an input and output and used in various tests.
|
|
||||||
var multiTxIns = []*TxIn{
|
|
||||||
{
|
|
||||||
PreviousOutpoint: Outpoint{
|
|
||||||
TxID: daghash.TxID{},
|
|
||||||
Index: 0xffffffff,
|
|
||||||
},
|
|
||||||
SignatureScript: []byte{
|
|
||||||
0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62,
|
|
||||||
},
|
|
||||||
Sequence: math.MaxUint64,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
var multiTxOuts = []*TxOut{
|
|
||||||
{
|
|
||||||
Value: 0x12a05f200,
|
|
||||||
ScriptPubKey: []byte{
|
|
||||||
0x41, // OP_DATA_65
|
|
||||||
0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
|
|
||||||
0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
|
|
||||||
0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
|
|
||||||
0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
|
|
||||||
0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
|
|
||||||
0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
|
|
||||||
0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
|
|
||||||
0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
|
|
||||||
0xa6, // 65-byte signature
|
|
||||||
0xac, // OP_CHECKSIG
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Value: 0x5f5e100,
|
|
||||||
ScriptPubKey: []byte{
|
|
||||||
0x41, // OP_DATA_65
|
|
||||||
0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
|
|
||||||
0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
|
|
||||||
0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
|
|
||||||
0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
|
|
||||||
0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
|
|
||||||
0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
|
|
||||||
0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
|
|
||||||
0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
|
|
||||||
0xa6, // 65-byte signature
|
|
||||||
0xac, // OP_CHECKSIG
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
var multiTx = NewNativeMsgTx(1, multiTxIns, multiTxOuts)
|
|
||||||
|
|
||||||
// multiTxEncoded is the appmessage encoded bytes for multiTx using protocol version
|
|
||||||
// 60002 and is used in the various tests.
|
|
||||||
var multiTxEncoded = []byte{
|
|
||||||
0x01, 0x00, 0x00, 0x00, // Version
|
|
||||||
0x01, // Varint for number of input transactions
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash
|
|
||||||
0xff, 0xff, 0xff, 0xff, // Prevous output index
|
|
||||||
0x07, // Varint for length of signature script
|
|
||||||
0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62, // Signature script
|
|
||||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // Sequence
|
|
||||||
0x02, // Varint for number of output transactions
|
|
||||||
0x00, 0xf2, 0x05, 0x2a, 0x01, 0x00, 0x00, 0x00, // Transaction amount
|
|
||||||
0x43, // Varint for length of scriptPubKey
|
|
||||||
0x41, // OP_DATA_65
|
|
||||||
0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
|
|
||||||
0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
|
|
||||||
0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
|
|
||||||
0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
|
|
||||||
0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
|
|
||||||
0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
|
|
||||||
0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
|
|
||||||
0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
|
|
||||||
0xa6, // 65-byte signature
|
|
||||||
0xac, // OP_CHECKSIG
|
|
||||||
0x00, 0xe1, 0xf5, 0x05, 0x00, 0x00, 0x00, 0x00, // Transaction amount
|
|
||||||
0x43, // Varint for length of scriptPubKey
|
|
||||||
0x41, // OP_DATA_65
|
|
||||||
0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
|
|
||||||
0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
|
|
||||||
0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
|
|
||||||
0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
|
|
||||||
0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
|
|
||||||
0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
|
|
||||||
0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
|
|
||||||
0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
|
|
||||||
0xa6, // 65-byte signature
|
|
||||||
0xac, // OP_CHECKSIG
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Lock time
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, // Sub Network ID
|
|
||||||
}
|
|
||||||
|
|
||||||
// multiTxScriptPubKeyLocs is the location information for the public key scripts
|
|
||||||
// located in multiTx.
|
|
||||||
var multiTxScriptPubKeyLocs = []int{67, 143}
|
|
||||||
19
app/appmessage/p2p_blockheaders.go
Normal file
19
app/appmessage/p2p_blockheaders.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// BlockHeadersMessage represents a kaspa BlockHeaders message
|
||||||
|
type BlockHeadersMessage struct {
|
||||||
|
baseMessage
|
||||||
|
BlockHeaders []*MsgBlockHeader
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *BlockHeadersMessage) Command() MessageCommand {
|
||||||
|
return CmdBlockHeaders
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewBlockHeadersMessage returns a new kaspa BlockHeaders message
|
||||||
|
func NewBlockHeadersMessage(blockHeaders []*MsgBlockHeader) *BlockHeadersMessage {
|
||||||
|
return &BlockHeadersMessage{
|
||||||
|
BlockHeaders: blockHeaders,
|
||||||
|
}
|
||||||
|
}
|
||||||
30
app/appmessage/p2p_msgaddresses.go
Normal file
30
app/appmessage/p2p_msgaddresses.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
// Copyright (c) 2013-2015 The btcsuite developers
|
||||||
|
// Use of this source code is governed by an ISC
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package appmessage
|
||||||
|
|
||||||
|
// MaxAddressesPerMsg is the maximum number of addresses that can be in a single
|
||||||
|
// kaspa Addresses message (MsgAddresses).
|
||||||
|
const MaxAddressesPerMsg = 1000
|
||||||
|
|
||||||
|
// MsgAddresses implements the Message interface and represents a kaspa
|
||||||
|
// Addresses message.
|
||||||
|
type MsgAddresses struct {
|
||||||
|
baseMessage
|
||||||
|
AddressList []*NetAddress
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message. This is part
|
||||||
|
// of the Message interface implementation.
|
||||||
|
func (msg *MsgAddresses) Command() MessageCommand {
|
||||||
|
return CmdAddresses
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMsgAddresses returns a new kaspa Addresses message that conforms to the
|
||||||
|
// Message interface. See MsgAddresses for details.
|
||||||
|
func NewMsgAddresses(addressList []*NetAddress) *MsgAddresses {
|
||||||
|
return &MsgAddresses{
|
||||||
|
AddressList: addressList,
|
||||||
|
}
|
||||||
|
}
|
||||||
74
app/appmessage/p2p_msgblock.go
Normal file
74
app/appmessage/p2p_msgblock.go
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
// Copyright (c) 2013-2016 The btcsuite developers
|
||||||
|
// Use of this source code is governed by an ISC
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package appmessage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
// defaultTransactionAlloc is the default size used for the backing array
|
||||||
|
// for transactions. The transaction array will dynamically grow as needed, but
|
||||||
|
// this figure is intended to provide enough space for the number of
|
||||||
|
// transactions in the vast majority of blocks without needing to grow the
|
||||||
|
// backing array multiple times.
|
||||||
|
const defaultTransactionAlloc = 2048
|
||||||
|
|
||||||
|
// TxLoc holds locator data for the offset and length of where a transaction is
|
||||||
|
// located within a MsgBlock data buffer.
|
||||||
|
type TxLoc struct {
|
||||||
|
TxStart int
|
||||||
|
TxLen int
|
||||||
|
}
|
||||||
|
|
||||||
|
// MsgBlock implements the Message interface and represents a kaspa
|
||||||
|
// block message. It is used to deliver block and transaction information in
|
||||||
|
// response to a getdata message (MsgGetData) for a given block hash.
|
||||||
|
type MsgBlock struct {
|
||||||
|
baseMessage
|
||||||
|
Header MsgBlockHeader
|
||||||
|
Transactions []*MsgTx
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddTransaction adds a transaction to the message.
|
||||||
|
func (msg *MsgBlock) AddTransaction(tx *MsgTx) {
|
||||||
|
msg.Transactions = append(msg.Transactions, tx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ClearTransactions removes all transactions from the message.
|
||||||
|
func (msg *MsgBlock) ClearTransactions() {
|
||||||
|
msg.Transactions = make([]*MsgTx, 0, defaultTransactionAlloc)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message. This is part
|
||||||
|
// of the Message interface implementation.
|
||||||
|
func (msg *MsgBlock) Command() MessageCommand {
|
||||||
|
return CmdBlock
|
||||||
|
}
|
||||||
|
|
||||||
|
// MaxPayloadLength returns the maximum length the payload can be for the
|
||||||
|
// receiver. This is part of the Message interface implementation.
|
||||||
|
func (msg *MsgBlock) MaxPayloadLength(pver uint32) uint32 {
|
||||||
|
return MaxMessagePayload
|
||||||
|
}
|
||||||
|
|
||||||
|
// ConvertToPartial clears out all the payloads of the subnetworks that are
|
||||||
|
// incompatible with the given subnetwork ID.
|
||||||
|
// Note: this operation modifies the block in place.
|
||||||
|
func (msg *MsgBlock) ConvertToPartial(subnetworkID *externalapi.DomainSubnetworkID) {
|
||||||
|
for _, tx := range msg.Transactions {
|
||||||
|
if !tx.SubnetworkID.Equal(subnetworkID) {
|
||||||
|
tx.Payload = []byte{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMsgBlock returns a new kaspa block message that conforms to the
|
||||||
|
// Message interface. See MsgBlock for details.
|
||||||
|
func NewMsgBlock(blockHeader *MsgBlockHeader) *MsgBlock {
|
||||||
|
return &MsgBlock{
|
||||||
|
Header: *blockHeader,
|
||||||
|
Transactions: make([]*MsgTx, 0, defaultTransactionAlloc),
|
||||||
|
}
|
||||||
|
}
|
||||||
240
app/appmessage/p2p_msgblock_test.go
Normal file
240
app/appmessage/p2p_msgblock_test.go
Normal file
@@ -0,0 +1,240 @@
|
|||||||
|
// Copyright (c) 2013-2016 The btcsuite developers
|
||||||
|
// Use of this source code is governed by an ISC
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package appmessage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
"github.com/kaspanet/kaspad/util/mstime"
|
||||||
|
"math"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/utils/subnetworks"
|
||||||
|
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestBlock tests the MsgBlock API.
|
||||||
|
func TestBlock(t *testing.T) {
|
||||||
|
pver := ProtocolVersion
|
||||||
|
|
||||||
|
// Block 1 header.
|
||||||
|
parentHashes := blockOne.Header.ParentHashes
|
||||||
|
hashMerkleRoot := blockOne.Header.HashMerkleRoot
|
||||||
|
acceptedIDMerkleRoot := blockOne.Header.AcceptedIDMerkleRoot
|
||||||
|
utxoCommitment := blockOne.Header.UTXOCommitment
|
||||||
|
bits := blockOne.Header.Bits
|
||||||
|
nonce := blockOne.Header.Nonce
|
||||||
|
bh := NewBlockHeader(1, parentHashes, hashMerkleRoot, acceptedIDMerkleRoot, utxoCommitment, bits, nonce)
|
||||||
|
|
||||||
|
// Ensure the command is expected value.
|
||||||
|
wantCmd := MessageCommand(5)
|
||||||
|
msg := NewMsgBlock(bh)
|
||||||
|
if cmd := msg.Command(); cmd != wantCmd {
|
||||||
|
t.Errorf("NewMsgBlock: wrong command - got %v want %v",
|
||||||
|
cmd, wantCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure max payload is expected value for latest protocol version.
|
||||||
|
wantPayload := uint32(1024 * 1024 * 32)
|
||||||
|
maxPayload := msg.MaxPayloadLength(pver)
|
||||||
|
if maxPayload != wantPayload {
|
||||||
|
t.Errorf("MaxPayloadLength: wrong max payload length for "+
|
||||||
|
"protocol version %d - got %v, want %v", pver,
|
||||||
|
maxPayload, wantPayload)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure we get the same block header data back out.
|
||||||
|
if !reflect.DeepEqual(&msg.Header, bh) {
|
||||||
|
t.Errorf("NewMsgBlock: wrong block header - got %v, want %v",
|
||||||
|
spew.Sdump(&msg.Header), spew.Sdump(bh))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure transactions are added properly.
|
||||||
|
tx := blockOne.Transactions[0].Copy()
|
||||||
|
msg.AddTransaction(tx)
|
||||||
|
if !reflect.DeepEqual(msg.Transactions, blockOne.Transactions) {
|
||||||
|
t.Errorf("AddTransaction: wrong transactions - got %v, want %v",
|
||||||
|
spew.Sdump(msg.Transactions),
|
||||||
|
spew.Sdump(blockOne.Transactions))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure transactions are properly cleared.
|
||||||
|
msg.ClearTransactions()
|
||||||
|
if len(msg.Transactions) != 0 {
|
||||||
|
t.Errorf("ClearTransactions: wrong transactions - got %v, want %v",
|
||||||
|
len(msg.Transactions), 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestConvertToPartial(t *testing.T) {
|
||||||
|
localSubnetworkID := &externalapi.DomainSubnetworkID{0x12}
|
||||||
|
|
||||||
|
transactions := []struct {
|
||||||
|
subnetworkID *externalapi.DomainSubnetworkID
|
||||||
|
payload []byte
|
||||||
|
expectedPayloadLength int
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
subnetworkID: &subnetworks.SubnetworkIDNative,
|
||||||
|
payload: []byte{},
|
||||||
|
expectedPayloadLength: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
subnetworkID: &subnetworks.SubnetworkIDRegistry,
|
||||||
|
payload: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08},
|
||||||
|
expectedPayloadLength: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
subnetworkID: localSubnetworkID,
|
||||||
|
payload: []byte{0x01},
|
||||||
|
expectedPayloadLength: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
subnetworkID: &externalapi.DomainSubnetworkID{0x34},
|
||||||
|
payload: []byte{0x02},
|
||||||
|
expectedPayloadLength: 0,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
block := MsgBlock{}
|
||||||
|
payload := []byte{1}
|
||||||
|
for _, transaction := range transactions {
|
||||||
|
block.Transactions = append(block.Transactions, NewSubnetworkMsgTx(1, nil, nil, transaction.subnetworkID, 0, payload))
|
||||||
|
}
|
||||||
|
|
||||||
|
block.ConvertToPartial(localSubnetworkID)
|
||||||
|
|
||||||
|
for _, testTransaction := range transactions {
|
||||||
|
var subnetworkTx *MsgTx
|
||||||
|
for _, blockTransaction := range block.Transactions {
|
||||||
|
if blockTransaction.SubnetworkID.Equal(testTransaction.subnetworkID) {
|
||||||
|
subnetworkTx = blockTransaction
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if subnetworkTx == nil {
|
||||||
|
t.Errorf("ConvertToPartial: subnetworkID '%s' not found in block!", testTransaction.subnetworkID)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
payloadLength := len(subnetworkTx.Payload)
|
||||||
|
if payloadLength != testTransaction.expectedPayloadLength {
|
||||||
|
t.Errorf("ConvertToPartial: unexpected payload length for subnetwork '%s': expected: %d, got: %d",
|
||||||
|
testTransaction.subnetworkID, testTransaction.expectedPayloadLength, payloadLength)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//blockOne is the first block in the mainnet block DAG.
|
||||||
|
var blockOne = MsgBlock{
|
||||||
|
Header: MsgBlockHeader{
|
||||||
|
Version: 0,
|
||||||
|
ParentHashes: []*externalapi.DomainHash{mainnetGenesisHash, simnetGenesisHash},
|
||||||
|
HashMerkleRoot: mainnetGenesisMerkleRoot,
|
||||||
|
AcceptedIDMerkleRoot: exampleAcceptedIDMerkleRoot,
|
||||||
|
UTXOCommitment: exampleUTXOCommitment,
|
||||||
|
Timestamp: mstime.UnixMilliseconds(0x17315ed0f99),
|
||||||
|
Bits: 0x1d00ffff, // 486604799
|
||||||
|
Nonce: 0x9962e301, // 2573394689
|
||||||
|
},
|
||||||
|
Transactions: []*MsgTx{
|
||||||
|
NewNativeMsgTx(1,
|
||||||
|
[]*TxIn{
|
||||||
|
{
|
||||||
|
PreviousOutpoint: Outpoint{
|
||||||
|
TxID: externalapi.DomainTransactionID{},
|
||||||
|
Index: 0xffffffff,
|
||||||
|
},
|
||||||
|
SignatureScript: []byte{
|
||||||
|
0x04, 0xff, 0xff, 0x00, 0x1d, 0x01, 0x04,
|
||||||
|
},
|
||||||
|
Sequence: math.MaxUint64,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
[]*TxOut{
|
||||||
|
{
|
||||||
|
Value: 0x12a05f200,
|
||||||
|
ScriptPubKey: &externalapi.ScriptPublicKey{
|
||||||
|
Script: []byte{
|
||||||
|
0x41, // OP_DATA_65
|
||||||
|
0x04, 0x96, 0xb5, 0x38, 0xe8, 0x53, 0x51, 0x9c,
|
||||||
|
0x72, 0x6a, 0x2c, 0x91, 0xe6, 0x1e, 0xc1, 0x16,
|
||||||
|
0x00, 0xae, 0x13, 0x90, 0x81, 0x3a, 0x62, 0x7c,
|
||||||
|
0x66, 0xfb, 0x8b, 0xe7, 0x94, 0x7b, 0xe6, 0x3c,
|
||||||
|
0x52, 0xda, 0x75, 0x89, 0x37, 0x95, 0x15, 0xd4,
|
||||||
|
0xe0, 0xa6, 0x04, 0xf8, 0x14, 0x17, 0x81, 0xe6,
|
||||||
|
0x22, 0x94, 0x72, 0x11, 0x66, 0xbf, 0x62, 0x1e,
|
||||||
|
0x73, 0xa8, 0x2c, 0xbf, 0x23, 0x42, 0xc8, 0x58,
|
||||||
|
0xee, // 65-byte signature
|
||||||
|
0xac, // OP_CHECKSIG
|
||||||
|
},
|
||||||
|
Version: 0},
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Block one serialized bytes.
|
||||||
|
var blockOneBytes = []byte{
|
||||||
|
0x00, 0x00, // Version 0
|
||||||
|
0x02, // NumParentBlocks
|
||||||
|
0xdc, 0x5f, 0x5b, 0x5b, 0x1d, 0xc2, 0xa7, 0x25, // mainnetGenesisHash
|
||||||
|
0x49, 0xd5, 0x1d, 0x4d, 0xee, 0xd7, 0xa4, 0x8b,
|
||||||
|
0xaf, 0xd3, 0x14, 0x4b, 0x56, 0x78, 0x98, 0xb1,
|
||||||
|
0x8c, 0xfd, 0x9f, 0x69, 0xdd, 0xcf, 0xbb, 0x63,
|
||||||
|
0xf6, 0x7a, 0xd7, 0x69, 0x5d, 0x9b, 0x66, 0x2a, // simnetGenesisHash
|
||||||
|
0x72, 0xff, 0x3d, 0x8e, 0xdb, 0xbb, 0x2d, 0xe0,
|
||||||
|
0xbf, 0xa6, 0x7b, 0x13, 0x97, 0x4b, 0xb9, 0x91,
|
||||||
|
0x0d, 0x11, 0x6d, 0x5c, 0xbd, 0x86, 0x3e, 0x68,
|
||||||
|
0x4a, 0x5e, 0x1e, 0x4b, 0xaa, 0xb8, 0x9f, 0x3a, // HashMerkleRoot
|
||||||
|
0x32, 0x51, 0x8a, 0x88, 0xc3, 0x1b, 0xc8, 0x7f,
|
||||||
|
0x61, 0x8f, 0x76, 0x67, 0x3e, 0x2c, 0xc7, 0x7a,
|
||||||
|
0xb2, 0x12, 0x7b, 0x7a, 0xfd, 0xed, 0xa3, 0x3b,
|
||||||
|
0x09, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C, // AcceptedIDMerkleRoot
|
||||||
|
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
|
||||||
|
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
|
||||||
|
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
|
||||||
|
0x10, 0x3B, 0xC7, 0xE3, 0x67, 0x11, 0x7B, 0x3C, // UTXOCommitment
|
||||||
|
0x30, 0xC1, 0xF8, 0xFD, 0xD0, 0xD9, 0x72, 0x87,
|
||||||
|
0x7F, 0x16, 0xC5, 0x96, 0x2E, 0x8B, 0xD9, 0x63,
|
||||||
|
0x65, 0x9C, 0x79, 0x3C, 0xE3, 0x70, 0xD9, 0x5F,
|
||||||
|
0x99, 0x0f, 0xed, 0x15, 0x73, 0x01, 0x00, 0x00, // Timestamp
|
||||||
|
0xff, 0xff, 0x00, 0x1d, // Bits
|
||||||
|
0x01, 0xe3, 0x62, 0x99, 0x00, 0x00, 0x00, 0x00, // Fake Nonce
|
||||||
|
0x01, // TxnCount
|
||||||
|
0x00, 0x00, 0x00, 0x00, // Version
|
||||||
|
0x01, // Varint for number of transaction inputs
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Previous output hash
|
||||||
|
0xff, 0xff, 0xff, 0xff, // Prevous output index
|
||||||
|
0x07, // Varint for length of signature script
|
||||||
|
0x04, 0xff, 0xff, 0x00, 0x1d, 0x01, 0x04, // Signature script (coinbase)
|
||||||
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // Sequence
|
||||||
|
0x01, // Varint for number of transaction outputs
|
||||||
|
0x00, 0xf2, 0x05, 0x2a, 0x01, 0x00, 0x00, 0x00, // Transaction amount
|
||||||
|
0x43, // Varint for length of scriptPubKey
|
||||||
|
0x41, // OP_DATA_65
|
||||||
|
0x04, 0x96, 0xb5, 0x38, 0xe8, 0x53, 0x51, 0x9c,
|
||||||
|
0x72, 0x6a, 0x2c, 0x91, 0xe6, 0x1e, 0xc1, 0x16,
|
||||||
|
0x00, 0xae, 0x13, 0x90, 0x81, 0x3a, 0x62, 0x7c,
|
||||||
|
0x66, 0xfb, 0x8b, 0xe7, 0x94, 0x7b, 0xe6, 0x3c,
|
||||||
|
0x52, 0xda, 0x75, 0x89, 0x37, 0x95, 0x15, 0xd4,
|
||||||
|
0xe0, 0xa6, 0x04, 0xf8, 0x14, 0x17, 0x81, 0xe6,
|
||||||
|
0x22, 0x94, 0x72, 0x11, 0x66, 0xbf, 0x62, 0x1e,
|
||||||
|
0x73, 0xa8, 0x2c, 0xbf, 0x23, 0x42, 0xc8, 0x58,
|
||||||
|
0xee, // 65-byte uncompressed public key
|
||||||
|
0xac, // OP_CHECKSIG
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // Lock time
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, // SubnetworkID
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transaction location information for block one transactions.
|
||||||
|
var blockOneTxLocs = []TxLoc{
|
||||||
|
{TxStart: 186, TxLen: 162},
|
||||||
|
}
|
||||||
108
app/appmessage/p2p_msgblockheader.go
Normal file
108
app/appmessage/p2p_msgblockheader.go
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
// Copyright (c) 2013-2016 The btcsuite developers
|
||||||
|
// Use of this source code is governed by an ISC
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package appmessage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math"
|
||||||
|
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
|
||||||
|
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
|
"github.com/kaspanet/kaspad/util/mstime"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
// BaseBlockHeaderPayload is the base number of bytes a block header can be,
|
||||||
|
// not including the list of parent block headers.
|
||||||
|
// Version 4 bytes + Timestamp 8 bytes + Bits 4 bytes + Nonce 8 bytes +
|
||||||
|
// + NumParentBlocks 1 byte + HashMerkleRoot hash +
|
||||||
|
// + AcceptedIDMerkleRoot hash + UTXOCommitment hash.
|
||||||
|
// To get total size of block header len(ParentHashes) * externalapi.DomainHashSize should be
|
||||||
|
// added to this value
|
||||||
|
const BaseBlockHeaderPayload = 25 + 3*(externalapi.DomainHashSize)
|
||||||
|
|
||||||
|
// MaxNumParentBlocks is the maximum number of parent blocks a block can reference.
|
||||||
|
// Currently set to 255 as the maximum number NumParentBlocks can be due to it being a byte
|
||||||
|
const MaxNumParentBlocks = 255
|
||||||
|
|
||||||
|
// MaxBlockHeaderPayload is the maximum number of bytes a block header can be.
|
||||||
|
// BaseBlockHeaderPayload + up to MaxNumParentBlocks hashes of parent blocks
|
||||||
|
const MaxBlockHeaderPayload = BaseBlockHeaderPayload + (MaxNumParentBlocks * externalapi.DomainHashSize)
|
||||||
|
|
||||||
|
// MsgBlockHeader defines information about a block and is used in the kaspa
|
||||||
|
// block (MsgBlock) and headers (MsgHeader) messages.
|
||||||
|
type MsgBlockHeader struct {
|
||||||
|
baseMessage
|
||||||
|
|
||||||
|
// Version of the block. This is not the same as the protocol version.
|
||||||
|
Version uint16
|
||||||
|
|
||||||
|
// Hashes of the parent block headers in the blockDAG.
|
||||||
|
ParentHashes []*externalapi.DomainHash
|
||||||
|
|
||||||
|
// HashMerkleRoot is the merkle tree reference to hash of all transactions for the block.
|
||||||
|
HashMerkleRoot *externalapi.DomainHash
|
||||||
|
|
||||||
|
// AcceptedIDMerkleRoot is merkle tree reference to hash all transactions
|
||||||
|
// accepted form the block.Blues
|
||||||
|
AcceptedIDMerkleRoot *externalapi.DomainHash
|
||||||
|
|
||||||
|
// UTXOCommitment is an ECMH UTXO commitment to the block UTXO.
|
||||||
|
UTXOCommitment *externalapi.DomainHash
|
||||||
|
|
||||||
|
// Time the block was created.
|
||||||
|
Timestamp mstime.Time
|
||||||
|
|
||||||
|
// Difficulty target for the block.
|
||||||
|
Bits uint32
|
||||||
|
|
||||||
|
// Nonce used to generate the block.
|
||||||
|
Nonce uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
// NumParentBlocks return the number of entries in ParentHashes
|
||||||
|
func (h *MsgBlockHeader) NumParentBlocks() byte {
|
||||||
|
numParents := len(h.ParentHashes)
|
||||||
|
if numParents > math.MaxUint8 {
|
||||||
|
panic(errors.Errorf("number of parents is %d, which is more than one byte can fit", numParents))
|
||||||
|
}
|
||||||
|
return byte(numParents)
|
||||||
|
}
|
||||||
|
|
||||||
|
// BlockHash computes the block identifier hash for the given block header.
|
||||||
|
func (h *MsgBlockHeader) BlockHash() *externalapi.DomainHash {
|
||||||
|
return consensushashing.HeaderHash(BlockHeaderToDomainBlockHeader(h))
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsGenesis returns true iff this block is a genesis block
|
||||||
|
func (h *MsgBlockHeader) IsGenesis() bool {
|
||||||
|
return h.NumParentBlocks() == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message. This is part
|
||||||
|
// of the Message interface implementation.
|
||||||
|
func (h *MsgBlockHeader) Command() MessageCommand {
|
||||||
|
return CmdHeader
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewBlockHeader returns a new MsgBlockHeader using the provided version, previous
|
||||||
|
// block hash, hash merkle root, accepted ID merkle root, difficulty bits, and nonce used to generate the
|
||||||
|
// block with defaults or calclulated values for the remaining fields.
|
||||||
|
func NewBlockHeader(version uint16, parentHashes []*externalapi.DomainHash, hashMerkleRoot *externalapi.DomainHash,
|
||||||
|
acceptedIDMerkleRoot *externalapi.DomainHash, utxoCommitment *externalapi.DomainHash, bits uint32, nonce uint64) *MsgBlockHeader {
|
||||||
|
|
||||||
|
// Limit the timestamp to one millisecond precision since the protocol
|
||||||
|
// doesn't support better.
|
||||||
|
return &MsgBlockHeader{
|
||||||
|
Version: version,
|
||||||
|
ParentHashes: parentHashes,
|
||||||
|
HashMerkleRoot: hashMerkleRoot,
|
||||||
|
AcceptedIDMerkleRoot: acceptedIDMerkleRoot,
|
||||||
|
UTXOCommitment: utxoCommitment,
|
||||||
|
Timestamp: mstime.Now(),
|
||||||
|
Bits: bits,
|
||||||
|
Nonce: nonce,
|
||||||
|
}
|
||||||
|
}
|
||||||
84
app/appmessage/p2p_msgblockheader_test.go
Normal file
84
app/appmessage/p2p_msgblockheader_test.go
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
// Copyright (c) 2013-2016 The btcsuite developers
|
||||||
|
// Use of this source code is governed by an ISC
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package appmessage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
|
"github.com/kaspanet/kaspad/util/mstime"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestBlockHeader tests the MsgBlockHeader API.
|
||||||
|
func TestBlockHeader(t *testing.T) {
|
||||||
|
nonce := uint64(0xba4d87a69924a93d)
|
||||||
|
|
||||||
|
hashes := []*externalapi.DomainHash{mainnetGenesisHash, simnetGenesisHash}
|
||||||
|
|
||||||
|
merkleHash := mainnetGenesisMerkleRoot
|
||||||
|
acceptedIDMerkleRoot := exampleAcceptedIDMerkleRoot
|
||||||
|
bits := uint32(0x1d00ffff)
|
||||||
|
bh := NewBlockHeader(1, hashes, merkleHash, acceptedIDMerkleRoot, exampleUTXOCommitment, bits, nonce)
|
||||||
|
|
||||||
|
// Ensure we get the same data back out.
|
||||||
|
if !reflect.DeepEqual(bh.ParentHashes, hashes) {
|
||||||
|
t.Errorf("NewBlockHeader: wrong prev hashes - got %v, want %v",
|
||||||
|
spew.Sprint(bh.ParentHashes), spew.Sprint(hashes))
|
||||||
|
}
|
||||||
|
if bh.HashMerkleRoot != merkleHash {
|
||||||
|
t.Errorf("NewBlockHeader: wrong merkle root - got %v, want %v",
|
||||||
|
spew.Sprint(bh.HashMerkleRoot), spew.Sprint(merkleHash))
|
||||||
|
}
|
||||||
|
if bh.Bits != bits {
|
||||||
|
t.Errorf("NewBlockHeader: wrong bits - got %v, want %v",
|
||||||
|
bh.Bits, bits)
|
||||||
|
}
|
||||||
|
if bh.Nonce != nonce {
|
||||||
|
t.Errorf("NewBlockHeader: wrong nonce - got %v, want %v",
|
||||||
|
bh.Nonce, nonce)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsGenesis(t *testing.T) {
|
||||||
|
nonce := uint64(123123) // 0x1e0f3
|
||||||
|
bits := uint32(0x1d00ffff)
|
||||||
|
timestamp := mstime.UnixMilliseconds(0x495fab29000)
|
||||||
|
|
||||||
|
baseBlockHdr := &MsgBlockHeader{
|
||||||
|
Version: 1,
|
||||||
|
ParentHashes: []*externalapi.DomainHash{mainnetGenesisHash, simnetGenesisHash},
|
||||||
|
HashMerkleRoot: mainnetGenesisMerkleRoot,
|
||||||
|
Timestamp: timestamp,
|
||||||
|
Bits: bits,
|
||||||
|
Nonce: nonce,
|
||||||
|
}
|
||||||
|
genesisBlockHdr := &MsgBlockHeader{
|
||||||
|
Version: 1,
|
||||||
|
ParentHashes: []*externalapi.DomainHash{},
|
||||||
|
HashMerkleRoot: mainnetGenesisMerkleRoot,
|
||||||
|
Timestamp: timestamp,
|
||||||
|
Bits: bits,
|
||||||
|
Nonce: nonce,
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
in *MsgBlockHeader // Block header to encode
|
||||||
|
isGenesis bool // Expected result for call of .IsGenesis
|
||||||
|
}{
|
||||||
|
{genesisBlockHdr, true},
|
||||||
|
{baseBlockHdr, false},
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Logf("Running %d tests", len(tests))
|
||||||
|
for i, test := range tests {
|
||||||
|
isGenesis := test.in.IsGenesis()
|
||||||
|
if isGenesis != test.isGenesis {
|
||||||
|
t.Errorf("MsgBlockHeader.IsGenesis: #%d got: %t, want: %t",
|
||||||
|
i, isGenesis, test.isGenesis)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package appmessage
|
package appmessage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MaxBlockLocatorsPerMsg is the maximum number of block locator hashes allowed
|
// MaxBlockLocatorsPerMsg is the maximum number of block locator hashes allowed
|
||||||
@@ -13,7 +13,7 @@ const MaxBlockLocatorsPerMsg = 500
|
|||||||
// syncing with you.
|
// syncing with you.
|
||||||
type MsgBlockLocator struct {
|
type MsgBlockLocator struct {
|
||||||
baseMessage
|
baseMessage
|
||||||
BlockLocatorHashes []*daghash.Hash
|
BlockLocatorHashes []*externalapi.DomainHash
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
// Command returns the protocol command string for the message. This is part
|
||||||
@@ -24,7 +24,7 @@ func (msg *MsgBlockLocator) Command() MessageCommand {
|
|||||||
|
|
||||||
// NewMsgBlockLocator returns a new kaspa locator message that conforms to
|
// NewMsgBlockLocator returns a new kaspa locator message that conforms to
|
||||||
// the Message interface. See MsgBlockLocator for details.
|
// the Message interface. See MsgBlockLocator for details.
|
||||||
func NewMsgBlockLocator(locatorHashes []*daghash.Hash) *MsgBlockLocator {
|
func NewMsgBlockLocator(locatorHashes []*externalapi.DomainHash) *MsgBlockLocator {
|
||||||
return &MsgBlockLocator{
|
return &MsgBlockLocator{
|
||||||
BlockLocatorHashes: locatorHashes,
|
BlockLocatorHashes: locatorHashes,
|
||||||
}
|
}
|
||||||
@@ -3,19 +3,20 @@ package appmessage
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestBlockLocator tests the MsgBlockLocator API.
|
// TestBlockLocator tests the MsgBlockLocator API.
|
||||||
func TestBlockLocator(t *testing.T) {
|
func TestBlockLocator(t *testing.T) {
|
||||||
hashStr := "000000000002e7ad7b9eef9479e4aabc65cb831269cc20d2632c13684406dee0"
|
hashStr := "000000000002e7ad7b9eef9479e4aabc65cb831269cc20d2632c13684406dee0"
|
||||||
locatorHash, err := daghash.NewHashFromStr(hashStr)
|
locatorHash, err := externalapi.NewDomainHashFromString(hashStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("NewHashFromStr: %v", err)
|
t.Errorf("NewHashFromStr: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
msg := NewMsgBlockLocator([]*daghash.Hash{locatorHash})
|
msg := NewMsgBlockLocator([]*externalapi.DomainHash{locatorHash})
|
||||||
|
|
||||||
// Ensure the command is expected value.
|
// Ensure the command is expected value.
|
||||||
wantCmd := MessageCommand(10)
|
wantCmd := MessageCommand(10)
|
||||||
22
app/appmessage/p2p_msgdoneheaders.go
Normal file
22
app/appmessage/p2p_msgdoneheaders.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// MsgDoneHeaders implements the Message interface and represents a kaspa
|
||||||
|
// DoneHeaders message. It is used to notify the IBD syncing peer that the
|
||||||
|
// syncer sent all the requested headers.
|
||||||
|
//
|
||||||
|
// This message has no payload.
|
||||||
|
type MsgDoneHeaders struct {
|
||||||
|
baseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message. This is part
|
||||||
|
// of the Message interface implementation.
|
||||||
|
func (msg *MsgDoneHeaders) Command() MessageCommand {
|
||||||
|
return CmdDoneHeaders
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMsgDoneHeaders returns a new kaspa DoneIBDBlocks message that conforms to the
|
||||||
|
// Message interface.
|
||||||
|
func NewMsgDoneHeaders() *MsgDoneHeaders {
|
||||||
|
return &MsgDoneHeaders{}
|
||||||
|
}
|
||||||
16
app/appmessage/p2p_msgdonepruningpointutxosetchunks.go
Normal file
16
app/appmessage/p2p_msgdonepruningpointutxosetchunks.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// MsgDonePruningPointUTXOSetChunks represents a kaspa DonePruningPointUTXOSetChunks message
|
||||||
|
type MsgDonePruningPointUTXOSetChunks struct {
|
||||||
|
baseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *MsgDonePruningPointUTXOSetChunks) Command() MessageCommand {
|
||||||
|
return CmdDonePruningPointUTXOSetChunks
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMsgDonePruningPointUTXOSetChunks returns a new MsgDonePruningPointUTXOSetChunks.
|
||||||
|
func NewMsgDonePruningPointUTXOSetChunks() *MsgDonePruningPointUTXOSetChunks {
|
||||||
|
return &MsgDonePruningPointUTXOSetChunks{}
|
||||||
|
}
|
||||||
@@ -5,7 +5,6 @@
|
|||||||
package appmessage
|
package appmessage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -26,7 +25,7 @@ func TestIBDBlock(t *testing.T) {
|
|||||||
bh := NewBlockHeader(1, parentHashes, hashMerkleRoot, acceptedIDMerkleRoot, utxoCommitment, bits, nonce)
|
bh := NewBlockHeader(1, parentHashes, hashMerkleRoot, acceptedIDMerkleRoot, utxoCommitment, bits, nonce)
|
||||||
|
|
||||||
// Ensure the command is expected value.
|
// Ensure the command is expected value.
|
||||||
wantCmd := MessageCommand(17)
|
wantCmd := MessageCommand(15)
|
||||||
msg := NewMsgIBDBlock(NewMsgBlock(bh))
|
msg := NewMsgIBDBlock(NewMsgBlock(bh))
|
||||||
if cmd := msg.Command(); cmd != wantCmd {
|
if cmd := msg.Command(); cmd != wantCmd {
|
||||||
t.Errorf("NewMsgIBDBlock: wrong command - got %v want %v",
|
t.Errorf("NewMsgIBDBlock: wrong command - got %v want %v",
|
||||||
@@ -64,55 +63,3 @@ func TestIBDBlock(t *testing.T) {
|
|||||||
len(msg.Transactions), 0)
|
len(msg.Transactions), 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestIBDBlockEncoding tests the MsgIBDBlock appmessage encode and decode for various numbers
|
|
||||||
// of transaction inputs and outputs and protocol versions.
|
|
||||||
func TestIBDBlockEncoding(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
in *MsgIBDBlock // Message to encode
|
|
||||||
out *MsgIBDBlock // Expected decoded message
|
|
||||||
buf []byte // Encoded value
|
|
||||||
txLocs []TxLoc // Expected transaction locations
|
|
||||||
pver uint32 // Protocol version for appmessage encoding
|
|
||||||
}{
|
|
||||||
// Latest protocol version.
|
|
||||||
{
|
|
||||||
&MsgIBDBlock{MsgBlock: &blockOne},
|
|
||||||
&MsgIBDBlock{MsgBlock: &blockOne},
|
|
||||||
blockOneBytes,
|
|
||||||
blockOneTxLocs,
|
|
||||||
ProtocolVersion,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Logf("Running %d tests", len(tests))
|
|
||||||
for i, test := range tests {
|
|
||||||
// Encode the message to appmessage format.
|
|
||||||
var buf bytes.Buffer
|
|
||||||
err := test.in.KaspaEncode(&buf, test.pver)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("KaspaEncode #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !bytes.Equal(buf.Bytes(), test.buf) {
|
|
||||||
t.Errorf("KaspaEncode #%d\n got: %s want: %s", i,
|
|
||||||
spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Decode the message from appmessage format.
|
|
||||||
var msg MsgIBDBlock
|
|
||||||
msg.MsgBlock = new(MsgBlock)
|
|
||||||
rbuf := bytes.NewReader(test.buf)
|
|
||||||
err = msg.KaspaDecode(rbuf, test.pver)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("KaspaDecode #%d error %v", i, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !reflect.DeepEqual(&msg, test.out) {
|
|
||||||
t.Errorf("KaspaDecode #%d\n got: %s want: %s", i,
|
|
||||||
spew.Sdump(&msg), spew.Sdump(test.out))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
27
app/appmessage/p2p_msgibdblocklocator.go
Normal file
27
app/appmessage/p2p_msgibdblocklocator.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MsgIBDBlockLocator represents a kaspa ibdBlockLocator message
|
||||||
|
type MsgIBDBlockLocator struct {
|
||||||
|
baseMessage
|
||||||
|
TargetHash *externalapi.DomainHash
|
||||||
|
BlockLocatorHashes []*externalapi.DomainHash
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *MsgIBDBlockLocator) Command() MessageCommand {
|
||||||
|
return CmdIBDBlockLocator
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMsgIBDBlockLocator returns a new kaspa ibdBlockLocator message
|
||||||
|
func NewMsgIBDBlockLocator(targetHash *externalapi.DomainHash,
|
||||||
|
blockLocatorHashes []*externalapi.DomainHash) *MsgIBDBlockLocator {
|
||||||
|
|
||||||
|
return &MsgIBDBlockLocator{
|
||||||
|
TargetHash: targetHash,
|
||||||
|
BlockLocatorHashes: blockLocatorHashes,
|
||||||
|
}
|
||||||
|
}
|
||||||
23
app/appmessage/p2p_msgibdblocklocatorhighesthash.go
Normal file
23
app/appmessage/p2p_msgibdblocklocatorhighesthash.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MsgIBDBlockLocatorHighestHash represents a kaspa BlockLocatorHighestHash message
|
||||||
|
type MsgIBDBlockLocatorHighestHash struct {
|
||||||
|
baseMessage
|
||||||
|
HighestHash *externalapi.DomainHash
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *MsgIBDBlockLocatorHighestHash) Command() MessageCommand {
|
||||||
|
return CmdIBDBlockLocatorHighestHash
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMsgIBDBlockLocatorHighestHash returns a new BlockLocatorHighestHash message
|
||||||
|
func NewMsgIBDBlockLocatorHighestHash(highestHash *externalapi.DomainHash) *MsgIBDBlockLocatorHighestHash {
|
||||||
|
return &MsgIBDBlockLocatorHighestHash{
|
||||||
|
HighestHash: highestHash,
|
||||||
|
}
|
||||||
|
}
|
||||||
16
app/appmessage/p2p_msgibdblocklocatorhighesthashnotfound.go
Normal file
16
app/appmessage/p2p_msgibdblocklocatorhighesthashnotfound.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// MsgIBDBlockLocatorHighestHashNotFound represents a kaspa BlockLocatorHighestHashNotFound message
|
||||||
|
type MsgIBDBlockLocatorHighestHashNotFound struct {
|
||||||
|
baseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *MsgIBDBlockLocatorHighestHashNotFound) Command() MessageCommand {
|
||||||
|
return CmdIBDBlockLocatorHighestHashNotFound
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMsgIBDBlockLocatorHighestHashNotFound returns a new IBDBlockLocatorHighestHashNotFound message
|
||||||
|
func NewMsgIBDBlockLocatorHighestHashNotFound() *MsgIBDBlockLocatorHighestHashNotFound {
|
||||||
|
return &MsgIBDBlockLocatorHighestHashNotFound{}
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package appmessage
|
package appmessage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MsgInvRelayBlock implements the Message interface and represents a kaspa
|
// MsgInvRelayBlock implements the Message interface and represents a kaspa
|
||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
// by sending their hash, and let the receiving node decide if it needs it.
|
// by sending their hash, and let the receiving node decide if it needs it.
|
||||||
type MsgInvRelayBlock struct {
|
type MsgInvRelayBlock struct {
|
||||||
baseMessage
|
baseMessage
|
||||||
Hash *daghash.Hash
|
Hash *externalapi.DomainHash
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
// Command returns the protocol command string for the message. This is part
|
||||||
@@ -20,7 +20,7 @@ func (msg *MsgInvRelayBlock) Command() MessageCommand {
|
|||||||
|
|
||||||
// NewMsgInvBlock returns a new kaspa invrelblk message that conforms to
|
// NewMsgInvBlock returns a new kaspa invrelblk message that conforms to
|
||||||
// the Message interface. See MsgInvRelayBlock for details.
|
// the Message interface. See MsgInvRelayBlock for details.
|
||||||
func NewMsgInvBlock(hash *daghash.Hash) *MsgInvRelayBlock {
|
func NewMsgInvBlock(hash *externalapi.DomainHash) *MsgInvRelayBlock {
|
||||||
return &MsgInvRelayBlock{
|
return &MsgInvRelayBlock{
|
||||||
Hash: hash,
|
Hash: hash,
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package appmessage
|
package appmessage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MaxInvPerTxInvMsg is the maximum number of hashes that can
|
// MaxInvPerTxInvMsg is the maximum number of hashes that can
|
||||||
@@ -13,7 +13,7 @@ const MaxInvPerTxInvMsg = MaxInvPerMsg
|
|||||||
// by sending their ID, and let the receiving node decide if it needs it.
|
// by sending their ID, and let the receiving node decide if it needs it.
|
||||||
type MsgInvTransaction struct {
|
type MsgInvTransaction struct {
|
||||||
baseMessage
|
baseMessage
|
||||||
TxIDs []*daghash.TxID
|
TxIDs []*externalapi.DomainTransactionID
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
// Command returns the protocol command string for the message. This is part
|
||||||
@@ -24,7 +24,7 @@ func (msg *MsgInvTransaction) Command() MessageCommand {
|
|||||||
|
|
||||||
// NewMsgInvTransaction returns a new kaspa TxInv message that conforms to
|
// NewMsgInvTransaction returns a new kaspa TxInv message that conforms to
|
||||||
// the Message interface. See MsgInvTransaction for details.
|
// the Message interface. See MsgInvTransaction for details.
|
||||||
func NewMsgInvTransaction(ids []*daghash.TxID) *MsgInvTransaction {
|
func NewMsgInvTransaction(ids []*externalapi.DomainTransactionID) *MsgInvTransaction {
|
||||||
return &MsgInvTransaction{
|
return &MsgInvTransaction{
|
||||||
TxIDs: ids,
|
TxIDs: ids,
|
||||||
}
|
}
|
||||||
@@ -6,17 +6,12 @@ package appmessage
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/kaspanet/kaspad/util/random"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestPing tests the MsgPing API against the latest protocol version.
|
// TestPing tests the MsgPing API against the latest protocol version.
|
||||||
func TestPing(t *testing.T) {
|
func TestPing(t *testing.T) {
|
||||||
// Ensure we get the same nonce back out.
|
// Ensure we get the same nonce back out.
|
||||||
nonce, err := random.Uint64()
|
nonce := uint64(0x61c2c5535902862)
|
||||||
if err != nil {
|
|
||||||
t.Errorf("random.Uint64: Error generating nonce: %v", err)
|
|
||||||
}
|
|
||||||
msg := NewMsgPing(nonce)
|
msg := NewMsgPing(nonce)
|
||||||
if msg.Nonce != nonce {
|
if msg.Nonce != nonce {
|
||||||
t.Errorf("NewMsgPing: wrong nonce - got %v, want %v",
|
t.Errorf("NewMsgPing: wrong nonce - got %v, want %v",
|
||||||
@@ -6,16 +6,11 @@ package appmessage
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/kaspanet/kaspad/util/random"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestPongLatest tests the MsgPong API against the latest protocol version.
|
// TestPongLatest tests the MsgPong API against the latest protocol version.
|
||||||
func TestPongLatest(t *testing.T) {
|
func TestPongLatest(t *testing.T) {
|
||||||
nonce, err := random.Uint64()
|
nonce := uint64(0x1a05b581a5182c)
|
||||||
if err != nil {
|
|
||||||
t.Errorf("random.Uint64: error generating nonce: %v", err)
|
|
||||||
}
|
|
||||||
msg := NewMsgPong(nonce)
|
msg := NewMsgPong(nonce)
|
||||||
if msg.Nonce != nonce {
|
if msg.Nonce != nonce {
|
||||||
t.Errorf("NewMsgPong: wrong nonce - got %v, want %v",
|
t.Errorf("NewMsgPong: wrong nonce - got %v, want %v",
|
||||||
23
app/appmessage/p2p_msgpruningpointhash.go
Normal file
23
app/appmessage/p2p_msgpruningpointhash.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MsgPruningPointHashMessage represents a kaspa PruningPointHash message
|
||||||
|
type MsgPruningPointHashMessage struct {
|
||||||
|
baseMessage
|
||||||
|
Hash *externalapi.DomainHash
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *MsgPruningPointHashMessage) Command() MessageCommand {
|
||||||
|
return CmdPruningPointHash
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewPruningPointHashMessage returns a new kaspa PruningPointHash message
|
||||||
|
func NewPruningPointHashMessage(hash *externalapi.DomainHash) *MsgPruningPointHashMessage {
|
||||||
|
return &MsgPruningPointHashMessage{
|
||||||
|
Hash: hash,
|
||||||
|
}
|
||||||
|
}
|
||||||
36
app/appmessage/p2p_msgpruningpointutxosetchunk.go
Normal file
36
app/appmessage/p2p_msgpruningpointutxosetchunk.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
import "github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
|
|
||||||
|
// MsgPruningPointUTXOSetChunk represents a kaspa PruningPointUTXOSetChunk message
|
||||||
|
type MsgPruningPointUTXOSetChunk struct {
|
||||||
|
baseMessage
|
||||||
|
OutpointAndUTXOEntryPairs []*OutpointAndUTXOEntryPair
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *MsgPruningPointUTXOSetChunk) Command() MessageCommand {
|
||||||
|
return CmdPruningPointUTXOSetChunk
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMsgPruningPointUTXOSetChunk returns a new MsgPruningPointUTXOSetChunk.
|
||||||
|
func NewMsgPruningPointUTXOSetChunk(outpointAndUTXOEntryPairs []*OutpointAndUTXOEntryPair) *MsgPruningPointUTXOSetChunk {
|
||||||
|
return &MsgPruningPointUTXOSetChunk{
|
||||||
|
OutpointAndUTXOEntryPairs: outpointAndUTXOEntryPairs,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// OutpointAndUTXOEntryPair is an outpoint along with its
|
||||||
|
// respective UTXO entry
|
||||||
|
type OutpointAndUTXOEntryPair struct {
|
||||||
|
Outpoint *Outpoint
|
||||||
|
UTXOEntry *UTXOEntry
|
||||||
|
}
|
||||||
|
|
||||||
|
// UTXOEntry houses details about an individual transaction output in a UTXO
|
||||||
|
type UTXOEntry struct {
|
||||||
|
Amount uint64
|
||||||
|
ScriptPublicKey *externalapi.ScriptPublicKey
|
||||||
|
BlockDAAScore uint64
|
||||||
|
IsCoinbase bool
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@
|
|||||||
package appmessage
|
package appmessage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kaspanet/kaspad/util/subnetworkid"
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MsgRequestAddresses implements the Message interface and represents a kaspa
|
// MsgRequestAddresses implements the Message interface and represents a kaspa
|
||||||
@@ -17,7 +17,7 @@ import (
|
|||||||
type MsgRequestAddresses struct {
|
type MsgRequestAddresses struct {
|
||||||
baseMessage
|
baseMessage
|
||||||
IncludeAllSubnetworks bool
|
IncludeAllSubnetworks bool
|
||||||
SubnetworkID *subnetworkid.SubnetworkID
|
SubnetworkID *externalapi.DomainSubnetworkID
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
// Command returns the protocol command string for the message. This is part
|
||||||
@@ -28,7 +28,7 @@ func (msg *MsgRequestAddresses) Command() MessageCommand {
|
|||||||
|
|
||||||
// NewMsgRequestAddresses returns a new kaspa RequestAddresses message that conforms to the
|
// NewMsgRequestAddresses returns a new kaspa RequestAddresses message that conforms to the
|
||||||
// Message interface. See MsgRequestAddresses for details.
|
// Message interface. See MsgRequestAddresses for details.
|
||||||
func NewMsgRequestAddresses(includeAllSubnetworks bool, subnetworkID *subnetworkid.SubnetworkID) *MsgRequestAddresses {
|
func NewMsgRequestAddresses(includeAllSubnetworks bool, subnetworkID *externalapi.DomainSubnetworkID) *MsgRequestAddresses {
|
||||||
return &MsgRequestAddresses{
|
return &MsgRequestAddresses{
|
||||||
IncludeAllSubnetworks: includeAllSubnetworks,
|
IncludeAllSubnetworks: includeAllSubnetworks,
|
||||||
SubnetworkID: subnetworkID,
|
SubnetworkID: subnetworkID,
|
||||||
@@ -1,17 +1,18 @@
|
|||||||
package appmessage
|
package appmessage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MsgRequestBlockLocator implements the Message interface and represents a kaspa
|
// MsgRequestBlockLocator implements the Message interface and represents a kaspa
|
||||||
// RequestBlockLocator message. It is used to request a block locator between high
|
// RequestBlockLocator message. It is used to request a block locator between low
|
||||||
// and low hash.
|
// and high hash.
|
||||||
// The locator is returned via a locator message (MsgBlockLocator).
|
// The locator is returned via a locator message (MsgBlockLocator).
|
||||||
type MsgRequestBlockLocator struct {
|
type MsgRequestBlockLocator struct {
|
||||||
baseMessage
|
baseMessage
|
||||||
HighHash *daghash.Hash
|
LowHash *externalapi.DomainHash
|
||||||
LowHash *daghash.Hash
|
HighHash *externalapi.DomainHash
|
||||||
|
Limit uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
// Command returns the protocol command string for the message. This is part
|
||||||
@@ -23,9 +24,10 @@ func (msg *MsgRequestBlockLocator) Command() MessageCommand {
|
|||||||
// NewMsgRequestBlockLocator returns a new RequestBlockLocator message that conforms to the
|
// NewMsgRequestBlockLocator returns a new RequestBlockLocator message that conforms to the
|
||||||
// Message interface using the passed parameters and defaults for the remaining
|
// Message interface using the passed parameters and defaults for the remaining
|
||||||
// fields.
|
// fields.
|
||||||
func NewMsgRequestBlockLocator(highHash, lowHash *daghash.Hash) *MsgRequestBlockLocator {
|
func NewMsgRequestBlockLocator(lowHash, highHash *externalapi.DomainHash, limit uint32) *MsgRequestBlockLocator {
|
||||||
return &MsgRequestBlockLocator{
|
return &MsgRequestBlockLocator{
|
||||||
HighHash: highHash,
|
|
||||||
LowHash: lowHash,
|
LowHash: lowHash,
|
||||||
|
HighHash: highHash,
|
||||||
|
Limit: limit,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,20 +3,20 @@ package appmessage
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestRequestBlockLocator tests the MsgRequestBlockLocator API.
|
// TestRequestBlockLocator tests the MsgRequestBlockLocator API.
|
||||||
func TestRequestBlockLocator(t *testing.T) {
|
func TestRequestBlockLocator(t *testing.T) {
|
||||||
hashStr := "000000000002e7ad7b9eef9479e4aabc65cb831269cc20d2632c13684406dee0"
|
hashStr := "000000000002e7ad7b9eef9479e4aabc65cb831269cc20d2632c13684406dee0"
|
||||||
highHash, err := daghash.NewHashFromStr(hashStr)
|
highHash, err := externalapi.NewDomainHashFromString(hashStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("NewHashFromStr: %v", err)
|
t.Errorf("NewHashFromStr: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure the command is expected value.
|
// Ensure the command is expected value.
|
||||||
wantCmd := MessageCommand(9)
|
wantCmd := MessageCommand(9)
|
||||||
msg := NewMsgRequestBlockLocator(highHash, &daghash.ZeroHash)
|
msg := NewMsgRequestBlockLocator(highHash, &externalapi.DomainHash{}, 0)
|
||||||
if cmd := msg.Command(); cmd != wantCmd {
|
if cmd := msg.Command(); cmd != wantCmd {
|
||||||
t.Errorf("NewMsgRequestBlockLocator: wrong command - got %v want %v",
|
t.Errorf("NewMsgRequestBlockLocator: wrong command - got %v want %v",
|
||||||
cmd, wantCmd)
|
cmd, wantCmd)
|
||||||
34
app/appmessage/p2p_msgrequestheaders.go
Normal file
34
app/appmessage/p2p_msgrequestheaders.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
// Copyright (c) 2013-2016 The btcsuite developers
|
||||||
|
// Use of this source code is governed by an ISC
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package appmessage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MsgRequestHeaders implements the Message interface and represents a kaspa
|
||||||
|
// RequestHeaders message. It is used to request a list of blocks starting after the
|
||||||
|
// low hash and until the high hash.
|
||||||
|
type MsgRequestHeaders struct {
|
||||||
|
baseMessage
|
||||||
|
LowHash *externalapi.DomainHash
|
||||||
|
HighHash *externalapi.DomainHash
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message. This is part
|
||||||
|
// of the Message interface implementation.
|
||||||
|
func (msg *MsgRequestHeaders) Command() MessageCommand {
|
||||||
|
return CmdRequestHeaders
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMsgRequstHeaders returns a new kaspa RequestHeaders message that conforms to the
|
||||||
|
// Message interface using the passed parameters and defaults for the remaining
|
||||||
|
// fields.
|
||||||
|
func NewMsgRequstHeaders(lowHash, highHash *externalapi.DomainHash) *MsgRequestHeaders {
|
||||||
|
return &MsgRequestHeaders{
|
||||||
|
LowHash: lowHash,
|
||||||
|
HighHash: highHash,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,34 +7,34 @@ package appmessage
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestRequstIBDBlocks tests the MsgRequestIBDBlocks API.
|
// TestRequstIBDBlocks tests the MsgRequestHeaders API.
|
||||||
func TestRequstIBDBlocks(t *testing.T) {
|
func TestRequstIBDBlocks(t *testing.T) {
|
||||||
hashStr := "000000000002e7ad7b9eef9479e4aabc65cb831269cc20d2632c13684406dee0"
|
hashStr := "000000000002e7ad7b9eef9479e4aabc65cb831269cc20d2632c13684406dee0"
|
||||||
lowHash, err := daghash.NewHashFromStr(hashStr)
|
lowHash, err := externalapi.NewDomainHashFromString(hashStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("NewHashFromStr: %v", err)
|
t.Errorf("NewHashFromStr: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
hashStr = "3ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
|
hashStr = "000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
|
||||||
highHash, err := daghash.NewHashFromStr(hashStr)
|
highHash, err := externalapi.NewDomainHashFromString(hashStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("NewHashFromStr: %v", err)
|
t.Errorf("NewHashFromStr: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure we get the same data back out.
|
// Ensure we get the same data back out.
|
||||||
msg := NewMsgRequstIBDBlocks(lowHash, highHash)
|
msg := NewMsgRequstHeaders(lowHash, highHash)
|
||||||
if !msg.HighHash.IsEqual(highHash) {
|
if !msg.HighHash.Equal(highHash) {
|
||||||
t.Errorf("NewMsgRequstIBDBlocks: wrong high hash - got %v, want %v",
|
t.Errorf("NewMsgRequstHeaders: wrong high hash - got %v, want %v",
|
||||||
msg.HighHash, highHash)
|
msg.HighHash, highHash)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure the command is expected value.
|
// Ensure the command is expected value.
|
||||||
wantCmd := MessageCommand(4)
|
wantCmd := MessageCommand(4)
|
||||||
if cmd := msg.Command(); cmd != wantCmd {
|
if cmd := msg.Command(); cmd != wantCmd {
|
||||||
t.Errorf("NewMsgRequstIBDBlocks: wrong command - got %v want %v",
|
t.Errorf("NewMsgRequstHeaders: wrong command - got %v want %v",
|
||||||
cmd, wantCmd)
|
cmd, wantCmd)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
26
app/appmessage/p2p_msgrequestibdblocks.go
Normal file
26
app/appmessage/p2p_msgrequestibdblocks.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MsgRequestIBDBlocks implements the Message interface and represents a kaspa
|
||||||
|
// RequestIBDBlocks message. It is used to request blocks as part of the IBD
|
||||||
|
// protocol.
|
||||||
|
type MsgRequestIBDBlocks struct {
|
||||||
|
baseMessage
|
||||||
|
Hashes []*externalapi.DomainHash
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message. This is part
|
||||||
|
// of the Message interface implementation.
|
||||||
|
func (msg *MsgRequestIBDBlocks) Command() MessageCommand {
|
||||||
|
return CmdRequestIBDBlocks
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMsgRequestIBDBlocks returns a new MsgRequestIBDBlocks.
|
||||||
|
func NewMsgRequestIBDBlocks(hashes []*externalapi.DomainHash) *MsgRequestIBDBlocks {
|
||||||
|
return &MsgRequestIBDBlocks{
|
||||||
|
Hashes: hashes,
|
||||||
|
}
|
||||||
|
}
|
||||||
22
app/appmessage/p2p_msgrequestnextheaders.go
Normal file
22
app/appmessage/p2p_msgrequestnextheaders.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// MsgRequestNextHeaders implements the Message interface and represents a kaspa
|
||||||
|
// RequestNextHeaders message. It is used to notify the IBD syncer peer to send
|
||||||
|
// more headers.
|
||||||
|
//
|
||||||
|
// This message has no payload.
|
||||||
|
type MsgRequestNextHeaders struct {
|
||||||
|
baseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message. This is part
|
||||||
|
// of the Message interface implementation.
|
||||||
|
func (msg *MsgRequestNextHeaders) Command() MessageCommand {
|
||||||
|
return CmdRequestNextHeaders
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMsgRequestNextHeaders returns a new kaspa RequestNextHeaders message that conforms to the
|
||||||
|
// Message interface.
|
||||||
|
func NewMsgRequestNextHeaders() *MsgRequestNextHeaders {
|
||||||
|
return &MsgRequestNextHeaders{}
|
||||||
|
}
|
||||||
16
app/appmessage/p2p_msgrequestnextpruningpointutxosetchunk.go
Normal file
16
app/appmessage/p2p_msgrequestnextpruningpointutxosetchunk.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// MsgRequestNextPruningPointUTXOSetChunk represents a kaspa RequestNextPruningPointUTXOSetChunk message
|
||||||
|
type MsgRequestNextPruningPointUTXOSetChunk struct {
|
||||||
|
baseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *MsgRequestNextPruningPointUTXOSetChunk) Command() MessageCommand {
|
||||||
|
return CmdRequestNextPruningPointUTXOSetChunk
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMsgRequestNextPruningPointUTXOSetChunk returns a new MsgRequestNextPruningPointUTXOSetChunk.
|
||||||
|
func NewMsgRequestNextPruningPointUTXOSetChunk() *MsgRequestNextPruningPointUTXOSetChunk {
|
||||||
|
return &MsgRequestNextPruningPointUTXOSetChunk{}
|
||||||
|
}
|
||||||
23
app/appmessage/p2p_msgrequestpruningpointutxosetandblock.go
Normal file
23
app/appmessage/p2p_msgrequestpruningpointutxosetandblock.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MsgRequestPruningPointUTXOSetAndBlock represents a kaspa RequestPruningPointUTXOSetAndBlock message
|
||||||
|
type MsgRequestPruningPointUTXOSetAndBlock struct {
|
||||||
|
baseMessage
|
||||||
|
PruningPointHash *externalapi.DomainHash
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *MsgRequestPruningPointUTXOSetAndBlock) Command() MessageCommand {
|
||||||
|
return CmdRequestPruningPointUTXOSetAndBlock
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMsgRequestPruningPointUTXOSetAndBlock returns a new MsgRequestPruningPointUTXOSetAndBlock
|
||||||
|
func NewMsgRequestPruningPointUTXOSetAndBlock(pruningPointHash *externalapi.DomainHash) *MsgRequestPruningPointUTXOSetAndBlock {
|
||||||
|
return &MsgRequestPruningPointUTXOSetAndBlock{
|
||||||
|
PruningPointHash: pruningPointHash,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,19 +1,19 @@
|
|||||||
package appmessage
|
package appmessage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MsgRequestRelayBlocksHashes is the maximum number of hashes that can
|
// MaxRequestRelayBlocksHashes is the maximum number of hashes that can
|
||||||
// be in a single RequestRelayBlocks message.
|
// be in a single RequestRelayBlocks message.
|
||||||
const MsgRequestRelayBlocksHashes = MaxInvPerMsg
|
const MaxRequestRelayBlocksHashes = MaxInvPerMsg
|
||||||
|
|
||||||
// MsgRequestRelayBlocks implements the Message interface and represents a kaspa
|
// MsgRequestRelayBlocks implements the Message interface and represents a kaspa
|
||||||
// RequestRelayBlocks message. It is used to request blocks as part of the block
|
// RequestRelayBlocks message. It is used to request blocks as part of the block
|
||||||
// relay protocol.
|
// relay protocol.
|
||||||
type MsgRequestRelayBlocks struct {
|
type MsgRequestRelayBlocks struct {
|
||||||
baseMessage
|
baseMessage
|
||||||
Hashes []*daghash.Hash
|
Hashes []*externalapi.DomainHash
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
// Command returns the protocol command string for the message. This is part
|
||||||
@@ -24,7 +24,7 @@ func (msg *MsgRequestRelayBlocks) Command() MessageCommand {
|
|||||||
|
|
||||||
// NewMsgRequestRelayBlocks returns a new kaspa RequestRelayBlocks message that conforms to
|
// NewMsgRequestRelayBlocks returns a new kaspa RequestRelayBlocks message that conforms to
|
||||||
// the Message interface. See MsgRequestRelayBlocks for details.
|
// the Message interface. See MsgRequestRelayBlocks for details.
|
||||||
func NewMsgRequestRelayBlocks(hashes []*daghash.Hash) *MsgRequestRelayBlocks {
|
func NewMsgRequestRelayBlocks(hashes []*externalapi.DomainHash) *MsgRequestRelayBlocks {
|
||||||
return &MsgRequestRelayBlocks{
|
return &MsgRequestRelayBlocks{
|
||||||
Hashes: hashes,
|
Hashes: hashes,
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package appmessage
|
package appmessage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MaxInvPerRequestTransactionsMsg is the maximum number of hashes that can
|
// MaxInvPerRequestTransactionsMsg is the maximum number of hashes that can
|
||||||
@@ -13,7 +13,7 @@ const MaxInvPerRequestTransactionsMsg = MaxInvPerMsg
|
|||||||
// transactions relay protocol.
|
// transactions relay protocol.
|
||||||
type MsgRequestTransactions struct {
|
type MsgRequestTransactions struct {
|
||||||
baseMessage
|
baseMessage
|
||||||
IDs []*daghash.TxID
|
IDs []*externalapi.DomainTransactionID
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
// Command returns the protocol command string for the message. This is part
|
||||||
@@ -24,7 +24,7 @@ func (msg *MsgRequestTransactions) Command() MessageCommand {
|
|||||||
|
|
||||||
// NewMsgRequestTransactions returns a new kaspa RequestTransactions message that conforms to
|
// NewMsgRequestTransactions returns a new kaspa RequestTransactions message that conforms to
|
||||||
// the Message interface. See MsgRequestTransactions for details.
|
// the Message interface. See MsgRequestTransactions for details.
|
||||||
func NewMsgRequestTransactions(ids []*daghash.TxID) *MsgRequestTransactions {
|
func NewMsgRequestTransactions(ids []*externalapi.DomainTransactionID) *MsgRequestTransactions {
|
||||||
return &MsgRequestTransactions{
|
return &MsgRequestTransactions{
|
||||||
IDs: ids,
|
IDs: ids,
|
||||||
}
|
}
|
||||||
@@ -5,14 +5,14 @@
|
|||||||
package appmessage
|
package appmessage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
)
|
)
|
||||||
|
|
||||||
// MsgTransactionNotFound defines a kaspa TransactionNotFound message which is sent in response to
|
// MsgTransactionNotFound defines a kaspa TransactionNotFound message which is sent in response to
|
||||||
// a RequestTransactions message if any of the requested data in not available on the peer.
|
// a RequestTransactions message if any of the requested data in not available on the peer.
|
||||||
type MsgTransactionNotFound struct {
|
type MsgTransactionNotFound struct {
|
||||||
baseMessage
|
baseMessage
|
||||||
ID *daghash.TxID
|
ID *externalapi.DomainTransactionID
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command returns the protocol command string for the message. This is part
|
// Command returns the protocol command string for the message. This is part
|
||||||
@@ -23,7 +23,7 @@ func (msg *MsgTransactionNotFound) Command() MessageCommand {
|
|||||||
|
|
||||||
// NewMsgTransactionNotFound returns a new kaspa transactionsnotfound message that conforms to the
|
// NewMsgTransactionNotFound returns a new kaspa transactionsnotfound message that conforms to the
|
||||||
// Message interface. See MsgTransactionNotFound for details.
|
// Message interface. See MsgTransactionNotFound for details.
|
||||||
func NewMsgTransactionNotFound(id *daghash.TxID) *MsgTransactionNotFound {
|
func NewMsgTransactionNotFound(id *externalapi.DomainTransactionID) *MsgTransactionNotFound {
|
||||||
return &MsgTransactionNotFound{
|
return &MsgTransactionNotFound{
|
||||||
ID: id,
|
ID: id,
|
||||||
}
|
}
|
||||||
316
app/appmessage/p2p_msgtx.go
Normal file
316
app/appmessage/p2p_msgtx.go
Normal file
@@ -0,0 +1,316 @@
|
|||||||
|
// Copyright (c) 2013-2016 The btcsuite developers
|
||||||
|
// Use of this source code is governed by an ISC
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package appmessage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
|
||||||
|
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/utils/subnetworks"
|
||||||
|
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// MaxPrevOutIndex is the maximum index the index field of a previous
|
||||||
|
// outpoint can be.
|
||||||
|
MaxPrevOutIndex uint32 = 0xffffffff
|
||||||
|
|
||||||
|
// defaultTxInOutAlloc is the default size used for the backing array for
|
||||||
|
// transaction inputs and outputs. The array will dynamically grow as needed,
|
||||||
|
// but this figure is intended to provide enough space for the number of
|
||||||
|
// inputs and outputs in a typical transaction without needing to grow the
|
||||||
|
// backing array multiple times.
|
||||||
|
defaultTxInOutAlloc = 15
|
||||||
|
|
||||||
|
// minTxInPayload is the minimum payload size for a transaction input.
|
||||||
|
// PreviousOutpoint.TxID + PreviousOutpoint.Index 4 bytes + Varint for
|
||||||
|
// SignatureScript length 1 byte + Sequence 4 bytes.
|
||||||
|
minTxInPayload = 9 + externalapi.DomainHashSize
|
||||||
|
|
||||||
|
// maxTxInPerMessage is the maximum number of transactions inputs that
|
||||||
|
// a transaction which fits into a message could possibly have.
|
||||||
|
maxTxInPerMessage = (MaxMessagePayload / minTxInPayload) + 1
|
||||||
|
|
||||||
|
// MinTxOutPayload is the minimum payload size for a transaction output.
|
||||||
|
// Value 8 bytes + version 2 bytes + Varint for ScriptPublicKey length 1 byte.
|
||||||
|
MinTxOutPayload = 11
|
||||||
|
|
||||||
|
// maxTxOutPerMessage is the maximum number of transactions outputs that
|
||||||
|
// a transaction which fits into a message could possibly have.
|
||||||
|
maxTxOutPerMessage = (MaxMessagePayload / MinTxOutPayload) + 1
|
||||||
|
|
||||||
|
// minTxPayload is the minimum payload size for a transaction. Note
|
||||||
|
// that any realistically usable transaction must have at least one
|
||||||
|
// input or output, but that is a rule enforced at a higher layer, so
|
||||||
|
// it is intentionally not included here.
|
||||||
|
// Version 4 bytes + Varint number of transaction inputs 1 byte + Varint
|
||||||
|
// number of transaction outputs 1 byte + LockTime 4 bytes + min input
|
||||||
|
// payload + min output payload.
|
||||||
|
minTxPayload = 10
|
||||||
|
)
|
||||||
|
|
||||||
|
// Outpoint defines a kaspa data type that is used to track previous
|
||||||
|
// transaction outputs.
|
||||||
|
type Outpoint struct {
|
||||||
|
TxID externalapi.DomainTransactionID
|
||||||
|
Index uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewOutpoint returns a new kaspa transaction outpoint point with the
|
||||||
|
// provided hash and index.
|
||||||
|
func NewOutpoint(txID *externalapi.DomainTransactionID, index uint32) *Outpoint {
|
||||||
|
return &Outpoint{
|
||||||
|
TxID: *txID,
|
||||||
|
Index: index,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// String returns the Outpoint in the human-readable form "txID:index".
|
||||||
|
func (o Outpoint) String() string {
|
||||||
|
// Allocate enough for ID string, colon, and 10 digits. Although
|
||||||
|
// at the time of writing, the number of digits can be no greater than
|
||||||
|
// the length of the decimal representation of maxTxOutPerMessage, the
|
||||||
|
// maximum message payload may increase in the future and this
|
||||||
|
// optimization may go unnoticed, so allocate space for 10 decimal
|
||||||
|
// digits, which will fit any uint32.
|
||||||
|
buf := make([]byte, 2*externalapi.DomainHashSize+1, 2*externalapi.DomainHashSize+1+10)
|
||||||
|
copy(buf, o.TxID.String())
|
||||||
|
buf[2*externalapi.DomainHashSize] = ':'
|
||||||
|
buf = strconv.AppendUint(buf, uint64(o.Index), 10)
|
||||||
|
return string(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TxIn defines a kaspa transaction input.
|
||||||
|
type TxIn struct {
|
||||||
|
PreviousOutpoint Outpoint
|
||||||
|
SignatureScript []byte
|
||||||
|
Sequence uint64
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewTxIn returns a new kaspa transaction input with the provided
|
||||||
|
// previous outpoint point and signature script with a default sequence of
|
||||||
|
// MaxTxInSequenceNum.
|
||||||
|
func NewTxIn(prevOut *Outpoint, signatureScript []byte, sequence uint64) *TxIn {
|
||||||
|
return &TxIn{
|
||||||
|
PreviousOutpoint: *prevOut,
|
||||||
|
SignatureScript: signatureScript,
|
||||||
|
Sequence: sequence,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TxOut defines a kaspa transaction output.
|
||||||
|
type TxOut struct {
|
||||||
|
Value uint64
|
||||||
|
ScriptPubKey *externalapi.ScriptPublicKey
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewTxOut returns a new kaspa transaction output with the provided
|
||||||
|
// transaction value and public key script.
|
||||||
|
func NewTxOut(value uint64, scriptPubKey *externalapi.ScriptPublicKey) *TxOut {
|
||||||
|
return &TxOut{
|
||||||
|
Value: value,
|
||||||
|
ScriptPubKey: scriptPubKey,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MsgTx implements the Message interface and represents a kaspa tx message.
|
||||||
|
// It is used to deliver transaction information in response to a getdata
|
||||||
|
// message (MsgGetData) for a given transaction.
|
||||||
|
//
|
||||||
|
// Use the AddTxIn and AddTxOut functions to build up the list of transaction
|
||||||
|
// inputs and outputs.
|
||||||
|
type MsgTx struct {
|
||||||
|
baseMessage
|
||||||
|
Version uint16
|
||||||
|
TxIn []*TxIn
|
||||||
|
TxOut []*TxOut
|
||||||
|
LockTime uint64
|
||||||
|
SubnetworkID externalapi.DomainSubnetworkID
|
||||||
|
Gas uint64
|
||||||
|
Payload []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddTxIn adds a transaction input to the message.
|
||||||
|
func (msg *MsgTx) AddTxIn(ti *TxIn) {
|
||||||
|
msg.TxIn = append(msg.TxIn, ti)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddTxOut adds a transaction output to the message.
|
||||||
|
func (msg *MsgTx) AddTxOut(to *TxOut) {
|
||||||
|
msg.TxOut = append(msg.TxOut, to)
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsCoinBase determines whether or not a transaction is a coinbase transaction. A coinbase
|
||||||
|
// transaction is a special transaction created by miners that distributes fees and block subsidy
|
||||||
|
// to the previous blocks' miners, and to specify the scriptPubKey that will be used to pay the current
|
||||||
|
// miner in future blocks. Each input of the coinbase transaction should set index to maximum
|
||||||
|
// value and reference the relevant block id, instead of previous transaction id.
|
||||||
|
func (msg *MsgTx) IsCoinBase() bool {
|
||||||
|
// A coinbase transaction must have subnetwork id SubnetworkIDCoinbase
|
||||||
|
return msg.SubnetworkID == subnetworks.SubnetworkIDCoinbase
|
||||||
|
}
|
||||||
|
|
||||||
|
// TxHash generates the Hash for the transaction.
|
||||||
|
func (msg *MsgTx) TxHash() *externalapi.DomainHash {
|
||||||
|
return consensushashing.TransactionHash(MsgTxToDomainTransaction(msg))
|
||||||
|
}
|
||||||
|
|
||||||
|
// TxID generates the Hash for the transaction without the signature script, gas and payload fields.
|
||||||
|
func (msg *MsgTx) TxID() *externalapi.DomainTransactionID {
|
||||||
|
return consensushashing.TransactionID(MsgTxToDomainTransaction(msg))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy creates a deep copy of a transaction so that the original does not get
|
||||||
|
// modified when the copy is manipulated.
|
||||||
|
func (msg *MsgTx) Copy() *MsgTx {
|
||||||
|
// Create new tx and start by copying primitive values and making space
|
||||||
|
// for the transaction inputs and outputs.
|
||||||
|
newTx := MsgTx{
|
||||||
|
Version: msg.Version,
|
||||||
|
TxIn: make([]*TxIn, 0, len(msg.TxIn)),
|
||||||
|
TxOut: make([]*TxOut, 0, len(msg.TxOut)),
|
||||||
|
LockTime: msg.LockTime,
|
||||||
|
SubnetworkID: msg.SubnetworkID,
|
||||||
|
Gas: msg.Gas,
|
||||||
|
}
|
||||||
|
|
||||||
|
if msg.Payload != nil {
|
||||||
|
newTx.Payload = make([]byte, len(msg.Payload))
|
||||||
|
copy(newTx.Payload, msg.Payload)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deep copy the old TxIn data.
|
||||||
|
for _, oldTxIn := range msg.TxIn {
|
||||||
|
// Deep copy the old previous outpoint.
|
||||||
|
oldOutpoint := oldTxIn.PreviousOutpoint
|
||||||
|
newOutpoint := Outpoint{}
|
||||||
|
newOutpoint.TxID = oldOutpoint.TxID
|
||||||
|
newOutpoint.Index = oldOutpoint.Index
|
||||||
|
|
||||||
|
// Deep copy the old signature script.
|
||||||
|
var newScript []byte
|
||||||
|
oldScript := oldTxIn.SignatureScript
|
||||||
|
oldScriptLen := len(oldScript)
|
||||||
|
if oldScriptLen > 0 {
|
||||||
|
newScript = make([]byte, oldScriptLen)
|
||||||
|
copy(newScript, oldScript[:oldScriptLen])
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create new txIn with the deep copied data.
|
||||||
|
newTxIn := TxIn{
|
||||||
|
PreviousOutpoint: newOutpoint,
|
||||||
|
SignatureScript: newScript,
|
||||||
|
Sequence: oldTxIn.Sequence,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finally, append this fully copied txin.
|
||||||
|
newTx.TxIn = append(newTx.TxIn, &newTxIn)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deep copy the old TxOut data.
|
||||||
|
for _, oldTxOut := range msg.TxOut {
|
||||||
|
// Deep copy the old ScriptPublicKey
|
||||||
|
var newScript externalapi.ScriptPublicKey
|
||||||
|
oldScript := oldTxOut.ScriptPubKey
|
||||||
|
oldScriptLen := len(oldScript.Script)
|
||||||
|
if oldScriptLen > 0 {
|
||||||
|
newScript = externalapi.ScriptPublicKey{Script: make([]byte, oldScriptLen), Version: oldScript.Version}
|
||||||
|
copy(newScript.Script, oldScript.Script[:oldScriptLen])
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create new txOut with the deep copied data and append it to
|
||||||
|
// new Tx.
|
||||||
|
newTxOut := TxOut{
|
||||||
|
Value: oldTxOut.Value,
|
||||||
|
ScriptPubKey: &newScript,
|
||||||
|
}
|
||||||
|
newTx.TxOut = append(newTx.TxOut, &newTxOut)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &newTx
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message. This is part
|
||||||
|
// of the Message interface implementation.
|
||||||
|
func (msg *MsgTx) Command() MessageCommand {
|
||||||
|
return CmdTx
|
||||||
|
}
|
||||||
|
|
||||||
|
// MaxPayloadLength returns the maximum length the payload can be for the
|
||||||
|
// receiver. This is part of the Message interface implementation.
|
||||||
|
func (msg *MsgTx) MaxPayloadLength(pver uint32) uint32 {
|
||||||
|
return MaxMessagePayload
|
||||||
|
}
|
||||||
|
|
||||||
|
// IsSubnetworkCompatible return true iff subnetworkID is one or more of the following:
|
||||||
|
// 1. The SupportsAll subnetwork (full node)
|
||||||
|
// 2. The native subnetwork
|
||||||
|
// 3. The transaction's subnetwork
|
||||||
|
func (msg *MsgTx) IsSubnetworkCompatible(subnetworkID *externalapi.DomainSubnetworkID) bool {
|
||||||
|
return subnetworkID == nil ||
|
||||||
|
subnetworkID.Equal(&subnetworks.SubnetworkIDNative) ||
|
||||||
|
subnetworkID.Equal(&msg.SubnetworkID)
|
||||||
|
}
|
||||||
|
|
||||||
|
// newMsgTx returns a new tx message that conforms to the Message interface.
|
||||||
|
//
|
||||||
|
// All fields except version and gas has default values if nil is passed:
|
||||||
|
// txIn, txOut - empty arrays
|
||||||
|
// payload - an empty payload
|
||||||
|
//
|
||||||
|
// The payload hash is calculated automatically according to provided payload.
|
||||||
|
// Also, the lock time is set to zero to indicate the transaction is valid
|
||||||
|
// immediately as opposed to some time in future.
|
||||||
|
func newMsgTx(version uint16, txIn []*TxIn, txOut []*TxOut, subnetworkID *externalapi.DomainSubnetworkID,
|
||||||
|
gas uint64, payload []byte, lockTime uint64) *MsgTx {
|
||||||
|
|
||||||
|
if txIn == nil {
|
||||||
|
txIn = make([]*TxIn, 0, defaultTxInOutAlloc)
|
||||||
|
}
|
||||||
|
|
||||||
|
if txOut == nil {
|
||||||
|
txOut = make([]*TxOut, 0, defaultTxInOutAlloc)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &MsgTx{
|
||||||
|
Version: version,
|
||||||
|
TxIn: txIn,
|
||||||
|
TxOut: txOut,
|
||||||
|
SubnetworkID: *subnetworkID,
|
||||||
|
Gas: gas,
|
||||||
|
Payload: payload,
|
||||||
|
LockTime: lockTime,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewNativeMsgTx returns a new tx message in the native subnetwork
|
||||||
|
func NewNativeMsgTx(version uint16, txIn []*TxIn, txOut []*TxOut) *MsgTx {
|
||||||
|
return newMsgTx(version, txIn, txOut, &subnetworks.SubnetworkIDNative, 0, nil, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewSubnetworkMsgTx returns a new tx message in the specified subnetwork with specified gas and payload
|
||||||
|
func NewSubnetworkMsgTx(version uint16, txIn []*TxIn, txOut []*TxOut, subnetworkID *externalapi.DomainSubnetworkID,
|
||||||
|
gas uint64, payload []byte) *MsgTx {
|
||||||
|
|
||||||
|
return newMsgTx(version, txIn, txOut, subnetworkID, gas, payload, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewNativeMsgTxWithLocktime returns a new tx message in the native subnetwork with a locktime.
|
||||||
|
//
|
||||||
|
// See newMsgTx for further documntation of the parameters
|
||||||
|
func NewNativeMsgTxWithLocktime(version uint16, txIn []*TxIn, txOut []*TxOut, locktime uint64) *MsgTx {
|
||||||
|
return newMsgTx(version, txIn, txOut, &subnetworks.SubnetworkIDNative, 0, nil, locktime)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewRegistryMsgTx creates a new MsgTx that registers a new subnetwork
|
||||||
|
func NewRegistryMsgTx(version uint16, txIn []*TxIn, txOut []*TxOut, gasLimit uint64) *MsgTx {
|
||||||
|
payload := make([]byte, 8)
|
||||||
|
binary.LittleEndian.PutUint64(payload, gasLimit)
|
||||||
|
|
||||||
|
return NewSubnetworkMsgTx(version, txIn, txOut, &subnetworks.SubnetworkIDRegistry, 0, payload)
|
||||||
|
}
|
||||||
261
app/appmessage/p2p_msgtx_test.go
Normal file
261
app/appmessage/p2p_msgtx_test.go
Normal file
@@ -0,0 +1,261 @@
|
|||||||
|
// Copyright (c) 2013-2016 The btcsuite developers
|
||||||
|
// Use of this source code is governed by an ISC
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package appmessage
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"math"
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/utils/constants"
|
||||||
|
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/utils/subnetworks"
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/utils/transactionid"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestTx tests the MsgTx API.
|
||||||
|
func TestTx(t *testing.T) {
|
||||||
|
pver := ProtocolVersion
|
||||||
|
|
||||||
|
txIDStr := "000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"
|
||||||
|
txID, err := transactionid.FromString(txIDStr)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("NewTxIDFromStr: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure the command is expected value.
|
||||||
|
wantCmd := MessageCommand(6)
|
||||||
|
msg := NewNativeMsgTx(1, nil, nil)
|
||||||
|
if cmd := msg.Command(); cmd != wantCmd {
|
||||||
|
t.Errorf("NewMsgAddresses: wrong command - got %v want %v",
|
||||||
|
cmd, wantCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure max payload is expected value for latest protocol version.
|
||||||
|
wantPayload := uint32(1024 * 1024 * 32)
|
||||||
|
maxPayload := msg.MaxPayloadLength(pver)
|
||||||
|
if maxPayload != wantPayload {
|
||||||
|
t.Errorf("MaxPayloadLength: wrong max payload length for "+
|
||||||
|
"protocol version %d - got %v, want %v", pver,
|
||||||
|
maxPayload, wantPayload)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure we get the same transaction outpoint data back out.
|
||||||
|
// NOTE: This is a block hash and made up index, but we're only
|
||||||
|
// testing package functionality.
|
||||||
|
prevOutIndex := uint32(1)
|
||||||
|
prevOut := NewOutpoint(txID, prevOutIndex)
|
||||||
|
if !prevOut.TxID.Equal(txID) {
|
||||||
|
t.Errorf("NewOutpoint: wrong ID - got %v, want %v",
|
||||||
|
spew.Sprint(&prevOut.TxID), spew.Sprint(txID))
|
||||||
|
}
|
||||||
|
if prevOut.Index != prevOutIndex {
|
||||||
|
t.Errorf("NewOutpoint: wrong index - got %v, want %v",
|
||||||
|
prevOut.Index, prevOutIndex)
|
||||||
|
}
|
||||||
|
prevOutStr := fmt.Sprintf("%s:%d", txID.String(), prevOutIndex)
|
||||||
|
if s := prevOut.String(); s != prevOutStr {
|
||||||
|
t.Errorf("Outpoint.String: unexpected result - got %v, "+
|
||||||
|
"want %v", s, prevOutStr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure we get the same transaction input back out.
|
||||||
|
sigScript := []byte{0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62}
|
||||||
|
txIn := NewTxIn(prevOut, sigScript, constants.MaxTxInSequenceNum)
|
||||||
|
if !reflect.DeepEqual(&txIn.PreviousOutpoint, prevOut) {
|
||||||
|
t.Errorf("NewTxIn: wrong prev outpoint - got %v, want %v",
|
||||||
|
spew.Sprint(&txIn.PreviousOutpoint),
|
||||||
|
spew.Sprint(prevOut))
|
||||||
|
}
|
||||||
|
if !bytes.Equal(txIn.SignatureScript, sigScript) {
|
||||||
|
t.Errorf("NewTxIn: wrong signature script - got %v, want %v",
|
||||||
|
spew.Sdump(txIn.SignatureScript),
|
||||||
|
spew.Sdump(sigScript))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure we get the same transaction output back out.
|
||||||
|
txValue := uint64(5000000000)
|
||||||
|
scriptPubKey := &externalapi.ScriptPublicKey{
|
||||||
|
Script: []byte{
|
||||||
|
0x41, // OP_DATA_65
|
||||||
|
0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
|
||||||
|
0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
|
||||||
|
0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
|
||||||
|
0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
|
||||||
|
0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
|
||||||
|
0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
|
||||||
|
0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
|
||||||
|
0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
|
||||||
|
0xa6, // 65-byte signature
|
||||||
|
0xac, // OP_CHECKSIG
|
||||||
|
},
|
||||||
|
Version: 0}
|
||||||
|
txOut := NewTxOut(txValue, scriptPubKey)
|
||||||
|
if txOut.Value != txValue {
|
||||||
|
t.Errorf("NewTxOut: wrong scriptPubKey - got %v, want %v",
|
||||||
|
txOut.Value, txValue)
|
||||||
|
|
||||||
|
}
|
||||||
|
if !bytes.Equal(txOut.ScriptPubKey.Script, scriptPubKey.Script) {
|
||||||
|
t.Errorf("NewTxOut: wrong scriptPubKey - got %v, want %v",
|
||||||
|
spew.Sdump(txOut.ScriptPubKey),
|
||||||
|
spew.Sdump(scriptPubKey))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure transaction inputs are added properly.
|
||||||
|
msg.AddTxIn(txIn)
|
||||||
|
if !reflect.DeepEqual(msg.TxIn[0], txIn) {
|
||||||
|
t.Errorf("AddTxIn: wrong transaction input added - got %v, want %v",
|
||||||
|
spew.Sprint(msg.TxIn[0]), spew.Sprint(txIn))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure transaction outputs are added properly.
|
||||||
|
msg.AddTxOut(txOut)
|
||||||
|
if !reflect.DeepEqual(msg.TxOut[0], txOut) {
|
||||||
|
t.Errorf("AddTxIn: wrong transaction output added - got %v, want %v",
|
||||||
|
spew.Sprint(msg.TxOut[0]), spew.Sprint(txOut))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure the copy produced an identical transaction message.
|
||||||
|
newMsg := msg.Copy()
|
||||||
|
if !reflect.DeepEqual(newMsg, msg) {
|
||||||
|
t.Errorf("Copy: mismatched tx messages - got %v, want %v",
|
||||||
|
spew.Sdump(newMsg), spew.Sdump(msg))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestTxHash tests the ability to generate the hash of a transaction accurately.
|
||||||
|
func TestTxHashAndID(t *testing.T) {
|
||||||
|
txHash1Str := "93663e597f6c968d32d229002f76408edf30d6a0151ff679fc729812d8cb2acc"
|
||||||
|
txID1Str := "24079c6d2bdf602fc389cc307349054937744a9c8dc0f07c023e6af0e949a4e7"
|
||||||
|
wantTxID1, err := transactionid.FromString(txID1Str)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewTxIDFromStr: %v", err)
|
||||||
|
}
|
||||||
|
wantTxHash1, err := transactionid.FromString(txHash1Str)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewTxIDFromStr: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A coinbase transaction
|
||||||
|
txIn := &TxIn{
|
||||||
|
PreviousOutpoint: Outpoint{
|
||||||
|
TxID: externalapi.DomainTransactionID{},
|
||||||
|
Index: math.MaxUint32,
|
||||||
|
},
|
||||||
|
SignatureScript: []byte{0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62},
|
||||||
|
Sequence: math.MaxUint64,
|
||||||
|
}
|
||||||
|
txOut := &TxOut{
|
||||||
|
Value: 5000000000,
|
||||||
|
ScriptPubKey: &externalapi.ScriptPublicKey{Script: []byte{
|
||||||
|
0x41, // OP_DATA_65
|
||||||
|
0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
|
||||||
|
0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
|
||||||
|
0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
|
||||||
|
0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
|
||||||
|
0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
|
||||||
|
0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
|
||||||
|
0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
|
||||||
|
0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
|
||||||
|
0xa6, // 65-byte signature
|
||||||
|
0xac, // OP_CHECKSIG
|
||||||
|
}, Version: 0},
|
||||||
|
}
|
||||||
|
tx1 := NewSubnetworkMsgTx(0, []*TxIn{txIn}, []*TxOut{txOut}, &subnetworks.SubnetworkIDCoinbase, 0, nil)
|
||||||
|
|
||||||
|
// Ensure the hash produced is expected.
|
||||||
|
tx1Hash := tx1.TxHash()
|
||||||
|
if *tx1Hash != (externalapi.DomainHash)(*wantTxHash1) {
|
||||||
|
t.Errorf("TxHash: wrong hash - got %v, want %v",
|
||||||
|
spew.Sprint(tx1Hash), spew.Sprint(wantTxHash1))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure the TxID for coinbase transaction is the same as TxHash.
|
||||||
|
tx1ID := tx1.TxID()
|
||||||
|
if !tx1ID.Equal(wantTxID1) {
|
||||||
|
t.Errorf("TxID: wrong ID - got %v, want %v",
|
||||||
|
spew.Sprint(tx1ID), spew.Sprint(wantTxID1))
|
||||||
|
}
|
||||||
|
|
||||||
|
hash2Str := "8dafd1bec24527d8e3b443ceb0a3b92fffc0d60026317f890b2faf5e9afc177a"
|
||||||
|
wantHash2, err := externalapi.NewDomainHashFromString(hash2Str)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("NewTxIDFromStr: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
id2Str := "89ffb49474637502d9059af38b8a95fc2f0d3baef5c801d7a9b9c8830671b711"
|
||||||
|
wantID2, err := transactionid.FromString(id2Str)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("NewTxIDFromStr: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
payload := []byte{1, 2, 3}
|
||||||
|
txIns := []*TxIn{{
|
||||||
|
PreviousOutpoint: Outpoint{
|
||||||
|
Index: 0,
|
||||||
|
TxID: *externalapi.NewDomainTransactionIDFromByteArray(&[externalapi.DomainHashSize]byte{1, 2, 3}),
|
||||||
|
},
|
||||||
|
SignatureScript: []byte{
|
||||||
|
0x49, 0x30, 0x46, 0x02, 0x21, 0x00, 0xDA, 0x0D, 0xC6, 0xAE, 0xCE, 0xFE, 0x1E, 0x06, 0xEF, 0xDF,
|
||||||
|
0x05, 0x77, 0x37, 0x57, 0xDE, 0xB1, 0x68, 0x82, 0x09, 0x30, 0xE3, 0xB0, 0xD0, 0x3F, 0x46, 0xF5,
|
||||||
|
0xFC, 0xF1, 0x50, 0xBF, 0x99, 0x0C, 0x02, 0x21, 0x00, 0xD2, 0x5B, 0x5C, 0x87, 0x04, 0x00, 0x76,
|
||||||
|
0xE4, 0xF2, 0x53, 0xF8, 0x26, 0x2E, 0x76, 0x3E, 0x2D, 0xD5, 0x1E, 0x7F, 0xF0, 0xBE, 0x15, 0x77,
|
||||||
|
0x27, 0xC4, 0xBC, 0x42, 0x80, 0x7F, 0x17, 0xBD, 0x39, 0x01, 0x41, 0x04, 0xE6, 0xC2, 0x6E, 0xF6,
|
||||||
|
0x7D, 0xC6, 0x10, 0xD2, 0xCD, 0x19, 0x24, 0x84, 0x78, 0x9A, 0x6C, 0xF9, 0xAE, 0xA9, 0x93, 0x0B,
|
||||||
|
0x94, 0x4B, 0x7E, 0x2D, 0xB5, 0x34, 0x2B, 0x9D, 0x9E, 0x5B, 0x9F, 0xF7, 0x9A, 0xFF, 0x9A, 0x2E,
|
||||||
|
0xE1, 0x97, 0x8D, 0xD7, 0xFD, 0x01, 0xDF, 0xC5, 0x22, 0xEE, 0x02, 0x28, 0x3D, 0x3B, 0x06, 0xA9,
|
||||||
|
0xD0, 0x3A, 0xCF, 0x80, 0x96, 0x96, 0x8D, 0x7D, 0xBB, 0x0F, 0x91, 0x78,
|
||||||
|
},
|
||||||
|
Sequence: math.MaxUint64,
|
||||||
|
}}
|
||||||
|
txOuts := []*TxOut{
|
||||||
|
{
|
||||||
|
Value: 244623243,
|
||||||
|
ScriptPubKey: &externalapi.ScriptPublicKey{Script: []byte{
|
||||||
|
0x76, 0xA9, 0x14, 0xBA, 0xDE, 0xEC, 0xFD, 0xEF, 0x05, 0x07, 0x24, 0x7F, 0xC8, 0xF7, 0x42, 0x41,
|
||||||
|
0xD7, 0x3B, 0xC0, 0x39, 0x97, 0x2D, 0x7B, 0x88, 0xAC,
|
||||||
|
}, Version: 0},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Value: 44602432,
|
||||||
|
ScriptPubKey: &externalapi.ScriptPublicKey{Script: []byte{
|
||||||
|
0x76, 0xA9, 0x14, 0xC1, 0x09, 0x32, 0x48, 0x3F, 0xEC, 0x93, 0xED, 0x51, 0xF5, 0xFE, 0x95, 0xE7,
|
||||||
|
0x25, 0x59, 0xF2, 0xCC, 0x70, 0x43, 0xF9, 0x88, 0xAC,
|
||||||
|
}, Version: 0},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
tx2 := NewSubnetworkMsgTx(1, txIns, txOuts, &externalapi.DomainSubnetworkID{1, 2, 3}, 0, payload)
|
||||||
|
|
||||||
|
// Ensure the hash produced is expected.
|
||||||
|
tx2Hash := tx2.TxHash()
|
||||||
|
if !tx2Hash.Equal(wantHash2) {
|
||||||
|
t.Errorf("TxHash: wrong hash - got %v, want %v",
|
||||||
|
spew.Sprint(tx2Hash), spew.Sprint(wantHash2))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure the TxID for coinbase transaction is the same as TxHash.
|
||||||
|
tx2ID := tx2.TxID()
|
||||||
|
if !tx2ID.Equal(wantID2) {
|
||||||
|
t.Errorf("TxID: wrong ID - got %v, want %v",
|
||||||
|
spew.Sprint(tx2ID), spew.Sprint(wantID2))
|
||||||
|
}
|
||||||
|
|
||||||
|
if tx2ID.Equal((*externalapi.DomainTransactionID)(tx2Hash)) {
|
||||||
|
t.Errorf("tx2ID and tx2Hash shouldn't be the same for non-coinbase transaction with signature and/or payload")
|
||||||
|
}
|
||||||
|
|
||||||
|
tx2.TxIn[0].SignatureScript = []byte{}
|
||||||
|
newTx2Hash := tx2.TxHash()
|
||||||
|
if *tx2ID == (externalapi.DomainTransactionID)(*newTx2Hash) {
|
||||||
|
t.Errorf("tx2ID and newTx2Hash should not be the same even for transaction with an empty signature")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,14 +6,13 @@ package appmessage
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/kaspanet/kaspad/version"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
|
"github.com/kaspanet/kaspad/version"
|
||||||
|
|
||||||
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/id"
|
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/id"
|
||||||
"github.com/kaspanet/kaspad/util/mstime"
|
"github.com/kaspanet/kaspad/util/mstime"
|
||||||
|
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
|
||||||
"github.com/kaspanet/kaspad/util/subnetworkid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// MaxUserAgentLen is the maximum allowed length for the user agent field in a
|
// MaxUserAgentLen is the maximum allowed length for the user agent field in a
|
||||||
@@ -54,14 +53,11 @@ type MsgVersion struct {
|
|||||||
// on the appmessage. This has a max length of MaxUserAgentLen.
|
// on the appmessage. This has a max length of MaxUserAgentLen.
|
||||||
UserAgent string
|
UserAgent string
|
||||||
|
|
||||||
// The selected tip hash of the generator of the version message.
|
|
||||||
SelectedTipHash *daghash.Hash
|
|
||||||
|
|
||||||
// Don't announce transactions to peer.
|
// Don't announce transactions to peer.
|
||||||
DisableRelayTx bool
|
DisableRelayTx bool
|
||||||
|
|
||||||
// The subnetwork of the generator of the version message. Should be nil in full nodes
|
// The subnetwork of the generator of the version message. Should be nil in full nodes
|
||||||
SubnetworkID *subnetworkid.SubnetworkID
|
SubnetworkID *externalapi.DomainSubnetworkID
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasService returns whether the specified service is supported by the peer
|
// HasService returns whether the specified service is supported by the peer
|
||||||
@@ -86,7 +82,7 @@ func (msg *MsgVersion) Command() MessageCommand {
|
|||||||
// Message interface using the passed parameters and defaults for the remaining
|
// Message interface using the passed parameters and defaults for the remaining
|
||||||
// fields.
|
// fields.
|
||||||
func NewMsgVersion(addr *NetAddress, id *id.ID, network string,
|
func NewMsgVersion(addr *NetAddress, id *id.ID, network string,
|
||||||
selectedTipHash *daghash.Hash, subnetworkID *subnetworkid.SubnetworkID) *MsgVersion {
|
subnetworkID *externalapi.DomainSubnetworkID) *MsgVersion {
|
||||||
|
|
||||||
// Limit the timestamp to one millisecond precision since the protocol
|
// Limit the timestamp to one millisecond precision since the protocol
|
||||||
// doesn't support better.
|
// doesn't support better.
|
||||||
@@ -98,7 +94,6 @@ func NewMsgVersion(addr *NetAddress, id *id.ID, network string,
|
|||||||
Address: addr,
|
Address: addr,
|
||||||
ID: id,
|
ID: id,
|
||||||
UserAgent: DefaultUserAgent,
|
UserAgent: DefaultUserAgent,
|
||||||
SelectedTipHash: selectedTipHash,
|
|
||||||
DisableRelayTx: false,
|
DisableRelayTx: false,
|
||||||
SubnetworkID: subnetworkID,
|
SubnetworkID: subnetworkID,
|
||||||
}
|
}
|
||||||
@@ -5,12 +5,12 @@
|
|||||||
package appmessage
|
package appmessage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/davecgh/go-spew/spew"
|
|
||||||
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/id"
|
|
||||||
"github.com/kaspanet/kaspad/util/daghash"
|
|
||||||
"net"
|
"net"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
"github.com/kaspanet/kaspad/infrastructure/network/netadapter/id"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestVersion tests the MsgVersion API.
|
// TestVersion tests the MsgVersion API.
|
||||||
@@ -18,16 +18,15 @@ func TestVersion(t *testing.T) {
|
|||||||
pver := ProtocolVersion
|
pver := ProtocolVersion
|
||||||
|
|
||||||
// Create version message data.
|
// Create version message data.
|
||||||
selectedTipHash := &daghash.Hash{12, 34}
|
|
||||||
tcpAddrMe := &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 16111}
|
tcpAddrMe := &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 16111}
|
||||||
me := NewNetAddress(tcpAddrMe, SFNodeNetwork)
|
me := NewNetAddress(tcpAddrMe)
|
||||||
generatedID, err := id.GenerateID()
|
generatedID, err := id.GenerateID()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("id.GenerateID: %s", err)
|
t.Fatalf("id.GenerateID: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ensure we get the correct data back out.
|
// Ensure we get the correct data back out.
|
||||||
msg := NewMsgVersion(me, generatedID, "mainnet", selectedTipHash, nil)
|
msg := NewMsgVersion(me, generatedID, "mainnet", nil)
|
||||||
if msg.ProtocolVersion != pver {
|
if msg.ProtocolVersion != pver {
|
||||||
t.Errorf("NewMsgVersion: wrong protocol version - got %v, want %v",
|
t.Errorf("NewMsgVersion: wrong protocol version - got %v, want %v",
|
||||||
msg.ProtocolVersion, pver)
|
msg.ProtocolVersion, pver)
|
||||||
@@ -44,10 +43,6 @@ func TestVersion(t *testing.T) {
|
|||||||
t.Errorf("NewMsgVersion: wrong user agent - got %v, want %v",
|
t.Errorf("NewMsgVersion: wrong user agent - got %v, want %v",
|
||||||
msg.UserAgent, DefaultUserAgent)
|
msg.UserAgent, DefaultUserAgent)
|
||||||
}
|
}
|
||||||
if !msg.SelectedTipHash.IsEqual(selectedTipHash) {
|
|
||||||
t.Errorf("NewMsgVersion: wrong selected tip hash - got %s, want %s",
|
|
||||||
msg.SelectedTipHash, selectedTipHash)
|
|
||||||
}
|
|
||||||
if msg.DisableRelayTx {
|
if msg.DisableRelayTx {
|
||||||
t.Errorf("NewMsgVersion: disable relay tx is not false by "+
|
t.Errorf("NewMsgVersion: disable relay tx is not false by "+
|
||||||
"default - got %v, want %v", msg.DisableRelayTx, false)
|
"default - got %v, want %v", msg.DisableRelayTx, false)
|
||||||
@@ -15,9 +15,6 @@ type NetAddress struct {
|
|||||||
// Last time the address was seen.
|
// Last time the address was seen.
|
||||||
Timestamp mstime.Time
|
Timestamp mstime.Time
|
||||||
|
|
||||||
// Bitfield which identifies the services supported by the address.
|
|
||||||
Services ServiceFlag
|
|
||||||
|
|
||||||
// IP address of the peer.
|
// IP address of the peer.
|
||||||
IP net.IP
|
IP net.IP
|
||||||
|
|
||||||
@@ -26,17 +23,6 @@ type NetAddress struct {
|
|||||||
Port uint16
|
Port uint16
|
||||||
}
|
}
|
||||||
|
|
||||||
// HasService returns whether the specified service is supported by the address.
|
|
||||||
func (na *NetAddress) HasService(service ServiceFlag) bool {
|
|
||||||
return na.Services&service == service
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddService adds service as a supported service by the peer generating the
|
|
||||||
// message.
|
|
||||||
func (na *NetAddress) AddService(service ServiceFlag) {
|
|
||||||
na.Services |= service
|
|
||||||
}
|
|
||||||
|
|
||||||
// TCPAddress converts the NetAddress to *net.TCPAddr
|
// TCPAddress converts the NetAddress to *net.TCPAddr
|
||||||
func (na *NetAddress) TCPAddress() *net.TCPAddr {
|
func (na *NetAddress) TCPAddress() *net.TCPAddr {
|
||||||
return &net.TCPAddr{
|
return &net.TCPAddr{
|
||||||
@@ -47,20 +33,19 @@ func (na *NetAddress) TCPAddress() *net.TCPAddr {
|
|||||||
|
|
||||||
// NewNetAddressIPPort returns a new NetAddress using the provided IP, port, and
|
// NewNetAddressIPPort returns a new NetAddress using the provided IP, port, and
|
||||||
// supported services with defaults for the remaining fields.
|
// supported services with defaults for the remaining fields.
|
||||||
func NewNetAddressIPPort(ip net.IP, port uint16, services ServiceFlag) *NetAddress {
|
func NewNetAddressIPPort(ip net.IP, port uint16) *NetAddress {
|
||||||
return NewNetAddressTimestamp(mstime.Now(), services, ip, port)
|
return NewNetAddressTimestamp(mstime.Now(), ip, port)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewNetAddressTimestamp returns a new NetAddress using the provided
|
// NewNetAddressTimestamp returns a new NetAddress using the provided
|
||||||
// timestamp, IP, port, and supported services. The timestamp is rounded to
|
// timestamp, IP, port, and supported services. The timestamp is rounded to
|
||||||
// single millisecond precision.
|
// single millisecond precision.
|
||||||
func NewNetAddressTimestamp(
|
func NewNetAddressTimestamp(
|
||||||
timestamp mstime.Time, services ServiceFlag, ip net.IP, port uint16) *NetAddress {
|
timestamp mstime.Time, ip net.IP, port uint16) *NetAddress {
|
||||||
// Limit the timestamp to one millisecond precision since the protocol
|
// Limit the timestamp to one millisecond precision since the protocol
|
||||||
// doesn't support better.
|
// doesn't support better.
|
||||||
na := NetAddress{
|
na := NetAddress{
|
||||||
Timestamp: timestamp,
|
Timestamp: timestamp,
|
||||||
Services: services,
|
|
||||||
IP: ip,
|
IP: ip,
|
||||||
Port: port,
|
Port: port,
|
||||||
}
|
}
|
||||||
@@ -69,6 +54,6 @@ func NewNetAddressTimestamp(
|
|||||||
|
|
||||||
// NewNetAddress returns a new NetAddress using the provided TCP address and
|
// NewNetAddress returns a new NetAddress using the provided TCP address and
|
||||||
// supported services with defaults for the remaining fields.
|
// supported services with defaults for the remaining fields.
|
||||||
func NewNetAddress(addr *net.TCPAddr, services ServiceFlag) *NetAddress {
|
func NewNetAddress(addr *net.TCPAddr) *NetAddress {
|
||||||
return NewNetAddressIPPort(addr.IP, uint16(addr.Port), services)
|
return NewNetAddressIPPort(addr.IP, uint16(addr.Port))
|
||||||
}
|
}
|
||||||
@@ -15,7 +15,7 @@ func TestNetAddress(t *testing.T) {
|
|||||||
port := 16111
|
port := 16111
|
||||||
|
|
||||||
// Test NewNetAddress.
|
// Test NewNetAddress.
|
||||||
na := NewNetAddress(&net.TCPAddr{IP: ip, Port: port}, 0)
|
na := NewNetAddress(&net.TCPAddr{IP: ip, Port: port})
|
||||||
|
|
||||||
// Ensure we get the same ip, port, and services back out.
|
// Ensure we get the same ip, port, and services back out.
|
||||||
if !na.IP.Equal(ip) {
|
if !na.IP.Equal(ip) {
|
||||||
@@ -25,21 +25,4 @@ func TestNetAddress(t *testing.T) {
|
|||||||
t.Errorf("NetNetAddress: wrong port - got %v, want %v", na.Port,
|
t.Errorf("NetNetAddress: wrong port - got %v, want %v", na.Port,
|
||||||
port)
|
port)
|
||||||
}
|
}
|
||||||
if na.Services != 0 {
|
|
||||||
t.Errorf("NetNetAddress: wrong services - got %v, want %v",
|
|
||||||
na.Services, 0)
|
|
||||||
}
|
|
||||||
if na.HasService(SFNodeNetwork) {
|
|
||||||
t.Errorf("HasService: SFNodeNetwork service is set")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure adding the full service node flag works.
|
|
||||||
na.AddService(SFNodeNetwork)
|
|
||||||
if na.Services != SFNodeNetwork {
|
|
||||||
t.Errorf("AddService: wrong services - got %v, want %v",
|
|
||||||
na.Services, SFNodeNetwork)
|
|
||||||
}
|
|
||||||
if !na.HasService(SFNodeNetwork) {
|
|
||||||
t.Errorf("HasService: SFNodeNetwork service not set")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
16
app/appmessage/p2p_requestpruningpointhash.go
Normal file
16
app/appmessage/p2p_requestpruningpointhash.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// MsgRequestPruningPointHashMessage represents a kaspa RequestPruningPointHashMessage message
|
||||||
|
type MsgRequestPruningPointHashMessage struct {
|
||||||
|
baseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *MsgRequestPruningPointHashMessage) Command() MessageCommand {
|
||||||
|
return CmdRequestPruningPointHash
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMsgRequestPruningPointHashMessage returns a new kaspa RequestPruningPointHash message
|
||||||
|
func NewMsgRequestPruningPointHashMessage() *MsgRequestPruningPointHashMessage {
|
||||||
|
return &MsgRequestPruningPointHashMessage{}
|
||||||
|
}
|
||||||
16
app/appmessage/p2p_unexpectedpruningpoint.go
Normal file
16
app/appmessage/p2p_unexpectedpruningpoint.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// MsgUnexpectedPruningPoint represents a kaspa UnexpectedPruningPoint message
|
||||||
|
type MsgUnexpectedPruningPoint struct {
|
||||||
|
baseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *MsgUnexpectedPruningPoint) Command() MessageCommand {
|
||||||
|
return CmdUnexpectedPruningPoint
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewMsgUnexpectedPruningPoint returns a new kaspa UnexpectedPruningPoint message
|
||||||
|
func NewMsgUnexpectedPruningPoint() *MsgUnexpectedPruningPoint {
|
||||||
|
return &MsgUnexpectedPruningPoint{}
|
||||||
|
}
|
||||||
39
app/appmessage/rpc_add_peer.go
Normal file
39
app/appmessage/rpc_add_peer.go
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// AddPeerRequestMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type AddPeerRequestMessage struct {
|
||||||
|
baseMessage
|
||||||
|
Address string
|
||||||
|
IsPermanent bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *AddPeerRequestMessage) Command() MessageCommand {
|
||||||
|
return CmdAddPeerRequestMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewAddPeerRequestMessage returns a instance of the message
|
||||||
|
func NewAddPeerRequestMessage(address string, isPermanent bool) *AddPeerRequestMessage {
|
||||||
|
return &AddPeerRequestMessage{
|
||||||
|
Address: address,
|
||||||
|
IsPermanent: isPermanent,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddPeerResponseMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type AddPeerResponseMessage struct {
|
||||||
|
baseMessage
|
||||||
|
Error *RPCError
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *AddPeerResponseMessage) Command() MessageCommand {
|
||||||
|
return CmdAddPeerResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewAddPeerResponseMessage returns a instance of the message
|
||||||
|
func NewAddPeerResponseMessage() *AddPeerResponseMessage {
|
||||||
|
return &AddPeerResponseMessage{}
|
||||||
|
}
|
||||||
39
app/appmessage/rpc_ban.go
Normal file
39
app/appmessage/rpc_ban.go
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// BanRequestMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type BanRequestMessage struct {
|
||||||
|
baseMessage
|
||||||
|
|
||||||
|
IP string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *BanRequestMessage) Command() MessageCommand {
|
||||||
|
return CmdBanRequestMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewBanRequestMessage returns an instance of the message
|
||||||
|
func NewBanRequestMessage(ip string) *BanRequestMessage {
|
||||||
|
return &BanRequestMessage{
|
||||||
|
IP: ip,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BanResponseMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type BanResponseMessage struct {
|
||||||
|
baseMessage
|
||||||
|
|
||||||
|
Error *RPCError
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *BanResponseMessage) Command() MessageCommand {
|
||||||
|
return CmdBanResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewBanResponseMessage returns a instance of the message
|
||||||
|
func NewBanResponseMessage() *BanResponseMessage {
|
||||||
|
return &BanResponseMessage{}
|
||||||
|
}
|
||||||
41
app/appmessage/rpc_get_block.go
Normal file
41
app/appmessage/rpc_get_block.go
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// GetBlockRequestMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetBlockRequestMessage struct {
|
||||||
|
baseMessage
|
||||||
|
Hash string
|
||||||
|
IncludeTransactionVerboseData bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetBlockRequestMessage) Command() MessageCommand {
|
||||||
|
return CmdGetBlockRequestMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetBlockRequestMessage returns a instance of the message
|
||||||
|
func NewGetBlockRequestMessage(hash string, includeTransactionVerboseData bool) *GetBlockRequestMessage {
|
||||||
|
return &GetBlockRequestMessage{
|
||||||
|
Hash: hash,
|
||||||
|
IncludeTransactionVerboseData: includeTransactionVerboseData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetBlockResponseMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetBlockResponseMessage struct {
|
||||||
|
baseMessage
|
||||||
|
Block *RPCBlock
|
||||||
|
|
||||||
|
Error *RPCError
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetBlockResponseMessage) Command() MessageCommand {
|
||||||
|
return CmdGetBlockResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetBlockResponseMessage returns a instance of the message
|
||||||
|
func NewGetBlockResponseMessage() *GetBlockResponseMessage {
|
||||||
|
return &GetBlockResponseMessage{}
|
||||||
|
}
|
||||||
42
app/appmessage/rpc_get_block_count.go
Normal file
42
app/appmessage/rpc_get_block_count.go
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
import "github.com/kaspanet/kaspad/domain/consensus/model/externalapi"
|
||||||
|
|
||||||
|
// GetBlockCountRequestMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetBlockCountRequestMessage struct {
|
||||||
|
baseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetBlockCountRequestMessage) Command() MessageCommand {
|
||||||
|
return CmdGetBlockCountRequestMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetBlockCountRequestMessage returns a instance of the message
|
||||||
|
func NewGetBlockCountRequestMessage() *GetBlockCountRequestMessage {
|
||||||
|
return &GetBlockCountRequestMessage{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetBlockCountResponseMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetBlockCountResponseMessage struct {
|
||||||
|
baseMessage
|
||||||
|
BlockCount uint64
|
||||||
|
HeaderCount uint64
|
||||||
|
|
||||||
|
Error *RPCError
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetBlockCountResponseMessage) Command() MessageCommand {
|
||||||
|
return CmdGetBlockCountResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetBlockCountResponseMessage returns a instance of the message
|
||||||
|
func NewGetBlockCountResponseMessage(syncInfo *externalapi.SyncInfo) *GetBlockCountResponseMessage {
|
||||||
|
return &GetBlockCountResponseMessage{
|
||||||
|
BlockCount: syncInfo.BlockCount,
|
||||||
|
HeaderCount: syncInfo.HeaderCount,
|
||||||
|
}
|
||||||
|
}
|
||||||
43
app/appmessage/rpc_get_block_dag_info.go
Normal file
43
app/appmessage/rpc_get_block_dag_info.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// GetBlockDAGInfoRequestMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetBlockDAGInfoRequestMessage struct {
|
||||||
|
baseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetBlockDAGInfoRequestMessage) Command() MessageCommand {
|
||||||
|
return CmdGetBlockDAGInfoRequestMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetBlockDAGInfoRequestMessage returns a instance of the message
|
||||||
|
func NewGetBlockDAGInfoRequestMessage() *GetBlockDAGInfoRequestMessage {
|
||||||
|
return &GetBlockDAGInfoRequestMessage{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetBlockDAGInfoResponseMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetBlockDAGInfoResponseMessage struct {
|
||||||
|
baseMessage
|
||||||
|
NetworkName string
|
||||||
|
BlockCount uint64
|
||||||
|
HeaderCount uint64
|
||||||
|
TipHashes []string
|
||||||
|
VirtualParentHashes []string
|
||||||
|
Difficulty float64
|
||||||
|
PastMedianTime int64
|
||||||
|
PruningPointHash string
|
||||||
|
|
||||||
|
Error *RPCError
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetBlockDAGInfoResponseMessage) Command() MessageCommand {
|
||||||
|
return CmdGetBlockDAGInfoResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetBlockDAGInfoResponseMessage returns a instance of the message
|
||||||
|
func NewGetBlockDAGInfoResponseMessage() *GetBlockDAGInfoResponseMessage {
|
||||||
|
return &GetBlockDAGInfoResponseMessage{}
|
||||||
|
}
|
||||||
43
app/appmessage/rpc_get_block_template.go
Normal file
43
app/appmessage/rpc_get_block_template.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// GetBlockTemplateRequestMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetBlockTemplateRequestMessage struct {
|
||||||
|
baseMessage
|
||||||
|
PayAddress string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetBlockTemplateRequestMessage) Command() MessageCommand {
|
||||||
|
return CmdGetBlockTemplateRequestMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetBlockTemplateRequestMessage returns a instance of the message
|
||||||
|
func NewGetBlockTemplateRequestMessage(payAddress string) *GetBlockTemplateRequestMessage {
|
||||||
|
return &GetBlockTemplateRequestMessage{
|
||||||
|
PayAddress: payAddress,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetBlockTemplateResponseMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetBlockTemplateResponseMessage struct {
|
||||||
|
baseMessage
|
||||||
|
Block *RPCBlock
|
||||||
|
IsSynced bool
|
||||||
|
|
||||||
|
Error *RPCError
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetBlockTemplateResponseMessage) Command() MessageCommand {
|
||||||
|
return CmdGetBlockTemplateResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetBlockTemplateResponseMessage returns a instance of the message
|
||||||
|
func NewGetBlockTemplateResponseMessage(block *RPCBlock, isSynced bool) *GetBlockTemplateResponseMessage {
|
||||||
|
return &GetBlockTemplateResponseMessage{
|
||||||
|
Block: block,
|
||||||
|
IsSynced: isSynced,
|
||||||
|
}
|
||||||
|
}
|
||||||
45
app/appmessage/rpc_get_blocks.go
Normal file
45
app/appmessage/rpc_get_blocks.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// GetBlocksRequestMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetBlocksRequestMessage struct {
|
||||||
|
baseMessage
|
||||||
|
LowHash string
|
||||||
|
IncludeBlocks bool
|
||||||
|
IncludeTransactionVerboseData bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetBlocksRequestMessage) Command() MessageCommand {
|
||||||
|
return CmdGetBlocksRequestMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetBlocksRequestMessage returns a instance of the message
|
||||||
|
func NewGetBlocksRequestMessage(lowHash string, includeBlocks bool,
|
||||||
|
includeTransactionVerboseData bool) *GetBlocksRequestMessage {
|
||||||
|
return &GetBlocksRequestMessage{
|
||||||
|
LowHash: lowHash,
|
||||||
|
IncludeBlocks: includeBlocks,
|
||||||
|
IncludeTransactionVerboseData: includeTransactionVerboseData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetBlocksResponseMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetBlocksResponseMessage struct {
|
||||||
|
baseMessage
|
||||||
|
BlockHashes []string
|
||||||
|
Blocks []*RPCBlock
|
||||||
|
|
||||||
|
Error *RPCError
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetBlocksResponseMessage) Command() MessageCommand {
|
||||||
|
return CmdGetBlocksResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetBlocksResponseMessage returns a instance of the message
|
||||||
|
func NewGetBlocksResponseMessage() *GetBlocksResponseMessage {
|
||||||
|
return &GetBlocksResponseMessage{}
|
||||||
|
}
|
||||||
50
app/appmessage/rpc_get_connected_peer_info.go
Normal file
50
app/appmessage/rpc_get_connected_peer_info.go
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// GetConnectedPeerInfoRequestMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetConnectedPeerInfoRequestMessage struct {
|
||||||
|
baseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetConnectedPeerInfoRequestMessage) Command() MessageCommand {
|
||||||
|
return CmdGetConnectedPeerInfoRequestMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetConnectedPeerInfoRequestMessage returns a instance of the message
|
||||||
|
func NewGetConnectedPeerInfoRequestMessage() *GetConnectedPeerInfoRequestMessage {
|
||||||
|
return &GetConnectedPeerInfoRequestMessage{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetConnectedPeerInfoResponseMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetConnectedPeerInfoResponseMessage struct {
|
||||||
|
baseMessage
|
||||||
|
Infos []*GetConnectedPeerInfoMessage
|
||||||
|
Error *RPCError
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetConnectedPeerInfoResponseMessage) Command() MessageCommand {
|
||||||
|
return CmdGetConnectedPeerInfoResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetConnectedPeerInfoResponseMessage returns a instance of the message
|
||||||
|
func NewGetConnectedPeerInfoResponseMessage(infos []*GetConnectedPeerInfoMessage) *GetConnectedPeerInfoResponseMessage {
|
||||||
|
return &GetConnectedPeerInfoResponseMessage{
|
||||||
|
Infos: infos,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetConnectedPeerInfoMessage holds information about a connected peer
|
||||||
|
type GetConnectedPeerInfoMessage struct {
|
||||||
|
ID string
|
||||||
|
Address string
|
||||||
|
LastPingDuration int64
|
||||||
|
IsOutbound bool
|
||||||
|
TimeOffset int64
|
||||||
|
UserAgent string
|
||||||
|
AdvertisedProtocolVersion uint32
|
||||||
|
TimeConnected int64
|
||||||
|
IsIBDPeer bool
|
||||||
|
}
|
||||||
38
app/appmessage/rpc_get_current_network.go
Normal file
38
app/appmessage/rpc_get_current_network.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// GetCurrentNetworkRequestMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetCurrentNetworkRequestMessage struct {
|
||||||
|
baseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetCurrentNetworkRequestMessage) Command() MessageCommand {
|
||||||
|
return CmdGetCurrentNetworkRequestMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetCurrentNetworkRequestMessage returns a instance of the message
|
||||||
|
func NewGetCurrentNetworkRequestMessage() *GetCurrentNetworkRequestMessage {
|
||||||
|
return &GetCurrentNetworkRequestMessage{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetCurrentNetworkResponseMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetCurrentNetworkResponseMessage struct {
|
||||||
|
baseMessage
|
||||||
|
CurrentNetwork string
|
||||||
|
|
||||||
|
Error *RPCError
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetCurrentNetworkResponseMessage) Command() MessageCommand {
|
||||||
|
return CmdGetCurrentNetworkResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetCurrentNetworkResponseMessage returns a instance of the message
|
||||||
|
func NewGetCurrentNetworkResponseMessage(currentNetwork string) *GetCurrentNetworkResponseMessage {
|
||||||
|
return &GetCurrentNetworkResponseMessage{
|
||||||
|
CurrentNetwork: currentNetwork,
|
||||||
|
}
|
||||||
|
}
|
||||||
45
app/appmessage/rpc_get_headers.go
Normal file
45
app/appmessage/rpc_get_headers.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// GetHeadersRequestMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetHeadersRequestMessage struct {
|
||||||
|
baseMessage
|
||||||
|
StartHash string
|
||||||
|
Limit uint64
|
||||||
|
IsAscending bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetHeadersRequestMessage) Command() MessageCommand {
|
||||||
|
return CmdGetHeadersRequestMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetHeadersRequestMessage returns a instance of the message
|
||||||
|
func NewGetHeadersRequestMessage(startHash string, limit uint64, isAscending bool) *GetHeadersRequestMessage {
|
||||||
|
return &GetHeadersRequestMessage{
|
||||||
|
StartHash: startHash,
|
||||||
|
Limit: limit,
|
||||||
|
IsAscending: isAscending,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetHeadersResponseMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetHeadersResponseMessage struct {
|
||||||
|
baseMessage
|
||||||
|
Headers []string
|
||||||
|
|
||||||
|
Error *RPCError
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetHeadersResponseMessage) Command() MessageCommand {
|
||||||
|
return CmdGetHeadersResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetHeadersResponseMessage returns a instance of the message
|
||||||
|
func NewGetHeadersResponseMessage(headers []string) *GetHeadersResponseMessage {
|
||||||
|
return &GetHeadersResponseMessage{
|
||||||
|
Headers: headers,
|
||||||
|
}
|
||||||
|
}
|
||||||
40
app/appmessage/rpc_get_info.go
Normal file
40
app/appmessage/rpc_get_info.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// GetInfoRequestMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetInfoRequestMessage struct {
|
||||||
|
baseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetInfoRequestMessage) Command() MessageCommand {
|
||||||
|
return CmdGetInfoRequestMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetInfoRequestMessage returns a instance of the message
|
||||||
|
func NewGetInfoRequestMessage() *GetInfoRequestMessage {
|
||||||
|
return &GetInfoRequestMessage{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetInfoResponseMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetInfoResponseMessage struct {
|
||||||
|
baseMessage
|
||||||
|
P2PID string
|
||||||
|
MempoolSize uint64
|
||||||
|
|
||||||
|
Error *RPCError
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetInfoResponseMessage) Command() MessageCommand {
|
||||||
|
return CmdGetInfoResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetInfoResponseMessage returns a instance of the message
|
||||||
|
func NewGetInfoResponseMessage(p2pID string, mempoolSize uint64) *GetInfoResponseMessage {
|
||||||
|
return &GetInfoResponseMessage{
|
||||||
|
P2PID: p2pID,
|
||||||
|
MempoolSize: mempoolSize,
|
||||||
|
}
|
||||||
|
}
|
||||||
38
app/appmessage/rpc_get_mempool_entries.go
Normal file
38
app/appmessage/rpc_get_mempool_entries.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// GetMempoolEntriesRequestMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetMempoolEntriesRequestMessage struct {
|
||||||
|
baseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetMempoolEntriesRequestMessage) Command() MessageCommand {
|
||||||
|
return CmdGetMempoolEntriesRequestMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetMempoolEntriesRequestMessage returns a instance of the message
|
||||||
|
func NewGetMempoolEntriesRequestMessage() *GetMempoolEntriesRequestMessage {
|
||||||
|
return &GetMempoolEntriesRequestMessage{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMempoolEntriesResponseMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetMempoolEntriesResponseMessage struct {
|
||||||
|
baseMessage
|
||||||
|
Entries []*MempoolEntry
|
||||||
|
|
||||||
|
Error *RPCError
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetMempoolEntriesResponseMessage) Command() MessageCommand {
|
||||||
|
return CmdGetMempoolEntriesResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetMempoolEntriesResponseMessage returns a instance of the message
|
||||||
|
func NewGetMempoolEntriesResponseMessage(entries []*MempoolEntry) *GetMempoolEntriesResponseMessage {
|
||||||
|
return &GetMempoolEntriesResponseMessage{
|
||||||
|
Entries: entries,
|
||||||
|
}
|
||||||
|
}
|
||||||
48
app/appmessage/rpc_get_mempool_entry.go
Normal file
48
app/appmessage/rpc_get_mempool_entry.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// GetMempoolEntryRequestMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetMempoolEntryRequestMessage struct {
|
||||||
|
baseMessage
|
||||||
|
TxID string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetMempoolEntryRequestMessage) Command() MessageCommand {
|
||||||
|
return CmdGetMempoolEntryRequestMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetMempoolEntryRequestMessage returns a instance of the message
|
||||||
|
func NewGetMempoolEntryRequestMessage(txID string) *GetMempoolEntryRequestMessage {
|
||||||
|
return &GetMempoolEntryRequestMessage{TxID: txID}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMempoolEntryResponseMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetMempoolEntryResponseMessage struct {
|
||||||
|
baseMessage
|
||||||
|
Entry *MempoolEntry
|
||||||
|
|
||||||
|
Error *RPCError
|
||||||
|
}
|
||||||
|
|
||||||
|
// MempoolEntry represents a transaction in the mempool.
|
||||||
|
type MempoolEntry struct {
|
||||||
|
Fee uint64
|
||||||
|
Transaction *RPCTransaction
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetMempoolEntryResponseMessage) Command() MessageCommand {
|
||||||
|
return CmdGetMempoolEntryResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetMempoolEntryResponseMessage returns a instance of the message
|
||||||
|
func NewGetMempoolEntryResponseMessage(fee uint64, transaction *RPCTransaction) *GetMempoolEntryResponseMessage {
|
||||||
|
return &GetMempoolEntryResponseMessage{
|
||||||
|
Entry: &MempoolEntry{
|
||||||
|
Fee: fee,
|
||||||
|
Transaction: transaction,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
46
app/appmessage/rpc_get_peer_addresses.go
Normal file
46
app/appmessage/rpc_get_peer_addresses.go
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// GetPeerAddressesRequestMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetPeerAddressesRequestMessage struct {
|
||||||
|
baseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetPeerAddressesRequestMessage) Command() MessageCommand {
|
||||||
|
return CmdGetPeerAddressesRequestMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetPeerAddressesRequestMessage returns a instance of the message
|
||||||
|
func NewGetPeerAddressesRequestMessage() *GetPeerAddressesRequestMessage {
|
||||||
|
return &GetPeerAddressesRequestMessage{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetPeerAddressesResponseMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetPeerAddressesResponseMessage struct {
|
||||||
|
baseMessage
|
||||||
|
Addresses []*GetPeerAddressesKnownAddressMessage
|
||||||
|
BannedAddresses []*GetPeerAddressesKnownAddressMessage
|
||||||
|
|
||||||
|
Error *RPCError
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetPeerAddressesResponseMessage) Command() MessageCommand {
|
||||||
|
return CmdGetPeerAddressesResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetPeerAddressesResponseMessage returns a instance of the message
|
||||||
|
func NewGetPeerAddressesResponseMessage(addresses []*GetPeerAddressesKnownAddressMessage, bannedAddresses []*GetPeerAddressesKnownAddressMessage) *GetPeerAddressesResponseMessage {
|
||||||
|
return &GetPeerAddressesResponseMessage{
|
||||||
|
Addresses: addresses,
|
||||||
|
BannedAddresses: bannedAddresses,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetPeerAddressesKnownAddressMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetPeerAddressesKnownAddressMessage struct {
|
||||||
|
Addr string
|
||||||
|
}
|
||||||
38
app/appmessage/rpc_get_selected_tip_hash.go
Normal file
38
app/appmessage/rpc_get_selected_tip_hash.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// GetSelectedTipHashRequestMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetSelectedTipHashRequestMessage struct {
|
||||||
|
baseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetSelectedTipHashRequestMessage) Command() MessageCommand {
|
||||||
|
return CmdGetSelectedTipHashRequestMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetSelectedTipHashRequestMessage returns a instance of the message
|
||||||
|
func NewGetSelectedTipHashRequestMessage() *GetSelectedTipHashRequestMessage {
|
||||||
|
return &GetSelectedTipHashRequestMessage{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSelectedTipHashResponseMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetSelectedTipHashResponseMessage struct {
|
||||||
|
baseMessage
|
||||||
|
SelectedTipHash string
|
||||||
|
|
||||||
|
Error *RPCError
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetSelectedTipHashResponseMessage) Command() MessageCommand {
|
||||||
|
return CmdGetSelectedTipHashResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetSelectedTipHashResponseMessage returns a instance of the message
|
||||||
|
func NewGetSelectedTipHashResponseMessage(selectedTipHash string) *GetSelectedTipHashResponseMessage {
|
||||||
|
return &GetSelectedTipHashResponseMessage{
|
||||||
|
SelectedTipHash: selectedTipHash,
|
||||||
|
}
|
||||||
|
}
|
||||||
41
app/appmessage/rpc_get_subnetwork.go
Normal file
41
app/appmessage/rpc_get_subnetwork.go
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// GetSubnetworkRequestMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetSubnetworkRequestMessage struct {
|
||||||
|
baseMessage
|
||||||
|
SubnetworkID string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetSubnetworkRequestMessage) Command() MessageCommand {
|
||||||
|
return CmdGetSubnetworkRequestMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetSubnetworkRequestMessage returns a instance of the message
|
||||||
|
func NewGetSubnetworkRequestMessage(subnetworkID string) *GetSubnetworkRequestMessage {
|
||||||
|
return &GetSubnetworkRequestMessage{
|
||||||
|
SubnetworkID: subnetworkID,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSubnetworkResponseMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetSubnetworkResponseMessage struct {
|
||||||
|
baseMessage
|
||||||
|
GasLimit uint64
|
||||||
|
|
||||||
|
Error *RPCError
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetSubnetworkResponseMessage) Command() MessageCommand {
|
||||||
|
return CmdGetSubnetworkResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetSubnetworkResponseMessage returns a instance of the message
|
||||||
|
func NewGetSubnetworkResponseMessage(gasLimit uint64) *GetSubnetworkResponseMessage {
|
||||||
|
return &GetSubnetworkResponseMessage{
|
||||||
|
GasLimit: gasLimit,
|
||||||
|
}
|
||||||
|
}
|
||||||
41
app/appmessage/rpc_get_utxos_by_addresses.go
Normal file
41
app/appmessage/rpc_get_utxos_by_addresses.go
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// GetUTXOsByAddressesRequestMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetUTXOsByAddressesRequestMessage struct {
|
||||||
|
baseMessage
|
||||||
|
Addresses []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetUTXOsByAddressesRequestMessage) Command() MessageCommand {
|
||||||
|
return CmdGetUTXOsByAddressesRequestMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetUTXOsByAddressesRequestMessage returns a instance of the message
|
||||||
|
func NewGetUTXOsByAddressesRequestMessage(addresses []string) *GetUTXOsByAddressesRequestMessage {
|
||||||
|
return &GetUTXOsByAddressesRequestMessage{
|
||||||
|
Addresses: addresses,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetUTXOsByAddressesResponseMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetUTXOsByAddressesResponseMessage struct {
|
||||||
|
baseMessage
|
||||||
|
Entries []*UTXOsByAddressesEntry
|
||||||
|
|
||||||
|
Error *RPCError
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetUTXOsByAddressesResponseMessage) Command() MessageCommand {
|
||||||
|
return CmdGetUTXOsByAddressesResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetUTXOsByAddressesResponseMessage returns a instance of the message
|
||||||
|
func NewGetUTXOsByAddressesResponseMessage(entries []*UTXOsByAddressesEntry) *GetUTXOsByAddressesResponseMessage {
|
||||||
|
return &GetUTXOsByAddressesResponseMessage{
|
||||||
|
Entries: entries,
|
||||||
|
}
|
||||||
|
}
|
||||||
38
app/appmessage/rpc_get_virtual_selected_parent_blue_score.go
Normal file
38
app/appmessage/rpc_get_virtual_selected_parent_blue_score.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// GetVirtualSelectedParentBlueScoreRequestMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetVirtualSelectedParentBlueScoreRequestMessage struct {
|
||||||
|
baseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetVirtualSelectedParentBlueScoreRequestMessage) Command() MessageCommand {
|
||||||
|
return CmdGetVirtualSelectedParentBlueScoreRequestMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetVirtualSelectedParentBlueScoreRequestMessage returns a instance of the message
|
||||||
|
func NewGetVirtualSelectedParentBlueScoreRequestMessage() *GetVirtualSelectedParentBlueScoreRequestMessage {
|
||||||
|
return &GetVirtualSelectedParentBlueScoreRequestMessage{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetVirtualSelectedParentBlueScoreResponseMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetVirtualSelectedParentBlueScoreResponseMessage struct {
|
||||||
|
baseMessage
|
||||||
|
BlueScore uint64
|
||||||
|
|
||||||
|
Error *RPCError
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetVirtualSelectedParentBlueScoreResponseMessage) Command() MessageCommand {
|
||||||
|
return CmdGetVirtualSelectedParentBlueScoreResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetVirtualSelectedParentBlueScoreResponseMessage returns a instance of the message
|
||||||
|
func NewGetVirtualSelectedParentBlueScoreResponseMessage(blueScore uint64) *GetVirtualSelectedParentBlueScoreResponseMessage {
|
||||||
|
return &GetVirtualSelectedParentBlueScoreResponseMessage{
|
||||||
|
BlueScore: blueScore,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// GetVirtualSelectedParentChainFromBlockRequestMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetVirtualSelectedParentChainFromBlockRequestMessage struct {
|
||||||
|
baseMessage
|
||||||
|
StartHash string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetVirtualSelectedParentChainFromBlockRequestMessage) Command() MessageCommand {
|
||||||
|
return CmdGetVirtualSelectedParentChainFromBlockRequestMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetVirtualSelectedParentChainFromBlockRequestMessage returns a instance of the message
|
||||||
|
func NewGetVirtualSelectedParentChainFromBlockRequestMessage(startHash string) *GetVirtualSelectedParentChainFromBlockRequestMessage {
|
||||||
|
return &GetVirtualSelectedParentChainFromBlockRequestMessage{
|
||||||
|
StartHash: startHash,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetVirtualSelectedParentChainFromBlockResponseMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type GetVirtualSelectedParentChainFromBlockResponseMessage struct {
|
||||||
|
baseMessage
|
||||||
|
RemovedChainBlockHashes []string
|
||||||
|
AddedChainBlocks []*ChainBlock
|
||||||
|
|
||||||
|
Error *RPCError
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *GetVirtualSelectedParentChainFromBlockResponseMessage) Command() MessageCommand {
|
||||||
|
return CmdGetVirtualSelectedParentChainFromBlockResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewGetVirtualSelectedParentChainFromBlockResponseMessage returns a instance of the message
|
||||||
|
func NewGetVirtualSelectedParentChainFromBlockResponseMessage(removedChainBlockHashes []string,
|
||||||
|
addedChainBlocks []*ChainBlock) *GetVirtualSelectedParentChainFromBlockResponseMessage {
|
||||||
|
|
||||||
|
return &GetVirtualSelectedParentChainFromBlockResponseMessage{
|
||||||
|
RemovedChainBlockHashes: removedChainBlockHashes,
|
||||||
|
AddedChainBlocks: addedChainBlocks,
|
||||||
|
}
|
||||||
|
}
|
||||||
53
app/appmessage/rpc_notify_block_added.go
Normal file
53
app/appmessage/rpc_notify_block_added.go
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// NotifyBlockAddedRequestMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type NotifyBlockAddedRequestMessage struct {
|
||||||
|
baseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *NotifyBlockAddedRequestMessage) Command() MessageCommand {
|
||||||
|
return CmdNotifyBlockAddedRequestMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewNotifyBlockAddedRequestMessage returns a instance of the message
|
||||||
|
func NewNotifyBlockAddedRequestMessage() *NotifyBlockAddedRequestMessage {
|
||||||
|
return &NotifyBlockAddedRequestMessage{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NotifyBlockAddedResponseMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type NotifyBlockAddedResponseMessage struct {
|
||||||
|
baseMessage
|
||||||
|
Error *RPCError
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *NotifyBlockAddedResponseMessage) Command() MessageCommand {
|
||||||
|
return CmdNotifyBlockAddedResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewNotifyBlockAddedResponseMessage returns a instance of the message
|
||||||
|
func NewNotifyBlockAddedResponseMessage() *NotifyBlockAddedResponseMessage {
|
||||||
|
return &NotifyBlockAddedResponseMessage{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// BlockAddedNotificationMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type BlockAddedNotificationMessage struct {
|
||||||
|
baseMessage
|
||||||
|
Block *RPCBlock
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *BlockAddedNotificationMessage) Command() MessageCommand {
|
||||||
|
return CmdBlockAddedNotificationMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewBlockAddedNotificationMessage returns a instance of the message
|
||||||
|
func NewBlockAddedNotificationMessage(block *RPCBlock) *BlockAddedNotificationMessage {
|
||||||
|
return &BlockAddedNotificationMessage{
|
||||||
|
Block: block,
|
||||||
|
}
|
||||||
|
}
|
||||||
72
app/appmessage/rpc_notify_finality_conflicts.go
Normal file
72
app/appmessage/rpc_notify_finality_conflicts.go
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// NotifyFinalityConflictsRequestMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type NotifyFinalityConflictsRequestMessage struct {
|
||||||
|
baseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *NotifyFinalityConflictsRequestMessage) Command() MessageCommand {
|
||||||
|
return CmdNotifyFinalityConflictsRequestMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewNotifyFinalityConflictsRequestMessage returns a instance of the message
|
||||||
|
func NewNotifyFinalityConflictsRequestMessage() *NotifyFinalityConflictsRequestMessage {
|
||||||
|
return &NotifyFinalityConflictsRequestMessage{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NotifyFinalityConflictsResponseMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type NotifyFinalityConflictsResponseMessage struct {
|
||||||
|
baseMessage
|
||||||
|
Error *RPCError
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *NotifyFinalityConflictsResponseMessage) Command() MessageCommand {
|
||||||
|
return CmdNotifyFinalityConflictsResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewNotifyFinalityConflictsResponseMessage returns a instance of the message
|
||||||
|
func NewNotifyFinalityConflictsResponseMessage() *NotifyFinalityConflictsResponseMessage {
|
||||||
|
return &NotifyFinalityConflictsResponseMessage{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// FinalityConflictNotificationMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type FinalityConflictNotificationMessage struct {
|
||||||
|
baseMessage
|
||||||
|
ViolatingBlockHash string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *FinalityConflictNotificationMessage) Command() MessageCommand {
|
||||||
|
return CmdFinalityConflictNotificationMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewFinalityConflictNotificationMessage returns a instance of the message
|
||||||
|
func NewFinalityConflictNotificationMessage(violatingBlockHash string) *FinalityConflictNotificationMessage {
|
||||||
|
return &FinalityConflictNotificationMessage{
|
||||||
|
ViolatingBlockHash: violatingBlockHash,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// FinalityConflictResolvedNotificationMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type FinalityConflictResolvedNotificationMessage struct {
|
||||||
|
baseMessage
|
||||||
|
FinalityBlockHash string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *FinalityConflictResolvedNotificationMessage) Command() MessageCommand {
|
||||||
|
return CmdFinalityConflictResolvedNotificationMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewFinalityConflictResolvedNotificationMessage returns a instance of the message
|
||||||
|
func NewFinalityConflictResolvedNotificationMessage(finalityBlockHash string) *FinalityConflictResolvedNotificationMessage {
|
||||||
|
return &FinalityConflictResolvedNotificationMessage{
|
||||||
|
FinalityBlockHash: finalityBlockHash,
|
||||||
|
}
|
||||||
|
}
|
||||||
83
app/appmessage/rpc_notify_pruning_point_utxo_set_override.go
Normal file
83
app/appmessage/rpc_notify_pruning_point_utxo_set_override.go
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
package appmessage
|
||||||
|
|
||||||
|
// NotifyPruningPointUTXOSetOverrideRequestMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type NotifyPruningPointUTXOSetOverrideRequestMessage struct {
|
||||||
|
baseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *NotifyPruningPointUTXOSetOverrideRequestMessage) Command() MessageCommand {
|
||||||
|
return CmdNotifyPruningPointUTXOSetOverrideRequestMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewNotifyPruningPointUTXOSetOverrideRequestMessage returns a instance of the message
|
||||||
|
func NewNotifyPruningPointUTXOSetOverrideRequestMessage() *NotifyPruningPointUTXOSetOverrideRequestMessage {
|
||||||
|
return &NotifyPruningPointUTXOSetOverrideRequestMessage{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NotifyPruningPointUTXOSetOverrideResponseMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type NotifyPruningPointUTXOSetOverrideResponseMessage struct {
|
||||||
|
baseMessage
|
||||||
|
Error *RPCError
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *NotifyPruningPointUTXOSetOverrideResponseMessage) Command() MessageCommand {
|
||||||
|
return CmdNotifyPruningPointUTXOSetOverrideResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewNotifyPruningPointUTXOSetOverrideResponseMessage returns a instance of the message
|
||||||
|
func NewNotifyPruningPointUTXOSetOverrideResponseMessage() *NotifyPruningPointUTXOSetOverrideResponseMessage {
|
||||||
|
return &NotifyPruningPointUTXOSetOverrideResponseMessage{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// PruningPointUTXOSetOverrideNotificationMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type PruningPointUTXOSetOverrideNotificationMessage struct {
|
||||||
|
baseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *PruningPointUTXOSetOverrideNotificationMessage) Command() MessageCommand {
|
||||||
|
return CmdPruningPointUTXOSetOverrideNotificationMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewPruningPointUTXOSetOverrideNotificationMessage returns a instance of the message
|
||||||
|
func NewPruningPointUTXOSetOverrideNotificationMessage() *PruningPointUTXOSetOverrideNotificationMessage {
|
||||||
|
return &PruningPointUTXOSetOverrideNotificationMessage{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// StopNotifyingPruningPointUTXOSetOverrideRequestMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type StopNotifyingPruningPointUTXOSetOverrideRequestMessage struct {
|
||||||
|
baseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *StopNotifyingPruningPointUTXOSetOverrideRequestMessage) Command() MessageCommand {
|
||||||
|
return CmdNotifyPruningPointUTXOSetOverrideRequestMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewStopNotifyingPruningPointUTXOSetOverrideRequestMessage returns a instance of the message
|
||||||
|
func NewStopNotifyingPruningPointUTXOSetOverrideRequestMessage() *StopNotifyingPruningPointUTXOSetOverrideRequestMessage {
|
||||||
|
return &StopNotifyingPruningPointUTXOSetOverrideRequestMessage{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// StopNotifyingPruningPointUTXOSetOverrideResponseMessage is an appmessage corresponding to
|
||||||
|
// its respective RPC message
|
||||||
|
type StopNotifyingPruningPointUTXOSetOverrideResponseMessage struct {
|
||||||
|
baseMessage
|
||||||
|
Error *RPCError
|
||||||
|
}
|
||||||
|
|
||||||
|
// Command returns the protocol command string for the message
|
||||||
|
func (msg *StopNotifyingPruningPointUTXOSetOverrideResponseMessage) Command() MessageCommand {
|
||||||
|
return CmdNotifyPruningPointUTXOSetOverrideResponseMessage
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewStopNotifyingPruningPointUTXOSetOverrideResponseMessage returns a instance of the message
|
||||||
|
func NewStopNotifyingPruningPointUTXOSetOverrideResponseMessage() *StopNotifyingPruningPointUTXOSetOverrideResponseMessage {
|
||||||
|
return &StopNotifyingPruningPointUTXOSetOverrideResponseMessage{}
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user