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 3 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
28 changes: 28 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,24 @@ func contractCodeHashFromTxEnvelope(transactionEnvelope xdr.TransactionV1Envelop
return ""
}

func ledgerKeyHashFromTxEnvelope(transactionEnvelope xdr.TransactionV1Envelope) string {
for _, ledgerKey := range transactionEnvelope.Tx.Ext.SorobanData.Resources.Footprint.ReadOnly {
ledgerKeyHash := utils.LedgerKeyToLedgerKeyHash(ledgerKey)
if ledgerKeyHash != "" {
return ledgerKeyHash
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

we just learned that restore/bump footprint ops will bump all keys in the footprint. Is it possible to store all keys in an array instead of saving only the first one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The last commit changes the ledgerKeyHashFromTxEnvelope function logic to store the ledger_key_hash in an array.

Here's an excerpt of an output from the export_operations command:

{
  "closed_at": "2024-02-06T18:42:32Z",
  "details": {
    "asset_balance_changes": [],
    "contract_code_hash": "4b9316721487281d8201e1c6044544400f120253487971e339eb23a465516935",
    "contract_id": "CAJRV7IFJ35V7W3NYDAUCG3BPQM32SBRX2R3OX7HQGTUJWOMJCA7VFKF",
    "function": "HostFunctionTypeHostFunctionTypeInvokeContract",
    "ledger_key_hash": [
      "980ea3840d17b86222a52d923a03cf9e918bd9851c00c0dd20a5f13aac18e523",
      "493f4eee738cf139a9d7690b355a0ce57f4bf36b489048edead7d1eba97a74e6",
      "8de7eeb5332495ed271a11d1849fc6f5ec970c73ca9cb5eb1a57126c4a278f56"
    ],
    "parameters": [
      {
        "type": "Address",
        "value": "AAAAEgAAAAETGv0FTvtf223AwUEbYXwZvUgxvqO3X+eBp0TZzEiB+g=="
      }...

Does that sound acceptable?


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

return ""
}

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