github.com/cheng762/platon-go@v1.8.17-0.20190529111256-7deff2d7be26/mpc/mpc_processor_on_windows_amd64.go (about)

     1  // +build mpcon
     2  
     3  package mpc
     4  
     5  /*
     6  #include <stdio.h>
     7  #include <stdlib.h>
     8  */
     9  import "C"
    10  
    11  import (
    12  	"github.com/PlatONnetwork/PlatON-Go/common"
    13  	"github.com/PlatONnetwork/PlatON-Go/log"
    14  	"path/filepath"
    15  	"syscall"
    16  	"unsafe"
    17  )
    18  
    19  var (
    20  	psdkdll					*syscall.DLL
    21  	securityInitProc		*syscall.Proc
    22  	securityCalculation		*syscall.Proc
    23  )
    24  
    25  type MPCParams struct {
    26  	TaskId		string
    27  	Pubkey 		string
    28  	From 		common.Address
    29  	IRAddr		common.Address
    30  	Method 		string
    31  	Extra 		string
    32  }
    33  
    34  func InitVM(icepath string, httpEndpoint string) {
    35  	// set default path of mpcLib.
    36  	workDir, _ := filepath.Abs("")
    37  	libPath := filepath.Join(workDir, "mpclib")
    38  
    39  	// init ptr
    40  	libPathPtr, _ := syscall.UTF16PtrFromString(libPath)
    41  	syscall.SetCurrentDirectory(libPathPtr)
    42  	psdkdll = syscall.MustLoadDLL("mpc_vm_platonsdk.dll")
    43  	securityInitProc = psdkdll.MustFindProc("notify_security_init")
    44  	securityCalculation = psdkdll.MustFindProc("notify_security_calculation")
    45  
    46  	// convert type
    47  	cCfg := C.CString(icepath)
    48  	cUrl := C.CString(httpEndpoint)
    49  
    50  	cfgUintPtr := uintptr(unsafe.Pointer(cCfg))
    51  	urlUintPtr := uintptr(unsafe.Pointer(cUrl))
    52  	syscall.Syscall(securityInitProc.Addr(), 2, cfgUintPtr, urlUintPtr, 0)
    53  
    54  	log.Info("Init mpc processor success", "osType", "window", "icepath", icepath, "httpEndpoint", httpEndpoint)
    55  	defer func() {
    56  		C.free(unsafe.Pointer(cCfg))
    57  		C.free(unsafe.Pointer(cUrl))
    58  	}()
    59  }
    60  
    61  func ExecuteMPCTx(params MPCParams) error {
    62  
    63  	defer func() {
    64  		if err := recover(); err != nil {
    65  			log.Error("execute mpc tx fail.", "err", err)
    66  		}
    67  	}()
    68  
    69  	cTaskId := C.CString(params.TaskId)
    70  	cPubKey := C.CString(params.Pubkey)
    71  	cAddr := C.CString(params.From.Hex())
    72  	cIRAddr := C.CString(params.IRAddr.Hex())
    73  	cMethod := C.CString(params.Method)
    74  	cExtra := C.CString(params.Extra)
    75  
    76  	// convert to uintptr
    77  	taskIdUintPtr := uintptr(unsafe.Pointer(cTaskId))
    78  	pubKeyUintPtr := uintptr(unsafe.Pointer(cPubKey))
    79  	addrUintPtr   := uintptr(unsafe.Pointer(cAddr))
    80  	irAddrUintPtr := uintptr(unsafe.Pointer(cIRAddr))
    81  	methodUintPtr := uintptr(unsafe.Pointer(cMethod))
    82  	extraUintPtr  := uintptr(unsafe.Pointer(cExtra))
    83  
    84  	syscall.Syscall6(securityCalculation.Addr(), 6, taskIdUintPtr, pubKeyUintPtr, addrUintPtr, irAddrUintPtr, methodUintPtr, extraUintPtr)
    85  
    86  	defer func() {
    87  		C.free(unsafe.Pointer(cTaskId))
    88  		C.free(unsafe.Pointer(cPubKey))
    89  		C.free(unsafe.Pointer(cAddr))
    90  		C.free(unsafe.Pointer(cIRAddr))
    91  		C.free(unsafe.Pointer(cMethod))
    92  		C.free(unsafe.Pointer(cExtra))
    93  	}()
    94  
    95  	log.Info("Notify mvm success, ExecuteMPCTx method invoke success.",
    96  		"taskId", params.TaskId,
    97  		"pubkey", params.Pubkey,
    98  		"from", params.From.Hex(),
    99  		"irAddr", params.IRAddr.Hex(),
   100  		"method", params.Method)
   101  
   102  	return nil
   103  }