Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UTXO combustion percent and expediteFee #2047

Merged
merged 1 commit into from
Sep 28, 2022
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
1 change: 1 addition & 0 deletions packages/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Block struct {
OutputsMap map[sqldb.KeyUTXO][]sqldb.SpentInfo
ClassifyTxsMap map[int][]*transaction.Transaction
PrevSysPar map[string]string
ComPercents map[int64]int64 // combustion percent for each ecosystem
}

// GetLogger is returns logger
Expand Down
26 changes: 22 additions & 4 deletions packages/block/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"bytes"
"encoding/hex"
"fmt"
"strconv"
"sync"

"github.com/IBAX-io/go-ibax/packages/common/random"
"github.com/IBAX-io/go-ibax/packages/conf/syspar"
"github.com/IBAX-io/go-ibax/packages/consts"
Expand All @@ -20,8 +23,6 @@ import (
"github.com/IBAX-io/go-ibax/packages/types"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"strconv"
"sync"
)

// PlaySafe is inserting block safely
Expand Down Expand Up @@ -113,16 +114,33 @@ func (b *Block) ProcessTxs(dbTx *sqldb.DbTransaction) (err error) {
//}

var keyIds []int64
var keyIdsMap = make(map[int64]bool)
var ecosystemIds []int64
var ecosystemIdsMap = make(map[int64]bool)
for indexTx := 0; indexTx < len(b.Transactions); indexTx++ {
t := b.Transactions[indexTx]
keyIds = append(keyIds, t.KeyID())
if !keyIdsMap[t.KeyID()] {
keyIdsMap[t.KeyID()] = true
keyIds = append(keyIds, t.KeyID())
}
if t.IsSmartContract() && !ecosystemIdsMap[t.SmartContract().TxSmart.EcosystemID] {
ecosystemIdsMap[t.SmartContract().TxSmart.EcosystemID] = true
ecosystemIds = append(ecosystemIds, t.SmartContract().TxSmart.EcosystemID)
}
}
// query all keys utxo
outputs, err := sqldb.GetTxOutputs(dbTx, keyIds)
if err != nil {
return err
}
b.OutputsMap = make(map[sqldb.KeyUTXO][]sqldb.SpentInfo)
sqldb.PutAllOutputsMap(outputs, b.OutputsMap)
// query all ecosystems combination percent
comPercents, err := sqldb.GetCombustionPercents(dbTx, ecosystemIds)
if err != nil {
return err
}
b.ComPercents = comPercents
// UTXO multiple ecosystem fuelRate
b.PrevSysPar = syspar.GetSysParCache()
var wg sync.WaitGroup
Expand Down Expand Up @@ -231,7 +249,7 @@ func (b *Block) serialExecuteTxs(dbTx *sqldb.DbTransaction, txBadChan chan badTx
logger.WithFields(log.Fields{"type": consts.DBError, "error": err, "tx_hash": t.Hash()}).Error("using savepoint")
return err
}
err = t.WithOption(notificator.NewQueue(), b.GenBlock, b.Header, b.PrevHeader, dbTx, rand.BytesSeed(t.Hash()), limits, consts.SetSavePointMarkBlock(hex.EncodeToString(t.Hash())), b.OutputsMap, b.PrevSysPar)
err = t.WithOption(notificator.NewQueue(), b.GenBlock, b.Header, b.PrevHeader, dbTx, rand.BytesSeed(t.Hash()), limits, consts.SetSavePointMarkBlock(hex.EncodeToString(t.Hash())), b.OutputsMap, b.PrevSysPar, b.ComPercents)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions packages/smart/smart.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type SmartContract struct {
TxInputsMap map[sqldb.KeyUTXO][]sqldb.SpentInfo
TxOutputsMap map[sqldb.KeyUTXO][]sqldb.SpentInfo
PrevSysPar map[string]string
ComPercents map[int64]int64
}

// AppendStack adds an element to the stack of contract call or removes the top element when name is empty
Expand Down
23 changes: 20 additions & 3 deletions packages/smart/smart_p.go
Original file line number Diff line number Diff line change
Expand Up @@ -807,14 +807,15 @@ func UtxoToken(sc *SmartContract, toID int64, value string) (flag bool, err erro
}
var fuels = make(map[int64]string)
var wallets = make(map[int64]string)

var expediteFee decimal.Decimal
fuels, err = getParams(syspar.FuelRate)
wallets, err = getParams(syspar.TaxesWallet)

fromID := sc.TxSmart.KeyID
outputsMap := sc.OutputsMap
txInputsMap := sc.TxInputsMap
txOutputsMap := sc.TxOutputsMap
comPercents := sc.ComPercents
//txHash := sc.Hash
ecosystem := sc.TxSmart.EcosystemID
blockId := sc.BlockHeader.BlockId
Expand All @@ -825,7 +826,10 @@ func UtxoToken(sc *SmartContract, toID int64, value string) (flag bool, err erro
if len(txInputs) == 0 {
return false, fmt.Errorf(eEcoCurrentBalance, converter.IDToAddress(fromID), ecosystem)
}

if expediteFee, err = sc.expediteFee(); err != nil {
return false, err
}
fmt.Println("expediteFee", expediteFee.String())
totalAmount := decimal.Zero

var txOutputs []sqldb.SpentInfo
Expand Down Expand Up @@ -868,7 +872,8 @@ func UtxoToken(sc *SmartContract, toID int64, value string) (flag bool, err erro
}
// ecosystem fuelRate /10 *( bit + len(input))
money1 = fuelRate1.Div(decimal.NewFromInt(10)).Mul(decimal.NewFromInt(sc.TxSize).Add(decimal.NewFromInt(int64(len(txInputs1)))))

// utxo ecosystem 1 expediteFee
money1 = money1.Add(expediteFee)
if money1.GreaterThan(totalAmount1) {
money1 = totalAmount1
}
Expand Down Expand Up @@ -922,6 +927,16 @@ func UtxoToken(sc *SmartContract, toID int64, value string) (flag bool, err erro
if money2.GreaterThan(totalAmount) {
money2 = totalAmount
}
percentMoney2 := decimal.Zero
if percent, hasPercent := comPercents[ecosystem2]; hasPercent && 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})
outputIndex++
money2 = money2.Sub(percentMoney2)
}
}
taxes2 = money2.Mul(decimal.NewFromInt(TaxesSize)).Div(decimal.New(100, 0)).Floor()
}
if money2.GreaterThan(decimal.Zero) && taxes2.GreaterThan(decimal.Zero) {
Expand Down Expand Up @@ -959,6 +974,8 @@ func UtxoToken(sc *SmartContract, toID int64, value string) (flag bool, err erro
} else {
// ecosystem fuelRate /10 *( bit + len(input))
money1 = fuelRate1.Div(decimal.NewFromInt(10)).Mul(decimal.NewFromInt(sc.TxSize).Add(decimal.NewFromInt(int64(len(txInputs)))))
// utxo ecosystem 1 expediteFee
money1 = money1.Add(expediteFee)
if money1.GreaterThan(totalAmount) {
money1 = totalAmount
}
Expand Down
30 changes: 30 additions & 0 deletions packages/storage/sqldb/ecosystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,36 @@ func GetAllSystemStatesIDs() ([]int64, []string, error) {
return ids, names, nil
}

// 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 ?
`

type Combustion struct {
Id int64
Percent int64
}

var ret []Combustion
if len(ids) > 0 {
err := GetDB(db).Debug().Raw(query, ids).Scan(&ret).Error
if err != nil {
return nil, err
}
}
var result = make(map[int64]int64)
for _, combustion := range ret {
result[combustion.Id] = combustion.Percent
}
return result, nil
}

// Get is fill receiver from db
func (sys *Ecosystem) Get(dbTx *DbTransaction, id int64) (bool, error) {
return isFound(GetDB(dbTx).First(sys, "id = ?", id))
Expand Down
1 change: 1 addition & 0 deletions packages/transaction/deliver.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type InToCxt struct {
TxCheckLimits *Limits
OutputsMap map[sqldb.KeyUTXO][]sqldb.SpentInfo
PrevSysPar map[string]string
ComPercents map[int64]int64
}

type OutCtx struct {
Expand Down
1 change: 1 addition & 0 deletions packages/transaction/smart_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (s *SmartTransactionParser) Init(t *InToCxt) error {
s.SysUpdate = false
s.OutputsMap = t.OutputsMap
s.PrevSysPar = t.PrevSysPar
s.ComPercents = t.ComPercents
s.TxInputsMap = make(map[sqldb.KeyUTXO][]sqldb.SpentInfo)
s.TxOutputsMap = make(map[sqldb.KeyUTXO][]sqldb.SpentInfo)
s.RollBackTx = make([]*types.RollbackTx, 0)
Expand Down
2 changes: 2 additions & 0 deletions packages/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func (tr *Transaction) WithOption(
sqlDbSavePoint string,
outputsMap map[sqldb.KeyUTXO][]sqldb.SpentInfo,
prevSysPar map[string]string,
comPercents map[int64]int64,
opts ...TransactionOption) error {
in := &InToCxt{
SqlDbSavePoint: sqlDbSavePoint,
Expand All @@ -94,6 +95,7 @@ func (tr *Transaction) WithOption(
Notifications: notifications,
OutputsMap: outputsMap,
PrevSysPar: prevSysPar,
ComPercents: comPercents,
}
in.DbTransaction.BinLogSql = nil
tr.InToCxt = in
Expand Down