Ejemplo n.º 1
0
        public byte[] Bytes()
        {
            TxParams        txParams = ToTransactionParam();
            TransactionUtil util     = new TransactionUtil();

            byte[] bytes = util.EncodeTransactionProto(txParams);
            return(bytes);
        }
Ejemplo n.º 2
0
        public override bool Validate()
        {
            if (this.contract == null)
            {
                throw new ContractValidateException("No contract!");
            }

            if (this.db_manager == null)
            {
                throw new ContractValidateException("No dbManager!");
            }

            if (this.contract.Is(WitnessUpdateContract.Descriptor))
            {
                WitnessUpdateContract witness_update_contract = null;
                try
                {
                    witness_update_contract = this.contract.Unpack <WitnessUpdateContract>();
                }
                catch (InvalidProtocolBufferException e)
                {
                    Logger.Debug(e.Message);
                    throw new ContractValidateException(e.Message);
                }

                byte[] owner_address = witness_update_contract.OwnerAddress.ToByteArray();
                if (!Wallet.IsValidAddress(owner_address))
                {
                    throw new ContractValidateException("Invalid address");
                }

                if (!this.db_manager.Account.Contains(owner_address))
                {
                    throw new ContractValidateException("account does not exist");
                }

                if (!TransactionUtil.ValidUrl(witness_update_contract.UpdateUrl.ToByteArray()))
                {
                    throw new ContractValidateException("Invalid url");
                }

                if (!this.db_manager.Witness.Contains(owner_address))
                {
                    throw new ContractValidateException("Witness does not exist");
                }
            }
            else
            {
                throw new ContractValidateException(
                          "contract type error,expected type [WitnessUpdateContract],real type[" + contract.GetType().Name + "]");
            }

            return(true);
        }
Ejemplo n.º 3
0
        public void Update()
        {
            if (!IsDirty)
            {
                return;
            }
            if (!IsValid())
            {
                return;
            }

            var _taskRepository = new TaskRepository();

            var dto = CreateDTOFromTask(this);

            TransactionUtil.DoTransactional(t =>
            {
                if (IsDeleted)
                {
                    foreach (Comment comment in Comments)
                    {
                        comment.MarkDeleted();
                        comment.Update(t);
                    }

                    _taskRepository.ExecuteDelete(dto.ID, t);
                }
                else
                {
                    if (IsNew)
                    {
                        _taskRepository.ExecuteInsert(dto, t);
                    }
                    else
                    {
                        _taskRepository.ExecuteUpdate(dto, t);
                    }
                }

                foreach (Comment comment in Comments)
                {
                    comment.Update(t);
                }

                foreach (Comment comment in Comments.DeleteComments)
                {
                    comment.Update(t);
                }

                Comments.DeleteComments.Clear();
            });
        }
Ejemplo n.º 4
0
        public void EncodeTransactionProto()
        {
            TxParams txParams = new TxParams();

            txParams.Version      = "0";
            txParams.Nonce        = "0";
            txParams.ToAddr       = "2E3C9B415B19AE4035503A06192A0FAD76E04243";
            txParams.SenderPubKey = "0246e7178dc8253201101e18fd6f6eb9972451d121fc57aa2a06dd5c111e58dc6a";
            txParams.Amount       = "10000";
            txParams.GasPrice     = "100";
            txParams.GasLimit     = "1000";
            txParams.Code         = "";
            txParams.Data         = "";

            TransactionUtil util = new TransactionUtil();

            byte[] bytes = util.EncodeTransactionProto(txParams);
            Console.WriteLine(ByteUtil.ByteArrayToHexString(bytes));
        }
Ejemplo n.º 5
0
        public void DeleteUser(int id)
        {
            try
            {
                TransactionUtil.OnEntry();

                this.userDao.DeleteUser(id);

                TransactionUtil.OnSuccess();
            }
            catch (Exception)
            {
                TransactionUtil.OnException();
                throw;
            }
            finally
            {
                TransactionUtil.OnExit();
            }
        }
Ejemplo n.º 6
0
        public void UpdatePassword(int id, string loginPwd)
        {
            try
            {
                TransactionUtil.OnEntry();

                this.userDao.UpdatePassword(id, loginPwd);

                TransactionUtil.OnSuccess();
            }
            catch (Exception)
            {
                TransactionUtil.OnException();
                throw;
            }
            finally
            {
                TransactionUtil.OnExit();
            }
        }
