예제 #1
0
        private Address PrivateDeploySystemSmartContract(Hash name, int category, byte[] code)
        {
            if (name != null)
            {
                Assert(State.NameAddressMapping[name] == null, "contract name already been registered");
            }

            var serialNumber = State.ContractSerialNumber.Value;

            // Increment
            State.ContractSerialNumber.Value = serialNumber + 1;
            var contractAddress = AddressHelper.BuildContractAddress(Context.ChainId, serialNumber);

            var codeHash = Hash.FromRawBytes(code);

            var info = new ContractInfo
            {
                SerialNumber = serialNumber,
                Owner        = Context.Sender,
                Category     = category,
                CodeHash     = codeHash
            };

            State.ContractInfos[contractAddress] = info;

            var reg = new SmartContractRegistration
            {
                Category = category,
                Code     = ByteString.CopyFrom(code),
                CodeHash = codeHash
            };

            State.SmartContractRegistrations[reg.CodeHash] = reg;

            Context.DeployContract(contractAddress, reg, name);

            Context.Fire(new ContractDeployed()
            {
                CodeHash = codeHash,
                Address  = contractAddress,
                Creator  = Context.Sender
            });

            Context.LogDebug(() => "BasicContractZero - Deployment ContractHash: " + codeHash.ToHex());
            Context.LogDebug(() => "BasicContractZero - Deployment success: " + contractAddress.GetFormatted());


            if (name != null)
            {
                State.NameAddressMapping[name] = contractAddress;
            }


            return(contractAddress);
        }
        private Address DeploySmartContract(Hash name, int category, byte[] code, bool isSystemContract,
                                            Address author)
        {
            if (name != null)
            {
                Assert(State.NameAddressMapping[name] == null, "contract name has already been registered before");
            }

            var codeHash = HashHelper.ComputeFrom(code);

            Assert(State.SmartContractRegistrations[codeHash] == null, "contract code has already been deployed before");

            var serialNumber = State.ContractSerialNumber.Value;

            // Increment
            State.ContractSerialNumber.Value = serialNumber + 1;
            var contractAddress = AddressHelper.BuildContractAddress(Context.ChainId, serialNumber);

            var info = new ContractInfo
            {
                SerialNumber     = serialNumber,
                Author           = author,
                Category         = category,
                CodeHash         = codeHash,
                IsSystemContract = isSystemContract,
                Version          = 1
            };

            State.ContractInfos[contractAddress] = info;

            var reg = new SmartContractRegistration
            {
                Category         = category,
                Code             = ByteString.CopyFrom(code),
                CodeHash         = codeHash,
                IsSystemContract = info.IsSystemContract,
                Version          = info.Version
            };

            State.SmartContractRegistrations[reg.CodeHash] = reg;

            Context.DeployContract(contractAddress, reg, name);

            Context.Fire(new ContractDeployed
            {
                CodeHash = codeHash,
                Address  = contractAddress,
                Author   = author,
                Version  = info.Version,
                Name     = name
            });

            Context.LogDebug(() => "BasicContractZero - Deployment ContractHash: " + codeHash.ToHex());
            Context.LogDebug(() => "BasicContractZero - Deployment success: " + contractAddress.ToBase58());

            if (name != null)
            {
                State.NameAddressMapping[name] = contractAddress;
            }

            return(contractAddress);
        }