github.com/Finschia/finschia-sdk@v0.48.1/server/mock/tx.go (about)

     1  // nolint
     2  package mock
     3  
     4  import (
     5  	"bytes"
     6  	"fmt"
     7  
     8  	sdk "github.com/Finschia/finschia-sdk/types"
     9  	sdkerrors "github.com/Finschia/finschia-sdk/types/errors"
    10  )
    11  
    12  // An sdk.Tx which is its own sdk.Msg.
    13  type kvstoreTx struct {
    14  	key   []byte
    15  	value []byte
    16  	bytes []byte
    17  }
    18  
    19  // dummy implementation of proto.Message
    20  func (msg kvstoreTx) Reset()         {}
    21  func (msg kvstoreTx) String() string { return "TODO" }
    22  func (msg kvstoreTx) ProtoMessage()  {}
    23  
    24  var (
    25  	_ sdk.Tx  = kvstoreTx{}
    26  	_ sdk.Msg = kvstoreTx{}
    27  )
    28  
    29  func NewTx(key, value string) kvstoreTx {
    30  	bytes := fmt.Sprintf("%s=%s", key, value)
    31  	return kvstoreTx{
    32  		key:   []byte(key),
    33  		value: []byte(value),
    34  		bytes: []byte(bytes),
    35  	}
    36  }
    37  
    38  func (tx kvstoreTx) Route() string {
    39  	return "kvstore"
    40  }
    41  
    42  func (tx kvstoreTx) Type() string {
    43  	return "kvstore_tx"
    44  }
    45  
    46  func (tx kvstoreTx) GetMsgs() []sdk.Msg {
    47  	return []sdk.Msg{tx}
    48  }
    49  
    50  func (tx kvstoreTx) GetMemo() string {
    51  	return ""
    52  }
    53  
    54  func (tx kvstoreTx) GetSignBytes() []byte {
    55  	return tx.bytes
    56  }
    57  
    58  // Should the app be calling this? Or only handlers?
    59  func (tx kvstoreTx) ValidateBasic() error {
    60  	return nil
    61  }
    62  
    63  func (tx kvstoreTx) GetSigners() []sdk.AccAddress {
    64  	return nil
    65  }
    66  
    67  // takes raw transaction bytes and decodes them into an sdk.Tx. An sdk.Tx has
    68  // all the signatures and can be used to authenticate.
    69  func decodeTx(txBytes []byte) (sdk.Tx, error) {
    70  	var tx sdk.Tx
    71  
    72  	split := bytes.Split(txBytes, []byte("="))
    73  	if len(split) == 1 {
    74  		k := split[0]
    75  		tx = kvstoreTx{k, k, txBytes}
    76  	} else if len(split) == 2 {
    77  		k, v := split[0], split[1]
    78  		tx = kvstoreTx{k, v, txBytes}
    79  	} else {
    80  		return nil, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "too many '='")
    81  	}
    82  
    83  	return tx, nil
    84  }