Skip to content

Commit

Permalink
Temporary commit for troubleshooting.
Browse files Browse the repository at this point in the history
  • Loading branch information
chopin-fan committed Oct 21, 2024
1 parent 9d9dd65 commit 040a266
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ T Call<T>(Address fromAddress, Address toAddress, string methodName, ByteString
where T : IMessage<T>, new();

void SendInline(Address toAddress, string methodName, ByteString args);
void SendInlineWithTransactionId(Address toAddress, string methodName, ByteString args);

void SendVirtualInline(Hash fromVirtualAddress, Address toAddress, string methodName, ByteString args);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ public class PlainTransactionExecutingService : IPlainTransactionExecutingServic
private readonly ISmartContractExecutiveService _smartContractExecutiveService;
private readonly ITransactionContextFactory _transactionContextFactory;
private readonly IFeatureDisableService _featureDisableService;
private readonly ITransactionManager _transactionManager;

public PlainTransactionExecutingService(ISmartContractExecutiveService smartContractExecutiveService,
IEnumerable<IPostExecutionPlugin> postPlugins, IEnumerable<IPreExecutionPlugin> prePlugins,
ITransactionContextFactory transactionContextFactory, IFeatureDisableService featureDisableService,
ITransactionManager transactionManager)
ITransactionContextFactory transactionContextFactory, IFeatureDisableService featureDisableService)
{
_smartContractExecutiveService = smartContractExecutiveService;
_transactionContextFactory = transactionContextFactory;
Expand All @@ -38,7 +36,6 @@ public PlainTransactionExecutingService(ISmartContractExecutiveService smartCont
_postPlugins = GetUniquePlugins(postPlugins);
Logger = NullLogger<PlainTransactionExecutingService>.Instance;
LocalEventBus = NullLocalEventBus.Instance;
_transactionManager = transactionManager;
}

public ILogger<PlainTransactionExecutingService> Logger { get; set; }
Expand Down Expand Up @@ -88,7 +85,7 @@ public async Task<List<ExecutionReturnSet>> ExecuteAsync(TransactionExecutingDto
}

List<TransactionTrace> traceList = new List<TransactionTrace>();
List<Transaction> inlineTxList = new List<Transaction>();
// List<Transaction> inlineTxList = new List<Transaction>();
WrapTraceList(traceList, trace);
// WrapinlineTxList(inlineTxList, trace);
// foreach (var inlinetransaction in inlineTxList)
Expand All @@ -109,7 +106,9 @@ public async Task<List<ExecutionReturnSet>> ExecuteAsync(TransactionExecutingDto
int index = 1;
foreach (var transactionTrace in traceList)
{
Console.WriteLine("index="+ index+++" "+transactionTrace.TransactionId);
// Console.WriteLine("index="+ index+++" "+transactionTrace.TransactionId);

Console.WriteLine("transactionTraceId="+index+++"="+transactionTrace.TransactionId);

if (!TryUpdateStateCache(transactionTrace, groupStateCache))
break;
Expand Down Expand Up @@ -166,23 +165,38 @@ private void WrapinlineTxList(List<Transaction> inlineTxList, TransactionTrace t
}
}

private readonly Dictionary<string, TransactionTrace> traceDictionary = new Dictionary<string, TransactionTrace>();

private List<TransactionTrace> WrapTraceList(List<TransactionTrace> traceList, TransactionTrace trace)
{
HashSet<Hash> txIdSet = new HashSet<Hash>();
// Create a stack to hold the traces for processing
Stack<TransactionTrace> stack = new Stack<TransactionTrace>();
stack.Push(trace);

int index = 1;
// Process all traces in the stack
while (stack.Count > 0)
{
// Pop a trace from the stack
var currentTrace = stack.Pop();

// Add the current trace to the list
if (currentTrace.IsInlineTransaction)
// if (currentTrace.IsInlineTransaction)
// {

// if (currentTrace.IsInlineTransaction && !txIdSet.Contains(currentTrace.TransactionId) || index == 1)
if (currentTrace.IsInlineTxWithId || index == 1)
{
traceList.Add(currentTrace);
// Console.WriteLine("currentTrace="+currentTrace.TransactionId);
traceList.AddIfNotContains(currentTrace);
txIdSet.Add(currentTrace.TransactionId);
index++;
break;
}


// }

// If the current trace contains inline traces, add them to the stack
if (currentTrace.InlineTraces != null)
Expand All @@ -197,6 +211,13 @@ private List<TransactionTrace> WrapTraceList(List<TransactionTrace> traceList, T
return traceList;
}

public static void AddIfNotContainsByTransactionId( List<TransactionTrace> traceList, TransactionTrace currentTrace)
{
if (traceList.All(trace => trace.TransactionId != currentTrace.TransactionId))
{
traceList.Add(currentTrace);
}
}

private void GetAllTrace(List<TransactionTrace> transactionTraces, TransactionTrace transactionTrace)
{
Expand Down Expand Up @@ -335,7 +356,8 @@ private async Task ExecuteInlineTransactions(int depth, Timestamp currentBlockTi
int index = 0;
foreach (var inlineTx in txContext.Trace.InlineTransactions)
{
if (txContext.IsInlineTransaction)
inlineTx.IsInlineTxWithId = true;
if (txContext.IsInlineTxWithId || inlineTx.IsInlineTxWithId)
{
AutoGenerateInlineTxId(inlineTx,originTransactionId,index);
}
Expand All @@ -358,6 +380,11 @@ private async Task ExecuteInlineTransactions(int depth, Timestamp currentBlockTi
// {
// inlineTrace.IsInlineTransaction = true;
// }
if (txContext.IsInlineTxWithId || inlineTx.IsInlineTxWithId)
{
inlineTrace.IsInlineTxWithId = true;
}

trace.InlineTraces.Add(inlineTrace);
if (!inlineTrace.IsSuccessful())
// Already failed, no need to execute remaining inline transactions
Expand All @@ -369,7 +396,10 @@ private async Task ExecuteInlineTransactions(int depth, Timestamp currentBlockTi

private void AutoGenerateInlineTxId(Transaction inlineTx, Hash originTransactionId, int index)
{
inlineTx.SetHash(HashHelper.XorAndCompute(originTransactionId, HashHelper.ComputeFrom(index)));
inlineTx.SetHash(HashHelper.XorAndCompute(originTransactionId, HashHelper.ComputeFrom(DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() )));
// inlineTx.SetHash(HashHelper.XorAndCompute(originTransactionId, HashHelper.ComputeFrom(index )));
Console.WriteLine("SetHash="+inlineTx.GetHash());
inlineTx.IsInlineTxWithId = true;
}

private async Task<bool> ExecutePluginOnPreTransactionStageAsync(IExecutive executive,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public void SendInline(Address toAddress, string methodName, ByteString args)

public void SendInlineWithTransactionId(Address toAddress, string methodName, ByteString args)
{
// TransactionContext.IsInlineTransaction = true;
TransactionContext.IsInlineTxWithId = true;
TransactionContext.Trace.InlineTransactions.Add(new Transaction
{
IsInlineTxWithId = true,
Expand Down Expand Up @@ -291,10 +291,12 @@ public void SendVirtualInline(Hash fromVirtualAddress, Address toAddress, string
public void SendVirtualInlineBySystemContract(Hash fromVirtualAddress, Address toAddress, string methodName,
ByteString args)
{
TransactionContext.IsInlineTxWithId = true;
TransactionContext.Trace.InlineTransactions.Add(new Transaction
{
From = ConvertVirtualAddressToContractAddressWithContractHashName(fromVirtualAddress, Self),
To = toAddress,
IsInlineTxWithId = true,
MethodName = methodName,
Params = args
});
Expand Down
2 changes: 1 addition & 1 deletion src/AElf.Kernel.SmartContract/ITransactionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ public interface ITransactionContext
Transaction Transaction { get; set; }
TransactionTrace Trace { get; set; }
IStateCache StateCache { get; set; }
bool IsInlineTransaction { get; set; }
bool IsInlineTxWithId { get; set; }
}
2 changes: 1 addition & 1 deletion src/AElf.Kernel.SmartContract/TransactionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ public class TransactionContext : ITransactionContext
public Transaction Transaction { get; set; }
public TransactionTrace Trace { get; set; }
public IStateCache StateCache { get; set; }
public bool IsInlineTransaction { get; set; }
public bool IsInlineTxWithId { get; set; }
}
2 changes: 1 addition & 1 deletion src/AElf.Kernel.Types/Transaction/TransactionTrace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public partial class TransactionTrace
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }

public bool IsInlineTransaction { get; set; }
public bool IsInlineTxWithId { get; set; }

public IEnumerable<LogEvent> FlattenedLogs
{
Expand Down
4 changes: 4 additions & 0 deletions src/AElf.Sdk.CSharp/CSharpSmartContractContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ public void SendInline(Address toAddress, string methodName, ByteString args)
{
SmartContractBridgeContextImplementation.SendInline(toAddress, methodName, args);
}
public void SendInlineWithTransactionId(Address toAddress, string methodName, ByteString args)
{
SmartContractBridgeContextImplementation.SendInlineWithTransactionId(toAddress, methodName, args);
}

/// <summary>
/// Sends a virtual inline transaction to another contract.
Expand Down
8 changes: 1 addition & 7 deletions src/AElf.Sdk.CSharp/SmartContractBridgeContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static T Call<T>(this ISmartContractBridgeContext context, Address addres
public static void SendInline(this ISmartContractBridgeContext context, Address toAddress, string methodName,
IMessage message)
{
context.SendInline(toAddress, methodName, ConvertToByteString(message));
context.SendInlineWithTransactionId(toAddress, methodName, ConvertToByteString(message));
}

/// <summary>
Expand Down Expand Up @@ -125,12 +125,6 @@ public static void SendInline(this CSharpSmartContractContext context, Address t
{
context.SendInline(toAddress, methodName, ConvertToByteString(message));
}

public static void SendInlineWithTransactionId(this CSharpSmartContractContext context, Address toAddress, string methodName,
IMessage message)
{
context.SendInline(toAddress, methodName, ConvertToByteString(message));
}

/// <summary>
/// Sends a virtual inline transaction to another contract.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public override Empty FailInlineTransfer(TransferInput input)
Memo = input.Memo
});

// Context.SendInlineWithTransactionId(State.TokenContract.Value, "Burn", new BurnInput
Context.SendInline(State.TokenContract.Value, "Burn", new BurnInput
{
Symbol = "ELF",
Expand Down

0 comments on commit 040a266

Please sign in to comment.