Skip to content
Snippets Groups Projects
ActionPrepareDistributedCommitedTransfer.java 1.25 KiB
Newer Older
Michael Kmoch's avatar
Michael Kmoch committed
package fucoin.actions.transaction;

import akka.actor.ActorRef;
import akka.actor.UntypedActorContext;
import fucoin.Wallet;

public class ActionPrepareDistributedCommitedTransfer extends Transaction {
Michael Kmoch's avatar
Michael Kmoch committed

    private ActorRef source;
    private ActorRef target;
    private int amount;
    private long timestamp;
    private long id;
Michael Kmoch's avatar
Michael Kmoch committed

    public ActionPrepareDistributedCommitedTransfer(ActorRef source,
                                                    ActorRef target, int amount, long timestamp, long id) {
        this.source = source;
        this.target = target;
        this.amount = amount;
        this.timestamp = timestamp;
        this.id = id;
    }
Michael Kmoch's avatar
Michael Kmoch committed

    @Override
    protected void onAction(ActorRef sender, ActorRef self,
                            UntypedActorContext context, Wallet wallet) {
        boolean granted = amount > 0 &&
                (sender.compareTo(source) == 0    //sender is supervisor(bank) has always money
                || (wallet.amounts.containsKey(source)    //sender is unknown, might be valid
                && wallet.amounts.getOrDefault(source, 0) >= amount)); //sender have enough money
        sender.tell(new ActionPrepareDistributedCommitedTransferAnswer(source, target, amount, timestamp, granted, id), self);
    }