Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
powerpook committed Jul 18, 2020
1 parent 0f8f6a4 commit ac76098
Show file tree
Hide file tree
Showing 10 changed files with 231 additions and 40 deletions.
186 changes: 186 additions & 0 deletions packages/api/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
package api

import (
"fmt"
"github.com/IBAX-io/go-ibax/packages/consts"
"github.com/IBAX-io/go-ibax/packages/model"
log "github.com/sirupsen/logrus"
"net/http"
)

type tableInfoForm struct {
listForm

Order string `schema:"order"`
InWhere string `schema:"where"`
}
type columnsInfo struct {
listForm
Order string `schema:"order"`
InWhere string `schema:"where"`
Name string `schema:"name"`
}
type rowsInfo struct {
columnsInfo
}

func (f *tableInfoForm) Validate(r *http.Request) error {
if err := f.listForm.Validate(r); err != nil {
return err
}
return nil
}
func (f *columnsInfo) Validate(r *http.Request) error {
if err := f.listForm.Validate(r); err != nil {
return err
}
return nil
}
func (f *rowsInfo) Validate(r *http.Request) error {
if err := f.listForm.Validate(r); err != nil {
return err
}
return nil
}

func getOpenDatabaseInfoHandler(w http.ResponseWriter, r *http.Request) {
result := &listResult{}
logger := getLogger(r)
sqlQuery := "SELECT current_user,CURRENT_CATALOG,VERSION (),pg_size_pretty(pg_database_size (CURRENT_CATALOG)),pg_postmaster_start_time() FROM pg_user LIMIT 1"
rows, err := model.GetDB(nil).Raw(sqlQuery).Rows()
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("GetDatabaseInfo rows failed")
errorResponse(w, err, http.StatusBadRequest)
return
}
result.List, err = model.GetResult(rows)
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("GetDatabaseInfo result failed")
errorResponse(w, err, http.StatusBadRequest)
return
}
result.Count = 1
jsonResponse(w, result)
}

func getOpenTablesInfoHandler(w http.ResponseWriter, r *http.Request) {
form := &tableInfoForm{}
result := &listResult{}
if err := parseForm(r, form); err != nil {
errorResponse(w, err, http.StatusBadRequest)
return
}
logger := getLogger(r)
if form.Limit < 1 || form.Offset < 0 {
err := fmt.Errorf("limit less than 1 recv:%d or offset is negative recv:%d", form.Limit, form.Offset)
errorResponse(w, err, http.StatusBadRequest)
return
}
order := "tablename asc"
q := model.GetDB(nil)
if err := q.Raw("SELECT count(*) FROM pg_tables WHERE schemaname='public'").Take(&result.Count).Error; err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("getOpenTables row from table")
errorResponse(w, err, http.StatusBadRequest)
return
}
query := fmt.Sprintf("select tablename from pg_tables where schemaname ='public' order by %s offset %d limit %d", order, form.Offset, form.Limit)
rows, err := q.Raw(query).Rows()
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err, "query": query}).Error("getOpenTables rows from tablesInfo")
errorResponse(w, err, http.StatusBadRequest)
return
}
result.List, err = model.GetResult(rows)
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("getOpenTables getResult")
errorResponse(w, err, http.StatusBadRequest)
return
}

jsonResponse(w, result)
}

