Example #1
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}");
            }
        }