Ejemplo n.º 7
0
 public void WorkThreadFunction()
 {
     try
     {
         TransactionUtil.transfer2Server();
         TransactionUtil.downloadStation();
         TransactionUtil.downloadAuthenCode();
         TransactionUtil.downloadUsers();
         TransactionUtil.downloadLounge();
         TransactionUtil.downloadArea();
         TransactionUtil.downloadGroup();
         TransactionUtil.downloadRole();
         // do any background work
     }
     catch (Exception ex)
     {
         // log errors
         logger.Equals(ex.Message);
     }
 }
Ejemplo n.º 8
0
        public void UpdateUser(UserInfo info)
        {
            try
            {
                TransactionUtil.OnEntry();

                this.userDao.UpdateUser(info);

                TransactionUtil.OnSuccess();
            }
            catch (Exception)
            {
                TransactionUtil.OnException();
                throw;
            }
            finally
            {
                TransactionUtil.OnExit();
            }
        }
Ejemplo n.º 9
0
        static RawTransactionSignerDelegate CreateRawTransactionSigner()
        {
            return(new RawTransactionSignerDelegate(async(rpcClient, txParams) =>
            {
                var account = GlobalVariables.AccountKeys.First(a => a.Address == txParams.From.Value);
                var nonce = await rpcClient.GetTransactionCount(account.Address, BlockParameterType.Pending);
                var chainID = GlobalVariables.ChainID;

                byte[] signedTxBytes = TransactionUtil.SignRawTransaction(
                    account.Account,
                    nonce,
                    (BigInteger?)txParams.GasPrice ?? 0,
                    (BigInteger?)txParams.Gas ?? 0,
                    txParams.To,
                    (BigInteger?)txParams.Value ?? 0,
                    txParams.Data,
                    chainID);

                return signedTxBytes;
            }));
        }
Ejemplo n.º 10
0
        public UserInfo AddUser(UserInfo info)
        {
            try
            {
                TransactionUtil.OnEntry();

                info = this.userDao.AddUser(info);

                TransactionUtil.OnSuccess();
            }
            catch (Exception)
            {
                TransactionUtil.OnException();
                throw;
            }
            finally
            {
                TransactionUtil.OnExit();
            }

            return(info);
        }
Ejemplo n.º 11
0
        public override bool Validate()
        {
            if (this.contract == null)
            {
                throw new ContractValidateException("No contract!");
            }

            if (this.db_manager == null)
            {
                throw new ContractValidateException("No this.db_manager!");
            }

            if (this.contract.Is(UpdateAssetContract.Descriptor))
            {
                UpdateAssetContract update_asset_contract = null;
                try
                {
                    update_asset_contract = this.contract.Unpack <UpdateAssetContract>();
                }
                catch (InvalidProtocolBufferException e)
                {
                    Logger.Debug(e.Message);
                    throw new ContractValidateException(e.Message);
                }

                long       new_limit        = update_asset_contract.NewLimit;
                long       new_public_limit = update_asset_contract.NewPublicLimit;
                byte[]     owner_address    = update_asset_contract.OwnerAddress.ToByteArray();
                ByteString new_url          = update_asset_contract.Url;
                ByteString new_description  = update_asset_contract.Description;

                if (!Wallet.IsValidAddress(owner_address))
                {
                    throw new ContractValidateException("Invalid owner_address");
                }

                AccountCapsule account = this.db_manager.Account.Get(owner_address);
                if (account == null)
                {
                    throw new ContractValidateException("Account has not existed");
                }

                if (this.db_manager.DynamicProperties.GetAllowSameTokenName() == 0)
                {
                    if (account.AssetIssuedName.IsEmpty)
                    {
                        throw new ContractValidateException("Account has not issue any asset");
                    }

                    if (this.db_manager.AssetIssue.Get(account.AssetIssuedName.ToByteArray()) == null)
                    {
                        throw new ContractValidateException("Asset not exists in AssetIssueStore");
                    }
                }
                else
                {
                    if (account.AssetIssuedID.IsEmpty)
                    {
                        throw new ContractValidateException("Account has not issue any asset");
                    }

                    if (this.db_manager.AssetIssueV2.Get(account.AssetIssuedID.ToByteArray()) == null)
                    {
                        throw new ContractValidateException("Asset not exists  in AssetIssueV2Store");
                    }
                }

                if (!TransactionUtil.ValidUrl(new_url.ToByteArray()))
                {
                    throw new ContractValidateException("Invalid url");
                }

                if (!TransactionUtil.ValidAssetDescription(new_description.ToByteArray()))
                {
                    throw new ContractValidateException("Invalid description");
                }

                if (new_limit < 0 || new_limit >= this.db_manager.DynamicProperties.GetOneDayNetLimit())
                {
                    throw new ContractValidateException("Invalid FreeAssetNetLimit");
                }

                if (new_public_limit < 0 || new_public_limit >= this.db_manager.DynamicProperties.GetOneDayNetLimit())
                {
                    throw new ContractValidateException("Invalid PublicFreeAssetNetLimit");
                }
            }
            else
            {
                throw new ContractValidateException(
                          "contract type error,expected type [UpdateAssetContract],real type[" + contract.GetType().Name + "]");
            }

            return(true);
        }
