From 274266df44e7c48bfb94ed31380ce8f937dbc713 Mon Sep 17 00:00:00 2001 From: Andrej Zavgorodnij Date: Tue, 4 Aug 2020 10:45:32 +0300 Subject: [PATCH 1/2] feat: added cmd --- client/client.go | 3 + cmd/client/main.go | 93 ++++++++++++++++++++++++++ go.mod | 1 + go.sum | 128 ++++++++++++++++++++++++++++++++++++ qr/qr.go | 4 +- storage/fileStorage.go | 4 +- storage/fileStorage_test.go | 2 +- 7 files changed, 231 insertions(+), 4 deletions(-) create mode 100644 cmd/client/main.go diff --git a/client/client.go b/client/client.go index 5923173..a5677f1 100644 --- a/client/client.go +++ b/client/client.go @@ -6,6 +6,7 @@ import ( "fmt" "log" "path/filepath" + "sync" "time" "github.com/depools/dc4bc/qr" @@ -18,6 +19,7 @@ const ( ) type Client struct { + sync.Mutex ctx context.Context fsm interface{} state State @@ -86,6 +88,7 @@ func (c *Client) Poll() { } } case <-c.ctx.Done(): + log.Println("Context closed, stop polling...") return } } diff --git a/cmd/client/main.go b/cmd/client/main.go new file mode 100644 index 0000000..2f1c0af --- /dev/null +++ b/cmd/client/main.go @@ -0,0 +1,93 @@ +package main + +import ( + "context" + "log" + "os" + "os/signal" + "syscall" + + "github.com/depools/dc4bc/client" + "github.com/depools/dc4bc/qr" + "github.com/depools/dc4bc/storage" + + "github.com/spf13/cobra" +) + +const ( + flagListenAddr = "listen_addr" + flagStateDBDSN = "state_dbdsn" + flagStorageDBDSN = "storage_dbdsn" +) + +func init() { + rootCmd.PersistentFlags().String(flagListenAddr, "localhost:8080", "Listen address") + rootCmd.PersistentFlags().String(flagStateDBDSN, "./dc4bc_client_state", "State DBDSN") + rootCmd.PersistentFlags().String(flagStorageDBDSN, "./dc4bc_file_storage", "Storage DBDSN") +} + +var rootCmd = &cobra.Command{ + Use: "dc4bc_client", + Short: "dc4bc client implementation", + Run: func(cmd *cobra.Command, args []string) { + listenAddr, err := cmd.PersistentFlags().GetString(flagListenAddr) + if err != nil { + log.Fatalf("failed to read configuration: %v", err) + } + + stateDBDSN, err := cmd.PersistentFlags().GetString(flagStateDBDSN) + if err != nil { + log.Fatalf("failed to read configuration: %v", err) + } + + storageDBDSN, err := cmd.PersistentFlags().GetString(flagStorageDBDSN) + if err != nil { + log.Fatalf("failed to read configuration: %v", err) + } + + ctx := context.Background() + ctx, cancel := context.WithCancel(ctx) + + state, err := client.NewLevelDBState(stateDBDSN) + if err != nil { + log.Fatalf("Failed to init state client: %v", err) + } + + stg, err := storage.NewFileStorage(storageDBDSN) + if err != nil { + log.Fatalf("Failed to init storage client: %v", err) + } + + processor := qr.NewCameraProcessor() + + // TODO: create state machine. + + cli, err := client.NewClient(ctx, nil, state, stg, processor) + if err != nil { + log.Fatalf("Failed to init client: %v", err) + } + + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) + go func() { + <-sigs + + log.Println("Received signal, stopping client...") + cancel() + + log.Println("Client stopped, exiting") + os.Exit(0) + }() + + log.Printf("Client started on %s...", listenAddr) + if err := cli.StartHTTPServer(listenAddr); err != nil { + log.Fatalf("Client error: %v", err) + } + }, +} + +func main() { + if err := rootCmd.Execute(); err != nil { + log.Fatalf("Failed to execute root command: %v", err) + } +} diff --git a/go.mod b/go.mod index 1fcf577..48c4606 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/looplab/fsm v0.1.0 github.com/makiuchi-d/gozxing v0.0.0-20190830103442-eaff64b1ceb7 github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e + github.com/spf13/cobra v1.0.0 github.com/stretchr/testify v1.6.1 github.com/syndtr/goleveldb v1.0.0 go.dedis.ch/kyber/v3 v3.0.9 diff --git a/go.sum b/go.sum index 1bbc3d8..31a3765 100644 --- a/go.sum +++ b/go.sum @@ -1,35 +1,123 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= +github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= +github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= +github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= +github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk= +github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE= +github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= +github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4= +github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/corestario/kyber/v3 v3.0.0-20200218082721-8ed10c357c05 h1:ICuDs+sbQzDem2pIAFyI+u6s0RiETzMc2IhnobgHkvE= github.com/corestario/kyber/v3 v3.0.0-20200218082721-8ed10c357c05/go.mod h1:kXy7p3STAurkADD+/aZcsznZGKVHEqbtmdIzvPfrs1U= +github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= +github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= +github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= +github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc= github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4= github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db h1:woRePGFeVFfLKN/pOkfl+p/TAqKOfFu+7KPlMVpok/w= github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= +github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= +github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/jinzhu/configor v1.2.0 h1:u78Jsrxw2+3sGbGMgpY64ObKU4xWCNmNRJIjGVqxYQA= +github.com/jinzhu/configor v1.2.0/go.mod h1:nX89/MOmDba7ZX7GCyU/VIaQ2Ar2aizBl2d3JLF/rDc= +github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/juju/fslock v0.0.0-20160525022230-4d5c94c67b4b h1:FQ7+9fxhyp82ks9vAuyPzG0/vVbWwMwLJ+P6yJI5FN8= github.com/juju/fslock v0.0.0-20160525022230-4d5c94c67b4b/go.mod h1:HMcgvsgd0Fjj4XXDkbjdmlbI505rUPBs6WBMYg2pXks= +github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/looplab/fsm v0.1.0 h1:Qte7Zdn/5hBNbXzP7yxVU4OIFHWXBovyTT2LaBTyC20= github.com/looplab/fsm v0.1.0/go.mod h1:m2VaOfDHxqXBBMgc26m6yUOwkFn8H2AlJDE+jd/uafI= +github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/makiuchi-d/gozxing v0.0.0-20190830103442-eaff64b1ceb7 h1:CfWnkHgRG8zmxQI7RAhLIUFPkg+RfDdWiEtoE3y1+4w= github.com/makiuchi-d/gozxing v0.0.0-20190830103442-eaff64b1ceb7/go.mod h1:WoI7z45M7ZNA5BJxiJHaB+x7+k8S/3phW5Y13IR4yWY= +github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= +github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.7.0 h1:WSHQ+IS43OoUrWtD1/bbclrwK8TTH5hzp+umCiuxHgs= github.com/onsi/ginkgo v1.7.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU= github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY= +github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= +github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso= +github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= +github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= +github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= +github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= +github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= +github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU= +github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= +github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= +github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= +github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= +github.com/spf13/cobra v1.0.0 h1:6m/oheQuQ13N9ks4hubMG6BnvwOeaJrqSPLahSnczz8= +github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE= +github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= +github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= +github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= @@ -37,39 +125,79 @@ github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFd github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= github.com/tendermint/crypto v0.0.0-20180820045704-3764759f34a5 h1:u8i49c+BxloX3XQ55cvzFNXplizZP/q00i+IlttUjAU= github.com/tendermint/crypto v0.0.0-20180820045704-3764759f34a5/go.mod h1:z4YtwM70uOnk8h0pjJYlj3zdYwi9l03By6iAIF5j/Pk= +github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= +github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= +github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= +github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q= go.dedis.ch/fixbuf v1.0.3 h1:hGcV9Cd/znUxlusJ64eAlExS+5cJDIyTyEG+otu5wQs= go.dedis.ch/fixbuf v1.0.3/go.mod h1:yzJMt34Wa5xD37V5RTdmp38cz3QhMagdGoem9anUalw= go.dedis.ch/protobuf v1.0.11 h1:FTYVIEzY/bfl37lu3pR4lIj+F9Vp1jE8oh91VmxKgLo= go.dedis.ch/protobuf v1.0.11/go.mod h1:97QR256dnkimeNdfmURz0wAMNVbd1VmLXhG1CrTYrJ4= +go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= +go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= +go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= +go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q= gocv.io/x/gocv v0.23.0 h1:3Fgbt06/uR8Zf9emWndhjbUjdrw+nto69R/b4noFydY= gocv.io/x/gocv v0.23.0/go.mod h1:Rar2PS6DV+T4FL+PM535EImD/h13hGVaHhnCu1xarBs= golang.org/x/image v0.0.0-20200618115811-c13761719519 h1:1e2ufUJNM3lCHEY5jIgac/7UTjd6cgJNdatjPdFWf34= golang.org/x/image v0.0.0-20200618115811-c13761719519/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd h1:nTDtHvHSdCn1m6ITfMRqtOd/9+7a3s8RBNOZ3eYZzJA= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181220203305-927f97764cc3/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a h1:oWX7TPOiFAMXLq8o0ikBYfCJVlRHBcsciT5bXOrH628= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190522155817-f3200d17e092 h1:4QSRKanuywn15aTZvI/mIDEgPQpswuFndXpOj3rKEco= +golang.org/x/net v0.0.0-20190522155817-f3200d17e092/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f h1:wMNYb4v58l5UBM7MYRLPG6ZhfOqbKu7X5eyFl8ZhKvA= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190124100055-b90733256f2e h1:3GIlrlVLfkoipSReOMNAgApI0ajnalyLa/EZHHca/XI= golang.org/x/sys v0.0.0-20190124100055-b90733256f2e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM= +gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/qr/qr.go b/qr/qr.go index b264437..6e98b72 100644 --- a/qr/qr.go +++ b/qr/qr.go @@ -13,7 +13,9 @@ import ( "gocv.io/x/gocv" ) -const timeToScan = time.Second * 5 +const ( + timeToScan = time.Second * 5 +) type Processor interface { ReadQR() ([]byte, error) diff --git a/storage/fileStorage.go b/storage/fileStorage.go index 063816d..477d5a8 100644 --- a/storage/fileStorage.go +++ b/storage/fileStorage.go @@ -34,9 +34,9 @@ func countLines(r io.Reader) uint64 { return count } -// InitFileStorage inits append-only file storage +// NewFileStorage inits append-only file storage // It takes two arguments: filename - path to a data file, lockFilename (optional) - path to a lock file -func InitFileStorage(filename string, lockFilename ...string) (Storage, error) { +func NewFileStorage(filename string, lockFilename ...string) (Storage, error) { var ( fs FileStorage err error diff --git a/storage/fileStorage_test.go b/storage/fileStorage_test.go index bf164a2..8624809 100644 --- a/storage/fileStorage_test.go +++ b/storage/fileStorage_test.go @@ -21,7 +21,7 @@ func TestFileStorage_GetMessages(t *testing.T) { N := 10 var offset uint64 = 5 var testFile = "/tmp/dc4bc_test_file_storage" - fs, err := InitFileStorage(testFile) + fs, err := NewFileStorage(testFile) if err != nil { t.Error(err) } From ae4d1671952a4c36e706319f3fec31a1b4215f1b Mon Sep 17 00:00:00 2001 From: Andrej Zavgorodnij Date: Mon, 10 Aug 2020 00:37:53 +0300 Subject: [PATCH 2/2] feat: single process tests --- client/client.go | 12 +-- client/http_server.go | 16 +-- client/state.go | 12 ++- main.go | 226 ++++++++++++++++++------------------------ 4 files changed, 119 insertions(+), 147 deletions(-) diff --git a/client/client.go b/client/client.go index a5677f1..26b59df 100644 --- a/client/client.go +++ b/client/client.go @@ -51,7 +51,7 @@ func (c *Client) SendMessage(message storage.Message) error { return nil } -func (c *Client) Poll() { +func (c *Client) Poll() error { tk := time.NewTicker(pollingPeriod) for { select { @@ -63,7 +63,7 @@ func (c *Client) Poll() { messages, err := c.storage.GetMessages(offset) if err != nil { - panic(err) + return fmt.Errorf("failed to GetMessages: %w", err) } for _, message := range messages { @@ -75,21 +75,21 @@ func (c *Client) Poll() { // I.e., if FSM returned an Operation for us. if operation != nil { if err := c.state.PutOperation(operation); err != nil { - panic(err) + return fmt.Errorf("failed to PutOperation: %w", err) } } if err := c.state.SaveOffset(message.Offset); err != nil { - panic(err) + return fmt.Errorf("failed to SaveOffset: %w", err) } if err := c.state.SaveFSM(c.fsm); err != nil { - panic(err) + return fmt.Errorf("failed to SaveFSM: %w", err) } } case <-c.ctx.Done(): log.Println("Context closed, stop polling...") - return + return nil } } } diff --git a/client/http_server.go b/client/http_server.go index 8170d28..560dfa4 100644 --- a/client/http_server.go +++ b/client/http_server.go @@ -27,14 +27,16 @@ func successResponse(w http.ResponseWriter, response []byte) { } func (c *Client) StartHTTPServer(listenAddr string) error { - http.HandleFunc("/sendMessage", c.sendMessageHandler) - http.HandleFunc("/getOperations", c.getOperationsHandler) - http.HandleFunc("/getOperationQRPath", c.getOperationQRPathHandler) - http.HandleFunc("/readProcessedOperationFromCamera", c.readProcessedOperationFromCameraHandler) + mux := http.NewServeMux() + mux.HandleFunc("/sendMessage", c.sendMessageHandler) + mux.HandleFunc("/getOperations", c.getOperationsHandler) + mux.HandleFunc("/getOperationQRPath", c.getOperationQRPathHandler) + mux.HandleFunc("/readProcessedOperationFromCamera", c.readProcessedOperationFromCameraHandler) - http.HandleFunc("/readProcessedOperation", c.readProcessedOperationFromBodyHandler) - http.HandleFunc("/getOperationQR", c.getOperationQRToBodyHandler) - return http.ListenAndServe(listenAddr, nil) + mux.HandleFunc("/readProcessedOperation", c.readProcessedOperationFromBodyHandler) + mux.HandleFunc("/getOperationQR", c.getOperationQRToBodyHandler) + + return http.ListenAndServe(listenAddr, mux) } func (c *Client) sendMessageHandler(w http.ResponseWriter, r *http.Request) { diff --git a/client/state.go b/client/state.go index cee4580..c4fa48e 100644 --- a/client/state.go +++ b/client/state.go @@ -43,14 +43,22 @@ func NewLevelDBState(stateDbPath string) (State, error) { stateDb: db, } - if err := state.initKey(operationsKey, map[string]*Operation{}); err != nil { + // Init state key for operations JSON. + if err := state.initJsonKey(operationsKey, map[string]*Operation{}); err != nil { return nil, fmt.Errorf("failed to init %s storage: %w", operationsKey, err) } + // Init state key for offset bytes. + bz := make([]byte, 8) + binary.LittleEndian.PutUint64(bz, 0) + if err := db.Put([]byte(offsetKey), bz, nil); err != nil { + return nil, fmt.Errorf("failed to init %s storage: %w", offsetKey, err) + } + return state, nil } -func (s *LevelDBState) initKey(key string, data interface{}) error { +func (s *LevelDBState) initJsonKey(key string, data interface{}) error { if _, err := s.stateDb.Get([]byte(key), nil); err != nil { operationsBz, err := json.Marshal(data) if err != nil { diff --git a/main.go b/main.go index 59634fe..6998d5a 100644 --- a/main.go +++ b/main.go @@ -1,16 +1,16 @@ package main import ( + "context" "fmt" _ "image/jpeg" "log" "sync" - dkg "go.dedis.ch/kyber/v3/share/dkg/pedersen" + "github.com/depools/dc4bc/qr" + "github.com/depools/dc4bc/storage" - "go.dedis.ch/kyber/v3" - - dkglib "github.com/depools/dc4bc/dkg" + "github.com/depools/dc4bc/client" _ "image/gif" _ "image/png" @@ -21,160 +21,122 @@ import ( ) type Transport struct { - nodes []*dkglib.DKG -} - -func (t *Transport) getNodeByParticipantID(id int) *dkglib.DKG { - for _, node := range t.nodes { - if node.ParticipantID == id { - return node - } - } - return nil -} - -func (t *Transport) BroadcastPK(participant string, pk kyber.Point) { - for idx, node := range t.nodes { - if ok := node.StorePubKey(participant, pk); !ok { - log.Fatalf("Failed to store PK for participant %d", idx) - } - } -} - -func (t *Transport) BroadcastCommits(participant string, commits []kyber.Point) { - for _, node := range t.nodes { - node.StoreCommits(participant, commits) - } -} - -func (t *Transport) BroadcastDeals(participant string, deals map[int]*dkg.Deal) { - for index, deal := range deals { - dstNode := t.getNodeByParticipantID(index) - if dstNode == nil { - fmt.Printf("Node with index #%d not found\n", index) - continue - } - dstNode.StoreDeal(participant, deal) - } -} - -func (t *Transport) BroadcastResponses(participant string, responses []*dkg.Response) { - for _, node := range t.nodes { - node.StoreResponses(participant, responses) - } + nodes []*client.Client } func main() { - var threshold = 3 var transport = &Transport{} var numNodes = 4 - for i := 0; i < numNodes; i++ { - transport.nodes = append(transport.nodes, dkglib.Init()) - } - - // Participants broadcast PKs. - runStep(transport, func(participantID string, participant *dkglib.DKG, wg *sync.WaitGroup) { - transport.BroadcastPK(participantID, participant.GetPubKey()) - wg.Done() - }) - - // 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() - }) - - // Participants broadcast their deal. - runStep(transport, func(participantID string, participant *dkglib.DKG, wg *sync.WaitGroup) { - deals, err := participant.GetDeals() + 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("failed to getDeals for participant %s: %v", participantID, err) + log.Fatalf("node %d failed to init state: %v\n", nodeID, err) } - transport.BroadcastDeals(participantID, deals) - wg.Done() - }) - // Participants broadcast their responses. - runStep(transport, func(participantID string, participant *dkglib.DKG, wg *sync.WaitGroup) { - responses, err := participant.ProcessDeals() + stg, err := storage.NewFileStorage("/tmp/dc4bc_storage") if err != nil { - log.Fatalf("failed to ProcessDeals for participant %s: %v", participantID, err) + log.Fatalf("node %d failed to init storage: %v\n", nodeID, err) } - transport.BroadcastResponses(participantID, responses) - wg.Done() - }) - // 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) - } - wg.Done() - }) - - 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") + clt, err := client.NewClient( + ctx, + nil, + state, + stg, + qr.NewCameraProcessor(), + ) + if err != nil { + log.Fatalf("node %d failed to init client: %v\n", nodeID, err) } + transport.nodes = append(transport.nodes, clt) } -} -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) + 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) + + 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) + + log.Printf("client %d started...\n", nodeID) } + + var wg = sync.WaitGroup{} + wg.Add(1) wg.Wait() } -//func runQRTest() { -// clearTerminal() -// var data = "Hello, world!" +// // Participants broadcast PKs. +// runStep(transport, func(participantID string, participant *dkglib.DKG, wg *sync.WaitGroup) { +// transport.BroadcastPK(participantID, participant.GetPubKey()) +// wg.Done() +// }) // -// log.Println("A QR code will be shown on your screen.") -// log.Println("Please take a photo of the QR code with your smartphone.") -// log.Println("When you close the image, you will have 5 seconds to" + -// "scan the QR code with your laptop's camera.") -// err := qr.ShowQR(data) -// if err != nil { -// log.Fatalf("Failed to show QR code: %v", err) -// } +// // 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() +// }) // -// var scannedData string -// for { -// clearTerminal() +// // Participants broadcast their Commits. +// runStep(transport, func(participantID string, participant *dkglib.DKG, wg *sync.WaitGroup) { +// commits := participant.GetCommits() +// transport.BroadcastCommits(participantID, commits) +// wg.Done() +// }) +// +// // Participants broadcast their deal. +// runStep(transport, func(participantID string, participant *dkglib.DKG, wg *sync.WaitGroup) { +// deals, err := participant.GetDeals() // if err != nil { -// log.Printf("Failed to scan QR code: %v\n", err) +// log.Fatalf("failed to getDeals for participant %s: %v", participantID, err) // } +// transport.BroadcastDeals(participantID, deals) +// wg.Done() +// }) // -// log.Println("Please center the photo of the QR-code in front" + -// "of your web-camera...") +// // 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() +// }) // -// scannedData, err = qr.ReadQR() -// if err == nil { -// break +// // 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) +// } +// wg.Done() +// }) +// +// 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") // } // } -// -// clearTerminal() -// log.Printf("QR code successfully scanned; the data is: %s\n", scannedData) //} // -//func clearTerminal() { -// c := exec.Command("clear") -// c.Stdout = os.Stdout -// _ = c.Run() +//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() //}