Package org.ethereum.core

Source Code of org.ethereum.core.Account

package org.ethereum.core;

import java.math.BigInteger;
import java.util.*;

import org.ethereum.crypto.ECKey;
import org.ethereum.manager.WorldManager;
import org.ethereum.util.Utils;
import org.spongycastle.util.encoders.Hex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
* Representation of an actual account or contract
*/
@Component
@Scope("prototype")
public class Account  {

  private ECKey ecKey;
  private byte[] address;

    private Set<Transaction> pendingTransactions =
            Collections.synchronizedSet(new HashSet<Transaction>());

    @Autowired
    WorldManager worldManager;

  public Account() {
  }

    public void init(){
        this.ecKey = new ECKey(Utils.getRandom());
        address = this.ecKey.getAddress();
    }

  public void init(ECKey ecKey) {
    this.ecKey = ecKey;
        address = this.ecKey.getAddress();
  }

    public BigInteger getNonce(){
        AccountState accountState =
                worldManager.getRepository().getAccountState(getAddress());

        return accountState.getNonce();
    }

    public BigInteger getBalance(){

        AccountState accountState =
                worldManager.getRepository().getAccountState(this.getAddress());

        BigInteger balance = BigInteger.ZERO;

        if (accountState != null)
            balance = accountState.getBalance();

        synchronized (getPendingTransactins()){
            if (!getPendingTransactins().isEmpty()){

                for (Transaction tx : getPendingTransactins()){
                    if (Arrays.equals(getAddress(), tx.getSender())){
                        balance = balance.subtract(new BigInteger(1, tx.getValue()));
                    }

                    if (Arrays.equals(getAddress(), tx.getReceiveAddress())){
                        balance = balance.add(new BigInteger(1, tx.getValue()));
                    }
                }
                // todo: calculate the fee for pending
            }
        }


        return balance;
    }


    public ECKey getEcKey() {
        return ecKey;
    }

  public byte[] getAddress() {
    return address;
  }

  public void setAddress(byte[] address) {
    this.address = address;
  }

    public Set<Transaction> getPendingTransactins() {
      return this.pendingTransactions;
    }

    public void addPendingTransaction(Transaction transaction){
        synchronized (pendingTransactions){
            pendingTransactions.add(transaction);
        }
    }

    public void clearAllPendingTransactions(){
        synchronized (pendingTransactions){
            pendingTransactions.clear();
        }
    }
}
TOP

Related Classes of org.ethereum.core.Account

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.