Ejemplo n.º 12
0
        public override bool Validate()
        {
            if (this.contract == null)
            {
                throw new ContractValidateException("No contract!");
            }
            if (this.db_manager == null)
            {
                throw new ContractValidateException("No dbManager!");
            }

            if (this.contract.Is(WitnessCreateContract.Descriptor))
            {
                WitnessCreateContract witness_create_contract = null;
                try
                {
                    witness_create_contract = this.contract.Unpack <WitnessCreateContract>();
                }
                catch (InvalidProtocolBufferException e)
                {
                    throw new ContractValidateException(e.Message);
                }

                byte[] owner_address     = witness_create_contract.OwnerAddress.ToByteArray();
                string owner_address_str = owner_address.ToHexString();

                if (!Wallet.IsValidAddress(owner_address))
                {
                    throw new ContractValidateException("Invalid address");
                }

                if (!TransactionUtil.ValidUrl(witness_create_contract.Url.ToByteArray()))
                {
                    throw new ContractValidateException("Invalid url");
                }

                AccountCapsule account = this.db_manager.Account.Get(owner_address);

                if (account == null)
                {
                    throw new ContractValidateException("account[" + owner_address_str + "] not exists");
                }


                if (this.db_manager.Witness.Contains(owner_address))
                {
                    throw new ContractValidateException("Witness[" + owner_address_str + "] has existed");
                }

                if (account.Balance < this.db_manager.DynamicProperties.GetAccountUpgradeCost())
                {
                    throw new ContractValidateException("balance < AccountUpgradeCost");
                }
            }
            else
            {
                throw new ContractValidateException(
                          "contract type error,expected type [WitnessCreateContract],real type[" + this.contract.GetType().Name + "]");
            }

            return(true);
        }
