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

Add ledger_key_hash to history_operations #237

Merged
merged 4 commits into from
Apr 15, 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
27 changes: 27 additions & 0 deletions internal/transform/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,7 @@ func extractOperationDetails(operation xdr.Operation, transaction ingest.LedgerT
details["type"] = "invoke_contract"

transactionEnvelope := getTransactionV1Envelope(transaction.Envelope)
details["ledger_key_hash"] = ledgerKeyHashFromTxEnvelope(transactionEnvelope)
details["contract_id"] = contractIdFromTxEnvelope(transactionEnvelope)
details["contract_code_hash"] = contractCodeHashFromTxEnvelope(transactionEnvelope)

Expand Down Expand Up @@ -1068,6 +1069,7 @@ func extractOperationDetails(operation xdr.Operation, transaction ingest.LedgerT
details["type"] = "create_contract"

transactionEnvelope := getTransactionV1Envelope(transaction.Envelope)
details["ledger_key_hash"] = ledgerKeyHashFromTxEnvelope(transactionEnvelope)
details["contract_id"] = contractIdFromTxEnvelope(transactionEnvelope)
details["contract_code_hash"] = contractCodeHashFromTxEnvelope(transactionEnvelope)

Expand All @@ -1089,6 +1091,7 @@ func extractOperationDetails(operation xdr.Operation, transaction ingest.LedgerT
case xdr.HostFunctionTypeHostFunctionTypeUploadContractWasm:
details["type"] = "upload_wasm"
transactionEnvelope := getTransactionV1Envelope(transaction.Envelope)
details["ledger_key_hash"] = ledgerKeyHashFromTxEnvelope(transactionEnvelope)
details["contract_code_hash"] = contractCodeHashFromTxEnvelope(transactionEnvelope)
default:
panic(fmt.Errorf("unknown host function type: %s", op.HostFunction.Type))
Expand All @@ -1099,12 +1102,14 @@ func extractOperationDetails(operation xdr.Operation, transaction ingest.LedgerT
details["extend_to"] = op.ExtendTo

transactionEnvelope := getTransactionV1Envelope(transaction.Envelope)
details["ledger_key_hash"] = ledgerKeyHashFromTxEnvelope(transactionEnvelope)
details["contract_id"] = contractIdFromTxEnvelope(transactionEnvelope)
details["contract_code_hash"] = contractCodeHashFromTxEnvelope(transactionEnvelope)
case xdr.OperationTypeRestoreFootprint:
details["type"] = "restore_footprint"

transactionEnvelope := getTransactionV1Envelope(transaction.Envelope)
details["ledger_key_hash"] = ledgerKeyHashFromTxEnvelope(transactionEnvelope)
details["contract_id"] = contractIdFromTxEnvelope(transactionEnvelope)
details["contract_code_hash"] = contractCodeHashFromTxEnvelope(transactionEnvelope)
default:
Expand Down Expand Up @@ -1629,6 +1634,7 @@ func (operation *transactionOperationWrapper) Details() (map[string]interface{},
details["type"] = "invoke_contract"

transactionEnvelope := getTransactionV1Envelope(operation.transaction.Envelope)
details["ledger_key_hash"] = ledgerKeyHashFromTxEnvelope(transactionEnvelope)
details["contract_id"] = contractIdFromTxEnvelope(transactionEnvelope)
details["contract_code_hash"] = contractCodeHashFromTxEnvelope(transactionEnvelope)

Expand Down Expand Up @@ -1666,6 +1672,7 @@ func (operation *transactionOperationWrapper) Details() (map[string]interface{},
details["type"] = "create_contract"

transactionEnvelope := getTransactionV1Envelope(operation.transaction.Envelope)
details["ledger_key_hash"] = ledgerKeyHashFromTxEnvelope(transactionEnvelope)
details["contract_id"] = contractIdFromTxEnvelope(transactionEnvelope)
details["contract_code_hash"] = contractCodeHashFromTxEnvelope(transactionEnvelope)

Expand All @@ -1687,6 +1694,7 @@ func (operation *transactionOperationWrapper) Details() (map[string]interface{},
case xdr.HostFunctionTypeHostFunctionTypeUploadContractWasm:
details["type"] = "upload_wasm"
transactionEnvelope := getTransactionV1Envelope(operation.transaction.Envelope)
details["ledger_key_hash"] = ledgerKeyHashFromTxEnvelope(transactionEnvelope)
details["contract_code_hash"] = contractCodeHashFromTxEnvelope(transactionEnvelope)
default:
panic(fmt.Errorf("unknown host function type: %s", op.HostFunction.Type))
Expand All @@ -1697,12 +1705,14 @@ func (operation *transactionOperationWrapper) Details() (map[string]interface{},
details["extend_to"] = op.ExtendTo

transactionEnvelope := getTransactionV1Envelope(operation.transaction.Envelope)
details["ledger_key_hash"] = ledgerKeyHashFromTxEnvelope(transactionEnvelope)
details["contract_id"] = contractIdFromTxEnvelope(transactionEnvelope)
details["contract_code_hash"] = contractCodeHashFromTxEnvelope(transactionEnvelope)
case xdr.OperationTypeRestoreFootprint:
details["type"] = "restore_footprint"

transactionEnvelope := getTransactionV1Envelope(operation.transaction.Envelope)
details["ledger_key_hash"] = ledgerKeyHashFromTxEnvelope(transactionEnvelope)
details["contract_id"] = contractIdFromTxEnvelope(transactionEnvelope)
details["contract_code_hash"] = contractCodeHashFromTxEnvelope(transactionEnvelope)
default:
Expand Down Expand Up @@ -1782,6 +1792,23 @@ func contractCodeHashFromTxEnvelope(transactionEnvelope xdr.TransactionV1Envelop
return ""
}

func ledgerKeyHashFromTxEnvelope(transactionEnvelope xdr.TransactionV1Envelope) []string {
var ledgerKeyHash []string
for _, ledgerKey := range transactionEnvelope.Tx.Ext.SorobanData.Resources.Footprint.ReadOnly {
if utils.LedgerKeyToLedgerKeyHash(ledgerKey) != "" {
ledgerKeyHash = append(ledgerKeyHash, utils.LedgerKeyToLedgerKeyHash(ledgerKey))
}
}

for _, ledgerKey := range transactionEnvelope.Tx.Ext.SorobanData.Resources.Footprint.ReadWrite {
if utils.LedgerKeyToLedgerKeyHash(ledgerKey) != "" {
ledgerKeyHash = append(ledgerKeyHash, utils.LedgerKeyToLedgerKeyHash(ledgerKey))
}
}

return ledgerKeyHash
}

func contractCodeFromContractData(ledgerKey xdr.LedgerKey) string {
contractCode, ok := ledgerKey.GetContractCode()
if !ok {
Expand Down
8 changes: 8 additions & 0 deletions internal/utils/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,3 +713,11 @@ func LedgerEntryToLedgerKeyHash(ledgerEntry xdr.LedgerEntry) string {

return ledgerKeyHash
}

func LedgerKeyToLedgerKeyHash(ledgerKey xdr.LedgerKey) string {
ledgerKeyByte, _ := ledgerKey.MarshalBinary()
hashedLedgerKeyByte := hash.Hash(ledgerKeyByte)
ledgerKeyHash := hex.EncodeToString(hashedLedgerKeyByte[:])

return ledgerKeyHash
}
Loading