250 lines
4.4 KiB
Go
250 lines
4.4 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/gob"
|
|
"fmt"
|
|
"io"
|
|
"io/ioutil"
|
|
"log"
|
|
"net"
|
|
)
|
|
|
|
const protocol = "tcp"
|
|
const dnsNodeID = "3000"
|
|
const nodeVersion = 1
|
|
const commandLength = 12
|
|
|
|
var nodeAddress string
|
|
var knownNodes []string
|
|
|
|
type addr struct {
|
|
AddrList []string
|
|
}
|
|
|
|
type getblocks struct {
|
|
AddrFrom string
|
|
}
|
|
|
|
type inv struct {
|
|
Type string
|
|
Items [][]byte
|
|
}
|
|
|
|
type verack struct {
|
|
}
|
|
|
|
type verzion struct {
|
|
Version int
|
|
|
|
AddrFrom string
|
|
}
|
|
|
|
func commandToBytes(command string) []byte {
|
|
var bytes [commandLength]byte
|
|
|
|
for i, c := range command {
|
|
bytes[i] = byte(c)
|
|
}
|
|
|
|
return bytes[:]
|
|
}
|
|
|
|
func bytesToCommand(bytes []byte) string {
|
|
var command []byte
|
|
|
|
for _, b := range bytes {
|
|
if b != 0x0 {
|
|
command = append(command, b)
|
|
}
|
|
}
|
|
|
|
return fmt.Sprintf("%s", command)
|
|
}
|
|
|
|
func extractCommand(request []byte) []byte {
|
|
return request[:commandLength]
|
|
}
|
|
|
|
func requestBlocks() {
|
|
for _, node := range knownNodes {
|
|
sendGetBlocks(node)
|
|
}
|
|
}
|
|
|
|
func sendAddr(address string) {
|
|
nodes := addr{knownNodes}
|
|
nodes.AddrList = append(nodes.AddrList, nodeAddress)
|
|
payload := gobEncode(nodes)
|
|
request := append(commandToBytes("addr"), payload...)
|
|
|
|
sendData(address, request)
|
|
}
|
|
|
|
func sendData(addr string, data []byte) {
|
|
conn, err := net.Dial(protocol, addr)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
defer conn.Close()
|
|
|
|
_, err = io.Copy(conn, bytes.NewReader(data))
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
}
|
|
|
|
func sendInv(address, kind string, items [][]byte) {
|
|
inventory := inv{kind, items}
|
|
payload := gobEncode(inventory)
|
|
request := append(commandToBytes("inv"), payload...)
|
|
|
|
sendData(address, request)
|
|
}
|
|
|
|
func sendGetBlocks(address string) {
|
|
payload := gobEncode(getblocks{nodeAddress})
|
|
request := append(commandToBytes("getblocks"), payload...)
|
|
|
|
sendData(address, request)
|
|
}
|
|
|
|
func sendVersion(addr string) {
|
|
payload := gobEncode(verzion{nodeVersion, nodeAddress})
|
|
|
|
request := append(commandToBytes("version"), payload...)
|
|
|
|
sendData(addr, request)
|
|
}
|
|
|
|
func sendVrack(addr string) {
|
|
payload := gobEncode(verack{})
|
|
|
|
request := append(commandToBytes("verack"), payload...)
|
|
|
|
sendData(addr, request)
|
|
}
|
|
|
|
func handleAddr(request []byte) {
|
|
var buff bytes.Buffer
|
|
var payload addr
|
|
|
|
buff.Write(request[commandLength:])
|
|
dec := gob.NewDecoder(&buff)
|
|
err := dec.Decode(&payload)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
knownNodes = append(knownNodes, payload.AddrList...)
|
|
fmt.Printf("There are %d known nodes now!\n", len(knownNodes))
|
|
requestBlocks()
|
|
}
|
|
|
|
func handleInv(request []byte) {
|
|
var buff bytes.Buffer
|
|
var payload inv
|
|
|
|
buff.Write(request[commandLength:])
|
|
dec := gob.NewDecoder(&buff)
|
|
err := dec.Decode(&payload)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
fmt.Printf("Recevied %d %s\n", len(payload.Items), payload.Type)
|
|
}
|
|
|
|
func handleGetBlocks(request []byte, bc *Blockchain) {
|
|
var buff bytes.Buffer
|
|
var payload getblocks
|
|
|
|
buff.Write(request[commandLength:])
|
|
dec := gob.NewDecoder(&buff)
|
|
err := dec.Decode(&payload)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
blocks := bc.GetBlockHashes()
|
|
sendInv(payload.AddrFrom, "blocks", blocks)
|
|
}
|
|
|
|
func handleVersion(request []byte) {
|
|
var buff bytes.Buffer
|
|
var payload verzion
|
|
|
|
buff.Write(request[commandLength:])
|
|
dec := gob.NewDecoder(&buff)
|
|
err := dec.Decode(&payload)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
sendVrack(payload.AddrFrom)
|
|
sendAddr(payload.AddrFrom)
|
|
knownNodes = append(knownNodes, payload.AddrFrom)
|
|
}
|
|
|
|
func handleConnection(conn net.Conn, bc *Blockchain) {
|
|
request, err := ioutil.ReadAll(conn)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
command := bytesToCommand(request[:commandLength])
|
|
fmt.Printf("Received %s command\n", command)
|
|
|
|
switch command {
|
|
case "addr":
|
|
handleAddr(request)
|
|
case "inv":
|
|
handleInv(request)
|
|
case "getblocks":
|
|
handleGetBlocks(request, bc)
|
|
case "version":
|
|
handleVersion(request)
|
|
case "verack":
|
|
//
|
|
default:
|
|
fmt.Println("Unknown command!")
|
|
}
|
|
|
|
conn.Close()
|
|
}
|
|
|
|
// StartServer starts a node
|
|
func StartServer(nodeID string) {
|
|
nodeAddress = fmt.Sprintf("localhost:%s", nodeID)
|
|
ln, err := net.Listen(protocol, nodeAddress)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
defer ln.Close()
|
|
|
|
if nodeID != dnsNodeID {
|
|
sendVersion(fmt.Sprintf("localhost:%s", dnsNodeID))
|
|
}
|
|
|
|
bc := NewBlockchain(nodeID)
|
|
|
|
for {
|
|
conn, err := ln.Accept()
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
go handleConnection(conn, bc)
|
|
}
|
|
}
|
|
|
|
func gobEncode(data interface{}) []byte {
|
|
var buff bytes.Buffer
|
|
|
|
enc := gob.NewEncoder(&buff)
|
|
err := enc.Encode(data)
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
return buff.Bytes()
|
|
}
|