Ejemplo n.º 13
0
        public override bool Validate()
        {
            if (this.contract == null)
            {
                throw new ContractValidateException("No contract!");
            }

            if (this.db_manager == null)
            {
                throw new ContractValidateException("No this.db_manager!");
            }

            if (!this.contract.Is(ExchangeCreateContract.Descriptor))
            {
                ExchangeCreateContract contract = null;

                try
                {
                    contract = this.contract.Unpack <ExchangeCreateContract>();
                }
                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 (!this.db_manager.Account.Contains(owner_address))
                {
                    throw new ContractValidateException("account[" + owner_address.ToHexString() + "] not exists");
                }

                AccountCapsule account = this.db_manager.Account.Get(owner_address);

                if (account.Balance < CalcFee())
                {
                    throw new ContractValidateException("No enough balance for exchange create fee!");
                }

                byte[] first_token_id       = contract.FirstTokenId.ToByteArray();
                byte[] secodn_token_id      = contract.SecondTokenId.ToByteArray();
                long   first_token_balance  = contract.FirstTokenBalance;
                long   second_token_balance = contract.SecondTokenBalance;

                if (this.db_manager.DynamicProperties.GetAllowSameTokenName() == 1)
                {
                    if (!first_token_id.SequenceEqual(COMPARE_CHARICTOR) && !TransactionUtil.IsNumber(first_token_id))
                    {
                        throw new ContractValidateException("first token id is not a valid number");
                    }
                    if (!secodn_token_id.SequenceEqual(COMPARE_CHARICTOR) && !TransactionUtil.IsNumber(secodn_token_id))
                    {
                        throw new ContractValidateException("second token id is not a valid number");
                    }
                }

                if (first_token_id.SequenceEqual(secodn_token_id))
                {
                    throw new ContractValidateException("cannot exchange same tokens");
                }

                if (first_token_balance <= 0 || second_token_balance <= 0)
                {
                    throw new ContractValidateException("token balance must greater than zero");
                }

                long balance_limit = this.db_manager.DynamicProperties.GetExchangeBalanceLimit();
                if (first_token_balance > balance_limit || second_token_balance > balance_limit)
                {
                    throw new ContractValidateException("token balance must less than " + balance_limit);
                }

                if (first_token_id.SequenceEqual(COMPARE_CHARICTOR))
                {
                    if (account.Balance < (first_token_balance + CalcFee()))
                    {
                        throw new ContractValidateException("balance is not enough");
                    }
                }
                else
                {
                    if (!account.AssetBalanceEnoughV2(first_token_id, first_token_balance, this.db_manager))
                    {
                        throw new ContractValidateException("first token balance is not enough");
                    }
                }

                if (secodn_token_id.SequenceEqual(COMPARE_CHARICTOR))
                {
                    if (account.Balance < (second_token_balance + CalcFee()))
                    {
                        throw new ContractValidateException("balance is not enough");
                    }
                }
                else
                {
                    if (!account.AssetBalanceEnoughV2(secodn_token_id, second_token_balance, this.db_manager))
                    {
                        throw new ContractValidateException("second token balance is not enough");
                    }
                }
            }
            else
            {
                throw new ContractValidateException(
                          "contract type error,expected type [ExchangeCreateContract],real type[" + this.contract.GetType().Name + "]");
            }

            return(true);
        }
