Skip to content

Commit

Permalink
Fix FullTracer logs
Browse files Browse the repository at this point in the history
  • Loading branch information
fractasy committed Mar 7, 2024
1 parent 9a334d7 commit ec8d3ea
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 45 deletions.
4 changes: 3 additions & 1 deletion src/config/zkresult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ struct

{ ZKR_SM_MAIN_INVALID_WITNESS, "ZKR_SM_MAIN_INVALID_WITNESS" },
{ ZKR_CBOR_INVALID_DATA, "ZKR_CBOR_INVALID_DATA" },
{ ZKR_DATA_STREAM_INVALID_DATA, "ZKR_DATA_STREAM_INVALID_DATA" }
{ ZKR_DATA_STREAM_INVALID_DATA, "ZKR_DATA_STREAM_INVALID_DATA" },

{ ZKR_SM_MAIN_INVALID_TX_STATUS_ERROR, "ZKR_SM_MAIN_INVALID_TX_STATUS_ERROR" }
};

string zkresult2string (int code)
Expand Down
2 changes: 2 additions & 0 deletions src/config/zkresult.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ typedef enum : int
ZKR_SM_MAIN_INVALID_WITNESS = 96, // Main state machine input witness is invalid or corrupt
ZKR_CBOR_INVALID_DATA = 97, // CBOR data is invalid
ZKR_DATA_STREAM_INVALID_DATA = 98, // Data stream data is invalid

ZKR_SM_MAIN_INVALID_TX_STATUS_ERROR = 99, // Invalid TX status-error combination

} zkresult;

Expand Down
32 changes: 17 additions & 15 deletions src/grpc/gen/executor.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/grpc/gen/executor.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src/grpc/proto/executor.proto
Original file line number Diff line number Diff line change
Expand Up @@ -890,4 +890,6 @@ enum ExecutorError {
EXECUTOR_ERROR_INVALID_DATA_STREAM = 115;
// EXECUTOR_ERROR_INVALID_UPDATE_MERKLE_TREE indicates that the provided update merkle tree is invalid, e.g. because the executor is configured not to write to database
EXECUTOR_ERROR_INVALID_UPDATE_MERKLE_TREE = 116;
// EXECUTOR_ERROR_SM_MAIN_INVALID_TX_STATUS_ERROR indicates that a TX has an invalid status-error combination
EXECUTOR_ERROR_SM_MAIN_INVALID_TX_STATUS_ERROR = 117;
}
117 changes: 90 additions & 27 deletions src/main_sm/fork_8/main/full_tracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -611,13 +611,18 @@ zkresult FullTracer::onStoreLog (Context &ctx, const RomCommand &cmd)
}

// Store data in the proper vector
string dataString = PrependZeros(data.get_str(16), 64);
if (isTopic)
{
string dataString = PrependZeros(data.get_str(16), 64);
it->second.topics.push_back(dataString);
}
else
{
// Data length is stored in C
mpz_class cScalar;
getRegFromCtx(ctx, reg_C, cScalar);
uint64_t size = zkmin(cScalar.get_ui(), 32);
string dataString = PrependZeros(data.get_str(16), size*2);
it->second.data.push_back(dataString);
}

Expand Down Expand Up @@ -922,14 +927,27 @@ zkresult FullTracer::onFinishBlock (Context &ctx)
}

// Append to response logs, overwriting log indexes to be sequential
uint64_t logIndex = 0;
//uint64_t logIndex = 0;
map<uint64_t, LogV2>::iterator auxLogsIt;
for (auxLogsIt = auxLogs.begin(); auxLogsIt != auxLogs.end(); auxLogsIt++)
{
auxLogsIt->second.index = logIndex;
logIndex++;
// Set log index
//auxLogsIt->second.index = logIndex;
//logIndex++;

// Set block hash
auxLogsIt->second.block_hash = currentBlock.block_hash;
currentBlock.logs.push_back(auxLogsIt->second);

// Store block log
currentBlock.logs.emplace_back(auxLogsIt->second);

// Store transaction log
if (auxLogsIt->second.tx_index >= currentBlock.responses.size())
{
zklog.error("FullTracer::onFinishBlock() found log.tx_index=" + to_string(auxLogsIt->second.tx_index) + " >= currentBlock.responses.size=" + to_string(currentBlock.responses.size()));
exitProcess();
}
currentBlock.responses[auxLogsIt->second.tx_index].logs.emplace_back(auxLogsIt->second);
}

