Example #1
0
        public AssetIssueCapsule GetAssetIssue(byte[] token_id)
        {
            AssetIssueCapsule asset_issue = null;

            byte[] id  = ByteUtil.StripLeadingZeroes(token_id);
            Key    key = new Key(id);

            if (this.asset_issue_cache.ContainsKey(key))
            {
                asset_issue = this.asset_issue_cache[key].ToCapsule <AssetIssueCapsule, Protocol.AssetIssueContract>();
            }
            else
            {
                if (this.parent != null)
                {
                    asset_issue = this.parent.GetAssetIssue(id);
                }
                else
                {
                    asset_issue = this.db_manager.AssetIssue.Get(id);
                }

                if (asset_issue != null)
                {
                    this.asset_issue_cache.Add(key, Value.Create(asset_issue.Data));
                }
            }

            return(asset_issue);
        }
Example #2
0
        public static AssetIssueContract GetAssetIssueByName(byte[] name)
        {
            if (name == null || name.Length == 0)
            {
                throw new ArgumentException("Invalid name");
            }

            AssetIssueContract contract = null;

            if (Manager.Instance.DBManager.DynamicProperties.GetAllowSameTokenName() == 0)
            {
                AssetIssueCapsule asset_issue = Manager.Instance.DBManager.AssetIssue.Get(name);
                contract = asset_issue != null ? asset_issue.Instance : null;
            }
            else
            {
                ByteString     asset_name       = ByteString.CopyFrom(name);
                AssetIssueList asset_issue_list = new AssetIssueList();
                foreach (var asset_issue in Manager.Instance.DBManager.AssetIssueV2.AllAssetIssues)
                {
                    if (asset_issue.Name.Equals(asset_name))
                    {
                        asset_issue_list.AssetIssue.Add(asset_issue.Instance);
                    }
                }

                if (asset_issue_list.AssetIssue.Count > 1)
                {
                    throw new NonUniqueObjectException("get more than one asset, please use " + RpcCommand.AssetIssue.AssetIssueById);
                }
                else
                {
                    AssetIssueCapsule asset_issue = Manager.Instance.DBManager.AssetIssueV2.Get(asset_name.ToByteArray());
                    if (asset_name != null)
                    {
                        if (asset_issue_list.AssetIssue.Count > 0 &&
                            asset_issue_list.AssetIssue[0].Id.Equals(asset_issue.Instance.Id))
                        {
                            contract = asset_issue.Instance;
                        }
                        else
                        {
                            asset_issue_list.AssetIssue.Add(asset_issue.Instance);
                            if (asset_issue_list.AssetIssue.Count > 1)
                            {
                                throw new NonUniqueObjectException("get more than one asset, please use " + RpcCommand.AssetIssue.AssetIssueById);
                            }
                            contract = asset_issue_list.AssetIssue[0];
                        }
                    }
                }
            }

            return(contract);
        }
Example #3
0
        public static AssetIssueContract GetAssetIssueById(byte[] id)
        {
            if (id == null || id.Length == 0)
            {
                throw new ArgumentException("Invalid id");
            }

            AssetIssueCapsule asset_issue = Manager.Instance.DBManager.AssetIssueV2.Get(id);

            return(asset_issue != null ? asset_issue.Instance : null);
        }
