github.com/turingchain2020/turingchain@v1.1.21/executor/plugin_test.go (about)

     1  package executor
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/turingchain2020/turingchain/types"
     8  	"github.com/turingchain2020/turingchain/util"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestPlugin(t *testing.T) {
    13  	exec, _ := initEnv(types.GetDefaultCfgstring())
    14  	cfg := exec.client.GetConfig()
    15  	dir, ldb, kvdb := util.CreateTestDB()
    16  	defer util.CloseTestDB(dir, ldb)
    17  	ctx := &executorCtx{
    18  		stateHash:  nil,
    19  		height:     1,
    20  		blocktime:  time.Now().Unix(),
    21  		difficulty: 1,
    22  		mainHash:   nil,
    23  		parentHash: nil,
    24  	}
    25  	var txs []*types.Transaction
    26  	addr, priv := util.Genaddress()
    27  	tx := util.CreateCoinsTx(cfg, priv, addr, types.Coin)
    28  	tx.Sign(types.SECP256K1, priv)
    29  	txs = append(txs, tx)
    30  	var stateHash [32]byte
    31  	stateHash[0] = 30
    32  	for _, plugin := range globalPlugins {
    33  		detail := &types.BlockDetail{
    34  			Block:    &types.Block{Txs: txs, StateHash: stateHash[:]},
    35  			Receipts: []*types.ReceiptData{{}},
    36  		}
    37  		executor := newExecutor(ctx, exec, kvdb, txs, nil)
    38  		_, _, err := plugin.CheckEnable(executor, false)
    39  		assert.NoError(t, err)
    40  		kvs, err := plugin.ExecLocal(executor, detail)
    41  		assert.NoError(t, err)
    42  		for _, kv := range kvs {
    43  			err = kvdb.Set(kv.Key, kv.Value)
    44  			assert.NoError(t, err)
    45  		}
    46  		_, err = plugin.ExecDelLocal(executor, detail)
    47  		assert.NoError(t, err)
    48  	}
    49  }
    50  
    51  func TestPluginBase(t *testing.T) {
    52  	exec, _ := initEnv(types.GetDefaultCfgstring())
    53  	base := new(pluginBase)
    54  	dir, ldb, kvdb := util.CreateTestDB()
    55  	defer util.CloseTestDB(dir, ldb)
    56  	ctx := &executorCtx{
    57  		stateHash:  nil,
    58  		height:     0,
    59  		blocktime:  time.Now().Unix(),
    60  		difficulty: 1,
    61  		mainHash:   nil,
    62  		parentHash: nil,
    63  	}
    64  	executor := newExecutor(ctx, exec, kvdb, nil, nil)
    65  	_, _, err := base.checkFlag(executor, nil, true)
    66  	assert.NoError(t, err)
    67  
    68  	k := []byte("test")
    69  	v := types.Encode(&types.Int64{})
    70  	err = kvdb.Set(k, v)
    71  	assert.NoError(t, err)
    72  	_, _, err = base.checkFlag(executor, k, true)
    73  	assert.NoError(t, err)
    74  }