Ejemplo n.º 14
0
        public override bool Validate()
        {
            if (this.contract == null)
            {
                throw new ContractValidateException("No contract!");
            }

            if (this.db_manager == null)
            {
                throw new ContractValidateException("No dbManager!");
            }

            if (this.contract.Is(AssetIssueContract.Descriptor))
            {
                AssetIssueContract asset_issue_contract = null;
                try
                {
                    asset_issue_contract = this.contract.Unpack <AssetIssueContract>();
                }
                catch (InvalidProtocolBufferException e)
                {
                    Logger.Debug(e.Message);
                    throw new ContractValidateException(e.Message);
                }

                byte[] owner_address = asset_issue_contract.OwnerAddress.ToByteArray();
                if (!Wallet.IsValidAddress(owner_address))
                {
                    throw new ContractValidateException("Invalid ownerAddress");
                }

                if (!TransactionUtil.ValidAssetName(asset_issue_contract.Name.ToByteArray()))
                {
                    throw new ContractValidateException("Invalid assetName");
                }

                if (this.db_manager.DynamicProperties.GetAllowSameTokenName() != 0)
                {
                    string name = asset_issue_contract.Name.ToStringUtf8().ToLower();
                    if (name.Equals("tx"))
                    {
                        throw new ContractValidateException("assetName can't be tx");
                    }
                }

                int precision = asset_issue_contract.Precision;
                if (precision != 0 && this.db_manager.DynamicProperties.GetAllowSameTokenName() != 0)
                {
                    if (precision < 0 || precision > 6)
                    {
                        throw new ContractValidateException("precision cannot exceed 6");
                    }
                }

                if ((!asset_issue_contract.Abbr.IsEmpty) && !TransactionUtil.ValidAssetName(asset_issue_contract.Abbr.ToByteArray()))
                {
                    throw new ContractValidateException("Invalid abbreviation for token");
                }

                if (!TransactionUtil.ValidUrl(asset_issue_contract.Url.ToByteArray()))
                {
                    throw new ContractValidateException("Invalid url");
                }

                if (!TransactionUtil.ValidAssetDescription(asset_issue_contract.Description.ToByteArray()))
                {
                    throw new ContractValidateException("Invalid description");
                }

                if (asset_issue_contract.StartTime == 0)
                {
                    throw new ContractValidateException("Start time should be not empty");
                }
                if (asset_issue_contract.EndTime == 0)
                {
                    throw new ContractValidateException("End time should be not empty");
                }
                if (asset_issue_contract.EndTime <= asset_issue_contract.StartTime)
                {
                    throw new ContractValidateException("End time should be greater than start time");
                }
                if (asset_issue_contract.StartTime <= this.db_manager.GetHeadBlockTimestamp())
                {
                    throw new ContractValidateException("Start time should be greater than HeadBlockTime");
                }

                if (this.db_manager.DynamicProperties.GetAllowSameTokenName() == 0 &&
                    this.db_manager.AssetIssue.Get(asset_issue_contract.Name.ToByteArray()) != null)
                {
                    throw new ContractValidateException("Token exists");
                }

                if (asset_issue_contract.TotalSupply <= 0)
                {
                    throw new ContractValidateException("TotalSupply must greater than 0!");
                }

                if (asset_issue_contract.TrxNum <= 0)
                {
                    throw new ContractValidateException("TrxNum must greater than 0!");
                }

                if (asset_issue_contract.Num <= 0)
                {
                    throw new ContractValidateException("Num must greater than 0!");
                }

                if (asset_issue_contract.PublicFreeAssetNetUsage != 0)
                {
                    throw new ContractValidateException("PublicFreeAssetNetUsage must be 0!");
                }

                if (asset_issue_contract.FrozenSupply.Count > this.db_manager.DynamicProperties.GetMaxFrozenSupplyNumber())
                {
                    throw new ContractValidateException("Frozen supply list length is too long");
                }

                if (asset_issue_contract.FreeAssetNetLimit < 0 ||
                    asset_issue_contract.FreeAssetNetLimit >= this.db_manager.DynamicProperties.GetOneDayNetLimit())
                {
                    throw new ContractValidateException("Invalid FreeAssetNetLimit");
                }

                if (asset_issue_contract.PublicFreeAssetNetLimit < 0 ||
                    asset_issue_contract.PublicFreeAssetNetLimit >= this.db_manager.DynamicProperties.GetOneDayNetLimit())
                {
                    throw new ContractValidateException("Invalid PublicFreeAssetNetLimit");
                }

                long remain_supply   = asset_issue_contract.TotalSupply;
                long min_frozen_time = this.db_manager.DynamicProperties.GetMinFrozenSupplyTime();
                long max_frozen_time = this.db_manager.DynamicProperties.GetMaxFrozenSupplyTime();

                foreach (AssetIssueContract.Types.FrozenSupply frozen in asset_issue_contract.FrozenSupply)
                {
                    if (frozen.FrozenAmount <= 0)
                    {
                        throw new ContractValidateException("Frozen supply must be greater than 0!");
                    }
                    if (frozen.FrozenAmount > remain_supply)
                    {
                        throw new ContractValidateException("Frozen supply cannot exceed total supply");
                    }
                    if (!(frozen.FrozenDays >= min_frozen_time &&
                          frozen.FrozenDays <= max_frozen_time))
                    {
                        throw new ContractValidateException(
                                  "frozenDuration must be less than " + max_frozen_time + " days "
                                  + "and more than " + min_frozen_time + " days");
                    }
                    remain_supply -= frozen.FrozenAmount;
                }

                AccountCapsule account = this.db_manager.Account.Get(owner_address);
                if (account == null)
                {
                    throw new ContractValidateException("Account not exists");
                }

                if (!account.AssetIssuedName.IsEmpty)
                {
                    throw new ContractValidateException("An account can only issue one asset");
                }

                if (account.Balance < CalcFee())
                {
                    throw new ContractValidateException("No enough balance for fee!");
                }
            }
            else
            {
                throw new ContractValidateException(
                          "contract type error,expected type [AssetIssueContract],real type[" + contract.GetType().Name + "]");
            }

            return(true);
        }
