github.com/turingchain2020/turingchain@v1.1.21/cmd/tools/strategy/create_plugin_file_template.go (about)

     1  /*
     2   * Copyright Turing Corp. 2018 All Rights Reserved.
     3   * Use of this source code is governed by a BSD-style
     4   * license that can be found in the LICENSE file.
     5   */
     6  
     7  package strategy
     8  
     9  //const
    10  const (
    11  	// 創建main.go的文件模板
    12  	CpftMainGo = `package main
    13  
    14  import (
    15  	_ "github.com/turingchain2020/turingchain/system"
    16  	_ "${PROJECTPATH}/plugin"
    17  
    18  	"github.com/turingchain2020/turingchain/types"
    19  	"github.com/turingchain2020/turingchain/util/cli"
    20  )
    21  
    22  func main() {
    23  	types.S("cfg.${PROJECTNAME}", ${PROJECTNAME})
    24  	cli.RunTuringchain("${PROJECTNAME}")
    25  }
    26  `
    27  
    28  	// 生成的配置文件模板 xxx.toml
    29  	CpftCfgToml = `
    30  Title="${PROJECTNAME}"
    31  FixTime=false
    32  
    33  [log]
    34  # 日志级别,支持debug(dbug)/info/warn/error(eror)/crit
    35  loglevel = "debug"
    36  logConsoleLevel = "info"
    37  # 日志文件名,可带目录,所有生成的日志文件都放到此目录下
    38  logFile = "logs/${PROJECTNAME}.log"
    39  # 单个日志文件的最大值(单位:兆)
    40  maxFileSize = 300
    41  # 最多保存的历史日志文件个数
    42  maxBackups = 100
    43  # 最多保存的历史日志消息(单位:天)
    44  maxAge = 28
    45  # 日志文件名是否使用本地事件(否则使用UTC时间)
    46  localTime = true
    47  # 历史日志文件是否压缩(压缩格式为gz)
    48  compress = true
    49  # 是否打印调用源文件和行号
    50  callerFile = false
    51  # 是否打印调用方法
    52  callerFunction = false
    53  
    54  [blockchain]
    55  dbPath="datadir"
    56  dbCache=64
    57  batchsync=false
    58  isRecordBlockSequence=false
    59  enableTxQuickIndex=false
    60  
    61  [p2p]
    62  seeds=[]
    63  isSeed=false
    64  innerSeedEnable=true
    65  useGithub=true
    66  innerBounds=300
    67  dbPath="datadir/addrbook"
    68  dbCache=4
    69  grpcLogFile="grpc33.log"
    70  
    71  [rpc]
    72  jrpcBindAddr="localhost:9671"
    73  grpcBindAddr="localhost:9672"
    74  whitelist=["127.0.0.1"]
    75  jrpcFuncWhitelist=["*"]
    76  grpcFuncWhitelist=["*"]
    77  
    78  [mempool]
    79  maxTxNumPerAccount=100
    80  
    81  [store]
    82  dbPath="datadir/mavltree"
    83  dbCache=128
    84  enableMavlPrefix=false
    85  enableMVCC=false
    86  enableMavlPrune=false
    87  pruneHeight=10000
    88  
    89  [wallet]
    90  dbPath="wallet"
    91  dbCache=16
    92  
    93  [wallet.sub.ticket]
    94  minerdisable=false
    95  minerwhitelist=["*"]
    96  
    97  [exec]
    98  enableStat=false
    99  enableMVCC=false
   100  
   101  [exec.sub.token]
   102  saveTokenTxList=false
   103  `
   104  
   105  	CpftRunmainBlock = `package main
   106  
   107  var ${PROJECTNAME} = `
   108  
   109  	// 生成项目运行主程序的模板 xxx.go
   110  	// 顶部还需要加上package main
   111  	//var turingchaincoin = `CPFT_RUNMAIN`
   112  	CpftRunMain = `TestNet=false
   113  [blockchain]
   114  defCacheSize=128
   115  maxFetchBlockNum=128
   116  timeoutSeconds=5
   117  batchBlockNum=128
   118  driver="leveldb"
   119  isStrongConsistency=false
   120  singleMode=false
   121  [p2p]
   122  enable=true
   123  serverStart=true
   124  msgCacheSize=10240
   125  driver="leveldb"
   126  [mempool]
   127  poolCacheSize=102400
   128  minTxFeeRate=100000
   129  [consensus]
   130  name="ticket"
   131  minerstart=true
   132  genesisBlockTime=1514533394
   133  genesis="14KEKbYtKKQm4wMthSK9J4La4nAiidGozt"
   134  [mver.consensus]
   135  fundKeyAddr = "1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP"
   136  coinReward = 18
   137  coinDevFund = 12
   138  ticketPrice = 10000
   139  powLimitBits = "0x1f00ffff"
   140  retargetAdjustmentFactor = 4
   141  futureBlockTime = 15
   142  ticketFrozenTime = 43200
   143  ticketWithdrawTime = 172800
   144  ticketMinerWaitTime = 7200
   145  maxTxNumber = 1500
   146  targetTimespan = 2160
   147  targetTimePerBlock = 15
   148  [consensus.sub.ticket]
   149  genesisBlockTime=1526486816
   150  [[consensus.sub.ticket.genesis]]
   151  minerAddr="184wj4nsgVxKyz2NhM3Yb5RK5Ap6AFRFq2"
   152  returnAddr="1FB8L3DykVF7Y78bRfUrRcMZwesKue7CyR"
   153  count=3000
   154  [[consensus.sub.ticket.genesis]]
   155  minerAddr="1M4ns1eGHdHak3SNc2UTQB75vnXyJQd91s"
   156  returnAddr="1Lw6QLShKVbKM6QvMaCQwTh5Uhmy4644CG"
   157  count=3000
   158  [[consensus.sub.ticket.genesis]]
   159  minerAddr="19ozyoUGPAQ9spsFiz9CJfnUCFeszpaFuF"
   160  returnAddr="1PSYYfCbtSeT1vJTvSKmQvhz8y6VhtddWi"
   161  count=3000
   162  [[consensus.sub.ticket.genesis]]
   163  minerAddr="1MoEnCDhXZ6Qv5fNDGYoW6MVEBTBK62HP2"
   164  returnAddr="1BG9ZoKtgU5bhKLpcsrncZ6xdzFCgjrZud"
   165  count=3000
   166  [[consensus.sub.ticket.genesis]]
   167  minerAddr="1FjKcxY7vpmMH6iB5kxNYLvJkdkQXddfrp"
   168  returnAddr="1G7s64AgX1ySDcUdSW5vDa8jTYQMnZktCd"
   169  count=3000
   170  [[consensus.sub.ticket.genesis]]
   171  minerAddr="12T8QfKbCRBhQdRfnAfFbUwdnH7TDTm4vx"
   172  returnAddr="1FiDC6XWHLe7fDMhof8wJ3dty24f6aKKjK"
   173  count=3000
   174  [[consensus.sub.ticket.genesis]]
   175  minerAddr="1bgg6HwQretMiVcSWvayPRvVtwjyKfz1J"
   176  returnAddr="1AMvuuQ7V7FPQ4hkvHQdgNWy8wVL4d4hmp"
   177  count=3000
   178  [[consensus.sub.ticket.genesis]]
   179  minerAddr="1EwkKd9iU1pL2ZwmRAC5RrBoqFD1aMrQ2"
   180  returnAddr="1ExRRLoJXa8LzXdNxnJvBkVNZpVw3QWMi4"
   181  count=3000
   182  [[consensus.sub.ticket.genesis]]
   183  minerAddr="1HFUhgxarjC7JLru1FLEY6aJbQvCSL58CB"
   184  returnAddr="1KNGHukhbBnbWWnMYxu1C7YMoCj45Z3amm"
   185  count=3000
   186  [[consensus.sub.ticket.genesis]]
   187  minerAddr="1C9M1RCv2e9b4GThN9ddBgyxAphqMgh5zq"
   188  returnAddr="1AH9HRd4WBJ824h9PP1jYpvRZ4BSA4oN6Y"
   189  count=4733
   190  [store]
   191  name="mavl"
   192  driver="leveldb"
   193  [wallet]
   194  minFee=100000
   195  driver="leveldb"
   196  signType="secp256k1"
   197  [exec]
   198  
   199  [exec.sub.token]
   200  #配置一个空值,防止配置文件被覆盖
   201  tokenApprs = []
   202  [exec.sub.relay]
   203  genesis="14KEKbYtKKQm4wMthSK9J4La4nAiidGozt"
   204  [exec.sub.manage]
   205  superManager=[
   206  "1JmFaA6unrCFYEWPGRi7uuXY1KthTJxJEP",
   207  ]
   208  #系统中所有的fork,默认用turingchain的测试网络的
   209  #但是我们可以替换
   210  [fork.system]
   211  ForkChainParamV1= 0
   212  ForkCheckTxDup=0
   213  ForkBlockHash= 1
   214  ForkMinerTime= 0
   215  ForkTransferExec= 100000
   216  ForkExecKey= 200000
   217  ForkTxGroup= 200000
   218  ForkResetTx0= 200000
   219  ForkWithdraw= 200000
   220  ForkExecRollback= 450000
   221  ForkCheckBlockTime=1200000
   222  ForkMultiSignAddress=1298600
   223  ForkTxHeight= -1
   224  ForkTxGroupPara= -1
   225  ForkChainParamV2= -1
   226  ForkBase58AddressCheck=1800000
   227  ForkTicketFundAddrV1=-1
   228  ForkRootHash=1
   229  [fork.sub.coins]
   230  Enable=0
   231  [fork.sub.ticket]
   232  Enable=0
   233  ForkTicketId = 1200000
   234  [fork.sub.retrieve]
   235  Enable=0
   236  ForkRetrive=0
   237  [fork.sub.hashlock]
   238  Enable=0
   239  [fork.sub.manage]
   240  Enable=0
   241  ForkManageExec=100000
   242  [fork.sub.token]
   243  Enable=0
   244  ForkTokenBlackList= 0
   245  ForkBadTokenSymbol= 0
   246  ForkTokenPrice= 300000
   247  [fork.sub.trade]
   248  Enable=0
   249  ForkTradeBuyLimit= 0
   250  ForkTradeAsset= -1
   251  `
   252  
   253  	// 生成项目Makefile文件的模板 Makefile
   254  	CpftMakefile = `
   255  TURINGCHAIN=github.com/turingchain2020/turingchain
   256  TURINGCHAIN_PATH=vendor/${TURINGCHAIN}
   257  all: vendor proto build
   258  
   259  build:
   260  	go build -i -o ${PROJECTNAME}
   261  	go build -i -o ${PROJECTNAME}-cli ${PROJECTPATH}/cli
   262  
   263  vendor:
   264  	make update
   265  	make updatevendor
   266  
   267  proto: 
   268  	cd ${GOPATH}/src/${PROJECTPATH}/plugin/dapp/${EXECNAME}/proto && sh create_protobuf.sh
   269  
   270  update:
   271  	go get -u -v github.com/kardianos/govendor
   272  	rm -rf ${TURINGCHAIN_PATH}
   273  	git clone --depth 1 -b master https://${TURINGCHAIN}.git ${TURINGCHAIN_PATH}
   274  	rm -rf vendor/${TURINGCHAIN}/.git
   275  	rm -rf vendor/${TURINGCHAIN}/vendor/github.com/apache/thrift/tutorial/erl/
   276  	cp -Rf vendor/${TURINGCHAIN}/vendor/* vendor/
   277  	rm -rf vendor/${TURINGCHAIN}/vendor
   278  	govendor init
   279  	go build -i -o tool github.com/turingchain2020/plugin/vendor/github.com/turingchain2020/turingchain/cmd/tools
   280  	./tool import --path "plugin" --packname "${PROJECTPATH}/plugin" --conf "plugin/plugin.toml"
   281  
   282  updatevendor:
   283  	govendor add +e
   284  	govendor fetch -v +m
   285  
   286  clean:
   287  	@rm -rf vendor
   288  	@rm -rf datadir
   289  	@rm -rf logs
   290  	@rm -rf wallet
   291  	@rm -rf grpc33.log
   292  	@rm -rf ${PROJECTNAME}
   293  	@rm -rf ${PROJECTNAME}-cli
   294  	@rm -rf tool
   295  	@rm -rf plugin/init.go
   296  	@rm -rf plugin/consensus/init
   297  	@rm -rf plugin/dapp/init
   298  	@rm -rf plugin/crypto/init
   299  	@rm -rf plugin/store/init
   300  	@rm -rf plugin/mempool/init
   301  `
   302  
   303  	// 生成 .travis.yml 文件模板
   304  	CpftTravisYml = `
   305  language: go
   306  
   307  go:
   308    - "1.9"
   309    - master
   310  `
   311  
   312  	// 生成 plugin/plugin.toml的文件模板
   313  	CpftPluginToml = `
   314  # type字段仅支持 consensus  dapp store mempool
   315  [dapp-ticket]
   316  gitrepo = "github.com/turingchain2020/plugin/plugin/dapp/ticket"
   317  
   318  [consensus-ticket]
   319  gitrepo = "github.com/turingchain2020/plugin/plugin/consensus/ticket"
   320  
   321  [dapp-retrieve]
   322  gitrepo = "github.com/turingchain2020/plugin/plugin/dapp/retrieve"
   323  
   324  [dapp-hashlock]
   325  gitrepo = "github.com/turingchain2020/plugin/plugin/dapp/hashlock"
   326  
   327  [dapp-token]
   328  gitrepo = "github.com/turingchain2020/plugin/plugin/dapp/token"
   329  
   330  [dapp-trade]
   331  gitrepo = "github.com/turingchain2020/plugin/plugin/dapp/trade"
   332  
   333  [mempool-price]
   334  gitrepo = "github.com/turingchain2020/plugin/plugin/mempool/price"
   335  
   336  [mempool-score]
   337  gitrepo = "github.com/turingchain2020/plugin/plugin/mempool/score"
   338  `
   339  	// 项目 cli/main.go 文件模板
   340  	CpftCliMain = `package main
   341  
   342  import (
   343  	_ "${PROJECTPATH}/plugin"
   344  	_ "github.com/turingchain2020/turingchain/system"
   345  	"github.com/turingchain2020/turingchain/util/cli"
   346  )
   347  
   348  func main() {
   349  	cli.Run("", "")
   350  }
   351  `
   352  	// plugin/dapp/xxxx/commands/cmd.go文件的模板c
   353  	CpftDappCommands = `package commands
   354  
   355  import (
   356  	"github.com/spf13/cobra"
   357  )
   358  
   359  func Cmd() *cobra.Command {
   360  	return nil
   361  }`
   362  
   363  	// plugin/dapp/xxxx/plugin.go文件的模板
   364  	CpftDappPlugin = `package ${PROJECTNAME}
   365  
   366  import (
   367  	"github.com/turingchain2020/turingchain/pluginmgr"
   368  	"${PROJECTPATH}/plugin/dapp/${PROJECTNAME}/commands"
   369  	"${PROJECTPATH}/plugin/dapp/${PROJECTNAME}/executor"
   370  	"${PROJECTPATH}/plugin/dapp/${PROJECTNAME}/types"
   371  )
   372  
   373  func init() {
   374  	pluginmgr.Register(&pluginmgr.PluginBase{
   375  		Name:     types.${EXECNAME_FB}X,
   376  		ExecName: executor.GetName(),
   377  		Exec:     executor.Init,
   378  		Cmd:      commands.Cmd,
   379  		RPC:      nil,
   380  	})
   381  }
   382  `
   383  
   384  	// plugin/dapp/xxxx/executor/xxxx.go文件模板
   385  	CpftDappExec = `package executor
   386  
   387  import (
   388  	log "github.com/inconshreveable/log15"
   389  	drivers "github.com/turingchain2020/turingchain/system/dapp"
   390  	"github.com/turingchain2020/turingchain/types"
   391  )
   392  
   393  var clog = log.New("module", "execs.${EXECNAME}")
   394  var driverName = "${EXECNAME}"
   395  
   396  func init() {
   397  	ety := types.LoadExecutorType(driverName)
   398  	if ety != nil {
   399  		ety.InitFuncList(types.ListMethod(&${CLASSNAME}{}))
   400  	}
   401  }
   402  
   403  func Init(name string, sub []byte) {
   404  	ety := types.LoadExecutorType(driverName)
   405  	if ety != nil {
   406  		ety.InitFuncList(types.ListMethod(&${CLASSNAME}{}))
   407  	}
   408  
   409  	clog.Debug("register ${EXECNAME} execer")
   410  	drivers.Register(GetName(), new${CLASSNAME}, types.GetDappFork(driverName, "Enable"))
   411  }
   412  
   413  func GetName() string {
   414  	return new${CLASSNAME}().GetName()
   415  }
   416  
   417  type ${CLASSNAME} struct {
   418  	drivers.DriverBase
   419  }
   420  
   421  func new${CLASSNAME}() drivers.Driver {
   422  	n := &${CLASSNAME}{}
   423  	n.SetChild(n)
   424  	n.SetIsFree(true)
   425  	n.SetExecutorType(types.LoadExecutorType(driverName))
   426  	return n
   427  }
   428  
   429  func (this *${CLASSNAME}) GetDriverName() string {
   430  	return driverName
   431  }
   432  
   433  func (this *${CLASSNAME}) CheckTx(tx *types.Transaction, index int) error {
   434  	return nil
   435  }
   436  `
   437  	// plugin/dapp/xxxx/proto/create_protobuf.sh文件模板
   438  	CpftDappCreatepb = `#!/bin/sh
   439  protoc --go_out=plugins=grpc:../types ./*.proto --proto_path=. 
   440  `
   441  
   442  	// plugin/dapp/xxxx/proto/Makefile 文件模板
   443  	CpftDappMakefile = `all:
   444  	sh ./create_protobuf.sh
   445  `
   446  
   447  	// plugin/dapp/xxxx/proto/xxxx.proto的文件模板
   448  	CpftDappProto = `syntax = "proto3";
   449  package types;
   450  
   451  message ${ACTIONNAME} {
   452      int32 ty = 1;
   453  	oneof value {
   454  		${ACTIONNAME}None none = 2;
   455  	}
   456  }
   457  
   458  message ${ACTIONNAME}None {
   459  }
   460  `
   461  
   462  	// plugin/dapp/xxxx/types/types.go的文件模板cd
   463  	CpftDappTypefile = `package types
   464  
   465  import (
   466  	"github.com/turingchain2020/turingchain/types"
   467  )
   468  
   469  var (
   470  	${EXECNAME_FB}X      = "${EXECNAME}"
   471  	Execer${EXECNAME_FB} = []byte(${EXECNAME_FB}X)
   472  	actionName  = map[string]int32{}
   473  	logmap = map[int64]*types.LogInfo{}
   474  )
   475  
   476  func init() {
   477  	types.AllowUserExec = append(types.AllowUserExec, Execer${EXECNAME_FB})
   478  	types.RegistorExecutor("${EXECNAME}", NewType())
   479  
   480  	types.RegisterDappFork(${EXECNAME_FB}X, "Enable", 0)
   481  }
   482  
   483  type ${CLASSTYPENAME} struct {
   484  	types.ExecTypeBase
   485  }
   486  
   487  func NewType() *${CLASSTYPENAME} {
   488  	c := &${CLASSTYPENAME}{}
   489  	c.SetChild(c)
   490  	return c
   491  }
   492  
   493  func (coins *${CLASSTYPENAME}) GetPayload() types.Message {
   494  	return &${ACTIONNAME}{}
   495  }
   496  
   497  func (coins *${CLASSTYPENAME}) GetName() string {
   498  	return ${EXECNAME_FB}X
   499  }
   500  
   501  func (coins *${CLASSTYPENAME}) GetLogMap() map[int64]*types.LogInfo {
   502  	return logmap
   503  }
   504  
   505  func (c *${CLASSTYPENAME}) GetTypeMap() map[string]int32 {
   506  	return actionName
   507  }
   508  `
   509  )