From 6bac7462801b5e6da47f1231681bb1516a7dd4bb Mon Sep 17 00:00:00 2001 From: powerpook <75034263+powerpook@users.noreply.github.com> Date: Mon, 17 Oct 2022 15:43:27 +0800 Subject: [PATCH] support ecosystem 'utxo_fee' parameter settings (#2058) --- packages/smart/smart_p.go | 6 ++++-- packages/storage/sqldb/ecosystem.go | 11 +++++------ packages/transaction/smart_contract.go | 2 +- packages/types/custom_tx.go | 15 +++++++++++---- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/packages/smart/smart_p.go b/packages/smart/smart_p.go index 21e2c9af..c63826ad 100644 --- a/packages/smart/smart_p.go +++ b/packages/smart/smart_p.go @@ -912,7 +912,9 @@ func UtxoToken(sc *SmartContract, toID int64, value string) (flag bool, err erro var money2 = decimal.Zero var fuelRate2 = decimal.Zero var taxes2 = decimal.Zero - if ret, ok := fuels[ecosystem2]; ok { + ret, ok := fuels[ecosystem2] + percent, hasPercent := comPercents[ecosystem2] + if ok && hasPercent { fuelRate2, err = decimal.NewFromString(ret) if err != nil { @@ -925,7 +927,7 @@ func UtxoToken(sc *SmartContract, toID int64, value string) (flag bool, err erro money2 = totalAmount } percentMoney2 := decimal.Zero - if percent, hasPercent := comPercents[ecosystem2]; hasPercent && percent > 0 && money2.GreaterThan(decimal.Zero) { + if percent > 0 && money2.GreaterThan(decimal.Zero) { percentMoney2 = money2.Mul(decimal.NewFromInt(percent)).Div(decimal.New(100, 0)).Floor() if percentMoney2.GreaterThan(decimal.Zero) { txOutputs = append(txOutputs, sqldb.SpentInfo{OutputIndex: outputIndex, OutputKeyId: 0, OutputValue: percentMoney2.String(), BlockId: blockId, Ecosystem: ecosystem2, Type: consts.UTXO_Type_Combustion}) diff --git a/packages/storage/sqldb/ecosystem.go b/packages/storage/sqldb/ecosystem.go index 04c90155..66477d80 100644 --- a/packages/storage/sqldb/ecosystem.go +++ b/packages/storage/sqldb/ecosystem.go @@ -85,14 +85,13 @@ func GetAllSystemStatesIDs() ([]int64, []string, error) { // GetCombustionPercents is ecosystem combustion percent func GetCombustionPercents(db *DbTransaction, ids []int64) (map[int64]int64, error) { - //select id, (fee_mode_info::json #>> '{combustion,percent}')::int as percent from "1_ecosystems" where (fee_mode_info::json #>> '{combustion,flag}')::int = 2 and id in - //select id,(fee_mode_info -> 'combustion' ->> 'percent')::int as percent from "1_ecosystems" where (fee_mode_info -> 'combustion' ->> 'flag')::int = 2 and id in query := ` - select id,(fee_mode_info::json#>>'{combustion,percent}')::int as percent - from "1_ecosystems" - where (fee_mode_info::json#>>'{combustion,flag}')::int=2 and id IN ? - ` + SELECT eco.id,(eco.fee_mode_info::json#>>'{combustion,percent}')::int as percent + FROM "1_parameters" as par + LEFT JOIN "1_ecosystems" as eco ON par.ecosystem = eco.id + WHERE par.name = 'utxo_fee' and par.value = '1' and par.ecosystem IN ? + ` type Combustion struct { Id int64 diff --git a/packages/transaction/smart_contract.go b/packages/transaction/smart_contract.go index 775d9947..66922951 100644 --- a/packages/transaction/smart_contract.go +++ b/packages/transaction/smart_contract.go @@ -94,6 +94,7 @@ func (s *SmartTransactionParser) Action(in *InToCxt, out *OutCtx) (err error) { } out.Apply( WithOutCtxTxResult(ret), + WithOutCtxSysUpdate(s.SysUpdate), WithOutCtxRollBackTx(s.RollBackTx), ) if err != nil || s.Penalty { @@ -109,7 +110,6 @@ func (s *SmartTransactionParser) Action(in *InToCxt, out *OutCtx) (err error) { ret.BlockId = s.BlockHeader.BlockId out.Apply( WithOutCtxTxResult(ret), - WithOutCtxSysUpdate(s.SysUpdate), WithOutCtxTxOutputs(s.TxOutputsMap), WithOutCtxTxInputs(s.TxInputsMap), ) diff --git a/packages/types/custom_tx.go b/packages/types/custom_tx.go index c1abf89c..ea6d3ea3 100644 --- a/packages/types/custom_tx.go +++ b/packages/types/custom_tx.go @@ -5,6 +5,7 @@ package types import ( + "errors" "fmt" "regexp" "strings" @@ -142,14 +143,20 @@ func (txSmart *SmartTransaction) Validate() error { if txSmart.NetworkID != conf.Config.LocalConf.NetworkID { return fmt.Errorf("error networkid invalid") } - if txSmart.UTXO != nil && len(txSmart.UTXO.Value) > 0 { + if txSmart.UTXO != nil { if ok, _ := regexp.MatchString("^\\d+$", txSmart.UTXO.Value); !ok { - return fmt.Errorf("error UTXO %s must integer", txSmart.UTXO.Value) + return errors.New("error UTXO Value must be a positive integer") + } + if value, err := decimal.NewFromString(txSmart.UTXO.Value); err != nil || value.LessThanOrEqual(decimal.Zero) { + return errors.New("error UTXO Value must be greater than zero") } } - if txSmart.TransferSelf != nil && len(txSmart.TransferSelf.Value) > 0 { + if txSmart.TransferSelf != nil { if ok, _ := regexp.MatchString("^\\d+$", txSmart.TransferSelf.Value); !ok { - return fmt.Errorf("error TransferSelf %s must integer", txSmart.TransferSelf.Value) + return errors.New("error TransferSelf Value must be a positive integer") + } + if value, err := decimal.NewFromString(txSmart.TransferSelf.Value); err != nil || value.LessThanOrEqual(decimal.Zero) { + return errors.New("error TransferSelf Value must be greater than zero") } }