Skip to content

Commit

Permalink
Merge branch 'master' of github.com:pegnet/pegnetd
Browse files Browse the repository at this point in the history
  • Loading branch information
Emyrk committed Sep 30, 2019
2 parents e17cb52 + 7b097d3 commit dd4aec9
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 125 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.13

require (
github.com/Factom-Asset-Tokens/factom v0.0.0-20190911201853-7b283996f02a
github.com/Factom-Asset-Tokens/fatd v0.6.0
github.com/Factom-Asset-Tokens/fatd v0.6.1-0.20190927200133-81408234a2b5
github.com/mattn/go-sqlite3 v1.11.0
github.com/pegnet/pegnet v0.1.0-rc4.0.20190924093136-5a53cdfd85af
github.com/sirupsen/logrus v1.4.2
Expand Down
122 changes: 13 additions & 109 deletions go.sum

Large diffs are not rendered by default.

18 changes: 3 additions & 15 deletions node/node.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package node

import (
"database/sql"
"os"

"github.com/Factom-Asset-Tokens/factom"
_ "github.com/mattn/go-sqlite3"
"github.com/pegnet/pegnet/modules/grader"
Expand All @@ -23,9 +20,6 @@ type Pegnetd struct {
Sync BlockSync

Pegnet *pegnet.Pegnet

// This is the sqlite db to store state
DB *sql.DB
}

func NewPegnetd(conf *viper.Viper) (*Pegnetd, error) {
Expand All @@ -44,21 +38,15 @@ func NewPegnetd(conf *viper.Viper) (*Pegnetd, error) {
}

n.Pegnet = pegnet.New(conf)
if err := n.Pegnet.Init(); err != nil {
return nil, err
}

// TODO: Check this, harcoding it high to skip the initial stuff
n.Sync.Synced = 206421

// TODO :Is this the spot spot to init?
grader.InitLX()

// Load the sqldb (or create it)
path := os.ExpandEnv(viper.GetString(config.SqliteDBPath))
// TODO: Idc which sqlite to use. Change this if you want.
db, err := sql.Open("sqlite3", path)
if err != nil {
return nil, err
}
n.DB = db

return n, nil
}
90 changes: 90 additions & 0 deletions node/pegnet/addresses.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package pegnet

import (
"github.com/Factom-Asset-Tokens/factom"
"github.com/pegnet/pegnetd/fat/fat2"
)

const createTableAddresses = `CREATE TABLE "pn_addresses" (
"id" INTEGER PRIMARY KEY,
"address" BLOB NOT NULL UNIQUE,
"peg_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("peg_balance" >= 0),
"pusd_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pusd_balance" >= 0),
"peur_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("peur_balance" >= 0),
"pjpy_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pjpy_balance" >= 0),
"pgbp_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pgbp_balance" >= 0),
"pcad_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pcad_balance" >= 0),
"pchf_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pchf_balance" >= 0),
"pinr_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pinr_balance" >= 0),
"psgd_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("psgd_balance" >= 0),
"pcny_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pcny_balance" >= 0),
"phkd_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("phkd_balance" >= 0),
"pkrw_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pkrw_balance" >= 0),
"pbrl_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pbrl_balance" >= 0),
"pphp_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pphp_balance" >= 0),
"pmxn_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pmxn_balance" >= 0),
"pxau_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pxau_balance" >= 0),
"pxag_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pxag_balance" >= 0),
"pxbt_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pxbt_balance" >= 0),
"peth_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("peth_balance" >= 0),
"pltc_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pltc_balance" >= 0),
"prvn_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("prvn_balance" >= 0),
"pxbc_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pxbc_balance" >= 0),
"pfct_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pfct_balance" >= 0),
"pbnb_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pbnb_balance" >= 0),
"pxlm_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pxlm_balance" >= 0),
"pada_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pada_balance" >= 0),
"pxmr_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pxmr_balance" >= 0),
"pdas_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pdas_balance" >= 0),
"pzec_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pzec_balance" >= 0),
"pdcr_balance" INTEGER NOT NULL
CONSTRAINT "insufficient balance" CHECK ("pdcr_balance" >= 0)
);
`

func (p *Pegnet) CreateTableAddresses() error {
_, err := p.DB.Exec(createTableAddresses)
if err != nil {
return err
}
return nil
}

func (p *Pegnet) AddToBalance(adr *factom.FAAddress, ticker fat2.PTicker, add uint64) (int64, error) {
// TODO: implement AddToBalance
return 0, nil
}

func (p *Pegnet) SubFromBalance(adr *factom.FAAddress, ticker fat2.PTicker, add uint64) (int64, error) {
// TODO: implement SubFromBalance
return 0, nil
}
31 changes: 31 additions & 0 deletions node/pegnet/pegnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package pegnet

import (
"container/list"
"database/sql"
"fmt"
"github.com/pegnet/pegnetd/config"
"os"
"os/user"

"github.com/pegnet/pegnet/modules/grader"

Expand All @@ -13,6 +18,9 @@ type Pegnet struct {

// TODO: Make this a database
PegnetChain *list.List

// This is the sqlite db to store state
DB *sql.DB
}

func New(conf *viper.Viper) *Pegnet {
Expand All @@ -23,6 +31,29 @@ func New(conf *viper.Viper) *Pegnet {
return p
}

func (p *Pegnet) Init() error {
path := os.ExpandEnv(viper.GetString(config.SqliteDBPath))
usr, err := user.Current()
if err != nil {
return err
}
path = fmt.Sprintf("%s/%s", usr.HomeDir, path)
// TODO: Idc which sqlite to use. Change this if you want.T
db, err := sql.Open("sqlite3", path)
if err != nil {
return err
}
p.DB = db
err = p.CreateTableAddresses()
if err != nil {
// TODO: implement better schema validation
if err.Error() != "table \"pn_addresses\" already exists" {
return err
}
}
return nil
}

func (p *Pegnet) InsertGradedBlock(block grader.GradedBlock) {
p.PegnetChain.PushBack(block)
}
Expand Down
1 change: 1 addition & 0 deletions pegnetd-conf.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Pegnetd config file
[app]
loglevel = "info"
dbpath = ".pegnet/node.db"

[dblocksync]
retry = "5s"

0 comments on commit dd4aec9

Please sign in to comment.