github.com/turingchain2020/turingchain@v1.1.21/system/dapp/driver_test.go (about)

     1  package dapp
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"sync"
     8  
     9  	"github.com/turingchain2020/turingchain/client/mocks"
    10  	"github.com/turingchain2020/turingchain/rpc/grpcclient"
    11  	"github.com/turingchain2020/turingchain/types"
    12  	"github.com/turingchain2020/turingchain/util"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/mock"
    15  )
    16  
    17  var runonce sync.Once
    18  
    19  type demoApp struct {
    20  	*DriverBase
    21  }
    22  
    23  func newdemoApp() Driver {
    24  	demo := &demoApp{DriverBase: &DriverBase{}}
    25  	demo.SetChild(demo)
    26  	return demo
    27  }
    28  
    29  func (demo *demoApp) GetDriverName() string {
    30  	return "demo"
    31  }
    32  
    33  type noneApp struct {
    34  	*DriverBase
    35  }
    36  
    37  func newnoneApp() Driver {
    38  	none := &noneApp{DriverBase: &DriverBase{}}
    39  	none.SetChild(none)
    40  	return none
    41  }
    42  
    43  func (none *noneApp) GetDriverName() string {
    44  	return "none"
    45  }
    46  
    47  func Init(cfg *types.TuringchainConfig) {
    48  	runonce.Do(func() {
    49  		Register(cfg, "none", newnoneApp, 0)
    50  		Register(cfg, "demo", newdemoApp, 1)
    51  	})
    52  }
    53  
    54  func TestReigister(t *testing.T) {
    55  	cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring())
    56  	Init(cfg)
    57  	api := &mocks.QueueProtocolAPI{}
    58  	api.On("GetConfig", mock.Anything).Return(cfg)
    59  
    60  	_, err := LoadDriver("demo", 0)
    61  	assert.Equal(t, err, types.ErrUnknowDriver)
    62  	_, err = LoadDriver("demo", 1)
    63  	assert.Equal(t, err, nil)
    64  
    65  	tx := &types.Transaction{Execer: []byte("demo")}
    66  	driver := LoadDriverAllow(api, tx, 0, 0)
    67  	assert.Equal(t, "none", driver.GetDriverName())
    68  	driver = LoadDriverAllow(api, tx, 0, 1)
    69  	assert.Equal(t, "demo", driver.GetDriverName())
    70  
    71  	cfg.SetTitleOnlyForTest("user.p.hello.")
    72  	tx = &types.Transaction{Execer: []byte("demo")}
    73  	driver = LoadDriverAllow(api, tx, 0, 0)
    74  	assert.Equal(t, "none", driver.GetDriverName())
    75  	driver = LoadDriverAllow(api, tx, 0, 1)
    76  	assert.Equal(t, "demo", driver.GetDriverName())
    77  
    78  	tx.Execer = []byte("user.p.hello.demo")
    79  	driver = LoadDriverAllow(api, tx, 0, 1)
    80  	assert.Equal(t, "demo", driver.GetDriverName())
    81  
    82  	tx.Execer = []byte("user.p.hello2.demo")
    83  	driver = LoadDriverAllow(api, tx, 0, 1)
    84  	assert.Equal(t, "none", driver.GetDriverName())
    85  }
    86  
    87  func TestDriverAPI(t *testing.T) {
    88  	cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring())
    89  	Init(cfg)
    90  	api := &mocks.QueueProtocolAPI{}
    91  	api.On("GetConfig", mock.Anything).Return(cfg)
    92  
    93  	tx := &types.Transaction{Execer: []byte("demo")}
    94  	demo := LoadDriverAllow(api, tx, 0, 1).(*demoApp)
    95  	dir, ldb, kvdb := util.CreateTestDB()
    96  	defer util.CloseTestDB(dir, ldb)
    97  	demo.SetEnv(1, time.Now().Unix(), 1)
    98  	demo.SetBlockInfo([]byte("parentHash"), []byte("mainHash"), 1)
    99  	demo.SetLocalDB(kvdb)
   100  	demo.SetStateDB(kvdb)
   101  	demo.SetAPI(api)
   102  	gcli, err := grpcclient.NewMainChainClient(cfg, "")
   103  	assert.Nil(t, err)
   104  	demo.SetExecutorAPI(api, gcli)
   105  	assert.NotNil(t, demo.GetAPI())
   106  	assert.NotNil(t, demo.GetExecutorAPI())
   107  	cfg.SetTitleOnlyForTest("turingchain")
   108  	assert.Equal(t, "parentHash", string(demo.GetParentHash()))
   109  	assert.Equal(t, "parentHash", string(demo.GetLastHash()))
   110  	cfg.SetTitleOnlyForTest("user.p.wzw.")
   111  	assert.Equal(t, "parentHash", string(demo.GetParentHash()))
   112  	assert.Equal(t, "mainHash", string(demo.GetLastHash()))
   113  	assert.Equal(t, int64(1), demo.GetMainHeight())
   114  	assert.Equal(t, true, IsDriverAddress(ExecAddress("none"), 0))
   115  	assert.Equal(t, false, IsDriverAddress(ExecAddress("demo"), 0))
   116  	assert.Equal(t, true, IsDriverAddress(ExecAddress("demo"), 1))
   117  }
   118  
   119  func TestExecAddress(t *testing.T) {
   120  	assert.Equal(t, "16htvcBNSEA7fZhAdLJphDwQRQJaHpyHTp", ExecAddress("ticket"))
   121  }
   122  
   123  func TestAllow(t *testing.T) {
   124  	cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring())
   125  	Init(cfg)
   126  	api := &mocks.QueueProtocolAPI{}
   127  	api.On("GetConfig", mock.Anything).Return(cfg)
   128  
   129  	tx := &types.Transaction{Execer: []byte("demo")}
   130  	demo := LoadDriverAllow(api, tx, 0, 1).(*demoApp)
   131  	assert.Equal(t, true, demo.AllowIsSame([]byte("demo")))
   132  	cfg.SetTitleOnlyForTest("user.p.wzw.")
   133  	assert.Equal(t, true, demo.AllowIsSame([]byte("user.p.wzw.demo")))
   134  	assert.Equal(t, false, demo.AllowIsSame([]byte("user.p.wzw2.demo")))
   135  	assert.Equal(t, false, demo.AllowIsUserDot1([]byte("user.p.wzw.demo")))
   136  	assert.Equal(t, true, demo.AllowIsUserDot1([]byte("user.demo")))
   137  	assert.Equal(t, true, demo.AllowIsUserDot1([]byte("user.p.wzw.user.demo")))
   138  	assert.Equal(t, true, demo.AllowIsUserDot2([]byte("user.p.wzw.user.demo.xxxx")))
   139  	assert.Equal(t, true, demo.AllowIsUserDot2([]byte("user.demo.xxxx")))
   140  	assert.Equal(t, nil, demo.Allow(tx, 0))
   141  	tx = &types.Transaction{Execer: []byte("demo2")}
   142  	assert.Equal(t, types.ErrNotAllow, demo.Allow(tx, 0))
   143  	assert.Equal(t, false, demo.IsFriend(nil, nil, nil))
   144  }
   145  
   146  func TestDriverBase(t *testing.T) {
   147  	cfg := types.NewTuringchainConfig(types.GetDefaultCfgstring())
   148  	api := &mocks.QueueProtocolAPI{}
   149  	api.On("GetConfig", mock.Anything).Return(cfg)
   150  	Init(cfg)
   151  	dir, ldb, kvdb := util.CreateTestDB()
   152  	defer util.CloseTestDB(dir, ldb)
   153  	demo := newdemoApp().(*demoApp)
   154  	demo.SetAPI(api)
   155  	demo.SetExecutorType(nil)
   156  	assert.Nil(t, demo.GetPayloadValue())
   157  	assert.Nil(t, demo.GetExecutorType())
   158  	assert.True(t, demo.ExecutorOrder() == 0)
   159  	assert.Nil(t, demo.GetFuncMap())
   160  	demo.SetIsFree(false)
   161  	assert.False(t, demo.IsFree())
   162  
   163  	tx := &types.Transaction{Execer: []byte("demo"), To: ExecAddress("demo"), GroupCount: 1}
   164  	t.Log("addr:", ExecAddress("demo"))
   165  	_, err := demo.ExecLocal(tx, nil, 0)
   166  	assert.NoError(t, err)
   167  	_, err = demo.ExecDelLocal(tx, nil, 0)
   168  	assert.NoError(t, err)
   169  	_, err = demo.Exec(tx, 0)
   170  	assert.NoError(t, err)
   171  	err = demo.CheckTx(tx, 0)
   172  	assert.NoError(t, err)
   173  
   174  	txs := []*types.Transaction{tx}
   175  	demo.SetTxs(txs)
   176  	assert.Equal(t, txs, demo.GetTxs())
   177  	_, err = demo.GetTxGroup(0)
   178  	assert.Equal(t, types.ErrTxGroupFormat, err)
   179  
   180  	demo.SetReceipt(nil)
   181  	assert.Nil(t, demo.GetReceipt())
   182  	demo.SetLocalDB(nil)
   183  	assert.Nil(t, demo.GetLocalDB())
   184  	assert.Nil(t, demo.GetStateDB())
   185  	assert.True(t, demo.GetHeight() == 0)
   186  	assert.True(t, demo.GetBlockTime() == 0)
   187  	assert.True(t, demo.GetDifficulty() == 0)
   188  	assert.Equal(t, "demo", demo.GetName())
   189  	assert.Equal(t, "demo", demo.GetCurrentExecName())
   190  
   191  	name := demo.GetActionName(tx)
   192  	assert.Equal(t, "unknown", name)
   193  	assert.True(t, demo.CheckSignatureData(tx, 0))
   194  	assert.NotNil(t, demo.GetCoinsAccount())
   195  	assert.False(t, demo.CheckReceiptExecOk())
   196  
   197  	err = CheckAddress(cfg, "1HUiTRFvp6HvW6eacgV9EoBSgroRDiUsMs", 0)
   198  	assert.NoError(t, err)
   199  
   200  	demo.SetLocalDB(kvdb)
   201  	execer := "user.p.guodun.demo"
   202  	kvs := []*types.KeyValue{
   203  		{
   204  			Key:   []byte("hello"),
   205  			Value: []byte("world"),
   206  		},
   207  	}
   208  	newkvs := demo.AddRollbackKV(tx, []byte(execer), kvs)
   209  	assert.Equal(t, 2, len(newkvs))
   210  	assert.Equal(t, string(newkvs[0].Key), "hello")
   211  	assert.Equal(t, string(newkvs[0].Value), "world")
   212  	assert.Equal(t, string(newkvs[1].Key), string(append([]byte("LODB-demo-rollback-"), tx.Hash()...)))
   213  
   214  	rollbackkvs := []*types.KeyValue{
   215  		{
   216  			Key:   []byte("hello"),
   217  			Value: nil,
   218  		},
   219  	}
   220  	data := types.Encode(&types.LocalDBSet{KV: rollbackkvs})
   221  	assert.Equal(t, string(newkvs[1].Value), string(types.Encode(&types.ReceiptLog{Ty: types.TyLogRollback, Log: data})))
   222  
   223  	kvdb.Set(newkvs[1].Key, newkvs[1].Value)
   224  	newkvs, err = demo.DelRollbackKV(tx, []byte(execer))
   225  	assert.Nil(t, err)
   226  	assert.Equal(t, 2, len(newkvs))
   227  	assert.Equal(t, string(newkvs[0].Key), "hello")
   228  	assert.Equal(t, newkvs[0].Value, []byte(nil))
   229  
   230  	assert.Equal(t, string(newkvs[1].Key), string(append([]byte("LODB-demo-rollback-"), tx.Hash()...)))
   231  	assert.Equal(t, newkvs[1].Value, []byte(nil))
   232  }
   233  
   234  func TestDriverBase_Query(t *testing.T) {
   235  	dir, ldb, kvdb := util.CreateTestDB()
   236  	defer util.CloseTestDB(dir, ldb)
   237  	demo := newdemoApp().(*demoApp)
   238  	demo.SetLocalDB(kvdb)
   239  	addr := &types.ReqAddr{Addr: "1HUiTRFvp6HvW6eacgV9EoBSgroRDiUsMs", Count: 1, Direction: 1}
   240  	kvdb.Set(types.CalcTxAddrHashKey(addr.GetAddr(), ""), types.Encode(&types.ReplyTxInfo{Height: 1}))
   241  	_, err := demo.GetTxsByAddr(addr)
   242  	assert.Equal(t, types.ErrNotFound, err)
   243  
   244  	addr.Height = -1
   245  	_, err = demo.GetTxsByAddr(addr)
   246  	assert.Equal(t, nil, err)
   247  
   248  	c, err := demo.GetPrefixCount(&types.ReqKey{Key: types.CalcTxAddrHashKey(addr.GetAddr(), "")})
   249  	assert.NoError(t, err)
   250  	assert.True(t, c.(*types.Int64).Data == 1)
   251  
   252  	_, err = demo.GetAddrTxsCount(&types.ReqKey{Key: types.CalcTxAddrHashKey(addr.GetAddr(), "")})
   253  	assert.NoError(t, err)
   254  
   255  	_, err = demo.Query("", nil)
   256  	assert.Equal(t, types.ErrActionNotSupport, err)
   257  }