Skip to content
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
4 changes: 2 additions & 2 deletions cmd/cql-minerd/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ func benchDB(b *testing.B, db *sql.DB, createDB bool) {
})

routineCount := runtime.NumGoroutine()
if routineCount > 150 {
if routineCount > 500 {
b.Errorf("go routine count: %d", routineCount)
} else {
log.Infof("go routine count: %d", routineCount)
Expand Down Expand Up @@ -788,7 +788,7 @@ func benchDB(b *testing.B, db *sql.DB, createDB bool) {
})

routineCount = runtime.NumGoroutine()
if routineCount > 150 {
if routineCount > 500 {
b.Errorf("go routine count: %d", routineCount)
} else {
log.Infof("go routine count: %d", routineCount)
Expand Down
230 changes: 181 additions & 49 deletions cmd/cql/internal/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,32 @@ import (

"github.com/CovenantSQL/CovenantSQL/client"
"github.com/CovenantSQL/CovenantSQL/conf"
"github.com/CovenantSQL/CovenantSQL/crypto"
"github.com/CovenantSQL/CovenantSQL/crypto/asymmetric"
"github.com/CovenantSQL/CovenantSQL/crypto/kms"
"github.com/CovenantSQL/CovenantSQL/proto"
"github.com/CovenantSQL/CovenantSQL/route"
"github.com/CovenantSQL/CovenantSQL/rpc/mux"
"github.com/CovenantSQL/CovenantSQL/types"
)

var (
tokenName string // get specific token's balance of current account
tokenName string // get specific token's balance of current account
databaseID string
)

// CmdWallet is cql wallet command entity.
var CmdWallet = &Command{
UsageLine: "cql wallet [common params] [-token type]",
UsageLine: "cql wallet [common params] [-token type] [-dsn dsn]",
Short: "get the wallet address and the balance of current account",
Long: `
Wallet gets the CovenantSQL wallet address and the token balances of the current account.
e.g.
cql wallet

cql wallet -token Particle

cql wallet -dsn "covenantsql://4119ef997dedc585bfbcfae00ab6b87b8486fab323a8e107ea1fd4fc4f7eba5c"
`,
}

Expand All @@ -47,64 +56,187 @@ func init() {

addCommonFlags(CmdWallet)
addConfigFlag(CmdWallet)

CmdWallet.Flag.StringVar(&tokenName, "token", "", "Get specific token's balance of current account, e.g. Particle, Wave, All")
CmdWallet.Flag.StringVar(&databaseID, "dsn", "", "Show specified database deposit")
}

func runWallet(cmd *Command, args []string) {
commonFlagsInit(cmd)
configInit()
func showTokenBalance(tokenName string) {
var (
tokenBalance uint64
err error
)

fmt.Printf("\n\nwallet address: %s\n", conf.GConf.WalletAddress)
tokenType := types.FromString(tokenName)

var err error
if strings.ToLower(tokenName) == "" {
var stableCoinBalance, covenantCoinBalance uint64

if stableCoinBalance, err = client.GetTokenBalance(types.Particle); err != nil {
if strings.Contains(err.Error(), "no such token balance") {
fmt.Println("Your account is not created in the TestNet, please apply tokens from our faucet first.")
SetExitStatus(1)
} else {
ConsoleLog.WithError(err).Error("get Particle balance failed")
SetExitStatus(1)
}
if !tokenType.Listed() {
values := make([]string, len(types.TokenList))
for i := types.Particle; i < types.SupportTokenNumber; i++ {
values[i] = types.TokenList[i]
}
ExitIfErrors()
if covenantCoinBalance, err = client.GetTokenBalance(types.Wave); err != nil {
if strings.Contains(err.Error(), "no such token balance") {
fmt.Println("Your account is not created in the TestNet, please apply tokens from our faucet first.")
SetExitStatus(1)
} else {
ConsoleLog.WithError(err).Error("get Wave balance failed")
SetExitStatus(1)
}
ConsoleLog.Errorf("no such token supporting in CovenantSQL (what we support: %s)",
strings.Join(values, ", "))
SetExitStatus(1)
return
}
if tokenBalance, err = client.GetTokenBalance(tokenType); err != nil {
if strings.Contains(err.Error(), "no such token balance") {
fmt.Println("Your account is not created in the TestNet, please apply tokens from our faucet first.")
} else {
ConsoleLog.WithError(err).Error("get token balance failed")
SetExitStatus(1)
return
}
ExitIfErrors()
}

fmt.Printf("Particle balance is: %d\n", stableCoinBalance)
fmt.Printf("Wave balance is: %d\n", covenantCoinBalance)
} else {
var tokenBalance uint64
tokenType := types.FromString(tokenName)
if !tokenType.Listed() {
values := make([]string, len(types.TokenList))
for i := types.Particle; i < types.SupportTokenNumber; i++ {
values[i] = types.TokenList[i]
}
ConsoleLog.Errorf("no such token supporting in CovenantSQL (what we support: %s)",
strings.Join(values, ", "))
SetExitStatus(1)
fmt.Printf("%s balance is: %d\n", tokenType, tokenBalance)
}

func showAllTokenBalance() {
var (
stableCoinBalance uint64
covenantCoinBalance uint64
err error
)

if stableCoinBalance, err = client.GetTokenBalance(types.Particle); err != nil {
if strings.Contains(err.Error(), "no such token balance") {
fmt.Println("Your account is not created in the TestNet, please apply tokens from our faucet first.")
} else {
ConsoleLog.WithError(err).Error("get Particle balance failed")
}

SetExitStatus(1)
return
}

if covenantCoinBalance, err = client.GetTokenBalance(types.Wave); err != nil {
if strings.Contains(err.Error(), "no such token balance") {
fmt.Println("Your account is not created in the TestNet, please apply tokens from our faucet first.")
} else {
ConsoleLog.WithError(err).Error("get Wave balance failed")
}

SetExitStatus(1)
return
}

fmt.Printf("Particle balance is: %d\n", stableCoinBalance)
fmt.Printf("Wave balance is: %d\n", covenantCoinBalance)
}

func showDatabaseDeposit(dsn string) {
dsnCfg, err := client.ParseDSN(dsn)
if err != nil {
ConsoleLog.WithError(err).Error("parse database dsn failed")
SetExitStatus(1)
return
}

var (
req = new(types.QuerySQLChainProfileReq)
resp = new(types.QuerySQLChainProfileResp)
pubKey *asymmetric.PublicKey
addr proto.AccountAddress
)

req.DBID = proto.DatabaseID(dsnCfg.DatabaseID)

if err = mux.RequestBP(route.MCCQuerySQLChainProfile.String(), req, resp); err != nil {
ConsoleLog.WithError(err).Error("query database chain profile failed")
SetExitStatus(1)
return
}

if pubKey, err = kms.GetLocalPublicKey(); err != nil {
ConsoleLog.WithError(err).Error("query database chain profile failed")
SetExitStatus(1)
return
}

if addr, err = crypto.PubKeyHash(pubKey); err != nil {
ConsoleLog.WithError(err).Error("query database chain profile failed")
SetExitStatus(1)
return
}

for _, user := range resp.Profile.Users {
if user.Address == addr && user.Permission != nil && user.Permission.Role != types.Void {
fmt.Printf("Chain token type: %s\n", resp.Profile.TokenType.String())
fmt.Printf("Depsoit: %d\n", user.Deposit)
fmt.Printf("Arrears: %d\n", user.Arrears)
fmt.Printf("AdvancePayment: %d\n", user.AdvancePayment)
return
}
}

ConsoleLog.Error("no permission to the database")
SetExitStatus(1)
return
}

func showAllDatabaseDeposit() {
var (
req = new(types.QueryAccountSQLChainProfilesReq)
resp = new(types.QueryAccountSQLChainProfilesResp)
pubKey *asymmetric.PublicKey
err error
)

if pubKey, err = kms.GetLocalPublicKey(); err != nil {
ConsoleLog.WithError(err).Error("query database chain profile failed")
SetExitStatus(1)
return
}

if req.Addr, err = crypto.PubKeyHash(pubKey); err != nil {
ConsoleLog.WithError(err).Error("query database chain profile failed")
SetExitStatus(1)
return
}

if err = mux.RequestBP(route.MCCQueryAccountSQLChainProfiles.String(), req, resp); err != nil {
if strings.Contains(err.Error(), "can't find method") {
// old version block producer
ConsoleLog.WithError(err).Warning("query account database profiles is not supported in old version block producer")
return
}
if tokenBalance, err = client.GetTokenBalance(tokenType); err != nil {
if strings.Contains(err.Error(), "no such token balance") {
fmt.Println("Your account is not created in the TestNet, please apply tokens from our faucet first.")
} else {
ConsoleLog.WithError(err).Error("get token balance failed")
SetExitStatus(1)
return

ConsoleLog.WithError(err).Error("query account database profiles failed")
SetExitStatus(1)
return
}

if len(resp.Profiles) == 0 {
fmt.Println("found no related database")
return
}

fmt.Printf("Database Deposits:\n\n")
fmt.Printf("%-64s\tDeposit\tArrears\tAdvancePayment\n", "DatabaseID")

for _, p := range resp.Profiles {
for _, user := range p.Users {
if user.Address == req.Addr && user.Permission != nil && user.Permission.Role != types.Void {
fmt.Printf("%s\t%d\t%d\t%d\n",
p.ID, user.Deposit, user.Arrears, user.AdvancePayment)
}
}
fmt.Printf("%s balance is: %d\n", tokenType, tokenBalance)
}
}

func runWallet(cmd *Command, args []string) {
commonFlagsInit(cmd)
configInit()

fmt.Printf("\n\nwallet address: %s\n", conf.GConf.WalletAddress)

if databaseID != "" {
showDatabaseDeposit(databaseID)
} else if tokenName == "" {
showAllTokenBalance()
showAllDatabaseDeposit()
} else {
showTokenBalance(tokenName)
}
}
4 changes: 3 additions & 1 deletion test/testnet_client/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ ${BIN}/cql transfer -config ${PROJECT_DIR}/conf/testnet/config.yaml -wait-tx-con

${BIN}/cql wallet

${BIN}/cql create -wait-tx-confirm -node 2
# create database only in miner00 and miner01
${BIN}/cql create -wait-tx-confirm -node 2 \
-target-miners 'ba0ba731c7a76ccef2c1170f42038f7e228dfb474ef0190dfe35d9a37911ed37,1a7b0959bbd0d0ec529278a61c0056c277bffe75b2646e1699b46b10a90210be'

#get dsn
dsn=$(cat ~/.cql/.dsn | tail -n1)
Expand Down
2 changes: 1 addition & 1 deletion types/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (t TokenType) String() string {
func FromString(t string) TokenType {
var i TokenType
for ; i < SupportTokenNumber; i++ {
if strings.ToLower(TokenList[i]) == strings.ToLower(t) {
if strings.EqualFold(TokenList[i], t) {
return i
}
}
Expand Down