Skip to content

CMD fix and addition #762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/zoobc/zoobc-core/cmd/noderegistry"
"github.com/zoobc/zoobc-core/cmd/parser"
"github.com/zoobc/zoobc-core/cmd/rollback"
"github.com/zoobc/zoobc-core/cmd/scramblednodes"
"github.com/zoobc/zoobc-core/cmd/signature"
"github.com/zoobc/zoobc-core/cmd/transaction"
)
Expand Down Expand Up @@ -40,6 +41,8 @@ func main() {
generateCmd.AddCommand(block.Commands())
generateCmd.AddCommand(noderegistry.Commands())
parserCmd.AddCommand(parser.Commands())
generateCmd.AddCommand(scramblednodes.Commands()["getScrambledNodesCmd"])
generateCmd.AddCommand(scramblednodes.Commands()["getPriorityPeersCmd"])
_ = rootCmd.Execute()

}
46 changes: 34 additions & 12 deletions cmd/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,37 @@ Command line interface to as a utility tools to develop the zoobc system.
- example: `go run main.go account generate` will generate account to use.

### See more help about commands

- `go run main --help` to see available commands and flags
- `go run main {command} --help` to see to see available subcommands and flags
- `go run main {command} {subcommand} --help` to see to see available subcommands and flags of subcommand

### Transaction general flag
- `--output` to provide generated ouput type. Example: `--ouput bytes`
- `--version` to provide version of transaction. Example: `--version 1`
- `--timestamp` to provide timestamp of trasaction. Example: `--timestamp 1234567`
- `--sender-seed` to provide the seed of sender transaction. Example: `--sender-seed "concur vocalist rotten busload gap quote stinging undiluted surfer goofiness deviation starved"`
- `--recipient` provide recepient transaction. Example `--recipient VZvYd80p5S-rxSNQmMZwYXC7LyAzBmcfcj4MUUAdudWM`
- `--fee` to provide fee transaction, Example `--fee 1`
- `--post` to define automate post transaction or not. Example: `-post true`
- `--post-host` to provide where the transaction will post. Example: `--post-host "127.0.0.1:7000"`
- `--sender-signature-type` to provide type of transaction signature and effected to the type of the sender account. Example: `--sender-signature-type 1`
### Transaction general flag

- `--output` to provide generated ouput type. Example: `--ouput bytes`
- `--version` to provide version of transaction. Example: `--version 1`
- `--timestamp` to provide timestamp of trasaction. Example: `--timestamp 1234567`
- `--sender-seed` to provide the seed of sender transaction. Example: `--sender-seed "concur vocalist rotten busload gap quote stinging undiluted surfer goofiness deviation starved"`
- `--recipient` provide recepient transaction. Example `--recipient VZvYd80p5S-rxSNQmMZwYXC7LyAzBmcfcj4MUUAdudWM`
- `--fee` to provide fee transaction, Example `--fee 1`
- `--post` to define automate post transaction or not. Example: `-post true`
- `--post-host` to provide where the transaction will post. Example: `--post-host "127.0.0.1:7000"`
- `--sender-signature-type` to provide type of transaction signature and effected to the type of the sender account. Example: `--sender-signature-type 1`

### Transaction Send Money

```
go run main.go generate transaction send-money --timestamp 1257894000 --sender-seed "concur vocalist rotten busload gap quote stinging undiluted surfer goofiness deviation starved" --recipient VZvYd80p5S-rxSNQmMZwYXC7LyAzBmcfcj4MUUAdudWM --amount 5000000000
```

#### Transaction send money escrow, set flag `--escrow true` and 3 more fields: `--approver-address`, `--timeout`, `--commission` and `--instruction`