Example #4
0
        public override bool Execute(TransactionResultCapsule result)
        {
            long fee = CalcFee();

            try
            {
                UpdateAssetContract update_asset_contract = this.contract.Unpack <UpdateAssetContract>();

                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;
                AccountCapsule    account          = this.db_manager.Account.Get(owner_address);
                AssetIssueCapsule asset_issue      = null;
                AssetIssueCapsule asset_issue_v2   = null;

                asset_issue_v2 = this.db_manager.AssetIssueV2.Get(account.AssetIssuedID.ToByteArray());
                asset_issue_v2.FreeAssetNetLimit       = new_limit;
                asset_issue_v2.PublicFreeAssetNetLimit = new_public_limit;
                asset_issue_v2.Url         = new_url;
                asset_issue_v2.Description = new_description;

                if (this.db_manager.DynamicProperties.GetAllowSameTokenName() == 0)
                {
                    asset_issue = this.db_manager.AssetIssue.Get(account.AssetIssuedName.ToByteArray());
                    asset_issue.FreeAssetNetLimit       = new_limit;
                    asset_issue.PublicFreeAssetNetLimit = new_public_limit;
                    asset_issue.Url         = new_url;
                    asset_issue.Description = new_description;

                    this.db_manager.AssetIssue.Put(asset_issue.CreateDatabaseKey(), asset_issue);
                    this.db_manager.AssetIssueV2.Put(asset_issue_v2.CreateDatabaseKeyV2(), asset_issue_v2);
                }
                else
                {
                    this.db_manager.AssetIssueV2.Put(asset_issue_v2.CreateDatabaseKeyV2(), asset_issue_v2);
                }

                result.SetStatus(fee, code.Sucess);
            }
            catch (InvalidProtocolBufferException e)
            {
                Logger.Debug(e.Message);
                result.SetStatus(fee, code.Failed);
                throw new ContractExeException(e.Message);
            }

            return(true);
        }
Example #5
0
        public override bool Execute(TransactionResultCapsule result)
        {
            long fee = CalcFee();

            try
            {
                ParticipateAssetIssueContract asset_issue_contract = this.contract.Unpack <ParticipateAssetIssueContract>();
                long cost = asset_issue_contract.Amount;

                //subtract from owner address
                byte[]         owner_address = asset_issue_contract.OwnerAddress.ToByteArray();
                AccountCapsule owner_account = this.db_manager.Account.Get(owner_address);
                owner_account.Balance = owner_account.Balance - cost - fee;

                byte[] key = asset_issue_contract.AssetName.ToByteArray();

                AssetIssueCapsule asset_issue = this.db_manager.GetAssetIssueStoreFinal().Get(key);

                long exchange_amount = cost * asset_issue.Num;
                exchange_amount = (long)Math.Floor((double)(exchange_amount / asset_issue.TransactionNum));
                owner_account.AddAssetAmountV2(key, exchange_amount, this.db_manager);

                byte[]         to_address = asset_issue_contract.ToAddress.ToByteArray();
                AccountCapsule to_account = this.db_manager.Account.Get(to_address);
                to_account.Balance = to_account.Balance + cost;
                if (!to_account.ReduceAssetAmountV2(key, exchange_amount, this.db_manager))
                {
                    throw new ContractExeException("reduceAssetAmount failed !");
                }

                //write to db
                this.db_manager.Account.Put(owner_address, owner_account);
                this.db_manager.Account.Put(to_address, to_account);
                result.SetStatus(fee, code.Sucess);
            }
            catch (InvalidProtocolBufferException e)
            {
                Logger.Debug(e.Message);
                result.SetStatus(fee, code.Failed);
                throw new ContractExeException(e.Message);
            }
            catch (ArithmeticException e)
            {
                Logger.Debug(e.Message);
                result.SetStatus(fee, code.Failed);
                throw new ContractExeException(e.Message);
            }

            return(true);
        }
Example #6
0
        public List <AssetIssueCapsule> GetAllAssetIssues()
        {
            List <AssetIssueCapsule> result = new List <AssetIssueCapsule>();

            long block_num         = 1;
            long latest_header_num = this.db_manager.DynamicProperties.GetLatestBlockHeaderNumber();

            while (block_num <= latest_header_num)
            {
                if (block_num % 100000 == 0)
                {
                    Logger.Info(
                        string.Format("The number of block that have processed:{0}",
                                      block_num));
                }
                try
                {
                    BlockCapsule block = this.db_manager.GetBlockByNum(block_num);
                    foreach (TransactionCapsule tx in block.Transactions)
                    {
                        if (tx.Instance.RawData.Contract[0].Type == Protocol.Transaction.Types.Contract.Types.ContractType.AssetIssueContract)
                        {
                            AssetIssueContract contract    = tx.Instance.RawData.Contract[0].Parameter.Unpack <AssetIssueContract>();
                            AssetIssueCapsule  asset_issue = new AssetIssueCapsule(contract);

                            result.Add(this.db_manager.AssetIssue.Get(asset_issue.CreateDatabaseKey()));
                        }
                    }
                }
                catch (System.Exception e)
                {
                    throw new System.Exception("Block not exists,num:" + block_num, e);
                }

                block_num++;
            }

            Logger.Info(
                string.Format("Total block:{0}",
                              block_num));

            if (this.db_manager.AssetIssue.AllAssetIssues.Count != result.Count)
            {
                throw new System.Exception("Asset num is wrong!");
            }

            return(result);
        }
