github.com/turingchain2020/turingchain@v1.1.21/cmd/tools/gencode/dappcode/executor/exec.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  	base.RegisterCodeFile(execCode{})
    14  	base.RegisterCodeFile(execLocalCode{})
    15  	base.RegisterCodeFile(execDelLocalCode{})
    16  }
    17  
    18  type execCode struct {
    19  	executorCodeFile
    20  }
    21  
    22  func (execCode) GetFiles() map[string]string {
    23  
    24  	return map[string]string{
    25  		execName: execContent,
    26  	}
    27  }
    28  
    29  func (execCode) GetFileReplaceTags() []string {
    30  
    31  	return []string{types.TagExecName, types.TagImportPath, types.TagClassName, types.TagExecFileContent, types.TagExecObject}
    32  }
    33  
    34  type execLocalCode struct {
    35  	executorCodeFile
    36  }
    37  
    38  func (execLocalCode) GetFiles() map[string]string {
    39  
    40  	return map[string]string{
    41  		execLocalName: execLocalContent,
    42  	}
    43  }
    44  
    45  func (execLocalCode) GetFileReplaceTags() []string {
    46  
    47  	return []string{types.TagExecName, types.TagImportPath, types.TagExecLocalFileContent, types.TagExecObject}
    48  }
    49  
    50  type execDelLocalCode struct {
    51  	executorCodeFile
    52  }
    53  
    54  func (execDelLocalCode) GetFiles() map[string]string {
    55  
    56  	return map[string]string{
    57  		execDelName: execDelContent,
    58  	}
    59  }
    60  
    61  func (execDelLocalCode) GetFileReplaceTags() []string {
    62  
    63  	return []string{types.TagExecName, types.TagImportPath, types.TagExecDelLocalFileContent, types.TagExecObject}
    64  }
    65  
    66  var (
    67  	execName    = "exec.go"
    68  	execContent = `package executor
    69  
    70  import (
    71  	${EXECNAME}types "${IMPORTPATH}/${EXECNAME}/types"
    72  	"github.com/turingchain2020/turingchain/types"
    73  )
    74  
    75  /*
    76   * 实现交易的链上执行接口
    77   * 关键数据上链(statedb)并生成交易回执(log)
    78  */
    79  
    80  ${EXECFILECONTENT}`
    81  
    82  	execLocalName    = "exec_local.go"
    83  	execLocalContent = `package executor
    84  
    85  import (
    86  	${EXECNAME}types "${IMPORTPATH}/${EXECNAME}/types"
    87  	"github.com/turingchain2020/turingchain/types"
    88  )
    89  
    90  /*
    91   * 实现交易相关数据本地执行,数据不上链
    92   * 非关键数据,本地存储(localDB), 用于辅助查询,效率高
    93  */
    94  
    95  ${EXECLOCALFILECONTENT}
    96  
    97  //当区块回滚时,框架支持自动回滚localdb kv,需要对exec-local返回的kv进行封装
    98  func (${EXEC_OBJECT} *${EXECNAME}) addAutoRollBack(tx *types.Transaction, kv []*types.KeyValue) *types.LocalDBSet {
    99  
   100  	dbSet := &types.LocalDBSet{}
   101  	dbSet.KV = ${EXEC_OBJECT}.AddRollbackKV(tx, tx.Execer, kv)
   102  	return dbSet
   103  }
   104  `
   105  
   106  	execDelName    = "exec_del_local.go"
   107  	execDelContent = `package executor
   108  
   109  import (
   110  	"github.com/turingchain2020/turingchain/types"
   111  )
   112  
   113  /* 
   114   * 实现区块回退时本地执行的数据清除
   115  */
   116  
   117  // ExecDelLocal localdb kv数据自动回滚接口
   118  func (${EXEC_OBJECT} *${EXECNAME}) ExecDelLocal(tx *types.Transaction, receipt *types.ReceiptData, index int) (*types.LocalDBSet, error) {
   119  	kvs, err := ${EXEC_OBJECT}.DelRollbackKV(tx, tx.Execer)
   120  	if err != nil {
   121  		return nil, err
   122  	}
   123  	dbSet := &types.LocalDBSet{}
   124  	dbSet.KV = append(dbSet.KV, kvs...)
   125  	return dbSet, nil
   126  }
   127  `
   128  )