Example #1
0
        public void PayEnergyBill(DatabaseManager manager,
                                  AccountCapsule origin,
                                  AccountCapsule caller,
                                  long percent,
                                  long origin_energy_limit,
                                  EnergyProcessor energy_processor,
                                  long now)
        {
            if (this.receipt.EnergyUsageTotal <= 0)
            {
                return;
            }

            if (caller.Address.Equals(origin.Address))
            {
                PayEnergyBill(manager, caller, this.receipt.EnergyUsageTotal, energy_processor, now);
            }
            else
            {
                long origin_usage = (this.receipt.EnergyUsageTotal * percent) / 100;
                origin_usage = GetOriginUsage(manager, origin,
                                              origin_energy_limit,
                                              energy_processor,
                                              origin_usage);

                long caller_usage = this.receipt.EnergyUsageTotal - origin_usage;
                energy_processor.UseEnergy(origin, origin_usage, now);
                this.receipt.OriginEnergyUsage = origin_usage;

                PayEnergyBill(manager, caller, caller_usage, energy_processor, now);
            }
        }
Example #2
0
        public void AddSignature(byte[] privatekey, AccountStore account_store)
        {
            Transaction.Types.Contract contract = this.transaction.RawData.Contract[0];

            byte[] owner         = GetOwner(contract);
            int    permission_id = contract.PermissionId;

            AccountCapsule account = account_store.Get(owner);

            if (account == null)
            {
                throw new PermissionException("Account is not exist.");
            }

            Permission permission = account.GetPermissionById(permission_id);

            if (permission == null)
            {
                throw new PermissionException("Permission is not exist");
            }

            if (permission_id != 0)
            {
                if (permission.Type != Permission.Types.PermissionType.Active)
                {
                    throw new PermissionException("Permission type is error");
                }
                if (Wallet.CheckPermissionOperations(permission, contract))
                {
                    throw new PermissionException("Invalid permission");
                }
            }

            List <ByteString> approves = new List <ByteString>();
            ECKey             ec_key   = ECKey.FromPrivateKey(privatekey);

            byte[] address = Wallet.PublickKeyToAddress(ec_key.PublicKey);

            if (this.transaction.Signature.Count > 0)
            {
                CheckWeight(permission, new List <ByteString>(this.transaction.Signature), this.GetRawHash().Hash, approves);
                if (approves.Contains(ByteString.CopyFrom(address)))
                {
                    throw new PermissionException(Wallet.AddressToBase58(address) + "had signed!");
                }
            }

            long weight = GetWeight(permission, address);

            if (weight == 0)
            {
                throw new PermissionException(
                          privatekey.ToHexString() + " address is " +
                          Wallet.AddressToBase58(address) + "but it is not contained of permission.");
            }

            ECDSASignature signature = ec_key.Sign(this.GetRawHash().Hash);

            this.transaction.Signature.Add(ByteString.CopyFrom(signature.ToByteArray()));
        }
Example #3
0
        public TransactionCapsule(TransferContract contract, AccountStore account_store)
        {
            AccountCapsule account = account_store.Get(contract.OwnerAddress.ToByteArray());

            if (account == null || account.Balance < contract.Amount)
            {
                return;
            }

            CreateTransaction(contract, Transaction.Types.Contract.Types.ContractType.TransferContract);
        }
Example #4
0
        public TransactionCapsule(AccountCreateContract contract, AccountStore account_store)
        {
            AccountCapsule account = account_store.Get(contract.OwnerAddress.ToByteArray());

            if (account != null && account.Type == contract.Type)
            {
                return;
            }

            CreateTransaction(contract, Transaction.Types.Contract.Types.ContractType.AccountCreateContract);
        }
Example #5
0
        private long GetOriginUsage(DatabaseManager manager,
                                    AccountCapsule origin,
                                    long origin_energy_limit,
                                    EnergyProcessor energy_processor,
                                    long origin_usage)
        {
            if (VMConfig.EnergyLimitHardFork)
            {
                return(Math.Min(origin_usage,
                                Math.Min(energy_processor.GetAccountLeftEnergyFromFreeze(origin), origin_energy_limit)));
            }

            return(Math.Min(origin_usage, energy_processor.GetAccountLeftEnergyFromFreeze(origin)));
        }
Example #6
0
        public static bool ValidateSignature(Transaction tx, byte[] hash, DatabaseManager db_manager)
        {
            Permission   permission    = null;
            AccountStore account_store = db_manager.Account;

            Transaction.Types.Contract contract = tx.RawData.Contract?[0];

            int permission_id = contract.PermissionId;

            byte[]         owner   = GetOwner(contract);
            AccountCapsule account = account_store.Get(owner);

            if (account == null)
            {
                if (permission_id == 0)
                {
                    permission = AccountCapsule.GetDefaultPermission(ByteString.CopyFrom(owner));
                }
                else if (permission_id == 2)
                {
                    permission = AccountCapsule.CreateDefaultActivePermission(ByteString.CopyFrom(owner), db_manager);
                }
            }
            else
            {
                permission = account.GetPermissionById(permission_id);
            }

            if (permission == null)
            {
                throw new PermissionException("Permission is not exist");
            }

            if (permission_id != 0)
            {
                if (permission.Type != Permission.Types.PermissionType.Active)
                {
                    throw new PermissionException("Permission type is error");
                }

                if (!Wallet.CheckPermissionOperations(permission, contract))
                {
                    throw new PermissionException("Invalid Permission");
                }
            }

            return(CheckWeight(permission, new List <ByteString>(tx.Signature), hash, null) >= permission.Threshold);
        }
Example #7
0
        private void PayEnergyBill(DatabaseManager manager,
                                   AccountCapsule account,
                                   long usage,
                                   EnergyProcessor energy_processor,
                                   long now)
        {
            long accountEnergyLeft = energy_processor.GetAccountLeftEnergyFromFreeze(account);

            if (accountEnergyLeft >= usage)
            {
                energy_processor.UseEnergy(account, usage, now);
                this.receipt.EnergyUsage = usage;
            }
            else
            {
                energy_processor.UseEnergy(account, accountEnergyLeft, now);
                long sun_energy         = DefineParameter.SUN_PER_ENERGY;
                long dynamic_energy_fee = manager.DynamicProperties.GetEnergyFee();
                if (dynamic_energy_fee > 0)
                {
                    sun_energy = dynamic_energy_fee;
                }

                long energy_fee = (usage - accountEnergyLeft) * sun_energy;

                this.receipt.EnergyUsage = accountEnergyLeft;
                this.receipt.EnergyFee   = energy_fee;

                long balance = account.Balance;
                if (balance < energy_fee)
                {
                    throw new BalanceInsufficientException(
                              account.CreateDatabaseKey().ToHexString() + " insufficient balance");
                }

                account.Balance = balance - energy_fee;
                manager.AdjustBalance(manager.Account.GetBlackHole().Address.ToByteArray(), energy_fee);
            }

            manager.Account.Put(account.Address.ToByteArray(), account);
        }
Example #8
0
 public int CompareTo(AccountCapsule other)
 {
     return(other.Balance.CompareTo(this.Balance));
 }