public static RpcApiResult CreateProposalContract(byte[] owner_address, Dictionary <long, long> parameters, out ProposalCreateContract contract) { contract = new ProposalCreateContract(); contract.OwnerAddress = ByteString.CopyFrom(owner_address); foreach (var parameter in parameters) { contract.Parameters.Add(parameter.Key, parameter.Value); } return(RpcApiResult.Success); }
public override bool Execute(TransactionResultCapsule result) { long fee = CalcFee(); try { ProposalCreateContract proposal_create_contract = this.contract.Unpack <ProposalCreateContract>(); long id = Deposit == null? this.db_manager.DynamicProperties.GetLatestProposalNum() + 1 : Deposit.GetLatestProposalNum() + 1; ProposalCapsule proposal = new ProposalCapsule(proposal_create_contract.OwnerAddress, id); proposal.Parameters = new Dictionary <long, long>(proposal_create_contract.Parameters); long now = this.db_manager.GetHeadBlockTimestamp(); long maintenance_interval = Deposit == null? this.db_manager.DynamicProperties.GetMaintenanceTimeInterval() : Deposit.GetMaintenanceTimeInterval(); proposal.CreateTime = now; long current_maintenance_time = Deposit == null? this.db_manager.DynamicProperties.GetNextMaintenanceTime() : Deposit.GetNextMaintenanceTime(); long now3 = now + (int)Args.Instance.Block.ProposalExpireTime; long round = (now3 - current_maintenance_time) / maintenance_interval; long expiration_time = current_maintenance_time + (round + 1) * maintenance_interval; proposal.ExpirationTime = expiration_time; if (Deposit == null) { this.db_manager.Proposal.Put(proposal.CreateDatabaseKey(), proposal); this.db_manager.DynamicProperties.PutLatestProposalNum(id); } else { Deposit.PutProposalValue(proposal.CreateDatabaseKey(), proposal); Deposit.PutDynamicPropertiesWithLatestProposalNum(id); } result.SetStatus(fee, code.Sucess); } catch (InvalidProtocolBufferException e) { Logger.Debug(e.Message); result.SetStatus(fee, code.Failed); throw new ContractExeException(e.Message); } return(true); }
public override bool Validate() { if (this.contract == null) { throw new ContractValidateException("No contract!"); } if (this.db_manager == null && (deposit == null || deposit.DBManager == null)) { throw new ContractValidateException("No this.db_manager!"); } if (this.contract.Is(ProposalCreateContract.Descriptor)) { ProposalCreateContract contract = null; try { contract = this.contract.Unpack <ProposalCreateContract>(); } catch (InvalidProtocolBufferException e) { throw new ContractValidateException(e.Message); } byte[] owner_address = contract.OwnerAddress.ToByteArray(); if (!Wallet.IsValidAddress(owner_address)) { throw new ContractValidateException("Invalid address"); } if (Deposit != null) { if (deposit.GetAccount(owner_address) == null) { throw new ContractValidateException( ActuatorParameter.ACCOUNT_EXCEPTION_STR + owner_address.ToHexString() + ActuatorParameter.NOT_EXIST_STR); } } else if (!this.db_manager.Account.Contains(owner_address)) { throw new ContractValidateException( ActuatorParameter.ACCOUNT_EXCEPTION_STR + owner_address.ToHexString() + ActuatorParameter.NOT_EXIST_STR); } if (Deposit != null) { if (Deposit.GetWitness(owner_address) == null) { throw new ContractValidateException( ActuatorParameter.WITNESS_EXCEPTION_STR + owner_address.ToHexString() + ActuatorParameter.NOT_EXIST_STR); } } else if (!this.db_manager.Witness.Contains(owner_address)) { throw new ContractValidateException( ActuatorParameter.WITNESS_EXCEPTION_STR + owner_address.ToHexString() + ActuatorParameter.NOT_EXIST_STR); } if (contract.Parameters.Count == 0) { throw new ContractValidateException("This proposal has no parameter."); } foreach (KeyValuePair <long, long> entry in contract.Parameters) { if (!ValidKey(entry.Key)) { throw new ContractValidateException("Bad chain parameter id"); } ValidateValue(entry); } } else { throw new ContractValidateException( "contract type error,expected type [ProposalCreateContract],real type[" + contract.GetType().Name + "]"); } return(true); }