Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log Transaction Codes and Operation Traces #228

Merged
merged 2 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 85 additions & 8 deletions internal/transform/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,27 @@ func TransformOperation(operation xdr.Operation, operationIndex int32, transacti
return OperationOutput{}, err
}

outputOperationResults, ok := transaction.Result.Result.OperationResults()
if !ok {
return OperationOutput{}, err
}
outputOperationResultCode := outputOperationResults[operationIndex].Code.String()
outputOperationTraceCode, err := mapOperationTrace(*outputOperationResults[operationIndex].Tr)
if err != nil {
return OperationOutput{}, err
}

transformedOperation := OperationOutput{
SourceAccount: outputSourceAccount,
SourceAccountMuxed: outputSourceAccountMuxed.String,
Type: outputOperationType,
TypeString: outputOperationTypeString,
TransactionID: outputTransactionID,
OperationID: outputOperationID,
OperationDetails: outputDetails,
ClosedAt: outputCloseTime,
SourceAccount: outputSourceAccount,
SourceAccountMuxed: outputSourceAccountMuxed.String,
Type: outputOperationType,
TypeString: outputOperationTypeString,
TransactionID: outputTransactionID,
OperationID: outputOperationID,
OperationDetails: outputDetails,
ClosedAt: outputCloseTime,
OperationResultCode: outputOperationResultCode,
OperationTraceCode: outputOperationTraceCode,
}

return transformedOperation, nil
Expand Down Expand Up @@ -145,6 +157,71 @@ func mapOperationType(operation xdr.Operation) (string, error) {
return op_string_type, nil
}

func mapOperationTrace(operationTrace xdr.OperationResultTr) (string, error) {
var operationTraceDescription string
operationType := operationTrace.Type

switch operationType {
case xdr.OperationTypeCreateAccount:
operationTraceDescription = operationTrace.CreateAccountResult.Code.String()
case xdr.OperationTypePayment:
operationTraceDescription = operationTrace.PaymentResult.Code.String()
case xdr.OperationTypePathPaymentStrictReceive:
operationTraceDescription = operationTrace.PathPaymentStrictReceiveResult.Code.String()
case xdr.OperationTypePathPaymentStrictSend:
operationTraceDescription = operationTrace.PathPaymentStrictSendResult.Code.String()
case xdr.OperationTypeManageBuyOffer:
operationTraceDescription = operationTrace.ManageBuyOfferResult.Code.String()
case xdr.OperationTypeManageSellOffer:
operationTraceDescription = operationTrace.ManageSellOfferResult.Code.String()
case xdr.OperationTypeCreatePassiveSellOffer:
operationTraceDescription = operationTrace.CreatePassiveSellOfferResult.Code.String()
case xdr.OperationTypeSetOptions:
operationTraceDescription = operationTrace.SetOptionsResult.Code.String()
case xdr.OperationTypeChangeTrust:
operationTraceDescription = operationTrace.ChangeTrustResult.Code.String()
case xdr.OperationTypeAllowTrust:
operationTraceDescription = operationTrace.AllowTrustResult.Code.String()
case xdr.OperationTypeAccountMerge:
operationTraceDescription = operationTrace.AccountMergeResult.Code.String()
case xdr.OperationTypeInflation:
operationTraceDescription = operationTrace.InflationResult.Code.String()
case xdr.OperationTypeManageData:
operationTraceDescription = operationTrace.ManageDataResult.Code.String()
case xdr.OperationTypeBumpSequence:
operationTraceDescription = operationTrace.BumpSeqResult.Code.String()
case xdr.OperationTypeCreateClaimableBalance:
operationTraceDescription = operationTrace.CreateClaimableBalanceResult.Code.String()
case xdr.OperationTypeClaimClaimableBalance:
operationTraceDescription = operationTrace.ClaimClaimableBalanceResult.Code.String()
case xdr.OperationTypeBeginSponsoringFutureReserves:
operationTraceDescription = operationTrace.BeginSponsoringFutureReservesResult.Code.String()
case xdr.OperationTypeEndSponsoringFutureReserves:
operationTraceDescription = operationTrace.EndSponsoringFutureReservesResult.Code.String()
case xdr.OperationTypeRevokeSponsorship:
operationTraceDescription = operationTrace.RevokeSponsorshipResult.Code.String()
case xdr.OperationTypeClawback:
operationTraceDescription = operationTrace.ClawbackResult.Code.String()
case xdr.OperationTypeClawbackClaimableBalance:
operationTraceDescription = operationTrace.ClawbackClaimableBalanceResult.Code.String()
case xdr.OperationTypeSetTrustLineFlags:
operationTraceDescription = operationTrace.SetTrustLineFlagsResult.Code.String()
case xdr.OperationTypeLiquidityPoolDeposit:
operationTraceDescription = operationTrace.LiquidityPoolDepositResult.Code.String()
case xdr.OperationTypeLiquidityPoolWithdraw:
operationTraceDescription = operationTrace.LiquidityPoolWithdrawResult.Code.String()
case xdr.OperationTypeInvokeHostFunction:
operationTraceDescription = operationTrace.InvokeHostFunctionResult.Code.String()
case xdr.OperationTypeExtendFootprintTtl:
operationTraceDescription = operationTrace.ExtendFootprintTtlResult.Code.String()
case xdr.OperationTypeRestoreFootprint:
operationTraceDescription = operationTrace.RestoreFootprintResult.Code.String()
default:
return operationTraceDescription, fmt.Errorf("Unknown operation type: %s", operationTrace.Type.String())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think operationTrace.Type.String() would cause an error if the OperationType is bad.

// String returns the name of `e`
func (e OperationType) String() string {
	name, _ := operationTypeMap[int32(e)]
	return name
}

And operationTypeMap[int32(e)] would return a invalid key in map or something

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this default error was designed to raise when the Operation Type is valid, we just haven't mapped it in our case statements. The behavior mirrors the way we define mapOperationType. My understanding is if the operationtype itself is bad, our code would already error and be unable to parse that operation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to change how we handle that in both functions? And if so, any guidance? Looks like the operation.Type.String() function does not return an error, so we don't have the ability to pass the error through

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is if the operationtype itself is bad, our code would already error and be unable to parse that operation

Ah you're right. I think the only time this would actually be a problem is if XDR was updated ahead of what stellar-etl is using. And most likely something else would error out before it even reached this point

Do you want to change how we handle that in both functions?

Nope it's fine as is

}
return operationTraceDescription, nil
}

func PoolIDToString(id xdr.PoolId) string {
return xdr.Hash(id).HexString()
}
Expand Down
Loading
Loading