Exemplo n.º 1
0
        private bool Contract_Create(ExecutionEngine engine)
        {
            if (Trigger != TriggerType.Application)
            {
                return(false);
            }
            byte[] script = engine.CurrentContext.EvaluationStack.Pop().GetByteArray();
            if (script.Length > 1024 * 1024)
            {
                return(false);
            }
            ContractPropertyState contract_properties = (ContractPropertyState)(byte)engine.CurrentContext.EvaluationStack.Pop().GetBigInteger();
            UInt160       hash     = script.ToScriptHash();
            ContractState contract = Snapshot.Contracts.TryGet(hash);

            if (contract == null)
            {
                contract = new ContractState
                {
                    Script             = script,
                    ContractProperties = contract_properties
                };
                Snapshot.Contracts.Add(hash, contract);
                ContractsCreated.Add(hash, new UInt160(engine.CurrentContext.ScriptHash));
            }
            engine.CurrentContext.EvaluationStack.Push(StackItem.FromInterface(contract));
            return(true);
        }
Exemplo n.º 2
0
        private bool Contract_Migrate(ExecutionEngine engine)
        {
            if (Trigger != TriggerType.Application)
            {
                return(false);
            }
            byte[] script = engine.CurrentContext.EvaluationStack.Pop().GetByteArray();
            if (script.Length > 1024 * 1024)
            {
                return(false);
            }
            ContractPropertyState contract_properties = (ContractPropertyState)(byte)engine.CurrentContext.EvaluationStack.Pop().GetBigInteger();
            UInt160       hash     = script.ToScriptHash();
            ContractState contract = Snapshot.Contracts.TryGet(hash);

            if (contract == null)
            {
                contract = new ContractState
                {
                    Script             = script,
                    ContractProperties = contract_properties
                };
                Snapshot.Contracts.Add(hash, contract);
                ContractsCreated.Add(hash, new UInt160(engine.CurrentContext.ScriptHash));
                if (contract.HasStorage)
                {
                    foreach (var pair in Snapshot.Storages.Find(engine.CurrentContext.ScriptHash).ToArray())
                    {
                        Snapshot.Storages.Add(new StorageKey
                        {
                            ScriptHash = hash,
                            Key        = pair.Key.Key
                        }, new StorageItem
                        {
                            Value      = pair.Value.Value,
                            IsConstant = false
                        });
                    }
                }
            }
            engine.CurrentContext.EvaluationStack.Push(StackItem.FromInterface(contract));
            return(Contract_Destroy(engine));
        }
Exemplo n.º 3
0
        private bool Contract_Migrate(ExecutionEngine engine)
        {
            if (Trigger != TriggerType.Application)
            {
                return(false);
            }
            byte[] script = engine.CurrentContext.EvaluationStack.Pop().GetByteArray();
            if (script.Length > 1024 * 1024)
            {
                return(false);
            }
            ContractParameterType[] parameter_list = engine.CurrentContext.EvaluationStack.Pop().GetByteArray().Select(p => (ContractParameterType)p).ToArray();
            if (parameter_list.Length > 252)
            {
                return(false);
            }
            ContractParameterType return_type         = (ContractParameterType)(byte)engine.CurrentContext.EvaluationStack.Pop().GetBigInteger();
            ContractPropertyState contract_properties = (ContractPropertyState)(byte)engine.CurrentContext.EvaluationStack.Pop().GetBigInteger();

            if (engine.CurrentContext.EvaluationStack.Peek().GetByteArray().Length > 252)
            {
                return(false);
            }
            string name = Encoding.UTF8.GetString(engine.CurrentContext.EvaluationStack.Pop().GetByteArray());

            if (engine.CurrentContext.EvaluationStack.Peek().GetByteArray().Length > 252)
            {
                return(false);
            }
            string version = Encoding.UTF8.GetString(engine.CurrentContext.EvaluationStack.Pop().GetByteArray());

            if (engine.CurrentContext.EvaluationStack.Peek().GetByteArray().Length > 252)
            {
                return(false);
            }
            string author = Encoding.UTF8.GetString(engine.CurrentContext.EvaluationStack.Pop().GetByteArray());

            if (engine.CurrentContext.EvaluationStack.Peek().GetByteArray().Length > 252)
            {
                return(false);
            }
            string email = Encoding.UTF8.GetString(engine.CurrentContext.EvaluationStack.Pop().GetByteArray());

            if (engine.CurrentContext.EvaluationStack.Peek().GetByteArray().Length > 65536)
            {
                return(false);
            }
            string        description = Encoding.UTF8.GetString(engine.CurrentContext.EvaluationStack.Pop().GetByteArray());
            UInt160       hash        = script.ToScriptHash();
            ContractState contract    = Snapshot.Contracts.TryGet(hash);

            if (contract == null)
            {
                contract = new ContractState
                {
                    Script             = script,
                    ParameterList      = parameter_list,
                    ReturnType         = return_type,
                    ContractProperties = contract_properties,
                    Name        = name,
                    CodeVersion = version,
                    Author      = author,
                    Email       = email,
                    Description = description
                };
                Snapshot.Contracts.Add(hash, contract);
                ContractsCreated.Add(hash, new UInt160(engine.CurrentContext.ScriptHash));
                if (contract.HasStorage)
                {
                    foreach (var pair in Snapshot.Storages.Find(engine.CurrentContext.ScriptHash).ToArray())
                    {
                        Snapshot.Storages.Add(new StorageKey
                        {
                            ScriptHash = hash,
                            Key        = pair.Key.Key
                        }, new StorageItem
                        {
                            Value      = pair.Value.Value,
                            IsConstant = false
                        });
                    }
                }
            }
            engine.CurrentContext.EvaluationStack.Push(StackItem.FromInterface(contract));
            return(Contract_Destroy(engine));
        }
