2017-09-30 20:44:41 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-09-30 21:33:33 -05:00
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
2017-10-03 03:47:27 -05:00
|
|
|
"encoding/hex"
|
2017-09-30 20:44:41 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2017-09-30 21:33:33 -05:00
|
|
|
"io/ioutil"
|
2017-09-30 20:44:41 -05:00
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
)
|
|
|
|
|
2017-09-30 21:33:33 -05:00
|
|
|
const protocol = "tcp"
|
|
|
|
const nodeVersion = 1
|
|
|
|
const commandLength = 12
|
|
|
|
|
|
|
|
var nodeAddress string
|
2017-10-03 04:21:15 -05:00
|
|
|
var miningAddress string
|
2017-10-01 09:33:03 -05:00
|
|
|
var knownNodes = []string{"localhost:3000"}
|
|
|
|
var blocksInTransit = [][]byte{}
|
2017-10-03 03:47:27 -05:00
|
|
|
var mempool = make(map[string]Transaction)
|
2017-09-30 21:33:33 -05:00
|
|
|
|
2017-09-30 22:25:11 -05:00
|
|
|
type addr struct {
|
|
|
|
AddrList []string
|
2017-09-30 21:33:33 -05:00
|
|
|
}
|
|
|
|
|
2017-09-30 23:50:27 -05:00
|
|
|
type block struct {
|
|
|
|
AddrFrom string
|
|
|
|
Block []byte
|
|
|
|
}
|
|
|
|
|
2017-09-30 23:19:43 -05:00
|
|
|
type getblocks struct {
|
|
|
|
AddrFrom string
|
|
|
|
}
|
|
|
|
|
2017-09-30 23:50:27 -05:00
|
|
|
type getdata struct {
|
|
|
|
AddrFrom string
|
|
|
|
Type string
|
|
|
|
ID []byte
|
|
|
|
}
|
|
|
|
|
2017-09-30 23:19:43 -05:00
|
|
|
type inv struct {
|
2017-09-30 23:50:27 -05:00
|
|
|
AddrFrom string
|
|
|
|
Type string
|
|
|
|
Items [][]byte
|
2017-09-30 23:19:43 -05:00
|
|
|
}
|
|
|
|
|
2017-10-03 03:47:27 -05:00
|
|
|
type tx struct {
|
|
|
|
AddFrom string
|
|
|
|
Transaction []byte
|
|
|
|
}
|
|
|
|
|
2017-09-30 22:25:11 -05:00
|
|
|
type verzion struct {
|
2017-10-01 09:33:03 -05:00
|
|
|
Version int
|
|
|
|
BestHeight int
|
|
|
|
AddrFrom string
|
2017-09-30 22:25:11 -05:00
|
|
|
}
|
|
|
|
|
2017-09-30 21:33:33 -05:00
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2017-09-30 23:19:43 -05:00
|
|
|
func requestBlocks() {
|
|
|
|
for _, node := range knownNodes {
|
|
|
|
sendGetBlocks(node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-30 22:25:11 -05:00
|
|
|
func sendAddr(address string) {
|
|
|
|
nodes := addr{knownNodes}
|
|
|
|
nodes.AddrList = append(nodes.AddrList, nodeAddress)
|
|
|
|
payload := gobEncode(nodes)
|
|
|
|
request := append(commandToBytes("addr"), payload...)
|
|
|
|
|
|
|
|
sendData(address, request)
|
|
|
|
}
|
|
|
|
|
2017-09-30 23:50:27 -05:00
|
|
|
func sendBlock(addr string, b *Block) {
|
|
|
|
data := block{nodeAddress, b.Serialize()}
|
|
|
|
payload := gobEncode(data)
|
|
|
|
request := append(commandToBytes("block"), payload...)
|
|
|
|
|
|
|
|
sendData(addr, request)
|
|
|
|
}
|
|
|
|
|
2017-09-30 22:08:51 -05:00
|
|
|
func sendData(addr string, data []byte) {
|
|
|
|
conn, err := net.Dial(protocol, addr)
|
2017-09-30 21:33:33 -05:00
|
|
|
if err != nil {
|
2017-10-05 23:30:51 -05:00
|
|
|
fmt.Printf("%s is not available\n", addr)
|
|
|
|
var updatedNodes []string
|
|
|
|
|
|
|
|
for _, node := range knownNodes {
|
|
|
|
if node != addr {
|
|
|
|
updatedNodes = append(updatedNodes, node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
knownNodes = updatedNodes
|
|
|
|
|
|
|
|
return
|
2017-09-30 21:33:33 -05:00
|
|
|
}
|
2017-09-30 22:08:51 -05:00
|
|
|
defer conn.Close()
|
2017-09-30 21:33:33 -05:00
|
|
|
|
2017-09-30 22:08:51 -05:00
|
|
|
_, err = io.Copy(conn, bytes.NewReader(data))
|
2017-09-30 21:33:33 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2017-09-30 22:08:51 -05:00
|
|
|
}
|
|
|
|
|
2017-09-30 23:19:43 -05:00
|
|
|
func sendInv(address, kind string, items [][]byte) {
|
2017-09-30 23:50:27 -05:00
|
|
|
inventory := inv{nodeAddress, kind, items}
|
2017-09-30 23:19:43 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-09-30 23:50:27 -05:00
|
|
|
func sendGetData(address, kind string, id []byte) {
|
|
|
|
payload := gobEncode(getdata{nodeAddress, kind, id})
|
|
|
|
request := append(commandToBytes("getdata"), payload...)
|
|
|
|
|
|
|
|
sendData(address, request)
|
|
|
|
}
|
|
|
|
|
2017-10-03 03:47:27 -05:00
|
|
|
func sendTx(addr string, tnx *Transaction) {
|
|
|
|
data := tx{nodeAddress, tnx.Serialize()}
|
|
|
|
payload := gobEncode(data)
|
|
|
|
request := append(commandToBytes("tx"), payload...)
|
|
|
|
|
|
|
|
sendData(addr, request)
|
|
|
|
}
|
|
|
|
|
2017-10-01 09:33:03 -05:00
|
|
|
func sendVersion(addr string, bc *Blockchain) {
|
|
|
|
bestHeight := bc.GetBestHeight()
|
|
|
|
payload := gobEncode(verzion{nodeVersion, bestHeight, nodeAddress})
|
2017-09-30 22:08:51 -05:00
|
|
|
|
|
|
|
request := append(commandToBytes("version"), payload...)
|
|
|
|
|
|
|
|
sendData(addr, request)
|
|
|
|
}
|
|
|
|
|
2017-09-30 22:25:11 -05:00
|
|
|
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))
|
2017-09-30 23:19:43 -05:00
|
|
|
requestBlocks()
|
|
|
|
}
|
|
|
|
|
2017-09-30 23:50:27 -05:00
|
|
|
func handleBlock(request []byte, bc *Blockchain) {
|
|
|
|
var buff bytes.Buffer
|
|
|
|
var payload block
|
|
|
|
|
|
|
|
buff.Write(request[commandLength:])
|
|
|
|
dec := gob.NewDecoder(&buff)
|
|
|
|
err := dec.Decode(&payload)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
blockData := payload.Block
|
|
|
|
block := DeserializeBlock(blockData)
|
|
|
|
|
|
|
|
fmt.Println("Recevied a new block!")
|
2017-10-01 08:30:21 -05:00
|
|
|
bc.AddBlock(block)
|
2017-10-03 04:21:15 -05:00
|
|
|
|
2017-10-01 09:33:03 -05:00
|
|
|
fmt.Printf("Added block %x\n", block.Hash)
|
|
|
|
|
|
|
|
if len(blocksInTransit) > 0 {
|
|
|
|
blockHash := blocksInTransit[0]
|
|
|
|
sendGetData(payload.AddrFrom, "block", blockHash)
|
|
|
|
|
|
|
|
blocksInTransit = blocksInTransit[1:]
|
2017-10-06 00:29:55 -05:00
|
|
|
} else {
|
|
|
|
UTXOSet := UTXOSet{bc}
|
|
|
|
UTXOSet.Reindex()
|
2017-10-01 09:33:03 -05:00
|
|
|
}
|
2017-09-30 23:50:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func handleInv(request []byte, bc *Blockchain) {
|
2017-09-30 23:19:43 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2017-09-30 23:50:27 -05:00
|
|
|
fmt.Printf("Recevied inventory with %d %s\n", len(payload.Items), payload.Type)
|
|
|
|
|
2017-10-05 23:30:51 -05:00
|
|
|
if payload.Type == "block" {
|
2017-10-01 09:33:03 -05:00
|
|
|
blocksInTransit = payload.Items
|
|
|
|
|
|
|
|
blockHash := payload.Items[0]
|
|
|
|
sendGetData(payload.AddrFrom, "block", blockHash)
|
|
|
|
|
|
|
|
newInTransit := [][]byte{}
|
|
|
|
for _, b := range blocksInTransit {
|
|
|
|
if bytes.Compare(b, blockHash) != 0 {
|
|
|
|
newInTransit = append(newInTransit, b)
|
|
|
|
}
|
2017-09-30 23:50:27 -05:00
|
|
|
}
|
2017-10-01 09:33:03 -05:00
|
|
|
blocksInTransit = newInTransit
|
2017-09-30 23:50:27 -05:00
|
|
|
}
|
2017-10-03 03:47:27 -05:00
|
|
|
|
|
|
|
if payload.Type == "tx" {
|
|
|
|
txID := payload.Items[0]
|
|
|
|
|
|
|
|
if mempool[hex.EncodeToString(txID)].ID == nil {
|
|
|
|
sendGetData(payload.AddrFrom, "tx", txID)
|
|
|
|
}
|
|
|
|
}
|
2017-09-30 23:19:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2017-10-05 23:30:51 -05:00
|
|
|
sendInv(payload.AddrFrom, "block", blocks)
|
2017-09-30 22:25:11 -05:00
|
|
|
}
|
|
|
|
|
2017-09-30 23:50:27 -05:00
|
|
|
func handleGetData(request []byte, bc *Blockchain) {
|
|
|
|
var buff bytes.Buffer
|
|
|
|
var payload getdata
|
|
|
|
|
|
|
|
buff.Write(request[commandLength:])
|
|
|
|
dec := gob.NewDecoder(&buff)
|
|
|
|
err := dec.Decode(&payload)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if payload.Type == "block" {
|
|
|
|
block, err := bc.GetBlock([]byte(payload.ID))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sendBlock(payload.AddrFrom, &block)
|
|
|
|
}
|
2017-10-03 03:47:27 -05:00
|
|
|
|
|
|
|
if payload.Type == "tx" {
|
|
|
|
txID := hex.EncodeToString(payload.ID)
|
|
|
|
tx := mempool[txID]
|
|
|
|
|
|
|
|
sendTx(payload.AddrFrom, &tx)
|
|
|
|
// delete(mempool, txID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleTx(request []byte, bc *Blockchain) {
|
|
|
|
var buff bytes.Buffer
|
|
|
|
var payload tx
|
|
|
|
|
|
|
|
buff.Write(request[commandLength:])
|
|
|
|
dec := gob.NewDecoder(&buff)
|
|
|
|
err := dec.Decode(&payload)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
txData := payload.Transaction
|
|
|
|
tx := DeserializeTransaction(txData)
|
|
|
|
mempool[hex.EncodeToString(tx.ID)] = tx
|
|
|
|
|
|
|
|
if nodeAddress == knownNodes[0] {
|
|
|
|
for _, node := range knownNodes {
|
2017-10-03 04:21:15 -05:00
|
|
|
if node != nodeAddress && node != payload.AddFrom {
|
2017-10-03 03:47:27 -05:00
|
|
|
sendInv(node, "tx", [][]byte{tx.ID})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-10-03 04:21:15 -05:00
|
|
|
if len(mempool) >= 2 && len(miningAddress) > 0 {
|
2017-10-06 00:29:55 -05:00
|
|
|
MineTransactions:
|
2017-10-03 03:47:27 -05:00
|
|
|
var txs []*Transaction
|
|
|
|
|
2017-10-06 00:29:55 -05:00
|
|
|
for id := range mempool {
|
|
|
|
tx := mempool[id]
|
2017-10-03 04:21:15 -05:00
|
|
|
if bc.VerifyTransaction(&tx) {
|
|
|
|
txs = append(txs, &tx)
|
|
|
|
}
|
2017-10-03 03:47:27 -05:00
|
|
|
}
|
2017-10-03 04:21:15 -05:00
|
|
|
|
2017-10-05 23:30:51 -05:00
|
|
|
if len(txs) == 0 {
|
|
|
|
fmt.Println("All transactions are invalid! Waiting for new ones...")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-10-03 04:21:15 -05:00
|
|
|
cbTx := NewCoinbaseTX(miningAddress, "")
|
|
|
|
txs = append(txs, cbTx)
|
|
|
|
|
2017-10-03 03:47:27 -05:00
|
|
|
newBlock := bc.MineBlock(txs)
|
|
|
|
UTXOSet := UTXOSet{bc}
|
2017-10-06 00:29:55 -05:00
|
|
|
UTXOSet.Reindex()
|
2017-10-03 03:47:27 -05:00
|
|
|
|
2017-10-05 23:30:51 -05:00
|
|
|
fmt.Println("New block is mined!")
|
|
|
|
|
2017-10-03 04:21:15 -05:00
|
|
|
for _, tx := range txs {
|
|
|
|
txID := hex.EncodeToString(tx.ID)
|
|
|
|
delete(mempool, txID)
|
|
|
|
}
|
2017-10-05 23:30:51 -05:00
|
|
|
|
|
|
|
for _, node := range knownNodes {
|
|
|
|
if node != nodeAddress {
|
|
|
|
sendInv(node, "block", [][]byte{newBlock.Hash})
|
|
|
|
}
|
|
|
|
}
|
2017-10-06 00:29:55 -05:00
|
|
|
|
|
|
|
if len(mempool) > 0 {
|
|
|
|
goto MineTransactions
|
|
|
|
}
|
2017-10-03 03:47:27 -05:00
|
|
|
}
|
|
|
|
}
|
2017-09-30 23:50:27 -05:00
|
|
|
}
|
|
|
|
|
2017-10-01 09:33:03 -05:00
|
|
|
func handleVersion(request []byte, bc *Blockchain) {
|
2017-09-30 22:08:51 -05:00
|
|
|
var buff bytes.Buffer
|
|
|
|
var payload verzion
|
|
|
|
|
|
|
|
buff.Write(request[commandLength:])
|
|
|
|
dec := gob.NewDecoder(&buff)
|
|
|
|
err := dec.Decode(&payload)
|
2017-09-30 21:33:33 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2017-09-30 22:08:51 -05:00
|
|
|
|
2017-10-01 09:33:03 -05:00
|
|
|
myBestHeight := bc.GetBestHeight()
|
|
|
|
foreignerBestHeight := payload.BestHeight
|
|
|
|
|
|
|
|
if myBestHeight < foreignerBestHeight {
|
|
|
|
sendGetBlocks(payload.AddrFrom)
|
2017-10-03 03:47:56 -05:00
|
|
|
} else if myBestHeight > foreignerBestHeight {
|
2017-10-01 09:33:03 -05:00
|
|
|
sendVersion(payload.AddrFrom, bc)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendAddr(payload.AddrFrom)
|
|
|
|
if !nodeIsKnown(payload.AddrFrom) {
|
|
|
|
knownNodes = append(knownNodes, payload.AddrFrom)
|
|
|
|
}
|
2017-09-30 21:33:33 -05:00
|
|
|
}
|
|
|
|
|
2017-09-30 22:42:34 -05:00
|
|
|
func handleConnection(conn net.Conn, bc *Blockchain) {
|
2017-09-30 21:33:33 -05:00
|
|
|
request, err := ioutil.ReadAll(conn)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
command := bytesToCommand(request[:commandLength])
|
2017-09-30 22:25:11 -05:00
|
|
|
fmt.Printf("Received %s command\n", command)
|
2017-09-30 21:33:33 -05:00
|
|
|
|
|
|
|
switch command {
|
2017-09-30 22:25:11 -05:00
|
|
|
case "addr":
|
|
|
|
handleAddr(request)
|
2017-09-30 23:50:27 -05:00
|
|
|
case "block":
|
|
|
|
handleBlock(request, bc)
|
2017-09-30 23:19:43 -05:00
|
|
|
case "inv":
|
2017-09-30 23:50:27 -05:00
|
|
|
handleInv(request, bc)
|
2017-09-30 23:19:43 -05:00
|
|
|
case "getblocks":
|
|
|
|
handleGetBlocks(request, bc)
|
2017-09-30 23:50:27 -05:00
|
|
|
case "getdata":
|
|
|
|
handleGetData(request, bc)
|
2017-10-03 03:47:27 -05:00
|
|
|
case "tx":
|
|
|
|
handleTx(request, bc)
|
2017-09-30 21:33:33 -05:00
|
|
|
case "version":
|
2017-10-01 09:33:03 -05:00
|
|
|
handleVersion(request, bc)
|
2017-09-30 21:33:33 -05:00
|
|
|
default:
|
2017-09-30 22:25:11 -05:00
|
|
|
fmt.Println("Unknown command!")
|
2017-09-30 21:33:33 -05:00
|
|
|
}
|
|
|
|
|
2017-09-30 20:44:41 -05:00
|
|
|
conn.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartServer starts a node
|
2017-10-03 04:21:15 -05:00
|
|
|
func StartServer(nodeID, minerAddress string) {
|
2017-09-30 22:29:04 -05:00
|
|
|
nodeAddress = fmt.Sprintf("localhost:%s", nodeID)
|
2017-10-03 04:21:15 -05:00
|
|
|
miningAddress = minerAddress
|
2017-09-30 21:33:33 -05:00
|
|
|
ln, err := net.Listen(protocol, nodeAddress)
|
2017-09-30 20:44:41 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
defer ln.Close()
|
|
|
|
|
2017-09-30 22:42:34 -05:00
|
|
|
bc := NewBlockchain(nodeID)
|
|
|
|
|
2017-10-01 09:33:03 -05:00
|
|
|
if nodeAddress != knownNodes[0] {
|
|
|
|
sendVersion(knownNodes[0], bc)
|
|
|
|
}
|
|
|
|
|
2017-09-30 20:44:41 -05:00
|
|
|
for {
|
|
|
|
conn, err := ln.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2017-09-30 22:42:34 -05:00
|
|
|
go handleConnection(conn, bc)
|
2017-09-30 20:44:41 -05:00
|
|
|
}
|
|
|
|
}
|
2017-09-30 22:08:51 -05:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
2017-10-01 09:33:03 -05:00
|
|
|
|
|
|
|
func nodeIsKnown(addr string) bool {
|
|
|
|
for _, node := range knownNodes {
|
|
|
|
if node == addr {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|