github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/v8/engine.h (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
     6  // modify 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
    17  // <http://www.gnu.org/licenses/>.
    18  //
    19  
    20  #ifndef _NEBULAS_NF_NVM_V8_ENGINE_H_
    21  #define _NEBULAS_NF_NVM_V8_ENGINE_H_
    22  // #include <v8.h>
    23  
    24  #if BUILDING_DLL
    25  #define EXPORT __attribute__((__visibility__("default")))
    26  #else
    27  #define EXPORT
    28  #endif
    29  
    30  #ifdef __cplusplus
    31  extern "C" {
    32  #endif // __cplusplus
    33  
    34  #include <stddef.h>
    35  #include <stdint.h>
    36  #include <string.h>
    37  #include <stdbool.h>
    38  #include "lib/nvm_error.h"
    39  
    40  enum LogLevel {
    41    DEBUG = 1,
    42    WARN = 2,
    43    INFO = 3,
    44    ERROR = 4,
    45  };
    46  
    47  enum OptType {
    48    INSTRUCTION     = 1,
    49    INSTRUCTIONTS  = 2,
    50    RUNSCRIPT       = 3,
    51  };
    52  #define BUILD_FUNC_MASK         0x00
    53  #define BUILD_ALL             0xFFFFFFFFFFFFFFFF
    54  #define BUILD_MATH            0x0000000000000001
    55  #define BUILD_MATH_RANDOM       0x0000000000000002
    56  #define BUILD_BLOCKCHAIN        0x0000000000000004
    57  #define BUILD_BLOCKCHAIN_GET_RUN_SOURCE     0x0000000000000008
    58  #define BUILD_BLOCKCHAIN_RUN_CONTRACT   0x0000000000000010
    59  #define BUILD_DEFAULT_VER (BUILD_MATH | BUILD_BLOCKCHAIN)
    60  #define BUILD_INNER_VER  (BUILD_MATH | BUILD_MATH_RANDOM | BUILD_BLOCKCHAIN | BUILD_BLOCKCHAIN_GET_RUN_SOURCE | BUILD_BLOCKCHAIN_RUN_CONTRACT)
    61  
    62  // log
    63  typedef void (*LogFunc)(int level, const char *msg);
    64  EXPORT const char *GetLogLevelText(int level);
    65  EXPORT void InitializeLogger(LogFunc f);
    66  
    67  // event.
    68  typedef void (*EventTriggerFunc)(void *handler, const char *topic,
    69                                   const char *data, size_t *counterVal);
    70  EXPORT void InitializeEvent(EventTriggerFunc trigger);
    71  
    72  // storage
    73  typedef char *(*StorageGetFunc)(void *handler, const char *key,
    74                                  size_t *counterVal);
    75  typedef int (*StoragePutFunc)(void *handler, const char *key, const char *value,
    76                                size_t *counterVal);
    77  typedef int (*StorageDelFunc)(void *handler, const char *key,
    78                                size_t *counterVal);
    79  EXPORT void InitializeStorage(StorageGetFunc get, StoragePutFunc put,
    80                                StorageDelFunc del);
    81  
    82  // blockchain
    83  typedef char *(*GetTxByHashFunc)(void *handler, const char *hash,
    84                                   size_t *counterVal);
    85  typedef int (*GetAccountStateFunc)(void *handler, const char *address,
    86                                       size_t *counterVal, char **result, char **info);
    87  typedef int (*TransferFunc)(void *handler, const char *to, const char *value,
    88                              size_t *counterVal);
    89  typedef int (*VerifyAddressFunc)(void *handler, const char *address,
    90                                   size_t *counterVal);
    91  typedef int (*GetPreBlockHashFunc)(void *handler, unsigned long long offset, size_t *counterVal, char **result, char **info);
    92  
    93  typedef int (*GetPreBlockSeedFunc)(void *handler, unsigned long long offset, size_t *counterVal, char **result, char **info);
    94  
    95  typedef int (*GetLatestNebulasRankFunc)(void *handler, const char *address, size_t *counterVal, char **result, char **info);
    96  
    97  typedef int (*GetLatestNebulasRankSummaryFunc)(void *handler, size_t *counterVal, char **result, char **info);
    98  
    99  typedef char *(*GetContractSourceFunc)(void *handler, const char *address,
   100                                   size_t *counterVal);
   101  typedef char *(*InnerContractFunc)(void *handler, const char *address, const char *funcName, const char * v,
   102  		const char *args, size_t *gasCnt);
   103  
   104  EXPORT void InitializeBlockchain(GetTxByHashFunc getTx,
   105                                   GetAccountStateFunc getAccount,
   106                                   TransferFunc transfer,
   107                                   VerifyAddressFunc verifyAddress,
   108                                   GetPreBlockHashFunc getPreBlockHash,
   109                                   GetPreBlockSeedFunc getPreBlockSeed,
   110                                   GetContractSourceFunc contractSource,
   111                                   InnerContractFunc rMultContract,
   112                                   GetLatestNebulasRankFunc getLatestNR,
   113                                   GetLatestNebulasRankSummaryFunc getLatestNRSum);
   114  
   115  // crypto
   116  typedef char *(*Sha256Func)(const char *data, size_t *counterVal);
   117  typedef char *(*Sha3256Func)(const char *data, size_t *counterVal);
   118  typedef char *(*Ripemd160Func)(const char *data, size_t *counterVal);
   119  typedef char *(*RecoverAddressFunc)(int alg, const char *data, const char *sign,
   120                                   size_t *counterVal);
   121  typedef char *(*Md5Func)(const char *data, size_t *counterVal);
   122  typedef char *(*Base64Func)(const char *data, size_t *counterVal);
   123  
   124  EXPORT void InitializeCrypto(Sha256Func sha256,
   125                                   Sha3256Func sha3256,
   126                                   Ripemd160Func ripemd160,
   127                                   RecoverAddressFunc recoverAddress,
   128                                   Md5Func md5,
   129                                   Base64Func base64);
   130                                   
   131  
   132  // version
   133  EXPORT char *GetV8Version();
   134  
   135  // require callback.
   136  typedef char *(*RequireDelegate)(void *handler, const char *filename,
   137                                   size_t *lineOffset);
   138  typedef char *(*AttachLibVersionDelegate)(void *handler, const char *libname);
   139  
   140  EXPORT void InitializeRequireDelegate(RequireDelegate delegate, AttachLibVersionDelegate libDelegate);
   141  
   142  EXPORT void InitializeExecutionEnvDelegate(AttachLibVersionDelegate libDelegate);
   143  // random callback
   144  typedef int(*GetTxRandomFunc)(void *handler, size_t *gasCnt, char **result, char **exceptionInfo);
   145  EXPORT void InitializeRandom(GetTxRandomFunc delegate);
   146  
   147  typedef struct V8EngineStats {
   148    size_t count_of_executed_instructions;
   149    size_t total_memory_size;
   150    size_t total_heap_size;
   151    size_t total_heap_size_executable;
   152    size_t total_physical_size;
   153    size_t total_available_size;
   154    size_t used_heap_size;
   155    size_t heap_size_limit;
   156    size_t malloced_memory;
   157    size_t peak_malloced_memory;
   158    size_t total_array_buffer_size;
   159    size_t peak_array_buffer_size;
   160  } V8EngineStats;
   161  
   162  typedef struct V8Engine {
   163  
   164    void *isolate;
   165    void *allocator;
   166    size_t limits_of_executed_instructions;
   167    size_t limits_of_total_memory_size;
   168    bool is_requested_terminate_execution;
   169    bool is_unexpected_error_happen;
   170    bool is_inner_nvm_error_happen;
   171    int testing;
   172    int timeout;
   173    uint64_t ver;
   174    V8EngineStats stats;
   175   
   176  } V8Engine;
   177  typedef struct v8ThreadContextInput {
   178    uintptr_t lcs;  
   179    uintptr_t gcs;
   180    enum OptType opt;  
   181    int line_offset;
   182    int allow_usage;
   183    const char *source;
   184  } v8ThreadContextInput;
   185  typedef struct v8ThreadContextOutput {
   186    int ret;  //output
   187    int line_offset;
   188    char *result; //output
   189  } v8ThreadContextOutput;
   190  typedef struct v8ThreadContext_ {
   191    V8Engine *e; 
   192    v8ThreadContextInput input;
   193    v8ThreadContextOutput output;
   194    bool is_finished;
   195  } v8ThreadContext;
   196  
   197  EXPORT void Initialize();
   198  EXPORT void Dispose();
   199  
   200  EXPORT V8Engine *CreateEngine();
   201  
   202  EXPORT int RunScriptSource(char **result, V8Engine *e, const char *source,
   203                             int source_line_offset, uintptr_t lcsHandler,
   204                             uintptr_t gcsHandler);
   205  
   206  EXPORT char *InjectTracingInstructions(V8Engine *e, const char *source,
   207                                         int *source_line_offset,
   208                                         int strictDisallowUsage);
   209  
   210  EXPORT char *TranspileTypeScriptModule(V8Engine *e, const char *source,
   211                                         int *source_line_offset);
   212  
   213  EXPORT int IsEngineLimitsExceeded(V8Engine *e);
   214  
   215  EXPORT void ReadMemoryStatistics(V8Engine *e);
   216  
   217  EXPORT void TerminateExecution(V8Engine *e);
   218  
   219  EXPORT void DeleteEngine(V8Engine *e);
   220  
   221  EXPORT void ExecuteLoop(const char *file);
   222  
   223  EXPORT char *InjectTracingInstructionsThread(V8Engine *e, const char *source,
   224                                  int *source_line_offset,
   225                                  int allow_usage);
   226  EXPORT char *TranspileTypeScriptModuleThread(V8Engine *e, const char *source,
   227                                  int *source_line_offset);
   228  EXPORT int RunScriptSourceThread(char **result, V8Engine *e, const char *source,
   229                      int source_line_offset, uintptr_t lcs_handler,
   230                      uintptr_t gcs_handler);
   231  EXPORT void EnableInnerContract(V8Engine *e);
   232  
   233  void SetInnerContractErrFlag(V8Engine *e);
   234  
   235  bool CreateScriptThread(v8ThreadContext *pc);
   236  void SetRunScriptArgs(v8ThreadContext *pc, V8Engine *e, int opt, const char *source, int line_offset, int allow_usage);
   237  #ifdef __cplusplus
   238  }
   239  #endif // __cplusplus
   240  
   241  #endif // _NEBULAS_NF_NVM_V8_ENGINE_H_