Skip to content

Commit

Permalink
Hashes DB Content on every persist of a new TX.
Browse files Browse the repository at this point in the history
Updates to version v0.3.0
  • Loading branch information
web3coach committed Apr 14, 2020
1 parent 6420451 commit b99e519
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cmd/tbb/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var balancesListCmd = &cobra.Command{
}
defer state.Close()

fmt.Println("Accounts balances:")
fmt.Printf("Accounts balances at %x:\n", state.LatestSnapshot())
fmt.Println("__________________")
fmt.Println("")
for account, balance := range state.Balances {
Expand Down
4 changes: 2 additions & 2 deletions cmd/tbb/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ func txAddCmd() *cobra.Command {
os.Exit(1)
}

err = state.Persist()
_, err = state.Persist()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

fmt.Println("TX successfully added to the ledger.")
fmt.Println("TX successfully persisted to the ledger.")
},
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/tbb/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
)

const Major = "0"
const Minor = "2"
const Minor = "3"
const Fix = "0"
const Verbal = "tbb tx add --data=reward"
const Verbal = "Immutable Snapshots"

var versionCmd = &cobra.Command{
Use: "version",
Expand Down
71 changes: 56 additions & 15 deletions database/state.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package database

import (
"os"
"path/filepath"
"encoding/json"
"bufio"
"crypto/sha256"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)

type Snapshot [32]byte

type State struct {
Balances map[Account]uint
Balances map[Account]uint
txMempool []Tx

dbFile *os.File
dbFile *os.File
snapshot Snapshot
}

func NewStateFromDisk() (*State, error) {
Expand All @@ -38,24 +43,36 @@ func NewStateFromDisk() (*State, error) {

scanner := bufio.NewScanner(f)

state := &State{balances, make([]Tx, 0), f}
state := &State{balances, make([]Tx, 0), f, Snapshot{}}

for scanner.Scan() {
if err := scanner.Err(); err != nil {
return nil, err
}

var tx Tx
json.Unmarshal(scanner.Bytes(), &tx)
err = json.Unmarshal(scanner.Bytes(), &tx)
if err != nil {
return nil, err
}

if err := state.apply(tx); err != nil {
return nil, err
}
}

err = state.doSnapshot()
if err != nil {
return nil, err
}

return state, nil
}

func (s *State) LatestSnapshot() Snapshot {
return s.snapshot
}

func (s *State) Add(tx Tx) error {
if err := s.apply(tx); err != nil {
return err
Expand All @@ -66,28 +83,36 @@ func (s *State) Add(tx Tx) error {
return nil
}

func (s *State) Persist() error {
func (s *State) Persist() (Snapshot, error) {
mempool := make([]Tx, len(s.txMempool))
copy(mempool, s.txMempool)

for i := 0; i < len(mempool); i++ {
txJson, err := json.Marshal(s.txMempool[i])
if err != nil {
return err
return Snapshot{}, err
}

fmt.Printf("Persisting new TX to disk:\n")
fmt.Printf("\t%s\n", txJson)
if _, err = s.dbFile.Write(append(txJson, '\n')); err != nil {
return err
return Snapshot{}, err
}

err = s.doSnapshot()
if err != nil {
return Snapshot{}, err
}
fmt.Printf("New DB Snapshot: %x\n", s.snapshot)

s.txMempool = append(s.txMempool[:i], s.txMempool[i+1:]...)
}

return nil
return s.snapshot, nil
}

func (s *State) Close() {
s.dbFile.Close()
func (s *State) Close() error {
return s.dbFile.Close()
}

func (s *State) apply(tx Tx) error {
Expand All @@ -96,12 +121,28 @@ func (s *State) apply(tx Tx) error {
return nil
}

if s.Balances[tx.From] - tx.Value < 0 {
if s.Balances[tx.From]-tx.Value < 0 {
return fmt.Errorf("insufficient balance")
}

s.Balances[tx.From] -= tx.Value
s.Balances[tx.To] += tx.Value

return nil
}
}

func (s *State) doSnapshot() error {
// Re-read the whole file from the first byte
_, err := s.dbFile.Seek(0, 0)
if err != nil {
return err
}

txsData, err := ioutil.ReadAll(s.dbFile)
if err != nil {
return err
}
s.snapshot = sha256.Sum256(txsData)

return nil
}
2 changes: 2 additions & 0 deletions database/tx.db
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
{"from":"babayaga","to":"andrej","value":50,"data":""}
{"from":"andrej","to":"andrej","value":100,"data":"reward"}
{"from":"andrej","to":"andrej","value":100,"data":"reward"}
{"from":"andrej","to":"andrej","value":100,"data":"reward"}
{"from":"andrej","to":"andrej","value":100,"data":"reward"}

0 comments on commit b99e519

Please sign in to comment.