```bash
go run main.go generate transaction send-money --escrow true --approver-address BCZEGOb3WNx3fDOVf9ZS4EjvOIv_UeW4TVBQJ_6tHKlE --timeout 200 --sender-seed "execute beach inflict session course dance vanish cover lawsuit earth casino fringe waste warfare also habit skull donate window cannon scene salute dawn good" --amount 1111 --commission 111 --instruction "Check amount should be 111" --recipient nK_ouxdDDwuJiogiDAi_zs1LqeN7f5ZsXbFtXGqGc0Pd
```

### Transaction Register Node

```bash
go run main.go generate transaction register-node --timestamp 1257894000 --sender-seed "concur vocalist rotten busload gap quote stinging undiluted surfer goofiness deviation starved" --node-owner-account-address "VZvYd80p5S-rxSNQmMZwYXC7LyAzBmcfcj4MUUAdudWM" --node-seed "sprinkled sneak species pork outpost thrift unwind cheesy vexingly dizzy neurology neatness" --node-address "127.0.0.1:8001" --locked-balance 1000000000 --poow-hex "7233537248687a792d35726c71475f644f473258626a504263574f68445552495070465267675254732d327458d880d3d1e6d68a8afeaa2c030ce50b7562fca7b7cb2ddac419c6e2ee33e0a7030000004d4e8d33954aa3deee656de56289e77d17ba29baff32da82147500e354ceaacf6cdafd6437a1037f243574dbeb2b81f52dd459ae8f0ee2ce4cbc272f832"
```
Expand Down Expand Up @@ -79,6 +83,7 @@ go run main.go generate transaction remove-account-dataset --timestamp 125789400
```

### Transaction Escrow Approval

```bash
go run main.go generate transaction escrow-approval --transaction-id -2546596465476625657 --approval true --sender-seed "concur vocalist rotten busload gap quote stinging undiluted surfer goofiness deviation starved" --fee 111
```
Expand All @@ -104,6 +109,7 @@ go run main.go generate account bitcoin --seed "concur vocalist rotten busload g
```

### Account Generating multisig

```bash
go run main.go generate account multisig --addresses "BCZnSfqpP5tqFQlMTYkDeBVFWnbyVK7vLr5ORFpTjgtN" --addresses "BCZD_VxfO2S9aziIL3cn_cXW7uPDVPOrnXuP98GEAUC7" --addresses "BCZKLvgUYZ1KKx-jtF9KoJskjVPvB9jpIjfzzI6zDW0J" —-min-sigs=2 --nonce=3
```
Expand Down Expand Up @@ -134,22 +140,38 @@ outputs cmd/genesis.go.new and cmd/cluster_config.json
```

### Generate Proof of Ownership Node Registry

```bash
go run main.go generate poow --node-seed "concur vocalist rotten busload gap quote stinging undiluted surfer goofiness deviation starved" --node-owner-account-address "VZvYd80p5S-rxSNQmMZwYXC7LyAzBmcfcj4MUUAdudWM" --db-node-path "../resource" --db-node-name "zoobc.db"
--output-type "hex"
--output-type "hex"
```

### Rollback Blockchain State

```bash
go run main.go rollback blockchain --to-height 10 --db-path "../resource" --db-name "zoobc.db"
```

### Signature Signing data using Ed25519

```bash
go run main.go signature sign ed25519 --data-bytes='1, 222, 54, 12, 32' --use-slip10=true
```

### Signature Verifying data

```bash
go run main.go signature verify --data-bytes='1, 222, 54, 12, 32' --signature-hex=0000000063851d61318eaf923ff72457fd9b5716db9904aacbe53eb1bc25cd8a7bf2816c61402b0c52d4324e1336bae4ea28194d6f5c531292fd266e63a293519f20c20b --account-address=WI-u0jyKMGsPHk6K7eT1Utnxc6WiKehkIEs87Zf3fIsH
```
```

### Scrambled Nodes

```
go run main.go generate scrambledNodes --db-name zoobc_2.db --height 11153
```

### Priority Peers

