Skip to content

Commit

Permalink
Address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio committed Jan 21, 2023
1 parent 3bb8896 commit 3411a2f
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions cmd/soroban-rpc/internal/ledgerentry_storage/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,21 +242,28 @@ func (l *ledgerUpdaterTx) flushLedgerEntryBatch() error {
}

func (l *ledgerUpdaterTx) UpsertLedgerEntry(key xdr.LedgerKey, entry xdr.LedgerEntry) error {
if err := l.upsertLedgerEntry(key, entry); err != nil {
_ = l.tx.Rollback()
return err
}
return nil
}

// UpsertLedgerEntry() counterpart with no rollbacks (so that we only rollback in one place)
func (l *ledgerUpdaterTx) upsertLedgerEntry(key xdr.LedgerKey, entry xdr.LedgerEntry) error {
encodedKey, err := encodeLedgerKey(l.buffer, key)
if err != nil {
return err
}
// safe since we cast to string right away
encodedEntry, err := l.buffer.UnsafeMarshalBinary(&entry)
if err != nil {
_ = l.tx.Rollback()
return err
}
encodedEntryStr := string(encodedEntry)
l.keyToEntryBatch[encodedKey] = &encodedEntryStr
if len(l.keyToEntryBatch) >= l.maxBatchSize {
if err := l.flushLedgerEntryBatch(); err != nil {
_ = l.tx.Rollback()
return err
}
// reset map
Expand All @@ -266,15 +273,22 @@ func (l *ledgerUpdaterTx) UpsertLedgerEntry(key xdr.LedgerKey, entry xdr.LedgerE
}

func (l *ledgerUpdaterTx) DeleteLedgerEntry(key xdr.LedgerKey) error {
if err := l.deleteLedgerEntry(key); err != nil {
_ = l.tx.Rollback()
return err
}
return nil
}

// DeleteLedgerEntry() counterpart with no rollbacks (so that we only rollback in one place)
func (l *ledgerUpdaterTx) deleteLedgerEntry(key xdr.LedgerKey) error {
encodedKey, err := encodeLedgerKey(l.buffer, key)
if err != nil {
_ = l.tx.Rollback()
return err
}
l.keyToEntryBatch[encodedKey] = nil
if len(l.keyToEntryBatch) > l.maxBatchSize {
if err := l.flushLedgerEntryBatch(); err != nil {
_ = l.tx.Rollback()
return err
}
// reset map
Expand All @@ -284,11 +298,7 @@ func (l *ledgerUpdaterTx) DeleteLedgerEntry(key xdr.LedgerKey) error {
}

func (l *ledgerUpdaterTx) Done() error {
if err := l.flushLedgerEntryBatch(); err != nil {
_ = l.tx.Rollback()
return err
}
if err := upsertLatestLedgerSequence(l.tx, l.forLedgerSequence); err != nil {
if err := l.done(); err != nil {
_ = l.tx.Rollback()
return err
}
Expand All @@ -303,6 +313,14 @@ func (l *ledgerUpdaterTx) Done() error {
return nil
}

// Done() counterpart with no rollbacks or commits (so that we only rollback in one place)
func (l *ledgerUpdaterTx) done() error {
if err := l.flushLedgerEntryBatch(); err != nil {
return err
}
return upsertLatestLedgerSequence(l.tx, l.forLedgerSequence)
}

func encodeLedgerKey(buffer *xdr.EncodingBuffer, key xdr.LedgerKey) (string, error) {
// this is safe since we are converting to string right away, which causes a copy
binKey, err := buffer.LedgerKeyUnsafeMarshalBinaryCompress(key)
Expand Down

0 comments on commit 3411a2f

Please sign in to comment.