Exemplo n.º 1
0
        public static RpcApiResult CreateUnfreezeBalanceContract(byte[] owner_address,
                                                                 byte[] address,
                                                                 int resource_code,
                                                                 out UnfreezeBalanceContract contract)
        {
            contract = new UnfreezeBalanceContract();
            contract.OwnerAddress = ByteString.CopyFrom(owner_address);
            contract.Resource     = (ResourceCode)resource_code;

            if (address != null)
            {
                contract.ReceiverAddress = ByteString.CopyFrom(address);
            }

            return(RpcApiResult.Success);
        }
Exemplo 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 this.db_manager!");
            }
            if (this.contract.Is(UnfreezeBalanceContract.Descriptor))
            {
                UnfreezeBalanceContract unfreeze_balance_contract = null;
                try
                {
                    unfreeze_balance_contract = this.contract.Unpack <UnfreezeBalanceContract>();
                }
                catch (InvalidProtocolBufferException e)
                {
                    Logger.Debug(e.Message);
                    throw new ContractValidateException(e.Message);
                }

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

                AccountCapsule account = this.db_manager.Account.Get(owner_address);
                if (account == null)
                {
                    throw new ContractValidateException(
                              "Account[" + owner_address.ToHexString() + "] not exists");
                }
                long   now = this.db_manager.GetHeadBlockTimestamp();
                byte[] receiver_address = unfreeze_balance_contract.ReceiverAddress.ToByteArray();
                if (receiver_address != null && receiver_address.Length > 0 && this.db_manager.DynamicProperties.SupportDR())
                {
                    if (receiver_address.SequenceEqual(owner_address))
                    {
                        throw new ContractValidateException("receiver_address must not be the same as owner_address");
                    }

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

                    AccountCapsule receiver = this.db_manager.Account.Get(receiver_address);
                    if (receiver == null)
                    {
                        throw new ContractValidateException("Account[" + receiver_address.ToHexString() + "] not exists");
                    }

                    byte[] key = DelegatedResourceCapsule.CreateDatabaseKey(
                        unfreeze_balance_contract.OwnerAddress.ToByteArray(),
                        unfreeze_balance_contract.ReceiverAddress.ToByteArray());

                    DelegatedResourceCapsule delegated_resource = this.db_manager.DelegatedResource.Get(key);
                    if (delegated_resource == null)
                    {
                        throw new ContractValidateException("delegated Resource not exists");
                    }

                    switch (unfreeze_balance_contract.Resource)
                    {
                    case ResourceCode.Bandwidth:
                    {
                        if (delegated_resource.FrozenBalanceForBandwidth <= 0)
                        {
                            throw new ContractValidateException("no delegatedFrozenBalance(BANDWIDTH)");
                        }

                        if (receiver.AcquiredDelegatedFrozenBalanceForBandwidth < delegated_resource.FrozenBalanceForBandwidth)
                        {
                            throw new ContractValidateException(
                                      "AcquiredDelegatedFrozenBalanceForBandwidth["
                                      + receiver.AcquiredDelegatedFrozenBalanceForBandwidth
                                      + "] < delegatedBandwidth["
                                      + delegated_resource.FrozenBalanceForBandwidth
                                      + "],this should never happen");
                        }

                        if (delegated_resource.ExpireTimeForBandwidth > now)
                        {
                            throw new ContractValidateException("It's not time to unfreeze.");
                        }
                    }
                    break;

                    case ResourceCode.Energy:
                    {
                        if (delegated_resource.FrozenBalanceForEnergy <= 0)
                        {
                            throw new ContractValidateException("no delegateFrozenBalance(Energy)");
                        }
                        if (receiver.AcquiredDelegatedFrozenBalanceForEnergy < delegated_resource.FrozenBalanceForEnergy)
                        {
                            throw new ContractValidateException(
                                      "AcquiredDelegatedFrozenBalanceForEnergy["
                                      + receiver.AcquiredDelegatedFrozenBalanceForEnergy
                                      + "] < delegatedEnergy["
                                      + delegated_resource.FrozenBalanceForEnergy
                                      + "],this should never happen");
                        }
                        if (delegated_resource.GetExpireTimeForEnergy(this.db_manager) > now)
                        {
                            throw new ContractValidateException("It's not time to unfreeze.");
                        }
                    }
                    break;

                    default:
                    {
                        throw new ContractValidateException("ResourceCode error.valid ResourceCode[BANDWIDTH、Energy]");
                    }
                    }
                }
                else
                {
                    switch (unfreeze_balance_contract.Resource)
                    {
                    case ResourceCode.Bandwidth:
                    {
                        if (account.FrozenCount <= 0)
                        {
                            throw new ContractValidateException("no frozenBalance(BANDWIDTH)");
                        }

                        long unfreeze_count = account.FrozenList.Where(frozen => frozen.ExpireTime <= now).Count();
                        if (unfreeze_count <= 0)
                        {
                            throw new ContractValidateException("It's not time to unfreeze(BANDWIDTH).");
                        }
                    }
                    break;

                    case ResourceCode.Energy:
                    {
                        Frozen frozen = account.AccountResource.FrozenBalanceForEnergy;
                        if (frozen.FrozenBalance <= 0)
                        {
                            throw new ContractValidateException("no frozenBalance(Energy)");
                        }

                        if (frozen.ExpireTime > now)
                        {
                            throw new ContractValidateException("It's not time to unfreeze(Energy).");
                        }
                    }
                    break;

                    default:
                    {
                        throw new ContractValidateException("ResourceCode error.valid ResourceCode[BANDWIDTH、Energy]");
                    }
                    }
                }
            }
            else
            {
                throw new ContractValidateException(
                          "contract type error,expected type [UnfreezeBalanceContract],real type[" + contract.GetType().Name + "]");
            }

            return(true);
        }
Exemplo n.º 3
0
 public async Task <TransactionExtention> UnfreezeBalanceAsync(UnfreezeBalanceContract contract)
 {
     return(await _grpcClient.UnfreezeBalance2Async(contract));
 }
Exemplo n.º 4
0
        public async Task <Transaction> UnfreezeBalanceAsync(UnfreezeBalanceContract contract, CancellationToken token = default)
        {
            var wallet = GetWallet();

            return(await wallet.UnfreezeBalanceAsync(contract, _configuration.GetCallOptions(token)));
        }