github.com/turingchain2020/turingchain@v1.1.21/cmd/tools/gencode/dappcode/executor/executor.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  	"github.com/turingchain2020/turingchain/cmd/tools/gencode/base"
     9  	"github.com/turingchain2020/turingchain/cmd/tools/types"
    10  )
    11  
    12  func init() {
    13  
    14  	base.RegisterCodeFile(executorCodeFile{})
    15  }
    16  
    17  type executorCodeFile struct {
    18  	base.DappCodeFile
    19  }
    20  
    21  func (c executorCodeFile) GetDirName() string {
    22  
    23  	return "executor"
    24  }
    25  
    26  func (c executorCodeFile) GetFiles() map[string]string {
    27  
    28  	return map[string]string{
    29  		executorName: executorContent,
    30  		kvName:       kvContent,
    31  	}
    32  }
    33  
    34  func (c executorCodeFile) GetFileReplaceTags() []string {
    35  
    36  	return []string{types.TagExecName, types.TagExecObject, types.TagImportPath, types.TagClassName}
    37  }
    38  
    39  var (
    40  	executorName    = "${EXECNAME}.go"
    41  	executorContent = `package executor
    42  
    43  import (
    44  	log "github.com/turingchain2020/turingchain/common/log/log15"
    45  	${EXECNAME}types "${IMPORTPATH}/${EXECNAME}/types"
    46  	drivers "github.com/turingchain2020/turingchain/system/dapp"
    47  	"github.com/turingchain2020/turingchain/types"
    48  )
    49  
    50  /* 
    51   * 执行器相关定义
    52   * 重载基类相关接口
    53  */ 
    54  
    55  
    56  var (
    57  	//日志
    58  	elog = log.New("module", "${EXECNAME}.executor")
    59  )
    60  
    61  var driverName = ${EXECNAME}types.${CLASSNAME}X
    62  
    63  // Init register dapp
    64  func Init(name string, cfg *types.TuringchainConfig, sub []byte) {
    65  	drivers.Register(cfg, GetName(), new${CLASSNAME}, cfg.GetDappFork(driverName, "Enable"))
    66      InitExecType()
    67  }
    68  
    69  // InitExecType Init Exec Type
    70  func InitExecType() {
    71  	ety := types.LoadExecutorType(driverName)
    72  	ety.InitFuncList(types.ListMethod(&${EXECNAME}{}))
    73  }
    74  
    75  type ${EXECNAME} struct {
    76  	drivers.DriverBase
    77  }
    78  
    79  func new${CLASSNAME}() drivers.Driver {
    80  	t := &${EXECNAME}{}
    81  	t.SetChild(t)
    82  	t.SetExecutorType(types.LoadExecutorType(driverName))
    83  	return t
    84  }
    85  
    86  // GetName get driver name
    87  func GetName() string {
    88  	return new${CLASSNAME}().GetName()
    89  }
    90  
    91  func (${EXEC_OBJECT} *${EXECNAME}) GetDriverName() string {
    92  	return driverName
    93  }
    94  
    95  // CheckTx 实现自定义检验交易接口,供框架调用
    96  func (${EXEC_OBJECT} *${EXECNAME}) CheckTx(tx *types.Transaction, index int) error {
    97  	// implement code
    98  	return nil
    99  }
   100  
   101  `
   102  
   103  	kvName    = "kv.go"
   104  	kvContent = `package executor
   105  
   106  /*
   107   * 用户合约存取kv数据时,key值前缀需要满足一定规范
   108   * 即key = keyPrefix + userKey
   109   * 需要字段前缀查询时,使用’-‘作为分割符号
   110  */
   111  
   112  var (
   113  	//KeyPrefixStateDB state db key必须前缀
   114  	KeyPrefixStateDB = "mavl-${EXECNAME}-"
   115  	//KeyPrefixLocalDB local db的key必须前缀
   116  	KeyPrefixLocalDB = "LODB-${EXECNAME}-"
   117  )
   118  `
   119  )