Skip to content

Commit

Permalink
handle exceptions in cross-chain calls (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
bokobza authored and monsieurleberre committed Jun 20, 2018
1 parent 504c5a2 commit e9a294b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public IActionResult CreateSessionOnCounterChain([FromBody] CreateCounterChainSe
/// <returns>An ActionResult.</returns>
[Route("process-session-oncounterchain")]
[HttpPost]
public IActionResult ProcessSessionOnCounterChain([FromBody] CreateCounterChainSessionRequest createCounterChainSessionRequest)
public async Task<IActionResult> ProcessSessionOnCounterChain([FromBody] CreateCounterChainSessionRequest createCounterChainSessionRequest)
{
Guard.NotNull(createCounterChainSessionRequest, nameof(createCounterChainSessionRequest));

Expand All @@ -93,7 +93,7 @@ public IActionResult ProcessSessionOnCounterChain([FromBody] CreateCounterChainS

try
{
var result = this.counterChainSessionManager.ProcessCounterChainSession(
var result = await this.counterChainSessionManager.ProcessCounterChainSession(
createCounterChainSessionRequest.SessionId,
createCounterChainSessionRequest.Amount,
createCounterChainSessionRequest.DestinationAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ 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 transctionId
// 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
Expand Down Expand Up @@ -209,19 +209,23 @@ public async Task<uint256> ProcessCounterChainSession(uint256 sessionId, Money a
Sign = false
};

this.logger.LogInformation("ProcessCounterChainSession: Building Transaction.");
this.logger.LogInformation("Building template Transaction.");
var templateTransaction = this.federationWalletTransactionHandler.BuildTransaction(multiSigContext);

//add my own partial
this.logger.LogInformation("ProcessCounterChainSession: Signing own partial.");
this.logger.LogInformation("Signing own partial.");
var counterChainSession = this.VerifySession(sessionId, templateTransaction);

if (counterChainSession == null) return uint256.One;
if (counterChainSession == null)
{
this.logger.LogInformation("CounterChainSession is null, returning.");
return uint256.One;
}
this.MarkSessionAsSigned(counterChainSession);
var partialTransaction = wallet.SignPartialTransaction(templateTransaction, this.federationWalletManager.Secret.WalletPassword);

uint256 bossCard = BossTable.MakeBossTableEntry(sessionId, this.federationGatewaySettings.PublicKey);
this.logger.LogInformation("ProcessCounterChainSession: My bossCard: {0}.", bossCard);
this.logger.LogInformation("My bossCard: {0}.", bossCard);
this.ReceivePartial(sessionId, partialTransaction, bossCard);

//now build the requests for the partials
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,16 @@ private void CreateSessionOnCounterChain(int apiPortForSidechain, uint256 transa

var uri = new Uri($"http://localhost:{apiPortForSidechain}/api/FederationGateway/create-session-oncounterchain");
var request = new JsonContent(createCounterChainSessionRequest);
var httpResponseMessage = client.PostAsync(uri, request).Result;
string json = httpResponseMessage.Content.ReadAsStringAsync().Result;
//todo: handler error

try
{
var httpResponseMessage = client.PostAsync(uri, request).ConfigureAwait(false).GetAwaiter().GetResult();
string json = httpResponseMessage.Content.ReadAsStringAsync().ConfigureAwait(false).GetAwaiter().GetResult();
}
catch (Exception e)
{
this.logger.LogError(e, "Error occurred when calling /api/FederationGateway/create-session-oncounterchain: {0}", e.Message);
}
}

this.logger.LogTrace("(-)");
Expand Down Expand Up @@ -246,12 +253,19 @@ private async Task<uint256> ProcessSessionOnCounterChain(int apiPortForSidechain

var uri = new Uri($"http://localhost:{apiPortForSidechain}/api/FederationGateway/process-session-oncounterchain");
var request = new JsonContent(createPartialTransactionSessionRequest);
var httpResponseMessage = await client.PostAsync(uri, request);
string json = await httpResponseMessage.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<uint256>(json, new UInt256JsonConverter());

try
{
var httpResponseMessage = await client.PostAsync(uri, request);
string json = await httpResponseMessage.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<uint256>(json, new UInt256JsonConverter());
}
catch (Exception e)
{
this.logger.LogError(e, "Error occurred when calling /api/FederationGateway/process-session-oncounterchain: {0}", e.Message);
return uint256.Zero;
}
}

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

0 comments on commit e9a294b

Please sign in to comment.