Skip to content

Commit

Permalink
Add storage package
Browse files Browse the repository at this point in the history
  • Loading branch information
frankh committed Jan 9, 2018
1 parent 7405e59 commit ff25bfe
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
./rai-vanity
./rai-vanity
db.sqlite
2 changes: 1 addition & 1 deletion blocks/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

const LiveGenesisBlockHash rai.BlockHash = "991CF190094C00F0B68E2E5F75F6BEE95A2E0BD93CEAA4A6734DB9F19B728948"

var live_genesis_block = JsonBlock([]byte(`{
var LiveGenesisBlock = JsonBlock([]byte(`{
"type": "open",
"source": "E89208DD038FBB269987689621D52292AE9C35941A7484756ECCED92A65093BA",
"representative": "xrb_3t6k35gi95xu6tergt6p69ck76ogmitsa8mnijtpxm9fkcm736xtoncuohr3",
Expand Down
6 changes: 3 additions & 3 deletions blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func TestSignMessage(t *testing.T) {
}

func TestValidateWork(t *testing.T) {
live_block_hash, _ := address.AddressToPub(live_genesis_block.Account)
live_work_bytes, _ := hex.DecodeString(string(live_genesis_block.Work))
live_block_hash, _ := address.AddressToPub(LiveGenesisBlock.Account)
live_work_bytes, _ := hex.DecodeString(string(LiveGenesisBlock.Work))
live_bad_work, _ := hex.DecodeString("0000000000000000")

if !ValidateWork(live_block_hash, live_work_bytes) {
Expand All @@ -44,7 +44,7 @@ func TestValidateWork(t *testing.T) {
}

func TestHashOpen(t *testing.T) {
live_hash := strings.ToUpper(hex.EncodeToString(live_genesis_block.Hash()))
live_hash := strings.ToUpper(hex.EncodeToString(LiveGenesisBlock.Hash()))
if live_hash != string(LiveGenesisBlockHash) {
t.Errorf("Genesis block hash is not correct, expected %s, got %s", LiveGenesisBlockHash, live_hash)
}
Expand Down
103 changes: 103 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package storage

import (
"database/sql"
"github.com/frankh/rai/blocks"
_ "github.com/mattn/go-sqlite3"
)

var Conn *sql.DB

func Init(path string) {
var err error

Conn, err = sql.Open("sqlite3", path)
if err != nil {
panic(err)
}

table_check, err := Conn.Query(`SELECT name FROM sqlite_master WHERE type='table' AND name='block'`)
if err != nil {
panic(err)
}

if !table_check.Next() {
prep, err := Conn.Prepare(`
CREATE TABLE 'block' (
'hash' TEXT PRIMARY KEY,
'type' TEXT NOT NULL,
'account' TEXT NULL,
'source' TEXT NULL,
'representative' TEXT NULL,
'previous' TEXT NULL,
'balance' TEXT NULL,
'destination' TEXT NULL,
'work' TEXT NULL,
'signature' VARCHAR(64) NULL,
'created' DATE DEFAULT CURRENT_TIMESTAMP NOT NULL,
FOREIGN KEY(previous) REFERENCES block(hash)
)
`)
if err != nil {
panic(err)
}

_, err = prep.Exec()
if err != nil {
panic(err)
}
}

rows, err := Conn.Query(`SELECT hash FROM block WHERE hash=?`, blocks.LiveGenesisBlockHash)
if err != nil {
panic(err)
}
if !rows.Next() {
StoreBlock(blocks.LiveGenesisBlock)
}

}

func StoreBlock(b blocks.Block) {
switch b.Type {
case blocks.Open:
prep, err := Conn.Prepare(`
INSERT INTO block (
hash,
type,
source,
representative,
account,
work,
signature
) values (
?,
'open',
?,
?,
?,
?,
?
)
`)

if err != nil {
panic(err)
}

_, err = prep.Exec(
b.HashToString(),
b.Source,
b.Representative,
b.Account,
b.Work,
b.Signature,
)

if err != nil {
panic(err)
}
default:
panic("Unknown block type")
}
}
9 changes: 9 additions & 0 deletions storage/storage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package storage

import (
"testing"
)

func TestInit(t *testing.T) {
Init(":memory:")
}

0 comments on commit ff25bfe

Please sign in to comment.