Exemplo n.º 4
0
        protected bool Contract_Migrate(ExecutionEngine engine)
        {
            if (Trigger != TriggerType.Application)
            {
                return(false);
            }
            byte[] script = engine.CurrentContext.EvaluationStack.Pop().GetByteArray();
            if (script.Length > 1024 * 1024)
            {
                return(false);
            }
            ContractParameterType[] parameter_list = engine.CurrentContext.EvaluationStack.Pop().GetByteArray().Select(p => (ContractParameterType)p).ToArray();
            if (parameter_list.Length > 252)
            {
                return(false);
            }
            ContractParameterType return_type         = (ContractParameterType)(byte)engine.CurrentContext.EvaluationStack.Pop().GetBigInteger();
            ContractPropertyState contract_properties = (ContractPropertyState)(byte)engine.CurrentContext.EvaluationStack.Pop().GetBigInteger();

            if (engine.CurrentContext.EvaluationStack.Peek().GetByteArray().Length > 252)
            {
                return(false);
            }
            string name = Encoding.UTF8.GetString(engine.CurrentContext.EvaluationStack.Pop().GetByteArray());

            if (engine.CurrentContext.EvaluationStack.Peek().GetByteArray().Length > 252)
            {
                return(false);
            }
            string version = Encoding.UTF8.GetString(engine.CurrentContext.EvaluationStack.Pop().GetByteArray());

            if (engine.CurrentContext.EvaluationStack.Peek().GetByteArray().Length > 252)
            {
                return(false);
            }
            string author = Encoding.UTF8.GetString(engine.CurrentContext.EvaluationStack.Pop().GetByteArray());

            if (engine.CurrentContext.EvaluationStack.Peek().GetByteArray().Length > 252)
            {
                return(false);
            }
            string email = Encoding.UTF8.GetString(engine.CurrentContext.EvaluationStack.Pop().GetByteArray());

            if (engine.CurrentContext.EvaluationStack.Peek().GetByteArray().Length > 65536)
            {
                return(false);
            }
            string description = Encoding.UTF8.GetString(engine.CurrentContext.EvaluationStack.Pop().GetByteArray());

            // 在应用链上更改合约要检查应用链的类型设置和所有员签名
            if (!Snapshot.Blockchain.ChainHash.Equals(UInt160.Zero))
            {
                AppChainState state = Snapshot.AppChainState.GetAndChange();
                if (state.Hash == null)
                {
                    return(false);
                }

                // 对于私有链,只有应用链的所有者有权限发布合约
                if (state.Type.HasFlag(AppChainType.Private) && !CheckWitness(engine, state.Owner))
                {
                    return(false);
                }
            }

            UInt160       hash     = script.ToScriptHash();
            ContractState contract = Snapshot.Contracts.TryGet(hash);

            if (contract == null)
            {
                contract = new ContractState
                {
                    Script             = script,
                    ParameterList      = parameter_list,
                    ReturnType         = return_type,
                    ContractProperties = contract_properties,
                    Name        = name,
                    CodeVersion = version,
                    Author      = author,
                    Email       = email,
                    Description = description
                };
                Snapshot.Contracts.Add(hash, contract);
                ContractsCreated.Add(hash, new UInt160(engine.CurrentContext.ScriptHash));
                if (contract.HasStorage)
                {
                    foreach (var pair in Snapshot.Storages.Find(engine.CurrentContext.ScriptHash).ToArray())
                    {
                        Snapshot.Storages.Add(new StorageKey
                        {
                            ScriptHash = hash,
                            Key        = pair.Key.Key
                        }, new StorageItem
                        {
                            Value      = pair.Value.Value,
                            IsConstant = false
                        });
                    }
                }
            }
            engine.CurrentContext.EvaluationStack.Push(StackItem.FromInterface(contract));
            return(Contract_Destroy(engine));
        }