Example #7
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(ParticipateAssetIssueContract.Descriptor))
            {
                ParticipateAssetIssueContract asset_issue_contract;
                try
                {
                    asset_issue_contract = this.contract.Unpack <ParticipateAssetIssueContract>();
                }
                catch (InvalidProtocolBufferException e)
                {
                    Logger.Debug(e.Message);
                    throw new ContractValidateException(e.Message);
                }

                byte[] owner_address = asset_issue_contract.OwnerAddress.ToByteArray();
                byte[] to_address    = asset_issue_contract.ToAddress.ToByteArray();
                byte[] asset_name    = asset_issue_contract.AssetName.ToByteArray();
                long   amount        = asset_issue_contract.Amount;

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

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

                if (amount <= 0)
                {
                    throw new ContractValidateException("Amount must greater than 0!");
                }

                if (owner_address.SequenceEqual(to_address))
                {
                    throw new ContractValidateException("Cannot participate asset Issue yourself !");
                }

                AccountCapsule owner_account = this.db_manager.Account.Get(owner_address);
                if (owner_account == null)
                {
                    throw new ContractValidateException("Account does not exist!");
                }

                try
                {
                    //Whether the balance is enough
                    long fee = CalcFee();
                    if (owner_account.Balance < amount + fee)
                    {
                        throw new ContractValidateException("No enough balance !");
                    }

                    AssetIssueCapsule asset_issue = this.db_manager.GetAssetIssueStoreFinal().Get(asset_name);
                    if (asset_issue == null)
                    {
                        throw new ContractValidateException("No asset named " + Encoding.UTF8.GetString(asset_name));
                    }

                    if (!to_address.SequenceEqual(asset_issue.OwnerAddress.ToByteArray()))
                    {
                        throw new ContractValidateException(
                                  "The asset is not issued by " + to_address.ToHexString());
                    }

                    long now = this.db_manager.DynamicProperties.GetLatestBlockHeaderTimestamp();
                    if (now >= asset_issue.EndTime || now < asset_issue.StartTime)
                    {
                        throw new ContractValidateException("No longer valid period!");
                    }

                    int  tx_num          = asset_issue.TransactionNum;
                    int  num             = asset_issue.Num;
                    long exchange_amount = amount * num;
                    exchange_amount = (long)Math.Floor((double)(exchange_amount / tx_num));

                    if (exchange_amount <= 0)
                    {
                        throw new ContractValidateException("Can not process the exchange!");
                    }

                    AccountCapsule to_account = this.db_manager.Account.Get(to_address);
                    if (to_account == null)
                    {
                        throw new ContractValidateException("To account does not exist!");
                    }

                    if (!to_account.AssetBalanceEnoughV2(asset_name, exchange_amount,
                                                         this.db_manager))
                    {
                        throw new ContractValidateException("Asset balance is not enough !");
                    }
                }
                catch (ArithmeticException e)
                {
                    Logger.Debug(e.Message);
                    throw new ContractValidateException(e.Message);
                }
            }
            else
            {
                throw new ContractValidateException(
                          "contract type error,expected type [ParticipateAssetIssueContract],real type[" + contract.GetType().Name + "]");
            }

            return(true);
        }
