Skip to content

Commit

Permalink
basic join built
Browse files Browse the repository at this point in the history
  • Loading branch information
rainer37 committed Dec 28, 2017
1 parent 81f3616 commit 2d9e061
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 54 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ Collection of Coins.
## Bank
Trusted Node managing Coin Exchage.
## Coin(Onion)
Currency Unit for messages forwarding.
Currency Unit for messages forwarding.
## P2P
port 1338 normal node
port 8331 bank node
6 changes: 3 additions & 3 deletions coin/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ func print(str interface{}) {

}
}
func New_Coin() *Coin {
func NewCoin() *Coin {
coin := new(Coin)
coin.RID = "1338"
coin.Content = []byte("hello world")
print("Coin is an Onion : " + string(coin.RID))
return coin
}

func (c *Coin) Get_Content() []byte {
func (c *Coin) GetContent() []byte {
return c.Content
}

func (c *Coin) Get_RID() string {
func (c *Coin) GetRID() string {
return c.RID
}
6 changes: 3 additions & 3 deletions coin/raw_coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ type RawCoin struct {
Content []byte
}

func New_RawCoin() *RawCoin {
func NewRawCoin() *RawCoin {
coin := new(RawCoin)
coin.RID = "1338"
coin.Content = []byte("hello world")
print("Coin is an Onion : " + string(coin.RID))
return coin
}

func (c *RawCoin) Get_Content() []byte {
func (c *RawCoin) GetContent() []byte {
return c.Content
}

func (c *RawCoin) Get_RID() string {
func (c *RawCoin) GetRID() string {
return c.RID
}
25 changes: 19 additions & 6 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@ import(

const NODE_PREFIX = "[NODE]"

type PKPair struct {
pk []byte
sk []byte
}

type Node struct {
vault *vault.Vault
IP string
Port string
*vault.Vault
*PKPair
*RoutingTable
}

func checkErr(err error){
Expand All @@ -32,22 +40,27 @@ func print(str interface{}) {
func NewNode() *Node {
print("Create a new node.")
n := new(Node)
n.vault = new(vault.Vault)
n.vault.InitVault()
n.Vault = new(vault.Vault)
n.RoutingTable = new(RoutingTable)
n.InitRT()
n.InitVault()
return n
}

func (n *Node) GetBalance() int {
return n.vault.Len()
return n.Len()
}

func (n *Node) Deposit(coin *coin.Coin) error {
return n.vault.Deposit(coin)
return n.Deposit(coin)
}

func (n *Node) Withdraw(rid string) *coin.Coin {
return n.vault.Withdraw(rid)
return n.Withdraw(rid)
}

func (n *Node) Join(address string) {
n.sendActive("JOIN"+n.Port, address)
}


77 changes: 63 additions & 14 deletions node/peernet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,99 @@ import (
"net"
"fmt"
"strconv"
"strings"
)

const (
FWD = "FWD "
JOIN = "JOIN"
FIND = "FIND"
FREE = "FREE"
COIN = "COIN"
EXPT = "EXPT"
)

func (n *Node) PeerNetInit(port string) {
print("Peernet Initiated.")
p,e := strconv.Atoi(port)
checkErr(e)
n.Serve("127.0.0.1", p)
func (n *Node) SelfInit() {
print("PeerNet Initiated.")
p,err := strconv.Atoi(n.Port)
checkErr(err)
n.Serve(":", p)
}

func (n *Node) dispatch(incoming []byte, con *net.UDPConn, add *net.UDPAddr) {
switch string(incoming[:4]) {
case FWD:
con.WriteTo([]byte("Hello World"), add)
print("Forwarding")
n.send([]byte("Fine i will take the coin though."), con, add)
case JOIN:
print("Joining "+string(incoming[4:]))
ok, id, address := deSegementJoinMsg(string(incoming[4:]))

if !ok {
print("Invalidate message format, rejected!")
n.send([]byte("INVALID MSG FORMAT, REJECTED"), con, add)
}

verified := n.verifyID(id)

if !verified {
print("Invalidate ID, be aware!")
n.send([]byte("UNABLE TO VERIFY YOUR ID, REJECTED"), con, add)
}

n.insert(id, address)

case FIND:
print("Finding")
case FREE:
//receive the free list
case COIN:
//receive the coin
case EXPT:
//any exception
default:
print("Unknown Msg, discard.")
}
}

func (n *Node) Serve(ip string, port int) {
addr := net.UDPAddr{Port: port, IP: net.ParseIP(ip)}
pc, err := net.ListenUDP("udp", &addr)
con, err := net.ListenUDP("udp", &addr)
buffer := make([]byte, 2048)

checkErr(err)

defer pc.Close()
defer con.Close()

for {
len, add, e := pc.ReadFromUDP(buffer)
len, add, e := con.ReadFromUDP(buffer)
checkErr(e)
incoming := buffer[0:len]
fmt.Println(NODE_PREFIX,"From",add, len, "bytes:", incoming)

go n.dispatch(incoming, pc, add)
fmt.Println(NODE_PREFIX,"From", add, len, "bytes:[", string(incoming),"]")
//TODO: verify authenticity of msg
go n.dispatch(incoming, con, add)
}
}

func (n *Node) send() {}
func (n *Node) receive() {}
func (n *Node) send(msg []byte, con *net.UDPConn, add *net.UDPAddr) {
_, err := con.WriteTo(msg, add)
checkErr(err)
}

func (n *Node) sendActive(msg string, add string) {
con, err := net.Dial("udp", add)
defer con.Close()
checkErr(err)
_, err = con.Write([]byte(msg))
checkErr(err)
}

func (n *Node) verifyID(id string) bool { return true }

func deSegementJoinMsg(msg string) (bool,string, string) {
segs := strings.Split(msg, "@")
if len(segs) != 2 {
return false,"",""
}
return true, segs[0], segs[1]
}
22 changes: 22 additions & 0 deletions node/routing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package node

import "fmt"

type RoutingTable struct {
table map[string]string
}

func (rt *RoutingTable) InitRT() {
fmt.Println(rt)
rt.table = make(map[string]string)
}

func (rt *RoutingTable) insert(id string, address string) {
rt.table[id] = address
}
func (rt *RoutingTable) remove(id string) {
delete(rt.table, id)
}
func (rt *RoutingTable) get(id string) string {
return rt.table[id]
}
32 changes: 12 additions & 20 deletions oc/oc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package main
import(
"fmt"
"os"
//"github.com/rainer37/OnionCoin/coin"
//"github.com/rainer37/OnionCoin/vault"
//"github.com/rainer37/OnionCoin/ocrypto"
"github.com/rainer37/OnionCoin/node"
)

Expand All @@ -16,26 +13,21 @@ func main() {
os.Exit(1)
}

port := os.Args[1]

fmt.Println("[MAIN] OnionCoin v1.0.0 Started...")

/*
ocrypto.NewCryptoTK()
n := node.NewNode()
fmt.Println("[MAIN] Balance:", n.GetBalance())
var vault vault.Vault
coin := coin.New_Coin()
vault.InitVault()
defer func() {
fmt.Println("[MAIN] OnionCoin shudown.")
}()

err := vault.Deposit(coin)
if err != nil {
println(err.Error())
}
cmd := os.Args[1]

vault.Withdraw("1338")
*/
n := node.NewNode()
n.IP = "127.0.0.1"
n.Port = os.Args[2]

new(node.Node).PeerNetInit(port)
if cmd == "j" {
n.Join(os.Args[3])
} else if cmd == "i" {
n.SelfInit()
}
}
12 changes: 5 additions & 7 deletions vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ type Vault struct {

func print(str interface{}) {

if !debugged {
return
}
if !debugged { return }

switch str.(type) {
case int, uint, uint64:
Expand All @@ -39,20 +37,20 @@ func (vault *Vault) InitVault() {
}

func (vault *Vault) Contains(coin *coin.Coin) bool {
if _, ok := vault.Coins[coin.Get_RID()]; ok {
if _, ok := vault.Coins[coin.GetRID()]; ok {
return true
}
return false
}

func (vault *Vault) Deposit(coin *coin.Coin) error {
print("Depositing Coin :"+coin.Get_RID())
print("Depositing Coin :"+coin.GetRID())
if !vault.Contains(coin) {
vault.Coins[coin.Get_RID()] = coin
vault.Coins[coin.GetRID()] = coin
//print(vault.Len())
return nil
}
return fmt.Errorf("Error: %s is in the Vault", coin.Get_RID())
return fmt.Errorf("error: %s is in the vault", coin.GetRID())
}

func (vault *Vault) Withdraw(id string) *coin.Coin {
Expand Down

0 comments on commit 2d9e061

Please sign in to comment.