github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/protocol/bc/tx_status.go (about)

     1  package bc
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"io"
     7  )
     8  
     9  const transactionStatusVersion = 1
    10  
    11  // NewTransactionStatus create a empty TransactionStatus struct
    12  func NewTransactionStatus() *TransactionStatus {
    13  	return &TransactionStatus{
    14  		Version:      transactionStatusVersion,
    15  		VerifyStatus: []*TxVerifyResult{},
    16  	}
    17  }
    18  
    19  // SetStatus set the tx status of given index
    20  func (ts *TransactionStatus) SetStatus(i int, gasOnly bool) error {
    21  	if i > len(ts.VerifyStatus) {
    22  		return errors.New("setStatus should be set one by one")
    23  	}
    24  
    25  	if i == len(ts.VerifyStatus) {
    26  		ts.VerifyStatus = append(ts.VerifyStatus, &TxVerifyResult{StatusFail: gasOnly})
    27  	} else {
    28  		ts.VerifyStatus[i].StatusFail = gasOnly
    29  	}
    30  	return nil
    31  }
    32  
    33  // GetStatus get the tx status of given index
    34  func (ts *TransactionStatus) GetStatus(i int) (bool, error) {
    35  	if i >= len(ts.VerifyStatus) {
    36  		return false, errors.New("GetStatus is out of range")
    37  	}
    38  
    39  	return ts.VerifyStatus[i].StatusFail, nil
    40  }
    41  
    42  // WriteTo will write TxVerifyResult struct to io.Writer
    43  func (tvr *TxVerifyResult) WriteTo(w io.Writer) (int64, error) {
    44  	bytes, err := json.Marshal(tvr)
    45  	if err != nil {
    46  		return 0, err
    47  	}
    48  
    49  	n, err := w.Write(bytes)
    50  	return int64(n), err
    51  }