github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/engine/access/rest/models/transaction.go (about)

     1  package models
     2  
     3  import (
     4  	"github.com/onflow/flow-go/access"
     5  	"github.com/onflow/flow-go/engine/access/rest/util"
     6  	"github.com/onflow/flow-go/model/flow"
     7  )
     8  
     9  func (t *Transaction) Build(tx *flow.TransactionBody, txr *access.TransactionResult, link LinkGenerator) {
    10  	args := make([]string, len(tx.Arguments))
    11  	for i, arg := range tx.Arguments {
    12  		args[i] = util.ToBase64(arg)
    13  	}
    14  
    15  	auths := make([]string, len(tx.Authorizers))
    16  	for i, auth := range tx.Authorizers {
    17  		auths[i] = auth.String()
    18  	}
    19  
    20  	// if transaction result is provided then add that to the response, else add the result link to the expandable
    21  	t.Expandable = &TransactionExpandable{}
    22  	if txr != nil {
    23  		var txResult TransactionResult
    24  		txResult.Build(txr, tx.ID(), link)
    25  		t.Result = &txResult
    26  	} else {
    27  		resultLink, _ := link.TransactionResultLink(tx.ID())
    28  		t.Expandable.Result = resultLink
    29  	}
    30  
    31  	var payloadSigs TransactionSignatures
    32  	payloadSigs.Build(tx.PayloadSignatures)
    33  
    34  	var envelopeSigs TransactionSignatures
    35  	envelopeSigs.Build(tx.EnvelopeSignatures)
    36  
    37  	var proposalKey ProposalKey
    38  	proposalKey.Build(tx.ProposalKey)
    39  
    40  	t.Id = tx.ID().String()
    41  	t.Script = util.ToBase64(tx.Script)
    42  	t.Arguments = args
    43  	t.ReferenceBlockId = tx.ReferenceBlockID.String()
    44  	t.GasLimit = util.FromUint64(tx.GasLimit)
    45  	t.Payer = tx.Payer.String()
    46  	t.ProposalKey = &proposalKey
    47  	t.Authorizers = auths
    48  	t.PayloadSignatures = payloadSigs
    49  	t.EnvelopeSignatures = envelopeSigs
    50  
    51  	self, _ := SelfLink(tx.ID(), link.TransactionLink)
    52  	t.Links = self
    53  }
    54  
    55  type Transactions []Transaction
    56  
    57  func (t *Transactions) Build(transactions []*flow.TransactionBody, link LinkGenerator) {
    58  	txs := make([]Transaction, len(transactions))
    59  	for i, tr := range transactions {
    60  		var tx Transaction
    61  		tx.Build(tr, nil, link)
    62  		txs[i] = tx
    63  	}
    64  
    65  	*t = txs
    66  }
    67  
    68  type TransactionSignatures []TransactionSignature
    69  
    70  func (t *TransactionSignatures) Build(signatures []flow.TransactionSignature) {
    71  	sigs := make([]TransactionSignature, len(signatures))
    72  	for i, s := range signatures {
    73  		var sig TransactionSignature
    74  		sig.Build(s)
    75  		sigs[i] = sig
    76  	}
    77  
    78  	*t = sigs
    79  }
    80  
    81  func (t *TransactionSignature) Build(sig flow.TransactionSignature) {
    82  	t.Address = sig.Address.String()
    83  	t.KeyIndex = util.FromUint64(sig.KeyIndex)
    84  	t.Signature = util.ToBase64(sig.Signature)
    85  }
    86  
    87  func (t *TransactionResult) Build(txr *access.TransactionResult, txID flow.Identifier, link LinkGenerator) {
    88  	var status TransactionStatus
    89  	status.Build(txr.Status)
    90  
    91  	var execution TransactionExecution
    92  	execution.Build(txr)
    93  
    94  	var events Events
    95  	events.Build(txr.Events)
    96  
    97  	if txr.BlockID != flow.ZeroID { // don't send back 0 ID
    98  		t.BlockId = txr.BlockID.String()
    99  	}
   100  
   101  	if txr.CollectionID != flow.ZeroID { // don't send back 0 ID
   102  		t.CollectionId = txr.CollectionID.String()
   103  	}
   104  
   105  	t.Status = &status
   106  	t.Execution = &execution
   107  	t.StatusCode = int32(txr.StatusCode)
   108  	t.ErrorMessage = txr.ErrorMessage
   109  	t.ComputationUsed = util.FromUint64(0) // todo: define this
   110  	t.Events = events
   111  
   112  	self, _ := SelfLink(txID, link.TransactionResultLink)
   113  	t.Links = self
   114  }
   115  
   116  func (t *TransactionStatus) Build(status flow.TransactionStatus) {
   117  	switch status {
   118  	case flow.TransactionStatusExpired:
   119  		*t = EXPIRED
   120  	case flow.TransactionStatusExecuted:
   121  		*t = EXECUTED
   122  	case flow.TransactionStatusFinalized:
   123  		*t = FINALIZED
   124  	case flow.TransactionStatusSealed:
   125  		*t = SEALED
   126  	case flow.TransactionStatusPending:
   127  		*t = PENDING
   128  	default:
   129  		*t = ""
   130  	}
   131  }
   132  
   133  func (t *TransactionExecution) Build(result *access.TransactionResult) {
   134  	*t = PENDING_RESULT
   135  
   136  	if result.Status == flow.TransactionStatusSealed && result.ErrorMessage == "" {
   137  		*t = SUCCESS_RESULT
   138  	}
   139  	if result.ErrorMessage != "" {
   140  		*t = FAILURE_RESULT
   141  	}
   142  	if result.Status == flow.TransactionStatusExpired {
   143  		*t = FAILURE_RESULT
   144  	}
   145  }
   146  
   147  func (p *ProposalKey) Build(key flow.ProposalKey) {
   148  	p.Address = key.Address.String()
   149  	p.KeyIndex = util.FromUint64(key.KeyIndex)
   150  	p.SequenceNumber = util.FromUint64(key.SequenceNumber)
   151  }