dc4bc/main.go

143 lines
3.8 KiB
Go
Raw Normal View History

2020-07-22 04:53:06 -07:00
package main
import (
2020-08-09 14:37:53 -07:00
"context"
2020-07-23 06:29:20 -07:00
"fmt"
2020-07-22 04:53:06 -07:00
_ "image/jpeg"
"log"
2020-07-23 06:29:20 -07:00
"sync"
2020-07-22 04:53:06 -07:00
2020-08-09 14:37:53 -07:00
"github.com/depools/dc4bc/qr"
"github.com/depools/dc4bc/storage"
2020-07-30 03:29:47 -07:00
2020-08-09 14:37:53 -07:00
"github.com/depools/dc4bc/client"
2020-07-22 04:53:06 -07:00
_ "image/gif"
_ "image/png"
_ "golang.org/x/image/bmp"
_ "golang.org/x/image/tiff"
_ "golang.org/x/image/webp"
)
2020-07-23 06:29:20 -07:00
type Transport struct {
2020-08-09 14:37:53 -07:00
nodes []*client.Client
2020-07-23 08:39:56 -07:00
}
2020-07-22 04:53:06 -07:00
func main() {
2020-07-23 06:29:20 -07:00
var transport = &Transport{}
var numNodes = 4
2020-08-09 14:37:53 -07:00
for nodeID := 0; nodeID < numNodes; nodeID++ {
var ctx = context.Background()
var state, err = client.NewLevelDBState(fmt.Sprintf("/tmp/dc4bc_node_%d_state", nodeID))
if err != nil {
log.Fatalf("node %d failed to init state: %v\n", nodeID, err)
2020-07-22 04:53:06 -07:00
}
2020-08-09 14:37:53 -07:00
stg, err := storage.NewFileStorage("/tmp/dc4bc_storage")
2020-07-23 08:39:56 -07:00
if err != nil {
2020-08-09 14:37:53 -07:00
log.Fatalf("node %d failed to init storage: %v\n", nodeID, err)
2020-07-23 08:39:56 -07:00
}
2020-08-09 14:37:53 -07:00
clt, err := client.NewClient(
ctx,
nil,
state,
stg,
qr.NewCameraProcessor(),
)
2020-07-23 08:39:56 -07:00
if err != nil {
2020-08-09 14:37:53 -07:00
log.Fatalf("node %d failed to init client: %v\n", nodeID, err)
2020-07-23 08:39:56 -07:00
}
2020-08-09 14:37:53 -07:00
transport.nodes = append(transport.nodes, clt)
}
2020-07-23 08:39:56 -07:00
2020-08-09 14:37:53 -07:00
for nodeID, node := range transport.nodes {
go func(nodeID int, node *client.Client) {
if err := node.StartHTTPServer(fmt.Sprintf("localhost:808%d", nodeID)); err != nil {
log.Fatalf("client %d http server failed: %v\n", nodeID, err)
}
}(nodeID, node)
2020-07-23 08:39:56 -07:00
2020-08-09 14:37:53 -07:00
go func(nodeID int, node *client.Client) {
if err := node.Poll(); err != nil {
log.Fatalf("client %d poller failed: %v\n", nodeID, err)
}
}(nodeID, node)
2020-07-22 04:53:06 -07:00
2020-08-09 14:37:53 -07:00
log.Printf("client %d started...\n", nodeID)
2020-07-23 06:29:20 -07:00
}
2020-08-09 14:37:53 -07:00
var wg = sync.WaitGroup{}
wg.Add(1)
2020-07-23 06:29:20 -07:00
wg.Wait()
2020-07-22 04:53:06 -07:00
}
2020-07-23 06:29:20 -07:00
2020-08-09 14:37:53 -07:00
// // Participants broadcast PKs.
// runStep(transport, func(participantID string, participant *dkglib.DKG, wg *sync.WaitGroup) {
// transport.BroadcastPK(participantID, participant.GetPubKey())
// wg.Done()
// })
2020-07-23 06:29:20 -07:00
//
2020-08-09 14:37:53 -07:00
// // Participants init their DKGInstances.
// runStep(transport, func(participantID string, participant *dkglib.DKG, wg *sync.WaitGroup) {
// if err := participant.InitDKGInstance(threshold); err != nil {
// log.Fatalf("Failed to InitDKGInstance: %v", err)
// }
// wg.Done()
// })
//
// // Participants broadcast their Commits.
// runStep(transport, func(participantID string, participant *dkglib.DKG, wg *sync.WaitGroup) {
// commits := participant.GetCommits()
// transport.BroadcastCommits(participantID, commits)
// wg.Done()
// })
2020-07-23 06:29:20 -07:00
//
2020-08-09 14:37:53 -07:00
// // Participants broadcast their deal.
// runStep(transport, func(participantID string, participant *dkglib.DKG, wg *sync.WaitGroup) {
// deals, err := participant.GetDeals()
2020-07-23 06:29:20 -07:00
// if err != nil {
2020-08-09 14:37:53 -07:00
// log.Fatalf("failed to getDeals for participant %s: %v", participantID, err)
2020-07-23 06:29:20 -07:00
// }
2020-08-09 14:37:53 -07:00
// transport.BroadcastDeals(participantID, deals)
// wg.Done()
// })
2020-07-23 06:29:20 -07:00
//
2020-08-09 14:37:53 -07:00
// // Participants broadcast their responses.
// runStep(transport, func(participantID string, participant *dkglib.DKG, wg *sync.WaitGroup) {
// responses, err := participant.ProcessDeals()
// if err != nil {
// log.Fatalf("failed to ProcessDeals for participant %s: %v", participantID, err)
// }
// transport.BroadcastResponses(participantID, responses)
// wg.Done()
// })
2020-07-23 06:29:20 -07:00
//
2020-08-09 14:37:53 -07:00
// // Participants process their responses.
// runStep(transport, func(participantID string, participant *dkglib.DKG, wg *sync.WaitGroup) {
// if err := participant.ProcessResponses(); err != nil {
// log.Fatalf("failed to ProcessResponses for participant %s: %v", participantID, err)
2020-07-23 06:29:20 -07:00
// }
2020-08-09 14:37:53 -07:00
// wg.Done()
// })
2020-07-23 06:29:20 -07:00
//
2020-08-09 14:37:53 -07:00
// for idx, node := range transport.nodes {
// if err := node.Reconstruct(); err != nil {
// fmt.Println("Node", idx, "is not ready:", err)
// } else {
// fmt.Println("Node", idx, "is ready")
// }
// }
2020-07-23 06:29:20 -07:00
//}
//
2020-08-09 14:37:53 -07:00
//func runStep(transport *Transport, cb func(participantID string, participant *dkglib.DKG, wg *sync.WaitGroup)) {
// var wg = &sync.WaitGroup{}
// for idx, node := range transport.nodes {
// wg.Add(1)
// n := node
// go cb(fmt.Sprintf("participant_%d", idx), n, wg)
// }
// wg.Wait()
2020-07-23 06:29:20 -07:00
//}