github.com/aergoio/aergo@v1.3.1/message/msghelper.go (about)

     1  package message
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  
     7  	"github.com/aergoio/aergo/types"
     8  )
     9  
    10  const RPCSvc = "RPCSvc"
    11  
    12  // Helper is helper interface for extracting tx or block from actor response
    13  type Helper interface {
    14  	// ExtractBlockFromResponseAndError get rawResponse and error and return pointer of Block
    15  	ExtractBlockFromResponseAndError(rawResponse interface{}, err error) (*types.Block, error)
    16  	ExtractBlockFromResponse(rawResponse interface{}) (*types.Block, error)
    17  	ExtractTxFromResponseAndError(rawResponse interface{}, err error) (*types.Tx, error)
    18  	ExtractTxFromResponse(rawResponse interface{}) (*types.Tx, error)
    19  	ExtractTxsFromResponseAndError(rawResponse interface{}, err error) ([]*types.Tx, error)
    20  	ExtractTxsFromResponse(rawResponse interface{}) ([]*types.Tx, error)
    21  }
    22  
    23  func GetHelper() Helper {
    24  	return &baseHelper{}
    25  }
    26  
    27  type baseHelper struct {
    28  }
    29  
    30  func (h baseHelper) ExtractBlockFromResponseAndError(rawResponse interface{}, err error) (*types.Block, error) {
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	return h.ExtractBlockFromResponse(rawResponse)
    35  }
    36  
    37  func (h baseHelper) ExtractBlockFromResponse(rawResponse interface{}) (*types.Block, error) {
    38  	var blockRsp *GetBlockRsp
    39  	switch v := rawResponse.(type) {
    40  	case GetBlockRsp:
    41  		blockRsp = &v
    42  	case GetBestBlockRsp:
    43  		blockRsp = (*GetBlockRsp)(&v)
    44  	case GetBlockByNoRsp:
    45  		blockRsp = (*GetBlockRsp)(&v)
    46  	default:
    47  		panic(fmt.Sprintf("unexpected result type %s, expected %s", reflect.TypeOf(rawResponse),
    48  			"message.GetBlockRsp"))
    49  	}
    50  	return extractBlock(blockRsp)
    51  }
    52  
    53  func extractBlock(from *GetBlockRsp) (*types.Block, error) {
    54  	if nil != from.Err {
    55  		return nil, from.Err
    56  	}
    57  	return from.Block, nil
    58  }
    59  
    60  func (h baseHelper) ExtractTxFromResponseAndError(rawResponse interface{}, err error) (*types.Tx, error) {
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	return h.ExtractTxFromResponse(rawResponse)
    65  }
    66  
    67  func (h baseHelper) ExtractTxFromResponse(rawResponse interface{}) (*types.Tx, error) {
    68  	switch v := rawResponse.(type) {
    69  	case *MemPoolExistRsp:
    70  		return v.Tx, nil
    71  	default:
    72  		panic(fmt.Sprintf("unexpected result type %s, expected %s", reflect.TypeOf(rawResponse),
    73  			"message.MemPoolExistRsp"))
    74  	}
    75  }
    76  
    77  func (h baseHelper) ExtractTxsFromResponseAndError(rawResponse interface{}, err error) ([]*types.Tx, error) {
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	return h.ExtractTxsFromResponse(rawResponse)
    82  }
    83  
    84  func (h baseHelper) ExtractTxsFromResponse(rawResponse interface{}) ([]*types.Tx, error) {
    85  	switch v := rawResponse.(type) {
    86  	case *MemPoolGetRsp:
    87  		if v.Err != nil {
    88  			return nil, v.Err
    89  		}
    90  		res := make([]*types.Tx, 0)
    91  		for _, x := range v.Txs {
    92  			res = append(res, x.GetTx())
    93  		}
    94  		return res, nil
    95  	case *MemPoolExistExRsp:
    96  		return v.Txs, nil
    97  	default:
    98  		panic(fmt.Sprintf("unexpected result type %s, expected %s", reflect.TypeOf(rawResponse),
    99  			"message.MemPoolGetRsp"))
   100  	}
   101  }