当前位置: 首页 > 软件库 > Web3 > 区块链 >

Blockchain

A simple implementation of blockchain in java
授权协议 Apache-2.0 License
开发语言 JavaScript
所属分类 Web3、 区块链
软件类型 开源软件
地区 不详
投 递 者 楚举
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

A simple implementation of blockchain

This project aims to create a simple implementation of blockchain concept and demostrate it in a user friendly way.

Design Concept

This project consists of two main parts: agent and interface.

Agent

An agent stands for one peer which is able to store and mine blocks in the network. Every agent is connected to all the other agents in the network to construct a P2P distributed network. The basic functions for an agent are:

  1. Send message to other agents, in order to broadcast its newly mined block
  2. Receive message from other agents, in order to receive blocks mined by other agents
  3. Mine, validate and grow blocks on its own bloackchain
  4. Sync latest blockchain with other agents

The algorithm for mining is the key of a blockchain. In this project we only use SHA256 hash to simulate the mining procedure.

Interface

An interface implemented with Springboot is included in this project to demostrate the usage of the blockchain. It might make people feel like a centralized management interface, however we need to understand that agents can also run independently. The interface is RESTful and all return data is in json format. A single page application is also provided to visualize the blockchain concept in a better way.

Quick Start

Start server

Navigate to project root dir and start the server:

$ gradle bootRun

Use web interface

Open http://localhost:8080/ in browser and try it from web page. Basic actions are:

  • Add an agent to the network
  • Delete an agent from the network
  • Mine a new block and broadcast to the network. A color scheme is used to mark different blocks created by different agents.

block chain demo

Use rest interface

Use curl directly from command line to interact with the server:

Create new agent

curl -X POST "http://localhost:8080/agent?name=A1&port=1001"
{"name":"A1","port":1001,"blockchain":[{"index":0,"timestamp":1502193341671,"hash":"4f99b67b06b6831886815ffe66a55be2e34dcefdfc16b6214710313062a8a480","previousHash":"ROOT_HASH"}]}

curl -X POST "http://localhost:8080/agent?name=A2&port=1002"
{"name":"A2","port":1002,"blockchain":[{"index":0,"timestamp":1502193341671,"hash":"4f99b67b06b6831886815ffe66a55be2e34dcefdfc16b6214710313062a8a480","previousHash":"ROOT_HASH"}]}

curl -X POST "http://localhost:8080/agent?name=A3&port=1003"
{"name":"A3","port":1003,"blockchain":[{"index":0,"timestamp":1502193341671,"hash":"4f99b67b06b6831886815ffe66a55be2e34dcefdfc16b6214710313062a8a480","previousHash":"ROOT_HASH"}]}

Mine block

curl -X POST "http://localhost:8080/agent/mine?agent=A1"
{"index":1,"timestamp":1502194172250,"hash":"2461f27f811df15a969391c70f136869a282224e8cc6fe8b628d16a499515d21","previousHash":"4f99b67b06b6831886815ffe66a55be2e34dcefdfc16b6214710313062a8a480"}

curl -X POST "http://localhost:8080/mine?name=A3"
{"timestamp":1502194200235,"status":404,"error":"Not Found","message":"No message available","path":"/mine"}

Show agents and blocks

curl http://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"}]}

curl http://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"}]}

curl http://localhost:8080/agent/all

Remove agent

curl -X DELETE http://localhost:8080/agent/all

Change log

  • 20170902 Let newly added agent sync the latest blockchain from peers.
  • 20170818 Add a web interface.
  • 20170807 Server side code base with rest interface.

This project is inspired by naivechain.

  • 今天为大家汇总一下区块链专业术语,以防聊天时不知道对方在说什么。 1.区块链(BlockChain)         区块链是一个信息技术领域的术语。从本质上讲,它是一个共享数据库,存储于其中的数据或信息,具有“不可伪造”、“全程留痕”、“可以追溯”、“公开透明”、“集体维护”等特征。基于这些特征,区块链技术奠定了坚实的“信任“基础,创造了可靠的“合作”机制,具有广阔的运用前景。 2.区块(Blo

相关阅读

相关文章

相关问答

相关文档