// Set block hash to all txs of block
Expand Down Expand Up @@ -1657,34 +1675,18 @@ zkresult FullTracer::onFinishTx(Context &ctx, const RomCommand &cmd)
currentBlock.responses[currentBlock.responses.size() - 1].has_gasprice_opcode = hasGaspriceOpcode;
currentBlock.responses[currentBlock.responses.size() - 1].has_balance_opcode = hasBalanceOpcode;

// Order all logs (from all CTX) in order of index
map<uint64_t, LogV2> auxLogs;
map<uint64_t, map<uint64_t, LogV2>>::iterator logIt;
map<uint64_t, LogV2>::const_iterator it;
for (logIt=logs.begin(); logIt!=logs.end(); logIt++)
{
for (it = logIt->second.begin(); it != logIt->second.end(); it++)
{
auxLogs[it->second.index] = it->second;
}
}
// Increase transaction index
txIndex++;

// Append to response logs, overwriting log indexes to be sequential
uint64_t logIndex = 0;
map<uint64_t, LogV2>::iterator auxLogsIt;
for (auxLogsIt = auxLogs.begin(); auxLogsIt != auxLogs.end(); auxLogsIt++)
// Check TX status
if ((response.error.empty() && (response.status == 0)) || (!response.error.empty() && (response.status == 1)))
{
auxLogsIt->second.index = logIndex;
logIndex++;
currentBlock.responses[currentBlock.responses.size() - 1].logs.push_back(auxLogsIt->second);
zklog.error("FullTracer::onFinishTx() invalid TX status-error error=" + response.error + " status=" + to_string(response.status));
return ZKR_SM_MAIN_INVALID_TX_STATUS_ERROR;
}

// Increase transaction index
txIndex++;

// Clean aux array for next iteration
full_trace.clear();
logs.clear();
callData.clear();

// Reset opcodes counters
Expand Down Expand Up @@ -2119,6 +2121,67 @@ zkresult FullTracer::onOpcode(Context &ctx, const RomCommand &cmd)
uint64_t numOpcodes = full_trace.size();
Opcode * prevTraceCall = (numOpcodes > 0) ? &full_trace.at(numOpcodes - 1) : NULL;

// In case there is a log0 with 0 data length (and 0 topics), we must add it manually to logs array because it
// wont be added detected by onStoreLog event
if (codeId == 0xa0 /* LOG0 */)
{
// Get current log index
zkr = getVarFromCtx(ctx, true, ctx.rom.currentLogIndexOffset, auxScalar);
if (zkr != ZKR_SUCCESS)
{
zklog.error("FullTracer::onOpcode() failed calling getVarFromCtx(ctx.rom.currentLogIndexOffset)");
return zkr;
}
uint64_t indexLog = auxScalar.get_ui();

// Init logs[CTX][indexLog], if required
uint64_t CTX = ctx.fr.toU64(ctx.pols.CTX[*ctx.pStep]);
map<uint64_t, map<uint64_t, LogV2>>::iterator itCTX;
itCTX = logs.find(CTX);
if (itCTX == logs.end())
{
map<uint64_t, LogV2> aux;
LogV2 log;
aux[indexLog] = log;
logs[CTX] = aux;
itCTX = logs.find(CTX);
zkassert(itCTX != logs.end());
}

map<uint64_t, LogV2>::iterator it;
it = itCTX->second.find(indexLog);
if (it == itCTX->second.end())
{
LogV2 log;
logs[CTX][indexLog] = log;
it = itCTX->second.find(indexLog);
zkassert(it != itCTX->second.end());
}

it->second.data.clear();

// Add log info
mpz_class auxScalar;
zkr = getVarFromCtx(ctx, false, ctx.rom.storageAddrOffset, auxScalar);
if (zkr != ZKR_SUCCESS)
{
zklog.error("FullTracer::onOpcode() failed calling getVarFromCtx(ctx.rom.storageAddrOffset)");
return zkr;
}
it->second.address = NormalizeTo0xNFormat(auxScalar.get_str(16), 40);
zkr = getVarFromCtx(ctx, true, ctx.rom.blockNumOffset, auxScalar);
if (zkr != ZKR_SUCCESS)
{
zklog.error("FullTracer::onOpcode() failed calling getVarFromCtx(ctx.rom.blockNumOffset)");
return zkr;
}
it->second.block_number = auxScalar.get_ui();
it->second.tx_hash = currentBlock.responses[txIndex].tx_hash;
it->second.tx_hash_l2 = currentBlock.responses[txIndex].tx_hash_l2;
it->second.tx_index = txIndex;
it->second.index = indexLog;
}

