Beispiel #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);
        }
Beispiel #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);
        }
Beispiel #3
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);
        }
Beispiel #4
0
        public bool Execute(DbSnapshot dbSnapshot, BlockSub transfer, long height, out object[] result)
        {
            LuaVMCall    luaVMCall    = new LuaVMCall();
            LuaVMScript  luaVMScript  = null;
            LuaVMContext LuaVMContext = null;

            result = null;
            try
            {
                string address = GetContractAddress(transfer);
                LuaEnv luaenv  = GetLuaEnv(address);

                //LuaEnv Global
                s_consAddress = address;
                s_transfer    = transfer;
                s_dbSnapshot  = dbSnapshot;

                luaenv.DoString(initScript);

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

                    luaVMScript = new LuaVMScript()
                    {
                        script = FileHelper.GetFileData($"./Data/Contract/{transfer.depend}.lua").ToByteArray()
                    };
                    LuaVMContext = new LuaVMContext()
                    {
                        jsonData = "{}".ToByteArray()
                    };
                    luaVMCall = LuaVMCall.Decode(transfer.data);
                    dbSnapshot.Contracts.Add(address, luaVMScript);
                    luaenv.DoString(luaVMScript.script);
                }
                else
                {
                    luaVMScript  = dbSnapshot.Contracts.Get(address);
                    LuaVMContext = dbSnapshot.Storages.Get(address);
                    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);
                result = luaFun?.Call(args);

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

                s_consAddress = "";
                s_dbSnapshot  = null;
                return(true);
            }
            catch (Exception e)
            {
                Log.Error(e);
                Log.Info($"LuaVMEnv.Execute Error, transfer.hash: {transfer.hash} , contract: {transfer.addressOut} func:{luaVMCall.fnName}");
            }
            s_consAddress = "";
            s_dbSnapshot  = null;
            return(false);
        }
Beispiel #5
0
        public void Execute(DbSnapshot dbSnapshot, Transfer transfer, long height)
        {
            LuaVMCall    luaVMCall    = new LuaVMCall();
            LuaVMScript  luaVMScript  = null;
            LuaVMContext LuaVMContext = null;

            try
            {
                s_dbSnapshot = dbSnapshot;
                string address = (transfer.addressOut == null || transfer.addressOut == "") ? Wallet.ToAddress(CryptoHelper.Sha256(Encoding.UTF8.GetBytes(transfer.data))) : transfer.addressOut;
                LuaEnv luaenv  = GetLuaEnv(address);

                // rapidjson待优化,改为直接在C#层调用
                luaenv.Global.Set("contractAddress", address);
                luaenv.Global.Set("dbSnapshot", dbSnapshot);

                luaenv.DoString(initScript);

                if (transfer.addressOut == null || transfer.addressOut == "")
                {
                    // 当前版本只能由superAddress发布智能合约
                    if (consensus.superAddress != transfer.addressIn)
                    {
                        return;
                    }

                    luaVMScript = new LuaVMScript()
                    {
                        script = Base58.Decode(transfer.data)
                    };
                    LuaVMContext = new LuaVMContext()
                    {
                        jsonData = "{}".ToByteArray()
                    };
                    luaVMCall.fnName = "Create";
                    luaVMCall.args   = new FieldParam[0];
                    if (dbSnapshot.Contracts.Get(address) == null)
                    {
                        dbSnapshot.Contracts.Add(address, luaVMScript);
                    }
                    luaenv.DoString(luaVMScript.script);
                }
                else
                {
                    luaVMScript  = dbSnapshot.Contracts.Get(address);
                    LuaVMContext = dbSnapshot.Storages.Get(address);
                    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.DoString($"curHeight    =  {height}\n");
                luaenv.DoString($"curAddress   = '{transfer.addressIn}'\n");
                luaFun.Call(args);

                // 待优化,改为直接在C#层调用
                luaenv.DoString("StoragesJson = rapidjson.encode(Storages)\n");
                LuaVMContext.jsonData = luaenv.Global.Get <string>("StoragesJson").ToByteArray();
                dbSnapshot.Storages.Add(address, LuaVMContext);
            }
            catch (Exception)
            {
                Log.Info($"LuaVMEnv.Execute Error, transfer.hash: {transfer.hash} , contract: {transfer.addressOut} func:{luaVMCall.fnName}");
            }
        }
Beispiel #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);
            }
        }
Beispiel #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);
                }
            }
        }