Skip to content
This repository has been archived by the owner on Feb 24, 2023. It is now read-only.

Commit

Permalink
Merge pull request #179 from bandprotocol/changeNodeConfiguration
Browse files Browse the repository at this point in the history
remove env variable, use config and input params instead
  • Loading branch information
prin-r committed Sep 2, 2019
2 parents c827adc + 1abd894 commit fee5011
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 32 deletions.
12 changes: 5 additions & 7 deletions go/adapter/querywrapper.go
Expand Up @@ -3,25 +3,23 @@ package adapter
import (
"fmt"
"log"
"os"
"reflect"
"strings"

"github.com/ethereum/go-ethereum/common"
)

var debug bool

func init() {
debug = strings.ToLower(os.Getenv("DEBUG")) == "true"
}
var debug = false

func logDebug(s string) {
if debug {
log.Println(s)
}
}

func TurnOnQueryDebugging() {
debug = true
}

func DoQuery(adapter Adapter, key []byte) (common.Hash, error) {
var adapterName string
if t := reflect.TypeOf(adapter); t.Kind() == reflect.Ptr {
Expand Down
4 changes: 4 additions & 0 deletions go/coord.yaml
@@ -1,3 +1,7 @@
port: 8000
ethRpc: "https://kovan.infura.io"
queryDebug: false
privateKey: "xxxxxx"
datasets:
- "0x1234"
- "0x5678"
Expand Down
86 changes: 84 additions & 2 deletions go/coordinator/main.go
Expand Up @@ -8,15 +8,20 @@ import (
"io/ioutil"
"log"
"net/http"
"os"
"sort"
"strconv"
"strings"

"github.com/bandprotocol/band/go/adapter"
"github.com/bandprotocol/band/go/reqmsg"
"github.com/olekukonko/tablewriter"

"github.com/bandprotocol/band/go/eth"
"github.com/ethereum/go-ethereum/accounts/abi"

"github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

Expand All @@ -43,6 +48,12 @@ type valueWithTimeStamp struct {
Timestamp uint64
}

var rootCmd = &cobra.Command{
Use: "./[this] -h \n ./[this] --help \n ./[this] [path to node.yml]",
Short: "The Band coordinator node is middleware, gathering data from all provider nodes",
Run: func(cmd *cobra.Command, args []string) {},
}

func getProviderUrl(provider common.Address) (string, error) {
key := "providers." + provider.Hex()
if !viper.IsSet(key) {
Expand Down Expand Up @@ -355,11 +366,82 @@ func handleRequest(w http.ResponseWriter, r *http.Request) {
}

func main() {
viper.SetConfigName("coord")
var port string
var ethRpc string
var privateKey string
var queryDebug bool

if len(os.Args) < 2 {
fmt.Println("should have at least 1 argument, -h or --help for more detail")
os.Exit(1)
} else if os.Args[1] == "-h" || os.Args[1] == "--help" {
rootCmd.PersistentFlags().StringVar(&port, "port", "should be set by node.yml", `port of your app, for example "5000"`)
rootCmd.PersistentFlags().StringVar(&ethRpc, "ethRpc", "should be set by node.yml", `Ethereum rcp url, for example "https://kovan.infura.io"`)
rootCmd.PersistentFlags().StringVar(&privateKey, "privateKey", "should be set by node.yml", `Private Key of the data provider, 64 hex characters`)
rootCmd.PersistentFlags().BoolVar(&queryDebug, "debug", false, `turn on debugging when query`)
err := rootCmd.Execute()
if err != nil {
log.Println(err)
}
return
}

viper.SetConfigName(os.Args[1])
viper.AddConfigPath(".")
if err := viper.ReadInConfig(); err != nil {
log.Fatal("Unable to locate config file (coord.yaml)")
}

privateKey = viper.GetString("privateKey")
ethRpc = viper.GetString("ethRpc")
port = viper.GetString("port")
queryDebug = viper.GetBool("queryDebug")

rootCmd.PersistentFlags().StringVar(&port, "port", port, `port of your app, for example "5000"`)
rootCmd.PersistentFlags().StringVar(&ethRpc, "ethRpc", ethRpc, `Ethereum rcp url, for example "https://kovan.infura.io"`)
rootCmd.PersistentFlags().StringVar(&privateKey, "privateKey", privateKey, `Private Key of the data provider, 64 hex characters`)
rootCmd.PersistentFlags().BoolVar(&queryDebug, "debug", false, `turn on debugging when query`)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}

if queryDebug {
adapter.TurnOnQueryDebugging()
}
err := eth.SetPrivateKey(privateKey)
if err != nil {
log.Println(err.Error())
log.Println("Warning: Private key not set. Ethereum node according to ethRpc will be used for signing")
}
err = eth.SetRpcClient(ethRpc)
if err != nil {
log.Println("create ethRpc client error")
log.Fatal(err)
}
_, err = strconv.Atoi(port)
if err != nil {
log.Println("wrong port format")
log.Fatal(err)
}

fmt.Println("start coordinator node with these following parameters")
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Parameter Name", "Value"})
table.Append([]string{"port", port})
table.Append([]string{"ethRpc", ethRpc})
table.Append([]string{"debug", strconv.FormatBool(queryDebug)})
table.Render()

fmt.Println("provider list in config file")
table = tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Provider Address", "URL"})
providers := viper.GetStringMapString("providers")
for addr, url := range providers {
table.Append([]string{addr, url})
}
table.Render()

http.HandleFunc("/", handleRequest)
log.Fatal(http.ListenAndServe(":8000", nil))
log.Fatal(http.ListenAndServe(":"+port, nil))
}
25 changes: 10 additions & 15 deletions go/eth/rpc.go
Expand Up @@ -5,9 +5,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"log"
"math/big"
"os"
"strconv"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -25,19 +23,6 @@ type Signature struct {
var pk *ecdsa.PrivateKey
var rpcClient *rpc.Client

func init() {

err := SetPrivateKey(os.Getenv("ETH_PRIVATE_KEY"))
if err != nil {
log.Println("no private key found, try to connect with localnode")
}

rpcClient, err = rpc.Dial(os.Getenv("ETH_RPC"))
if err != nil {
log.Fatal(err)
}
}

// SetPrivateKey transform private key string to ECDSA Key
func SetPrivateKey(newPrivateKey string) error {
_pk, err := crypto.HexToECDSA(newPrivateKey)
Expand All @@ -48,6 +33,16 @@ func SetPrivateKey(newPrivateKey string) error {
return nil
}

// SetRpcClient receive url of ethereum node to initiate rpcClient
func SetRpcClient(rpcUrl string) error {
var err error
rpcClient, err = rpc.Dial(rpcUrl)
if err != nil {
return err
}
return nil
}

// GetAddress returns the address of the
func GetAddress() (common.Address, error) {
if pk != nil {
Expand Down
3 changes: 2 additions & 1 deletion go/go.mod
Expand Up @@ -19,10 +19,11 @@ require (
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/mattn/go-isatty v0.0.9 // indirect
github.com/mattn/go-runewidth v0.0.4 // indirect
github.com/olekukonko/tablewriter v0.0.1 // indirect
github.com/olekukonko/tablewriter v0.0.1
github.com/pborman/uuid v1.2.0 // indirect
github.com/rjeczalik/notify v0.9.2 // indirect
github.com/rs/cors v1.7.0 // indirect
github.com/spf13/cobra v0.0.5
github.com/spf13/viper v1.4.0
github.com/status-im/keycard-go v0.0.0-20190424133014-d95853db0f48 // indirect
github.com/steakknife/bloomfilter v0.0.0-20180922174646-6819c0d2a570 // indirect
Expand Down
11 changes: 11 additions & 0 deletions go/go.sum
Expand Up @@ -15,9 +15,11 @@ github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghf
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-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
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/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
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/deckarep/golang-set v1.7.1 h1:SCQV0S6gTtp6itiFrTqI+pfmJ4LN85S1YzhDf9rTHJQ=
Expand Down Expand Up @@ -70,6 +72,7 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
github.com/huin/goupnp v1.0.0 h1:wg75sLpL6DZqwHQN6E1Cfk6mtfzS45z8OV+ic+DtHRo=
github.com/huin/goupnp v1.0.0/go.mod h1:n9v9KO1tAxYH82qOn+UTIFQDmx5n1Zxd/ClZDMX7Bnc=
github.com/huin/goutil v0.0.0-20170803182201-1ca381bf3150/go.mod h1:PpLOETDnJ0o3iZrZfqZzyLl6l7F3c6L1oWn7OICBi6o=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jackpal/go-nat-pmp v1.0.1 h1:i0LektDkO1QlrTm/cSuP+PyBCDnYvjPLGl4LdWEMiaA=
github.com/jackpal/go-nat-pmp v1.0.1/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
Expand All @@ -95,6 +98,7 @@ github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2y
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
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 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
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=
Expand Down Expand Up @@ -127,17 +131,21 @@ github.com/rjeczalik/notify v0.9.2/go.mod h1:aErll2f0sUX9PXZnVNyeiObbmTlk5jnMoCa
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
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 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v0.0.5 h1:f0B+LkLX6DtmRH1isoNA9VTtNUK9K8xYd28JNNfOv/s=
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk=
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.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/spf13/viper v1.4.0 h1:yXHLWeravcrgGyFSyCgdYpXQ9dR9c/WED3pg1RhxqEU=
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
github.com/status-im/keycard-go v0.0.0-20190424133014-d95853db0f48 h1:ju5UTwk5Odtm4trrY+4Ca4RMj5OyXbmVeDAVad2T0Jw=
Expand All @@ -161,6 +169,7 @@ github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1
github.com/tyler-smith/go-bip39 v1.0.2 h1:+t3w+KwLXO6154GNJY+qUtIxLTmFjfUmpguQT1OlOT8=
github.com/tyler-smith/go-bip39 v1.0.2/go.mod h1:sJ5fKU0s6JVwZjjcUEX2zFOnvq0ASQ2K9Zr6cf67kNs=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208 h1:1cngl9mPEoITZG8s8cVcUy5CeIBYhEESkOB7m6Gmkrk=
github.com/wsddn/go-ecdh v0.0.0-20161211032359-48726bab9208/go.mod h1:IotVbo4F+mw0EzQ08zFqg7pK3FebNXpaMsRy2RT+Ees=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
Expand All @@ -170,6 +179,7 @@ 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=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand All @@ -194,6 +204,7 @@ golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20180926160741-c2ed4eda69e7/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-20181205085412-a5c9d58dba9a/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/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
2 changes: 2 additions & 0 deletions go/node.yaml
@@ -1,4 +1,6 @@
port: 5000
ethRpc: "https://kovan.infura.io"
queryDebug: false
privateKey: xxxxxx
adapters:
"0xa24dF0420dE1f3b8d740A52AAEB9d55d6D64478e":
Expand Down

0 comments on commit fee5011

Please sign in to comment.