Ejemplo n.º 15
0
        public override bool Validate()
        {
            if (this.contract == null)
            {
                throw new ContractValidateException("No contract!");
            }

            if (this.db_manager == null)
            {
                throw new ContractValidateException("No this.db_manager!");
            }

            if (this.contract.Is(ExchangeWithdrawContract.Descriptor))
            {
                ExchangeWithdrawContract contract = null;
                try
                {
                    contract = this.contract.Unpack <ExchangeWithdrawContract>();
                }
                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 (!this.db_manager.Account.Contains(owner_address))
                {
                    throw new ContractValidateException("account[" + owner_address.ToHexString() + "] not exists");
                }

                AccountCapsule account = this.db_manager.Account.Get(owner_address);

                if (account.Balance < CalcFee())
                {
                    throw new ContractValidateException("No enough balance for exchange withdraw fee!");
                }

                ExchangeCapsule exchange = null;
                try
                {
                    exchange = this.db_manager.ExchangeFinal.Get(BitConverter.GetBytes(contract.ExchangeId));
                }
                catch (ItemNotFoundException e)
                {
                    throw new ContractValidateException("Exchange[" + contract.ExchangeId + "] not exists", e);
                }

                if (!account.Address.Equals(exchange.CreatorAddress))
                {
                    throw new ContractValidateException("account[" + owner_address.ToHexString() + "] is not creator");
                }

                byte[] first_token_id       = exchange.FirstTokenId.ToByteArray();
                byte[] second_token_id      = exchange.SecondTokenId.ToByteArray();
                long   first_token_balance  = exchange.FirstTokenBalance;
                long   second_token_balance = exchange.SecondTokenBalance;
                byte[] token_id             = contract.TokenId.ToByteArray();
                long   token_quantity       = contract.Quant;

                long other_token_quantity = 0;
                if (this.db_manager.DynamicProperties.GetAllowSameTokenName() == 1)
                {
                    if (!token_id.SequenceEqual(COMPARE_CHARICTOR) && !TransactionUtil.IsNumber(token_id))
                    {
                        throw new ContractValidateException("token id is not a valid number");
                    }
                }

                if (!token_id.SequenceEqual(first_token_id) && !token_id.SequenceEqual(second_token_id))
                {
                    throw new ContractValidateException("token is not in exchange");
                }

                if (token_quantity <= 0)
                {
                    throw new ContractValidateException("withdraw token quant must greater than zero");
                }

                if (first_token_balance == 0 || second_token_balance == 0)
                {
                    throw new ContractValidateException("Token balance in exchange is equal with 0,"
                                                        + "the exchange has been closed");
                }


                BigDecimal first_balance  = new BigDecimal(first_token_balance);
                BigDecimal second_balance = new BigDecimal(second_token_balance);
                BigDecimal bigTokenQuant  = new BigDecimal(token_quantity);
                if (token_id.SequenceEqual(first_token_id))
                {
                    other_token_quantity = second_balance.Multiply(bigTokenQuant)
                                           .DivideToIntegralValue(first_balance).ToInt64();

                    if (first_token_balance < token_quantity || second_token_balance < other_token_quantity)
                    {
                        throw new ContractValidateException("exchange balance is not enough");
                    }

                    if (other_token_quantity <= 0)
                    {
                        throw new ContractValidateException("withdraw another token quant must greater than zero");
                    }

                    double remainder = second_balance.Multiply(bigTokenQuant)
                                       .Divide(first_balance, 4, RoundingMode.HalfUp).ToDouble();
                    remainder -= other_token_quantity;

                    if (remainder / other_token_quantity > 0.0001)
                    {
                        throw new ContractValidateException("Not precise enough");
                    }
                }
                else
                {
                    other_token_quantity = first_balance.Multiply(bigTokenQuant)
                                           .DivideToIntegralValue(second_balance).ToInt64();

                    if (second_token_balance < token_quantity || first_token_balance < other_token_quantity)
                    {
                        throw new ContractValidateException("exchange balance is not enough");
                    }

                    if (other_token_quantity <= 0)
                    {
                        throw new ContractValidateException("withdraw another token quant must greater than zero");
                    }

                    double remainder = first_balance.Multiply(bigTokenQuant)
                                       .Divide(second_balance, 4, RoundingMode.HalfUp).ToDouble();
                    remainder -= other_token_quantity;

                    if (remainder / other_token_quantity > 0.0001)
                    {
                        throw new ContractValidateException("Not precise enough");
                    }
                }
            }
            else
            {
                throw new ContractValidateException(
                          "contract type error,expected type [ExchangeWithdrawContract],real type[" + this.contract.GetType().Name + "]");
            }

            return(true);
        }
