Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
justcode740 committed Apr 11, 2024
1 parent fef7ceb commit 84414db
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 116 deletions.
43 changes: 43 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
secp256k1 = "0.29.0"
sha2 = "0.10.8"

hex = "0.4.3"
num-bigint = "0.4.4"
204 changes: 102 additions & 102 deletions output.txt
Original file line number Diff line number Diff line change
@@ -1,103 +1,103 @@
BlockHeader { version: 1, previous_block_hash: "", merkle_root: "", time: 0, bits: 0, nonce: 35022 }
{"version":4,"previous_block_hash":"","merkle_root":"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855","time":1712867678,"bits":520159231,"nonce":67699}
{"version":1,"locktime":0,"vin":[{"txid":"","vout":4294967295,"prevout":{"scriptpubkey":"","scriptpubkey_asm":"","scriptpubkey_type":"coinbase","scriptpubkey_address":"","value":0},"scriptsig":"arbitrary data","scriptsig_asm":"","witness":null,"is_coinbase":true,"sequence":4294967295}],"vout":[{"scriptpubkey":"miner's address or script","scriptpubkey_asm":"","scriptpubkey_type":"pay to script hash or pay to public key hash","scriptpubkey_address":"miner's address","value":6250616824}]}
fd6c51917e9dcef4b36f06a97c45fc012b32954c2606b29ea46fbf24627180b2
f61ba1f5c008032ec0c6226935e41c0906e18768182253dabab18fb414977d79
b03607c000e41a2c35c3d67f8410274428e02b9fc323b8c5d88013f5c36adaf2
b69ebd3e9354c7197e66e8994b0d736bf72471952325ec48ae8fd99f234c7486
c29f43f417fd8f7f4e6c49c9909ff05a21a7f860fe337aec08ab2210c3df0bca
c9ffefcac1150a79dca59e54218a4e2267987193210c3154c57e7e3c152e9f4f
5b6a68bcdeecc661d5e4762fbdd10bb2ab46850fa9a3e7fae8fc925797c30349
e5dadb7cf200305d9a32270707f1e28792e7e59cafd412025a629d99d8fa8c74
e8df4626764e473b45dc2f17cac9ccf1abcaefe14ace65c5a8b36dfe5bc1d52b
1da98f44f9799a7afd4f4733745403797a3ea9560a181e8bf35d33a7ec84f383
921e498ba907d5821d47579619da62b42d4e73f1c5fcf5fe30b8c11a2196a8c8
d21374704def04b718ef13b90597cc5eed31a85f2d5e38cdece8f3b067081195
6ee77c8e124b843ceb7ccdb1988bb9808b39323c0aaf72585b1f849200044c31
20668f91b715e308fdd8f6dba0027fa309b01651f54109f0541d8ee9763c260d
c9dc384fd54a9b7d9f9ee214be33d3dcf5b63ae7a959d30efbc5c805868ccb98
ed4935165003a09098aea1296f11d4166ab5cf6e50d7489e0f82a08660f7cc8a
8989ed84ee8a37609e4b8aa16aa49ba8daf8885aea56f85d1672bb258e2fd1f2
c58b9bbdad53334a763df2bd609e49a587c8cb7fb284424ce8543b878fdda8d1
fc418b1aec8fa42583e87794f16e6c346bb660c51e46212a824e8bd0d79ac5de
9ab68801d474c278928d0dd11e0abccd3a8653b9b30a5185feb05a43e14283f2
323e4f3ae31a1f934b7ba0494393402b0e9c6e040a57da9bdcce857402493717
f29043c4e2144d4e6b71e728ca6f7cb3718f52e5bd40a438edcbc5e41255becd
de8e5af87d677bf5e4f1dfbe145f16fb922752cb3650986c125ac67d2171b722
abedc0cfc2071f03c46265708132d267a4396cf77c5f99066c398b4aea711c90
7999c95635436a2f6833fecb8f7bf9a700c1ef042cb9ded6317afbc1b0a0cb94
233cb4f1be5aadc6ed2c9d5a3aca55fe790e1a77e6a7be736eb78da5e94cfc9d
1b9e7d37d4f2a5fdc38682d680e36502e4eb983d35bc7060ca871b66607958b3
0ef0a023b634d42f6065d6fa6ffa65c59c0bf3fd80cd245dccf6491774f372a8
a15ad6d104a37b8209ce7c054b8d985fc9254777f20e0fa9117e2f803da6f798
1ff66ac7cce05dd73ba2b3dbc3efea124759582e643a53b6ff14e939b077b0bc
3bcde627e009d88f7c90e6a7c41a473c63df225b5b351b31572e9a3116164418
1b8794ac391616ce39ee36d27ac2c7a0fa6f59c86d2770abf4787477b224c8a7
5240468a06cff25050e66ea7eb29b9fb3a202a69c4a0aa29ed026ca395ae531a
8ea7bbecc3fdc9217f51d596056226f21558f31fdcf339aea72a3adcaa1a0354
65de04a24879dd627414b942117eac52f9ce3e588357b7b48c9e2cceeaa10020
1245a76885888a1f83996a310c5e8338cc213fdd00fef669d19846251a3dd9a9
8bb9cc3fae73b5d6dedc305b24767fa5219bdfbf42a26682b174283dc532ee1f
88a33ef8a1fe8e5b8a0ab2d2bb92c43b8d328a34be4c7af85b1b371fe32ea05b
dd89640609e9d61132ea3fc31683b39d19aa6eefbe88ce6d7f6466a7475d9286
5e1cb0b51d84b243ee144961e4d89e28ab1c2bcb74f9b8edf778f1b7ca9a0051
3325fd0f39d2dbfcf1af7c189fcf8185204883dde64d48f35cc4ef280a9fed63
1ad66f8eda77b2ceb26cb9e744a0c2db4ec49f7dab1d12fd7ddeb1048d1e8f94
fd54b1a1b24e6bdd651dbf0cac4dbf0c66fad9863179277257ce5debe67baf2e
6550df27a673cc8a39d6e0525e742a9d3d51cec7b5839fcbd7bc3763464561a2
0fc7e1dca82d1e5e09ccc1de37f8146bf9c71612fa7f8eb015594fb1976af7de
6b287dbfe2d4c8d82a570f7234981a62a476a257fe2f675228d513fe3b1171f7
395d4b7d04db6ea40b9a9f227c6c8ea36797912dd988b4c3e2ecb16f89d4cd44
e2adf87c5fe805e62645e6c95fa79f3955d17a68bc85422e07e484e5e7ead201
f0d95f67d006510afe4f596a4d9a434d0d972857a989edd713af33dbe338cce5
54f134f03552bb71aa8e29949710f8fa53f041838b20b041573f3ac077e1446b
394eba71a6c97104ba52a3e593a9de0ee72a8081cce0dc9ad15120cc0c54af22
4ab31bba7fe1a0e49f319eb7456d560330680203116cc108cdd3a1adedcba2f6
a72c35285d0b157ba1a10aefa54fb093658217daebfc6395896dd5263e116514
ebcecdf8a1d9aab3e1c1aafc5b33336cfa8b5970f672c6494694447fe90630a9
1a03612f53738cab1a31e84c934572b8e6521512231c97a2c56bf18dccb40624
e2dbe2c3b1be040ccb1f2787165d59784cbb329e9d2751311f892a35a4832eae
da87057bfb3bb32c0c5a90c85cf329f82059134cd014259bfb6b2cd73aa32fd4
b49d68458ebf1ef0d9f6361d5458d973e7c74eee74ce1f6a7135d52600b17444
37fc115f78757a7878fd1c8b1a0096f515afb4ad2a6e3d64da1cfa4925b8bb6c
0b4e8ce425e7f1f4d93d0216cbdf0d036b9f29a759f6bd4dd1d0ed4798093bb4
d6164d3110ce40e78244d11976b3238a24b19d6db4011bae7e1f58c83e5b555f
e466d26c4bf9c467f2d3d7f5f4e994e3ca3e7117a7e6c134b406e418f608c18f
6dd4d4439ff6ea50722e4f7309a0f3e60ddf361a483b5b7cac0e80b582ee484e
20ac37ddd19b6ab3412f24b480584ea59903bad3a9245466f1ad41f4921d68cf
dfb908de8bf20c672a2512038f5a681c056c50e4c17bc615ab0b4aef9e5b9efd
c90f1f60fec4b71dbf58355f91237c77a761b7a40be8ea8464fa732707058245
cd83b145ba40b14843ff5fc816e106e3c744df0a01d9d1fd2f1bf33af61f6cf6
ca4426c4d51b0b3a07518b3897836b0270b9fc5d33a1c56792e96dc2af4611cf
797328fde30e4868c95cf26c8d209d8ae9d1bc4b81ce5779bb63b4b2f37b87c4
9a43a49242e9f0757afd059988b25d941da898625c703ede3a91e83b3da98def
c85e3a02297694ca971b5318c9e503bc18bc7aec8b2f1ed9c471e9356b330acb
82ac019ce9e671a02b5191afd8365d90e79dd9e5c93dfe9d94c311bc60c4023c
e347236eefec653d30e2d938d240289a867f5c247b8a9e52bfd36220668b2812
42e9b2be6a03a0b67cb9df59824dc7afccdac0f99b8d68fbe5558aabcc0d6e5f
dd8df538a8d70e0928a4857ca07baeaee128dffe4f60cea0a0358261e946ef15
a9abb01a4f6ba8c4132fdd15464400c71acd917656bfa5d18a669e06f959b437
a59d7855abf3721e7dfab2473b76dfa7ddfca873bec1b681e7a1bd0311ca0f3a
9326485a16a912c7c78cc18caa8ca357f1305fbe98ce49e345e7ba063a008767
9dec9675da7db73484e1b316a4b82c8f02b8988f59982b6261f31b6db2dfbbd6
efdd8ab778aaaa50b80632815a28c44d8ca30349b76ae894cf892ee311c49175
c1c533466cc4499c293c93350ed6809a8a9bd8bd10f3176327b7a9d8253d0773
84c9e1abdacf151cf6848e25a9a8c568122fd6fa5e5d6f43e5583b585d2ccaac
2b8f5fb8ce910609fc6e5b72e58e1b7fa65e58c5eccdb67e4e038d2423ded6fd
f7f6cd7bef68c5b7887ee8b9ecc42dbba89d7483022dff7953aced44ddfbac2a
b297e1616f3f554bdde09c375498ab793d4587053fb9e8623a867cde9988a265
827192f8870bb35a4319827778844666fee36c62d3db6cc4e74095ddde6bf0d3
0b1f2f3f33f6a8c553236045d05eb17799299c25e95b625e2fd58b0b42fa2d4f
9bb6b6df0dcfa78c163fc9a32b8951d7d427bb9a32d00a923628ed7c5106c5dc
55145fa5bcf5ece7d038dab44bc7d50f77684be936de75ce5aed5c9a19c1923b
26efae64a334f0c1fe74cc069138bc5c25129e4469df0cb22a81076040674f67
e1c9ed9226011da72fffa01c517d32b65dfc73d2583049a349222a1306cc87cf
7d5fb57fef2d28dc55bbddd6b6b6edd2faaefea7d2c510761b05d28ecc557eba
b9bf6b8785f569aaf5a7e77930f1b0da9124103808b30801a5a90f755010226d
bc05f09bc92b926b9e605178d1341fadbcdb8ce0766ea87bc4ea0a0c6b30eaee
5131a48061a1b95e208dca9bf03e457209c49e8bd0115c6cea1d5a41da79e3df
43763e32d64f917007033677bef1f7fe768421c48c5459635b11aff19c71fed4
de3e74676f63474069deb4e3276eaf98e4ae9297ae2f451248f7a966c97cbe36
d24484c3dc35542d2f4d4c862e61126b29f7563411608f399fd4ddc45f76b7e9
492f69d73f2b16a00c516ae9fa1b27ae4f1c60681d96d1b5964c59c2122d9f3a
d7c9dc1d6ff72609f2d72c2842732f33201ca02968280c3b41b1c49cbb44b6e6
83a4d16652052c9dee5e50200371960979ba379c29a181a51a13c80fdce449a2
812b802d700089a9bd8860ea096fc36a4f539afa695f5a074ffcc62a8afb6d8d
5f28da0c20bcb1004da92c616e98a508c772767f0f991cd44e90c8e74f019b4b
ac842ed609010cc043bc118219d89b742d6e9201adf7d9a81710734378d3fcab
4aecf2a0331dc8d1404179d7908ad82c4eb28d37254e56756f1e40ea871e6f0c
3f69a7033b494a330e1a61dd47bd4fa462de810278ec9e82021090401e2d9d1a
8a274f7ef6de311ccd9d06dd0987d7cbac0ced74dc3489cdd02ca5ebf64100c1
857111bed24a0410afbc2bdb620d1c4d23b6baec8022a15976e028cc7de153f1
7ca14d62f4eec620dacfb142cb8fde789082e5febce9297138c9bbd415006da8
b16c4ef532c799620a56ec44054a4ce0f1895dc3c49c4bc90e445012e5a3679d
80db29cd345c047414d25e0d5180220fbcb00cb538afaa99beb6a19cc44f45c4
6dbd28434b1564e0d4b386d428c6cf903b98e0356043b5337be0511a18ed213c
130a835719cca437b97d7fc938df6cd24ae3e8a44bc872afccbe01f6855ccc12
b0a9e7aee90b8bbb222cb9912e0b3bc0767983c0f415c42a4c5acc708678ba59
8556a44223ba6ffb6e74221e670e9a436bc66188fe510f73047e8f792866c3ed
55a99deebbfd81d5c7c6be2b207e713ce1716b2690c18821c8a841a0b36d17d9
480b4961009cddfcd7ee3b52433bc06073e6d9756089ed9f95ecc95510d46a14
846d0777a9d9fd89123d7ae6c813f3578512fc3e4cb77d79861bb54376db52b1
8ed9b57994e8b7a8201de65e56848de18c26b0585261bb892bc43963c789a790
2d8b832af08f8b6712cccb4ff197ef812720b5dbfd06333d9b234cde4fc451cb
376a3874f498b6ca6e2a4853379a53fbee0700fab17f358c36034ad651e9ae32
fae9961192db94a36c33a013ff5a827929df231794c785c9c3494b894d27b3cb
b345188389d02434a224aad7f0054b360c902589635860a6036563c1b562a681
8d4cc43c3d3b32247947a80068c9d0e859748430196c9ec89cf922b93fcaad2b
3ab21440157e2528fba1eb8dd11ebc162decf002a45fc01143255080f95de4e0
b9d016bc4ba5ff4a43c0944dc5227b0418045db0f7f14ee7ea0b1d7a54c43f44
1f5bc276f60f6b6041675427c89dd77a931dd4a308c782e1cac370f60d08b4fc
0ecbef8fa7e80f189d343cb43edf8a51e1a5d687ceace9fdd43f45ce940efab1
8f415bbd483339fda1e580243a15c01b2aac347af2b951d44b172e1304a2f62a
d425492e74cdfb79cc3caa85b4218339217ee29d28aee8e238ee2938c4f8fcf4
ac4a2bdb75d53ab83d1ce0784174847411b52546ebe667d1ef08336484eeaeac
ca7c618cf1e4f950fad1486c6f5f6b8eca879a2b6ec4fd73a7871874ec86d502
9447b7ddbff8827c5c5f656c39302a93b27da9e2e6cac89355ad83bdea05754a
c258366f4109983f754a0051e67e9578f8993526f517dc3bc70a0cb8f92d2121
a4ec5ab467fcec3f2b55768ddb4e39f439bc396795b9310871051ff58aa35b6f
a38204470156f644e0d4d88004e0834c8f50c6e41c0ddf87052946ff7ae7e870
f0eac6e0cb5540d0f43deffca807eed349e8d2815d89c0b0be3ec9f0f35f58fc
20408ef1807b14c3dbecd85f83fdc97a68b16f0b13b867cd21896e1dbd79cc33
b3bc00caf883c7d7b79c3ac46398e9c5a33d82e3e88b343ddbec69f0b5ff7c43
cdc3618937ffb93919531945ec24aaad49ae67e8252176f2934da81221796b48
2cb6754c908697db519777b08968017bf15c6f8571b2cba193984213e7daef3b
6f8bbeb53659edda67f0750dabf0caebfbfcc1f93e496c3d2e1ee17bee47b9dd
d3b03f6752a0b76bcf9628ce2ae612a237c0f0d3879456c620998030134f4f53
87f11a88e633890e9b16f155ef2c3fb082a9cb9568b433076f329fb59a0961b4
18b3a601dde276d55e714ea8522fc241b98351ed30070168af5f166e1d5d0ab0
1c4af816067f4c3eb4ae9c88a0773eacaae96b651712d9b2ec79ddc4a9119995
d708149ecbfd3200ea2dbbf4b9cdc25d22e4c6dd8121855dfd5e282835bb6de4
66615f385f658eafde6d7c42f1aa6d4d0c8a0af0edf62e5109f27483a29e2b4f
e306dcf31c8e096b237853268b6acfabf531e3b2570e4ec31699a3175140d1d4
59d8e439cfadcb9020246ca9db55fe371d12230a52782c385f6a459ab3a503a4
0783641716485412357d77bde1501ff6d75311157084cd96c86956347238d096
2db2fbc58096973a4c18bf355a25b2506fd9cde70504aa3f58ced8357c3f3091
d83b246906eb64b13e25a0b96b0bbdc1859058d69f78013f3adbec3025ba20ca
6c0cb2e64a41889692d01c553f7dcfb42821fe820adc746f3de3054a8a77355e
01a5170e4b15218dcf6df9d1faa65f9f8e5e93e8cf7c375ce6cd0d93ee34a1bb
5d88ca9b8720f6f031745d01139c6f44e530b023a82b265344cbb968ff2a0eeb
261e9cf018d41c47409db9fb36284539092bc8735addcfaa948d06d9c74f76e3
756e9cfaa1848823ee7753d72ef222ef8b35331a1a559abf1ccaea81400bb3ea
d6b0277a7ce54f49940ea6bc66368931384f7b105c96d4008edc4ed88e344eb6
fd8902c332c9a98f6b14e21a8bb6d08ebcbed3881b5fc399f81069be068213a4
539ba3a25b505d3f785009680a101cb5ea7d518fea73dc7cbe3feaa09c46e13b
545090c2b62f98e9ddcc63bbb1ab8b2eea471ef28699180ee7da223746dc35c2
963fa1b59e3ba26362e9d5cb9643b632b67babc6448214789c5b18ed5fe9a490
4d9ed25cddad91bca5497fc6c05f540de347f9afeb0bc4308cffe71e821fe8ca
b3a097e795927a1cc8b5c1e0358b2020e61dfd2d6e5b911f82b619ae728b7fdb
22d273c3eacadd3cc9e9849b3a5b637d316fb84189bf7d36a0a584a402a00f3c
a08674fdda39815394b53baddffc036ae4889c040a315b96b0a21decbc2b2592
6a2360025f90bfdffd244c6aee93fba27e9d75e2d55e3cc1ec391a73e386c11f
6c9fc7238529aa206507e4954040e768dc589479994afee00eb2622e5eb5d52a
45e544efa0bb3e6f930548f30ad6b6d3c2bb301114e26b1efd1f8b680d1a22c1
0663ec9add0589fbca3f29172d6128feca777ada5c4e75ae440cc0b446ef1908
383310ee2c8ec10fa34d4a0c4ee75a154883abd2fa666037ef490e2fdde0fa5b
8dce6de1e3832c873b55153c710c1a93b1d6f48565935d1174eaa5e8322d3053
e6bfe61bee3b4512d32de13059288c461205cac3d97f1996b25ce860f9e07465
b22d2d0b15c1e3b6727d1dba819173fa1eaccd7fa73531f2a00651b76460dfaa
2a3d24355fa0c4276e62248164c43b96aa186cce4a1a8ec9c735ecb4bdfdc52c
b1d9ee0c8b79baf5915d4266c98da744a4655165d7b9086e06ae458faf80dc4c
3337964829646794bf556557a2021facba73a0f17b184f86715e674e3ef3f932
aeade79dd3027f1b3d401f60942c73f595948934c7b1cf6f3711e95f0e4f6a9d
13c378cb796fb8e52f70422e3868cff0ae7c340d7a07d7c4c74634a695a1dbad
1ace5b9594f379ef4abc98ba026718f961a39881f5472faf28f0561a8f6a9bc2
c42596a8e94b35f3d477f49c096556fddb087f163fc2cf260e797b7286c2c6b0
004e7c1085da793d2de495963df33fa732ac4b2f6b1729dc2a6f66a5078414ef
f0c5ce00b6ae2db631e61f94e14a5bd9848747ac119a585e156af703653a7677
beb6f94a5b68ef8b9554768c1433e5f669ae107fade04c5993fc3ca4182be78a
8cd935826e7a6d57b2e3d8d2085588d0abe6cceed9baa079c51860818ac3e66f
483aba4675f200d4177114205bdcce26929696814f7f816ff10efc9cd1809802
d115efebfaed05793e47f1536fee2ff4daf8b6d845383b26c0d9e488485dc4ff
c029263d900c8e04961d056c314848d010500d609990520d06ae233aeb9bd272
9231a96b923052ad089508a1f11c5eb3d3d6c450a129a774a91abac1d77c86e1
b093a88a2b5340edd0334cd4a298abbe0a485ca232739ece472e28d7e9ed7d77
f41239ed60c9eb3c852ca11d6a8293ab54d9786b59fe70d15f5c717b439aa7eb
28567089aaaad292f9e891fd39e54f2c6b0b277463a78742d2948d0b51db21f7
42c9bde5103f2d81e28ef157956ed05f7f59eac49d03f9641e743c358a0eb5dc
76a9fb9e7bbe62d436daadd4b347bb6d91d6b64bb0de6430076f0e0ffeba42ce
bfb35048e1388fb3c4bed6beb4d726b5cc56304da2d2d7cf1b27b40e3e9a75c9
7dfcde409617ac38100b3c5e57ad8bac649b9ceb79ff7c777c227c643c815b0b
f82b68bcd4cc9d117661aeefa94d3ff50dc538ac7123e5d0381ab66e5cafd1f2
3a709476e8d9a560a2dc34815711493c0ed1a31b3acd0b804a8042dcb2e27e66
40abe3cf06833b58caef17ea9c6d54d34ff9776333f171351a3e4f9e7b7011ac
acaf99ebfaa7326f1b95db619ba937a08fb3bb3c61b8fbaa88730d62400d963f
5f5949e5d1d8db4f5ec39d2f91b85dab84980a1f1a2779eea349b3d81b1ed9e6
48 changes: 35 additions & 13 deletions src/block.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use num_bigint::BigUint;
use serde::{Deserialize, Serialize};
use serde_json;
use sha2::{Digest, Sha256};
use std::{fs::File, io::Write, vec};
use std::{fs::File, io::Write, time::SystemTime, vec};

