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

Update getTxn rpc with events data #58

Merged
merged 9 commits into from
Feb 12, 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
6 changes: 6 additions & 0 deletions cmd/soroban-rpc/internal/methods/get_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ type GetTransactionResponse struct {
Ledger uint32 `json:"ledger,omitempty"`
// LedgerCloseTime is the unix timestamp of when the transaction was included in the ledger.
LedgerCloseTime int64 `json:"createdAt,string,omitempty"`

// DiagnosticEventsXDR is present only if Status is equal to TransactionFailed.
// DiagnosticEventsXDR is a base64-encoded slice of xdr.DiagnosticEvent
DiagnosticEventsXDR []string `json:"diagnosticEventsXdr,omitempty"`
}

type GetTransactionRequest struct {
Expand Down Expand Up @@ -104,6 +108,8 @@ func GetTransaction(getter transactionGetter, request GetTransactionRequest) (Ge
response.ResultXdr = base64.StdEncoding.EncodeToString(tx.Result)
response.EnvelopeXdr = base64.StdEncoding.EncodeToString(tx.Envelope)
response.ResultMetaXdr = base64.StdEncoding.EncodeToString(tx.Meta)
response.DiagnosticEventsXDR = base64EncodeSlice(tx.Events)

if tx.Successful {
response.Status = TransactionStatusSuccess
} else {
Expand Down
3 changes: 3 additions & 0 deletions cmd/soroban-rpc/internal/methods/get_transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func TestGetTransaction(t *testing.T) {
ResultMetaXdr: expectedTxMeta,
Ledger: 101,
LedgerCloseTime: 2625,
DiagnosticEventsXDR: []string{},
psheth9 marked this conversation as resolved.
Show resolved Hide resolved
}, tx)

// ingest another (failed) transaction
Expand All @@ -177,6 +178,7 @@ func TestGetTransaction(t *testing.T) {
ResultMetaXdr: expectedTxMeta,
Ledger: 101,
LedgerCloseTime: 2625,
DiagnosticEventsXDR: []string{},
}, tx)

// the new transaction should also be there
Expand Down Expand Up @@ -206,5 +208,6 @@ func TestGetTransaction(t *testing.T) {
ResultMetaXdr: expectedTxMeta,
Ledger: 102,
LedgerCloseTime: 2650,
DiagnosticEventsXDR: []string{},
}, tx)
}
30 changes: 27 additions & 3 deletions cmd/soroban-rpc/internal/transactions/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,10 @@ type LedgerInfo struct {
}

type Transaction struct {
Result []byte // XDR encoded xdr.TransactionResult
Meta []byte // XDR encoded xdr.TransactionMeta
Envelope []byte // XDR encoded xdr.TransactionEnvelope
Result []byte // XDR encoded xdr.TransactionResult
Meta []byte // XDR encoded xdr.TransactionMeta
Envelope []byte // XDR encoded xdr.TransactionEnvelope
Events [][]byte // XDR encoded xdr.DiagnosticEvent
FeeBump bool
ApplicationOrder int32
Successful bool
Expand Down Expand Up @@ -198,10 +199,33 @@ func (m *MemoryStore) GetTransaction(hash xdr.Hash) (Transaction, bool, StoreRan
if !ok {
return Transaction{}, false, storeRange
}

var txMeta xdr.TransactionMeta
err := txMeta.UnmarshalBinary(internalTx.meta)
if err != nil {
return Transaction{}, false, storeRange
}

txEvents, err := txMeta.GetDiagnosticEvents()
if err != nil {
return Transaction{}, false, storeRange
}

var events = make([][]byte, 0, len(txEvents))

psheth9 marked this conversation as resolved.
Show resolved Hide resolved
for _, e := range txEvents {
diagnosticEventXDR, err := e.MarshalBinary()
if err != nil {
return Transaction{}, false, storeRange
}
events = append(events, diagnosticEventXDR)
}

tx := Transaction{
Result: internalTx.result,
Meta: internalTx.meta,
Envelope: internalTx.envelope,
Events: events,
FeeBump: internalTx.feeBump,
Successful: internalTx.successful,
ApplicationOrder: internalTx.applicationOrder,
Expand Down
51 changes: 51 additions & 0 deletions cmd/soroban-rpc/internal/transactions/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,42 @@ func txMeta(ledgerSequence uint32, feeBump bool) xdr.LedgerCloseMeta {
}
}

func txMetaWithEvents(ledgerSequence uint32, feeBump bool) xdr.LedgerCloseMeta {
tx := txMeta(ledgerSequence, feeBump)
contractIDBytes, _ := hex.DecodeString("df06d62447fd25da07c0135eed7557e5a5497ee7d15b7fe345bd47e191d8f577")
var contractID xdr.Hash
copy(contractID[:], contractIDBytes)
counter := xdr.ScSymbol("COUNTER")

tx.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 tx
}

func txEnvelope(ledgerSequence uint32, feeBump bool) xdr.TransactionEnvelope {
var envelope xdr.TransactionEnvelope
var err error
Expand Down Expand Up @@ -311,6 +347,21 @@ func TestIngestTransactions(t *testing.T) {
require.Len(t, store.transactions, 3)
}

func TestGetTransactionsWithEventData(t *testing.T) {
store := NewMemoryStore(interfaces.MakeNoOpDeamon(), "passphrase", 100)

// Insert ledger 1
metaWithEvents := txMetaWithEvents(1, false)
require.NoError(t, store.IngestTransactions(metaWithEvents))
require.Len(t, store.transactions, 1)

// check events data
tx, ok, _ := store.GetTransaction(txHash(1, false))
require.True(t, ok)
require.NotNil(t, tx.Events)
require.Equal(t, 1, len(tx.Events))
Shaptic marked this conversation as resolved.
Show resolved Hide resolved
}

func stableHeapInUse() int64 {
var (
m = runtime.MemStats{}
Expand Down
36 changes: 30 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,30 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/stellar/go v0.0.0-20240109175136-3ca501f09055
github.com/stellar/go v0.0.0-20240207003209-73de95c8eb55
github.com/stretchr/testify v1.8.4
golang.org/x/mod v0.13.0
gotest.tools/v3 v3.5.0
)

require (
cloud.google.com/go v0.111.0 // indirect
cloud.google.com/go/compute v1.23.3 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v1.1.5 // indirect
cloud.google.com/go/storage v1.30.1 // indirect
dario.cat/mergo v1.0.0 // indirect
github.com/cloudflare/circl v1.3.5 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pjbgf/sha1cd v0.3.0 // indirect
Expand All @@ -37,8 +50,19 @@ require (
github.com/skeema/knownhosts v1.2.1 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/oauth2 v0.13.0 // indirect
golang.org/x/tools v0.14.0 // indirect
google.golang.org/api v0.149.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231211222908-989df2bf70f3 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917 // indirect
google.golang.org/grpc v1.60.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)

Expand Down Expand Up @@ -85,13 +109,13 @@ require (
github.com/stellar/go-xdr v0.0.0-20231122183749-b53fb00bcac2 // indirect
github.com/stretchr/objx v0.5.1 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/tylerb/graceful.v1 v1.2.15 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading