Blockchain A simple implementation of blockchain i

我要开发同款
匿名用户2021年11月30日
52阅读
开发技术JavaScript
所属分类Web3、区块链
授权协议Apache-2.0 License

作品详情

Asimpleimplementationofblockchain

Thisprojectaimstocreateasimpleimplementationofblockchainconceptanddemostrateitinauserfriendlyway.

DesignConcept

Thisprojectconsistsoftwomainparts:agentandinterface.

Agent

Anagentstandsforonepeerwhichisabletostoreandmineblocksinthenetwork.EveryagentisconnectedtoalltheotheragentsinthenetworktoconstructaP2Pdistributednetwork.Thebasicfunctionsforanagentare:

Sendmessagetootheragents,inordertobroadcastitsnewlyminedblockReceivemessagefromotheragents,inordertoreceiveblocksminedbyotheragentsMine,validateandgrowblocksonitsownbloackchainSynclatestblockchainwithotheragents

Thealgorithmforminingisthekeyofablockchain.InthisprojectweonlyuseSHA256hashtosimulatetheminingprocedure.

Interface

AninterfaceimplementedwithSpringbootisincludedinthisprojecttodemostratetheusageoftheblockchain.Itmightmakepeoplefeellikeacentralizedmanagementinterface,howeverweneedtounderstandthatagentscanalsorunindependently.TheinterfaceisRESTfulandallreturndataisinjsonformat.Asinglepageapplicationisalsoprovidedtovisualizetheblockchainconceptinabetterway.

QuickStartStartserver

Navigatetoprojectrootdirandstarttheserver:

$gradlebootRunUsewebinterface

Openhttps://localhost:8080/inbrowserandtryitfromwebpage.Basicactionsare:

AddanagenttothenetworkDeleteanagentfromthenetworkMineanewblockandbroadcasttothenetwork.Acolorschemeisusedtomarkdifferentblockscreatedbydifferentagents.

Userestinterface

Usecurldirectlyfromcommandlinetointeractwiththeserver:

Createnewagentcurl-XPOST"https://localhost:8080/agent?name=A1&port=1001"{"name":"A1","port":1001,"blockchain":[{"index":0,"timestamp":1502193341671,"hash":"4f99b67b06b6831886815ffe66a55be2e34dcefdfc16b6214710313062a8a480","previousHash":"ROOT_HASH"}]}curl-XPOST"https://localhost:8080/agent?name=A2&port=1002"{"name":"A2","port":1002,"blockchain":[{"index":0,"timestamp":1502193341671,"hash":"4f99b67b06b6831886815ffe66a55be2e34dcefdfc16b6214710313062a8a480","previousHash":"ROOT_HASH"}]}curl-XPOST"https://localhost:8080/agent?name=A3&port=1003"{"name":"A3","port":1003,"blockchain":[{"index":0,"timestamp":1502193341671,"hash":"4f99b67b06b6831886815ffe66a55be2e34dcefdfc16b6214710313062a8a480","previousHash":"ROOT_HASH"}]}Mineblockcurl-XPOST"https://localhost:8080/agent/mine?agent=A1"{"index":1,"timestamp":1502194172250,"hash":"2461f27f811df15a969391c70f136869a282224e8cc6fe8b628d16a499515d21","previousHash":"4f99b67b06b6831886815ffe66a55be2e34dcefdfc16b6214710313062a8a480"}curl-XPOST"https://localhost:8080/mine?name=A3"{"timestamp":1502194200235,"status":404,"error":"NotFound","message":"Nomessageavailable","path":"/mine"}Showagentsandblockscurlhttps://localhost:8080/agent?name=A1{"name":"A1","port":1001,"blockchain":[{"index":0,"timestamp":1502193341671,"hash":"4f99b67b06b6831886815ffe66a55be2e34dcefdfc16b6214710313062a8a480","previousHash":"ROOT_HASH"},{"index":1,"timestamp":1502194172250,"hash":"2461f27f811df15a969391c70f136869a282224e8cc6fe8b628d16a499515d21","previousHash":"4f99b67b06b6831886815ffe66a55be2e34dcefdfc16b6214710313062a8a480"}]}curlhttps://localhost:8080/agent?name=A3{"name":"A3","port":1003,"blockchain":[{"index":0,"timestamp":1502193341671,"hash":"4f99b67b06b6831886815ffe66a55be2e34dcefdfc16b6214710313062a8a480","previousHash":"ROOT_HASH"},{"index":1,"timestamp":1502194172250,"hash":"2461f27f811df15a969391c70f136869a282224e8cc6fe8b628d16a499515d21","previousHash":"4f99b67b06b6831886815ffe66a55be2e34dcefdfc16b6214710313062a8a480"}]}curlhttps://localhost:8080/agent/allRemoveagentcurl-XDELETEhttps://localhost:8080/agent/allChangelog20170902Letnewlyaddedagentsyncthelatestblockchainfrompeers.20170818Addawebinterface.20170807Serversidecodebasewithrestinterface.

Thisprojectisinspiredbynaivechain.

声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论