Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dlcoracle prepared for run within mit-dci/lit testing framework. #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
11 changes: 7 additions & 4 deletions datasources/usdbtc-rounded-random.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package datasources

import (
"math"
//"math"
"math/rand"
"time"
"github.com/gertjaap/dlcoracle/gcfg"

)

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

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

func (ds *UsdBtcRoundedRandom) Value() (uint64, error) {
satoshiValue := uint64(math.Floor(float64(random(100, 150))+0.5)) * 100
return satoshiValue, nil
//satoshiValue := uint64(math.Floor(float64(random(100, 150))+0.5)) * 100
//return satoshiValue, nil
return gcfg.ValueToPublish, nil
}

func random(min, max int) int {
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
6 changes: 6 additions & 0 deletions gcfg/gcfg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package gcfg


var DataDir string
var Interval uint64
var ValueToPublish uint64
69 changes: 66 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,77 @@ 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."`
// For testing purposes
Interval uint64 `long:"Interval" description:"Interval in seconds."`
ValueToPublish uint64 `long:"ValueToPublish" description:"Value to publish"`
}


// 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"
// For testing purposes
defaultInterval = uint64(300)
defaultValueToPublish = uint64(11)
)

func main() {

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

conf := dlcConfig{
DataDir: defaultDataDir,
HttpPort: defaultHttpPort,
// For testing purposes
Interval: defaultInterval,
ValueToPublish: defaultValueToPublish,
}

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
gcfg.ValueToPublish = conf.ValueToPublish


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 +116,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