Exemplo n.º 1
0
 public static void Reset(BlockSub _transfer, LuaVMDB _dbSnapshot, string _consAddress, string _sender)
 {
     s_transfer   = _transfer;
     s_dbSnapshot = _dbSnapshot;
     s_maxDepth   = 0;
     queue.Clear();
     if (_consAddress != null && _sender != null)
     {
         Enqueue(_consAddress, _sender);
     }
 }
Exemplo n.º 2
0
        public bool LuaCreate(LuaVMDB dbSnapshot, string sender, string data, long timestamp, string depend, long height, out string consAddress)
        {
            LuaVMCall    luaVMCall    = new LuaVMCall();
            LuaVMScript  luaVMScript  = null;
            LuaVMContext LuaVMContext = null;
            LuaEnv       luaenv       = GetLuaEnv(depend, "LuaCreate");

            consAddress = GetContractAddress(sender, data, timestamp, depend);
            try
            {
                LuaVMStack.Enqueue(consAddress, sender);

                luaenv.DoString(initScript);

                luaVMScript = new LuaVMScript()
                {
                    script = FileHelper.GetFileData($"./Data/Contract/{depend}.lua").ToByteArray(), tablName = depend
                };
                LuaVMContext = new LuaVMContext()
                {
                    jsonData = "{}".ToByteArray()
                };
                luaVMCall = LuaVMCall.Decode(data);
                dbSnapshot.Contracts.Add(consAddress, luaVMScript);
                luaenv.DoString(luaVMScript.script);

                object[]    args   = luaVMCall.args.Select(a => a.GetValue()).ToArray();
                LuaFunction luaFun = luaenv.Global.Get <LuaFunction>(luaVMCall.fnName);
                luaenv.Global.Set("curHeight", height);
                luaenv.Global.Set("sender", sender);
                luaenv.Global.Set("addressThis", consAddress);
                if (luaFun != null)
                {
                    luaFun.Call(args);

                    // rapidjson
                    luaenv.DoString("StoragesJson = rapidjson.encode(Storages)\n");
                    LuaVMContext.jsonData = luaenv.Global.Get <string>("StoragesJson").ToByteArray();
                    dbSnapshot.Storages.Add(consAddress, LuaVMContext);

                    return(true);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                LuaVMStack.Dequeue();
            }
            return(false);
        }
Exemplo n.º 3
0
        public bool IsERC(LuaVMDB dbSnapshot, string consAddress, string scriptName)
        {
            var luaVMScript = LuaVMScript.Get(dbSnapshot, consAddress);

            if (luaVMScript != null)
            {
                if (string.IsNullOrEmpty(scriptName))
                {
                    return(true);
                }
                return(luaVMScript.tablName == scriptName);
            }
            return(false);
        }
Exemplo n.º 4
0
        static public LuaVMScript Get(LuaVMDB dbSnapshot, string consAddress)
        {
            var luaVMScript = dbSnapshot.Contracts.Get(consAddress);

            if (luaVMScript == null)
            {
                throw new Exception($"Smart Contract not exist: {consAddress}");
            }
            if (!string.IsNullOrEmpty(luaVMScript.tablName))
            {
                luaVMScript.script = FileHelper.GetFileData($"./Data/Contract/{luaVMScript.tablName}.lua").ToByteArray();
            }
            return(luaVMScript);
        }
Exemplo n.º 5
0
        public bool LuaCall(LuaVMDB dbSnapshot, string consAddress, string sender, string data, long height, out object[] result)
        {
            LuaVMCall    luaVMCall    = new LuaVMCall();
            LuaVMScript  luaVMScript  = null;
            LuaVMContext LuaVMContext = null;
            LuaEnv       luaenv       = GetLuaEnv(consAddress, "LuaCall");

            result = null;
            try
            {
                LuaVMStack.Enqueue(consAddress, sender);

                luaenv.DoString(initScript);

                luaVMScript  = LuaVMScript.Get(dbSnapshot, consAddress);
                LuaVMContext = dbSnapshot.Storages.Get(consAddress);
                luaVMCall    = LuaVMCall.Decode(data);
                luaenv.DoString(luaVMScript.script, consAddress);
                luaenv.DoString($"Storages = rapidjson.decode('{LuaVMContext.jsonData.ToStr()}')\n");

                object[]    args   = luaVMCall.args.Select(a => a.GetValue()).ToArray();
                LuaFunction luaFun = luaenv.Global.Get <LuaFunction>(luaVMCall.fnName);

                luaenv.Global.Set("curHeight", height);
                luaenv.Global.Set("sender", sender);
                luaenv.Global.Set("addressThis", consAddress);
                if (luaFun != null)
                {
                    result = luaFun.Call(args);

                    // rapidjson
                    luaenv.DoString("StoragesJson = rapidjson.encode(Storages)\n");
                    LuaVMContext.jsonData = luaenv.Global.Get <string>("StoragesJson").ToByteArray();
                    dbSnapshot.Storages.Add(consAddress, LuaVMContext);

                    return(true);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                LuaVMStack.Dequeue();
            }

            return(false);
        }
Exemplo n.º 6
0
        public Dictionary <string, RuleInfo> GetRules(string address, long height, DbSnapshot dbSnapshot)
        {
            lock (this)
            {
                Dictionary <string, RuleInfo> rules = new Dictionary <string, RuleInfo>();
                try
                {
                    LuaEnv       luaenv       = GetLuaEnv(address, "GetRules");
                    LuaVMCall    luaVMCall    = new LuaVMCall();
                    LuaVMScript  luaVMScript  = null;
                    LuaVMContext LuaVMContext = null;

                    var luaVMDB = new LuaVMDB(dbSnapshot);
                    LuaVMStack.Reset(null, luaVMDB, null, null);

                    LuaVMContext Storages = dbSnapshot?.Storages.Get(address);
                    // rapidjson
                    luaenv.DoString(initScript);
                    luaVMScript  = LuaVMScript.Get(dbSnapshot, address);
                    LuaVMContext = dbSnapshot.Storages.Get(address);
                    luaenv.DoString(luaVMScript.script, address);
                    luaenv.DoString($"Storages = rapidjson.decode('{LuaVMContext.jsonData.ToStr()}')\n");
                    luaVMCall.fnName = "update";
                    luaVMCall.args   = new FieldParam[0];

                    object[]    args   = luaVMCall.args.Select(a => a.GetValue()).ToArray();
                    LuaFunction luaFun = luaenv.Global.Get <LuaFunction>(luaVMCall.fnName);

                    luaenv.Global.Set("curHeight", height);
                    luaenv.Global.Set("sender", "");
                    luaenv.Global.Set("addressThis", address);
                    luaFun.Call(args);

                    // rapidjson
                    luaenv.DoString("StoragesJson = rapidjson.encode(Storages)\n");
                    LuaVMContext.jsonData = luaenv.Global.Get <string>("StoragesJson").ToByteArray();
                    dbSnapshot.Storages.Add(address, LuaVMContext);

                    JToken   jdStorages = JToken.Parse(LuaVMContext.jsonData.ToStr());
                    JToken[] jdRule     = jdStorages["Rules"].ToArray();
                    for (int ii = 0; ii < jdRule.Length; ii++)
                    {
                        RuleInfo rule = new RuleInfo();
                        rule.Address  = jdRule[ii]["Address"].ToString();
                        rule.Contract = jdRule[ii]["Contract"]?.ToString();
                        rule.Amount   = jdRule[ii]["Amount"]?.ToString();
                        rule.Start    = long.Parse(jdRule[ii]["Start"].ToString());
                        rule.End      = long.Parse(jdRule[ii]["End"].ToString());
                        rule.LBH      = long.Parse(jdRule[ii]["LBH"].ToString());
                        rules.Remove(rule.Address);
                        rules.Add(rule.Address, rule);
                    }
                    luaenv.GC();
                }
                catch (Exception e)
                {
                    Log.Debug(e.ToString());
                    Log.Info("GetRules Error!");
                }
                finally
                {
                    LuaVMStack.Reset(null, null, null, null);
                }
                return(rules);
            }
        }
Exemplo n.º 7
0
        public bool Execute(DbSnapshot dbSnapshot, BlockSub transfer, long height, out object[] result)
        {
            lock (this)
            {
                using (LuaVMDB luaVMDB = new LuaVMDB(dbSnapshot))
                {
                    result = null;
                    LuaVMCall    luaVMCall    = new LuaVMCall();
                    LuaVMScript  luaVMScript  = null;
                    LuaVMContext LuaVMContext = null;
                    try
                    {
                        string consAddress = GetContractAddress(transfer);
                        LuaEnv luaenv      = GetLuaEnv(consAddress, "Execute");

                        //LuaEnv Global
                        LuaVMStack.Reset(transfer, luaVMDB, consAddress, transfer.addressIn);

                        luaenv.DoString(initScript);

                        if (string.IsNullOrEmpty(transfer.addressOut))
                        {
                            // 已存在
                            if (luaVMDB.Contracts.Get(consAddress) != null)
                            {
                                return(false);
                            }

                            luaVMScript = new LuaVMScript()
                            {
                                script = FileHelper.GetFileData($"./Data/Contract/{transfer.depend}.lua").ToByteArray(), tablName = transfer.depend
                            };
                            LuaVMContext = new LuaVMContext()
                            {
                                jsonData = "{}".ToByteArray()
                            };
                            luaVMCall = LuaVMCall.Decode(transfer.data);
                            luaVMDB.Contracts.Add(consAddress, luaVMScript);
                            luaenv.DoString(luaVMScript.script);
                        }
                        else
                        {
                            luaVMScript  = LuaVMScript.Get(luaVMDB, consAddress);
                            LuaVMContext = luaVMDB.Storages.Get(consAddress);
                            luaVMCall    = LuaVMCall.Decode(transfer.data);
                            luaenv.DoString(luaVMScript.script, transfer.addressOut);
                            luaenv.DoString($"Storages = rapidjson.decode('{LuaVMContext.jsonData.ToStr()}')\n");
                        }

                        object[]    args   = luaVMCall.args.Select(a => a.GetValue()).ToArray();
                        LuaFunction luaFun = luaenv.Global.Get <LuaFunction>(luaVMCall.fnName);

                        luaenv.Global.Set("curHeight", height);
                        luaenv.Global.Set("sender", transfer.addressIn);
                        luaenv.Global.Set("addressThis", consAddress);
                        if (luaFun != null)
                        {
                            result = luaFun.Call(args);

                            // rapidjson
                            luaenv.DoString("StoragesJson = rapidjson.encode(Storages)\n");
                            LuaVMContext.jsonData = luaenv.Global.Get <string>("StoragesJson").ToByteArray();
                            luaVMDB.Storages.Add(consAddress, LuaVMContext);
                            luaVMDB.Commit();
                            luaenv.GC();

                            return(true);
                        }
                    }
                    catch (Exception e)
                    {
#if !RELEASE
                        Log.Error(e);
                        Log.Info($"LuaVMEnv.Execute Error, transfer.hash: {transfer.hash} , contract: {transfer.addressOut} func:{luaVMCall.fnName}");
#endif
                        var array = e.Message.Split("\n");
                        if (array != null && array.Length > 0)
                        {
                            array[0] = array[0].Replace("c# exception:XLua.LuaException: c# exception:System.Exception: ", "");
                            LuaVMStack.Add2TransferTemp(array[0]);
                        }
                    }
                    finally
                    {
                        LuaVMStack.Reset(null, null, null, null);
                    }
                    return(false);
                }
            }
        }