From 3bdc44aaa5f22ff158324c6037dc494649d3c76e Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Fri, 10 Aug 2018 14:05:55 -0400 Subject: [PATCH 1/5] add mutex so the only one channel writes to the socket at a given moment --- shyftRingWalletConn/app.go | 57 +++++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/shyftRingWalletConn/app.go b/shyftRingWalletConn/app.go index fe0daf40e9..6e4969c7b8 100644 --- a/shyftRingWalletConn/app.go +++ b/shyftRingWalletConn/app.go @@ -14,6 +14,8 @@ import ( "github.com/ShyftNetwork/go-empyrean/ethclient" "github.com/ShyftNetwork/go-empyrean/common" "context" + "time" + "sync" ) const ( @@ -28,6 +30,8 @@ var testPrivHex = "0123456789012345678901234567890123456789012345678901234567890 var client = &http.Client{} +var mutex = &sync.Mutex{} + // This gives context to the signed message and prevents signing of transactions. func signHash(data []byte) []byte { msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data) @@ -80,9 +84,14 @@ func handleMessages(channel chan []byte, checkBalancesChan chan []byte) { if prevMsg != nil { s := string(prevMsg[:]) if s == "-- ADDRESS --" { + fmt.Println("putting on channel 1") addressOfClient = msg checkBalancesChan <- addressOfClient } + if s == "-- GET_BALANCE --" { + fmt.Println("putting on channel 3") + checkBalancesChan <- msg + } if s == "-- SIGNATURE --" { signatureFromClient = msg } @@ -115,6 +124,8 @@ func handleMessages(channel chan []byte, checkBalancesChan chan []byte) { pubKey := crypto.ToECDSAPub(rpk) recoveredAddr := crypto.PubkeyToAddress(*pubKey) fmt.Println("ADDRESS IS ::", recoveredAddr.Hex()) + signatureFromClient = nil + msgFromClient = nil } } } @@ -143,22 +154,35 @@ func readerConn(conn net.Conn, channel chan []byte) { } func checkBalance(checkBalanceChan chan []byte, conn net.Conn) { - address := <-checkBalanceChan + fmt.Println("in check balance function") c, err := ethclient.Dial("http://127.0.0.1:8545") if err != nil { fmt.Println("Eth Client not initialized: " , err) } - balance, error := c.BalanceAt(context.Background(), common.HexToAddress(string(address[:])),nil) - if error != nil { - fmt.Println("Balance at error ", error) + for { + address := <-checkBalanceChan + fmt.Println("the address is ", string(address[:])) + + + balance, error := c.BalanceAt(context.Background(), common.HexToAddress(string(address[:])),nil) + if error != nil { + fmt.Println("Balance at error ", error) + } + mutex.Lock() + fmt.Println("in broadcasting balance") + fmt.Println("the bal is ", balance) + fmt.Println("The balance for address ", string(address[:]), " is ", balance) + fmt.Println([]byte("Broadcasting Balance")) + fmt.Println([]byte("\n")) + fmt.Println([]byte(balance.String())) + fmt.Println([]byte("\n")) + conn.Write([]byte("Broadcasting Balance")) + conn.Write([]byte("\n")) + conn.Write([]byte(balance.String())) + conn.Write([]byte("\n")) + mutex.Unlock() } - fmt.Println("the bal is ", balance) - fmt.Println("The balance for address ", address, " is ", balance) - conn.Write([]byte("Broadcasting Balance")) - conn.Write([]byte("\n")) - conn.Write([]byte(balance.String())) - conn.Write([]byte("\n")) } func sendRingSignedMsg(conn net.Conn){ @@ -174,10 +198,23 @@ func sendRingSignedMsg(conn net.Conn){ fmt.Println("The crypto.Sign err is ", err) } + mutex.Lock() + fmt.Println("in broadcasting message") + fmt.Println([]byte("Broadcasting Message")) + fmt.Println([]byte("\n")) + fmt.Println([]byte(f_msg)) + fmt.Println([]byte("\n")) + fmt.Println(new_sig) + fmt.Println([]byte("\n")) conn.Write([]byte("Broadcasting Message")) + time.Sleep(1000 * time.Millisecond) conn.Write([]byte("\n")) + time.Sleep(3000 * time.Millisecond) conn.Write([]byte(f_msg)) + time.Sleep(1000 * time.Millisecond) conn.Write([]byte("\n")) conn.Write(new_sig) + time.Sleep(1000 * time.Millisecond) conn.Write([]byte("\n")) + mutex.Unlock() } \ No newline at end of file From 62f13352bbc79899ff41b08a0625c74298e9136d Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Sat, 11 Aug 2018 17:13:05 -0400 Subject: [PATCH 2/5] allows client to send a signed transaction then call ethclient to send the transaction --- shyftRingWalletConn/app.go | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/shyftRingWalletConn/app.go b/shyftRingWalletConn/app.go index 6e4969c7b8..65242c3eca 100644 --- a/shyftRingWalletConn/app.go +++ b/shyftRingWalletConn/app.go @@ -16,6 +16,8 @@ import ( "context" "time" "sync" + "github.com/ShyftNetwork/go-empyrean/core/types" + "github.com/ShyftNetwork/go-empyrean/rlp" ) const ( @@ -63,15 +65,18 @@ func handleRequest(conn net.Conn) { messages := make(chan []byte) checkBalanceChan := make(chan []byte) + sendTransactionChan := make(chan []byte) + go readerConn(conn, messages) - go handleMessages(messages, checkBalanceChan) + go handleMessages(messages, checkBalanceChan, sendTransactionChan) go checkBalance(checkBalanceChan, conn) + go sendTransaction(sendTransactionChan) sendRingSignedMsg(conn) } -func handleMessages(channel chan []byte, checkBalancesChan chan []byte) { +func handleMessages(channel chan []byte, checkBalancesChan chan []byte, sendTransactionChan chan []byte) { var prevMsg []byte var addressOfClient []byte var signatureFromClient []byte @@ -92,6 +97,9 @@ func handleMessages(channel chan []byte, checkBalancesChan chan []byte) { fmt.Println("putting on channel 3") checkBalancesChan <- msg } + if s == "-- SEND_TRANSACTION --" { + sendTransactionChan <- msg + } if s == "-- SIGNATURE --" { signatureFromClient = msg } @@ -185,6 +193,27 @@ func checkBalance(checkBalanceChan chan []byte, conn net.Conn) { } } +func sendTransaction(sendTransactionChan chan []byte) { + fmt.Println("in sendTransaction function") + c, err := ethclient.Dial("http://127.0.0.1:8545") + if err != nil { + fmt.Println("Eth Client not initialized: " , err) + } + + for { + signedTransactionBytes := <-sendTransactionChan + signedTransaction := string(signedTransactionBytes[:]) + bytes, err := hexutil.Decode(signedTransaction) + if err != nil { + fmt.Println("error decoding signed transaction into bytes ", bytes) + } + var tx types.Transaction + rlp.DecodeBytes(bytes, &tx) + fmt.Println(tx.String()) + c.SendTransaction(context.Background(), &tx) + } +} + func sendRingSignedMsg(conn net.Conn){ key, _ := crypto.HexToECDSA(testPrivHex) From 1cbe9bf2770e3e86f92efda72fb9876c78007441 Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Sun, 12 Aug 2018 09:22:59 -0400 Subject: [PATCH 3/5] rm testing sleeps --- shyftRingWalletConn/app.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/shyftRingWalletConn/app.go b/shyftRingWalletConn/app.go index 65242c3eca..4f0863b68e 100644 --- a/shyftRingWalletConn/app.go +++ b/shyftRingWalletConn/app.go @@ -14,7 +14,6 @@ import ( "github.com/ShyftNetwork/go-empyrean/ethclient" "github.com/ShyftNetwork/go-empyrean/common" "context" - "time" "sync" "github.com/ShyftNetwork/go-empyrean/core/types" "github.com/ShyftNetwork/go-empyrean/rlp" @@ -236,14 +235,10 @@ func sendRingSignedMsg(conn net.Conn){ fmt.Println(new_sig) fmt.Println([]byte("\n")) conn.Write([]byte("Broadcasting Message")) - time.Sleep(1000 * time.Millisecond) conn.Write([]byte("\n")) - time.Sleep(3000 * time.Millisecond) conn.Write([]byte(f_msg)) - time.Sleep(1000 * time.Millisecond) conn.Write([]byte("\n")) conn.Write(new_sig) - time.Sleep(1000 * time.Millisecond) conn.Write([]byte("\n")) mutex.Unlock() } \ No newline at end of file From 2432128b60f699a68dc8c801bbc6ede6b10147cc Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Mon, 13 Aug 2018 14:44:36 -0400 Subject: [PATCH 4/5] clean up code --- shyftRingWalletConn/app.go | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/shyftRingWalletConn/app.go b/shyftRingWalletConn/app.go index 4f0863b68e..9e569aaff9 100644 --- a/shyftRingWalletConn/app.go +++ b/shyftRingWalletConn/app.go @@ -88,12 +88,10 @@ func handleMessages(channel chan []byte, checkBalancesChan chan []byte, sendTran if prevMsg != nil { s := string(prevMsg[:]) if s == "-- ADDRESS --" { - fmt.Println("putting on channel 1") addressOfClient = msg - checkBalancesChan <- addressOfClient + //checkBalancesChan <- addressOfClient } if s == "-- GET_BALANCE --" { - fmt.Println("putting on channel 3") checkBalancesChan <- msg } if s == "-- SEND_TRANSACTION --" { @@ -130,7 +128,7 @@ func handleMessages(channel chan []byte, checkBalancesChan chan []byte, sendTran pubKey := crypto.ToECDSAPub(rpk) recoveredAddr := crypto.PubkeyToAddress(*pubKey) - fmt.Println("ADDRESS IS ::", recoveredAddr.Hex()) + fmt.Println("Client connected with address :", recoveredAddr.Hex()) signatureFromClient = nil msgFromClient = nil } @@ -161,7 +159,6 @@ func readerConn(conn net.Conn, channel chan []byte) { } func checkBalance(checkBalanceChan chan []byte, conn net.Conn) { - fmt.Println("in check balance function") c, err := ethclient.Dial("http://127.0.0.1:8545") if err != nil { fmt.Println("Eth Client not initialized: " , err) @@ -177,13 +174,7 @@ func checkBalance(checkBalanceChan chan []byte, conn net.Conn) { fmt.Println("Balance at error ", error) } mutex.Lock() - fmt.Println("in broadcasting balance") - fmt.Println("the bal is ", balance) fmt.Println("The balance for address ", string(address[:]), " is ", balance) - fmt.Println([]byte("Broadcasting Balance")) - fmt.Println([]byte("\n")) - fmt.Println([]byte(balance.String())) - fmt.Println([]byte("\n")) conn.Write([]byte("Broadcasting Balance")) conn.Write([]byte("\n")) conn.Write([]byte(balance.String())) @@ -193,7 +184,6 @@ func checkBalance(checkBalanceChan chan []byte, conn net.Conn) { } func sendTransaction(sendTransactionChan chan []byte) { - fmt.Println("in sendTransaction function") c, err := ethclient.Dial("http://127.0.0.1:8545") if err != nil { fmt.Println("Eth Client not initialized: " , err) @@ -227,13 +217,6 @@ func sendRingSignedMsg(conn net.Conn){ } mutex.Lock() - fmt.Println("in broadcasting message") - fmt.Println([]byte("Broadcasting Message")) - fmt.Println([]byte("\n")) - fmt.Println([]byte(f_msg)) - fmt.Println([]byte("\n")) - fmt.Println(new_sig) - fmt.Println([]byte("\n")) conn.Write([]byte("Broadcasting Message")) conn.Write([]byte("\n")) conn.Write([]byte(f_msg)) From 0424770010ac7e8e559c194631cf47736c7fd1c6 Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Mon, 13 Aug 2018 14:45:01 -0400 Subject: [PATCH 5/5] clean up formatting --- shyftRingWalletConn/app.go | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/shyftRingWalletConn/app.go b/shyftRingWalletConn/app.go index 9e569aaff9..0f099d3eef 100644 --- a/shyftRingWalletConn/app.go +++ b/shyftRingWalletConn/app.go @@ -4,19 +4,19 @@ package main //to run server 'go run shyftRingWalletConn/*.go' import ( "bufio" + "context" "fmt" + "github.com/ShyftNetwork/go-empyrean/common" "github.com/ShyftNetwork/go-empyrean/common/hexutil" + "github.com/ShyftNetwork/go-empyrean/core/types" "github.com/ShyftNetwork/go-empyrean/crypto" + "github.com/ShyftNetwork/go-empyrean/ethclient" + "github.com/ShyftNetwork/go-empyrean/rlp" "io" "net" "net/http" "os" - "github.com/ShyftNetwork/go-empyrean/ethclient" - "github.com/ShyftNetwork/go-empyrean/common" - "context" "sync" - "github.com/ShyftNetwork/go-empyrean/core/types" - "github.com/ShyftNetwork/go-empyrean/rlp" ) const ( @@ -66,7 +66,6 @@ func handleRequest(conn net.Conn) { checkBalanceChan := make(chan []byte) sendTransactionChan := make(chan []byte) - go readerConn(conn, messages) go handleMessages(messages, checkBalanceChan, sendTransactionChan) go checkBalance(checkBalanceChan, conn) @@ -161,15 +160,14 @@ func readerConn(conn net.Conn, channel chan []byte) { func checkBalance(checkBalanceChan chan []byte, conn net.Conn) { c, err := ethclient.Dial("http://127.0.0.1:8545") if err != nil { - fmt.Println("Eth Client not initialized: " , err) + fmt.Println("Eth Client not initialized: ", err) } for { address := <-checkBalanceChan fmt.Println("the address is ", string(address[:])) - - balance, error := c.BalanceAt(context.Background(), common.HexToAddress(string(address[:])),nil) + balance, error := c.BalanceAt(context.Background(), common.HexToAddress(string(address[:])), nil) if error != nil { fmt.Println("Balance at error ", error) } @@ -186,7 +184,7 @@ func checkBalance(checkBalanceChan chan []byte, conn net.Conn) { func sendTransaction(sendTransactionChan chan []byte) { c, err := ethclient.Dial("http://127.0.0.1:8545") if err != nil { - fmt.Println("Eth Client not initialized: " , err) + fmt.Println("Eth Client not initialized: ", err) } for { @@ -203,7 +201,7 @@ func sendTransaction(sendTransactionChan chan []byte) { } } -func sendRingSignedMsg(conn net.Conn){ +func sendRingSignedMsg(conn net.Conn) { key, _ := crypto.HexToECDSA(testPrivHex) f_msg := "Hello World" @@ -224,4 +222,4 @@ func sendRingSignedMsg(conn net.Conn){ conn.Write(new_sig) conn.Write([]byte("\n")) mutex.Unlock() -} \ No newline at end of file +}