Skip to content

Commit

Permalink
Fix bugs, default interface, fix PrepareColumns, delete errors, fix h…
Browse files Browse the repository at this point in the history
…istory function
  • Loading branch information
powerpook committed Jul 11, 2020
1 parent fba6e33 commit d441107
Show file tree
Hide file tree
Showing 15 changed files with 175 additions and 79 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: go
go_import_path: github.com/IBAX/go-ibax

install: true
env: GO111MODULE=on

script: go build github.com/IBAX/go-ibax
go:
- 1.12.x
54 changes: 54 additions & 0 deletions cmd/rollback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) IBAX. All rights reserved.
* See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/

package cmd

import (
"github.com/IBAX-io/go-ibax/packages/conf"
"github.com/IBAX-io/go-ibax/packages/conf/syspar"
"github.com/IBAX-io/go-ibax/packages/model"
"github.com/IBAX-io/go-ibax/packages/rollback"
"github.com/IBAX-io/go-ibax/packages/smart"
"github.com/IBAX-io/go-ibax/packages/utils"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var blockID int64

// rollbackCmd represents the rollback command
return
}
if err := syspar.SysUpdate(nil); err != nil {
log.WithError(err).Error("can't read system parameters")
}
if err := syspar.SysTableColType(nil); err != nil {
log.WithFields(log.Fields{"error": err}).Error("updating sys table col type")
}

smart.InitVM()
if err := smart.LoadContracts(); err != nil {
log.WithError(err).Fatal("loading contracts")
return
}
err := rollback.ToBlockID(blockID, nil, log.WithFields(log.Fields{}))
if err != nil {
log.WithError(err).Fatal("rollback to block id")
return
}

// block id = 1, is a special case for full rollback
if blockID != 1 {
log.Info("Not full rollback, finishing work without checking")
return
}
},
}

