Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
psheth9 committed Feb 12, 2024
1 parent 40b6a1d commit fb034f5
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 12 deletions.
77 changes: 77 additions & 0 deletions cmd/soroban-rpc/internal/methods/get_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,44 @@ func txMeta(acctSeq uint32, successful bool) xdr.LedgerCloseMeta {
}
}

func txMetaWithEvents(acctSeq uint32, successful bool) xdr.LedgerCloseMeta {

meta := txMeta(acctSeq, successful)

contractIDBytes, _ := hex.DecodeString("df06d62447fd25da07c0135eed7557e5a5497ee7d15b7fe345bd47e191d8f577")
var contractID xdr.Hash
copy(contractID[:], contractIDBytes)
counter := xdr.ScSymbol("COUNTER")

meta.V1.TxProcessing[0].TxApplyProcessing.V3 = &xdr.TransactionMetaV3{
SorobanMeta: &xdr.SorobanTransactionMeta{
Events: []xdr.ContractEvent{{
ContractId: &contractID,
Type: xdr.ContractEventTypeContract,
Body: xdr.ContractEventBody{
V: 0,
V0: &xdr.ContractEventV0{
Topics: []xdr.ScVal{{
Type: xdr.ScValTypeScvSymbol,
Sym: &counter,
}},
Data: xdr.ScVal{
Type: xdr.ScValTypeScvSymbol,
Sym: &counter,
},
},
},
}},
ReturnValue: xdr.ScVal{
Type: xdr.ScValTypeScvSymbol,
Sym: &counter,
},
},
}

return meta
}

func txEnvelope(acctSeq uint32) xdr.TransactionEnvelope {
envelope, err := xdr.NewTransactionEnvelope(xdr.EnvelopeTypeEnvelopeTypeTx, xdr.TransactionV1Envelope{
Tx: xdr.Transaction{
Expand Down Expand Up @@ -210,4 +248,43 @@ func TestGetTransaction(t *testing.T) {
LedgerCloseTime: 2650,
DiagnosticEventsXDR: []string{},
}, tx)

// Test Txn with events
meta = txMetaWithEvents(3, true)
err = store.IngestTransactions(meta)
require.NoError(t, err)

xdrHash = txHash(3)
hash = hex.EncodeToString(xdrHash[:])

expectedTxResult, err = xdr.MarshalBase64(meta.V1.TxProcessing[0].Result.Result)
require.NoError(t, err)
expectedEnvelope, err = xdr.MarshalBase64(txEnvelope(3))
require.NoError(t, err)
expectedTxMeta, err = xdr.MarshalBase64(meta.V1.TxProcessing[0].TxApplyProcessing)
require.NoError(t, err)

diagnosticEvents, err := meta.V1.TxProcessing[0].TxApplyProcessing.GetDiagnosticEvents()
require.NoError(t, err)
expectedEventsMeta, err := xdr.MarshalBase64(diagnosticEvents[0])

tx, err = GetTransaction(store, GetTransactionRequest{hash})
require.NoError(t, err)
require.NoError(t, err)
require.Equal(t, GetTransactionResponse{
Status: TransactionStatusSuccess,
LatestLedger: 103,
LatestLedgerCloseTime: 2675,
OldestLedger: 101,
OldestLedgerCloseTime: 2625,
ApplicationOrder: 1,
FeeBump: false,
EnvelopeXdr: expectedEnvelope,
ResultXdr: expectedTxResult,
ResultMetaXdr: expectedTxMeta,
Ledger: 103,
LedgerCloseTime: 2675,
DiagnosticEventsXDR: []string{expectedEventsMeta},
}, tx)

}
17 changes: 5 additions & 12 deletions cmd/soroban-rpc/internal/transactions/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,18 +362,11 @@ func TestGetTransactionsWithEventData(t *testing.T) {
require.NotNil(t, tx.Events)
require.Len(t, tx.Events, 1)

var event xdr.DiagnosticEvent
err := event.UnmarshalBinary(tx.Events[0])
require.Nil(t, err)
require.Equal(t, xdr.ContractEventTypeContract, event.Event.Type)
require.Equal(t, int32(0), event.Event.Body.V)

require.NotNil(t, event.Event.Body.V0.Topics)
require.Equal(t, xdr.ScValTypeScvSymbol, event.Event.Body.V0.Topics[0].Type)

require.NotNil(t, event.Event.Body.V0.Data)
require.Equal(t, xdr.ScValTypeScvSymbol, event.Event.Body.V0.Data.Type)

events, err := metaWithEvents.V1.TxProcessing[0].TxApplyProcessing.GetDiagnosticEvents()
require.NoError(t, err)
eventBytes, err := events[0].MarshalBinary()
require.NoError(t, err)
require.Equal(t, eventBytes, tx.Events[0])
}

func stableHeapInUse() int64 {
Expand Down

0 comments on commit fb034f5

Please sign in to comment.