多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 1、创建一个钱包 ``` ./demon wallet new ``` ``` (windows下:.\demon.exe wallet listaddress) ``` 创建成功后,会在\tmp目录下创建mywallet.data文件 ## 列出钱包中的钱包地址 一个钱包集中可能有多个钱包 ``` 命令: .\demon.exe wallet listaddress 命令执行结果: 18q67cbMjpDr569zy3h1NsiGPNy7xAzdSZ 1P5Bf2VWMZcH1zErW5tecFhRm65bVaPXU5 12kH2rS5NNJUMHkGbPkX5U6aVN6mNCQ7Dj 在这里,将18q67cbMjpDr569zy3h1NsiGPNy7xAzdSZ作为挖矿钱包奖励地址,1P5Bf2VWMZcH1zErW5tecFhRm65bVaPXU5为初始化区块链的钱包地址(奖励将给到这个地址,同时,在send命令执行参数mine=true的情况下,奖励也将给到这个地址)。12kH2rS5NNJUMHkGbPkX5U6aVN6mNCQ7Dj为普通钱包地址。 ``` 上面列表显示了钱包集中有两个钱包地址。 每次执行命令: ``` ./demon wallet new ``` 都会在mywallet中追加一个钱包。 # 2、通过创建创始区块创建新的区块链 为了运行区块链的多个实例,我们使用--instanceid标志创建区块链。随后我们在访问该区块链的信息的时候,都需要带上该标志。 ``` 命令: ./demon init --address 1P5Bf2VWMZcH1zErW5tecFhRm65bVaPXU5 --instanceid 1000 命令执行结果: badger 2022/01/07 21:59:18 INFO: All 0 tables opened in 0s INFO[07 Jan 22 21:59 CST] No existing blockchain found INFO[07 Jan 22 21:59 CST] Target: 800000000000000000000000000000000000000000000000000000000000000 3299d8d598b65dfa04dd922e790976d1aed03a13cd431e756cc15da92e6ea9af 0aa71d60668c2c9cf749a8e3d9ebc63831eeafea7f3661522c249733d3049e17 79ef5cab41984b6eae11063aa939ea793852f4a444fed9902071a6f77658ee54 42590ec6ef463b97a39363a613acd79744b80a09ab7bf8b8f2bf82f8fc07ab49 e1f4d037fea23ad7fb89397ac5b22a43a0f5031471399ef4885d990f5a2967a6 dbc4477481eacdb24056e42ed014915cbaa87c3b268d9802215e37ce8bc6922f 0d5dd056269f1e13520f533c087144ed1b68358388b32373e851e43850af6d17 20b171fd63546955b3e7d11e9f725e3fa1d6b757a0e46ae32815b5b4c44f8eb7 2a5fcb21348a69f6ff497daeead8236a3b9c9347af01284e5a26b9a5b658f9c8 c4c26d1cf43b843cae000b075a62059e640f5127df97168a62b63af144456207 a5f55ece9974f61697b9e1559b291c8d3f4f72358fe84a5475ec6c4f22abdbb3 7a165434fc659e2f87fe0d51d1950d018236dd49847b44d207a8bc7fa8f44ef0 d2f0d2993aa9349cac1cad0cb5341362c7ae15415ff4317e5bdce1cd3b158144 43c6fe1711c5a53ff8c277ec557ae85916fbf17913fe2b874f3c72d3aca6ed31 4a13130ba1645575b69354625e921dda7085654f027c299f8c83e37177a6f941 8bef4d8c5588e95ff8e7b34412885bb92aec5a376188df29c62116430cc8b2b0 2583a6e55b2e510dc018322dffdee1bf463d4c20603cea6894b49eaa394ed818 f17cf29a6ce5c9e97813d17bc6325e4e984da34e71afe14aecc0e3fe6620ba28 c15ec62c509c6c0a1831f200efd4d36af6940955a3f15d4feaad491271eb6e37 cc62e08871fe8a46c0ee86f288c21d7f7bb179cc052cd5ce74fb3b37624b1692 349fea689e8ca1bf96afdc38e4aa4036e37594870e75e9d523fe25aa26b97eb7 cec88a30e6101db2f5e7acfe6f8154ef5a9466b3146bf6b085f47d6b2a6bf253 9db1778dbde2d58cd4a8a8eecd0ace4dc60e7e867799d170cd119427a69c0166 aa738012ee6ae319c63004f1e6f0b18e410c8387a73494ec86197daefbb2874c 17d10bd1ec0539f9b9d7ef1405c4d870454ae0ceca80dd41f87898099e13b3b5 031c46c005ad9a87a016dc00894104f1f752da5c9ad5929645b4ad21f2b4c653 INFO[07 Jan 22 21:59 CST] 找到! INFO[07 Jan 22 21:59 CST] 初始化区块链成功 badger 2022/01/07 21:59:18 DEBUG: Storing value log head: {Fid:0 Len:42 Offset:977} badger 2022/01/07 21:59:18 INFO: Got compaction priority: {level:0 score:1.73 dropPrefixes:[]} badger 2022/01/07 21:59:18 INFO: Running for level: 0 badger 2022/01/07 21:59:18 DEBUG: LOG Compact. Added 4 keys. Skipped 0 keys. Iteration took: 0s badger 2022/01/07 21:59:18 DEBUG: Discard stats: map[] badger 2022/01/07 21:59:18 INFO: LOG Compact 0->1, del 1 tables, add 1 tables, took 8.0005ms badger 2022/01/07 21:59:18 INFO: Compaction for level: 0 DONE badger 2022/01/07 21:59:18 INFO: Force compaction on level 0 done ``` 创建的区块链数据库文件结构: ![](https://img.kancloud.cn/34/15/34156c6bde97971d2e07954367b3dbc9_290x199.png) # 3、查询余额 ``` 命令 ./demon wallet balance --address 1P5Bf2VWMZcH1zErW5tecFhRm65bVaPXU5 --instanceid 1000 命令执行结果: badger 2022/01/07 22:00:19 INFO: All 1 tables opened in 1ms badger 2022/01/07 22:00:19 INFO: Replaying file id: 0 at offset: 1019 badger 2022/01/07 22:00:19 INFO: Replay took: 0s badger 2022/01/07 22:00:19 DEBUG: Value log discard stats empty INFO[07 Jan 22 22:00 CST] 1P5Bf2VWMZcH1zErW5tecFhRm65bVaPXU5的余额是:20.000000 badger 2022/01/07 22:00:19 INFO: Got compaction priority: {level:0 score:1.73 dropPrefixes:[]} 从结果看,1P5Bf2VWMZcH1zErW5tecFhRm65bVaPXU5钱包地址奖励了20个代币 ``` # 4、运行节点 (注)上面以instanceid=1000创建区块链,instanceid=2000 (1)创建1个交易,从一个钱包地址1P5Bf2VWMZcH1zErW5tecFhRm65bVaPXU5,给钱包地址12kH2rS5NNJUMHkGbPkX5U6aVN6mNCQ7Dj发10个代币。 命令(mine=true,发送者自行挖矿): ``` ./demon send --sendfrom 1P5Bf2VWMZcH1zErW5tecFhRm65bVaPXU5 --sendto 18q67cbMjpDr569zy3h1NsiGPNy7xAzdSZ --amount 10 --mine true --instanceid 1000 ``` 命令执行结果: ``` badger 2022/01/07 22:26:24 INFO: All 1 tables opened in 0s badger 2022/01/07 22:26:24 INFO: Replaying file id: 0 at offset: 2690 badger 2022/01/07 22:26:24 INFO: Replay took: 0s badger 2022/01/07 22:26:24 DEBUG: Value log discard stats empty INFO[07 Jan 22 22:26 CST] 交易已执行 INFO[07 Jan 22 22:26 CST] Target: 800000000000000000000000000000000000000000000000000000000000000 94b538db1ac7c410c9bbdc622fadf731e60cef81c2adaead369f243b23748316 3265080b99dba9277053a6267e3c4a3e6d43dbddf2066e26306acc0c192cdde6 bf8ba43024903af44dcaee855e74b1edb832a473f7443f2685aa054fbe09019f 65275458d620567cbee4702f2ba62d73b2b2fe256c384e7560fc36234ed41bd6 c8a137e4f5bf2ab3d165dd191dff44544608e511bbf8477fc25a9f49cb551b99 e6f2adb9e5db0af6e8032f06201f172349d49fb5a7a83fc00556ed53a8e2d01f 193c9f5b8fe24b64e7f96601eeb655f52841e955f5de5f2bde4bc25183983a1a 7e4b7261ef32f8ff982acc1abc4dcea4e1121ffe423e50a63f7835f4a1dd1bdb 481ee2375401f05d4a5a0093bbaa7f509c9b249b7a9a97750c083c0371fb2657 669c150150975c9ec4ba1ea0a9a0bab501fb73b60c2bc89c771327106f9d69d0 232254294d278dcfb7432ff2ea6bc8ea6710545a8768a4f9102964e7ef98ea06 d92eedfeba46de349d7324327ec7e91e4e84f41d48e3c602ffadf3e07469f19d f08446e526e9ced7b2eb9ea6e3539ce603d21814efbd9bc0446cf6e92b7fd80f 95f94e90d6f980dd5b16c989af1edc312e4dee56b7d2d279eeff2b8f862a3ec8 6301ade02977f7dd9e65a7bb8341809f95cfa4b9beb7ee8ef272cff3d6d0e011 3b18fa6caead8e35ae339c1a7fd622f4a5c9466d0034cf285fe1563a9b40c2c8 60199869f8bc76d759a0a174360be7d550615defef61ff0232551726434ceb01 bb61a8155dd0f5b879136f29026200e176b4ef3522746528dabe837015ce7d6e c9ee0f4c3e9396e23d49e8be2ec009a3e1d3fc49295c1f765c543918f945e49e 74f39e37bbb7a7a847d370c7780a7b005af0f20abb60b3e814ed4a0b5f7c522e 5faf9faa92d7aea7b9807a4ea05f0d22f09da4042c81f3795c788e0633865e7d ab33673211fd64a7ba473617efd9236f2e3d59c3bdb2eaf2731e8067c874b61c 615bdba124d468c14183a0988679422aac7b08d44fe7fb4b999a145d7c5a512b 2d9e84e1bd83502131070cae34de9b300c6852448956fc9d15ba52cd6989cae3 9b48756619ccbeb10bd4b0aa0a81b970c3e1814b562639e495551d6d8baec2b2 c8be337cece91c79fefeb4c1cea0699f44c82f5fde8cb85497d3c7659616de80 fc86b4e91801079aaff076a9dec8b5b4d2b0e006847b7547575f61941526fad4 1399074286fef732a924380f973a18e9681eb437c02191da20911b0f9d07aa0d 5e58418390b58d4d225abdcebb9a81b5b90fabbb8cf95641ffa944936b248d15 db3bd9994ba30b410f00d243fdc0b86a9a21c8063eef6bfaf5dae006a08e4f04 e091651b8a4c869a06eee1c4849fcf12ad34aa9e278c584c0163e283b7afd936 6f876688db9f7d3048d8beefa812da36680bbda7b87c42546f869f6177dc8160 d1f3f597738c0b77bcc3ff8382432a14088167cb8a26492749e2009874c03b6a de5592ea53e8f7369d99eb281ec9e1211069290da5fc01f982760046ee81fdcb 2c9a085306566a6284729ec36b06c1b369e8e8dd20abe7ee064ad3c1f52af7b6 80b83a846a8d5f67b0aa1e6a6f0fbebf06083b01aea3be2d8c8d3cc923739654 ce6e080dceb20219862793d737cf356589e6cadc78370103f022e8f76a628a17 356db09813a60aaaa97809770f361052e61104bb5e0cbe2dacbcbe1e4015b8fc 44509022859cb0001a11b77ecca95ff7525d82df78ee4fcb3a113ce6ec1b669a b19bfbe36fc02e588641544fab3bc06c7552f13d8e392cd49c6a928f8b49676f 6683691a334988de00d075d36c8ef9f61bc2fceeacb5738de8d6841a98f485fa 9d80a797b840aaec91d0f931df87bc2874014fe72f9be63fb70cc20d0f18b685 cdf226b1bf94920dbad41daf507ae6326f166c537b61e597729a63d2dc83353d 7bb91a354ec2fa94a9fc047490843969911337bdb11dbe09a8509c91f49bfda9 e002747ec9d1ab32dd84da5fe08de72b8fc8a9ca62c2ee0144f757f51a6f4e65 797f60b058494e84ff9cec4101cdf2b408687d9d83e7a74c28b23e70f7f81a74 14ed96d66ec55d527976968d4605a30a70238f57d62770a1fd0f2f1c8fd5d902 a4efaacafced094eda6f226df7a4a230dddca70197952064a4c4d793e252a75a 2bf70fca2ecbd4070ae7c07e9f662d7012ff8c7905df847603d678f5943e818b 67d1dc8b3399a343ce900ed3be4f542f6440680bee6d0ea2cc4be932fcbab061 5a6be0adaff964639ae01e06a5e9407e93cd93c75c6f27aa150dfae50a96bbff 09d6344380ac9c72fa4fba1a7eaef72197005ce78b90bed0fc86dd192bff2864 16c588fd751ab1bb308fcd72266572c5b6c0a987c19a52f2457576656d53f539 0a537ad51174f985809fee1a762383a9498b3ede9b91c92010563c4f76bfb266 dff787474fc2af1fc0d0c0d1e93659f9d6c698204c0f4312db6dbd6d7c6a3335 4e7b364eb15342ea77b84ab8667ef486f57e603d51a4a6ab97ed3b73ef006352 9a3eed2346df8504943b3c2cd9e8022a2d3265fd7d6ea49c6d35b4cfb90bcc1d a7f3aaf409ce68624c2e8c45d5edd3d50e3afff41b9c6f4e350bba79dad80347 97602c38c34d660e8c3ffd5b68fb526f985fa10364bcabf9c36e9cfe40e9399b 0e67899942764b7d2480a68545eb9373278f79746bf140dfb80911f0aeca9cfe 06461bf9543512193c8a2da53f3c5c39a04701a2779bd7df99edcc8bff5c4d8c INFO[07 Jan 22 22:26 CST] 找到! badger 2022/01/07 22:26:24 DEBUG: Storing value log head: {Fid:0 Len:42 Offset:4319} badger 2022/01/07 22:26:24 INFO: Got compaction priority: {level:0 score:1.73 dropPrefixes:[]} badger 2022/01/07 22:26:24 INFO: Running for level: 0 badger 2022/01/07 22:26:24 DEBUG: LOG Compact. Added 11 keys. Skipped 4 keys. Iteration took: 0s badger 2022/01/07 22:26:24 DEBUG: Discard stats: map[0:286] badger 2022/01/07 22:26:24 INFO: LOG Compact 0->1, del 2 tables, add 1 tables, took 9.0005ms badger 2022/01/07 22:26:24 INFO: Compaction for level: 0 DONE badger 2022/01/07 22:26:24 INFO: Force compaction on level 0 done ``` (2)再次查两个钱包地址的余额 查询钱包地址1P5Bf2VWMZcH1zErW5tecFhRm65bVaPXU5的余额命令: ``` ./demon wallet balance --address 1P5Bf2VWMZcH1zErW5tecFhRm65bVaPXU5 --instanceid 1000 ``` 命令执行结果: ``` badger 2022/01/07 22:23:54 INFO: All 1 tables opened in 1ms badger 2022/01/07 22:23:54 INFO: Replaying file id: 0 at offset: 2690 badger 2022/01/07 22:23:54 INFO: Replay took: 0s badger 2022/01/07 22:23:54 DEBUG: Value log discard stats empty INFO[07 Jan 22 22:23 CST] 1P5Bf2VWMZcH1zErW5tecFhRm65bVaPXU5的余额是:30.000000 badger 2022/01/07 22:23:54 INFO: Got compaction priority: {level:0 score:1.73 dropPrefixes:[]} ``` 查询钱包地址18q67cbMjpDr569zy3h1NsiGPNy7xAzdSZ余额命令: ``` ./demon wallet balance --address 18q67cbMjpDr569zy3h1NsiGPNy7xAzdSZ --instanceid 1000 ``` 从两个命令执行结果可以看到,发送代币后,sendfrom钱包的余额为:20(初始化区块链获得)-10(发送出去的)+20(sendfrom自行挖矿奖励的)=30,sendto钱包的余额是10,与预期一致。 # 5、打印区块链信息 ``` 命令: ./demon print --instanceid 1000 ``` 命令执行结果: ``` badger 2022/01/07 22:55:10 INFO: All 1 tables opened in 1ms badger 2022/01/07 22:55:10 INFO: Replaying file id: 0 at offset: 4361 badger 2022/01/07 22:55:10 INFO: Replay took: 0s badger 2022/01/07 22:55:10 DEBUG: Value log discard stats empty PrevHash: 0467ec73857597a0bd49e4301bb4b217fa49cdefab17f2ddcd5258b156717a64 Hash: 06461bf9543512193c8a2da53f3c5c39a04701a2779bd7df99edcc8bff5c4d8c Height: 3 INFO[07 Jan 22 22:55 CST] Target: 800000000000000000000000000000000000000000000000000000000000000 Valid: true ---Transaction: 21d9908ba9a577404f081ada356c7873f59283541c0df742093ea64c7dd1214c Input (0): TXID: Out: -1 Signature: PubKey: 383031633532306434343634346531323436613862343161313563376266393763343031333138313938343132653765 Output (0): Value: 20.000000 PubkeyHash: f21d53717bdd1da99333c64feacaf0eb900f40b8 ---Transaction: e85fec2424f932287edbe6499a77ba48176e4301f10d751799d373bc8101755b Input (0): TXID: 6b8adc524dc0ef47983923e1f6a10ce98a228a79a2d1caa35d4110752f65d19a Out: 0 Signature: 630cd44ca0dc0f4d71d40914a641d037db4c1547f8bf7e278e6ae14e3d0436a5e4415a0a15a4521963051cbc5f8039375cfaeb77669d8ee154f0f51f9df752cd PubKey: 5442e000a05be3b57d7f26358d9176acc727b6927844e011ee2a80d834fae7c3626ec57edee56a0dd278c898a0f45b82403694d3b90501cd1a31d8356f8723ca Output (0): Value: 10.000000 PubkeyHash: 55e11d99d3a3b22967ef08a6ccb407b447a005b6 Output (1): Value: 10.000000 PubkeyHash: f21d53717bdd1da99333c64feacaf0eb900f40b8 PrevHash: 031c46c005ad9a87a016dc00894104f1f752da5c9ad5929645b4ad21f2b4c653 Hash: 0467ec73857597a0bd49e4301bb4b217fa49cdefab17f2ddcd5258b156717a64 Height: 2 INFO[07 Jan 22 22:55 CST] Target: 800000000000000000000000000000000000000000000000000000000000000 Valid: true ---Transaction: 6b8adc524dc0ef47983923e1f6a10ce98a228a79a2d1caa35d4110752f65d19a Input (0): TXID: Out: -1 Signature: PubKey: 663666383234393537663632646365666234356534386635353136666563363637663264373061663561643532613931 Output (0): Value: 20.000000 PubkeyHash: f21d53717bdd1da99333c64feacaf0eb900f40b8 ---Transaction: d6eec0c72a4b6924460d6ab20d45880b8971ce89263c6061d72b3e435d52225f Input (0): TXID: 0967e4dd375b7ccf8b6ce37f9c0281b1e1225b844882b394aef499c818f87e8f Out: 0 Signature: 77c835cf6ed1e0a39bc6bb9a9cfa84831a027813410c686494a0ee6e062693ccaf9350b86735a023d8fcfaa6ca01426090535948304699b3bd2f2306326f6dd8 PubKey: 5442e000a05be3b57d7f26358d9176acc727b6927844e011ee2a80d834fae7c3626ec57edee56a0dd278c898a0f45b82403694d3b90501cd1a31d8356f8723ca Output (0): Value: 10.000000 PubkeyHash: 55e11d99d3a3b22967ef08a6ccb407b447a005b6 Output (1): Value: 10.000000 PubkeyHash: f21d53717bdd1da99333c64feacaf0eb900f40b8 PrevHash: Hash: 031c46c005ad9a87a016dc00894104f1f752da5c9ad5929645b4ad21f2b4c653 Height: 1 INFO[07 Jan 22 22:55 CST] Target: 800000000000000000000000000000000000000000000000000000000000000 Valid: true ---Transaction: 0967e4dd375b7ccf8b6ce37f9c0281b1e1225b844882b394aef499c818f87e8f Input (0): TXID: Out: -1 Signature: PubKey: 67656e65736973 Output (0): Value: 20.000000 PubkeyHash: f21d53717bdd1da99333c64feacaf0eb900f40b8 badger 2022/01/07 22:55:10 INFO: Got compaction priority: {level:0 score:1.73 dropPrefixes:[]} ``` 上面有5个transaction,其中主动发起的交易有2个,都来自于以下命令(完全相同的命令,包括参数在内): ``` ./demon send --sendfrom 1P5Bf2VWMZcH1zErW5tecFhRm65bVaPXU5 --sendto 18q67cbMjpDr569zy3h1NsiGPNy7xAzdSZ --amount 10 --mine true --instanceid 1000 ``` 另外3个都是挖矿奖励的交易,第1个挖矿奖励交易(也是全部交易的第一个交易)是区块链初始化的挖矿奖励交易,后面交替的两个,是两次主动发起且参数mine=true、由sendfrom自行完成挖矿产生的挖矿奖励交易。 ## 6、启动挖矿节点 为了在单机上模拟多台物理节点,这里使用instanceid作为多个节点的区分。实际应用部署的时候,每台服务器使用的instanceid均是1000(数据库名称为blocks_1000),也可以不设置它(数据库名称为blocks)。 挖矿节点使用instanceid=1000。blocks_1000数据库已经存在(通过上面初创区块链命令创建,后面作为创始区块链对待,每个实际的物理节点部署区块链时候,必须将它打包在内,一并部署到实际的物理节点)。 ## 7、启动全节点 全节点使用instanceid=2000,拷贝已经存在的数据库(作为创始区块链),重命名为blocks_2000,然后执行启动全节点的命令。 ## 8、启动普通节点 普通节点使用instanceid=3000,拷贝已经存在的数据库(作为创始区块链),重命名为blocks_3000,然后再执行启动普通节点的命令。