func init() {
rollbackCmd.Flags().Int64Var(&blockID, "blockId", 1, "blockID to rollback")
rollbackCmd.MarkFlagRequired("blockId")
}
13 changes: 5 additions & 8 deletions packages/crypto/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@ import (
func CutPub(pubKey []byte) []byte {
if len(pubKey) == 65 && pubKey[0] == 4 {
pubKey = pubKey[1:]
func KeyToAddress(pubKey []byte) string {
return converter.AddressToString(Address(pubKey))
}
return pubKey
}

// GetWalletIDByPublicKey converts public key to wallet id
func GetWalletIDByPublicKey(publicKey []byte) (int64, error) {
key, _ := HexToPub(string(publicKey))
return int64(Address(key)), nil
// KeyToAddress converts a public key to chain address XXXX-...-XXXX.
func KeyToAddress(pubKey []byte) string {
return converter.AddressToString(Address(pubKey))
}

// HexToPub encodes hex string to []byte of pub key
func HexToPub(pub string) ([]byte, error) {
key, err := hex.DecodeString(pub)
if err != nil {
return nil, err
Expand Down
18 changes: 18 additions & 0 deletions packages/migration/contracts/first_ecosystem/NewApplication.sim
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// +prop AppID = '1'
// +prop Conditions = 'ContractConditions("MainCondition")'
contract NewApplication {
data {

if Size($Name) == 0 {
warning "Application name missing"
}

if DBFind("applications").Columns("id").Where({name:$Name}).One("id") {
warning Sprintf( "Application %s already exists", $Name)
}
}

action {
$result = DBInsert("applications", {name: $Name,conditions: $Conditions})
}
}
26 changes: 16 additions & 10 deletions packages/model/batches.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ type AfterTxs struct {
Rts []*RollbackTx
Lts []*LogTransaction
UpdTxStatus []*updateBlockMsg
}

func AfterPlayTxs(dbTx *DbTransaction, blockID int64, playTx AfterTxs, logger *log.Entry) error {
return GetDB(dbTx).Transaction(func(tx *gorm.DB) error {
if err := DeleteTransactions(tx, playTx.UsedTx); err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("batches delete used transactions")
return err
}
if err := CreateLogTransactionBatches(tx, playTx.Lts); err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("batches insert log_transactions")
return err
}
if err := CreateBatchesRollbackTx(tx, playTx.Rts); err != nil {
Expand Down Expand Up @@ -79,6 +69,22 @@ func (l txser) BatchFindByHash(tr *DbTransaction, hs ArrHashes) error {
if tx.RowsAffected > 0 {
return errors.New("duplicated transaction at transactions")
}
return nil
}); result.Error != nil {
return result.Error
}
return nil
}

func (l queueser) BatchFindByHash(tr *DbTransaction, hs ArrHashes) error {
if result := GetDB(tr).Model(&QueueTx{}).Select("hash").Where("hash IN ?", hs).FindInBatches(&l, len(hs), func(tx *gorm.DB, batch int) error {
if tx.RowsAffected > 0 {
return errors.New("duplicated transaction at queue_tx")
}
return nil
}); result.Error != nil {
return result.Error
}
return nil
}

Expand Down
7 changes: 7 additions & 0 deletions packages/model/mint_pool_transfer_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,11 @@ func (m MintPoolTransferHistory) TableName() string {

// Get is retrieving model from database
func (m *MintPoolTransferHistory) Get(devid int64) (bool, error) {
return isFound(DBConn.Where("devid = ?", devid).First(m))
}

// Get is retrieving model from database
func (m *MintPoolTransferHistory) GetPool(keyid int64) (bool, error) {
return isFound(DBConn.Where("keyid = ? and status = ?", keyid, 1).Last(m))
}

15 changes: 9 additions & 6 deletions packages/model/querycost/formula_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,22 @@ func (s *QueryCostByFormulaTestSuite) TestGetTableNameFromSelect() {
func (s *QueryCostByFormulaTestSuite) TestGetTableNameFromInsertNoInto() {
_, err := InsertQueryType(`insert "1_keys"(id) values (1)`).GetTableName()
assert.Error(s.T(), err)
assert.Equal(s.T(), err, IntoStatementMissingError)
}

func (s *QueryCostByFormulaTestSuite) TestGetTableNameFromInsert() {
tableName, err := InsertQueryType("insert into keys(a,b,c) values (1,2,3)").GetTableName()
assert.Nil(s.T(), err)
assert.Equal(s.T(), tableName, "keys")
tableName, err = InsertQueryType(`insert into "1_keys" (a,b,c) values (1,2,3)`).GetTableName()
assert.Nil(s.T(), err)
assert.Equal(s.T(), tableName, "1_keys")
}

func (s *QueryCostByFormulaTestSuite) TestGetTableNameFromUpdateNoSet() {
_, err := UpdateQueryType(`update keys a = b where id = 1`).GetTableName()
assert.Error(s.T(), err)
assert.Equal(s.T(), err, SetStatementMissingError)
}

func (s *QueryCostByFormulaTestSuite) TestGetTableNameFromUpdate() {
tableName, err := UpdateQueryType("update keys set a = 1 where id = 2").GetTableName()
assert.Nil(s.T(), err)
assert.Equal(s.T(), tableName, "keys")
tableName, err = UpdateQueryType(`update "1_keys" set a = 1`).GetTableName()
assert.Nil(s.T(), err)
assert.Equal(s.T(), tableName, "1_keys")
Expand Down
12 changes: 6 additions & 6 deletions packages/model/state_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ func (sp *StateParameter) TableName() string {
// SetTablePrefix is setting table prefix
func (sp *StateParameter) SetTablePrefix(prefix string) *StateParameter {
sp.ecosystem = converter.StrToInt64(prefix)
return sp
}

// Get is retrieving model from database
func (sp *StateParameter) Get(transaction *DbTransaction, name string) (bool, error) {
return isFound(GetDB(transaction).Where("ecosystem = ? and name = ?", sp.ecosystem, name).First(sp))
func (sp *StateParameter) GetAllStateParameters() ([]StateParameter, error) {
parameters := make([]StateParameter, 0)
err := DBConn.Table(sp.TableName()).Where(`ecosystem = ?`, sp.ecosystem).Find(&parameters).Error
if err != nil {
return nil, err
}
return parameters, nil
}
14 changes: 7 additions & 7 deletions packages/model/subnode_agent_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ type SubNodeAgentData struct {
Hash string `gorm:"not null" json:"hash"`
Data []byte `gorm:"not null" json:"data"`
DataInfo string `gorm:"type:jsonb" json:"data_info"`
TranMode int64 `gorm:"not null" json:"tran_mode"`
DataSendState int64 `gorm:"not null" json:"data_send_state"`
DataSendErr string `gorm:"not null" json:"data_send_err"`
UpdateTime int64 `gorm:"not null" json:"update_time"`
CreateTime int64 `gorm:"not null" json:"create_time"`
}

//SubNodeSrcPubkey string `gorm:"not null" json:"subnode_src_pubkey"`
SubNodeSrcPubkey string `gorm:"column:subnode_src_pubkey;not null" json:"subnode_src_pubkey"`
//SubNodeDestPubkey string `gorm:"not null" json:"subnode_dest_pubkey"`
SubNodeDestPubkey string `gorm:"column:subnode_dest_pubkey;not null" json:"subnode_dest_pubkey"`
//SubNodeDestIP string `gorm:"not null" json:"subnode_dest_ip"`
SubNodeDestIP string `gorm:"column:subnode_dest_ip;not null" json:"subnode_dest_ip"`
//SubNodeAgentPubkey string `gorm:"not null" json:"subnode_agent_pubkey"`
func (SubNodeAgentData) TableName() string {
return "subnode_agent_data"
}
Expand Down
13 changes: 7 additions & 6 deletions packages/model/subnode_src_data_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ func (m *SubNodeSrcDataStatus) Create() error {
func (m *SubNodeSrcDataStatus) Updates() error {
return DBConn.Model(m).Updates(m).Error
}

func (m *SubNodeSrcDataStatus) Delete() error {
return DBConn.Delete(m).Error
}

func (m *SubNodeSrcDataStatus) GetAll() ([]SubNodeSrcDataStatus, error) {
var result []SubNodeSrcDataStatus
err := DBConn.Find(&result).Error
return result, err
Expand All @@ -62,6 +56,13 @@ func (m *SubNodeSrcDataStatus) GetAllByTaskUUID(TaskUUID string) ([]SubNodeSrcDa
return result, err
}

func (m *SubNodeSrcDataStatus) GetAllByDataSendStatus(DataSendStatus int64) ([]SubNodeSrcDataStatus, error) {
result := make([]SubNodeSrcDataStatus, 0)
err := DBConn.Table("subnode_src_data_status").Where("data_send_state = ?", DataSendStatus).Find(&result).Error
return result, err
}

func (m *SubNodeSrcDataStatus) GetAllByDataSendStatusAndAgentMode(DataSendStatus int64, AgentMode int64) ([]SubNodeSrcDataStatus, error) {
result := make([]SubNodeSrcDataStatus, 0)
err := DBConn.Table("subnode_src_data_status").Where("data_send_state = ? AND agent_mode = ?", DataSendStatus, AgentMode).Find(&result).Error
return result, err
Expand Down
9 changes: 2 additions & 7 deletions packages/model/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ func CreateTable(transaction *DbTransaction, tableName, colsSQL string) error {
// CreateView is creating view table
func CreateView(transaction *DbTransaction, inViewName, inTables, inWhere, inColSQL string) error {
inSQL := `CREATE VIEW "` + inViewName + `" AS SELECT ` + inColSQL + ` FROM ` + inTables + ` WHERE ` + inWhere + `;`
return GetDB(transaction).Exec(inSQL).Error
}

// DropView is drop view table
func DropView(transaction *DbTransaction, inViewName string) error {
Expand Down Expand Up @@ -195,13 +197,6 @@ func GetTableListQuery(table string, ecosystemID int64) *gorm.DB {

//
func SubNodeGetTableQuery(table string, ecosystemID int64) *gorm.DB {
//if converter.FirstEcosystemTables[table] {
// return DBConn.Table("1_"+table).Where("ecosystem = ?", ecosystemID)
//}
return DBConn.Table(converter.SubNodeParseTable(table, ecosystemID))
}

//
func VDEGetTableQuery(table string, ecosystemID int64) *gorm.DB {
//if converter.FirstEcosystemTables[table] {
// return DBConn.Table("1_"+table).Where("ecosystem = ?", ecosystemID)
Expand Down
26 changes: 15 additions & 11 deletions packages/network/tcpserver/type2.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@ import (
"github.com/IBAX-io/go-ibax/packages/crypto"
"github.com/IBAX-io/go-ibax/packages/model"
"github.com/IBAX-io/go-ibax/packages/network"
"github.com/IBAX-io/go-ibax/packages/transaction"
"github.com/IBAX-io/go-ibax/packages/utils"

log "github.com/sirupsen/logrus"
)

// Type2 serves requests from disseminator
func Type2(rw io.ReadWriter) error {
r := &network.DisRequest{}
if err := r.Read(rw); err != nil {
return err
}

txs, err := UnmarshalTxPacket(r.Data)
if err != nil {
return err
}
var rtxs []*model.RawTx
Expand Down Expand Up @@ -47,17 +62,6 @@ import (
}

//// Type2 serves requests from disseminator
//func Type2(rw io.ReadWriter) (*network.DisTrResponse, error) {
// r := &network.DisRequest{}
// if err := r.Read(rw); err != nil {
// return nil, err
// }
//
// binaryData := r.Data
// // take the transactions from usual users but not nodes.
// _, _, decryptedBinData, err := DecryptData(&binaryData)
// if err != nil {
// return nil, utils.ErrInfo(err)
// }
//
// if int64(len(binaryData)) > syspar.GetMaxTxSize() {
Expand Down
19 changes: 15 additions & 4 deletions packages/notificator/notificator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ func TestParseRecipientNotifications(t *testing.T) {
"role_id": "3",
"cnt": "4",
},
map[string]string{
"recipient_id": "2",
"role_id": "4",
"cnt": "3",
},
},
Want: map[int64]*[]notificationRecord{
1: &[]notificationRecord{
notificationRecord{
EcosystemID: 1,
RoleID: 1,
RecordsCount: 2,
},
notificationRecord{
EcosystemID: 1,
RoleID: 2,
RecordsCount: 1,
},
Expand Down Expand Up @@ -89,10 +104,6 @@ func containsNotificationRecord(slice []notificationRecord, rec notificationReco
return true
}
}

return false
}

func TestOutputFormat(t *testing.T) {
records := []notificationRecord{
notificationRecord{
Expand Down
14 changes: 5 additions & 9 deletions packages/smart/smart_p_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@ package smart

import (
"testing"
)

func TestRegexpMatch(t *testing.T) {
type args struct {
str string
reg string
}
tests := []struct {
name string
args args
want bool
}{
// TODO: Add test cases.
//{"email", args{"3@1.com", `^(?i)[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`}, true},
{"num alpha han", args{"one", `^[A-Za-z0-9\u4e00-\u9fa5]{2,4}$`}, true},
//{"url", args{"http://www.google.com", `(https?)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]`}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := RegexpMatch(tt.args.str, tt.args.reg); got != tt.want {
t.Errorf("RegexpMatch() = %v, want %v", got, tt.want)
Expand Down
8 changes: 6 additions & 2 deletions packages/types/mode_interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ type DaemonListFactory interface {
GetDaemonsList() []string
}

type EcosystemLookupGetter interface {
GetEcosystemLookup() ([]int64, []string, error)
}

type EcosystemNameGetter interface {
GetEcosystemName(id int64) (string, error)
type EcosystemIDValidator interface {
Validate(id, clientID int64, le *log.Entry) (int64, error)
}

// DaemonLoader allow implement different ways for loading daemons

0 comments on commit d441107

Please sign in to comment.