Пример #1
0
        public VotesCapsule GetVotesCapsule(byte[] address)
        {
            VotesCapsule result = null;
            Key          key    = new Key(address);

            if (this.votes_cache.ContainsKey(key))
            {
                result = this.votes_cache[key].ToCapsule <VotesCapsule, Protocol.Votes>();
            }
            else
            {
                if (this.parent != null)
                {
                    result = this.parent.GetVotesCapsule(address);
                }
                else
                {
                    result = this.db_manager.Votes.Get(address);
                }

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

            return(result);
        }
Пример #2
0
        private void CountVoteAccount(VoteWitnessContract contract, IDeposit deposit)
        {
            byte[] owner_address = contract.OwnerAddress.ToByteArray();

            VotesCapsule   votes   = null;
            AccountCapsule account = (Deposit == null) ? db_manager.Account.Get(owner_address) : Deposit.GetAccount(owner_address);

            if (Deposit != null)
            {
                if (Deposit.GetVotesCapsule(owner_address) == null)
                {
                    votes = new VotesCapsule(contract.OwnerAddress, account.GetVotesList());
                }
                else
                {
                    votes = Deposit.GetVotesCapsule(owner_address);
                }
            }
            else if (!db_manager.Votes.Contains(owner_address))
            {
                votes = new VotesCapsule(contract.OwnerAddress, account.GetVotesList());
            }
            else
            {
                votes = db_manager.Votes.Get(owner_address);
            }

            account.ClearVotes();
            votes.ClearNewVotes();

            foreach (Protocol.VoteWitnessContract.Types.Vote vote in contract.Votes)
            {
                Logger.Debug(
                    string.Format(
                        "CountVoteAccount, address[{0}]",
                        vote.VoteAddress.ToByteArray().ToHexString()));

                votes.AddNewVotes(vote.VoteAddress, vote.VoteCount);
                account.AddVotes(vote.VoteAddress, vote.VoteCount);
            }

            if (Deposit == null)
            {
                db_manager.Account.Put(account.CreateDatabaseKey(), account);
                db_manager.Votes.Put(owner_address, votes);
            }
            else
            {
                deposit.PutAccountValue(account.CreateDatabaseKey(), account);
                deposit.PutVoteValue(owner_address, votes);
            }
        }
Пример #3
0
        public override bool Execute(TransactionResultCapsule result)
        {
            long fee = CalcFee();
            UnfreezeBalanceContract unfreeze_balance_contract;

            try
            {
                unfreeze_balance_contract = contract.Unpack <UnfreezeBalanceContract>();
            }
            catch (InvalidProtocolBufferException e)
            {
                Logger.Debug(e.Message);
                result.SetStatus(fee, code.Failed);
                throw new ContractExeException(e.Message);
            }
            byte[] owner_address = unfreeze_balance_contract.OwnerAddress.ToByteArray();

            AccountCapsule account          = this.db_manager.Account.Get(owner_address);
            long           old_balance      = account.Balance;
            long           unfreeze_balance = 0L;

            byte[] receiver_address = unfreeze_balance_contract.ReceiverAddress.ToByteArray();
            if (receiver_address != null && receiver_address.Length > 0 && this.db_manager.DynamicProperties.SupportDR())
            {
                byte[] key = DelegatedResourceCapsule.CreateDatabaseKey(unfreeze_balance_contract.OwnerAddress.ToByteArray(),
                                                                        unfreeze_balance_contract.ReceiverAddress.ToByteArray());

                DelegatedResourceCapsule delegated_resource = this.db_manager.DelegatedResource.Get(key);
                AccountCapsule           receiver           = this.db_manager.Account.Get(receiver_address);

                switch (unfreeze_balance_contract.Resource)
                {
                case ResourceCode.Bandwidth:
                {
                    unfreeze_balance = delegated_resource.FrozenBalanceForBandwidth;
                    delegated_resource.SetFrozenBalanceForBandwidth(0, 0);
                    receiver.AddAcquiredDelegatedFrozenBalanceForBandwidth(-unfreeze_balance);
                    account.AddDelegatedFrozenBalanceForBandwidth(-unfreeze_balance);
                }
                break;

                case ResourceCode.Energy:
                {
                    unfreeze_balance = delegated_resource.FrozenBalanceForEnergy;
                    delegated_resource.SetFrozenBalanceForEnergy(0, 0);
                    receiver.AddAcquiredDelegatedFrozenBalanceForEnergy(-unfreeze_balance);
                    account.AddDelegatedFrozenBalanceForEnergy(-unfreeze_balance);
                }
                break;

                default:
                    break;
                }

                account.Balance = old_balance + unfreeze_balance;
                this.db_manager.Account.Put(receiver.CreateDatabaseKey(), receiver);

                if (delegated_resource.FrozenBalanceForBandwidth == 0 && delegated_resource.FrozenBalanceForEnergy == 0)
                {
                    this.db_manager.DelegatedResource.Delete(key);

                    DelegatedResourceAccountIndexCapsule delegate_account_index =
                        this.db_manager.DelegateResourceAccountIndex.Get(owner_address);

                    if (delegate_account_index != null)
                    {
                        List <ByteString> to_accounts = new List <ByteString>(delegate_account_index.ToAccounts);
                        to_accounts.Remove(ByteString.CopyFrom(receiver_address));

                        delegate_account_index.ToAccounts = to_accounts;
                        this.db_manager.DelegateResourceAccountIndex.Put(owner_address, delegate_account_index);
                    }

                    delegate_account_index = this.db_manager.DelegateResourceAccountIndex.Get(receiver_address);
                    if (delegate_account_index != null)
                    {
                        List <ByteString> from_accounts = new List <ByteString>(delegate_account_index.FromAccounts);
                        from_accounts.Remove(ByteString.CopyFrom(owner_address));

                        delegate_account_index.FromAccounts = from_accounts;
                        this.db_manager.DelegateResourceAccountIndex.Put(receiver_address, delegate_account_index);
                    }
                }
                else
                {
                    this.db_manager.DelegatedResource.Put(key, delegated_resource);
                }
            }
            else
            {
                switch (unfreeze_balance_contract.Resource)
                {
                case ResourceCode.Bandwidth:
                {
                    List <Frozen> frozens = new List <Frozen>();
                    frozens.AddRange(account.FrozenList);

                    long now = this.db_manager.GetHeadBlockTimestamp();
                    foreach (Frozen frozen in frozens)
                    {
                        if (frozen.ExpireTime <= now)
                        {
                            unfreeze_balance += frozen.FrozenBalance;
                            frozens.Remove(frozen);
                        }
                    }

                    account.Balance = old_balance + unfreeze_balance;
                    account.FrozenList.Clear();
                    account.FrozenList.AddRange(frozens);
                }
                break;

                case ResourceCode.Energy:
                {
                    unfreeze_balance = account.AccountResource.FrozenBalanceForEnergy.FrozenBalance;

                    account.AccountResource.FrozenBalanceForEnergy = new Frozen();
                    account.Balance = old_balance + unfreeze_balance;
                }
                break;

                default:
                    break;
                }
            }

            switch (unfreeze_balance_contract.Resource)
            {
            case ResourceCode.Bandwidth:
            {
                this.db_manager.DynamicProperties.AddTotalNetWeight(-unfreeze_balance / 1000_000L);
            }
            break;

            case ResourceCode.Energy:
            {
                this.db_manager.DynamicProperties.AddTotalEnergyWeight(-unfreeze_balance / 1000_000L);
            }
            break;

            default:
                break;
            }

            VotesCapsule votes = null;

            if (!this.db_manager.Votes.Contains(owner_address))
            {
                votes = new VotesCapsule(unfreeze_balance_contract.OwnerAddress, account.GetVotesList());
            }
            else
            {
                votes = this.db_manager.Votes.Get(owner_address);
            }

            account.ClearVotes();
            votes.ClearNewVotes();

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

            result.UnfreezeAmount = unfreeze_balance;
            result.SetStatus(fee, code.Sucess);

            return(true);
        }
Пример #4
0
 public void PutVoteValue(byte[] address, VotesCapsule votes)
 {
     this.votes_cache.Put(
         new Key(address),
         new Value(votes.Data, ValueType.VALUE_TYPE_CREATE));
 }
Пример #5
0
 public void PutVoteValue(byte[] address, VotesCapsule votes)
 {
     this.deposit.PutVoteValue(address, votes);
 }