#ifdef LOG_TIME_STATISTICS
tmsop.add("getCodeName", TimeDiff(top));
#endif
Expand Down
1 change: 1 addition & 0 deletions src/main_sm/fork_8/main/rom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ void Rom::load(Goldilocks &fr, json &romJson)
l2TxHashOffset = getMemoryOffset("l2TxHash");
currentTxOffset = getMemoryOffset("currentTx");
txStatusOffset = getMemoryOffset("txStatus");
currentLogIndexOffset = getMemoryOffset("currentLogIndex");
}

// Load ROM integer constants
Expand Down
4 changes: 3 additions & 1 deletion src/main_sm/fork_8/main/rom.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class Rom
uint64_t l2TxHashOffset;
uint64_t currentTxOffset;
uint64_t txStatusOffset;
uint64_t currentLogIndexOffset;

/* Constants */
RomConstants constants;
Expand Down Expand Up @@ -129,7 +130,8 @@ class Rom
txIndexOffset(0),
l2TxHashOffset(0),
currentTxOffset(0),
txStatusOffset(0)
txStatusOffset(0),
currentLogIndexOffset(0)
{ };

/* Destructor */
Expand Down
4 changes: 4 additions & 0 deletions src/service/executor/executor_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1475,6 +1475,8 @@ ::grpc::Status ExecutorServiceImpl::ProcessBatchV2 (::grpc::ServerContext* conte
pTransactionContext->set_gas_used(responses[tx].full_trace.context.gas_used); // Total gas used as result of execution
pTransactionContext->set_execution_time(responses[tx].full_trace.context.execution_time);
pTransactionContext->set_old_state_root(string2ba(responses[tx].full_trace.context.old_state_root)); // Starting state root
pTransactionContext->set_chain_id(responses[tx].full_trace.context.chainId);
pTransactionContext->set_tx_index(responses[tx].full_trace.context.txIndex);
for (uint64_t step=0; step<responses[tx].full_trace.steps.size(); step++)
{
executor::v1::TransactionStepV2 * pTransactionStep = pFullTrace->add_steps();
Expand Down Expand Up @@ -2555,6 +2557,8 @@ ::executor::v1::ExecutorError ExecutorServiceImpl::zkresult2error (zkresult &res
case ZKR_CBOR_INVALID_DATA: return ::executor::v1::EXECUTOR_ERROR_INVALID_CBOR;
case ZKR_DATA_STREAM_INVALID_DATA: return ::executor::v1::EXECUTOR_ERROR_INVALID_DATA_STREAM;

case ZKR_SM_MAIN_INVALID_TX_STATUS_ERROR: return ::executor::v1::EXECUTOR_ERROR_SM_MAIN_INVALID_TX_STATUS_ERROR;

case ZKR_AGGREGATED_PROOF_INVALID_INPUT: // Only returned when generating a proof
case ZKR_DB_VERSION_NOT_FOUND_KVDB: // To be mapped to an executor error when HashDB64 is operative
case ZKR_DB_VERSION_NOT_FOUND_GLOBAL: // To be mapped to an executor error when HashDB64 is operative
Expand Down

0 comments on commit ec8d3ea

Please sign in to comment.