github.com/turingchain2020/turingchain@v1.1.21/executor/execenv_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 executor 6 7 import ( 8 "testing" 9 "time" 10 11 drivers "github.com/turingchain2020/turingchain/system/dapp" 12 13 "strings" 14 15 _ "github.com/turingchain2020/turingchain/system" 16 "github.com/turingchain2020/turingchain/types" 17 "github.com/turingchain2020/turingchain/util" 18 "github.com/stretchr/testify/assert" 19 ) 20 21 func TestLoadDriverFork(t *testing.T) { 22 str := types.GetDefaultCfgstring() 23 new := strings.Replace(str, "Title=\"local\"", "Title=\"turingchain\"", 1) 24 exec, _ := initEnv(new) 25 cfg := exec.client.GetConfig() 26 execInit(cfg) 27 drivers.Register(cfg, "notAllow", newAllowApp, 0) 28 var txs []*types.Transaction 29 addr, _ := util.Genaddress() 30 genkey := util.TestPrivkeyList[0] 31 tx := util.CreateCoinsTx(cfg, genkey, addr, types.Coin) 32 txs = append(txs, tx) 33 tx.Execer = []byte("notAllow") 34 tx1 := *tx 35 tx1.Execer = []byte("user.p.para.notAllow") 36 tx2 := *tx 37 tx2.Execer = []byte("user.p.test.notAllow") 38 // local fork值 为0, 测试不出fork前的情况 39 //types.SetTitleOnlyForTest("turingchain") 40 t.Log("get fork value", cfg.GetFork("ForkCacheDriver"), cfg.GetTitle()) 41 cases := []struct { 42 height int64 43 driverName string 44 tx *types.Transaction 45 index int 46 }{ 47 {cfg.GetFork("ForkCacheDriver") - 1, "notAllow", tx, 0}, 48 //相当于平行链交易 使用none执行器 49 {cfg.GetFork("ForkCacheDriver") - 1, "none", &tx1, 0}, 50 // index > 0的allow, 但由于是fork之前,所以不会检查allow, 直接采用上一个缓存的,即none执行器 51 {cfg.GetFork("ForkCacheDriver") - 1, "none", &tx1, 1}, 52 // 不能通过allow判定 加载none执行器 53 {cfg.GetFork("ForkCacheDriver"), "none", &tx2, 0}, 54 // fork之后需要重新判定, index>0 通过allow判定 55 {cfg.GetFork("ForkCacheDriver"), "notAllow", &tx2, 1}, 56 } 57 ctx := &executorCtx{ 58 stateHash: nil, 59 blocktime: time.Now().Unix(), 60 difficulty: 1, 61 mainHash: nil, 62 parentHash: nil, 63 } 64 execute := newExecutor(ctx, exec, nil, txs, nil) 65 for idx, c := range cases { 66 if execute.height != c.height { 67 execute.driverCache = make(map[string]drivers.Driver) 68 execute.height = c.height 69 } 70 driver := execute.loadDriver(c.tx, c.index) 71 assert.Equal(t, c.driverName, driver.GetDriverName(), "case index=%d", idx) 72 } 73 } 74 75 type notAllowApp struct { 76 *drivers.DriverBase 77 } 78 79 func newAllowApp() drivers.Driver { 80 app := ¬AllowApp{DriverBase: &drivers.DriverBase{}} 81 app.SetChild(app) 82 return app 83 } 84 85 func (app *notAllowApp) GetDriverName() string { 86 return "notAllow" 87 } 88 89 func (app *notAllowApp) Allow(tx *types.Transaction, index int) error { 90 91 if app.GetHeight() == 0 { 92 return types.ErrActionNotSupport 93 } 94 // 这里简单模拟 同比交易不同action 的allow限定,大于0 只是配合上述测试用例 95 if index > 0 || string(tx.Execer) == "notAllow" { 96 return nil 97 } 98 return types.ErrActionNotSupport 99 }