forked from frankh/nano
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
118 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
./rai-vanity | ||
./rai-vanity | ||
db.sqlite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:") | ||
} |