diff --git a/README.md b/README.md index e53b7552ece8cca16ddd7b7998fb23e8a9fd1d6b..a0b4a0f3aa68290c86f54adaeb8879e6a7577ffc 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# How to setup +# How to setup local ``` cd local/ go mod init local @@ -22,4 +22,29 @@ go build client.go //vote and fake transactions not implemented yet. ``` -The file types/types.go is shared accross the two packages. It is imported as an external package inside the two main programs (server and client) \ No newline at end of file +The file types/types.go is shared accross the two packages. It is imported as an external package inside the two main programs (server and client) + +# How to getup global +You will need to deploy and .env file with the AZ_STORAGE_CONNECTION_STRING setup to access the containers +The containers name must be of the form container-{id} where id is the id in the neighbour-x.yaml file + +``` +cd global/ +go mod init global +go mod tidy +cd server/ +go build server.go + +deploy server executable to instances + +cd client/ +go build client.go +./client <ip_server> <action> +//action : 0 (create transaction), 1 (list transactions) + +//to create a transaction +./client <ip_server> 0 <id:int> <from:string> <to:string> <amount:int> + +//to list transactions +./client <ip_server> 1 +``` \ No newline at end of file diff --git a/global/client/client b/global/client/client index d23c565b1ee0655215bd60f52f633f43090d0742..c914210a9d90d93167b5cbaee669cf1a6a6980fd 100755 Binary files a/global/client/client and b/global/client/client differ diff --git a/global/client/client.go b/global/client/client.go index 52ed843c6c0baa71ea4d51b0e8bfb3399781d0a6..812096d509e71cfba5435192f6574ed2a159d761 100644 --- a/global/client/client.go +++ b/global/client/client.go @@ -3,7 +3,7 @@ package main import ( "encoding/json" "fmt" - types "local/helper" + types "global/helper" "net" "os" "strconv" diff --git a/global/go.mod b/global/go.mod index b6307071dd46e3338ca7b29cbe022c1e5a6134b6..43f4b53b589822894045ff1974ad783a0b5937ad 100644 --- a/global/go.mod +++ b/global/go.mod @@ -1,6 +1,6 @@ -module local +module global -go 1.18 +go 1.19 require ( github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v0.5.1 diff --git a/global/helper/azure_handler.go b/global/helper/azure_handler.go index bd3d5504bda3d67e006611c04556c9694bdc0778..03bb9c3c8140bc59750af4c01f5bd2346d4f338a 100644 --- a/global/helper/azure_handler.go +++ b/global/helper/azure_handler.go @@ -12,7 +12,7 @@ var client *azblob.Client var err error var ctx context.Context -func ConnectToStorage(connectionString string) { +func ConnectToStorage(connectionString string, container string, blobName string) { client, err = azblob.NewClientFromConnectionString(connectionString, nil) if err != nil { panic(err) @@ -47,8 +47,12 @@ func ReadFromStorage(containerName string, blobName string) []byte { } func WriteToStorage(containerName string, blobName string, data []byte) { + if err != nil { + panic(err) + } + blobContentReader := bytes.NewReader(data) - _, err := client.UploadStream(ctx, containerName, blobName, blobContentReader, nil) + _, err = client.UploadStream(ctx, containerName, blobName, blobContentReader, nil) if err != nil { panic(err) } diff --git a/global/neighbour-1.yaml b/global/neighbour-1.yaml index fa02d144e588215916cae75b4311f6d1724de78e..3b28e11ce4edfce53834c89fef35d7aa9438fb8c 100644 --- a/global/neighbour-1.yaml +++ b/global/neighbour-1.yaml @@ -1,10 +1,10 @@ id: 1 -address: "127.0.0.11" +address: "10.0.0.11" neighbours: - id: 2 - address: "127.0.0.12" + address: "10.0.0.12" edge_weight: 0 - id: 3 - address: "127.0.0.13" + address: "10.0.0.13" edge_weight: 0 \ No newline at end of file diff --git a/global/neighbour-2.yaml b/global/neighbour-2.yaml index b4131ad0485ad00bf39179d0d0250a23ac2d9a4e..05fa00317b859d3d78b37ea53aaa08d9e6cb6af6 100644 --- a/global/neighbour-2.yaml +++ b/global/neighbour-2.yaml @@ -1,10 +1,10 @@ id: 2 -address: "127.0.0.12" +address: "10.0.0.12" neighbours: - id: 1 - address: "127.0.0.11" + address: "10.0.0.11" edge_weight: 0 - id: 6 - address: "127.0.0.16" + address: "10.0.0.16" edge_weight: 0 \ No newline at end of file diff --git a/global/neighbour-3.yaml b/global/neighbour-3.yaml index 07763a7193af0ba8a19b0e4f12aa1b4e848475fa..589f560016789f6806dc50a3090640585944453c 100644 --- a/global/neighbour-3.yaml +++ b/global/neighbour-3.yaml @@ -1,10 +1,10 @@ id: 3 -address: "127.0.0.13" +address: "10.0.0.13" neighbours: - id: 1 - address: "127.0.0.11" + address: "10.0.0.11" edge_weight: 0 - id: 4 - address: "127.0.0.14" + address: "10.0.0.14" edge_weight: 0 \ No newline at end of file diff --git a/global/neighbour-4.yaml b/global/neighbour-4.yaml index 6af3c01402c2a974c1c65170559a7d8afcaf847b..6eedfc26874581cf4e999d327e4a0dc3b05fff47 100644 --- a/global/neighbour-4.yaml +++ b/global/neighbour-4.yaml @@ -1,10 +1,10 @@ id: 4 -address: "127.0.0.14" +address: "10.0.0.14" neighbours: - id: 3 - address: "127.0.0.13" + address: "10.0.0.13" edge_weight: 0 - id: 5 - address: "127.0.0.15" + address: "10.0.0.15" edge_weight: 0 \ No newline at end of file diff --git a/global/neighbour-5.yaml b/global/neighbour-5.yaml index e696a9131ed962dcc0815b861fc5fd58121c7c3d..90159a084a9d59814b2ec033dee5583f796de97c 100644 --- a/global/neighbour-5.yaml +++ b/global/neighbour-5.yaml @@ -1,10 +1,10 @@ id: 5 -address: "127.0.0.15" +address: "10.0.0.15" neighbours: - id: 4 - address: "127.0.0.14" + address: "10.0.0.14" edge_weight: 0 - id: 6 - address: "127.0.0.16" + address: "10.0.0.16" edge_weight: 0 \ No newline at end of file diff --git a/global/neighbour-6.yaml b/global/neighbour-6.yaml index c51edd45254d4473d2a86d137f8d6d3886eda0c6..6b97cca918c148b9e893a722c49a7445f221e1f7 100644 --- a/global/neighbour-6.yaml +++ b/global/neighbour-6.yaml @@ -1,10 +1,10 @@ id: 6 -address: "127.0.0.16" +address: "10.0.0.16" neighbours: - id: 2 - address: "127.0.0.12" + address: "10.0.0.12" edge_weight: 0 - id: 5 - address: "127.0.0.15" + address: "10.0.0.15" edge_weight: 0 \ No newline at end of file diff --git a/global/server/.env-sample b/global/server/.env-sample index 30c297d8a055d7f685203f716b3d74e70a985d85..6ce7fa153d40c674f2ba3a72eab4d880fc3def07 100644 --- a/global/server/.env-sample +++ b/global/server/.env-sample @@ -1,2 +1 @@ -AZ_ACCOUNT_ACCESS_KEY= -AZ_STORAGE_BLOB_URL= \ No newline at end of file +AZ_STORAGE_CONNECTION_STRING= \ No newline at end of file diff --git a/global/server/server b/global/server/server index b38d1bb3d9694f013c8aa73138e8ca23893c507b..9e0f5cfabb5b6305a8de07e787b8336f2e889089 100755 Binary files a/global/server/server and b/global/server/server differ diff --git a/global/server/server.go b/global/server/server.go index a7521da15f11b7a6535b091d3b936e92335222f7..6a64132cd365856d38088ac3d0e6cbd37cf27a99 100644 --- a/global/server/server.go +++ b/global/server/server.go @@ -4,8 +4,8 @@ import ( "encoding/json" "fmt" dotenv "github.com/joho/godotenv" + helper "global/helper" "gopkg.in/yaml.v2" - helper "local/helper" "net" "os" ) @@ -16,8 +16,6 @@ type Config struct { Neighbours []helper.Node `yaml:"neighbours"` } -var transactions []helper.Transaction - var config Config var node helper.CurrentNode @@ -39,7 +37,7 @@ func main() { config := parseYAML(yaml) initNodeFromConfig(config) connectionString := os.Getenv("AZ_STORAGE_CONNECTION_STRING") - helper.ConnectToStorage(connectionString) + helper.ConnectToStorage(connectionString, node.Container, TransactionsFileName) fmt.Printf("Loaded yaml file: %s\n", yaml) @@ -103,8 +101,6 @@ func processClient(connection net.Conn) { break } - //send outgoing message - //_, err = connection.Write([]byte("Thanks! Got your message:" + string(buffer[:mlen]))) connection.Close() } @@ -133,14 +129,11 @@ func AppendTransactionToStorage(container string, name string, t helper.Transact //append to array //parse array into json //store on object storage - data := helper.ReadFromStorage(container, TransactionsFileName) - var transactions []helper.Transaction - err := json.Unmarshal(data, &transactions) - if err != nil { - panic(err) - } + transactions := readTransactionsFromStorage(container, TransactionsFileName) transactions = append(transactions, t) - data, err = json.Marshal(transactions) + fmt.Printf("Transactions[append]: %s", transactions) + data, err := json.Marshal(transactions) + fmt.Printf("Marshal: %s", data) if err != nil { panic(err) } @@ -148,6 +141,7 @@ func AppendTransactionToStorage(container string, name string, t helper.Transact } func listTransactions(connection net.Conn) { + transactions := readTransactionsFromStorage(node.Container, TransactionsFileName) m := helper.Message{ Action: helper.MListTransactions, Proactive: proactive, @@ -200,6 +194,13 @@ func sendTransactionToServer(t helper.Transaction, address string) { } func isTransactionKnown(t helper.Transaction) bool { + //read transactions.json from object storage + //parse json to transactions array + //check in array if transaction exists + + transactions := readTransactionsFromStorage(node.Container, TransactionsFileName) + fmt.Printf("%s", transactions) + for _, transaction := range transactions { if transaction.Id == t.Id { fmt.Printf("Transaction already exists\n") @@ -208,3 +209,13 @@ func isTransactionKnown(t helper.Transaction) bool { } return false } + +func readTransactionsFromStorage(container string, blobName string) []helper.Transaction { + data := helper.ReadFromStorage(node.Container, TransactionsFileName) + var transactions []helper.Transaction + err := json.Unmarshal(data, &transactions) + if err != nil { + panic(err) + } + return transactions +}