-
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
Nov 1, 2022
0 parents
commit eea1b3a
Showing
20 changed files
with
602 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,61 @@ | ||
package main | ||
|
||
import ( | ||
"flutechain/database" | ||
"fmt" | ||
"github.com/spf13/cobra" | ||
"os" | ||
) | ||
|
||
var balancesListCommand = &cobra.Command{ | ||
Use: "list", | ||
Short: "list all wallet balances.", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
state, err := database.NewStateFromDisk() | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
|
||
defer state.Close() | ||
|
||
fmt.Println("Account Balances:") | ||
fmt.Println("__________________") | ||
fmt.Println("") | ||
|
||
for account, balance := range state.Balances { | ||
fmt.Println(fmt.Sprintf("%s: %d", account, balance)) | ||
} | ||
}, | ||
} | ||
|
||
func balancesCmd() *cobra.Command { | ||
var balanceCmd = &cobra.Command{ | ||
Use: "balances", | ||
Short: "interact with balances", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
state, err := database.NewStateFromDisk() | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
|
||
defer state.Close() | ||
|
||
account, _ := cmd.Flags().GetString(accountFlag) | ||
|
||
if account == "" { | ||
fmt.Errorf("incomplete request") | ||
os.Exit(1) | ||
} | ||
|
||
fmt.Println(fmt.Sprintf("%s: %d", account, state.Balances[database.Account(account)])) | ||
}, | ||
} | ||
|
||
addDefaultRequiredFlags(balanceCmd) | ||
|
||
balanceCmd.AddCommand(balancesListCommand) | ||
|
||
return balanceCmd | ||
} |
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,41 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/spf13/cobra" | ||
"os" | ||
) | ||
|
||
const accountFlag = "account" | ||
|
||
const fromFlag = "from" | ||
const toFlag = "to" | ||
const valueFlag = "value" | ||
|
||
func main() { | ||
var cmd = &cobra.Command{ | ||
Use: "flt", | ||
Short: "FluteChain CLI", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
}, | ||
} | ||
|
||
cmd.AddCommand(versionCmd) | ||
cmd.AddCommand(balancesCmd()) | ||
cmd.AddCommand(txCmd()) | ||
|
||
err := cmd.Execute() | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func incorrectUsageErr() error { | ||
return fmt.Errorf("incorrect usage") | ||
} | ||
|
||
func addDefaultRequiredFlags(cmd *cobra.Command) { | ||
cmd.Flags().String(accountFlag, "", "account to get balance") | ||
cmd.MarkFlagRequired(accountFlag) | ||
} |
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,68 @@ | ||
package main | ||
|
||
import ( | ||
"flutechain/database" | ||
"fmt" | ||
"github.com/spf13/cobra" | ||
"os" | ||
) | ||
|
||
func txCmd() *cobra.Command { | ||
var txsCmd = &cobra.Command{ | ||
Use: "tx", | ||
Short: "Interact with txs (add, )", | ||
PreRunE: func(cmd *cobra.Command, args []string) error { | ||
return incorrectUsageErr() | ||
}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
}, | ||
} | ||
|
||
txsCmd.AddCommand(txAddCmd()) | ||
|
||
return txsCmd | ||
} | ||
|
||
func txAddCmd() *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "add", | ||
Short: "Adds new Tx to database", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
from, _ := cmd.Flags().GetString(fromFlag) | ||
to, _ := cmd.Flags().GetString(toFlag) | ||
value, _ := cmd.Flags().GetUint(valueFlag) | ||
|
||
fromAcc := database.NewAccount(from) | ||
toAcc := database.NewAccount(to) | ||
|
||
tx := database.NewTx(fromAcc, toAcc, value, "") | ||
|
||
state, err := database.NewStateFromDisk() | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
os.Exit(1) | ||
} | ||
|
||
defer state.Close() | ||
|
||
err = state.Add(tx) | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
} | ||
|
||
_, err = state.Persist() | ||
if err != nil { | ||
fmt.Fprintln(os.Stderr, err) | ||
} | ||
|
||
fmt.Println("TX successfully added to ledger") | ||
}, | ||
} | ||
|
||
cmd.Flags().String(fromFlag, "", "Account to send tokens from") | ||
cmd.Flags().String(toFlag, "", "Account to send tokens to") | ||
cmd.Flags().Uint(valueFlag, 0, "Amount of tokens to send") | ||
|
||
return cmd | ||
} |
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,19 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const Major = "0" | ||
const Minor = "1" | ||
const Fix = "0" | ||
const Tag = "beta" | ||
|
||
var versionCmd = &cobra.Command{ | ||
Use: "version", | ||
Short: "Describes version", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Printf("version %s.%s.%s-%s \n", Major, Minor, Fix, Tag) | ||
}, | ||
} |
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,61 @@ | ||
package main | ||
|
||
import ( | ||
"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) | ||
} | ||
|
||
} |
Empty file.
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,45 @@ | ||
package database | ||
|
||
import ( | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"encoding/json" | ||
) | ||
|
||
type Hash [32]byte | ||
|
||
func (h Hash) MarshalText() ([]byte, error) { | ||
return []byte(h.Hex()), nil | ||
} | ||
|
||
func (h Hash) Hex() string { | ||
return hex.EncodeToString(h[:]) | ||
} | ||
|
||
type BlockHeader struct { | ||
Parent Hash | ||
Time uint64 | ||
} | ||
|
||
type Block struct { | ||
Header BlockHeader | ||
Tx []Tx | ||
} | ||
|
||
type BlockFS struct { | ||
Key Hash | ||
Value Block | ||
} | ||
|
||
func (b Block) Hash() (Hash, error) { | ||
blockJson, err := json.Marshal(b) | ||
if err != nil { | ||
return Hash{}, err | ||
} | ||
|
||
return sha256.Sum256(blockJson), nil | ||
} | ||
|
||
func NewBlock(hash Hash, time uint64, tx []Tx) Block { | ||
return Block{BlockHeader{hash, time}, tx} | ||
} |
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,26 @@ | ||
package database | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
) | ||
|
||
type Genesis struct { | ||
Balances map[Account]uint `json:"balances"` | ||
} | ||
|
||
func LoadGenesis(path string) (Genesis, error) { | ||
content, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return Genesis{}, err | ||
} | ||
|
||
var loadedGenesis Genesis | ||
|
||
err = json.Unmarshal(content, &loadedGenesis) | ||
if err != nil { | ||
return Genesis{}, err | ||
} | ||
|
||
return loadedGenesis, nil | ||
} |
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,7 @@ | ||
{ | ||
"genesis_time": "2020-06-01T00:00:00.000000000Z", | ||
"chain_id": "flutechain-mainnet", | ||
"balances": { | ||
"dan": 1000000 | ||
} | ||
} |
Oops, something went wrong.