Skip to content

Commit

Permalink
Improved process tx and col (#2009)
Browse files Browse the repository at this point in the history
* mv price to platform, rm deposit col

* change check sign only err

* fixed cant del tx when err
  • Loading branch information
scottafk authored Jul 28, 2022
1 parent a1e3da7 commit 191c665
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 93 deletions.
36 changes: 11 additions & 25 deletions packages/block/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,42 +89,28 @@ func (b *Block) Check() error {
}

// hash compare could be failed in the case of fork
_, err = b.CheckHash()
err = b.CheckSign()
if err != nil {
transaction.CleanCache()
return err
}
return nil
}

// CheckHash is checking hash
func (b *Block) CheckHash() (bool, error) {
logger := b.GetLogger()
if b.IsGenesis() {
return true, nil
}
if conf.Config.IsSubNode() {
return true, nil
}
// check block signature
if b.PrevHeader == nil {
return true, nil
func (b *Block) CheckSign() error {
if b.IsGenesis() || conf.Config.IsSubNode() || b.PrevHeader == nil {
return nil
}
nodePublicKey, err := syspar.GetNodePublicKeyByPosition(b.Header.NodePosition)
nodePub, err := syspar.GetNodePublicKeyByPosition(b.Header.NodePosition)
if err != nil {
return false, utils.ErrInfo(err)
return fmt.Errorf("%v: %w", fmt.Sprintf("get node public key by position '%d'", b.Header.NodePosition), err)
}
if len(nodePublicKey) == 0 {
logger.WithFields(log.Fields{"type": consts.EmptyObject}).Error("node public key is empty")
return false, utils.ErrInfo(fmt.Errorf("empty nodePublicKey"))
if len(nodePub) == 0 {
return fmt.Errorf("empty nodePublicKey")
}

_, err = utils.CheckSign([][]byte{nodePublicKey}, []byte(b.ForSign()), b.Header.Sign, true)

_, err = utils.CheckSign([][]byte{nodePub}, []byte(b.ForSign()), b.Header.Sign, true)
if err != nil {
//logger.WithFields(log.Fields{"error": err, "type": consts.CryptoError}).Error("checking block header sign")
return false, errors.Wrap(err, "checking block header sign")
//return false, errors.Wrap(err, fmt.Sprintf("per_block_id: %d, per_block_hash: %x", b.PrevHeader.BlockId, b.PrevHeader.Hash))
return errors.Wrap(err, "checking block header sign")
}
return true, nil
return nil
}
78 changes: 35 additions & 43 deletions packages/block/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"encoding/hex"
"fmt"
"strconv"
"strings"
"sync"

"github.com/IBAX-io/go-ibax/packages/common/random"
Expand All @@ -19,7 +18,6 @@ import (
"github.com/IBAX-io/go-ibax/packages/consts"
"github.com/IBAX-io/go-ibax/packages/notificator"
"github.com/IBAX-io/go-ibax/packages/pbgo"
"github.com/IBAX-io/go-ibax/packages/script"
"github.com/IBAX-io/go-ibax/packages/service/node"
"github.com/IBAX-io/go-ibax/packages/storage/sqldb"
"github.com/IBAX-io/go-ibax/packages/transaction"
Expand All @@ -37,24 +35,9 @@ func (b *Block) PlaySafe() error {
return err
}

inputTx := b.Transactions[:]
err = b.ProcessTxs(dbTx)
if err != nil {
dbTx.Rollback()
if b.GenBlock && len(b.TxFullData) == 0 {
var banK = make(map[string]struct{}, len(inputTx))
for i := 0; i < len(inputTx); i++ {
var t, k = inputTx[i], strconv.FormatInt(inputTx[i].KeyID(), 10)
if _, ok := banK[k]; !ok && t.IsSmartContract() {
transaction.BadTxForBan(t.KeyID())
banK[k] = struct{}{}
continue
}
if err := transaction.MarkTransactionBad(t.Hash(), err.Error()); err != nil {
return err
}
}
}
return err
}

Expand All @@ -77,27 +60,42 @@ func (b *Block) PlaySafe() error {
return nil
}

type badTxStruct struct {
index int
hash []byte
msg string
keyID int64
}

func (b *Block) ProcessTxs(dbTx *sqldb.DbTransaction) (err error) {
afters := &types.AfterTxs{
Rts: make([]*types.RollbackTx, 0),
Txs: make([]*types.AfterTx, 0),
}
logger := b.GetLogger()
txsMap := b.ClassifyTxsMap
limits := transaction.NewLimits(b.limitMode())
rand := random.NewRand(b.Header.Timestamp)
processedTx := make([][]byte, 0, len(b.Transactions))
var genBErr error

processBadTx := func() chan badTxStruct {
ch := make(chan badTxStruct)
go func() {
for badTxItem := range ch {
transaction.BadTxForBan(badTxItem.keyID)
_ = transaction.MarkTransactionBad(badTxItem.hash, badTxItem.msg)
}
}()
return ch
}

txBadChan := processBadTx()
defer func() {
close(txBadChan)
if b.IsGenesis() || b.GenBlock {
b.AfterTxs = afters
}
if b.GenBlock {
b.TxFullData = processedTx
}
if genBErr != nil {
err = genBErr
}

if errA := b.AfterPlayTxs(dbTx); errA != nil {
if err == nil {
err = errA
Expand Down Expand Up @@ -133,11 +131,12 @@ func (b *Block) ProcessTxs(dbTx *sqldb.DbTransaction) (err error) {
// StopNetworkTxType
if len(txsMap[types.StopNetworkTxType]) > 0 {
transactions := txsMap[types.StopNetworkTxType]
err := b.serialExecuteTxs(dbTx, logger, rand, limits, afters, &processedTx, transactions, lock, genBErr)
err := b.serialExecuteTxs(dbTx, txBadChan, afters, &processedTx, transactions, lock)
delete(txsMap, types.StopNetworkTxType)
if err != nil {
return err
}
return nil
}

// FirstBlockTxType
Expand All @@ -146,14 +145,11 @@ func (b *Block) ProcessTxs(dbTx *sqldb.DbTransaction) (err error) {
for _, tx := range b.Transactions {
t, err := transaction.UnmarshallTransaction(bytes.NewBuffer(tx.FullData))
if err != nil {
if t != nil && t.Hash() != nil {
transaction.MarkTransactionBad(t.Hash(), err.Error())
}
return fmt.Errorf("parse transaction error(%s)", err)
return err
}
transactions = append(transactions, t)
}
err := b.serialExecuteTxs(dbTx, logger, rand, limits, afters, &processedTx, transactions, lock, genBErr)
err := b.serialExecuteTxs(dbTx, txBadChan, afters, &processedTx, transactions, lock)
transactions = make([]*transaction.Transaction, 0)
if err != nil {
return err
Expand All @@ -163,7 +159,7 @@ func (b *Block) ProcessTxs(dbTx *sqldb.DbTransaction) (err error) {
// DelayTxType
if len(txsMap[types.DelayTxType]) > 0 {
transactions := txsMap[types.DelayTxType]
err := b.serialExecuteTxs(dbTx, logger, rand, limits, afters, &processedTx, transactions, lock, genBErr)
err := b.serialExecuteTxs(dbTx, txBadChan, afters, &processedTx, transactions, lock)
delete(txsMap, types.DelayTxType)
if err != nil {
return err
Expand All @@ -180,7 +176,7 @@ func (b *Block) ProcessTxs(dbTx *sqldb.DbTransaction) (err error) {
wg.Add(1)
go func(_dbTx *sqldb.DbTransaction, _g string, _transactions []*transaction.Transaction, _afters *types.AfterTxs, _processedTx *[][]byte, _utxoTxsGroupMap map[string][]*transaction.Transaction, _lock *sync.RWMutex) {
defer wg.Done()
err := b.serialExecuteTxs(_dbTx, logger, rand, limits, _afters, _processedTx, _transactions, _lock, genBErr)
err := b.serialExecuteTxs(_dbTx, txBadChan, _afters, _processedTx, _transactions, _lock)
if err != nil {
return
}
Expand All @@ -206,7 +202,7 @@ func (b *Block) ProcessTxs(dbTx *sqldb.DbTransaction) (err error) {
wg.Add(1)
go func(_dbTx *sqldb.DbTransaction, _g string, _transactions []*transaction.Transaction, _afters *types.AfterTxs, _processedTx *[][]byte, _utxoTxsGroupMap map[string][]*transaction.Transaction, _lock *sync.RWMutex) {
defer wg.Done()
err := b.serialExecuteTxs(_dbTx, logger, rand, limits, _afters, _processedTx, _transactions, _lock, genBErr)
err := b.serialExecuteTxs(_dbTx, txBadChan, _afters, _processedTx, _transactions, _lock)
if err != nil {
return
}
Expand All @@ -223,10 +219,12 @@ func (b *Block) ProcessTxs(dbTx *sqldb.DbTransaction) (err error) {
return nil
}

func (b *Block) serialExecuteTxs(dbTx *sqldb.DbTransaction, logger *log.Entry, rand *random.Rand, limits *transaction.Limits, afters *types.AfterTxs, processedTx *[][]byte, txs []*transaction.Transaction, _lock *sync.RWMutex, genBErr error) error {
func (b *Block) serialExecuteTxs(dbTx *sqldb.DbTransaction, txBadChan chan badTxStruct, afters *types.AfterTxs, processedTx *[][]byte, txs []*transaction.Transaction, _lock *sync.RWMutex) error {
_lock.Lock()
defer _lock.Unlock()

limits := transaction.NewLimits(b.limitMode())
rand := random.NewRand(b.Header.Timestamp)
logger := b.GetLogger()
for curTx := 0; curTx < len(txs); curTx++ {
t := txs[curTx]
err := dbTx.Savepoint(consts.SetSavePointMarkBlock(hex.EncodeToString(t.Hash())))
Expand All @@ -252,26 +250,20 @@ func (b *Block) serialExecuteTxs(dbTx *sqldb.DbTransaction, logger *log.Entry, r
if b.GenBlock {
if errors.Cause(err) == transaction.ErrLimitStop {
if curTx == 0 {
txBadChan <- badTxStruct{index: curTx, hash: t.Hash(), msg: err.Error(), keyID: t.KeyID()}
return err
}
break
}
if strings.Contains(err.Error(), script.ErrVMTimeLimit.Error()) {
err = script.ErrVMTimeLimit
}
}
if t.IsSmartContract() {
transaction.BadTxForBan(t.KeyID())
}
_ = transaction.MarkTransactionBad(t.Hash(), err.Error())
txBadChan <- badTxStruct{index: curTx, hash: t.Hash(), msg: err.Error(), keyID: t.KeyID()}
if t.SysUpdate {
if err := syspar.SysUpdate(t.DbTransaction); err != nil {
return fmt.Errorf("updating syspar: %w", err)
}
t.SysUpdate = false
}
if b.GenBlock {
genBErr = err
continue
}
return err
Expand Down
4 changes: 1 addition & 3 deletions packages/block/serialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package block

import (
"bytes"

"github.com/IBAX-io/go-ibax/packages/storage/sqldb"
"github.com/IBAX-io/go-ibax/packages/utils"
"github.com/pkg/errors"
Expand Down Expand Up @@ -71,9 +72,6 @@ func UnmarshallBlock(blockBuffer *bytes.Buffer) (*Block, error) {
for i := 0; i < len(block.TxFullData); i++ {
tx, err := transaction.UnmarshallTransaction(bytes.NewBuffer(block.TxFullData[i]))
if err != nil {
if tx != nil && tx.Hash() != nil {
transaction.MarkTransactionBad(tx.Hash(), err.Error())
}
return nil, err
}
if tx.Type() == types.StopNetworkTxType {
Expand Down
9 changes: 9 additions & 0 deletions packages/conf/syspar/syspar.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const (

PriceExec = "price_exec_"
AccessExec = "access_exec_"
PriceCreateExec = "price_create_exec_"
PayFreeContract = "pay_free_contract"
)

Expand Down Expand Up @@ -639,6 +640,14 @@ func GetPriceExec(s string) (price int64, ok bool) {
return
}

func GetPriceCreateExec(s string) (price int64, ok bool) {
if ok = HasSys(PriceCreateExec + s); !ok {
return
}
price = SysInt64(PriceCreateExec + s)
return
}

// SysTableColType reloads/updates values of all ecosystem table column data type
func SysTableColType(dbTx *sqldb.DbTransaction) error {
var err error
Expand Down
1 change: 0 additions & 1 deletion packages/migration/first_ecosystem_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ var sqlFirstEcosystemCommon = `
t.Column("pub", "bytea", {"default": ""})
t.Column("amount", "decimal(30)", {"default_raw": "'0' CHECK (amount >= 0)"})
t.Column("maxpay", "decimal(30)", {"default_raw": "'0' CHECK (maxpay >= 0)"})
t.Column("deposit", "decimal(30)", {"default_raw": "'0' CHECK (deposit >= 0)"})
t.Column("multi", "bigint", {"default": "0"})
t.Column("deleted", "bigint", {"default": "0"})
t.Column("blocked", "bigint", {"default": "0"})
Expand Down
1 change: 1 addition & 0 deletions packages/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var migrations = []*migration{
var updateMigrations = []*migration{
{"0.0.3", updates.MigrationUpdatePriceExec, false},
{"0.0.4", updates.MigrationUpdateAccessExec, false},
{"0.0.5", updates.MigrationUpdatePriceCreateExec, false},
}

type migration struct {
Expand Down
14 changes: 0 additions & 14 deletions packages/migration/platform_parameters_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,6 @@ INSERT INTO "1_platform_parameters" ("id","name", "value", "conditions") VALUES
(next_id('1_platform_parameters'),'rollback_blocks', '60', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'honor_nodes', '', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'number_of_nodes', '101', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_ecosystem', '100', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_table', '1', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_column', '1', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_contract', '1', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_menu', '1', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_page', '1', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_snippet', '1', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_view', '1', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_application', '1', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_token', '5000', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_asset', '1000', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_lang', '1', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_section', '1', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'max_block_size', '67108864', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'max_tx_size', '33554432', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'max_tx_block', '5000', 'ContractAccess("@1UpdatePlatformParam")'),
Expand All @@ -43,7 +30,6 @@ INSERT INTO "1_platform_parameters" ("id","name", "value", "conditions") VALUES
(next_id('1_platform_parameters'),'node_ban_time','86400000','ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'node_ban_time_local','1800000','ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_tx_size', '15', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_rate', '1000000', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'test','false','false'),
(next_id('1_platform_parameters'),'price_tx_data', '10', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'private_blockchain', '1', 'false'),
Expand Down
1 change: 0 additions & 1 deletion packages/migration/tables_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ var tablesDataSQL = `INSERT INTO "1_tables" ("id", "name", "permissions","column
"pub": "ContractAccess(\"@1NewUser\")",
"amount": "ContractAccess(\"@1TokensTransfer\",\"@1NewToken\",\"@1TeBurn\",\"@1ProfileEdit\")",
"maxpay": "ContractConditions(\"@1MainCondition\")",
"deposit": "ContractAccess(\"@1TokensDecDeposit\",\"@1TokensIncDeposit\")",
"deleted": "ContractConditions(\"@1MainCondition\")",
"blocked": "ContractAccess(\"@1TokensLockoutMember\")",
"account": "false",
Expand Down
17 changes: 17 additions & 0 deletions packages/migration/updates/migration_update_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@
*--------------------------------------------------------------------------------------------*/
package updates

var MigrationUpdatePriceCreateExec = `
INSERT INTO "1_platform_parameters" (id, name, value, conditions) VALUES
(next_id('1_platform_parameters'),'price_create_rate', '1000000', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_exec_@1_new_ecosystem', '100', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_exec_@1_new_table', '1', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_exec_@1_new_column', '1', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_exec_@1_new_contract', '1', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_exec_@1_new_menu', '1', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_exec_@1_new_page', '1', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_exec_@1_new_snippet', '1', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_exec_@1_new_view', '1', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_exec_@1_new_application', '1', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_exec_@1_new_token', '5000', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_exec_@1_new_lang', '1', 'ContractAccess("@1UpdatePlatformParam")'),
(next_id('1_platform_parameters'),'price_create_exec_@1_new_section', '1', 'ContractAccess("@1UpdatePlatformParam")');
`

var MigrationUpdatePriceExec = `
INSERT INTO "1_platform_parameters" (id, name, value, conditions) VALUES
(next_id('1_platform_parameters'), 'price_exec_get_block', '50', 'ContractAccess("@1UpdatePlatformParam")'),
Expand Down
4 changes: 2 additions & 2 deletions packages/script/func.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/IBAX-io/go-ibax/packages/consts"
"github.com/IBAX-io/go-ibax/packages/converter"
"github.com/IBAX-io/go-ibax/packages/types"
"github.com/IBAX-io/go-ibax/packages/utils"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -110,8 +111,7 @@ func ExecContract(rt *RunTime, name, txs string, params ...any) (any, error) {
}
}
rt.cost -= CostContract
if priceName, ok := ContractPrices[name]; ok {
price := syspar.SysInt64(priceName)
if price, ok := syspar.GetPriceCreateExec(utils.ToSnakeCase(name)); ok {
if price > 0 {
rt.cost -= price
}
Expand Down
Loading

0 comments on commit 191c665

Please sign in to comment.