Skip to content

Commit

Permalink
dlcoracle prepared for run within mit-dci/lit testing framework.
Browse files Browse the repository at this point in the history
Added some command line parameters to the dlcoracle:
DataDir - so It could save keys in a test folder
HttpPort - to create multiple instances
Interval - to control the data publishing interval. To set the appropriate time when creating a r-point.
  • Loading branch information
infografx committed Mar 20, 2019
1 parent 01d4767 commit 329f7d0
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 10 deletions.
5 changes: 3 additions & 2 deletions crypto/keyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func LoadKeyFromFileInteractive(filename string) (*[96]byte, error) {
if err != nil {
return nil, err
}
if a.Size() < 193 { // there can't be a password...

if a.Size() < 194 { // there can't be a password...
return LoadKeyFromFileArg(filename, nil)
}
fmt.Printf("passphrase: ")
Expand All @@ -57,7 +58,7 @@ func LoadKeyFromFileArg(filename string, pass []byte) (*[96]byte, error) {
return priv96, err
}

if len(enckey) == 96 { // UNencrypted key, length 32
if len(enckey) == 96 { // UNencrypted key, length 96
fmt.Printf("WARNING!! Key file not encrypted!!\n")
fmt.Printf("Anyone who can read the key file can take everything!\n")
fmt.Printf("You should start over and use a good passphrase!\n")
Expand Down
4 changes: 3 additions & 1 deletion datasources/eurbtc-rounded.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"net/http"

"github.com/gertjaap/dlcoracle/logging"

"github.com/gertjaap/dlcoracle/gcfg"
)

type EurBtcRounded struct {
Expand All @@ -24,7 +26,7 @@ func (ds *EurBtcRounded) Description() string {
}

func (ds *EurBtcRounded) Interval() uint64 {
return 300 // every 5 minutes
return gcfg.Interval
}

func (ds *EurBtcRounded) Value() (uint64, error) {
Expand Down
4 changes: 3 additions & 1 deletion datasources/usdbtc-rounded-random.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"math"
"math/rand"
"time"
"github.com/gertjaap/dlcoracle/gcfg"

)

type UsdBtcRoundedRandom struct {
Expand All @@ -22,7 +24,7 @@ func (ds *UsdBtcRoundedRandom) Description() string {
}

func (ds *UsdBtcRoundedRandom) Interval() uint64 {
return 300 // every 5 minutes
return gcfg.Interval
}

func (ds *UsdBtcRoundedRandom) Value() (uint64, error) {
Expand Down
5 changes: 4 additions & 1 deletion datasources/usdbtc-rounded.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"net/http"

"github.com/gertjaap/dlcoracle/logging"

"github.com/gertjaap/dlcoracle/gcfg"

)

type UsdBtcRounded struct {
Expand All @@ -24,7 +27,7 @@ func (ds *UsdBtcRounded) Description() string {
}

func (ds *UsdBtcRounded) Interval() uint64 {
return 300 // every 5 minutes
return gcfg.Interval
}

func (ds *UsdBtcRounded) Value() (uint64, error) {
Expand Down
4 changes: 3 additions & 1 deletion datasources/usdbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net/http"

"github.com/gertjaap/dlcoracle/logging"

"github.com/gertjaap/dlcoracle/gcfg"
)

type UsdBtc struct {
Expand All @@ -23,7 +25,7 @@ func (ds *UsdBtc) Description() string {
}

func (ds *UsdBtc) Interval() uint64 {
return 300 // every 5 minutes
return gcfg.Interval
}

type MinApiCryptoCompareBTCResponse struct {
Expand Down
5 changes: 5 additions & 0 deletions gcfg/gcfg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package gcfg


var DataDir string
var Interval uint64
62 changes: 59 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,70 @@ import (
"github.com/awnumar/memguard"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"

"path/filepath"
flags "github.com/jessevdk/go-flags"
"github.com/gertjaap/dlcoracle/gcfg"
)


type dlcConfig struct {
DataDir string `long:"DataDir" description:"Connect to bitcoin testnet3."`
HttpPort string `long:"HttpPort" description:"bc2 full node."`
Interval uint64 `long:"Interval" description:"Interval in seconds."`
}


// newConfigParser returns a new command line flags parser.
func newConfigParser(conf *dlcConfig, options flags.Options) *flags.Parser {
parser := flags.NewParser(conf, options)
return parser
}

var (
defaultDataDir = "oracle"
defaultHttpPort = "3000"
defaultInterval = uint64(300)
)

func main() {

logging.Init(os.Stdout, os.Stdout, os.Stdout, os.Stderr)

conf := dlcConfig{
DataDir: defaultDataDir,
HttpPort: defaultHttpPort,
Interval: defaultInterval,
}

var err error

preParser := newConfigParser(&conf, flags.HelpFlag)
_, err = preParser.ParseArgs(os.Args) // parse the cli
if err != nil {
logging.Error.Fatal(err)
}

// create home directory
_, err = os.Stat(conf.DataDir)
if err != nil {
logging.Info.Println("dlcoracle home directory does not exist.")
}
if os.IsNotExist(err) {
// first time the guy is running dlcoracle
os.Mkdir(conf.DataDir, 0700)
logging.Info.Println("dlcoracle home directory have been created")

}


gcfg.DataDir = conf.DataDir
gcfg.Interval = conf.Interval


logging.Info.Println("MIT-DCI Discreet Log Oracle starting...")

key, err := crypto.ReadKeyFile("data/privkey.hex")
key, err := crypto.ReadKeyFile(filepath.Join(conf.DataDir, "privkey.hex"))
if err != nil {
logging.Error.Fatal("Could not open or create keyfile:", err)
os.Exit(1)
Expand Down Expand Up @@ -53,7 +109,7 @@ func main() {
originsOk := handlers.AllowedOrigins([]string{"*"})
methodsOk := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"})

logging.Info.Println("Listening on port 3000")
logging.Info.Printf("Listening on port: %s", conf.HttpPort)

logging.Error.Fatal(http.ListenAndServe(":3000", handlers.CORS(originsOk, headersOk, methodsOk)(logging.WebLoggingMiddleware(r))))
logging.Error.Fatal(http.ListenAndServe(":" + conf.HttpPort, handlers.CORS(originsOk, headersOk, methodsOk)(logging.WebLoggingMiddleware(r))))
}
5 changes: 4 additions & 1 deletion store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ import (
"encoding/binary"
"fmt"
"time"
"path/filepath"


"github.com/adiabat/btcd/btcec"
"github.com/boltdb/bolt"
"github.com/gertjaap/dlcoracle/logging"
"github.com/gertjaap/dlcoracle/gcfg"
)

var db *bolt.DB

func Init() error {
database, err := bolt.Open("data/oracle.db", 0600, &bolt.Options{Timeout: 1 * time.Second})
database, err := bolt.Open(filepath.Join(gcfg.DataDir, "oracle.db"), 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
logging.Error.Fatal(err)
return err
Expand Down

0 comments on commit 329f7d0

Please sign in to comment.