github.com/turingchain2020/turingchain@v1.1.21/system/dapp/coins/executor/coins.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 coins执行器
     6  package executor
     7  
     8  /*
     9  coins 是一个货币的exec。内置货币的执行器。
    10  
    11  主要提供两种操作:
    12  EventTransfer -> 转移资产
    13  */
    14  
    15  // package none execer for unknow execer
    16  // all none transaction exec ok, execept nofee
    17  // nofee transaction will not pack into block
    18  
    19  import (
    20  	drivers "github.com/turingchain2020/turingchain/system/dapp"
    21  	"github.com/turingchain2020/turingchain/types"
    22  )
    23  
    24  // var clog = log.New("module", "execs.coins")
    25  var driverName = "coins"
    26  
    27  // Init defines a register function
    28  func Init(name string, cfg *types.TuringchainConfig, sub []byte) {
    29  	if name != driverName {
    30  		panic("system dapp can't be rename")
    31  	}
    32  	// 需要先 RegisterDappFork才可以Register dapp
    33  	drivers.Register(cfg, driverName, newCoins, cfg.GetDappFork(driverName, "Enable"))
    34  	InitExecType()
    35  }
    36  
    37  //InitExecType the initialization process is relatively heavyweight, lots of reflect, so it's global
    38  func InitExecType() {
    39  	ety := types.LoadExecutorType(driverName)
    40  	ety.InitFuncList(types.ListMethod(&Coins{}))
    41  }
    42  
    43  // GetName return name string
    44  func GetName() string {
    45  	return newCoins().GetName()
    46  }
    47  
    48  // Coins defines coins
    49  type Coins struct {
    50  	drivers.DriverBase
    51  }
    52  
    53  func newCoins() drivers.Driver {
    54  	c := &Coins{}
    55  	c.SetChild(c)
    56  	c.SetExecutorType(types.LoadExecutorType(driverName))
    57  	return c
    58  }
    59  
    60  // GetDriverName get drive name
    61  func (c *Coins) GetDriverName() string {
    62  	return driverName
    63  }
    64  
    65  // CheckTx check transaction amount 必须不能为负数
    66  func (c *Coins) CheckTx(tx *types.Transaction, index int) error {
    67  	ety := c.GetExecutorType()
    68  	amount, err := ety.Amount(tx)
    69  	if err != nil {
    70  		return err
    71  	}
    72  	if amount < 0 {
    73  		return types.ErrAmount
    74  	}
    75  	return nil
    76  }
    77  
    78  // IsFriend coins contract  the mining transaction that runs the ticket contract
    79  func (c *Coins) IsFriend(myexec, writekey []byte, othertx *types.Transaction) bool {
    80  	//step1 先判定自己合约的权限
    81  	if !c.AllowIsSame(myexec) {
    82  		return false
    83  	}
    84  	//step2 判定 othertx 的 执行器名称(只允许主链,并且是挖矿的行为)
    85  	types.AssertConfig(c.GetAPI())
    86  	types := c.GetAPI().GetConfig()
    87  	if othertx.ActionName() == "miner" {
    88  		for _, exec := range types.GetMinerExecs() {
    89  			if types.ExecName(exec) == string(othertx.Execer) {
    90  				return true
    91  			}
    92  		}
    93  	}
    94  
    95  	return false
    96  }
    97  
    98  // CheckReceiptExecOk return true to check if receipt ty is ok
    99  func (c *Coins) CheckReceiptExecOk() bool {
   100  	return true
   101  }