Skip to content

Commit 1f1253f

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

6 files changed

Lines changed: 83 additions & 10 deletions

File tree

core/store/ChainStore/ChainStore.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,8 @@ func (bd *ChainStore) persist(b *Block) error {
773773
b.Transactions[i].TxType == tx.BookKeeper ||
774774
b.Transactions[i].TxType == tx.PrivacyPayload ||
775775
b.Transactions[i].TxType == tx.BookKeeping ||
776-
b.Transactions[i].TxType == tx.DataFile {
776+
b.Transactions[i].TxType == tx.DataFile ||
777+
b.Transactions[i].TxType == tx.DestroyUTXO {
777778
err = bd.SaveTransaction(b.Transactions[i], b.Blockdata.Height)
778779
if err != nil {
779780
return err
@@ -798,6 +799,16 @@ func (bd *ChainStore) persist(b *Block) error {
798799
}
799800
}
800801

802+
if b.Transactions[i].TxType == tx.DestroyUTXO {
803+
results, err := b.Transactions[i].GetMergedAssetIDValueFromReference()
804+
if err != nil {
805+
log.Error("[GetMergedAssetIDValueFromReference] failed.")
806+
}
807+
for assetId, value := range results {
808+
quantities[assetId] -= value
809+
}
810+
}
811+
801812
for index := 0; index < len(b.Transactions[i].Outputs); index++ {
802813
output := b.Transactions[i].Outputs[index]
803814
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: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const (
3030
Record TransactionType = 0x81
3131
DeployCode TransactionType = 0xd0
3232
DataFile TransactionType = 0x12
33+
DestroyUTXO TransactionType = 0x18
3334
)
3435

3536
//Payload define the func for loading the payload data
@@ -200,6 +201,8 @@ func (tx *Transaction) DeserializeUnsignedWithoutType(r io.Reader) error {
200201
tx.Payload = new(payload.PrivacyPayload)
201202
case DataFile:
202203
tx.Payload = new(payload.DataFile)
204+
case DestroyUTXO:
205+
tx.Payload = new(payload.DestroyUTXO)
203206
default:
204207
return errors.New("[Transaction],invalide transaction type.")
205208
}
@@ -293,9 +296,6 @@ func (tx *Transaction) GetProgramHashes() ([]Uint160, error) {
293296
hashs = append(hashs, astHash)
294297
case IssueAsset:
295298
result := tx.GetMergedAssetIDValueFromOutputs()
296-
if err != nil {
297-
return nil, NewDetailErr(err, ErrNoCode, "[Transaction], GetTransactionResults failed.")
298-
}
299299
for k := range result {
300300
tx, err := TxStore.GetTransaction(k)
301301
if err != nil {
@@ -339,6 +339,27 @@ func (tx *Transaction) GetProgramHashes() ([]Uint160, error) {
339339
return nil, NewDetailErr(err, ErrNoCode, "[Transaction], GetProgramHashes ToCodeHash failed.")
340340
}
341341
hashs = append(hashs, astHash)
342+
case DestroyUTXO:
343+
inputs,err:= tx.GetMergedAssetIDValueFromReference()
344+
if err != nil {
345+
return nil, NewDetailErr(err, ErrNoCode, "[Transaction], GetTransactionInputs failed.")
346+
}
347+
for k := range inputs {
348+
tx, err := TxStore.GetTransaction(k)
349+
if err != nil {
350+
return nil, NewDetailErr(err, ErrNoCode, fmt.Sprintf("[Transaction], GetTransaction failed With AssetID:=%x", k))
351+
}
352+
if tx.TxType != RegisterAsset {
353+
return nil, NewDetailErr(errors.New("[Transaction] error"), ErrNoCode, fmt.Sprintf("[Transaction], Transaction Type ileage With AssetID:=%x", k))
354+
}
355+
356+
switch v1 := tx.Payload.(type) {
357+
case *payload.RegisterAsset:
358+
hashs = append(hashs, v1.Controller)
359+
default:
360+
return nil, NewDetailErr(errors.New("[Transaction] error"), ErrNoCode, fmt.Sprintf("[Transaction], payload is illegal", k))
361+
}
362+
}
342363
default:
343364
}
344365
//remove dupilicated hashes

core/validation/txValidator.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ func VerifyTransaction(Tx *tx.Transaction) error {
3131
return err
3232
}
3333

34-
if err := CheckTransactionContracts(Tx); err != nil {
34+
if err := CheckTransactionPayload(Tx); err != nil {
3535
return err
3636
}
3737

38-
if err := CheckTransactionPayload(Tx); err != nil {
38+
if err := CheckTransactionContracts(Tx); err != nil {
3939
return err
4040
}
4141

@@ -192,7 +192,7 @@ func CheckTransactionBalance(Tx *tx.Transaction) error {
192192
return errors.New("Invalide transaction UTXO output.")
193193
}
194194
}
195-
if Tx.TxType == tx.IssueAsset {
195+
if Tx.TxType == tx.IssueAsset || Tx.TxType == tx.DestroyUTXO {
196196
if len(Tx.UTXOInputs) > 0 {
197197
return errors.New("Invalide Issue transaction.")
198198
}
@@ -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)