Example #8
0
        public override bool Execute(TransactionResultCapsule result)
        {
            long fee = CalcFee();

            try
            {
                AssetIssueContract asset_issue_contract = contract.Unpack <AssetIssueContract>();
                AssetIssueCapsule  asset_issue          = new AssetIssueCapsule(asset_issue_contract);
                AssetIssueCapsule  asset_issue_v2       = new AssetIssueCapsule(asset_issue_contract);
                byte[]             owner_address        = asset_issue_contract.OwnerAddress.ToByteArray();
                long token_id = this.db_manager.DynamicProperties.GetTokenIdNum();

                token_id++;
                asset_issue.Id    = token_id.ToString();
                asset_issue_v2.Id = token_id.ToString();
                this.db_manager.DynamicProperties.PutTokenIdNum(token_id);

                if (this.db_manager.DynamicProperties.GetAllowSameTokenName() == 0)
                {
                    asset_issue_v2.Percision = 0;
                    this.db_manager.AssetIssue.Put(asset_issue.CreateDatabaseKey(), asset_issue);
                    this.db_manager.AssetIssueV2.Put(asset_issue_v2.CreateDatabaseKeyV2(), asset_issue_v2);
                }
                else
                {
                    this.db_manager.AssetIssueV2.Put(asset_issue_v2.CreateDatabaseKeyV2(), asset_issue_v2);
                }

                this.db_manager.AdjustBalance(owner_address, -fee);
                this.db_manager.AdjustBalance(this.db_manager.Account.GetBlackHole().Address.ToByteArray(), fee);

                AccountCapsule      account        = this.db_manager.Account.Get(owner_address);
                List <FrozenSupply> frozen_supplys = new List <FrozenSupply>(asset_issue_contract.FrozenSupply);

                long          remain_supply = asset_issue_contract.TotalSupply;
                List <Frozen> frozens       = new List <Frozen>();
                long          startTime     = asset_issue_contract.StartTime;

                foreach (AssetIssueContract.Types.FrozenSupply supply in asset_issue_contract.FrozenSupply)
                {
                    long   expire_time = startTime + supply.FrozenDays * 86_400_000;
                    Frozen frozen      = new Frozen();
                    frozen.FrozenBalance = supply.FrozenAmount;
                    frozen.ExpireTime    = expire_time;
                    frozens.Add(frozen);
                    remain_supply -= supply.FrozenAmount;
                }

                if (this.db_manager.DynamicProperties.GetAllowSameTokenName() == 0)
                {
                    account.AddAsset(asset_issue.CreateDatabaseKey(), remain_supply);
                }
                account.AssetIssuedName = ByteString.CopyFrom(asset_issue.CreateDatabaseKey());
                account.AssetIssuedID   = ByteString.CopyFrom(asset_issue.CreateDatabaseKeyV2());
                account.AddAssetV2(asset_issue_v2.CreateDatabaseKeyV2(), remain_supply);
                account.FrozenSupplyList.AddRange(frozens);

                this.db_manager.Account.Put(owner_address, account);

                result.AssetIssueID = token_id.ToString();
                result.SetStatus(fee, code.Sucess);
            }
            catch (InvalidProtocolBufferException e)
            {
                Logger.Debug(e.Message);
                result.SetStatus(fee, code.Failed);
                throw new ContractExeException(e.Message);
            }
            catch (BalanceInsufficientException e)
            {
                Logger.Debug(e.Message);
                result.SetStatus(fee, code.Failed);
                throw new ContractExeException(e.Message);
            }
            catch (ArithmeticException e)
            {
                Logger.Debug(e.Message);
                result.SetStatus(fee, code.Failed);
                throw new ContractExeException(e.Message);
            }

            return(true);
        }
