Пример #1
0
        static public void TransferEvent(string sender, string _to, string msg)
        {
            if (Entity.Root.GetComponent <Consensus>().transferShow)
            {
                // bind to account
                var consAddress = LuaVMStack.s_consAddress;
                var dbSnapshot  = LuaVMStack.s_dbSnapshot;
                var transfer    = LuaVMStack.s_transfer;
                if (!string.IsNullOrEmpty(consAddress))
                {
                    var mapIn = dbSnapshot.ABC.Get(sender) ?? new List <string>();
                    mapIn.Remove(consAddress);
                    mapIn.Add(consAddress);
                    dbSnapshot.ABC.Add(sender, mapIn);
                    if (!string.IsNullOrEmpty(_to))
                    {
                        var mapOut = dbSnapshot.ABC.Get(_to) ?? new List <string>();
                        mapOut.Remove(consAddress);
                        mapOut.Add(consAddress);
                        dbSnapshot.ABC.Add(_to, mapOut);

                        dbSnapshot.BindTransfer2Account($"{_to}{consAddress}", LuaVMStack.s_transfer.hash);
                    }
                }
                LuaVMStack.Add2TransferTemp(msg);
            }
        }
Пример #2
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);
                }
            }
        }