github.com/stellar/stellar-etl@v1.0.1-0.20240312145900-4874b6bf2b89/internal/transform/diagnostic_events_test.go (about) 1 package transform 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/assert" 8 9 "github.com/stellar/go/ingest" 10 "github.com/stellar/go/xdr" 11 ) 12 13 func TestTransformDiagnosticEvent(t *testing.T) { 14 type inputStruct struct { 15 transaction ingest.LedgerTransaction 16 historyHeader xdr.LedgerHeaderHistoryEntry 17 } 18 type transformTest struct { 19 input inputStruct 20 wantOutput []DiagnosticEventOutput 21 wantErr error 22 } 23 24 hardCodedTransaction, hardCodedLedgerHeader, err := makeDiagnosticEventTestInput() 25 assert.NoError(t, err) 26 hardCodedOutput, err := makeDiagnosticEventTestOutput() 27 assert.NoError(t, err) 28 29 tests := []transformTest{} 30 31 for i := range hardCodedTransaction { 32 tests = append(tests, transformTest{ 33 input: inputStruct{hardCodedTransaction[i], hardCodedLedgerHeader[i]}, 34 wantOutput: hardCodedOutput[i], 35 wantErr: nil, 36 }) 37 } 38 39 for _, test := range tests { 40 actualOutput, actualError, _ := TransformDiagnosticEvent(test.input.transaction, test.input.historyHeader) 41 assert.Equal(t, test.wantErr, actualError) 42 assert.Equal(t, test.wantOutput, actualOutput) 43 } 44 } 45 46 func makeDiagnosticEventTestOutput() (output [][]DiagnosticEventOutput, err error) { 47 output = [][]DiagnosticEventOutput{{ 48 DiagnosticEventOutput{ 49 TransactionHash: "a87fef5eeb260269c380f2de456aad72b59bb315aaac777860456e09dac0bafb", 50 LedgerSequence: 30521816, 51 TransactionID: 131090201534533632, 52 ClosedAt: time.Date(2020, time.July, 9, 5, 28, 42, 0, time.UTC), 53 InSuccessfulContractCall: true, 54 ExtV: 0, 55 ContractId: "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABSC4", 56 Type: "ContractEventTypeDiagnostic", 57 BodyV: 0, 58 Body: "AAAAAQAAAAAAAAABAAAAAAAAAAE=", 59 }, 60 }} 61 return 62 } 63 func makeDiagnosticEventTestInput() (transaction []ingest.LedgerTransaction, historyHeader []xdr.LedgerHeaderHistoryEntry, err error) { 64 hardCodedMemoText := "HL5aCgozQHIW7sSc5XdcfmR" 65 hardCodedTransactionHash := xdr.Hash([32]byte{0xa8, 0x7f, 0xef, 0x5e, 0xeb, 0x26, 0x2, 0x69, 0xc3, 0x80, 0xf2, 0xde, 0x45, 0x6a, 0xad, 0x72, 0xb5, 0x9b, 0xb3, 0x15, 0xaa, 0xac, 0x77, 0x78, 0x60, 0x45, 0x6e, 0x9, 0xda, 0xc0, 0xba, 0xfb}) 66 var hardCodedContractId xdr.Hash 67 hardCodedBool := true 68 hardCodedTxMetaV3 := xdr.TransactionMetaV3{ 69 SorobanMeta: &xdr.SorobanTransactionMeta{ 70 DiagnosticEvents: []xdr.DiagnosticEvent{ 71 { 72 InSuccessfulContractCall: true, 73 Event: xdr.ContractEvent{ 74 Ext: xdr.ExtensionPoint{ 75 V: 0, 76 }, 77 ContractId: &hardCodedContractId, 78 Type: xdr.ContractEventTypeDiagnostic, 79 Body: xdr.ContractEventBody{ 80 V: 0, 81 V0: &xdr.ContractEventV0{ 82 Topics: []xdr.ScVal{ 83 { 84 Type: xdr.ScValTypeScvBool, 85 B: &hardCodedBool, 86 }, 87 }, 88 Data: xdr.ScVal{ 89 Type: xdr.ScValTypeScvBool, 90 B: &hardCodedBool, 91 }, 92 }, 93 }, 94 }, 95 }, 96 }, 97 }, 98 } 99 100 genericResultResults := &[]xdr.OperationResult{ 101 xdr.OperationResult{ 102 Tr: &xdr.OperationResultTr{ 103 Type: xdr.OperationTypeCreateAccount, 104 CreateAccountResult: &xdr.CreateAccountResult{ 105 Code: 0, 106 }, 107 }, 108 }, 109 } 110 hardCodedMeta := xdr.TransactionMeta{ 111 V: 3, 112 V3: &hardCodedTxMetaV3, 113 } 114 115 destination := xdr.MuxedAccount{ 116 Type: xdr.CryptoKeyTypeKeyTypeEd25519, 117 Ed25519: &xdr.Uint256{1, 2, 3}, 118 } 119 120 transaction = []ingest.LedgerTransaction{ 121 ingest.LedgerTransaction{ 122 Index: 1, 123 UnsafeMeta: hardCodedMeta, 124 Envelope: xdr.TransactionEnvelope{ 125 Type: xdr.EnvelopeTypeEnvelopeTypeTx, 126 V1: &xdr.TransactionV1Envelope{ 127 Tx: xdr.Transaction{ 128 SourceAccount: testAccount1, 129 SeqNum: 112351890582290871, 130 Memo: xdr.Memo{ 131 Type: xdr.MemoTypeMemoText, 132 Text: &hardCodedMemoText, 133 }, 134 Fee: 90000, 135 Cond: xdr.Preconditions{ 136 Type: xdr.PreconditionTypePrecondTime, 137 TimeBounds: &xdr.TimeBounds{ 138 MinTime: 0, 139 MaxTime: 1594272628, 140 }, 141 }, 142 Operations: []xdr.Operation{ 143 xdr.Operation{ 144 SourceAccount: &testAccount2, 145 Body: xdr.OperationBody{ 146 Type: xdr.OperationTypePathPaymentStrictReceive, 147 PathPaymentStrictReceiveOp: &xdr.PathPaymentStrictReceiveOp{ 148 Destination: destination, 149 }, 150 }, 151 }, 152 }, 153 }, 154 }, 155 }, 156 Result: xdr.TransactionResultPair{ 157 TransactionHash: hardCodedTransactionHash, 158 Result: xdr.TransactionResult{ 159 FeeCharged: 300, 160 Result: xdr.TransactionResultResult{ 161 Code: xdr.TransactionResultCodeTxFailed, 162 Results: genericResultResults, 163 }, 164 }, 165 }, 166 }, 167 } 168 historyHeader = []xdr.LedgerHeaderHistoryEntry{ 169 xdr.LedgerHeaderHistoryEntry{ 170 Header: xdr.LedgerHeader{ 171 LedgerSeq: 30521816, 172 ScpValue: xdr.StellarValue{CloseTime: 1594272522}, 173 }, 174 }, 175 } 176 return 177 }