Skip to content

Commit

Permalink
Adds Faucet + gateway to launcher (#1734)
Browse files Browse the repository at this point in the history
* Adds Faucet + gateway to launcher

* port

* ports
  • Loading branch information
otherview authored Jan 3, 2024
1 parent 986f699 commit e984af1
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 8 deletions.
1 change: 0 additions & 1 deletion integration/faucet/faucet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ func TestFaucet(t *testing.T) {
time.Sleep(2 * time.Second)

faucetConfig := &faucet.Config{
Port: startPort,
Host: "localhost",
HTTPPort: startPort + integration.DefaultHostRPCHTTPOffset,
PK: "0x" + contractDeployerPrivateKeyHex,
Expand Down
53 changes: 53 additions & 0 deletions testnet/launcher/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/ten-protocol/go-ten/go/obsclient"
"github.com/ten-protocol/go-ten/go/rpc"
"github.com/ten-protocol/go-ten/testnet/launcher/eth2network"
"github.com/ten-protocol/go-ten/testnet/launcher/faucet"
"github.com/ten-protocol/go-ten/testnet/launcher/gateway"

l1cd "github.com/ten-protocol/go-ten/testnet/launcher/l1contractdeployer"
l2cd "github.com/ten-protocol/go-ten/testnet/launcher/l2contractdeployer"
Expand Down Expand Up @@ -149,6 +151,57 @@ func (t *Testnet) Start() error {
}
fmt.Println("L2 Contracts were successfully deployed...")

faucetPort := 99
faucetInst, err := faucet.NewDockerFaucet(
faucet.NewFaucetConfig(
faucet.WithFaucetPort(faucetPort),
faucet.WithTenNodePort(13010),
faucet.WithTenNodeHost("validator-host"),
faucet.WithFaucetPrivKey("0x8dfb8083da6275ae3e4f41e3e8a8c19d028d32c9247e24530933782f2a05035b"),
faucet.WithDockerImage("testnetobscuronet.azurecr.io/obscuronet/faucet:latest"),
),
)
if err != nil {
return fmt.Errorf("unable to instantiate faucet - %w", err)
}

if err = faucetInst.Start(); err != nil {
return fmt.Errorf("unable to start faucet - %w", err)
}

if err = faucetInst.IsReady(); err != nil {
return fmt.Errorf("unable to wait for faucet to be ready - %w", err)
}

fmt.Printf("Faucet ready to be accessed at http://127.0.0.1:%d/ ...\n", faucetPort)
fmt.Printf("Fund your account with `curl --request POST 'http://127.0.0.1:%d/fund/eth' --header 'Content-Type: application/json' --data-raw '{ \"address\":\"0x0....\" } `\n", faucetPort)

gatewayPort := 3000
gatewayInst, err := gateway.NewDockerGateway(
gateway.NewGatewayConfig(
gateway.WithGatewayHTTPPort(gatewayPort),
gateway.WithGatewayWSPort(3001),
gateway.WithTenNodeHTTPPort(13010),
gateway.WithTenNodeWSPort(13011),
gateway.WithTenNodeHost("validator-host"),
gateway.WithDockerImage("testnetobscuronet.azurecr.io/obscuronet/obscuro_gateway:latest"),
),
)
if err != nil {
return fmt.Errorf("unable to instantiate gateway - %w", err)
}

if err = gatewayInst.Start(); err != nil {
return fmt.Errorf("unable to start gateway - %w", err)
}

if err = gatewayInst.IsReady(); err != nil {
return fmt.Errorf("unable to wait for gateway to be ready - %w", err)
}

fmt.Printf("Gateway ready to be accessed at http://127.0.0.1:%d ...\n", gatewayPort)

fmt.Println("Network successfully launched !")
return nil
}

Expand Down
53 changes: 53 additions & 0 deletions testnet/launcher/faucet/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package faucet

// Option is a function that applies configs to a Config Object
type Option = func(c *Config)

// Config holds the properties that configure the package
type Config struct {
tenNodeHost string
tenNodePort int
faucetPort int
faucetPrivKey string
dockerImage string
}

func NewFaucetConfig(opts ...Option) *Config {
defaultConfig := &Config{}

for _, opt := range opts {
opt(defaultConfig)
}

return defaultConfig
}

func WithTenNodeHost(s string) Option {
return func(c *Config) {
c.tenNodeHost = s
}
}

func WithFaucetPrivKey(s string) Option {
return func(c *Config) {
c.faucetPrivKey = s
}
}

func WithDockerImage(s string) Option {
return func(c *Config) {
c.dockerImage = s
}
}

func WithTenNodePort(i int) Option {
return func(c *Config) {
c.tenNodePort = i
}
}

func WithFaucetPort(i int) Option {
return func(c *Config) {
c.faucetPort = i
}
}
55 changes: 55 additions & 0 deletions testnet/launcher/faucet/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package faucet

import (
"fmt"
"time"

"github.com/sanity-io/litter"
"github.com/ten-protocol/go-ten/go/common/docker"
"github.com/ten-protocol/go-ten/go/common/retry"
"github.com/valyala/fasthttp"
)

type DockerFaucet struct {
cfg *Config
}

func NewDockerFaucet(cfg *Config) (*DockerFaucet, error) {
return &DockerFaucet{
cfg: cfg,
}, nil // todo (@pedro) - add validation
}

func (n *DockerFaucet) Start() error {
fmt.Printf("Starting faucet with config: \n%s\n\n", litter.Sdump(*n.cfg))

cmds := []string{
"/home/obscuro/go-obscuro/tools/faucet/cmd/faucet",
"--nodeHost", n.cfg.tenNodeHost,
"--nodePort", fmt.Sprintf("%d", n.cfg.tenNodePort),
"--pk", n.cfg.faucetPrivKey,
"--jwtSecret", "someKey",
"--serverPort", fmt.Sprintf("%d", n.cfg.faucetPort),
}

_, err := docker.StartNewContainer("faucet", n.cfg.dockerImage, cmds, []int{n.cfg.faucetPort}, nil, nil, nil)
return err
}

func (n *DockerFaucet) IsReady() error {
timeout := time.Minute
interval := time.Second

return retry.Do(func() error {
statusCode, _, err := fasthttp.Get(nil, fmt.Sprintf("http://127.0.0.1:%d/health/", n.cfg.faucetPort))
if err != nil {
return err
}

if statusCode != fasthttp.StatusOK {
return fmt.Errorf("status not ok - status received: %s", fasthttp.StatusMessage(statusCode))
}

return nil
}, retry.NewTimeoutStrategy(timeout, interval))
}
60 changes: 60 additions & 0 deletions testnet/launcher/gateway/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package gateway

// Option is a function that applies configs to a Config Object
type Option = func(c *Config)

// Config holds the properties that configure the package
type Config struct {
tenNodeHost string
tenNodeHTTPPort int
tenNodeWSPort int
gatewayHTTPPort int
gatewayWSPort int
dockerImage string
}

func NewGatewayConfig(opts ...Option) *Config {
defaultConfig := &Config{}

for _, opt := range opts {
opt(defaultConfig)
}

return defaultConfig
}

func WithTenNodeHost(s string) Option {
return func(c *Config) {
c.tenNodeHost = s
}
}

func WithDockerImage(s string) Option {
return func(c *Config) {
c.dockerImage = s
}
}

func WithTenNodeHTTPPort(i int) Option {
return func(c *Config) {
c.tenNodeHTTPPort = i
}
}

func WithTenNodeWSPort(i int) Option {
return func(c *Config) {
c.tenNodeWSPort = i
}
}

func WithGatewayHTTPPort(i int) Option {
return func(c *Config) {
c.gatewayHTTPPort = i
}
}

func WithGatewayWSPort(i int) Option {
return func(c *Config) {
c.gatewayWSPort = i
}
}
57 changes: 57 additions & 0 deletions testnet/launcher/gateway/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package gateway

import (
"fmt"
"time"

"github.com/sanity-io/litter"
"github.com/ten-protocol/go-ten/go/common/docker"
"github.com/ten-protocol/go-ten/go/common/retry"
"github.com/valyala/fasthttp"
)

type DockerGateway struct {
cfg *Config
}

func NewDockerGateway(cfg *Config) (*DockerGateway, error) {
return &DockerGateway{
cfg: cfg,
}, nil // todo (@pedro) - add validation
}

func (n *DockerGateway) Start() error {
fmt.Printf("Starting gateway with config: \n%s\n\n", litter.Sdump(*n.cfg))

cmds := []string{
"/home/obscuro/go-obscuro/tools/walletextension/bin/wallet_extension_linux",
"--host", "0.0.0.0",
"--port", fmt.Sprintf("%d", n.cfg.gatewayHTTPPort),
"--portWS", fmt.Sprintf("%d", n.cfg.gatewayWSPort),
"--nodePortHTTP", fmt.Sprintf("%d", n.cfg.tenNodeHTTPPort),
"--nodePortWS", fmt.Sprintf("%d", n.cfg.tenNodeWSPort),
"--nodeHost", n.cfg.tenNodeHost,
"--dbType", "sqlite",
}

_, err := docker.StartNewContainer("gateway", n.cfg.dockerImage, cmds, []int{n.cfg.gatewayHTTPPort, n.cfg.gatewayWSPort}, nil, nil, nil)
return err
}

func (n *DockerGateway) IsReady() error {
timeout := time.Minute
interval := time.Second

return retry.Do(func() error {
statusCode, _, err := fasthttp.Get(nil, fmt.Sprintf("http://127.0.0.1:%d/health/", n.cfg.gatewayHTTPPort))
if err != nil {
return err
}

if statusCode != fasthttp.StatusOK {
return fmt.Errorf("status not ok - status received: %s", fasthttp.StatusMessage(statusCode))
}

return nil
}, retry.NewTimeoutStrategy(timeout, interval))
}
6 changes: 0 additions & 6 deletions tools/faucet/cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ import (

const (
// Flag names, defaults and usages.
faucetPortName = "port"
faucetPortDefault = 80
faucetPortUsage = "The port on which to serve the faucet endpoint. Default: 80."

nodeHostName = "nodeHost"
nodeHostDefault = "erpc.sepolia-testnet.obscu.ro"
nodeHostUsage = "The host on which to connect to the Obscuro node. Default: `erpc.sepolia-testnet.obscu.ro`."
Expand All @@ -41,7 +37,6 @@ const (
)

func parseCLIArgs() *faucet.Config {
faucetPort := flag.Int(faucetPortName, faucetPortDefault, faucetPortUsage)
nodeHost := flag.String(nodeHostName, nodeHostDefault, nodeHostUsage)
nodeHTTPPort := flag.Int(nodeHTTPPortName, nodeHTTPPortDefault, nodeHTTPPortUsage)
faucetPK := flag.String(faucetPKName, faucetPKDefault, faucetPKUsage)
Expand All @@ -51,7 +46,6 @@ func parseCLIArgs() *faucet.Config {
flag.Parse()

return &faucet.Config{
Port: *faucetPort,
Host: *nodeHost,
HTTPPort: *nodeHTTPPort,
PK: *faucetPK,
Expand Down
1 change: 0 additions & 1 deletion tools/faucet/faucet/faucet_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package faucet
import "math/big"

type Config struct {
Port int
Host string
HTTPPort int
PK string
Expand Down
9 changes: 9 additions & 0 deletions tools/faucet/webserver/web_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func NewWebServer(faucetServer *faucet.Faucet, bindAddress string, jwtSecret []b

r.GET("/balance", balanceReqHandler(faucetServer))

r.GET("/health", healthReqHandler())

return &WebServer{
engine: r,
faucet: faucetServer,
Expand Down Expand Up @@ -170,3 +172,10 @@ func balanceReqHandler(faucetServer *faucet.Faucet) gin.HandlerFunc {
c.JSON(http.StatusOK, gin.H{"balance": balance.String()})
}
}

// returns the remaining native balance of the faucet
func healthReqHandler() gin.HandlerFunc {
return func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"healthy": true})
}
}

0 comments on commit e984af1

Please sign in to comment.