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

Fix scvals to print in json string #305

Merged
merged 14 commits into from
Jan 28, 2025
10 changes: 8 additions & 2 deletions internal/transform/contract_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,14 @@ func (t *TransformContractDataStruct) TransformContractData(ledgerChange ingest.

ledgerSequence := header.Header.LedgerSeq

outputKey, outputKeyDecoded := serializeScVal(contractData.Key)
outputVal, outputValDecoded := serializeScVal(contractData.Val)
outputKey, outputKeyDecoded, err := serializeScVal(contractData.Key)
if err != nil {
return ContractDataOutput{}, err, false
}
outputVal, outputValDecoded, err := serializeScVal(contractData.Val)
if err != nil {
return ContractDataOutput{}, err, false
}

outputContractDataXDR, err := xdr.MarshalBase64(contractData)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/transform/contract_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func makeContractDataTestOutput() []ContractDataOutput {

keyDecoded := map[string]string{
"type": "Instance",
"value": "0000000000000000000000000000000000000000000000000000000000000000: [{a a}]",
"value": "{\"Instance\":{\"Executable\":{\"Type\":0,\"WasmHash\":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]},\"Storage\":[{\"Key\":{\"Str\":\"a\",\"Type\":14},\"Val\":{\"Str\":\"a\",\"Type\":14}}]},\"Type\":19}",
}

val := map[string]string{
Expand All @@ -145,7 +145,7 @@ func makeContractDataTestOutput() []ContractDataOutput {

valDecoded := map[string]string{
"type": "B",
"value": "true",
"value": "{\"B\":true,\"Type\":0}",
}

return []ContractDataOutput{
Expand Down
51 changes: 41 additions & 10 deletions internal/transform/contract_events.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package transform

import (
"bytes"
"encoding/base64"
"fmt"
"os/exec"

"github.com/stellar/stellar-etl/internal/toid"
"github.com/stellar/stellar-etl/internal/utils"
Expand Down Expand Up @@ -48,12 +50,18 @@ func TransformContractEvent(transaction ingest.LedgerTransaction, lhe xdr.Ledger
outputTypeString := event.Type.String()

eventTopics := getEventTopics(event.Body)
outputTopics, outputTopicsDecoded := serializeScValArray(eventTopics)
outputTopics, outputTopicsDecoded, err := serializeScValArray(eventTopics)
if err != nil {
return []ContractEventOutput{}, err
}
outputTopicsJson["topics"] = outputTopics
outputTopicsDecodedJson["topics_decoded"] = outputTopicsDecoded

eventData := getEventData(event.Body)
outputData, outputDataDecoded := serializeScVal(eventData)
outputData, outputDataDecoded, err := serializeScVal(eventData)
if err != nil {
return []ContractEventOutput{}, err
}

// Convert the xdrContactId to string
// TODO: https://stellarorg.atlassian.net/browse/HUBBLE-386 this should be a stellar/go/xdr function
Expand Down Expand Up @@ -117,7 +125,7 @@ func getEventData(eventBody xdr.ContractEventBody) xdr.ScVal {
}

// TODO this should also be used in the operations processor
func serializeScVal(scVal xdr.ScVal) (map[string]string, map[string]string) {
func serializeScVal(scVal xdr.ScVal) (map[string]string, map[string]string, error) {
serializedData := map[string]string{}
serializedData["value"] = "n/a"
serializedData["type"] = "n/a"
Expand All @@ -129,25 +137,48 @@ func serializeScVal(scVal xdr.ScVal) (map[string]string, map[string]string) {
if scValTypeName, ok := scVal.ArmForSwitch(int32(scVal.Type)); ok {
serializedData["type"] = scValTypeName
serializedDataDecoded["type"] = scValTypeName
if raw, err := scVal.MarshalBinary(); err == nil {
serializedData["value"] = base64.StdEncoding.EncodeToString(raw)
serializedDataDecoded["value"] = scVal.String()
raw, err := scVal.MarshalBinary()
if err != nil {
return nil, nil, err
}

serializedData["value"] = base64.StdEncoding.EncodeToString(raw)
serializedDataDecoded["value"], err = runStellarXdrDecode(serializedData["value"])
if err != nil {
return nil, nil, err
}
}

return serializedData, serializedDataDecoded
return serializedData, serializedDataDecoded, nil
}

// TODO this should also be used in the operations processor
func serializeScValArray(scVals []xdr.ScVal) ([]map[string]string, []map[string]string) {
func serializeScValArray(scVals []xdr.ScVal) ([]map[string]string, []map[string]string, error) {
data := make([]map[string]string, 0, len(scVals))
dataDecoded := make([]map[string]string, 0, len(scVals))

for _, scVal := range scVals {
serializedData, serializedDataDecoded := serializeScVal(scVal)
serializedData, serializedDataDecoded, err := serializeScVal(scVal)
if err != nil {
return nil, nil, err
}
data = append(data, serializedData)
dataDecoded = append(dataDecoded, serializedDataDecoded)
}

return data, dataDecoded
return data, dataDecoded, nil
}

func runStellarXdrDecode(input string) (string, error) {
chowbao marked this conversation as resolved.
Show resolved Hide resolved
cmd := exec.Command("stellar", "xdr", "decode", "--type", "ScVal")
cmd.Stdin = bytes.NewBufferString(input)

var result bytes.Buffer
cmd.Stdout = &result
err := cmd.Run()
if err != nil {
return "", err
}

return result.String(), nil
}
4 changes: 2 additions & 2 deletions internal/transform/contract_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func makeContractEventTestOutput() (output [][]ContractEventOutput, err error) {
topicsDecoded["topics_decoded"] = []map[string]string{
{
"type": "B",
"value": "true",
"value": "{\"B\":true,\"Type\":0}",
chowbao marked this conversation as resolved.
Show resolved Hide resolved
},
}

Expand All @@ -68,7 +68,7 @@ func makeContractEventTestOutput() (output [][]ContractEventOutput, err error) {

dataDecoded := map[string]string{
"type": "B",
"value": "true",
"value": "{\"B\":true,\"Type\":0}",
}

output = [][]ContractEventOutput{{
Expand Down
Loading
Loading