diff --git a/README.md b/README.md index 7a70ab8..e3f087b 100644 --- a/README.md +++ b/README.md @@ -6,4 +6,7 @@ Collection of Coins. ## Bank Trusted Node managing Coin Exchage. ## Coin(Onion) -Currency Unit for messages forwarding. \ No newline at end of file +Currency Unit for messages forwarding. +## P2P +port 1338 normal node +port 8331 bank node \ No newline at end of file diff --git a/coin/coin.go b/coin/coin.go index 1b3072d..6790562 100644 --- a/coin/coin.go +++ b/coin/coin.go @@ -21,7 +21,7 @@ func print(str interface{}) { } } -func New_Coin() *Coin { +func NewCoin() *Coin { coin := new(Coin) coin.RID = "1338" coin.Content = []byte("hello world") @@ -29,10 +29,10 @@ func New_Coin() *Coin { 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 } \ No newline at end of file diff --git a/coin/raw_coin.go b/coin/raw_coin.go index f496fdc..8a9a4b6 100644 --- a/coin/raw_coin.go +++ b/coin/raw_coin.go @@ -5,7 +5,7 @@ type RawCoin struct { Content []byte } -func New_RawCoin() *RawCoin { +func NewRawCoin() *RawCoin { coin := new(RawCoin) coin.RID = "1338" coin.Content = []byte("hello world") @@ -13,10 +13,10 @@ func New_RawCoin() *RawCoin { 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 } \ No newline at end of file diff --git a/node/node.go b/node/node.go index e752a37..2f8e451 100644 --- a/node/node.go +++ b/node/node.go @@ -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){ @@ -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) +} diff --git a/node/peernet.go b/node/peernet.go index 82ca5ce..dbecb68 100644 --- a/node/peernet.go +++ b/node/peernet.go @@ -4,27 +4,56 @@ 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.") } @@ -32,22 +61,42 @@ func (n *Node) dispatch(incoming []byte, con *net.UDPConn, add *net.UDPAddr) { 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() {} \ No newline at end of file +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] +} \ No newline at end of file diff --git a/node/routing.go b/node/routing.go new file mode 100644 index 0000000..9623380 --- /dev/null +++ b/node/routing.go @@ -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] +} \ No newline at end of file diff --git a/oc/oc.go b/oc/oc.go index 3fac924..fccf8ce 100644 --- a/oc/oc.go +++ b/oc/oc.go @@ -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" ) @@ -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() + } } \ No newline at end of file diff --git a/vault/vault.go b/vault/vault.go index cbbbc52..a6a0d64 100644 --- a/vault/vault.go +++ b/vault/vault.go @@ -15,9 +15,7 @@ type Vault struct { func print(str interface{}) { - if !debugged { - return - } + if !debugged { return } switch str.(type) { case int, uint, uint64: @@ -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 {