Skip to content

Commit 1624443

Browse files
committed
add Destroy UTXO payload
add Destroy UTXO payload Signed-off-by: luodanwg <luodan.wg@gmail.com>
1 parent fe0e9ab commit 1624443

6 files changed

Lines changed: 64 additions & 4 deletions

File tree

core/store/ChainStore/ChainStore.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,8 @@ func (bd *ChainStore) persist(b *Block) error {
749749
b.Transactions[i].TxType == tx.BookKeeper ||
750750
b.Transactions[i].TxType == tx.PrivacyPayload ||
751751
b.Transactions[i].TxType == tx.BookKeeping ||
752-
b.Transactions[i].TxType == tx.DataFile {
752+
b.Transactions[i].TxType == tx.DataFile ||
753+
b.Transactions[i].TxType == tx.DestroyUTXO {
753754
err = bd.SaveTransaction(b.Transactions[i], b.Blockdata.Height)
754755
if err != nil {
755756
return err
@@ -774,6 +775,20 @@ func (bd *ChainStore) persist(b *Block) error {
774775
}
775776
}
776777

778+
if b.Transactions[i].TxType == tx.DestroyUTXO {
779+
results, err := b.Transactions[i].GetMergedAssetIDValueFromReference()
780+
if err != nil {
781+
log.Error("[GetMergedAssetIDValueFromReference] failed.")
782+
}
783+
for assetId, value := range results {
784+
if _, ok := quantities[assetId]; !ok {
785+
quantities[assetId] -= value
786+
} else {
787+
quantities[assetId] -= value
788+
}
789+
}
790+
}
791+
777792
for index := 0; index < len(b.Transactions[i].Outputs); index++ {
778793
output := b.Transactions[i].Outputs[index]
779794
programHash := output.ProgramHash

core/transaction/TransactionBuilder.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,19 @@ func NewDataFileTransaction(path string, fileName string, note string, issuer *c
142142
Programs: []*program.Program{},
143143
}, nil
144144
}
145+
146+
func NewDestroyUTXOTransaction(inputs []*UTXOTxInput) (*Transaction, error) {
147+
148+
//TODO: check arguments
149+
150+
destroyUTXO := &payload.DestroyUTXO{}
151+
152+
return &Transaction{
153+
TxType: DestroyUTXO,
154+
Payload: destroyUTXO,
155+
Attributes: []*TxAttribute{},
156+
UTXOInputs: inputs,
157+
BalanceInputs: []*BalanceTxInput{},
158+
Programs: []*program.Program{},
159+
}, nil
160+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package payload
2+
3+
import "io"
4+
5+
type DestroyUTXO struct {
6+
}
7+
8+
func (a *DestroyUTXO) Data() []byte {
9+
//TODO: implement TransferAsset.Data()
10+
return []byte{0}
11+
12+
}
13+
14+
func (a *DestroyUTXO) Serialize(w io.Writer) error {
15+
return nil
16+
}
17+
18+
func (a *DestroyUTXO) Deserialize(r io.Reader) error {
19+
return nil
20+
}

core/transaction/transaction.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const (
3232
Record TransactionType = 0x81
3333
DeployCode TransactionType = 0xd0
3434
DataFile TransactionType = 0x12
35+
DestroyUTXO TransactionType = 0x18
3536
)
3637

3738
//Payload define the func for loading the payload data
@@ -202,6 +203,8 @@ func (tx *Transaction) DeserializeUnsignedWithoutType(r io.Reader) error {
202203
tx.Payload = new(payload.PrivacyPayload)
203204
case DataFile:
204205
tx.Payload = new(payload.DataFile)
206+
case DestroyUTXO:
207+
tx.Payload = new(payload.DestroyUTXO)
205208
default:
206209
return errors.New("[Transaction],invalide transaction type.")
207210
}
@@ -342,6 +345,7 @@ func (tx *Transaction) GetProgramHashes() ([]Uint160, error) {
342345
return nil, NewDetailErr(err, ErrNoCode, "[Transaction], GetProgramHashes ToCodeHash failed.")
343346
}
344347
hashs = append(hashs, astHash)
348+
case DestroyUTXO:
345349
default:
346350
}
347351
//remove dupilicated hashes

core/validation/txValidator.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,10 @@ func CheckTransactionPayload(Tx *tx.Transaction) error {
238238
return nil
239239
case *payload.RegisterAsset:
240240
if pld.Asset.Precision < asset.MinPrecision || pld.Asset.Precision > asset.MaxPrecision {
241-
return errors.New("Invalide asset Precision.")
241+
return errors.New("[CheckTransactionPayload],invalid asset Precision.")
242242
}
243243
if checkAmountPrecise(pld.Amount, pld.Asset.Precision) {
244-
return errors.New("Invalide asset value,out of precise.")
244+
return errors.New("[CheckTransactionPayload],invalid asset value,out of precise.")
245245
}
246246
case *payload.IssueAsset:
247247
case *payload.TransferAsset:
@@ -250,8 +250,12 @@ func CheckTransactionPayload(Tx *tx.Transaction) error {
250250
case *payload.Record:
251251
case *payload.DeployCode:
252252
case *payload.DataFile:
253+
case *payload.DestroyUTXO:
254+
if len(Tx.Outputs)>0{
255+
return errors.New("[CheckTransactionPayload],invalid transaction outputs.")
256+
}
253257
default:
254-
return errors.New("[txValidator],invalidate transaction payload type.")
258+
return errors.New("[CheckTransactionPayload],invalid transaction payload type.")
255259
}
256260
return nil
257261
}

net/httpjsonrpc/TransPayloadToHex.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ func TransPayloadToHex(p Payload) PayloadInfo {
137137
obj.Issuer.X = object.Issuer.X.String()
138138
obj.Issuer.Y = object.Issuer.Y.String()
139139
return obj
140+
case *payload.DestroyUTXO:
140141
}
141142
return nil
142143
}

0 commit comments

Comments
 (0)