Skip to content

Commit

Permalink
[DOC] complete documents
Browse files Browse the repository at this point in the history
  • Loading branch information
myth committed May 16, 2024
1 parent b1ef573 commit 8564f3f
Show file tree
Hide file tree
Showing 59 changed files with 574 additions and 637 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ test-subscriber:
go run tests/kafka/subscriber/main.go
test-mongo:
go run tests/mongo/main.go
test-ganache:
go run tests/ganache/main.go

unit-test:
go test ./...


start-blockchain-algorithm:
go run tests/blockchain/main.go
go run main.go blockchainAlgorithm
337 changes: 189 additions & 148 deletions README.md

Large diffs are not rendered by default.

39 changes: 39 additions & 0 deletions cmd/blockchainAlgorithm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
"github.com/spf13/cobra"

"openmyth/blockchain/cmd/srv/blockchain_algorithm"
)

// blockchainAlgorithmCmd represents the blockchainAlgorithm command
var blockchainAlgorithmCmd = &cobra.Command{
Use: "blockchainAlgorithm",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
blockchain_algorithm.Run(cmd.Context())
},
}

func init() {
rootCmd.AddCommand(blockchainAlgorithmCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// blockchainAlgorithmCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// blockchainAlgorithmCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
37 changes: 22 additions & 15 deletions tests/blockchain/main.go → cmd/srv/blockchain_algorithm/srv.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main
package blockchain_algorithm

import (
"context"
"flag"
"fmt"
"log"
Expand Down Expand Up @@ -33,19 +34,19 @@ func getPromptTemplate() *promptui.PromptTemplates {
}

const (
// FEATURE_ADD_DATA_TEXT ...
FEATURE_ADD_DATA_TEXT = "add data text"
// FEATURE_LIST_BLOCK ...
FEATURE_LIST_BLOCK = "list block"

FEATURE_EXIT = "exit"
// FeatureAddDataText represents for add data interaction
FeatureAddDataText = "add data text"
// FeatureListBlock represents for list block interaction
FeatureListBlock = "list block"
// FeatureExit represents for exit
FeatureExit = "exit"
)

var (
chooseFeature = promptui.Select{
Label: "Choose feature",
Items: []string{FEATURE_ADD_DATA_TEXT, FEATURE_LIST_BLOCK, FEATURE_EXIT},
Templates: getSelectTemplate("feature"),
Label: "Choose your interaction",
Items: []string{FeatureAddDataText, FeatureListBlock, FeatureExit},
Templates: getSelectTemplate("action"),
}

addData = promptui.Prompt{
Expand All @@ -57,11 +58,13 @@ var (
}
)

func main() {
// Run runs the blockchain algorithm with the specified context.
//
// ctx: the context to run the blockchain algorithm
func Run(_ context.Context) {
logger := slog.New(tint.NewHandler(os.Stdout, nil))

slog.SetDefault(logger)
// slog.SetLogLoggerLevel(slog.LevelDebug)

var (
numMiner int
Expand Down Expand Up @@ -92,8 +95,9 @@ func main() {
if err != nil {
log.Fatalf("failed to select feature: %v", err)
}
isExit := false
switch feature {
case FEATURE_ADD_DATA_TEXT:
case FeatureAddDataText:
data, err := addData.Run()
if err != nil {
log.Printf("invalid data: %v", err)
Expand All @@ -104,7 +108,7 @@ func main() {
miner.TransactionCh <- []byte(data)
}

case FEATURE_LIST_BLOCK:
case FeatureListBlock:
num := rand.Intn(numMiner)
bc := miners[num].GetBlockChain()
for _, block := range bc.GetBlocks() {
Expand All @@ -117,7 +121,10 @@ func main() {
nonce: %d
`, block.Index, block.Timestamp, block.Data, block.PrevBlockHash, block.Hash, block.Nonce)
}
case FEATURE_EXIT:
case FeatureExit:
isExit = true
}
if isExit {
break
}
}
Expand Down
15 changes: 6 additions & 9 deletions cmd/srv/contract_reader/srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ type Server struct {
service *processor.Service
}

// NewServer creates a new server instance with a new service.
func NewServer() *Server {
return &Server{
service: processor.NewService(),
}
}

// loadClients initializes the user client and factories for the server.
func (s *Server) loadClients() {
userConn := grpc_client.NewGrpcClient(s.service.Cfg.UserService)

Expand All @@ -52,19 +54,22 @@ func (s *Server) loadClients() {

}

// loadDatabases initializes the MongoDB client for the server.
func (s *Server) loadDatabases() {
s.mongoClient = mongoclient.NewMongoClient(s.service.Cfg.MongoDB.Address())

s.service.WithFactories(s.mongoClient)
}

// loadPublisher initializes a publisher for the server.
func (s *Server) loadPublisher() {
clientID := uuid.NewString()
s.publisher = kafka.NewPublisher(clientID, s.service.Cfg.Kafka.Address())

s.service.WithFactories(s.publisher)
}

// loadEthClient initializes the Ethereum client for the server.
func (s *Server) loadEthClient(ctx context.Context) {
cfg := s.service.Cfg

Expand All @@ -73,6 +78,7 @@ func (s *Server) loadEthClient(ctx context.Context) {
s.service.WithFactories(s.ethClient)
}

// loadRepositories initializes the necessary repositories for the server.
func (s *Server) loadRepositories() {
s.approvalRepo = mongo.NewApprovalRepository(s.mongoClient, s.service.Cfg.MongoDB.Database)
s.transferRepo = mongo.NewTransferRepository(s.mongoClient, s.service.Cfg.MongoDB.Database)
Expand All @@ -81,17 +87,11 @@ func (s *Server) loadRepositories() {
}

// loadServices initializes the contract reader service with the necessary repositories and publisher.
//
// No parameters.
// No return value.
func (s *Server) loadServices() {
s.contractReaderService = services.NewContractReaderService(s.approvalRepo, s.transferRepo, s.blockchainRepo, s.myTokenRepo, s.userClient, s.publisher)
}

// loadServer initializes the gRPC server for the Contract Reader Service.
//
// No parameters.
// No return value.
func (s *Server) loadServer() {
srv := grpc_server.NewGrpcServer(s.service.Cfg.ContractReaderService)

Expand All @@ -101,9 +101,6 @@ func (s *Server) loadServer() {
}

// Run runs the server with the provided context.
//
// ctx: the context.Context for the server.
// No return value.
func (s *Server) Run(ctx context.Context) {
s.service.LoadLogger()
s.service.LoadConfig()
Expand Down
9 changes: 6 additions & 3 deletions cmd/srv/contract_writer/srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,21 @@ type Server struct {
service *processor.Service
}

// NewServer returns a new Server instance with a new service.
func NewServer() *Server {
return &Server{
service: processor.NewService(),
}
}

// loadDatabases initializes the MongoDB client for the server.
func (s *Server) loadDatabases() {
s.mongoClient = mongoclient.NewMongoClient(s.service.Cfg.MongoDB.Address())

s.service.WithFactories(s.mongoClient)
}

// loadEthClient initializes the Ethereum clients for the server.
func (s *Server) loadEthClient(_ context.Context) {
cfg := s.service.Cfg

Expand All @@ -55,17 +58,20 @@ func (s *Server) loadEthClient(_ context.Context) {
s.service.WithFactories(s.ethClient, s.wsEthClient)
}

// loadRepositories initializes the necessary repositories for the server.
func (s *Server) loadRepositories() {
s.approvalRepo = mongo.NewApprovalRepository(s.mongoClient, s.service.Cfg.MongoDB.Database)
s.transferRepo = mongo.NewTransferRepository(s.mongoClient, s.service.Cfg.MongoDB.Database)
s.myTokenRepo = eth.NewMyTokenRepository(s.ethClient, s.wsEthClient, s.service.Cfg.ContractAddress)
s.blockchainRepo = eth.NewBlockchainRepository(s.ethClient)
}

// loadServices initializes the services in the server with the provided approval repository, transfer repository, MyToken repository, and blockchain repository.
func (s *Server) loadServices() {
s.contractWriter = services.NewContractWriterService(s.approvalRepo, s.transferRepo, s.myTokenRepo, s.blockchainRepo)
}

// loadSubscriber initializes the subscriber for the server with the specified topics and contract writer callback.
func (s *Server) loadSubscriber() {
s.subscriber = kafka.NewSubscriber(
os.Getenv("SERVICE"),
Expand All @@ -82,9 +88,6 @@ func (s *Server) loadSubscriber() {
}

// Run runs the server with the provided context.
//
// ctx: the context.Context for the server.
// No return value.
func (s *Server) Run(ctx context.Context) {
s.service.LoadLogger()
s.service.LoadConfig()
Expand Down
9 changes: 7 additions & 2 deletions cmd/srv/deploy_contract/srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,31 @@ type Server struct {
deployContract *services.DeployContractService
}

// NewServer creates a new server instance.
func NewServer() *Server {
return &Server{
service: *processor.NewService(),
}
}
func (s *Server) loadEthClient(ctx context.Context) {

// loadEthClient initializes the Ethereum client for the server.
func (s *Server) loadEthClient(_ context.Context) {
cfg := s.service.Cfg

s.ethClient = eth_client.NewDialClient(cfg.ETHClient.Address())

s.service.WithFactories(s.ethClient)
}

func (s *Server) loadServices(ctx context.Context) {
// loadServices initializes the services in the server with the provided Ethereum client and private key.
func (s *Server) loadServices(_ context.Context) {
cfg := s.service.Cfg
s.deployContract = services.NewDeployContractService(s.ethClient, cfg.PrivateKey)

s.service.WithProcessors(s.deployContract)
}

// Run runs the server with the provided context.
func (s *Server) Run(ctx context.Context) {
s.service.LoadLogger()
s.service.LoadConfig()
Expand Down
3 changes: 3 additions & 0 deletions cmd/srv/frontend/srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ type Server struct {
deployContract *services.DeployContractService
}

// NewServer creates a new server instance.
func NewServer() *Server {
return &Server{
service: *processor.NewService(),
}
}

// loadServer initializes the server to listen on a specified port for HTTP requests.
func (s *Server) loadServer() {
port := s.service.Cfg.Frontend.Port
handler := http.FileServer(http.Dir("/html"))
Expand All @@ -30,6 +32,7 @@ func (s *Server) loadServer() {
}
}

// Run runs the server with the provided context.
func (s *Server) Run(ctx context.Context) {
s.service.LoadLogger()
s.service.LoadConfig()
Expand Down
4 changes: 4 additions & 0 deletions cmd/srv/gateway/srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ type Server struct {
service *processor.Service
}

// NewServer creates a new server instance.
func NewServer() *Server {
return &Server{
service: processor.NewService(),
}
}

// loadClients initializes the user client and contract reader client for the server.
func (s *Server) loadClients() {
userConn := grpc_client.NewGrpcClient(s.service.Cfg.UserService)
contractReaderConn := grpc_client.NewGrpcClient(s.service.Cfg.ContractReaderService)
Expand All @@ -38,6 +40,7 @@ func (s *Server) loadClients() {

}

// loadServer initializes the HTTP server with the necessary handlers and processors.
func (s *Server) loadServer(ctx context.Context) {
srv := http_server.NewHttpServer(func(mux *runtime.ServeMux) {

Expand All @@ -50,6 +53,7 @@ func (s *Server) loadServer(ctx context.Context) {
s.service.WithProcessors(srv)
}

// Run runs the server with the provided context.
func (s *Server) Run(ctx context.Context) {
s.service.LoadLogger()
s.service.LoadConfig()
Expand Down
Binary file added docs/wiki/deploy_flow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/wiki/send_transaction_flow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 0 additions & 8 deletions internal/blockchain/watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ type defaultWatcher struct {
}

// NewWatcher creates a new Watcher instance.
//
// It takes a *eth.EthClient as a parameter and returns a Watcher.
func NewWatcher(myTokenRepo repositories.MyTokenRepository, publisher pubsub.Publisher) Watcher {
return &defaultWatcher{
Expand All @@ -40,7 +39,6 @@ func NewWatcher(myTokenRepo repositories.MyTokenRepository, publisher pubsub.Pub
}

// Start starts the defaultWatcher.
//
// It takes a context.Context as a parameter and returns an error.
func (w *defaultWatcher) Start(ctx context.Context) error {
if err := w.migrate(ctx); err != nil {
Expand Down Expand Up @@ -71,7 +69,6 @@ func (w *defaultWatcher) Start(ctx context.Context) error {
}

// Stop stops the defaultWatcher.
//
// It takes a context.Context as a parameter and returns an error.
func (w *defaultWatcher) Stop(_ context.Context) error {
w.isRunning = false
Expand All @@ -80,7 +77,6 @@ func (w *defaultWatcher) Stop(_ context.Context) error {
}

// handleEventLog executes a watch on the given event log.
//
// It takes a types.Log parameter named evLog and returns an error.
func (w *defaultWatcher) handleEventLog(evLog types.Log) {
if approval, err := w.myTokenRepo.ParseApproval(evLog); err == nil && approval != nil {
Expand Down Expand Up @@ -122,10 +118,6 @@ func (w *defaultWatcher) handleEventLog(evLog types.Log) {
}

// migrate migrates the watcher by filtering logs based on the contract address and handling each event log.
//
// Parameters:
// - ctx: The context.Context object for cancellation and timeouts.
// Return type: An error.
func (w *defaultWatcher) migrate(ctx context.Context) error {
query := ethereum.FilterQuery{
Addresses: []common.Address{
Expand Down
Loading

0 comments on commit 8564f3f

Please sign in to comment.