Skip to content

Commit

Permalink
add Destroy UTXO payload
Browse files Browse the repository at this point in the history
add Destroy UTXO payload

Signed-off-by: luodanwg <[email protected]>
  • Loading branch information
Arbio5zt committed Jul 28, 2017
1 parent fe0e9ab commit c6fe93d
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 7 deletions.
13 changes: 12 additions & 1 deletion core/store/ChainStore/ChainStore.go
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,8 @@ func (bd *ChainStore) persist(b *Block) error {
b.Transactions[i].TxType == tx.BookKeeper ||
b.Transactions[i].TxType == tx.PrivacyPayload ||
b.Transactions[i].TxType == tx.BookKeeping ||
b.Transactions[i].TxType == tx.DataFile {
b.Transactions[i].TxType == tx.DataFile ||
b.Transactions[i].TxType == tx.DestroyUTXO {
err = bd.SaveTransaction(b.Transactions[i], b.Blockdata.Height)
if err != nil {
return err
Expand All @@ -774,6 +775,16 @@ func (bd *ChainStore) persist(b *Block) error {
}
}

if b.Transactions[i].TxType == tx.DestroyUTXO {
results, err := b.Transactions[i].GetMergedAssetIDValueFromReference()
if err != nil {
log.Error("[GetMergedAssetIDValueFromReference] failed.")
}
for assetId, value := range results {
quantities[assetId] -= value
}
}

for index := 0; index < len(b.Transactions[i].Outputs); index++ {
output := b.Transactions[i].Outputs[index]
programHash := output.ProgramHash
Expand Down
16 changes: 16 additions & 0 deletions core/transaction/TransactionBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,19 @@ func NewDataFileTransaction(path string, fileName string, note string, issuer *c
Programs: []*program.Program{},
}, nil
}

func NewDestroyUTXOTransaction(inputs []*UTXOTxInput) (*Transaction, error) {

//TODO: check arguments

destroyUTXO := &payload.DestroyUTXO{}

return &Transaction{
TxType: DestroyUTXO,
Payload: destroyUTXO,
Attributes: []*TxAttribute{},
UTXOInputs: inputs,
BalanceInputs: []*BalanceTxInput{},
Programs: []*program.Program{},
}, nil
}
20 changes: 20 additions & 0 deletions core/transaction/payload/DestroyUTXO.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package payload

import "io"

type DestroyUTXO struct {
}

func (a *DestroyUTXO) Data() []byte {
//TODO: implement TransferAsset.Data()
return []byte{0}

}

func (a *DestroyUTXO) Serialize(w io.Writer) error {
return nil
}

func (a *DestroyUTXO) Deserialize(r io.Reader) error {
return nil
}
4 changes: 4 additions & 0 deletions core/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
Record TransactionType = 0x81
DeployCode TransactionType = 0xd0
DataFile TransactionType = 0x12
DestroyUTXO TransactionType = 0x18
)

//Payload define the func for loading the payload data
Expand Down Expand Up @@ -202,6 +203,8 @@ func (tx *Transaction) DeserializeUnsignedWithoutType(r io.Reader) error {
tx.Payload = new(payload.PrivacyPayload)
case DataFile:
tx.Payload = new(payload.DataFile)
case DestroyUTXO:
tx.Payload = new(payload.DestroyUTXO)
default:
return errors.New("[Transaction],invalide transaction type.")
}
Expand Down Expand Up @@ -342,6 +345,7 @@ func (tx *Transaction) GetProgramHashes() ([]Uint160, error) {
return nil, NewDetailErr(err, ErrNoCode, "[Transaction], GetProgramHashes ToCodeHash failed.")
}
hashs = append(hashs, astHash)
case DestroyUTXO:
default:
}
//remove dupilicated hashes
Expand Down
16 changes: 10 additions & 6 deletions core/validation/txValidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ func VerifyTransaction(Tx *tx.Transaction) error {
return err
}

if err := CheckTransactionContracts(Tx); err != nil {
if err := CheckTransactionPayload(Tx); err != nil {
return err
}

if err := CheckTransactionPayload(Tx); err != nil {
if err := CheckTransactionContracts(Tx); err != nil {
return err
}

Expand Down Expand Up @@ -192,7 +192,7 @@ func CheckTransactionBalance(Tx *tx.Transaction) error {
return errors.New("Invalide transaction UTXO output.")
}
}
if Tx.TxType == tx.IssueAsset {
if Tx.TxType == tx.IssueAsset || Tx.TxType == tx.DestroyUTXO {
if len(Tx.UTXOInputs) > 0 {
return errors.New("Invalide Issue transaction.")
}
Expand Down Expand Up @@ -238,10 +238,10 @@ func CheckTransactionPayload(Tx *tx.Transaction) error {
return nil
case *payload.RegisterAsset:
if pld.Asset.Precision < asset.MinPrecision || pld.Asset.Precision > asset.MaxPrecision {
return errors.New("Invalide asset Precision.")
return errors.New("[CheckTransactionPayload],invalid asset Precision.")
}
if checkAmountPrecise(pld.Amount, pld.Asset.Precision) {
return errors.New("Invalide asset value,out of precise.")
return errors.New("[CheckTransactionPayload],invalid asset value,out of precise.")
}
case *payload.IssueAsset:
case *payload.TransferAsset:
Expand All @@ -250,8 +250,12 @@ func CheckTransactionPayload(Tx *tx.Transaction) error {
case *payload.Record:
case *payload.DeployCode:
case *payload.DataFile:
case *payload.DestroyUTXO:
if len(Tx.Outputs) > 0 {
return errors.New("[CheckTransactionPayload],invalid transaction outputs.")
}
default:
return errors.New("[txValidator],invalidate transaction payload type.")
return errors.New("[CheckTransactionPayload],invalid transaction payload type.")
}
return nil
}
1 change: 1 addition & 0 deletions net/httpjsonrpc/TransPayloadToHex.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func TransPayloadToHex(p Payload) PayloadInfo {
obj.Issuer.X = object.Issuer.X.String()
obj.Issuer.Y = object.Issuer.Y.String()
return obj
case *payload.DestroyUTXO:
}
return nil
}

0 comments on commit c6fe93d

Please sign in to comment.