From 5aab7af012577c37889aac378be11522c0208033 Mon Sep 17 00:00:00 2001 From: Paladz Date: Thu, 10 Jun 2021 13:29:35 +0800 Subject: [PATCH] edit issuance (#1958) Co-authored-by: paladz --- protocol/bc/types/coinbase.go | 4 ++-- protocol/bc/types/issuance.go | 22 ++++++++++++++++------ protocol/bc/types/spend.go | 4 ++-- protocol/bc/types/txinput.go | 10 ++++------ protocol/bc/types/veto_input.go | 4 ++-- 5 files changed, 26 insertions(+), 18 deletions(-) diff --git a/protocol/bc/types/coinbase.go b/protocol/bc/types/coinbase.go index 6c08f91e..83e306b2 100644 --- a/protocol/bc/types/coinbase.go +++ b/protocol/bc/types/coinbase.go @@ -28,12 +28,12 @@ func (cb *CoinbaseInput) AssetID() bc.AssetID { // InputType is the interface function for return the input type func (cb *CoinbaseInput) InputType() uint8 { return CoinbaseInputType } -func (cb *CoinbaseInput) readCommitment(r *blockchain.Reader) (_ bc.AssetID, err error) { +func (cb *CoinbaseInput) readCommitment(r *blockchain.Reader) (err error) { cb.Arbitrary, err = blockchain.ReadVarstr31(r) return } -func (cb *CoinbaseInput) readWitness(_ *blockchain.Reader, _ bc.AssetID) error { return nil } +func (cb *CoinbaseInput) readWitness(_ *blockchain.Reader) error { return nil } func (cb *CoinbaseInput) writeCommitment(w io.Writer, _ uint64) error { if _, err := w.Write([]byte{CoinbaseInputType}); err != nil { diff --git a/protocol/bc/types/issuance.go b/protocol/bc/types/issuance.go index 1065be78..7bf73cf5 100644 --- a/protocol/bc/types/issuance.go +++ b/protocol/bc/types/issuance.go @@ -17,6 +17,8 @@ type IssuanceInput struct { VMVersion uint64 IssuanceProgram []byte Arguments [][]byte + + assetId bc.AssetID } // NewIssuanceInput create a new IssuanceInput struct. @@ -45,8 +47,11 @@ func (ii *IssuanceInput) AssetDefinitionHash() (defhash bc.Hash) { // AssetID calculate the assetID of the issuance input. func (ii *IssuanceInput) AssetID() bc.AssetID { - defhash := ii.AssetDefinitionHash() - return bc.ComputeAssetID(ii.IssuanceProgram, ii.VMVersion, &defhash) + if ii.assetId.IsZero() { + ii.assetId = ii.calcAssetID() + } + + return ii.assetId } // InputType is the interface function for return the input type. @@ -61,12 +66,17 @@ func (ii *IssuanceInput) NonceHash() (hash bc.Hash) { return hash } -func (ii *IssuanceInput) readCommitment(r *blockchain.Reader) (assetID bc.AssetID, err error) { +func (ii *IssuanceInput) calcAssetID() bc.AssetID { + defhash := ii.AssetDefinitionHash() + return bc.ComputeAssetID(ii.IssuanceProgram, ii.VMVersion, &defhash) +} + +func (ii *IssuanceInput) readCommitment(r *blockchain.Reader) (err error) { if ii.Nonce, err = blockchain.ReadVarstr31(r); err != nil { return } - if _, err = assetID.ReadFrom(r); err != nil { + if _, err = ii.assetId.ReadFrom(r); err != nil { return } @@ -74,7 +84,7 @@ func (ii *IssuanceInput) readCommitment(r *blockchain.Reader) (assetID bc.AssetI return } -func (ii *IssuanceInput) readWitness(r *blockchain.Reader, assetID bc.AssetID) (err error) { +func (ii *IssuanceInput) readWitness(r *blockchain.Reader) (err error) { if ii.AssetDefinition, err = blockchain.ReadVarstr31(r); err != nil { return err } @@ -87,7 +97,7 @@ func (ii *IssuanceInput) readWitness(r *blockchain.Reader, assetID bc.AssetID) ( return err } - if ii.AssetID() != assetID { + if ii.calcAssetID() != ii.assetId { return errBadAssetID } diff --git a/protocol/bc/types/spend.go b/protocol/bc/types/spend.go index dcb57341..5ed5fe4c 100644 --- a/protocol/bc/types/spend.go +++ b/protocol/bc/types/spend.go @@ -44,12 +44,12 @@ func (si *SpendInput) AssetID() bc.AssetID { // InputType is the interface function for return the input type. func (si *SpendInput) InputType() uint8 { return SpendInputType } -func (si *SpendInput) readCommitment(r *blockchain.Reader) (assetID bc.AssetID, err error) { +func (si *SpendInput) readCommitment(r *blockchain.Reader) (err error) { si.SpendCommitmentSuffix, err = si.SpendCommitment.readFrom(r, 1) return } -func (si *SpendInput) readWitness(r *blockchain.Reader, _ bc.AssetID) (err error) { +func (si *SpendInput) readWitness(r *blockchain.Reader) (err error) { si.Arguments, err = blockchain.ReadVarstrList(r) return err } diff --git a/protocol/bc/types/txinput.go b/protocol/bc/types/txinput.go index bdebc1f0..e4083568 100644 --- a/protocol/bc/types/txinput.go +++ b/protocol/bc/types/txinput.go @@ -51,8 +51,8 @@ type ( TypedInput interface { InputType() uint8 AssetID() bc.AssetID - readCommitment(*blockchain.Reader) (bc.AssetID, error) - readWitness(*blockchain.Reader, bc.AssetID) error + readCommitment(*blockchain.Reader) error + readWitness(*blockchain.Reader) error writeCommitment(io.Writer, uint64) error writeWitness(w io.Writer) error } @@ -127,7 +127,6 @@ func (t *TxInput) readFrom(r *blockchain.Reader) (err error) { return err } - var assetID bc.AssetID t.CommitmentSuffix, err = blockchain.ReadExtensibleString(r, func(r *blockchain.Reader) error { if t.AssetVersion != 1 { return nil @@ -137,8 +136,7 @@ func (t *TxInput) readFrom(r *blockchain.Reader) (err error) { return err } - assetID, err = t.readCommitment(r) - return err + return t.readCommitment(r) }) if err != nil { return err @@ -146,7 +144,7 @@ func (t *TxInput) readFrom(r *blockchain.Reader) (err error) { t.WitnessSuffix, err = blockchain.ReadExtensibleString(r, func(r *blockchain.Reader) error { if t.AssetVersion == 1 { - return t.readWitness(r, assetID) + return t.readWitness(r) } return nil diff --git a/protocol/bc/types/veto_input.go b/protocol/bc/types/veto_input.go index 5a382564..59e7f4b8 100644 --- a/protocol/bc/types/veto_input.go +++ b/protocol/bc/types/veto_input.go @@ -46,7 +46,7 @@ func (vi *VetoInput) AssetID() bc.AssetID { // InputType is the interface function for return the input type. func (vi *VetoInput) InputType() uint8 { return VetoInputType } -func (vi *VetoInput) readCommitment(r *blockchain.Reader) (assetID bc.AssetID, err error) { +func (vi *VetoInput) readCommitment(r *blockchain.Reader) (err error) { if vi.VetoCommitmentSuffix, err = vi.SpendCommitment.readFrom(r, 1); err != nil { return } @@ -55,7 +55,7 @@ func (vi *VetoInput) readCommitment(r *blockchain.Reader) (assetID bc.AssetID, e return } -func (vi *VetoInput) readWitness(r *blockchain.Reader, _ bc.AssetID) (err error) { +func (vi *VetoInput) readWitness(r *blockchain.Reader) (err error) { vi.Arguments, err = blockchain.ReadVarstrList(r) return err } -- 2.11.0