github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/event.go (about)

     1  // Copyright (C) 2017 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  
    19  package nvm
    20  
    21  import "C"
    22  import (
    23  	"unsafe"
    24  
    25  	"github.com/nebulasio/go-nebulas/core"
    26  
    27  	"github.com/nebulasio/go-nebulas/core/state"
    28  	"github.com/nebulasio/go-nebulas/util/logging"
    29  	"github.com/sirupsen/logrus"
    30  )
    31  
    32  const (
    33  	// EventBaseGasCount the gas count of a new event
    34  	EventBaseGasCount = 20
    35  )
    36  
    37  const (
    38  	// InnerTransferFailed failed status for transaction execute result.
    39  	InnerTransferFailed = 0
    40  
    41  	// InnerTransferExecutionSuccess success status for transaction execute result.
    42  	InnerTransferExecutionSuccess = 1
    43  )
    44  
    45  // TransferFromContractEvent event for transfer in contract
    46  type TransferFromContractEvent struct {
    47  	Amount string `json:"amount"`
    48  	From   string `json:"from"`
    49  	To     string `json:"to"`
    50  }
    51  
    52  // TransferFromContractFailureEvent event for transfer in contract
    53  type TransferFromContractFailureEvent struct {
    54  	Amount string `json:"amount"`
    55  	From   string `json:"from"`
    56  	To     string `json:"to"`
    57  	Status uint8  `json:"status"`
    58  	Error  string `json:"error"`
    59  }
    60  
    61  // InnerTransferContractEvent event for inner transfer in contract
    62  type InnerContractEvent struct {
    63  	From     string `json:"from"`
    64  	To       string `json:"to"`
    65  	Value    string `json:"value"`
    66  	Err      string `json:"error"`
    67  	Function string `json:"function,omitempty"`
    68  	Args     string `json:"args,omitempty"`
    69  }
    70  
    71  // EventTriggerFunc export EventTriggerFunc
    72  //export EventTriggerFunc
    73  func EventTriggerFunc(handler unsafe.Pointer, topic, data *C.char, gasCnt *C.size_t) {
    74  	gTopic := C.GoString(topic)
    75  	gData := C.GoString(data)
    76  	var engine *V8Engine
    77  	e := getEngineByEngineHandler(handler)
    78  	if e == nil {
    79  		logging.VLog().WithFields(logrus.Fields{
    80  			"category": 0, // ChainEventCategory.
    81  			"topic":    gTopic,
    82  			"data":     gData,
    83  		}).Error("Event.Trigger delegate handler does not found.")
    84  		return
    85  	}
    86  	if e.ctx.head != nil {
    87  		engine = getEngineByEngineHandler(e.ctx.head)
    88  		if engine == nil {
    89  			logging.VLog().WithFields(logrus.Fields{
    90  				"category": 0, // ChainEventCategory.
    91  				"topic":    gTopic,
    92  				"data":     gData,
    93  			}).Error("Event.Trigger delegate head handler does not found.")
    94  			return
    95  		}
    96  	} else {
    97  		engine = e
    98  	}
    99  	// calculate Gas.
   100  	*gasCnt = C.size_t(EventBaseGasCount + len(gTopic) + len(gData))
   101  
   102  	var (
   103  		contractTopic string
   104  	)
   105  	// after split height, contract event track contract address
   106  	if core.NbreSplitAtHeight(e.ctx.block.Height()) {
   107  		contractTopic = EventNameSpaceContract + "." + e.ctx.contract.Address().String() + "." + gTopic
   108  	} else {
   109  		contractTopic = EventNameSpaceContract + "." + gTopic
   110  	}
   111  	event := &state.Event{Topic: contractTopic, Data: gData}
   112  	e.ctx.state.RecordEvent(engine.ctx.tx.Hash(), event)
   113  }