Skip to content
Snippets Groups Projects
Verified Commit 4fce1365 authored by xavier.perret's avatar xavier.perret
Browse files

added fake transaction

parent bc083c99
No related branches found
No related tags found
1 merge request!2added file to separate client function from the server
......@@ -9,6 +9,7 @@ import (
"net"
"os"
"strconv"
"strings"
"time"
)
......@@ -168,13 +169,23 @@ func vote(server net.Listener, trans Transaction, config Config, parentAddress s
go sendVoteToAllNeighbours(config, trans, parentAddress)
}
}
if stimulatedByClient {
fmt.Println("***********************************")
fmt.Println("Transaction has been rated")
fmt.Println("Node correct : ", ack.AmountOfCorrectNode, "out of ", ack.TotalNodes)
fmt.Println("***********************************")
go sendAckToNeighbour(config, ack, parentAddress)
if !stimulatedByClient {
address := parentAddress
for _, neighbour := range config.Neighbours {
fmt.Println("Comparing ", neighbour.Address, " with ", parentAddress)
if neighbour.Address == parentAddress {
address = address + ":" + strconv.Itoa(neighbour.Port)
fmt.Println("Calculated address is ", address)
}
}
fmt.Println("Sending to ", address)
go sendAckToNeighbour(config, ack, address)
}
return ack
}
......@@ -184,7 +195,7 @@ func sendVoteToAllNeighbours(config Config, trans Transaction, parentIp string)
ip := config.Neighbours[neighbour].Address
port := strconv.Itoa(config.Neighbours[neighbour].Port)
address := ip + ":" + port
if address != parentIp {
if parentIp != ip {
go sendVoteToNeighbour(config, trans, address)
}
}
......@@ -345,18 +356,48 @@ func processClient(connection net.Conn, server net.Listener, config Config, stim
trans.Amount = body["amount"].(string)
printTransaction(trans)
// todo change this for cloud
vote(server, trans, config, connection.LocalAddr().String(), stimulatedByClient)
address := strings.Split(connection.RemoteAddr().String(), ":")[0]
vote(server, trans, config, address, stimulatedByClient)
} else if mess.MessageType == "fake" {
fmt.Println("Received a fake")
fmt.Println("Not yet implemented")
var fakeTransaction Transaction
var body map[string]interface{} = mess.MessageBody.(map[string]interface{})
fakeTransaction.Id = body["id"].(string)
fakeTransaction.Receiver = body["receiver"].(string)
fakeTransaction.Sender = body["sender"].(string)
fakeTransaction.Amount = body["amount"].(string)
for transactionIndex := range DB {
if DB[transactionIndex].Id == fakeTransaction.Id {
DB[transactionIndex].Sender = fakeTransaction.Sender
DB[transactionIndex].Receiver = fakeTransaction.Receiver
DB[transactionIndex].Amount = fakeTransaction.Amount
fmt.Println("Successfully replaced the legitimate transaction with a fake one!")
}
}
} else if mess.MessageType == "list" {
fmt.Println("Received a list all transactions order")
listAllTransactions()
listAllTransactionsToClient(connection)
} else {
fmt.Println("Unknown message type")
}
}
func listAllTransactionsToClient(conn net.Conn) {
mess := Message{
MessageType: "list",
MessageBody: DB,
}
fmt.Println("Sending message to neighbour", mess)
encoder := json.NewEncoder(conn)
err := encoder.Encode(mess)
if err != nil {
fmt.Println("Error while encoding the transaction", err)
os.Exit(1)
}
conn.Close()
}
func processTransaction(server net.Listener, config Config, trans Transaction, stimulatedByClient bool) {
fmt.Println("Processing transaction")
printTransaction(trans)
......@@ -527,12 +568,52 @@ func userInputLoop(config Config, isAlsoServer bool) {
break
case "3":
fmt.Println("You chose to fabricate a fake transaction")
fmt.Println("You chose to fake a transaction")
listAllTransactions()
fmt.Print("\nPlease enter the index of the transaction you want to overwrite by faking:")
var transID string
_, err := fmt.Scanln(&transID)
if err != nil {
fmt.Println("error :", err.Error())
os.Exit(1)
}
transIDInt, err := strconv.ParseInt(transID, 10, 64)
if err != nil {
fmt.Println("error :", err.Error())
os.Exit(1)
}
printAllNeighbours(config)
fmt.Println("Please enter the ID of the neighbour you to ask to fake the given transaction")
var neighbourID string
_, err = fmt.Scanln(&neighbourID)
if err != nil {
fmt.Println("error :", err.Error())
os.Exit(1)
}
neighbourIDInt, err := strconv.ParseInt(neighbourID, 10, 64)
if err != nil {
fmt.Println("error :", err.Error())
os.Exit(1)
}
tmpFakeTrans := userCreatedTransaction(config)
transToFake := DB[transIDInt]
fakeTrans := Transaction{
Id: transToFake.Id,
Sender: tmpFakeTrans.Sender,
Receiver: tmpFakeTrans.Receiver,
Amount: tmpFakeTrans.Amount,
}
ip := config.Neighbours[neighbourIDInt].Address
port := strconv.Itoa(config.Neighbours[neighbourIDInt].Port)
address := ip + ":" + port
sendFakeTransactionToNeighbour(fakeTrans, address)
break
case "4":
fmt.Println("You chose to print all transactions")
listAllTransactions()
break
case "5":
fmt.Println("You chose to ask for all transactions of a given node")
break
case "6":
break
......@@ -548,6 +629,32 @@ func userInputLoop(config Config, isAlsoServer bool) {
}
}
func sendFakeTransactionToNeighbour(trans Transaction, address string) {
fmt.Println()
fmt.Println("Trying to connect to ", address)
conn, err := net.Dial(ConnectionType, address)
if err != nil {
fmt.Println("Error while connecting to the neighbour", err)
os.Exit(1)
}
mess := Message{
MessageType: "fake",
MessageBody: trans,
}
fmt.Println("Sending message to neighbour", mess)
encoder := json.NewEncoder(conn)
err = encoder.Encode(mess)
if err != nil {
fmt.Println("Error while encoding the transaction", err)
os.Exit(1)
}
conn.Close()
fmt.Println("MessageBody successfully sent to neighbour")
}
func sendVoteToNeighbour(config Config, trans Transaction, address string) {
fmt.Println()
fmt.Println("Trying to connect to ", address)
......@@ -575,8 +682,12 @@ func sendVoteToNeighbour(config Config, trans Transaction, address string) {
}
func sendAckToNeighbour(config Config, ack AckTransaction, address string) {
fmt.Println()
fmt.Println("*******************STARTING TO SEND ACK*******************")
fmt.Println("Trying to connect to ", address)
var ackToSend AckTransaction
ackToSend.Id = ack.Id
ackToSend.AmountOfCorrectNode = ack.AmountOfCorrectNode
ackToSend.TotalNodes = ack.TotalNodes
conn, err := net.Dial(ConnectionType, address)
if err != nil {
......@@ -586,7 +697,7 @@ func sendAckToNeighbour(config Config, ack AckTransaction, address string) {
mess := Message{
MessageType: "AckResponse",
MessageBody: ack,
MessageBody: ackToSend,
}
fmt.Println("Sending message to neighbour", mess)
encoder := json.NewEncoder(conn)
......@@ -598,6 +709,7 @@ func sendAckToNeighbour(config Config, ack AckTransaction, address string) {
conn.Close()
fmt.Println("MessageBody successfully sent to neighbour")
fmt.Println("*******************FINISHED ACK*******************")
}
func main() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment