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 31, 2017
1 parent b0225d8 commit 4832635
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 13 deletions.
13 changes: 12 additions & 1 deletion core/store/ChainStore/ChainStore.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,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 @@ -798,6 +799,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(version byte) []byte {
//TODO: implement TransferAsset.Data()
return []byte{0}

}

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

func (a *DestroyUTXO) Deserialize(r io.Reader, version byte) error {
return nil
}
27 changes: 24 additions & 3 deletions core/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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 @@ -200,6 +201,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 @@ -293,9 +296,6 @@ func (tx *Transaction) GetProgramHashes() ([]Uint160, error) {
hashs = append(hashs, astHash)
case IssueAsset:
result := tx.GetMergedAssetIDValueFromOutputs()
if err != nil {
return nil, NewDetailErr(err, ErrNoCode, "[Transaction], GetTransactionResults failed.")
}
for k := range result {
tx, err := TxStore.GetTransaction(k)
if err != nil {
Expand Down Expand Up @@ -339,6 +339,27 @@ func (tx *Transaction) GetProgramHashes() ([]Uint160, error) {
return nil, NewDetailErr(err, ErrNoCode, "[Transaction], GetProgramHashes ToCodeHash failed.")
}
hashs = append(hashs, astHash)
case DestroyUTXO:
inputs, err := tx.GetMergedAssetIDValueFromReference()
if err != nil {
return nil, NewDetailErr(err, ErrNoCode, "[Transaction], GetTransactionInputs failed.")
}
for k := range inputs {
tx, err := TxStore.GetTransaction(k)
if err != nil {
return nil, NewDetailErr(err, ErrNoCode, fmt.Sprintf("[Transaction], GetTransaction failed With AssetID:=%x", k))
}
if tx.TxType != RegisterAsset {
return nil, NewDetailErr(errors.New("[Transaction] error"), ErrNoCode, fmt.Sprintf("[Transaction], Transaction Type ileage With AssetID:=%x", k))
}

switch v1 := tx.Payload.(type) {
case *payload.RegisterAsset:
hashs = append(hashs, v1.Controller)
default:
return nil, NewDetailErr(errors.New("[Transaction] error"), ErrNoCode, fmt.Sprintf("[Transaction], payload is illegal", k))
}
}
default:
}
//remove dupilicated hashes
Expand Down
22 changes: 13 additions & 9 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,10 +192,7 @@ func CheckTransactionBalance(Tx *tx.Transaction) error {
return errors.New("Invalide transaction UTXO output.")
}
}
if Tx.TxType == tx.IssueAsset {
if len(Tx.UTXOInputs) > 0 {
return errors.New("Invalide Issue transaction.")
}
if Tx.TxType == tx.IssueAsset || Tx.TxType == tx.DestroyUTXO {
return nil
}
results, err := Tx.GetTransactionResults()
Expand Down Expand Up @@ -238,20 +235,27 @@ 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:
if len(Tx.UTXOInputs) > 0 {
return errors.New("Invalide Issue transaction.")
}
case *payload.TransferAsset:
case *payload.BookKeeping:
case *payload.PrivacyPayload:
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 4832635

Please sign in to comment.