/*
* Copyright (c) [2016] [ <ether.camp> ]
* This file is part of the ethereumJ library.
*
* The ethereumJ library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ethereumJ library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
*/
package org.ethereum.core;
import org.ethereum.crypto.ECKey;
import org.ethereum.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
/**
* 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
Repository repository;
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() {
return repository.getNonce(getAddress());
}
public BigInteger getBalance() {
BigInteger balance =
repository.getBalance(this.getAddress());
synchronized (getPendingTransactions()) {
if (!getPendingTransactions().isEmpty()) {
for (Transaction tx : getPendingTransactions()) {
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> getPendingTransactions() {
return this.pendingTransactions;
}
public void addPendingTransaction(Transaction transaction) {
synchronized (pendingTransactions) {
pendingTransactions.add(transaction);
}
}
public void clearAllPendingTransactions() {
synchronized (pendingTransactions) {
pendingTransactions.clear();
}
}
}