github.com/turingchain2020/turingchain@v1.1.21/system/dapp/manage/executor/rpc_test.go (about)

     1  package executor
     2  
     3  import (
     4  	"testing"
     5  
     6  	rpctypes "github.com/turingchain2020/turingchain/rpc/types"
     7  	"github.com/turingchain2020/turingchain/types"
     8  	"github.com/turingchain2020/turingchain/util"
     9  	"github.com/turingchain2020/turingchain/util/testnode"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestManageConfig(t *testing.T) {
    14  	cfg := testnode.GetDefaultConfig()
    15  	mocker := testnode.NewWithConfig(cfg, nil)
    16  	defer mocker.Close()
    17  	mocker.Listen()
    18  	err := mocker.SendHot()
    19  	assert.Nil(t, err)
    20  	//创建黑名单
    21  	// -o add -v TRC
    22  	create := &types.ModifyConfig{
    23  		Key:   "token-blacklist",
    24  		Op:    "add",
    25  		Value: "TRC",
    26  		Addr:  "",
    27  	}
    28  	jsondata := types.MustPBToJSON(create)
    29  	/*
    30  	  {
    31  	  		"execer": "manage",
    32  	  		"actionName": "Modify",
    33  	  		"payload": {
    34  	  			"key": "token-blacklist",
    35  	  			"value": "TRC",
    36  	  			"op": "add",
    37  	  			"addr": ""
    38  	  		}
    39  	  	}
    40  	*/
    41  	req := &rpctypes.CreateTxIn{
    42  		Execer:     "manage",
    43  		ActionName: "Modify",
    44  		Payload:    jsondata,
    45  	}
    46  	var txhex string
    47  	err = mocker.GetJSONC().Call("Turingchain.CreateTransaction", req, &txhex)
    48  	assert.Nil(t, err)
    49  	hash, err := mocker.SendAndSign(mocker.GetHotKey(), txhex)
    50  	assert.Nil(t, err)
    51  	txinfo, err := mocker.WaitTx(hash)
    52  	assert.Nil(t, err)
    53  	assert.Equal(t, txinfo.Receipt.Ty, int32(2))
    54  
    55  	create = &types.ModifyConfig{
    56  		Key:   "token-blacklist",
    57  		Op:    "add",
    58  		Value: "YCC",
    59  		Addr:  "",
    60  	}
    61  	jsondata = types.MustPBToJSON(create)
    62  	req = &rpctypes.CreateTxIn{
    63  		Execer:     "manage",
    64  		ActionName: "Modify",
    65  		Payload:    jsondata,
    66  	}
    67  	err = mocker.GetJSONC().Call("Turingchain.CreateTransaction", req, &txhex)
    68  	assert.Nil(t, err)
    69  	hash, err = mocker.SendAndSign(mocker.GetHotKey(), txhex)
    70  	assert.Nil(t, err)
    71  	txinfo, err = mocker.WaitTx(hash)
    72  	assert.Nil(t, err)
    73  	assert.Equal(t, txinfo.Receipt.Ty, int32(2))
    74  
    75  	create = &types.ModifyConfig{
    76  		Key:   "token-blacklist",
    77  		Op:    "add",
    78  		Value: "TTT",
    79  		Addr:  "",
    80  	}
    81  	jsondata = types.MustPBToJSON(create)
    82  	req = &rpctypes.CreateTxIn{
    83  		Execer:     "manage",
    84  		ActionName: "Modify",
    85  		Payload:    jsondata,
    86  	}
    87  	err = mocker.GetJSONC().Call("Turingchain.CreateTransaction", req, &txhex)
    88  	assert.Nil(t, err)
    89  	hash, err = mocker.SendAndSign(mocker.GetHotKey(), txhex)
    90  	assert.Nil(t, err)
    91  	txinfo, err = mocker.WaitTx(hash)
    92  	assert.Nil(t, err)
    93  	assert.Equal(t, txinfo.Receipt.Ty, int32(2))
    94  	//做一个查询
    95  	/*
    96  		{
    97  			"execer": "manage",
    98  			"funcName": "GetConfigItem",
    99  			"payload": {
   100  				"data": "token-blacklist"
   101  			}
   102  		}
   103  	*/
   104  	queryreq := &types.ReqString{
   105  		Data: "token-blacklist",
   106  	}
   107  	query := &rpctypes.Query4Jrpc{
   108  		Execer:   "manage",
   109  		FuncName: "GetConfigItem",
   110  		Payload:  types.MustPBToJSON(queryreq),
   111  	}
   112  	var reply types.ReplyConfig
   113  	err = mocker.GetJSONC().Call("Turingchain.Query", query, &reply)
   114  	assert.Nil(t, err)
   115  	assert.Equal(t, reply.Key, "token-blacklist")
   116  	assert.Equal(t, reply.Value, "[TRC YCC TTT]")
   117  
   118  	create = &types.ModifyConfig{
   119  		Key:   "token-blacklist",
   120  		Op:    "delete",
   121  		Value: "TTT",
   122  		Addr:  "",
   123  	}
   124  	jsondata = types.MustPBToJSON(create)
   125  	req = &rpctypes.CreateTxIn{
   126  		Execer:     "manage",
   127  		ActionName: "Modify",
   128  		Payload:    jsondata,
   129  	}
   130  	err = mocker.GetJSONC().Call("Turingchain.CreateTransaction", req, &txhex)
   131  	assert.Nil(t, err)
   132  	hash, err = mocker.SendAndSign(mocker.GetHotKey(), txhex)
   133  	assert.Nil(t, err)
   134  	txinfo, err = mocker.WaitTx(hash)
   135  	assert.Nil(t, err)
   136  	util.JSONPrint(t, txinfo)
   137  	assert.Equal(t, txinfo.Receipt.Ty, int32(2))
   138  
   139  	queryreq = &types.ReqString{
   140  		Data: "token-blacklist",
   141  	}
   142  	query = &rpctypes.Query4Jrpc{
   143  		Execer:   "manage",
   144  		FuncName: "GetConfigItem",
   145  		Payload:  types.MustPBToJSON(queryreq),
   146  	}
   147  	err = mocker.GetJSONC().Call("Turingchain.Query", query, &reply)
   148  	assert.Nil(t, err)
   149  	assert.Equal(t, reply.Key, "token-blacklist")
   150  	assert.Equal(t, reply.Value, "[TRC YCC]")
   151  }
   152  
   153  func TestTokenFinisher(t *testing.T) {
   154  	cfg := testnode.GetDefaultConfig()
   155  	mocker := testnode.NewWithConfig(cfg, nil)
   156  	defer mocker.Close()
   157  	mocker.Listen()
   158  	err := mocker.SendHot()
   159  	assert.Nil(t, err)
   160  	//添加发币审核地址
   161  	create := &types.ModifyConfig{
   162  		Key:   "token-finisher",
   163  		Op:    "add",
   164  		Value: "1FCX9XJTZXvZteagTrefJEBPZMt8BFmdoi",
   165  		Addr:  "",
   166  	}
   167  	jsondata := types.MustPBToJSON(create)
   168  	/*
   169  	  {
   170  	  		"execer": "manage",
   171  	  		"actionName": "Modify",
   172  	  		"payload": {
   173  	  			"key": "token-finisher",
   174  	  			"value": "1FCX9XJTZXvZteagTrefJEBPZMt8BFmdoi",
   175  	  			"op": "add",
   176  	  			"addr": ""
   177  	  		}
   178  	  	}
   179  	*/
   180  	req := &rpctypes.CreateTxIn{
   181  		Execer:     "manage",
   182  		ActionName: "Modify",
   183  		Payload:    jsondata,
   184  	}
   185  	var txhex string
   186  	err = mocker.GetJSONC().Call("Turingchain.CreateTransaction", req, &txhex)
   187  	assert.Nil(t, err)
   188  	hash, err := mocker.SendAndSign(mocker.GetHotKey(), txhex)
   189  	assert.Nil(t, err)
   190  	txinfo, err := mocker.WaitTx(hash)
   191  	assert.Nil(t, err)
   192  
   193  	assert.Equal(t, txinfo.Receipt.Ty, int32(2))
   194  
   195  	queryreq := &types.ReqString{
   196  		Data: "token-finisher",
   197  	}
   198  	query := &rpctypes.Query4Jrpc{
   199  		Execer:   "manage",
   200  		FuncName: "GetConfigItem",
   201  		Payload:  types.MustPBToJSON(queryreq),
   202  	}
   203  	var reply types.ReplyConfig
   204  	err = mocker.GetJSONC().Call("Turingchain.Query", query, &reply)
   205  	assert.Nil(t, err)
   206  	assert.Equal(t, reply.Key, "token-finisher")
   207  	assert.Equal(t, reply.Value, "[1FCX9XJTZXvZteagTrefJEBPZMt8BFmdoi]")
   208  }
   209  
   210  func TestModify(t *testing.T) {
   211  	manager := new(Manage)
   212  
   213  	log := &types.ReceiptLog{Ty: 0, Log: types.Encode(&types.ReceiptConfig{Prev: &types.ConfigItem{}, Current: &types.ConfigItem{}})}
   214  	receipt := &types.ReceiptData{Logs: []*types.ReceiptLog{log}}
   215  
   216  	_, err := manager.ExecDelLocal_Modify(nil, nil, receipt, 0)
   217  	assert.NoError(t, err)
   218  
   219  	_, err = manager.ExecLocal_Modify(nil, nil, receipt, 0)
   220  	assert.NoError(t, err)
   221  }