Thisprojectaimstocreateasimpleimplementationofblockchainconceptanddemostrateitinauserfriendlyway.
DesignConceptThisprojectconsistsoftwomainparts:agentandinterface.
AgentAnagentstandsforonepeerwhichisabletostoreandmineblocksinthenetwork.EveryagentisconnectedtoalltheotheragentsinthenetworktoconstructaP2Pdistributednetwork.Thebasicfunctionsforanagentare:
Sendmessagetootheragents,inordertobroadcastitsnewlyminedblockReceivemessagefromotheragents,inordertoreceiveblocksminedbyotheragentsMine,validateandgrowblocksonitsownbloackchainSynclatestblockchainwithotheragentsThealgorithmforminingisthekeyofablockchain.InthisprojectweonlyuseSHA256hashtosimulatetheminingprocedure.
InterfaceAninterfaceimplementedwithSpringbootisincludedinthisprojecttodemostratetheusageoftheblockchain.Itmightmakepeoplefeellikeacentralizedmanagementinterface,howeverweneedtounderstandthatagentscanalsorunindependently.TheinterfaceisRESTfulandallreturndataisinjsonformat.Asinglepageapplicationisalsoprovidedtovisualizetheblockchainconceptinabetterway.
QuickStartStartserverNavigatetoprojectrootdirandstarttheserver:
$gradlebootRunUsewebinterfaceOpenhttps://localhost:8080/inbrowserandtryitfromwebpage.Basicactionsare:
AddanagenttothenetworkDeleteanagentfromthenetworkMineanewblockandbroadcasttothenetwork.Acolorschemeisusedtomarkdifferentblockscreatedbydifferentagents.UserestinterfaceUsecurldirectlyfromcommandlinetointeractwiththeserver:
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.
评论