```
go run main.go generate priorityPeers --db-name zoobc_2.db --height 11153 --sender-full-address "n56.alpha.proofofparticipation.network:8001"
```
12 changes: 6 additions & 6 deletions cmd/rollback/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,21 @@ func Commands() *cobra.Command {
func rollbackBlockChain() RunCommand {
var (
chaintypeRollback = chaintype.GetChainType(0)
dB, err = getSqliteDB(dBPath, dBName)
)
if err != nil {
fmt.Println("Failed get Db")
panic(err)
}

return func(ccmd *cobra.Command, args []string) {
var (
queryExecutor = query.NewQueryExecutor(dB)
derivedQueries = query.GetDerivedQuery(chaintypeRollback)
blockQuery = query.NewBlockQuery(chaintypeRollback)
dB, err = getSqliteDB(dBPath, dBName)
queryExecutor = query.NewQueryExecutor(dB)
rowLastBlock, _ = queryExecutor.ExecuteSelectRow(blockQuery.GetLastBlock(), false)
lastBlock model.Block
)
if err != nil {
fmt.Println("Failed get Db")
panic(err)
}

err = blockQuery.Scan(&lastBlock, rowLastBlock)
if err != nil {
Expand Down
133 changes: 133 additions & 0 deletions cmd/scramblednodes/scramblednodes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package scramblednodes

import (
"database/sql"
"encoding/json"
"fmt"

"github.com/spf13/cobra"
"github.com/zoobc/zoobc-core/common/chaintype"
"github.com/zoobc/zoobc-core/common/constant"
"github.com/zoobc/zoobc-core/common/database"
"github.com/zoobc/zoobc-core/common/model"
"github.com/zoobc/zoobc-core/common/query"
"github.com/zoobc/zoobc-core/core/service"
p2pUtil "github.com/zoobc/zoobc-core/p2p/util"
)

type (
// RunCommand represent of output function from rollback commands
RunCommand func(ccmd *cobra.Command, args []string)
)

var (
// Flag Command
wantedBlockHeight uint32
dBPath, dBName, senderFullAddress string

// subcommand, getScrambledNodes blockchain
getScrambledNodesCmd = &cobra.Command{
Use: "scrambledNodes",
Short: "scrambledNodes subcommand to get the list of nodes in the scrambledNodes at that height",
Long: "scrambledNodes subcommand to get the list of nodes in the scrambledNodes at that height",
}
getPriorityPeersCmd = &cobra.Command{
Use: "priorityPeers",
Short: "priorityPeers subcommand to get the list of nodes in the priorityPeers at that height",
Long: "priorityPeers subcommand to get the list of nodes in the priorityPeers at that height",
}
)

func init() {
// Rollback Blockchain flag
getScrambledNodesCmd.Flags().Uint32Var(&wantedBlockHeight, "height", 0, "Block height at which the scrambled nodes is positioned")
getScrambledNodesCmd.Flags().StringVar(&dBPath, "db-path", "../resource", "path of DB blockchain wanted to rollback")
getScrambledNodesCmd.Flags().StringVar(&dBName, "db-name", "zoobc.db", "name of DB blockchain wanted to rollback")

getPriorityPeersCmd.Flags().Uint32Var(&wantedBlockHeight, "height", 0, "Block height at which the scrambled nodes is positioned")
getPriorityPeersCmd.Flags().StringVar(&dBPath, "db-path", "../resource", "path of DB blockchain wanted to rollback")
getPriorityPeersCmd.Flags().StringVar(&dBName, "db-name", "zoobc.db", "name of DB blockchain wanted to rollback")
getPriorityPeersCmd.Flags().StringVar(&senderFullAddress, "sender-full-address", "127.0.0.1:8001", "the full address of the sender")
}

// Commands return Instance of rollback command
func Commands() map[string]*cobra.Command {
getScrambledNodesCmd.Run = func(ccmd *cobra.Command, args []string) {
scrambledNodes := getScrambledNodesAtHeight()
j, _ := json.MarshalIndent(scrambledNodes.IndexNodes, "", " ")
stringJSON := string(j)
fmt.Println(stringJSON)
fmt.Println("scrambledNodes length ", len(scrambledNodes.IndexNodes))
}

getPriorityPeersCmd.Run = func(ccmd *cobra.Command, args []string) {
priorityPeers := getPriorityPeers()
j, _ := json.MarshalIndent(priorityPeers, "", " ")
stringJSON := string(j)
fmt.Println(stringJSON)
}

return map[string]*cobra.Command{"getScrambledNodesCmd": getScrambledNodesCmd, "getPriorityPeersCmd": getPriorityPeersCmd}
}

func getPriorityPeers() map[string]*model.Peer {
scrambledNodes := getScrambledNodesAtHeight()
peers, err := p2pUtil.GetPriorityPeersByNodeFullAddress(
senderFullAddress,
scrambledNodes,
)
if err != nil {
panic(err)
}
return peers
}

// getScrambledNodesAtHeight func to run rollback to all
func getScrambledNodesAtHeight() *model.ScrambledNodes {
var (
dB, err = getSqliteDB(dBPath, dBName)
)
if err != nil {
fmt.Println("Failed get Db")
panic(err)
}

var (
queryExecutor = query.NewQueryExecutor(dB)

nodeRegistrationService = service.NewNodeRegistrationService(
queryExecutor,
query.NewAccountBalanceQuery(),
query.NewNodeRegistrationQuery(),
query.NewParticipationScoreQuery(),
query.NewBlockQuery(&chaintype.MainChain{}),
nil,
nil,
)
)

scrambledNodes, err := nodeRegistrationService.GetScrambleNodesByHeight(wantedBlockHeight)
if err != nil {
panic(err)
}
return scrambledNodes
}

// getSqliteDB to get sql.Db of sqlite based on DB path & name
func getSqliteDB(dbPath, dbName string) (*sql.DB, error) {
var sqliteDbInstance = database.NewSqliteDB()
if err := sqliteDbInstance.InitializeDB(dbPath, dbName); err != nil {
return nil, err
}
sqliteDB, err := sqliteDbInstance.OpenDB(
dbPath,
dbName,
constant.SQLMaxOpenConnetion,
constant.SQLMaxIdleConnections,
constant.SQLMaxConnectionLifetime,
)
if err != nil {
return nil, err
}
return sqliteDB, nil
}
8 changes: 7 additions & 1 deletion core/service/snapshotMainBlockService.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/zoobc/zoobc-core/common/chaintype"
"github.com/zoobc/zoobc-core/common/constant"
"github.com/zoobc/zoobc-core/common/model"
"github.com/zoobc/zoobc-core/common/monitoring"
"github.com/zoobc/zoobc-core/common/query"
)

Expand Down Expand Up @@ -192,7 +193,8 @@ func (ss *SnapshotMainBlockService) IsSnapshotHeight(height uint32) bool {
// InsertSnapshotPayloadToDB insert snapshot data to db
func (ss *SnapshotMainBlockService) InsertSnapshotPayloadToDB(payload *model.SnapshotPayload, height uint32) error {
var (
queries [][]interface{}
queries [][]interface{}
highestBlock *model.Block
)

err := ss.QueryExecutor.BeginTx()
Expand All @@ -212,6 +214,9 @@ func (ss *SnapshotMainBlockService) InsertSnapshotPayloadToDB(payload *model.Sna
case "block":
for _, rec := range payload.Blocks {
qry, args := ss.BlockQuery.InsertBlock(rec)
if highestBlock == nil || highestBlock.Height < rec.Height {
highestBlock = rec
}
queries = append(queries,
append(
[]interface{}{qry}, args...),
Expand Down Expand Up @@ -317,5 +322,6 @@ func (ss *SnapshotMainBlockService) InsertSnapshotPayloadToDB(payload *model.Sna
if err != nil {
return err
}
monitoring.SetLastBlock(ss.chainType, highestBlock)
return nil
}