func getOpenColumnsInfoHandler(w http.ResponseWriter, r *http.Request) {
form := &columnsInfo{}
result := &listResult{}
if err := parseForm(r, form); err != nil {
errorResponse(w, err, http.StatusBadRequest)
return
}
logger := getLogger(r)
//client:=getClient(r)
order := "ordinal_position ASC"
if len(form.Name) == 0 {
err := fmt.Errorf("tableName is null")
errorResponse(w, err, http.StatusBadRequest)
return
}

sqlQuery := fmt.Sprintf("SELECT column_name,data_type,column_default FROM information_schema.columns WHERE table_name='%s' ORDER BY %s", form.Name, order)
rows, err := model.GetDB(nil).Raw(sqlQuery).Rows()
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err, "query": sqlQuery}).Error("get colums info failed")
errorResponse(w, err, http.StatusBadRequest)
return
}
result.List, err = model.GetResult(rows)
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("get open Cloumns result info failed")
errorResponse(w, err, http.StatusBadRequest)
return
}
result.Count = int64(len(result.List))
jsonResponse(w, result)
}
result, err := GetRowsInfo(form.Name, form.Order, form.Offset, form.Limit, form.InWhere)
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("get rows info failed")
errorResponse(w, err, http.StatusBadRequest)
return
}
jsonResponse(w, result)
}
func GetRowsInfo(tableName, order string, offset, limit int, where string) (*listResult, error) {
result := &listResult{}
num, err := model.GetNodeRows(tableName)
if err != nil {
return result, err
}
var primaryOrder = make(map[string]string)
primaryOrder["confirmations"] = "block_id desc"
primaryOrder["info_block"] = "hash asc"
primaryOrder["install"] = "progress asc"
primaryOrder["log_transactions"] = "hash asc"
primaryOrder["queue_blocks"] = "hash asc"
primaryOrder["queue_tx"] = "hash asc"
primaryOrder["stop_daemons"] = "stop_time asc"
primaryOrder["transactions"] = "hash asc"
primaryOrder["transactions_attempts"] = "hash asc"
primaryOrder["transactions_status"] = "hash asc"
execOrder := order
if v, ok := primaryOrder[tableName]; ok {
execOrder = v
}
if execOrder == "" {
err = fmt.Errorf("order is null")
return nil, err
}

result.Count = num
var sqlQuest string
if where == "" {
sqlQuest = fmt.Sprintf(`select * from "%s" order by %s offset %d limit %d`, tableName, execOrder, offset, limit)
} else {
sqlQuest = fmt.Sprintf(`select * from "%s" where %s order by %s offset %d limit %d`, tableName, where, execOrder, offset, limit)
}
rows, err := model.GetDB(nil).Raw(sqlQuest).Rows()
if err != nil {
return result, fmt.Errorf("getRows raw err:%s in query %s", err, sqlQuest)
}

result.List, err = model.GetRowsInfo(rows, sqlQuest)
if err != nil {
return nil, err
}
return result, nil
}
17 changes: 8 additions & 9 deletions packages/crypto/ecies/ecccrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,6 @@ func EccDeCrypt(cryptText []byte,prv2 *ecies.PrivateKey) ([]byte, error) {
//
func EccPubEncrypt(plainText []byte, pub *ecdsa.PublicKey) (cryptText []byte, err error) { //

defer func() {
if err := recover(); err != nil {
switch err.(type) {
case runtime.Error:
log.Println("runtime err:", err, "check key ")
default:
log.Println("error:", err)
}
}
}()

publicKey := ImportECDSAPublic(pub)
Expand All @@ -58,6 +49,14 @@ func EccPriDeCrypt(cryptText []byte, priv *ecdsa.PrivateKey) (msg []byte, err er
privateKey := ImportECDSA(priv)

//
plainText, err := privateKey.Decrypt(cryptText, nil, nil)

return plainText, err
}

func EccCryptoKey(plainText []byte, publickey string) (cryptoText []byte, err error) {
pubbuff, err := crypto.HexToPub(publickey)
if err != nil {
return nil, err
}
pub, err := crypto.GetPublicKeys(pubbuff)
Expand Down
10 changes: 0 additions & 10 deletions packages/migration/menu_data.go
Original file line number Diff line number Diff line change
@@ -1,10 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) IBAX. All rights reserved.
* See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/

package migration

var menuDataSQL = `INSERT INTO "1_menu" (id, name, value, conditions, ecosystem) VALUES
(next_id('1_menu'), 'admin_menu', '', 'ContractAccess("@1EditMenu")','{{.Ecosystem}}'),
(next_id('1_menu'), 'developer_menu', 'MenuItem(Title:"Import", Page:@1import_upload, Icon:"icon-cloud-upload")', 'ContractAccess("@1EditMenu")','{{.Ecosystem}}');
3 changes: 0 additions & 3 deletions packages/migration/pages_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,3 @@
* See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/

package migration
(next_id('1_pages'), 'admin_index', '', 'admin_menu', 'ContractConditions("@1DeveloperCondition")', '{{.AppID}}', '{{.Ecosystem}}'),
(next_id('1_pages'), 'developer_index', '', 'developer_menu', 'ContractConditions("@1DeveloperCondition")', '{{.AppID}}', '{{.Ecosystem}}');`
20 changes: 15 additions & 5 deletions packages/model/mine_owner.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,22 @@ func (m MineOwner) TableName() string {
}

// Get is retrieving model from database
func (m *MineOwner) GetPoolManage(keyid int64) (bool, error) {
func (m *MineOwner) Get(devid int64) (bool, error) {
return isFound(DBConn.Where("devid = ? and type = ? and deleted = ? ", devid, 2, 0).First(m))
}

// Get is retrieving model from database
func (m *MineOwner) GetByTransaction(transaction *DbTransaction, devid int64) (bool, error) {
return isFound(GetDB(transaction).Where("devid = ? and type = ? and deleted = ? ", devid, 2, 0).First(m))
}

var k Key
d := k.SetTablePrefix(1)
f1, err1 := d.Get(nil, keyid)
if err1 != nil {
// Get is retrieving model from database
func (m *MineOwner) GetPool(keyid int64) (bool, error) {
return isFound(DBConn.Where("keyid = ? and type = ? ", keyid, 2).Order("id DESC").First(m))
}

// Get is retrieving model from database
func (m *MineOwner) GetPoolManage(keyid int64) (bool, error) {
return false, err1
}

Expand Down
13 changes: 10 additions & 3 deletions packages/model/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ import (
"gorm.io/gorm/clause"
"github.com/IBAX-io/go-ibax/packages/consts"
)

// This constants contains values of transactions priority
const (
TransactionRateOnBlock transactionRate = iota + 1
TransactionRateApiContract
TransactionRateSystemServer
TransactionRateEcosystemMiner
TransactionRateSystemMiner
TransactionRateStopNetwork
)
const expediteOrder = `high_rate,expedite DESC,time ASC`

type transactionRate int8
Expand Down Expand Up @@ -101,9 +111,6 @@ func MarkTransactionSent(transactionHash []byte) (int64, error) {
// MarkTransactionSentBatches is marking transaction as sent
func MarkTransactionSentBatches(hashArr [][]byte) error {
return DBConn.Exec("UPDATE transactions SET sent = 1 WHERE hash in(?)", hashArr).Error
}

// MarkTransactionUsed is marking transaction as used
func MarkTransactionUsed(transaction *DbTransaction, transactionHash []byte) (int64, error) {
query := GetDB(transaction).Exec("UPDATE transactions SET used = 1 WHERE hash = ?", transactionHash)
return query.RowsAffected, query.Error
Expand Down
1 change: 1 addition & 0 deletions packages/model/upd_full_nodes.go
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) IBAX. All rights reserved.
* See LICENSE in the project root for license information.
6 changes: 3 additions & 3 deletions packages/model/vde_agent_chain_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type VDEAgentChainInfo struct {

func (VDEAgentChainInfo) TableName() string {
return "vde_agent_chain_info"
}

func (m *VDEAgentChainInfo) Create() error {
return DBConn.Create(&m).Error
}

Expand All @@ -39,6 +42,3 @@ func (m *VDEAgentChainInfo) GetAll() ([]VDEAgentChainInfo, error) {
return result, err
}
func (m *VDEAgentChainInfo) GetOneByID() (*VDEAgentChainInfo, error) {
err := DBConn.Where("id=?", m.ID).First(&m).Error
return m, err
}
8 changes: 2 additions & 6 deletions packages/types/notifications.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) IBAX. All rights reserved.
* See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/
package types

type Notifications interface {
AddAccounts(ecosystem int64, accounts ...string)
AddRoles(ecosystem int64, roles ...int64)
Size() int
Send()
}
7 changes: 6 additions & 1 deletion packages/utils/tx/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@
* Copyright (c) IBAX. All rights reserved.
* See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/
}
package tx

// Header is contain header data
type Header struct {
ID int
Time int64

0 comments on commit ac76098

Please sign in to comment.