-
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
Daniel Fayemi
committed
Dec 20, 2022
1 parent
89b36fd
commit 7c6fd2c
Showing
8 changed files
with
155 additions
and
74 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
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 |
---|---|---|
@@ -1,61 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"flutechain/database" | ||
"fmt" | ||
"os" | ||
"time" | ||
) | ||
// "flutechain/database" | ||
// "fmt" | ||
// "os" | ||
// "time" | ||
|
||
func main() { | ||
state, err := database.NewStateFromDisk() | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
|
||
defer state.Close() | ||
|
||
state.Add(database.NewTx(database.Account("dan"), database.Account("andre"), 400, "")) | ||
state.Add(database.NewTx(database.Account("dan"), database.Account("dan"), 1500, "reward")) | ||
|
||
hash, err := state.Persist() | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
|
||
block0 := database.NewBlock( | ||
hash, | ||
uint64(time.Now().Unix()), | ||
[]database.Tx{ | ||
database.NewTx(database.Account("dan"), database.Account("andre"), 400, ""), | ||
database.NewTx(database.Account("dan"), database.Account("dan"), 1500, "reward"), | ||
}, | ||
) | ||
|
||
err = state.AddBlock(block0) | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
|
||
blockHash0 := state.LatestHash() | ||
|
||
block1 := database.NewBlock( | ||
blockHash0, | ||
uint64(time.Now().Unix()), | ||
[]database.Tx{ | ||
database.NewTx(database.Account("andre"), database.Account("dan"), 100, ""), | ||
database.NewTx(database.Account("dan"), database.Account("sam"), 500, ""), | ||
database.NewTx(database.Account("dan"), database.Account("dan"), 1000, "reward"), | ||
}, | ||
) | ||
|
||
err = state.AddBlock(block1) | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
// dataDir, _ := cmd.Flags().GetString(flagDataDir) | ||
|
||
// state, err := database.NewStateFromDisk() | ||
// if err != nil { | ||
// fmt.Fprintln(os.Stderr, err) | ||
// os.Exit(1) | ||
// } | ||
|
||
// defer state.Close() | ||
|
||
// // state.Add(database.NewTx(database.Account("dan"), database.Account("andre"), 400, "")) | ||
// // state.Add(database.NewTx(database.Account("dan"), database.Account("dan"), 1500, "reward")) | ||
|
||
// hash, err := state.Persist() | ||
// if err != nil { | ||
// fmt.Fprintln(os.Stderr, err) | ||
// os.Exit(1) | ||
// } | ||
|
||
// block0 := database.NewBlock( | ||
// hash, | ||
// uint64(time.Now().Unix()), | ||
// []database.Tx{ | ||
// database.NewTx(database.Account("dan"), database.Account("andre"), 400, ""), | ||
// database.NewTx(database.Account("dan"), database.Account("dan"), 1500, "reward"), | ||
// }, | ||
// ) | ||
|
||
// err = state.AddBlock(block0) | ||
// if err != nil { | ||
// fmt.Fprintln(os.Stderr, err) | ||
// os.Exit(1) | ||
// } | ||
|
||
// blockHash0 := state.LatestHash() | ||
|
||
// block1 := database.NewBlock( | ||
// blockHash0, | ||
// uint64(time.Now().Unix()), | ||
// []database.Tx{ | ||
// database.NewTx(database.Account("andre"), database.Account("dan"), 100, ""), | ||
// database.NewTx(database.Account("dan"), database.Account("sam"), 500, ""), | ||
// database.NewTx(database.Account("dan"), database.Account("dan"), 1000, "reward"), | ||
// }, | ||
// ) | ||
|
||
// state.AddBlock(block1) | ||
// // state.Persist() | ||
|
||
// if err != nil { | ||
// fmt.Fprintln(os.Stderr, err) | ||
// os.Exit(1) | ||
// } | ||
|
||
} |
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,55 @@ | ||
package database | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func initDataDirIfNotExists(dataDir string) error { | ||
if fileExist(getGenesisJsonFilePath(dataDir)) { | ||
return nil | ||
} | ||
|
||
dbDir := getDatabaseDirPath(dataDir) | ||
if err := os.MkdirAll(dbDir, os.ModePerm); err != nil { | ||
return err | ||
} | ||
|
||
gen := getGenesisJsonFilePath(dataDir) | ||
if err := writeEmptyBlocksDbToDisk(gen); err != nil { | ||
return err | ||
} | ||
|
||
blocks := getBlocksDbFilePath(dataDir) | ||
if err := writeEmptyBlocksDbToDisk(blocks); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func getDatabaseDirPath(dataDir string) string { | ||
return filepath.Join(dataDir, "database") | ||
} | ||
|
||
func getGenesisJsonFilePath(dataDir string) string { | ||
return filepath.Join(dataDir, "genesis.json") | ||
} | ||
|
||
func getBlocksDbFilePath(dataDir string) string { | ||
return filepath.Join(getDatabaseDirPath(dataDir), "block.db") | ||
} | ||
|
||
func fileExist(filePath string) bool { | ||
_, err := os.Stat(filePath) | ||
if err != nil && os.IsNotExist(err) { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
func writeEmptyBlocksDbToDisk(path string) error { | ||
return ioutil.WriteFile(path, []byte(""), os.ModePerm) | ||
} |
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