示例#1
0
        public Dictionary <string, RuleInfo> GetRules(string address, long height, bool bCommit = false)
        {
            Dictionary <string, RuleInfo> rules = new Dictionary <string, RuleInfo>();

            try
            {
                LuaEnv       luaenv       = GetLuaEnv(address);
                LuaVMCall    luaVMCall    = new LuaVMCall();
                LuaVMScript  luaVMScript  = null;
                LuaVMContext LuaVMContext = null;

                using (DbSnapshot dbSnapshot = Entity.Root.GetComponent <LevelDBStore>().GetSnapshot())
                {
                    s_dbSnapshot = dbSnapshot;
                    LuaVMContext Storages = dbSnapshot?.Storages.Get(address);
                    // rapidjson待优化,改为直接在C#层调用
                    luaenv.DoString(initScript);
                    luaVMScript  = dbSnapshot.Contracts.Get(address);
                    LuaVMContext = dbSnapshot.Storages.Get(address);
                    luaenv.DoString(luaVMScript.script);
                    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.DoString($"curHeight    =  {height}\n");
                    luaFun.Call(args);

                    // 待优化,改为直接在C#层调用
                    luaenv.DoString("StoragesJson = rapidjson.encode(Storages)\n");
                    LuaVMContext.jsonData = luaenv.Global.Get <string>("StoragesJson").ToByteArray();
                    if (bCommit)
                    {
                        dbSnapshot.Storages.Add(address, LuaVMContext);
                        dbSnapshot.Commit();
                    }

                    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.Start   = long.Parse(jdRule[ii]["Start"].ToString());
                        rule.End     = long.Parse(jdRule[ii]["End"].ToString());
                        rules.Remove(rule.Address);
                        rules.Add(rule.Address, rule);
                    }
                }
            }
            catch (Exception e)
            {
                Log.Debug(e.ToString());
                Log.Info("GetRules Error!");
            }
            return(rules);
        }
示例#2
0
        public bool IsRule(long height, string Address)
        {
            RuleInfo info = null;

            if (ruleInfos.TryGetValue(Address, out info))
            {
                if (info.Start <= height && (height < info.End || info.End == -1) && info.Address == Address)
                {
                    return(true);
                }
            }
            return(false);
        }
示例#3
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);
            }
        }