Example #9
0
        private bool UseAssetAccountNet(Contract contract, AccountCapsule account, long now, long bytes)
        {
            ByteString asset_name = null;

            try
            {
                asset_name = contract.Parameter.Unpack <TransferAssetContract>().AssetName;
            }
            catch (System.Exception ex)
            {
                throw new System.Exception(ex.Message);
            }

            AssetIssueCapsule asset_issue    = this.db_manager.GetAssetIssueStoreFinal().Get(asset_name.ToByteArray());
            AssetIssueCapsule asset_issue_v2 = null;

            if (asset_issue == null)
            {
                throw new ContractValidateException("asset not exists");
            }

            string token_name = Encoding.UTF8.GetString(asset_name.ToByteArray());
            string tokenID    = asset_issue.Id;

            if (asset_issue.OwnerAddress == account.Address)
            {
                return(UseAccountNet(account, bytes, now));
            }

            long new_public_free_asset = Increase(asset_issue.PublicFreeAssetNetUsage,
                                                  0,
                                                  asset_issue.PublicLatestFreeNetTime,
                                                  now);

            if (bytes > (asset_issue.PublicFreeAssetNetLimit - new_public_free_asset))
            {
                Logger.Debug("The " + tokenID + " public free bandwidth is not enough");
                return(false);
            }

            long free_asset_net_usage       = 0;
            long latest_asset_opration_time = 0;

            if (this.db_manager.DynamicProperties.GetAllowSameTokenName() == 0)
            {
                free_asset_net_usage       = account.GetFreeAssetNetUsange(token_name);
                latest_asset_opration_time = account.GetLatestAssetOperationTime(token_name);
            }
            else
            {
                free_asset_net_usage       = account.GetFreeAssetNetUsangeV2(tokenID);
                latest_asset_opration_time = account.GetLatestAssetOperationTimeV2(tokenID);
            }

            long new_free_asset_net_usage = Increase(free_asset_net_usage, 0, latest_asset_opration_time, now);

            if (bytes > (asset_issue.FreeAssetNetLimit - new_free_asset_net_usage))
            {
                Logger.Debug("The " + tokenID + " free bandwidth is not enough");
                return(false);
            }

            AccountCapsule issuer_account = this.db_manager.Account.Get(asset_issue.OwnerAddress.ToByteArray());

            long issuer_net_usage    = issuer_account.NetUsage;
            long latest_consume_time = issuer_account.LatestConsumeTime;
            long issuer_net_limit    = CalculateGlobalNetLimit(issuer_account);

            long newIssuerNetUsage = Increase(issuer_net_usage, 0, latest_consume_time, now);

            if (bytes > (issuer_net_limit - newIssuerNetUsage))
            {
                Logger.Debug("The " + tokenID + " issuer'bandwidth is not enough");
                return(false);
            }

            latest_consume_time                 = now;
            latest_asset_opration_time          = now;
            asset_issue.PublicLatestFreeNetTime = now;
            long latestOperationTime = this.db_manager.GetHeadBlockTimestamp();

            newIssuerNetUsage        = Increase(newIssuerNetUsage, bytes, latest_consume_time, now);
            new_free_asset_net_usage = Increase(new_free_asset_net_usage,
                                                bytes, latest_asset_opration_time, now);
            new_public_free_asset = Increase(new_public_free_asset, bytes,
                                             asset_issue.PublicLatestFreeNetTime, now);

            issuer_account.NetUsage          = newIssuerNetUsage;
            issuer_account.LatestConsumeTime = latest_consume_time;

            asset_issue.PublicFreeAssetNetUsage = new_public_free_asset;
            asset_issue.PublicLatestFreeNetTime = asset_issue.PublicLatestFreeNetTime;

            account.LatestOperationTime = latestOperationTime;
            if (this.db_manager.DynamicProperties.GetAllowSameTokenName() == 0)
            {
                account.PutLatestAssetOperationTime(token_name, latest_asset_opration_time);
                account.PutFreeAssetNetUsage(token_name, new_free_asset_net_usage);
                account.PutLatestAssetOperationTimeV2(tokenID, latest_asset_opration_time);
                account.PutFreeAssetNetUsageV2(tokenID, new_free_asset_net_usage);

                this.db_manager.AssetIssue.Put(asset_issue.CreateDatabaseKey(), asset_issue);

                asset_issue_v2 = this.db_manager.AssetIssueV2.Get(asset_issue.CreateDatabaseKeyV2());
                asset_issue_v2.PublicFreeAssetNetUsage = new_public_free_asset;
                asset_issue_v2.PublicLatestFreeNetTime = asset_issue.PublicLatestFreeNetTime;
                this.db_manager.AssetIssueV2.Put(asset_issue_v2.CreateDatabaseKeyV2(), asset_issue_v2);
            }
            else
            {
                account.PutLatestAssetOperationTimeV2(tokenID, latest_asset_opration_time);
                account.PutFreeAssetNetUsageV2(tokenID, new_free_asset_net_usage);
                this.db_manager.AssetIssueV2.Put(asset_issue.CreateDatabaseKeyV2(), asset_issue);
            }

            this.db_manager.Account.Put(account.CreateDatabaseKey(), account);
            this.db_manager.Account.Put(issuer_account.CreateDatabaseKey(), issuer_account);

            return(true);
        }