Ejemplo n.º 16
0
        public override bool Validate()
        {
            if (this.contract == null)
            {
                throw new ContractValidateException("No contract!");
            }

            if (this.db_manager == null)
            {
                throw new ContractValidateException("No this.db_manager!");
            }

            if (!this.contract.Is(ExchangeInjectContract.Descriptor))
            {
                ExchangeInjectContract contract;
                try
                {
                    contract = this.contract.Unpack <ExchangeInjectContract>();
                }
                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 (!this.db_manager.Account.Contains(owner_address))
                {
                    throw new ContractValidateException("account[" + owner_address.ToHexString() + "] not exists");
                }

                AccountCapsule account = this.db_manager.Account.Get(owner_address);

                if (account.Balance < CalcFee())
                {
                    throw new ContractValidateException("No enough balance for exchange inject fee!");
                }

                ExchangeCapsule exchange = null;

                try
                {
                    exchange = this.db_manager.ExchangeFinal.Get(BitConverter.GetBytes(contract.ExchangeId));
                }
                catch (ItemNotFoundException e)
                {
                    throw new ContractValidateException("Exchange[" + contract.ExchangeId + "] not exists", e);
                }

                if (!account.Address.Equals(exchange.CreatorAddress))
                {
                    throw new ContractValidateException("account[" + owner_address.ToHexString() + "] is not creator");
                }

                byte[] first_token_id       = exchange.FirstTokenId.ToByteArray();
                byte[] second_token_id      = exchange.SecondTokenId.ToByteArray();
                long   first_token_balance  = exchange.FirstTokenBalance;
                long   second_token_balance = exchange.SecondTokenBalance;

                byte[] token_id       = contract.TokenId.ToByteArray();
                long   token_quantity = contract.Quant;

                byte[] other_token_id       = null;
                long   other_token_quantity = 0;

                if (this.db_manager.DynamicProperties.GetAllowSameTokenName() == 1)
                {
                    if (!token_id.SequenceEqual(COMPARE_CHARICTOR) && !TransactionUtil.IsNumber(token_id))
                    {
                        throw new ContractValidateException("token id is not a valid number");
                    }
                }

                if (!token_id.SequenceEqual(first_token_id) && !token_id.SequenceEqual(second_token_id))
                {
                    throw new ContractValidateException("token id is not in exchange");
                }

                if (first_token_balance == 0 || second_token_balance == 0)
                {
                    throw new ContractValidateException("Token balance in exchange is equal with 0,"
                                                        + "the exchange has been closed");
                }

                if (token_quantity <= 0)
                {
                    throw new ContractValidateException("injected token quant must greater than zero");
                }

                BigInteger first_balance           = new BigInteger(first_token_balance);
                BigInteger second_balance          = new BigInteger(second_token_balance);
                BigInteger quantity                = new BigInteger(token_quantity);
                long       new_token_balance       = 0;
                long       new_other_token_balance = 0;
                if (token_id.SequenceEqual(first_token_id))
                {
                    other_token_id          = second_token_id;
                    other_token_quantity    = (long)BigInteger.Multiply(second_balance, token_quantity);
                    other_token_quantity    = (long)BigInteger.Divide(other_token_quantity, first_balance);
                    new_token_balance       = first_token_balance + token_quantity;
                    new_other_token_balance = second_token_balance + other_token_quantity;
                }
                else
                {
                    other_token_id          = first_token_id;
                    other_token_quantity    = (long)BigInteger.Multiply(first_balance, token_quantity);
                    other_token_quantity    = (long)BigInteger.Divide(other_token_quantity, second_balance);
                    new_token_balance       = second_token_balance + token_quantity;
                    new_other_token_balance = first_token_balance + other_token_quantity;
                }

                if (other_token_quantity <= 0)
                {
                    throw new ContractValidateException("the calculated token quant  must be greater than 0");
                }

                long balance_limit = this.db_manager.DynamicProperties.GetExchangeBalanceLimit();
                if (new_token_balance > balance_limit || new_other_token_balance > balance_limit)
                {
                    throw new ContractValidateException("token balance must less than " + balance_limit);
                }

                if (token_id.SequenceEqual(COMPARE_CHARICTOR))
                {
                    if (account.Balance < (token_quantity + CalcFee()))
                    {
                        throw new ContractValidateException("balance is not enough");
                    }
                }
                else
                {
                    if (!account.AssetBalanceEnoughV2(token_id, token_quantity, this.db_manager))
                    {
                        throw new ContractValidateException("token balance is not enough");
                    }
                }

                if (other_token_id.SequenceEqual(COMPARE_CHARICTOR))
                {
                    if (account.Balance < (other_token_quantity + CalcFee()))
                    {
                        throw new ContractValidateException("balance is not enough");
                    }
                }
                else
                {
                    if (!account.AssetBalanceEnoughV2(other_token_id, other_token_quantity, this.db_manager))
                    {
                        throw new ContractValidateException("other token balance is not enough");
                    }
                }
            }
            else
            {
                throw new ContractValidateException(
                          "contract type error,expected type [ExchangeInjectContract],real type[" + this.contract.GetType().Name + "]");
            }

            return(true);
        }
