Select Git revision
userinput.go
userinput.go 8.33 KiB
package command_line
import (
"fmt"
"math/rand"
ObjectStorageAPI "node/object-storage"
Sender "node/sender"
. "node/types"
"node/utilities"
"os"
"strconv"
"time"
)
// userCreatedTransaction is a function to interactively create a transaction using the terminal
func userCreatedTransaction(config Config) Transaction {
fmt.Println("--------------------STARTING USER CREATED TRANSACTION--------------------")
var trans Transaction
now := time.Now().String()
randomString := strconv.Itoa(rand.Int())
trans.Id = now + randomString
fmt.Println()
var receiver string
fmt.Print("\nPlease enter the receiver")
_, err := fmt.Scanln(&receiver)
if err != nil {
return Transaction{}
}
trans.Receiver = receiver
var sender string
fmt.Print("\nPlease enter the sender")
_, err = fmt.Scanln(&sender)
if err != nil {
return Transaction{}
}
trans.Sender = sender
fmt.Println("Creating a transaction with sender id:", trans.Sender)
fmt.Println("transaction id is ", trans.Id)
fmt.Print("\nEnter amount :")
_, err = fmt.Scanln(&trans.Amount)
if err != nil {
fmt.Println("error")
os.Exit(1)
}
for neighbour := range config.Neighbours {
fmt.Println("Neighbour ", config.Neighbours[neighbour].ID)
fmt.Println("address ", config.Neighbours[neighbour].Address)
fmt.Println("port", config.Neighbours[neighbour].Port)
}
fmt.Println("--------------------FINISHED USER CREATED TRANSACTION--------------------")
return trans
}
func ServerUserInputLoop(config Config, isAlsoServer bool, objectStorage Blob) {
var database Database
for true {
var operation string
fmt.Println()
fmt.Println()
fmt.Println("Please enter the operation you want to do")
fmt.Println("1. Fabricate a fake transaction")
fmt.Println("2. Print all transactions")
fmt.Println("3. Exit")
fmt.Print("Your choice: ")
_, err := fmt.Scanln(&operation)
if err != nil {
fmt.Println("error :", err.Error())
os.Exit(1)
}
switch operation {
case "1":
fmt.Println("You chose to fabricate a fake transaction")
fmt.Println("You chose to fake a transaction")
database = ObjectStorageAPI.ReadDatabaseFromBlobStorage(objectStorage)
utilities.PrintingDatabaseToConsole(database)
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)
}
tmpFakeTrans := userCreatedTransaction(config)
database = ObjectStorageAPI.ReadDatabaseFromBlobStorage(objectStorage)
database = ObjectStorageAPI.FakeTransaction(database[transIDInt], tmpFakeTrans, database)
ObjectStorageAPI.WriteDatabaseToBlobStorage(database, objectStorage)
database = ObjectStorageAPI.ReadDatabaseFromBlobStorage(objectStorage)
fmt.Println("Database after faking:")
utilities.PrintingDatabaseToConsole(database)
case "2":
fmt.Println("You chose to print all transactions")
database = ObjectStorageAPI.ReadDatabaseFromBlobStorage(objectStorage)
utilities.PrintingDatabaseToConsole(database)
break
case "3":
fmt.Println("You chose to exit")
return
default:
fmt.Println("You chose an invalid option")
break
}
}
}
func ClientUserInputLoop(clientConfig Config, isAlsoServer bool) {
for true {
var operation string
fmt.Println()
fmt.Println()
fmt.Println("Please enter the operation you want to do")
fmt.Println("1. Create a transaction and ask node to spread it")
fmt.Println("2. Ask node to rate a transaction")
fmt.Println("3. Ask node to fake a transaction")
fmt.Println("4. Ask node to list all transactions")
fmt.Println("5. Exit")
fmt.Print("Your choice: ")
_, err := fmt.Scanln(&operation)
if err != nil {
fmt.Println("error :", err.Error())
os.Exit(1)
}
switch operation {
case "1":
fmt.Println("You chose to create a transaction")
if !isAlsoServer {
newTrans := userCreatedTransaction(clientConfig)
utilities.PrintNeighbors(clientConfig.Neighbours)
fmt.Println("TRANSACTION READY TO BE SENT")
fmt.Println("Please enter the ID of the neighbour you want to send the transaction to")
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)
}
Sender.SendTransactionToNeighbour(clientConfig, newTrans, clientConfig.Neighbours[neighbourIDInt].Address, strconv.Itoa(clientConfig.Neighbours[neighbourIDInt].Port))
}
break
case "2":
fmt.Println("You chose to rate a transaction")
fmt.Println("We will ask you for an id of a transaction to rate")
fmt.Println("The given node will send a request to all its neighbours to rate the transaction")
fmt.Println("The node will then print the result of the rating")
fmt.Print("\nPlease enter the id of the transaction you want to rate:")
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)
}
utilities.PrintNeighbors(clientConfig.Neighbours)
fmt.Println("Please enter the ID of the neighbour you want to send the rate request transaction to")
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)
}
address := clientConfig.Neighbours[neighbourIDInt].Address + ":" + strconv.Itoa(clientConfig.Neighbours[neighbourIDInt].Port)
fmt.Println("Sending rate demand to ", address)
Sender.SendVoteRequestToNode(clientConfig, transIDInt, address)
break
case "3":
fmt.Println("You chose to fake a transaction")
fmt.Println("We will ask you for the id of a transaction to fake")
fmt.Println("and the node you want to ask to fake it")
fmt.Println("The node will receive the request and change the transaction with a fake one")
fmt.Print("\nPlease enter the id of the transaction you want to fake:")
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)
}
utilities.PrintNeighbors(clientConfig.Neighbours)
fmt.Println("Please enter the ID of the neighbour you want to send the rate request transaction to")
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)
}
address := clientConfig.Neighbours[neighbourIDInt].Address + ":" + strconv.Itoa(clientConfig.Neighbours[neighbourIDInt].Port)
fmt.Println("Sending rate demand to ", address)
Sender.SendFakeRequestToNode(clientConfig, transIDInt, address)
break
case "4":
fmt.Println("You chose to ask for all transactions of a given node")
utilities.PrintNeighbors(clientConfig.Neighbours)
fmt.Println("We will ask you for the ID of the neighbour you want to ask for the transactions")
fmt.Println("The node will then print locally its transactions")
fmt.Println("Please enter the ID of the neighbour you want to send the print request transaction to")
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)
}
address := clientConfig.Neighbours[neighbourIDInt].Address + ":" + strconv.Itoa(clientConfig.Neighbours[neighbourIDInt].Port)
fmt.Println("Sending rate demand to ", address)
Sender.SendListTransactionsRequestToNode(clientConfig, address)
break
case "5":
fmt.Println("You chose to exit")
return
default:
fmt.Println("You chose an invalid option")
break
}
}
}