├── block
├── p2pNode.cpp
├── blockChain.cpp
├── cryptography.cpp
├── threadPool.cpp
├── Makefile
├── cryptography.hpp
├── p2pNode.hpp
├── threadPool.hpp
├── blockChain.hpp
└── main.cpp
├── holePunching
├── server.cpp
└── Makefile
├── README.md
├── Makefile
├── LICENSE
└── include
└── p2pServer.hpp
/block/p2pNode.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangn1989/shacoin/HEAD/block/p2pNode.cpp
--------------------------------------------------------------------------------
/block/blockChain.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangn1989/shacoin/HEAD/block/blockChain.cpp
--------------------------------------------------------------------------------
/block/cryptography.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangn1989/shacoin/HEAD/block/cryptography.cpp
--------------------------------------------------------------------------------
/block/threadPool.cpp:
--------------------------------------------------------------------------------
1 | #include "threadPool.hpp"
2 |
3 | namespace ShaCoin
4 | {
5 |
6 | }
--------------------------------------------------------------------------------
/holePunching/server.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangn1989/shacoin/HEAD/holePunching/server.cpp
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # shacoin
2 | C++从零开始实现的一个简单的基于区块链技术的电子货币记账系统,主要用来学习区块链的底层原理,相关博文[戳这里](https://blog.csdn.net/mumufan05/category_9292513.html)
3 | 还有一个以太坊智能合约的学习笔记,代码比较简单没有上传,看一下博文就够了,相关博文[戳这里](https://blog.csdn.net/mumufan05/article/category/8049023)
4 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | DIRS = block holePunching
2 |
3 | all: subdirs
4 |
5 | .PHONY: subdirs clean
6 |
7 | subdirs: $(DIRS)
8 | for dir in $(DIRS); do make -C $$dir; done
9 |
10 | clean:
11 | @echo $(DIRS)
12 | for dir in $(DIRS); do make clean -C $$dir; done
13 |
--------------------------------------------------------------------------------
/block/Makefile:
--------------------------------------------------------------------------------
1 | CC = g++
2 | LINK = g++
3 | CFLAGS = -g -Wall -rdynamic -I../include
4 | LFLAGS = -lcrypto -lssl -pthread
5 | LIBS =
6 |
7 | SRC_DIR = .
8 | SFIX = .cpp
9 |
10 | SOURCES := $(foreach x,${SRC_DIR},\
11 | $(wildcard \
12 | $(addprefix ${x}/*,${SFIX}) ) )
13 | #SOURCES = $(wildcard *.cpp)
14 | OBJECTS = $(patsubst %.cpp, %.o, $(SOURCES))
15 | TARGET = shacoin
16 |
17 | first: all
18 |
19 | %.o: %.cpp
20 | $(CC) -c $(CFLAGS) -o $@ $<
21 |
22 | all: $(TARGET)
23 |
24 | $(TARGET): $(OBJECTS)
25 | @echo $(TARGET)
26 | $(LINK) $(LIBS) $(OBJECTS) $(CFLAGS) $(LFLAGS) -o ../$(TARGET)
27 |
28 | .PHONY: clean
29 |
30 | clean:
31 | rm -f $(OBJECTS) ../$(TARGET)
32 |
--------------------------------------------------------------------------------
/holePunching/Makefile:
--------------------------------------------------------------------------------
1 | CC = g++
2 | LINK = g++
3 | CFLAGS = -g -Wall -rdynamic -I../include
4 | LFLAGS = -lcrypto -lssl
5 | LIBS =
6 |
7 | SRC_DIR = .
8 | SFIX = .cpp
9 |
10 | SOURCES := $(foreach x,${SRC_DIR},\
11 | $(wildcard \
12 | $(addprefix ${x}/*,${SFIX}) ) )
13 | #SOURCES = $(wildcard *.cpp)
14 | OBJECTS = $(patsubst %.cpp, %.o, $(SOURCES))
15 | TARGET = p2pServer
16 |
17 | first: all
18 |
19 | %.o: %.cpp
20 | $(CC) -c $(CFLAGS) $(LFLAGS) -o $@ $<
21 |
22 | all: $(TARGET)
23 |
24 | $(TARGET): $(OBJECTS)
25 | @echo $(TARGET)
26 | $(LINK) $(LIBS) $(OBJECTS) $(CFLAGS) $(LFLAGS) -o ../$(TARGET)
27 |
28 | .PHONY: clean
29 |
30 | clean:
31 | rm -f $(OBJECTS) ../$(TARGET)
32 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 zhangn1989
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/include/p2pServer.hpp:
--------------------------------------------------------------------------------
1 | #ifndef __P2PSERVER_H
2 | #define __P2PSERVER_H
3 |
4 | #include
5 | #include
6 | #include
7 |
8 | #include
9 | #include
10 |
11 | namespace ShaCoin
12 | {
13 | #define ALITEST 0
14 |
15 | #if ALITEST
16 | #define SERVERIP "47.94.162.207"
17 | #define LOCALPORT 10000
18 | //#define IF_NAME "eth0"
19 | #else //1
20 | #define SERVERIP "192.168.180.133"
21 | #define LOCALPORT 20000
22 | //#define IF_NAME "ens33"
23 | #endif //1
24 |
25 | #define SERVERPORT 9527
26 |
27 | typedef enum
28 | {
29 | cmd_register = 0x1000,
30 | cmd_unregister,
31 | cmd_getnode,
32 | cmd_max
33 | } Command;
34 |
35 | typedef struct st_node
36 | {
37 | int count;
38 | char queryIp[16];
39 | int queryPort;
40 | char recvIp[16];
41 | int recvPort;
42 |
43 | bool operator == (const struct st_node & value) const
44 | {
45 | return
46 | this->count == value.count &&
47 | this->queryPort == value.queryPort &&
48 | this->recvPort == value.recvPort &&
49 | !strcmp(this->queryIp, value.queryIp) &&
50 | !strcmp(this->recvIp, value.recvIp);
51 | }
52 | } __attribute__((packed))
53 | Node;
54 |
55 | typedef struct st_nodeinfo
56 | {
57 | Command cmd;
58 | Node node;
59 | } __attribute__((packed))
60 | NodeInfo;
61 | }
62 | #endif // __P2PSERVER_H
--------------------------------------------------------------------------------
/block/cryptography.hpp:
--------------------------------------------------------------------------------
1 | #ifndef __CRYPTOGRAPHY_H
2 | #define __CRYPTOGRAPHY_H
3 |
4 | #include
5 | #include
6 |
7 | namespace ShaCoin
8 | {
9 | typedef struct __keydata
10 | {
11 | size_t len;
12 | unsigned char key[256];
13 | } KeyData;
14 |
15 | typedef struct __KeyPair
16 | {
17 | KeyData pubKey;
18 | KeyData priKey;
19 | } KeyPair;
20 |
21 | class Cryptography
22 | {
23 | public:
24 | static std::string GetHash(void const* buffer, std::size_t len);
25 | static std::string Base64Encode(const void*buff, int len);
26 | static void Base64Decode(const std::string &str64, void *outbuff, size_t outsize, size_t *outlen);
27 | static void Createkey(KeyPair &keyPair);
28 | static bool Signature(const KeyData &priKey, const void *data, int datalen, unsigned char *sign, size_t signszie, unsigned int *signlen);
29 | static int Verify(const KeyData &pubkey, const char *data, int datalen, const unsigned char *sign, size_t signszie, unsigned int signlen);
30 | static std::string StringToLower(const std::string &str);
31 | static bool CompareNoCase(const std::string &strA, const std::string &strB);
32 | static std::vector StringSplit(const std::string &str, const char sep);
33 |
34 | protected:
35 | Cryptography();
36 | virtual ~Cryptography();
37 |
38 | private:
39 |
40 | };
41 | }
42 |
43 | #endif //__CRYPTOGRAPHY_H
44 |
--------------------------------------------------------------------------------
/block/p2pNode.hpp:
--------------------------------------------------------------------------------
1 | #ifndef __P2PNODE_H
2 | #define __P2PNODE_H
3 |
4 | #include