BlockChain
BlockChain - P2P
보람-
2024. 1. 23. 17:55
** P2P 네트워크
- client와 server의 코드를 같이 작성하는 것 (둘다 작동되는 서버)
- 프로그램을 작성할 때 clinet코드와 server코드를 같이 구현한다는 뜻!
- front에서 작성했던 socket코드, back에서 작성했던 socket코드가 같이 구현되는것
** 블록체인 P2P
- node net 라이브러리 사용했던것처럼 사용, http, ws, tcp 사용함
- node
- http : express
- ws : node의 피어 전달해서 사용
- 클라이언트
- 블록 추가(마이닝 → 롱기스트 체인룰 검증)
- 전체 블록 조회
- 마지막 블록 조회
import { P2P } from './server/p2p';
import Chain from './chain/chain';
import { Client } from './server/client';
import { MessageType } from './server/interface/message.interface';
const chain = new Chain();
const clinet = new Client(chain, 8080);
const p2p = new P2P(chain);
p2p.listen(8080);
clinet.connect(MessageType.addBlock, ['TX01', 'TX02']);
P2P SERVER ON PORT : 8080
[+] new socket! ::ffff:127.0.0.1 : 54681
000100010001000110011011101110100010001010101010011001110011000111011100000000001100111000100001110111001000101110111000010001010101011000100011111111011101111000100010001000101010101010101011011101100010000110011000000000100010000101010100010001011101111001100111011101101110111101110110011001011100000111
{"type":"addBlock","payload":[{"version":"1.0.0","height":0,"timestamp":1705974551727,"hash":"0000000000000000000000000000000000000000000000000000000000000000","previousHash":"0000000000000000000000000000000000000000000000000000000000000000","merkleRoot":"0000000000000000000000000000000000000000000000000000000000000000","diffculty":0,"nonce":0,"data":["tx01"]},{"version":"1.0.0","timestamp":1705974551843,"height":1,"previousHash":"0000000000000000000000000000000000000000000000000000000000000000","merkleRoot":"42E95CCCE4E92A6C7C9F4DB9193BF7E693383E4DBE407B218C630EFBBE643DB8","hash":"923b1cb5d15f9e9464c61e70be446e8a9c703872e158f788aad86085179dbd97","nonce":1,"diffculty":0,"data":["TX01","TX02"]}]}
socket end!!!
[-] socket end! ::ffff:127.0.0.1 : 54681