Skip to content

Commit

Permalink
Make contract id a blob type
Browse files Browse the repository at this point in the history
  • Loading branch information
psheth9 committed Jun 26, 2024
1 parent 814b6ae commit f3c9461
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
10 changes: 4 additions & 6 deletions cmd/soroban-rpc/internal/db/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"io"
"time"

"github.com/stellar/go/strkey"

"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/ledgerbucketwindow"

sq "github.com/Masterminds/squirrel"
Expand All @@ -29,7 +27,7 @@ type EventWriter interface {

// EventReader has all the public methods to fetch events from DB
type EventReader interface {
GetEvents(ctx context.Context, cursorRange events.CursorRange, contractIDs []string, f ScanFunction) error
GetEvents(ctx context.Context, cursorRange events.CursorRange, contractIDs [][]byte, f ScanFunction) error
GetLedgerRange(ctx context.Context) (ledgerbucketwindow.LedgerRange, error)
}

Expand Down Expand Up @@ -96,9 +94,9 @@ func (eventHandler *eventHandler) InsertEvents(lcm xdr.LedgerCloseMeta) error {
Columns("id", "ledger_sequence", "application_order", "contract_id", "event_type")

for index, e := range txEvents {
var contractID string
var contractID []byte
if e.Event.ContractId != nil {
contractID = strkey.MustEncode(strkey.VersionByteContract, (*e.Event.ContractId)[:])
contractID = e.Event.ContractId[:]
}
id := events.Cursor{Ledger: lcm.LedgerSequence(), Tx: tx.Index, Op: 0, Event: uint32(index)}.String()
query = query.Values(id, lcm.LedgerSequence(), tx.Index, contractID, int(e.Event.Type))
Expand Down Expand Up @@ -142,7 +140,7 @@ func (eventHandler *eventHandler) trimEvents(latestLedgerSeq uint32, retentionWi
func (eventHandler *eventHandler) GetEvents(
ctx context.Context,
cursorRange events.CursorRange,
contractIDs []string,
contractIDs [][]byte,
f ScanFunction,
) error {
start := time.Now()
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-rpc/internal/db/sqlmigrations/03_events.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CREATE TABLE events(
id TEXT PRIMARY KEY,
ledger_sequence INTEGER NOT NULL,
application_order INTEGER NOT NULL,
contract_id TEXT,
contract_id BLOB(32),
event_type INTEGER NOT NULL
);

Expand Down
21 changes: 16 additions & 5 deletions cmd/soroban-rpc/internal/methods/get_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ type eventsRPCHandler struct {
networkPassphrase string
}

func combineContractIDs(filters []EventFilter) []string {
func combineContractIDs(filters []EventFilter) ([][]byte, error) {
contractIDSet := make(map[string]struct{})

for _, filter := range filters {
Expand All @@ -323,11 +323,16 @@ func combineContractIDs(filters []EventFilter) []string {
}
}

contractIDs := make([]string, 0, len(contractIDSet))
contractIDs := make([][]byte, 0, len(contractIDSet))
for contractID := range contractIDSet {
contractIDs = append(contractIDs, contractID)
id, err := strkey.Decode(strkey.VersionByteContract, contractID)
if err != nil {
return nil, fmt.Errorf("contract ID %v invalid", contractID)
}

contractIDs = append(contractIDs, id)
}
return contractIDs
return contractIDs, nil
}

func (h eventsRPCHandler) getEvents(ctx context.Context, request GetEventsRequest) (GetEventsResponse, error) {
Expand Down Expand Up @@ -379,7 +384,13 @@ func (h eventsRPCHandler) getEvents(ctx context.Context, request GetEventsReques
var found []entry
cursorSet := make(map[string]struct{})

contractIDs := combineContractIDs(request.Filters)
contractIDs, err := combineContractIDs(request.Filters)
if err != nil {
return GetEventsResponse{}, &jrpc2.Error{
Code: jrpc2.InvalidParams,
Message: err.Error(),
}
}

// Scan function to apply filters
f := func(event xdr.DiagnosticEvent, cursor events.Cursor, ledgerCloseTimestamp int64, txHash *xdr.Hash) bool {
Expand Down

0 comments on commit f3c9461

Please sign in to comment.