use crate::tx::Transaction;

Expand All @@ -23,13 +24,30 @@ pub struct Block {

impl Block {
pub fn mine(&mut self, difficulty_target: &str) {
// Set up the header with valid values
self.header.version = 4; // Ensure version is at least 4.
self.header.bits = 0x1f00ffff; // Set bits to specified value.
self.header.time = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs() as u32; // Set time to current timestamp.
self.header.merkle_root =
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855".to_string(); // This should be computed based on transactions.
self.header.previous_block_hash =
"0000000000000000000000000000000000000000000000000000000000000000".to_owned();

let difficulty_bytes =
hex::decode(difficulty_target).expect("Invalid hex in difficulty target");
let difficulty_target = BigUint::from_bytes_be(&difficulty_bytes);

loop {
let header = serde_json::to_string(&self.header).unwrap();
let hash = Sha256::digest(header.as_bytes());
let hash_hex = format!("{:x}", hash);
let header_bytes =
serde_json::to_vec(&self.header).expect("Failed to serialize header");
let hash = Sha256::digest(&Sha256::digest(&header_bytes));
let hash_int = BigUint::from_bytes_be(&hash);

if hash_hex < difficulty_target.to_owned() {
println!("Block mined: {}", hash_hex);
if hash_int < difficulty_target {
println!("Block mined with hash: {:x}", hash);
break;
}

Expand All @@ -38,20 +56,24 @@ impl Block {
}

pub fn generate_output(&self) {
let mut output = File::create("output.txt").unwrap();
let mut output = File::create("output.txt").expect("Failed to create output.txt");

// Write block header
writeln!(output, "{:?}", self.header).unwrap();
let header_str = serde_json::to_string(&self.header).expect("Failed to serialize header");
writeln!(output, "{}", header_str).expect("Failed to write header to file");

// Serialize and write coinbase transaction
let coinbase_tx = serde_json::to_string(&self.transactions[0]).unwrap();
writeln!(output, "{}", coinbase_tx).unwrap();
let coinbase_tx = &self.transactions[0];
let coinbase_tx_str =
serde_json::to_string(coinbase_tx).expect("Failed to serialize coinbase transaction");
writeln!(output, "{}", coinbase_tx_str)
.expect("Failed to write coinbase transaction to file");

// Write transaction IDs
for tx in &self.transactions {
let tx_json = serde_json::to_string(tx).unwrap();
let txid = Sha256::digest(tx_json.as_bytes());
writeln!(output, "{:x}", txid).unwrap();
let tx_str = serde_json::to_string(tx).expect("Failed to serialize transaction");
let txid = Sha256::digest(&Sha256::digest(tx_str.as_bytes()));
writeln!(output, "{:x}", txid).expect("Failed to write txid to file");
}
}
}

0 comments on commit 84414db

Please sign in to comment.