Skip to content

Commit

Permalink
Deposit (#58)
Browse files Browse the repository at this point in the history
Deposit Fix
  • Loading branch information
bokobza authored Jun 21, 2018
1 parent e9a294b commit b218d78
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public async Task<IActionResult> ProcessSessionOnCounterChain([FromBody] CreateC
createCounterChainSessionRequest.SessionId,
createCounterChainSessionRequest.Amount,
createCounterChainSessionRequest.DestinationAddress);
return this.Json(result);
return this.Json(uint256.Zero); //todo: this is temp.
//return this.Json(result);
}
catch (Exception e)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ internal class CounterChainSessionManager : ICounterChainSessionManager
// The sessions are stored here.
private readonly ConcurrentDictionary<uint256, CounterChainSession> sessions = new ConcurrentDictionary<uint256, CounterChainSession>();

private readonly IPEndPointComparer ipEndPointComparer;
private readonly IPAddressComparer ipAddressComparer;

// Get everything together before we get going.
public CounterChainSessionManager(
Expand Down Expand Up @@ -86,7 +86,7 @@ public CounterChainSessionManager(
this.federationWalletManager = federationWalletManager;
this.federationWalletTransactionHandler = federationWalletTransactionHandler;
this.federationGatewaySettings = federationGatewaySettings;
this.ipEndPointComparer = new IPEndPointComparer();
this.ipAddressComparer = new IPAddressComparer();
}

///<inheritdoc/>
Expand Down Expand Up @@ -168,23 +168,23 @@ public async Task<uint256> ProcessCounterChainSession(uint256 sessionId, Money a
this.logger.LogTrace("({0}:'{1}',{2}:'{3}',{4}:'{5}')", nameof(sessionId), sessionId, nameof(amount), amount, nameof(destinationAddress), destinationAddress);
this.logger.LogInformation("ProcessCounterChainSession: Session Registered.");

// Check if this has already been done then we just return the transactionId
if (this.sessions.TryGetValue(sessionId, out var counterchainSession))
{
// This is the mechanism that tells the round robin not to continue and also
// notifies the monitorChain of the completed transactionId from the counterChain transaction.
if (counterchainSession.CounterChainTransactionId != uint256.Zero)
{
// If we get here:
// 1. One of the nodes became the boss and successfully broadcast a completed transaction.
// 2. The monitor in this node received the block with the transaction (identified by the sessionId in the op_return).
// 3. The monitor wrote the CounterChainTransactionId into the counterChainSession to indicate all was done.
// This method then does not try to process the transaction and instead signals to the monitorChain that this
// transaction already completed by passing back the transactionId.
this.logger.LogInformation($"Counterchain Session: {sessionId} was already completed. Doing nothing.");
return counterchainSession.CounterChainTransactionId;
}
}
//// Check if this has already been done then we just return the transactionId
//if (this.sessions.TryGetValue(sessionId, out var counterchainSession))
//{
// // This is the mechanism that tells the round robin not to continue and also
// // notifies the monitorChain of the completed transactionId from the counterChain transaction.
// if (counterchainSession.CounterChainTransactionId != uint256.Zero)
// {
// // If we get here:
// // 1. One of the nodes became the boss and successfully broadcast a completed transaction.
// // 2. The monitor in this node received the block with the transaction (identified by the sessionId in the op_return).
// // 3. The monitor wrote the CounterChainTransactionId into the counterChainSession to indicate all was done.
// // This method then does not try to process the transaction and instead signals to the monitorChain that this
// // transaction already completed by passing back the transactionId.
// this.logger.LogInformation($"Counterchain Session: {sessionId} was already completed. Doing nothing.");
// return counterchainSession.CounterChainTransactionId;
// }
//}

//create the partial transaction template
var wallet = this.federationWalletManager.GetWallet();
Expand Down Expand Up @@ -231,9 +231,10 @@ public async Task<uint256> ProcessCounterChainSession(uint256 sessionId, Money a
//now build the requests for the partials
var requestPartialTransactionPayload = new RequestPartialTransactionPayload(sessionId, templateTransaction);

var federationNetworkPeers = this.connectionManager.ConnectedPeers
.Where(p => !p.Inbound &&
federationGatewaySettings.FederationNodeIpEndPoints.Any(e => this.ipEndPointComparer.Equals(e, p.PeerEndPoint)));
// Only broadcast to the federation members.
var federationNetworkPeers =
this.connectionManager.ConnectedPeers
.Where(p => !p.Inbound && federationGatewaySettings.FederationNodeIpEndPoints.Any(e => this.ipAddressComparer.Equals(e.Address, p.PeerEndPoint.Address)));
foreach (INetworkPeer peer in federationNetworkPeers)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public void ProcessTransaction(Transaction transaction, Block block, int blockNu
this.crossChainTransactionAuditor.Commit();

this.logger.LogInformation("AddCounterChainTransactionId: {0} for transaction {1}.", stringResult, transaction.GetHash());
this.counterChainSessionManager.AddCounterChainTransactionId(transaction.GetHash(), hash);
//this.counterChainSessionManager.AddCounterChainTransactionId(transaction.GetHash(), hash);
continue;
default:
throw new ArgumentOutOfRangeException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ private async Task RunSessionAsync()
{
monitorChainSession.Complete(result);
this.logger.LogInformation($"RunSessionAsync() - Completing Session {result}.");
this.crossChainTransactionAuditor.AddCounterChainTransactionId(monitorChainSession.SessionId, result);
//this.crossChainTransactionAuditor.AddCounterChainTransactionId(monitorChainSession.SessionId, result);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,19 @@ public int GetHashCode(IPEndPoint endPoint)
return endPoint.MapToIpv6().GetHashCode();
}
}

public class IPAddressComparer : IEqualityComparer<IPAddress>
{
public bool Equals(IPAddress x, IPAddress y)
{
if (x == null) return y == null;

return x.MapToIPv6().Equals(y.MapToIPv6());
}

public int GetHashCode(IPAddress endPoint)
{
return endPoint.MapToIPv6().GetHashCode();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ class PartialTransactionsBehavior : NetworkPeerBehavior

private readonly FederationGatewaySettings federationGatewaySettings;

private readonly IPEndPointComparer ipEndPointComparer;
private readonly IPAddressComparer ipAddressComparer;

public PartialTransactionsBehavior(
ILoggerFactory loggerFactory,
ILoggerFactory loggerFactory,
ICrossChainTransactionMonitor crossChainTransactionMonitor,
IFederationWalletManager federationWalletManager,
ICounterChainSessionManager counterChainSessionManager,
Network network,
IFederationWalletManager federationWalletManager,
ICounterChainSessionManager counterChainSessionManager,
Network network,
FederationGatewaySettings federationGatewaySettings)
{
Guard.NotNull(loggerFactory, nameof(loggerFactory));
Expand All @@ -50,15 +50,15 @@ public PartialTransactionsBehavior(
Guard.NotNull(counterChainSessionManager, nameof(counterChainSessionManager));
Guard.NotNull(network, nameof(network));
Guard.NotNull(federationGatewaySettings, nameof(federationGatewaySettings));

this.logger = loggerFactory.CreateLogger(this.GetType().FullName);
this.loggerFactory = loggerFactory;
this.crossChainTransactionMonitor = crossChainTransactionMonitor;
this.federationWalletManager = federationWalletManager;
this.counterChainSessionManager = counterChainSessionManager;
this.network = network;
this.federationGatewaySettings = federationGatewaySettings;
this.ipEndPointComparer = new IPEndPointComparer();
this.ipAddressComparer = new IPAddressComparer();
}

public override object Clone()
Expand All @@ -69,24 +69,36 @@ public override object Clone()
protected override void AttachCore()
{
this.logger.LogTrace("()");
if(federationGatewaySettings.FederationNodeIpEndPoints.Any(e => ipEndPointComparer.Equals(e, AttachedPeer.PeerEndPoint)))

if (federationGatewaySettings.FederationNodeIpEndPoints.Any(e => ipAddressComparer.Equals(e.Address, AttachedPeer.PeerEndPoint.Address)))
{
this.AttachedPeer.MessageReceived.Register(this.OnMessageReceivedAsync, true);
}

this.logger.LogTrace("(-)");
}

protected override void DetachCore()
{
this.logger.LogTrace("()");
if (federationGatewaySettings.FederationNodeIpEndPoints.Any(e => ipEndPointComparer.Equals(e, AttachedPeer.PeerEndPoint)))

if (federationGatewaySettings.FederationNodeIpEndPoints.Any(e => ipAddressComparer.Equals(e.Address, AttachedPeer.PeerEndPoint.Address)))
{
this.AttachedPeer.MessageReceived.Unregister(this.OnMessageReceivedAsync);
}

this.logger.LogTrace("(-)");
}

async Task Broadcast(RequestPartialTransactionPayload payload)
{
this.logger.LogTrace("({0}:'{1}',{2}:'{3}',{4}:'{5}')", nameof(payload.BossCard), payload.BossCard, nameof(payload.Command), payload.Command, nameof(payload.SessionId), payload.SessionId);
if (federationGatewaySettings.FederationNodeIpEndPoints.Any(e => ipEndPointComparer.Equals(e, AttachedPeer.PeerEndPoint)))

if (federationGatewaySettings.FederationNodeIpEndPoints.Any(e => ipAddressComparer.Equals(e.Address, AttachedPeer.PeerEndPoint.Address)))
{
await this.AttachedPeer.SendMessageAsync(payload);
}

this.logger.LogTrace("(-)");
}

Expand Down

0 comments on commit b218d78

Please sign in to comment.