From c5d3a43edc845acdd880d76606028038e46dc7bb Mon Sep 17 00:00:00 2001
From: capossele <angelocapossele@gmail.com>
Date: Tue, 7 Apr 2020 11:02:13 +0100
Subject: [PATCH] :recycle: refactors webapi and client lib

---
 client/drng.go                                | 49 +++++++++++++++++++
 go.mod                                        |  2 +-
 go.sum                                        |  4 --
 pluginmgr/webapi/plugins.go                   |  2 +
 plugins/webapi/broadcastData/plugin.go        |  9 +---
 .../webapi/drng/collectiveBeacon/handler.go   | 10 ++--
 plugins/webapi/drng/info/committee/handler.go | 29 +++++++++++
 .../webapi/drng/info/randomness/handler.go    | 27 ++++++++++
 plugins/webapi/drng/plugin.go                 |  5 ++
 9 files changed, 118 insertions(+), 19 deletions(-)
 create mode 100644 client/drng.go
 create mode 100644 plugins/webapi/drng/info/committee/handler.go
 create mode 100644 plugins/webapi/drng/info/randomness/handler.go

diff --git a/client/drng.go b/client/drng.go
new file mode 100644
index 00000000..5bd68cfa
--- /dev/null
+++ b/client/drng.go
@@ -0,0 +1,49 @@
+package client
+
+import (
+	"net/http"
+
+	webapi_collectiveBeacon "github.com/iotaledger/goshimmer/plugins/webapi/drng/collectiveBeacon"
+	webapi_committee "github.com/iotaledger/goshimmer/plugins/webapi/drng/info/committee"
+	webapi_randomness "github.com/iotaledger/goshimmer/plugins/webapi/drng/info/randomness"
+)
+
+const (
+	routeCollectiveBeacon = "drng/collectiveBeacon"
+	routeRandomness       = "drng/info/randomness"
+	routeCommittee        = "drng/info/committee"
+)
+
+// BroadcastData sends the given collective beacon (payload) by creating a message in the backend.
+func (api *GoShimmerAPI) BroadcastCollectiveBeacon(payload []byte) (string, error) {
+
+	res := &webapi_collectiveBeacon.Response{}
+	if err := api.do(http.MethodPost, routeCollectiveBeacon,
+		&webapi_collectiveBeacon.Request{Payload: payload}, res); err != nil {
+		return "", err
+	}
+
+	return res.Id, nil
+}
+
+// GetRandomness gets the current randomness.
+func (api *GoShimmerAPI) GetRandomness() (*webapi_randomness.Response, error) {
+	res := &webapi_randomness.Response{}
+	if err := api.do(http.MethodGet, func() string {
+		return routeRandomness
+	}(), nil, res); err != nil {
+		return nil, err
+	}
+	return res, nil
+}
+
+// GetRandomness gets the current randomness.
+func (api *GoShimmerAPI) GetCommittee() (*webapi_committee.Response, error) {
+	res := &webapi_committee.Response{}
+	if err := api.do(http.MethodGet, func() string {
+		return routeCommittee
+	}(), nil, res); err != nil {
+		return nil, err
+	}
+	return res, nil
+}
diff --git a/go.mod b/go.mod
index 8afbe825..2d77b532 100644
--- a/go.mod
+++ b/go.mod
@@ -15,7 +15,7 @@ require (
 	github.com/iotaledger/hive.go v0.0.0-20200403132600-4c10556e08a0
 	github.com/iotaledger/iota.go v1.0.0-beta.14
 	github.com/labstack/echo v3.3.10+incompatible
-	github.com/labstack/gommon v0.3.0 // indirect
+	github.com/labstack/gommon v0.3.0
 	github.com/magiconair/properties v1.8.1
 	github.com/mr-tron/base58 v1.1.3
 	github.com/panjf2000/ants/v2 v2.2.2
diff --git a/go.sum b/go.sum
index 06a6549e..b07f3342 100644
--- a/go.sum
+++ b/go.sum
@@ -159,10 +159,6 @@ github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T
 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/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
-github.com/iotaledger/hive.go v0.0.0-20200330121034-e4a505bcf2cd h1:GZ9zGBj+tK1jHqTD5+OoPLVVlk/sB2pkKmQt9vjR8uY=
-github.com/iotaledger/hive.go v0.0.0-20200330121034-e4a505bcf2cd/go.mod h1:LYUD1U+BxF+OY6zCZ4xp38vzjp/QWbUdCw9iwmxkGnc=
-github.com/iotaledger/hive.go v0.0.0-20200402231254-50e5bddb0da0 h1:Es3rPblh28a68LctnLwqhUOphmtkD8Q3UVKZoZYSlDM=
-github.com/iotaledger/hive.go v0.0.0-20200402231254-50e5bddb0da0/go.mod h1:LYUD1U+BxF+OY6zCZ4xp38vzjp/QWbUdCw9iwmxkGnc=
 github.com/iotaledger/hive.go v0.0.0-20200403132600-4c10556e08a0 h1:CyUsunZHlWuD1s9GVz+XqAIZVpRDxJBspb4DheJVknw=
 github.com/iotaledger/hive.go v0.0.0-20200403132600-4c10556e08a0/go.mod h1:LYUD1U+BxF+OY6zCZ4xp38vzjp/QWbUdCw9iwmxkGnc=
 github.com/iotaledger/iota.go v1.0.0-beta.9/go.mod h1:F6WBmYd98mVjAmmPVYhnxg8NNIWCjjH8VWT9qvv3Rc8=
diff --git a/pluginmgr/webapi/plugins.go b/pluginmgr/webapi/plugins.go
index dca728c1..b67a8246 100644
--- a/pluginmgr/webapi/plugins.go
+++ b/pluginmgr/webapi/plugins.go
@@ -3,6 +3,7 @@ package webapi
 import (
 	"github.com/iotaledger/goshimmer/plugins/webapi"
 	"github.com/iotaledger/goshimmer/plugins/webapi/broadcastData"
+	"github.com/iotaledger/goshimmer/plugins/webapi/drng"
 	"github.com/iotaledger/goshimmer/plugins/webapi/findMessageById"
 	"github.com/iotaledger/goshimmer/plugins/webapi/getNeighbors"
 	"github.com/iotaledger/goshimmer/plugins/webapi/spammer"
@@ -16,6 +17,7 @@ var PLUGINS = node.Plugins(
 	//gtta.PLUGIN,
 	spammer.PLUGIN,
 	broadcastData.PLUGIN,
+	drng.PLUGIN,
 	findMessageById.PLUGIN,
 	getNeighbors.PLUGIN,
 )
diff --git a/plugins/webapi/broadcastData/plugin.go b/plugins/webapi/broadcastData/plugin.go
index f4b94ef8..4b71c2fa 100644
--- a/plugins/webapi/broadcastData/plugin.go
+++ b/plugins/webapi/broadcastData/plugin.go
@@ -7,7 +7,6 @@ import (
 	"github.com/iotaledger/goshimmer/plugins/messagelayer"
 	"github.com/iotaledger/goshimmer/plugins/webapi"
 	"github.com/iotaledger/hive.go/logger"
-	"github.com/iotaledger/hive.go/marshalutil"
 	"github.com/iotaledger/hive.go/node"
 	"github.com/labstack/echo"
 )
@@ -30,13 +29,7 @@ func broadcastData(c echo.Context) error {
 	}
 
 	//TODO: to check max payload size allowed, if exceeding return an error
-
-	marshalUtil := marshalutil.New(request.Data)
-	parsedPayload, err := payload.Parse(marshalUtil)
-	if err != nil {
-		return c.JSON(http.StatusBadRequest, Response{Error: "Not a valid Payload"})
-	}
-	tx := messagelayer.MessageFactory.IssuePayload(parsedPayload)
+	tx := messagelayer.MessageFactory.IssuePayload(payload.NewData(request.Data))
 
 	return c.JSON(http.StatusOK, Response{Id: tx.Id().String()})
 }
diff --git a/plugins/webapi/drng/collectiveBeacon/handler.go b/plugins/webapi/drng/collectiveBeacon/handler.go
index 6180a02a..bdd3d8ef 100644
--- a/plugins/webapi/drng/collectiveBeacon/handler.go
+++ b/plugins/webapi/drng/collectiveBeacon/handler.go
@@ -3,8 +3,7 @@ package collectiveBeacon
 import (
 	"net/http"
 
-	cb "github.com/iotaledger/goshimmer/packages/binary/drng/subtypes/collectiveBeacon/payload"
-	generic "github.com/iotaledger/goshimmer/packages/binary/messagelayer/payload"
+	"github.com/iotaledger/goshimmer/packages/binary/drng/subtypes/collectiveBeacon/payload"
 	"github.com/iotaledger/goshimmer/plugins/messagelayer"
 	"github.com/iotaledger/hive.go/marshalutil"
 	"github.com/labstack/echo"
@@ -23,12 +22,11 @@ func Handler(c echo.Context) error {
 	//TODO: to check max payload size allowed, if exceeding return an error
 
 	marshalUtil := marshalutil.New(request.Payload)
-	cbPayload, err := cb.Parse(marshalUtil)
-
-	parsedPayload, err := generic.Parse(marshalUtil)
+	parsedPayload, err := payload.Parse(marshalUtil)
 	if err != nil {
-		return c.JSON(http.StatusBadRequest, Response{Error: "Not a valid Payload"})
+		return c.JSON(http.StatusBadRequest, Response{Error: "Not a valid Collective Beacon payload"})
 	}
+
 	tx := messagelayer.MessageFactory.IssuePayload(parsedPayload)
 
 	return c.JSON(http.StatusOK, Response{Id: tx.Id().String()})
diff --git a/plugins/webapi/drng/info/committee/handler.go b/plugins/webapi/drng/info/committee/handler.go
new file mode 100644
index 00000000..88b332f3
--- /dev/null
+++ b/plugins/webapi/drng/info/committee/handler.go
@@ -0,0 +1,29 @@
+package committee
+
+import (
+	"net/http"
+
+	"github.com/iotaledger/goshimmer/plugins/drng"
+	"github.com/iotaledger/hive.go/crypto/ed25519"
+	"github.com/labstack/echo"
+)
+
+// Handler creates a message of the given payload and
+// broadcasts it to the node's neighbors. It returns the message ID if successful.
+func Handler(c echo.Context) error {
+	committee := drng.Instance.State.Committee()
+	return c.JSON(http.StatusOK, Response{
+		InstanceID:    committee.InstanceID,
+		Threshold:     committee.Threshold,
+		Identities:    committee.Identities,
+		DistributedPK: committee.DistributedPK,
+	})
+}
+
+type Response struct {
+	InstanceID    uint32              `json:"instanceID,omitempty"`
+	Threshold     uint8               `json:"threshold,omitempty"`
+	Identities    []ed25519.PublicKey `json:"identitites,omitempty"`
+	DistributedPK []byte              `json:"distributedPK,omitempty"`
+	Error         string              `json:"error,omitempty"`
+}
diff --git a/plugins/webapi/drng/info/randomness/handler.go b/plugins/webapi/drng/info/randomness/handler.go
new file mode 100644
index 00000000..fd64d449
--- /dev/null
+++ b/plugins/webapi/drng/info/randomness/handler.go
@@ -0,0 +1,27 @@
+package randomness
+
+import (
+	"net/http"
+	"time"
+
+	"github.com/iotaledger/goshimmer/plugins/drng"
+	"github.com/labstack/echo"
+)
+
+// Handler creates a message of the given payload and
+// broadcasts it to the node's neighbors. It returns the message ID if successful.
+func Handler(c echo.Context) error {
+	randomness := drng.Instance.State.Randomness()
+	return c.JSON(http.StatusOK, Response{
+		Round:      randomness.Round,
+		Randomness: randomness.Randomness,
+		Timestamp:  randomness.Timestamp,
+	})
+}
+
+type Response struct {
+	Round      uint64    `json:"round,omitempty"`
+	Timestamp  time.Time `json:"timestamp,omitempty"`
+	Randomness []byte    `json:"randomness,omitempty"`
+	Error      string    `json:"error,omitempty"`
+}
diff --git a/plugins/webapi/drng/plugin.go b/plugins/webapi/drng/plugin.go
index 9b9649d2..1963f5c7 100644
--- a/plugins/webapi/drng/plugin.go
+++ b/plugins/webapi/drng/plugin.go
@@ -3,6 +3,8 @@ package drng
 import (
 	"github.com/iotaledger/goshimmer/plugins/webapi"
 	"github.com/iotaledger/goshimmer/plugins/webapi/drng/collectiveBeacon"
+	"github.com/iotaledger/goshimmer/plugins/webapi/drng/info/committee"
+	"github.com/iotaledger/goshimmer/plugins/webapi/drng/info/randomness"
 	"github.com/iotaledger/hive.go/logger"
 	"github.com/iotaledger/hive.go/node"
 )
@@ -13,4 +15,7 @@ var log *logger.Logger
 func configure(plugin *node.Plugin) {
 	log = logger.NewLogger("API-dRNG")
 	webapi.Server.POST("drng/collectiveBeacon", collectiveBeacon.Handler)
+
+	webapi.Server.GET("drng/info/committee", committee.Handler)
+	webapi.Server.GET("drng/info/randomness", randomness.Handler)
 }
-- 
GitLab