Skip to content

Commit

Permalink
services/horizon/internal/ingest/processors: Ingest new invoke host o…
Browse files Browse the repository at this point in the history
…peration type (#5488)
  • Loading branch information
tamirms authored Oct 9, 2024
1 parent 53e8638 commit 2490088
Show file tree
Hide file tree
Showing 13 changed files with 713 additions and 313 deletions.
73 changes: 51 additions & 22 deletions services/horizon/internal/ingest/processors/operations_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ func (operation *transactionOperationWrapper) Details() (map[string]interface{},
op := operation.operation.Body.MustClaimClaimableBalanceOp()
balanceID, err := xdr.MarshalHex(op.BalanceId)
if err != nil {
panic(fmt.Errorf("Invalid balanceId in op: %d", operation.index))
return nil, fmt.Errorf("Invalid balanceId in op: %d", operation.index)
}
details["balance_id"] = balanceID
addAccountAndMuxedAccountDetails(details, *source, "claimant")
Expand Down Expand Up @@ -585,7 +585,7 @@ func (operation *transactionOperationWrapper) Details() (map[string]interface{},
op := operation.operation.Body.MustClawbackClaimableBalanceOp()
balanceID, err := xdr.MarshalHex(op.BalanceId)
if err != nil {
panic(fmt.Errorf("Invalid balanceId in op: %d", operation.index))
return nil, fmt.Errorf("Invalid balanceId in op: %d", operation.index)
}
details["balance_id"] = balanceID
case xdr.OperationTypeSetTrustLineFlags:
Expand Down Expand Up @@ -676,22 +676,7 @@ func (operation *transactionOperationWrapper) Details() (map[string]interface{},
args = append(args, xdr.ScVal{Type: xdr.ScValTypeScvAddress, Address: &invokeArgs.ContractAddress})
args = append(args, xdr.ScVal{Type: xdr.ScValTypeScvSymbol, Sym: &invokeArgs.FunctionName})
args = append(args, invokeArgs.Args...)
params := make([]map[string]string, 0, len(args))

for _, param := range args {
serializedParam := map[string]string{}
serializedParam["value"] = "n/a"
serializedParam["type"] = "n/a"

if scValTypeName, ok := param.ArmForSwitch(int32(param.Type)); ok {
serializedParam["type"] = scValTypeName
if raw, err := param.MarshalBinary(); err == nil {
serializedParam["value"] = base64.StdEncoding.EncodeToString(raw)
}
}
params = append(params, serializedParam)
}
details["parameters"] = params
details["parameters"] = extractFunctionArgs(args)

if balanceChanges, err := operation.parseAssetBalanceChangesFromContractEvents(); err != nil {
return nil, err
Expand All @@ -706,7 +691,25 @@ func (operation *transactionOperationWrapper) Details() (map[string]interface{},
fromAddress := args.ContractIdPreimage.MustFromAddress()
address, err := fromAddress.Address.String()
if err != nil {
panic(fmt.Errorf("error obtaining address for: %s", args.ContractIdPreimage.Type))
return nil, fmt.Errorf("error obtaining address for: %s", args.ContractIdPreimage.Type)
}
details["from"] = "address"
details["address"] = address
details["salt"] = fromAddress.Salt.String()
case xdr.ContractIdPreimageTypeContractIdPreimageFromAsset:
details["from"] = "asset"
details["asset"] = args.ContractIdPreimage.MustFromAsset().StringCanonical()
default:
return nil, fmt.Errorf("unknown contract id type: %s", args.ContractIdPreimage.Type)
}
case xdr.HostFunctionTypeHostFunctionTypeCreateContractV2:
args := op.HostFunction.MustCreateContractV2()
switch args.ContractIdPreimage.Type {
case xdr.ContractIdPreimageTypeContractIdPreimageFromAddress:
fromAddress := args.ContractIdPreimage.MustFromAddress()
address, err := fromAddress.Address.String()
if err != nil {
return nil, fmt.Errorf("error obtaining address for: %s", args.ContractIdPreimage.Type)
}
details["from"] = "address"
details["address"] = address
Expand All @@ -715,18 +718,26 @@ func (operation *transactionOperationWrapper) Details() (map[string]interface{},
details["from"] = "asset"
details["asset"] = args.ContractIdPreimage.MustFromAsset().StringCanonical()
default:
panic(fmt.Errorf("unknown contract id type: %s", args.ContractIdPreimage.Type))
return nil, fmt.Errorf("unknown contract id type: %s", args.ContractIdPreimage.Type)
}

details["parameters"] = extractFunctionArgs(args.ConstructorArgs)

if balanceChanges, err := operation.parseAssetBalanceChangesFromContractEvents(); err != nil {
return nil, err
} else {
details["asset_balance_changes"] = balanceChanges
}
case xdr.HostFunctionTypeHostFunctionTypeUploadContractWasm:
default:
panic(fmt.Errorf("unknown host function type: %s", op.HostFunction.Type))
return nil, fmt.Errorf("unknown host function type: %s", op.HostFunction.Type)
}
case xdr.OperationTypeExtendFootprintTtl:
op := operation.operation.Body.MustExtendFootprintTtlOp()
details["extend_to"] = op.ExtendTo
case xdr.OperationTypeRestoreFootprint:
default:
panic(fmt.Errorf("unknown operation type: %s", operation.OperationType()))
return nil, fmt.Errorf("unknown operation type: %s", operation.OperationType())
}

sponsor, err := operation.getSponsor()
Expand All @@ -740,6 +751,24 @@ func (operation *transactionOperationWrapper) Details() (map[string]interface{},
return details, nil
}

func extractFunctionArgs(args []xdr.ScVal) []map[string]string {
params := make([]map[string]string, 0, len(args))
for _, param := range args {
serializedParam := map[string]string{}
serializedParam["value"] = "n/a"
serializedParam["type"] = "n/a"

if scValTypeName, ok := param.ArmForSwitch(int32(param.Type)); ok {
serializedParam["type"] = scValTypeName
if raw, err := param.MarshalBinary(); err == nil {
serializedParam["value"] = base64.StdEncoding.EncodeToString(raw)
}
}
params = append(params, serializedParam)
}
return params
}

// Searches an operation for SAC events that are of a type which represent
// asset balances having changed.
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2282,9 +2282,6 @@ func TestDetailsCoversAllOperationTypes(t *testing.T) {
operation: op,
ledgerSequence: 1,
}
// calling Details should panic with unknown operation type
f := func() {
operation.Details()
}
assert.PanicsWithError(t, "unknown operation type: ", f)
_, err := operation.Details()
assert.ErrorContains(t, err, "unknown operation type: ")
}
Loading

0 comments on commit 2490088

Please sign in to comment.