github.com/turingchain2020/turingchain@v1.1.21/client/mock_blockchain_test.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package client_test
     6  
     7  import (
     8  	"bytes"
     9  
    10  	"github.com/turingchain2020/turingchain/queue"
    11  	"github.com/turingchain2020/turingchain/types"
    12  )
    13  
    14  type mockBlockChain struct {
    15  }
    16  
    17  func (m *mockBlockChain) SetQueueClient(q queue.Queue) {
    18  	go func() {
    19  		blockchainKey := "blockchain"
    20  		client := q.Client()
    21  		client.Sub(blockchainKey)
    22  		for msg := range client.Recv() {
    23  			switch msg.Ty {
    24  			case types.EventGetBlocks:
    25  				if req, ok := msg.GetData().(*types.ReqBlocks); ok {
    26  					if req.Start == 1 {
    27  						msg.Reply(client.NewMessage(blockchainKey, types.EventBlocks, &types.Transaction{}))
    28  					} else {
    29  						msg.Reply(client.NewMessage(blockchainKey, types.EventBlocks, &types.BlockDetails{}))
    30  					}
    31  				} else {
    32  					msg.ReplyErr("Do not support", types.ErrInvalidParam)
    33  				}
    34  			case types.EventGetTransactionByAddr:
    35  				if req, ok := msg.GetData().(*types.ReqAddr); ok {
    36  					if req.Flag == 1 {
    37  						msg.Reply(client.NewMessage(blockchainKey, types.EventBlocks, &types.Transaction{}))
    38  					} else {
    39  						msg.Reply(client.NewMessage(blockchainKey, types.EventReplyTxInfo, &types.ReplyTxInfos{}))
    40  					}
    41  				} else {
    42  					msg.ReplyErr("Do not support", types.ErrInvalidParam)
    43  				}
    44  			case types.EventQueryTx:
    45  				if req, ok := msg.GetData().(*types.ReqHash); ok {
    46  					if bytes.Equal(req.Hash, []byte("case1")) {
    47  						msg.Reply(client.NewMessage(blockchainKey, types.EventTransactionDetail, &types.Transaction{}))
    48  					} else {
    49  						msg.Reply(client.NewMessage(blockchainKey, types.EventTransactionDetail, &types.TransactionDetail{}))
    50  					}
    51  				} else {
    52  					msg.ReplyErr("Do not support", types.ErrInvalidParam)
    53  				}
    54  			case types.EventGetTransactionByHash:
    55  				if req, ok := msg.GetData().(*types.ReqHashes); ok {
    56  					if len(req.GetHashes()) > 0 && bytes.Equal(req.Hashes[0], []byte("case1")) {
    57  						msg.Reply(client.NewMessage(blockchainKey, types.EventTransactionDetails, &types.Transaction{}))
    58  					} else {
    59  						msg.Reply(client.NewMessage(blockchainKey, types.EventTransactionDetails, &types.TransactionDetails{}))
    60  					}
    61  				} else {
    62  					msg.ReplyErr("Do not support", types.ErrInvalidParam)
    63  				}
    64  			case types.EventGetHeaders:
    65  				if req, ok := msg.GetData().(*types.ReqBlocks); ok {
    66  					if req.Start == 10 {
    67  						msg.Reply(client.NewMessage(blockchainKey, types.EventHeaders, &types.Transaction{}))
    68  					} else {
    69  						msg.Reply(client.NewMessage(blockchainKey, types.EventHeaders, &types.Headers{}))
    70  					}
    71  				} else {
    72  					msg.ReplyErr("Do not support", types.ErrInvalidParam)
    73  				}
    74  			case types.EventGetBlockOverview:
    75  				if req, ok := msg.GetData().(*types.ReqHash); ok {
    76  					if bytes.Equal(req.Hash, []byte("case1")) {
    77  						msg.Reply(client.NewMessage(blockchainKey, types.EventReplyBlockOverview, &types.Transaction{}))
    78  					} else {
    79  						msg.Reply(client.NewMessage(blockchainKey, types.EventReplyBlockOverview, &types.BlockOverview{}))
    80  					}
    81  				} else {
    82  					msg.ReplyErr("Do not support", types.ErrInvalidParam)
    83  				}
    84  			case types.EventGetAddrOverview:
    85  				if req, ok := msg.GetData().(*types.ReqAddr); ok {
    86  					if req.Addr == "case1" {
    87  						msg.Reply(client.NewMessage(blockchainKey, types.EventReplyAddrOverview, &types.Transaction{}))
    88  					} else {
    89  						msg.Reply(client.NewMessage(blockchainKey, types.EventReplyAddrOverview, &types.AddrOverview{}))
    90  					}
    91  				} else {
    92  					msg.ReplyErr("Do not support", types.ErrInvalidParam)
    93  				}
    94  			case types.EventGetBlockHash:
    95  				if req, ok := msg.GetData().(*types.ReqInt); ok {
    96  					if req.Height == 10 {
    97  						msg.Reply(client.NewMessage(blockchainKey, types.EventBlockHash, &types.Transaction{}))
    98  					} else {
    99  						msg.Reply(client.NewMessage(blockchainKey, types.EventBlockHash, &types.ReplyHash{}))
   100  					}
   101  				} else {
   102  					msg.ReplyErr("Do not support", types.ErrInvalidParam)
   103  				}
   104  
   105  			case types.EventGetSeqByHash:
   106  				msg.Reply(client.NewMessage(blockchainKey, types.EventReplyQuery, &types.Int64{Data: 1}))
   107  			case types.EventGetBlockBySeq:
   108  				if req, ok := msg.GetData().(*types.Int64); ok {
   109  					// just for cover
   110  					if req.Data == 10 {
   111  						msg.Reply(client.NewMessage(blockchainKey, types.EventReplyQuery, &types.Reply{IsOk: false, Msg: []byte("not support")}))
   112  					}
   113  					msg.Reply(client.NewMessage(blockchainKey, types.EventReplyQuery, &types.BlockSeq{Num: 1}))
   114  				}
   115  			case types.EventIsSync:
   116  				msg.Reply(client.NewMessage(blockchainKey, types.EventReplyIsSync, &types.IsCaughtUp{}))
   117  			case types.EventIsNtpClockSync:
   118  				msg.Reply(client.NewMessage(blockchainKey, types.EventReplyIsNtpClockSync, &types.IsNtpClockSync{}))
   119  			case types.EventGetLastHeader:
   120  				msg.Reply(client.NewMessage(blockchainKey, types.EventHeader, &types.Header{}))
   121  			case types.EventLocalGet:
   122  				if req, ok := msg.GetData().(*types.LocalDBGet); ok {
   123  					if len(req.Keys) > 0 && bytes.Equal(req.Keys[0], []byte("TotalFeeKey:case1")) {
   124  						msg.Reply(client.NewMessage(blockchainKey, types.EventReplyQuery, &types.Transaction{}))
   125  					} else {
   126  						msg.Reply(client.NewMessage(blockchainKey, types.EventReplyQuery, &types.LocalReplyValue{}))
   127  					}
   128  				} else {
   129  					msg.ReplyErr("Do not support", types.ErrInvalidParam)
   130  				}
   131  			case types.EventLocalList:
   132  				if _, ok := msg.GetData().(*types.LocalDBList); ok {
   133  					msg.Reply(client.NewMessage(blockchainKey, types.EventReplyQuery, &types.LocalReplyValue{}))
   134  				} else {
   135  					msg.ReplyErr("Do not support", types.ErrInvalidParam)
   136  				}
   137  			case types.EventLocalNew:
   138  				msg.Reply(client.NewMessage(blockchainKey, types.EventLocalNew, &types.Int64{Data: 9999}))
   139  			case types.EventLocalClose:
   140  				msg.Reply(client.NewMessage(blockchainKey, types.EventLocalClose, nil))
   141  			case types.EventLocalBegin:
   142  				if req, ok := msg.GetData().(*types.Int64); ok && req.Data == 9999 {
   143  					msg.Reply(client.NewMessage(blockchainKey, types.EventLocalBegin, nil))
   144  				} else {
   145  					msg.ReplyErr("transaction id must 9999", types.ErrInvalidParam)
   146  				}
   147  			case types.EventLocalCommit:
   148  				if req, ok := msg.GetData().(*types.Int64); ok && req.Data == 9999 {
   149  					msg.Reply(client.NewMessage(blockchainKey, types.EventLocalCommit, nil))
   150  				} else {
   151  					msg.ReplyErr("transaction id must 9999", types.ErrInvalidParam)
   152  				}
   153  			case types.EventLocalRollback:
   154  				if req, ok := msg.GetData().(*types.Int64); ok && req.Data == 9999 {
   155  					msg.Reply(client.NewMessage(blockchainKey, types.EventLocalRollback, nil))
   156  				} else {
   157  					msg.ReplyErr("transaction id must 9999", types.ErrInvalidParam)
   158  				}
   159  			case types.EventLocalSet:
   160  				if req, ok := msg.GetData().(*types.LocalDBSet); ok && req.Txid == 9999 {
   161  					msg.Reply(client.NewMessage(blockchainKey, types.EventLocalSet, nil))
   162  				} else {
   163  					msg.ReplyErr("transaction id must 9999", types.ErrInvalidParam)
   164  				}
   165  			case types.EventGetMainSeqByHash:
   166  				if req, ok := msg.GetData().(*types.ReqHash); ok && string(req.Hash) == "exist-hash" {
   167  					msg.Reply(client.NewMessage(blockchainKey, types.EventReplyMainSeqByHash, &types.Int64{Data: 9999}))
   168  				} else {
   169  					msg.ReplyErr("transaction hash is not exist-hash", types.ErrInvalidParam)
   170  				}
   171  			case types.EventGetLastBlockMainSequence:
   172  				if _, ok := msg.GetData().(*types.ReqNil); ok {
   173  					msg.Reply(client.NewMessage(blockchainKey, types.EventReplyLastBlockMainSequence, &types.Int64{Data: 9999}))
   174  				} else {
   175  					msg.ReplyErr("request must be nil", types.ErrInvalidParam)
   176  				}
   177  
   178  			case types.EventGetParaTxByTitle:
   179  				if req, ok := msg.GetData().(*types.ReqParaTxByTitle); ok {
   180  					// just for cover
   181  					if req.Title == "user" {
   182  						msg.Reply(client.NewMessage(blockchainKey, types.EventReplyParaTxByTitle, &types.Reply{IsOk: false, Msg: []byte("not support")}))
   183  					} else {
   184  						msg.Reply(client.NewMessage(blockchainKey, types.EventReplyParaTxByTitle, &types.ParaTxDetails{}))
   185  					}
   186  				}
   187  			case types.EventGetHeightByTitle:
   188  				if req, ok := msg.GetData().(*types.ReqHeightByTitle); ok {
   189  					// just for cover
   190  					if req.Title == "user" {
   191  						msg.Reply(client.NewMessage(blockchainKey, types.EventReplyHeightByTitle, &types.Reply{IsOk: false, Msg: []byte("not support")}))
   192  					} else {
   193  						msg.Reply(client.NewMessage(blockchainKey, types.EventReplyHeightByTitle, &types.ReplyHeightByTitle{}))
   194  					}
   195  				}
   196  			case types.EventGetParaTxByTitleAndHeight:
   197  				if req, ok := msg.GetData().(*types.ReqParaTxByHeight); ok {
   198  					// just for cover
   199  					if req.Title == "user" {
   200  						msg.Reply(client.NewMessage(blockchainKey, types.EventReplyParaTxByTitle, &types.Reply{IsOk: false, Msg: []byte("not support")}))
   201  					} else {
   202  						msg.Reply(client.NewMessage(blockchainKey, types.EventReplyParaTxByTitle, &types.ParaTxDetails{}))
   203  					}
   204  				}
   205  			case types.EventGetLastBlockSequence:
   206  				msg.Reply(client.NewMessage(blockchainKey, types.EventReplyLastBlockSequence, &types.Int64{}))
   207  			case types.EventGetBlockByHashes:
   208  				msg.Reply(client.NewMessage(blockchainKey, types.EventReplyLastBlockSequence, &types.BlockDetails{}))
   209  			case types.EventGetBlockSequences:
   210  				msg.Reply(client.NewMessage(blockchainKey, types.EventReplyBlockSequences, &types.BlockSequences{}))
   211  			case types.EventSubscribePush:
   212  				msg.Reply(client.NewMessage(blockchainKey, types.EventReplySubscribePush, &types.ReplySubscribePush{}))
   213  			case types.EventListPushes:
   214  				msg.Reply(client.NewMessage(blockchainKey, types.EventReplyQuery, &types.PushSubscribes{}))
   215  			case types.EventGetPushLastNum:
   216  				msg.Reply(client.NewMessage(blockchainKey, types.EventReplyQuery, &types.Int64{}))
   217  			default:
   218  				msg.ReplyErr("Do not support", types.ErrNotSupport)
   219  			}
   220  		}
   221  	}()
   222  }
   223  
   224  func (m *mockBlockChain) Close() {
   225  }