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

The UTXO type is added #2055

Merged
merged 2 commits into from
Oct 9, 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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,4 @@ initDatabase.txt
docker-compose.yml
Dockerfile
Dockerfile_src
test.log
11 changes: 11 additions & 0 deletions packages/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,14 @@ func Version() string {
func SetSavePointMarkBlock(idTx string) string {
return fmt.Sprintf("\"%s-%s\";", SavePointMarkBlock, idTx)
}

const (
UTXO_Type_First_Block = 1 //Initialize the first block
UTXO_Type_Self_UTXO = 11
UTXO_Type_Self_Account = 12
UTXO_Type_Packaging = 20
UTXO_Type_Taxes = 21
UTXO_Type_Output = 22
UTXO_Type_Combustion = 23
UTXO_Type_Transfer = 26
)
1 change: 1 addition & 0 deletions packages/migration/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ var (
t.Column("output_value", "decimal(30)", {"default_raw": "'0' CHECK (output_value >= 0)"})
t.Column("ecosystem", "bigint", {"default": "1"})
t.Column("block_id", "bigint")
t.Column("type", "bigint")
{{footer "primary(output_tx_hash,output_key_id,output_index)" "index(block_id)" "index(input_tx_hash)" "index(output_key_id)" "index(output_tx_hash)"}}

{{head "transactions"}}
Expand Down
32 changes: 13 additions & 19 deletions packages/smart/smart_p.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ func TransferSelf(sc *SmartContract, value string, source string, target string)
// The change
var txOutputs []sqldb.SpentInfo
if totalAmount.GreaterThan(decimal.Zero) {
txOutputs = append(txOutputs, sqldb.SpentInfo{OutputIndex: 0, OutputKeyId: fromID, OutputValue: totalAmount.String(), BlockId: blockId, Ecosystem: ecosystem}) // The change
txOutputs = append(txOutputs, sqldb.SpentInfo{OutputIndex: 0, OutputKeyId: fromID, OutputValue: totalAmount.String(), BlockId: blockId, Ecosystem: ecosystem, Type: consts.UTXO_Type_Self_UTXO}) // The change
}
if len(txInputs) > 0 {
sqldb.PutAllOutputsMap(txInputs, txInputsMap)
Expand All @@ -765,7 +765,7 @@ func TransferSelf(sc *SmartContract, value string, source string, target string)
}
if totalAmount.GreaterThanOrEqual(payValue) && payValue.GreaterThan(decimal.Zero) {
flag = true // The transfer was successful
txOutputs = append(txOutputs, sqldb.SpentInfo{OutputIndex: 0, OutputKeyId: fromID, OutputValue: value, BlockId: blockId, Ecosystem: ecosystem})
txOutputs = append(txOutputs, sqldb.SpentInfo{OutputIndex: 0, OutputKeyId: fromID, OutputValue: value, BlockId: blockId, Ecosystem: ecosystem, Type: consts.UTXO_Type_Self_Account})
totalAmount = totalAmount.Sub(payValue)
if _, _, err = sc.updateWhere([]string{`-amount`}, []any{payValue}, "1_keys", types.LoadMap(map[string]any{`id`: fromID, `ecosystem`: ecosystem})); err != nil {
return false, err
Expand Down Expand Up @@ -886,19 +886,17 @@ func UtxoToken(sc *SmartContract, toID int64, value string) (flag bool, err erro

flag = true
// 97%
txOutputs1 = append(txOutputs1, sqldb.SpentInfo{OutputIndex: outputIndex, OutputKeyId: sc.BlockHeader.KeyId, OutputValue: money1.Sub(taxes1).String(),
BlockId: blockId, Ecosystem: ecosystem1})
txOutputs1 = append(txOutputs1, sqldb.SpentInfo{OutputIndex: outputIndex, OutputKeyId: sc.BlockHeader.KeyId, OutputValue: money1.Sub(taxes1).String(), BlockId: blockId, Ecosystem: ecosystem1, Type: consts.UTXO_Type_Packaging})
outputIndex++
// 3%
txOutputs1 = append(txOutputs1, sqldb.SpentInfo{OutputIndex: outputIndex, OutputKeyId: taxesID, OutputValue: taxes1.String(),
BlockId: blockId, Ecosystem: ecosystem1})
txOutputs1 = append(txOutputs1, sqldb.SpentInfo{OutputIndex: outputIndex, OutputKeyId: taxesID, OutputValue: taxes1.String(), BlockId: blockId, Ecosystem: ecosystem1, Type: consts.UTXO_Type_Taxes})
outputIndex++
totalAmount1 = totalAmount1.Sub(money1)
}
}

if totalAmount1.GreaterThan(decimal.Zero) {
txOutputs1 = append(txOutputs1, sqldb.SpentInfo{OutputIndex: outputIndex, OutputKeyId: fromID, OutputValue: totalAmount1.String(), BlockId: blockId, Ecosystem: ecosystem1}) // The change
txOutputs1 = append(txOutputs1, sqldb.SpentInfo{OutputIndex: outputIndex, OutputKeyId: fromID, OutputValue: totalAmount1.String(), BlockId: blockId, Ecosystem: ecosystem1, Type: consts.UTXO_Type_Output}) // The change
outputIndex++
}

Expand Down Expand Up @@ -930,10 +928,10 @@ func UtxoToken(sc *SmartContract, toID int64, value string) (flag bool, err erro
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})
txOutputs = append(txOutputs, sqldb.SpentInfo{OutputIndex: outputIndex, OutputKeyId: 0, OutputValue: percentMoney2.String(), BlockId: blockId, Ecosystem: ecosystem2, Type: consts.UTXO_Type_Combustion})
outputIndex++
money2 = money2.Sub(percentMoney2)
totalAmount = totalAmount.Sub(percentMoney2)
}
}
taxes2 = money2.Mul(decimal.NewFromInt(TaxesSize)).Div(decimal.New(100, 0)).Floor()
Expand All @@ -944,12 +942,10 @@ func UtxoToken(sc *SmartContract, toID int64, value string) (flag bool, err erro

flag = true
// 97%
txOutputs = append(txOutputs, sqldb.SpentInfo{OutputIndex: outputIndex, OutputKeyId: sc.BlockHeader.KeyId, OutputValue: money2.Sub(taxes2).String(),
BlockId: blockId, Ecosystem: ecosystem2})
txOutputs = append(txOutputs, sqldb.SpentInfo{OutputIndex: outputIndex, OutputKeyId: sc.BlockHeader.KeyId, OutputValue: money2.Sub(taxes2).String(), BlockId: blockId, Ecosystem: ecosystem2, Type: consts.UTXO_Type_Packaging})
outputIndex++
// 3%
txOutputs = append(txOutputs, sqldb.SpentInfo{OutputIndex: outputIndex, OutputKeyId: taxesID, OutputValue: taxes2.String(),
BlockId: blockId, Ecosystem: ecosystem2})
txOutputs = append(txOutputs, sqldb.SpentInfo{OutputIndex: outputIndex, OutputKeyId: taxesID, OutputValue: taxes2.String(), BlockId: blockId, Ecosystem: ecosystem2, Type: consts.UTXO_Type_Taxes})
outputIndex++
totalAmount = totalAmount.Sub(money2)

Expand Down Expand Up @@ -987,12 +983,10 @@ func UtxoToken(sc *SmartContract, toID int64, value string) (flag bool, err erro

flag = true
// 97%
txOutputs = append(txOutputs, sqldb.SpentInfo{OutputIndex: outputIndex, OutputKeyId: sc.BlockHeader.KeyId, OutputValue: money1.Sub(taxes1).String(),
BlockId: blockId, Ecosystem: ecosystem1})
txOutputs = append(txOutputs, sqldb.SpentInfo{OutputIndex: outputIndex, OutputKeyId: sc.BlockHeader.KeyId, OutputValue: money1.Sub(taxes1).String(), BlockId: blockId, Ecosystem: ecosystem1, Type: consts.UTXO_Type_Packaging})
outputIndex++
// 3%
txOutputs = append(txOutputs, sqldb.SpentInfo{OutputIndex: outputIndex, OutputKeyId: taxesID, OutputValue: taxes1.String(),
BlockId: blockId, Ecosystem: ecosystem1})
txOutputs = append(txOutputs, sqldb.SpentInfo{OutputIndex: outputIndex, OutputKeyId: taxesID, OutputValue: taxes1.String(), BlockId: blockId, Ecosystem: ecosystem1, Type: consts.UTXO_Type_Taxes})
outputIndex++
totalAmount = totalAmount.Sub(money1)

Expand All @@ -1004,7 +998,7 @@ func UtxoToken(sc *SmartContract, toID int64, value string) (flag bool, err erro
payValue, _ := decimal.NewFromString(value)
if totalAmount.GreaterThanOrEqual(payValue) && payValue.GreaterThan(decimal.Zero) {
flag = true // The transfer was successful
txOutputs = append(txOutputs, sqldb.SpentInfo{OutputIndex: outputIndex, OutputKeyId: toID, OutputValue: value, BlockId: blockId, Ecosystem: ecosystem})
txOutputs = append(txOutputs, sqldb.SpentInfo{OutputIndex: outputIndex, OutputKeyId: toID, OutputValue: value, BlockId: blockId, Ecosystem: ecosystem, Type: consts.UTXO_Type_Transfer})
outputIndex++
totalAmount = totalAmount.Sub(payValue)
} else {
Expand All @@ -1014,7 +1008,7 @@ func UtxoToken(sc *SmartContract, toID int64, value string) (flag bool, err erro

// The change
if totalAmount.GreaterThan(decimal.Zero) {
txOutputs = append(txOutputs, sqldb.SpentInfo{OutputIndex: outputIndex, OutputKeyId: fromID, OutputValue: totalAmount.String(), BlockId: blockId, Ecosystem: ecosystem}) // The change
txOutputs = append(txOutputs, sqldb.SpentInfo{OutputIndex: outputIndex, OutputKeyId: fromID, OutputValue: totalAmount.String(), BlockId: blockId, Ecosystem: ecosystem, Type: consts.UTXO_Type_Output}) // The change
outputIndex++
}
if len(txInputs) > 0 && len(txOutputs) > 0 {
Expand Down
3 changes: 2 additions & 1 deletion packages/storage/sqldb/spent_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package sqldb
import (
"errors"
"fmt"

"github.com/IBAX-io/go-ibax/packages/consts"
"github.com/shopspring/decimal"
log "github.com/sirupsen/logrus"
Expand All @@ -25,7 +26,7 @@ type SpentInfo struct {
OutputValue string `gorm:"not null"`
Ecosystem int64
BlockId int64
Action string `gorm:"-"` // UTXO operation control : change
Type int32
}

type KeyUTXO struct {
Expand Down
4 changes: 2 additions & 2 deletions packages/transaction/first_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ func (f *FirstBlockParser) Action(in *InToCxt, out *OutCtx) (err error) {
return err
}

err = sqldb.GetDB(dbTx).Exec(`insert into "spent_info" (output_index,output_tx_hash,output_key_id,output_value,ecosystem,block_id) values(?,?,?,?,?,?)`,
0, f.TxHash, keyID, amount, 1, 1).Error
err = sqldb.GetDB(dbTx).Exec(`insert into "spent_info" (output_index,output_tx_hash,output_key_id,output_value,ecosystem,block_id,type) values(?,?,?,?,?,?,?)`,
0, f.TxHash, keyID, amount, 1, 1, consts.UTXO_Type_First_Block).Error
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("inserting spent info")
return err
Expand Down