From 520c4f932a6a9c9fd5db09bf5b333d7845631b04 Mon Sep 17 00:00:00 2001 From: Xavier Perret <xa.perret@protonmail.ch> Date: Wed, 12 Oct 2022 16:11:56 +0200 Subject: [PATCH] refactoring --- app/main.go | 24 +--- app/object-storage/object-storage.go | 164 +++++++++++++++++++++++++++ app/test.go | 11 ++ app/types/datastructures.go | 24 ++++ 4 files changed, 201 insertions(+), 22 deletions(-) create mode 100644 app/object-storage/object-storage.go create mode 100644 app/test.go create mode 100644 app/types/datastructures.go diff --git a/app/main.go b/app/main.go index eb09e4a..addbe37 100644 --- a/app/main.go +++ b/app/main.go @@ -7,6 +7,7 @@ import ( "io/ioutil" "math/rand" "net" + . "node/types" "os" "strconv" "strings" @@ -20,27 +21,6 @@ import ( * @date 28/09/2022 - 05/10/2022 */ -// Message - MessageBody can be of any type but will be use here for Transaction, AckTransaction -type Message struct { - MessageType string `json:"messageType"` - MessageBody interface{} `json:"messageBody"` -} - -// Transaction - represents a transaction in the blockchain -type Transaction struct { - Id string `json:"id"` - Sender string `json:"sender"` - Receiver string `json:"receiver"` - Amount string `json:"amount"` -} - -// AckTransaction - represents an acknowledgement of a transaction @see vote function -type AckTransaction struct { - Id string `json:"id"` - AmountOfCorrectNode int `json:"amountOfCorrectNode"` - TotalNodes int `json:"totalNodes"` -} - // Config - is the configuration of a node, it's id, address, port and neighbours type Config struct { ID int `yaml:"id"` @@ -57,7 +37,7 @@ type Config struct { var ServerReady = false // DB in memory database -var DB []Transaction +var DB Database // ConnectionType is the type of connection used by the server var ConnectionType = "tcp" diff --git a/app/object-storage/object-storage.go b/app/object-storage/object-storage.go new file mode 100644 index 0000000..229ed5e --- /dev/null +++ b/app/object-storage/object-storage.go @@ -0,0 +1,164 @@ +package object_storage + +import ( + "bufio" + "bytes" + "context" + "fmt" + "github.com/Azure/azure-sdk-for-go/sdk/azidentity" + "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob" + "log" + "math/rand" + . "node/types" + "os" + "strconv" + "time" +) + +func addTransactionToBlobStorage(transaction Transaction) { + +} + +func removeTransactionFromBlobStorage(transaction Transaction) { + +} + +func fakeTransaction(transactionToFake Transaction, fakeTransaction Transaction) bool { + return false +} + +func writeDatabaseToFile(filepath string, db Database) { + +} + +func readDatabaseFromFile(filepath string) Database { + return nil +} + +func readDatabaseFromBlobStorage() Database { + return nil +} + +func writeDatabaseToBlobStorage() { + +} + +// Azure Storage Quickstart Sample - Demonstrate how to upload, list, download, and delete blobs. +// +// Documentation References: +// - What is a Storage Account - https://docs.microsoft.com/azure/storage/common/storage-create-storage-account +// - Blob Service Concepts - https://docs.microsoft.com/rest/api/storageservices/Blob-Service-Concepts +// - Blob Service Go SDK API - https://godoc.org/github.com/Azure/azure-storage-blob-go +// - Blob Service REST API - https://docs.microsoft.com/rest/api/storageservices/Blob-Service-REST-API +// - Scalability and performance targets - https://docs.microsoft.com/azure/storage/common/storage-scalability-targets +// - Azure Storage Performance and Scalability checklist https://docs.microsoft.com/azure/storage/common/storage-performance-checklist +// - Storage Emulator - https://docs.microsoft.com/azure/storage/common/storage-use-emulator + +func randomString() string { + r := rand.New(rand.NewSource(time.Now().UnixNano())) + return strconv.Itoa(r.Int()) +} + +func createBlobStorageClient() { + +} + +func TestObjectStorage() { + fmt.Printf("Azure Blob storage quick start sample\n") + + url := "https://hepiadistributedsystems.blob.core.windows.net/" //replace <StorageAccountName> with your Azure storage account name + ctx := context.Background() + + // Create a default request pipeline using your storage account name and account key. + credential, err := azidentity.NewDefaultAzureCredential(nil) + if err != nil { + log.Fatal("Invalid credentials with error: " + err.Error()) + } + + serviceClient, err := azblob.NewServiceClient(url, credential, nil) + if err != nil { + log.Fatal("Invalid credentials with error: " + err.Error()) + } + + // Create the container + containerName := fmt.Sprintf("quickstart-%s", randomString()) + fmt.Printf("Creating a container named %s\n", containerName) + containerClient := serviceClient.NewContainerClient(containerName) + _, err = containerClient.Create(ctx, nil) + if err != nil { + log.Fatal(err) + } + + fmt.Printf("Creating a dummy file to test the upload and download\n") + + data := []byte("\nhello world this is a blob\n") + blobName := "quickstartblob" + "-" + randomString() + + blobClient, err := azblob.NewBlockBlobClient(url+containerName+"/"+blobName, credential, nil) + if err != nil { + log.Fatal(err) + } + + // Upload to data to blob storage + _, err = blobClient.UploadBufferToBlockBlob(ctx, data, azblob.HighLevelUploadToBlockBlobOption{}) + + if err != nil { + log.Fatalf("Failure to upload to blob: %+v", err) + } + + // List the blobs in the container + fmt.Println("Listing the blobs in the container:") + + pager := containerClient.ListBlobsFlat(nil) + + for pager.NextPage(ctx) { + resp := pager.PageResponse() + + for _, v := range resp.ContainerListBlobFlatSegmentResult.Segment.BlobItems { + fmt.Println(*v.Name) + } + } + + if err = pager.Err(); err != nil { + log.Fatalf("Failure to list blobs: %+v", err) + } + + // Download the blob + get, err := blobClient.Download(ctx, nil) + if err != nil { + log.Fatal(err) + } + + downloadedData := &bytes.Buffer{} + reader := get.Body(azblob.RetryReaderOptions{}) + _, err = downloadedData.ReadFrom(reader) + if err != nil { + log.Fatal(err) + } + err = reader.Close() + if err != nil { + log.Fatal(err) + } + + fmt.Println(downloadedData.String()) + + fmt.Printf("Press enter key to delete the blob fils, example container, and exit the application.\n") + bufio.NewReader(os.Stdin).ReadBytes('\n') + fmt.Printf("Cleaning up.\n") + + // Delete the blob + fmt.Printf("Deleting the blob " + blobName + "\n") + + _, err = blobClient.Delete(ctx, nil) + if err != nil { + log.Fatalf("Failure: %+v", err) + } + + // Delete the container + fmt.Printf("Deleting the blob " + containerName + "\n") + _, err = containerClient.Delete(ctx, nil) + + if err != nil { + log.Fatalf("Failure: %+v", err) + } +} diff --git a/app/test.go b/app/test.go new file mode 100644 index 0000000..a455cb9 --- /dev/null +++ b/app/test.go @@ -0,0 +1,11 @@ +package main + +import ( + "fmt" + . "node/object-storage" +) + +func main() { + fmt.Println("Testing file for the application") + TestObjectStorage() +} diff --git a/app/types/datastructures.go b/app/types/datastructures.go new file mode 100644 index 0000000..9fd37aa --- /dev/null +++ b/app/types/datastructures.go @@ -0,0 +1,24 @@ +package types + +// Message - MessageBody can be of any type but will be use here for Transaction, AckTransaction +type Message struct { + MessageType string `json:"messageType"` + MessageBody interface{} `json:"messageBody"` +} + +// Transaction - represents a transaction in the blockchain +type Transaction struct { + Id string `json:"id"` + Sender string `json:"sender"` + Receiver string `json:"receiver"` + Amount string `json:"amount"` +} + +// AckTransaction - represents an acknowledgement of a transaction @see vote function +type AckTransaction struct { + Id string `json:"id"` + AmountOfCorrectNode int `json:"amountOfCorrectNode"` + TotalNodes int `json:"totalNodes"` +} + +type Database []Transaction -- GitLab