Ejemplo n.º 17
0
        public override bool Validate()
        {
            if (this.contract == null)
            {
                throw new ContractValidateException("No contract!");
            }

            if (this.db_manager == null)
            {
                throw new ContractValidateException("No this.db_manager!");
            }

            if (this.contract.Is(SetAccountIdContract.Descriptor))
            {
                SetAccountIdContract account_id_contract = null;

                try
                {
                    account_id_contract = contract.Unpack <SetAccountIdContract>();
                }
                catch (InvalidProtocolBufferException e)
                {
                    Logger.Debug(e.Message);
                    throw new ContractValidateException(e.Message);
                }

                byte[] owner_address = account_id_contract.OwnerAddress.ToByteArray();
                byte[] account_id    = account_id_contract.AccountId.ToByteArray();
                if (!TransactionUtil.ValidAccountId(account_id))
                {
                    throw new ContractValidateException("Invalid accountId");
                }

                if (!Wallet.IsValidAddress(owner_address))
                {
                    throw new ContractValidateException("Invalid owner_address");
                }

                AccountCapsule account = this.db_manager.Account.Get(owner_address);
                if (account == null)
                {
                    throw new ContractValidateException("Account has not existed");
                }

                if (account.AccountId != null && !account.AccountId.IsEmpty)
                {
                    throw new ContractValidateException("This account id already set");
                }

                if (this.db_manager.AccountIdIndex.Contains(account_id))
                {
                    throw new ContractValidateException("This id has existed");
                }
            }
            else
            {
                throw new ContractValidateException(
                          "contract type error,expected type [SetAccountIdContract],real type[" + contract.GetType().Name + "]");
            }

            return(true);
        }
Ejemplo n.º 18
0
        public override bool Validate()
        {
            if (this.contract == null)
            {
                throw new ContractValidateException("No contract!");
            }

            if (this.db_manager == null)
            {
                throw new ContractValidateException("No dbManager!");
            }

            if (this.contract.Is(AccountUpdateContract.Descriptor))
            {
                AccountUpdateContract account_update_contract;
                try
                {
                    account_update_contract = contract.Unpack <AccountUpdateContract>();
                }
                catch (InvalidProtocolBufferException e)
                {
                    Logger.Debug(e.Message);
                    throw new ContractValidateException(e.Message);
                }

                byte[] owner_address = account_update_contract.OwnerAddress.ToByteArray();
                byte[] account_name  = account_update_contract.AccountName.ToByteArray();

                if (!TransactionUtil.ValidAccountName(account_name))
                {
                    throw new ContractValidateException("Invalid accountName");
                }
                if (!Wallet.IsValidAddress(owner_address))
                {
                    throw new ContractValidateException("Invalid ownerAddress");
                }

                AccountCapsule account = db_manager.Account.Get(owner_address);
                if (account == null)
                {
                    throw new ContractValidateException("Account has not existed");
                }

                if (account.AccountName != null && !account.AccountName.IsEmpty &&
                    db_manager.DynamicProperties.GetAllowUpdateAccountName() == 0)
                {
                    throw new ContractValidateException("This account name already exist");
                }

                if (db_manager.AccountIndex.Contains(account_name) &&
                    db_manager.DynamicProperties.GetAllowUpdateAccountName() == 0)
                {
                    throw new ContractValidateException("This name has existed");
                }
            }
            else
            {
                throw new ContractValidateException(
                          "contract type error,expected type [AccountUpdateContract], real type[" + contract.GetType().Name + "]");
            }

            return(true);
        }