github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/protocol/bc/types/output_commitment.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  
     7  	"github.com/bytom/bytom/encoding/blockchain"
     8  	"github.com/bytom/bytom/errors"
     9  	"github.com/bytom/bytom/protocol/bc"
    10  )
    11  
    12  // OutputCommitment contains the commitment data for a transaction output.
    13  type OutputCommitment struct {
    14  	bc.AssetAmount
    15  	VMVersion      uint64
    16  	ControlProgram []byte
    17  	StateData      [][]byte
    18  }
    19  
    20  func (oc *OutputCommitment) writeTo(w io.Writer, assetVersion uint64) (err error) {
    21  	if assetVersion == 1 {
    22  		if _, err = oc.AssetAmount.WriteTo(w); err != nil {
    23  			return errors.Wrap(err, "writing asset amount")
    24  		}
    25  		if _, err = blockchain.WriteVarint63(w, oc.VMVersion); err != nil {
    26  			return errors.Wrap(err, "writing vm version")
    27  		}
    28  		if _, err = blockchain.WriteVarstr31(w, oc.ControlProgram); err != nil {
    29  			return errors.Wrap(err, "writing control program")
    30  		}
    31  		if _, err = blockchain.WriteVarstrList(w, oc.StateData); err != nil {
    32  			return errors.Wrap(err, "writing state data")
    33  		}
    34  	}
    35  	return errors.Wrap(err, "writing suffix")
    36  }
    37  
    38  func (oc *OutputCommitment) readFrom(r *blockchain.Reader, assetVersion uint64) (err error) {
    39  	if assetVersion == 1 {
    40  		if err := oc.AssetAmount.ReadFrom(r); err != nil {
    41  			return errors.Wrap(err, "reading asset+amount")
    42  		}
    43  		oc.VMVersion, err = blockchain.ReadVarint63(r)
    44  		if err != nil {
    45  			return errors.Wrap(err, "reading VM version")
    46  		}
    47  		if oc.VMVersion != 1 {
    48  			return fmt.Errorf("unrecognized VM version %d for asset version 1", oc.VMVersion)
    49  		}
    50  		oc.ControlProgram, err = blockchain.ReadVarstr31(r)
    51  		if err != nil {
    52  			return errors.Wrap(err, "reading control program")
    53  		}
    54  		oc.StateData, err = blockchain.ReadVarstrList(r)
    55  		return errors.Wrap(err, "reading state data")
    56  	}
    57  	return nil
    58  }