From ec3f0494a6d3ff946f48722bf38ec05685e6cda8 Mon Sep 17 00:00:00 2001 From: Paladz Date: Fri, 11 Jun 2021 10:15:24 +0800 Subject: [PATCH] edit types (#1961) Co-authored-by: paladz --- protocol/bc/types/coinbase.go | 2 +- protocol/bc/types/issuance.go | 3 +++ protocol/bc/types/spend.go | 2 +- protocol/bc/types/transaction.go | 11 +++++++++- protocol/bc/types/txinput.go | 2 -- protocol/bc/types/txoutput.go | 43 ++++++++++++++++++++-------------------- 6 files changed, 36 insertions(+), 27 deletions(-) diff --git a/protocol/bc/types/coinbase.go b/protocol/bc/types/coinbase.go index 83e306b2..ec0d8c36 100644 --- a/protocol/bc/types/coinbase.go +++ b/protocol/bc/types/coinbase.go @@ -30,7 +30,7 @@ func (cb *CoinbaseInput) InputType() uint8 { return CoinbaseInputType } func (cb *CoinbaseInput) readCommitment(r *blockchain.Reader) (err error) { cb.Arbitrary, err = blockchain.ReadVarstr31(r) - return + return err } func (cb *CoinbaseInput) readWitness(_ *blockchain.Reader) error { return nil } diff --git a/protocol/bc/types/issuance.go b/protocol/bc/types/issuance.go index 7bf73cf5..e1d84be6 100644 --- a/protocol/bc/types/issuance.go +++ b/protocol/bc/types/issuance.go @@ -5,9 +5,12 @@ import ( "github.com/bytom/bytom/crypto/sha3pool" "github.com/bytom/bytom/encoding/blockchain" + "github.com/bytom/bytom/errors" "github.com/bytom/bytom/protocol/bc" ) +var errBadAssetID = errors.New("asset ID does not match other issuance parameters") + // IssuanceInput satisfies the TypedInput interface and represents a issuance. type IssuanceInput struct { Nonce []byte diff --git a/protocol/bc/types/spend.go b/protocol/bc/types/spend.go index 5ed5fe4c..6072765a 100644 --- a/protocol/bc/types/spend.go +++ b/protocol/bc/types/spend.go @@ -46,7 +46,7 @@ func (si *SpendInput) InputType() uint8 { return SpendInputType } func (si *SpendInput) readCommitment(r *blockchain.Reader) (err error) { si.SpendCommitmentSuffix, err = si.SpendCommitment.readFrom(r, 1) - return + return err } func (si *SpendInput) readWitness(r *blockchain.Reader) (err error) { diff --git a/protocol/bc/types/transaction.go b/protocol/bc/types/transaction.go index d36fb9a5..68b4e7e5 100644 --- a/protocol/bc/types/transaction.go +++ b/protocol/bc/types/transaction.go @@ -53,6 +53,8 @@ func (tx *Tx) SetInputArguments(n uint32, args [][]byte) { e.WitnessArguments = args case *bc.Spend: e.WitnessArguments = args + case *bc.VetoInput: + e.WitnessArguments = args } } @@ -101,6 +103,7 @@ func (tx *TxData) readFrom(r *blockchain.Reader) (err error) { if _, err = io.ReadFull(r, serflags[:]); err != nil { return errors.Wrap(err, "reading serialization flags") } + if serflags[0] != serRequired { return fmt.Errorf("unsupported serflags %#x", serflags[0]) } @@ -108,6 +111,7 @@ func (tx *TxData) readFrom(r *blockchain.Reader) (err error) { if tx.Version, err = blockchain.ReadVarint63(r); err != nil { return errors.Wrap(err, "reading transaction version") } + if tx.TimeRange, err = blockchain.ReadVarint63(r); err != nil { return err } @@ -122,6 +126,7 @@ func (tx *TxData) readFrom(r *blockchain.Reader) (err error) { if err = ti.readFrom(r); err != nil { return errors.Wrapf(err, "reading input %d", len(tx.Inputs)) } + tx.Inputs = append(tx.Inputs, ti) } @@ -135,8 +140,10 @@ func (tx *TxData) readFrom(r *blockchain.Reader) (err error) { if err = to.readFrom(r); err != nil { return errors.Wrapf(err, "reading output %d", len(tx.Outputs)) } + tx.Outputs = append(tx.Outputs, to) } + tx.SerializedSize = uint64(startSerializedSize - r.Len()) return nil } @@ -147,6 +154,7 @@ func (tx *TxData) WriteTo(w io.Writer) (int64, error) { if err := tx.writeTo(ew, serRequired); err != nil { return 0, err } + return ew.Written(), ew.Err() } @@ -154,9 +162,11 @@ func (tx *TxData) writeTo(w io.Writer, serflags byte) error { if _, err := w.Write([]byte{serflags}); err != nil { return errors.Wrap(err, "writing serialization flags") } + if _, err := blockchain.WriteVarint63(w, tx.Version); err != nil { return errors.Wrap(err, "writing transaction version") } + if _, err := blockchain.WriteVarint63(w, tx.TimeRange); err != nil { return errors.Wrap(err, "writing transaction maxtime") } @@ -182,4 +192,3 @@ func (tx *TxData) writeTo(w io.Writer, serflags byte) error { } return nil } - diff --git a/protocol/bc/types/txinput.go b/protocol/bc/types/txinput.go index e4083568..446d418a 100644 --- a/protocol/bc/types/txinput.go +++ b/protocol/bc/types/txinput.go @@ -58,8 +58,6 @@ type ( } ) -var errBadAssetID = errors.New("asset ID does not match other issuance parameters") - // Amount return the asset amount of the txinput func (t *TxInput) Amount() uint64 { switch inp := t.TypedInput.(type) { diff --git a/protocol/bc/types/txoutput.go b/protocol/bc/types/txoutput.go index 8e62ab6a..a94bb8f4 100644 --- a/protocol/bc/types/txoutput.go +++ b/protocol/bc/types/txoutput.go @@ -33,6 +33,25 @@ type TypedOutput interface { writeTo(io.Writer) error } +var outputTypeMap = map[uint8]func() TypedOutput{ + OriginalOutputType: func() TypedOutput { return &originalTxOutput{} }, + VoteOutputType: func() TypedOutput { return &VoteOutput{} }, +} + +func parseTypedOutput(r *blockchain.Reader) (TypedOutput, error) { + var outType [1]byte + if _, err := io.ReadFull(r, outType[:]); err != nil { + return nil, errors.Wrap(err, "reading output type") + } + + newOutFun, ok := outputTypeMap[outType[0]] + if !ok { + return nil, fmt.Errorf("unsupported output type %d", outType[0]) + } + + return newOutFun(), nil +} + func (to *TxOutput) readFrom(r *blockchain.Reader) (err error) { if to.AssetVersion, err = blockchain.ReadVarint63(r); err != nil { return errors.Wrap(err, "reading asset version") @@ -58,25 +77,6 @@ func (to *TxOutput) readFrom(r *blockchain.Reader) (err error) { return errors.Wrap(err, "reading output witness") } -var outputTypeMap = map[uint8]func() TypedOutput{ - OriginalOutputType: func() TypedOutput { return &originalTxOutput{} }, - VoteOutputType: func() TypedOutput { return &VoteOutput{} }, -} - -func parseTypedOutput(r *blockchain.Reader) (TypedOutput, error) { - var outType [1]byte - if _, err := io.ReadFull(r, outType[:]); err != nil { - return nil, errors.Wrap(err, "reading output type") - } - - newOutFun, ok := outputTypeMap[outType[0]] - if !ok { - return nil, fmt.Errorf("unsupported output type %d", outType[0]) - } - - return newOutFun(), nil -} - func (to *TxOutput) writeTo(w io.Writer) error { if _, err := blockchain.WriteVarint63(w, to.AssetVersion); err != nil { return errors.Wrap(err, "writing asset version") @@ -115,8 +115,7 @@ func ComputeOutputID(sc *SpendCommitment) (h bc.Hash, err error) { Value: &sc.AssetAmount, Position: sc.SourcePosition, } - o := bc.NewOutput(src, &bc.Program{VmVersion: sc.VMVersion, Code: sc.ControlProgram}, &bc.StateData{StateData: sc.StateData}, 0) - h = bc.EntryID(o) - return h, nil + o := bc.NewOutput(src, &bc.Program{VmVersion: sc.VMVersion, Code: sc.ControlProgram}, &bc.StateData{StateData: sc.StateData}, 0) + return bc.EntryID(o), nil } -- 2.11.0