Skip to content

Commit

Permalink
stellar#4433: used CheckpointManager for some of checkpoint math
Browse files Browse the repository at this point in the history
  • Loading branch information
sreuland committed Jul 22, 2022
1 parent 223626e commit 62370cf
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 30 deletions.
8 changes: 4 additions & 4 deletions exp/lighthorizon/actions/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func NewTXByAccountHandler(lightHorizon services.LightHorizon) func(http.Respons
txns, err := lightHorizon.Transactions.GetTransactionsByAccount(ctx, paginate.Cursor, paginate.Limit, accountId)
if err != nil {
log.Error(err)
sendErrorResponse(w, http.StatusInternalServerError, "")
sendErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

Expand All @@ -74,7 +74,7 @@ func NewTXByAccountHandler(lightHorizon services.LightHorizon) func(http.Respons
response, err = adapters.PopulateTransaction(r, &txn)
if err != nil {
log.Error(err)
sendErrorResponse(w, http.StatusInternalServerError, "")
sendErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

Expand Down Expand Up @@ -107,7 +107,7 @@ func NewOpsByAccountHandler(lightHorizon services.LightHorizon) func(http.Respon
ops, err := lightHorizon.Operations.GetOperationsByAccount(ctx, paginate.Cursor, paginate.Limit, accountId)
if err != nil {
log.Error(err)
sendErrorResponse(w, http.StatusInternalServerError, "")
sendErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

Expand All @@ -116,7 +116,7 @@ func NewOpsByAccountHandler(lightHorizon services.LightHorizon) func(http.Respon
response, err = adapters.PopulateOperation(r, &op)
if err != nil {
log.Error(err)
sendErrorResponse(w, http.StatusInternalServerError, "")
sendErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

Expand Down
3 changes: 2 additions & 1 deletion exp/lighthorizon/actions/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package actions
import (
"embed"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -65,7 +66,7 @@ func sendPageResponse(w http.ResponseWriter, page hal.Page) {

func sendErrorResponse(w http.ResponseWriter, errorCode int, errorMsg string) {
if errorMsg != "" {
http.Error(w, errorMsg, errorCode)
http.Error(w, fmt.Sprintf("Error: %s", errorMsg), errorCode)
} else {
http.Error(w, string(serverError), errorCode)
}
Expand Down
28 changes: 15 additions & 13 deletions exp/lighthorizon/services/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stellar/go/exp/lighthorizon/archive"
"github.com/stellar/go/exp/lighthorizon/common"
"github.com/stellar/go/exp/lighthorizon/index"
"github.com/stellar/go/historyarchive"
"github.com/stellar/go/toid"
"github.com/stellar/go/xdr"

Expand All @@ -18,6 +19,10 @@ const (
allIndexes = "all/all"
)

var (
checkpointManager = historyarchive.NewCheckpointManager(0)
)

type LightHorizon struct {
Operations OperationsService
Transactions TransactionsService
Expand Down Expand Up @@ -111,7 +116,7 @@ func searchTxByAccount(ctx context.Context, cursor int64, accountId string, conf
for {
ledger, ledgerErr := config.Archive.GetLedger(ctx, uint32(nextLedger))
if ledgerErr != nil {
return errors.Wrapf(ledgerErr, "ledger export state is out of sync, missing ledger %v from checkpoint %v", nextLedger, nextLedger/64)
return errors.Wrapf(ledgerErr, "ledger export state is out of sync, missing ledger %v from checkpoint %v", nextLedger, getCheckpointCounter(uint32(nextLedger)))
}

reader, readerErr := config.Archive.NewLedgerTransactionReaderFromLedgerCloseMeta(config.Passphrase, ledger)
Expand Down Expand Up @@ -157,31 +162,28 @@ func searchTxByAccount(ctx context.Context, cursor int64, accountId string, conf

// this deals in ledgers but adapts to the index model, which is currently keyed by checkpoint for now
func getAccountNextLedgerCursor(accountId string, cursor int64, store index.Store, indexName string) (uint64, error) {
nextLedger := toid.Parse(cursor).LedgerSequence + 1
nextLedger := uint32(toid.Parse(cursor).LedgerSequence + 1)

// done for performance reasons, skip reading the index for any requested ledger cursors
// only need to read the index when next cursor falls on checkpoint boundary
if !isCheckpoint(nextLedger) {
if !checkpointManager.IsCheckpoint(nextLedger) {
return uint64(nextLedger), nil
}

nextCheckpoint := uint32(nextLedger / 64)
queryStartingCheckpoint := nextCheckpoint
if queryStartingCheckpoint > 0 {
// the 'NextActive' index query takes a starting checkpoint, from which the index is scanned AFTER that checkpoint, non-inclusive
// so, we need to calculate one prior to our starting query
queryStartingCheckpoint = queryStartingCheckpoint - 1
}
// the 'NextActive' index query takes a starting checkpoint, from which the index is scanned AFTER that checkpoint, non-inclusive
// use the the currrent checkpoint as the starting point since it represents up to the cursor's ledger
queryStartingCheckpoint := getCheckpointCounter(nextLedger)
indexNextCheckpoint, err := store.NextActive(accountId, indexName, queryStartingCheckpoint)

if err != nil {
return 0, err
}

// return the first ledger of the next checkpoint that had account activity after cursor
return uint64(indexNextCheckpoint * 64), nil
return uint64(indexNextCheckpoint * checkpointManager.GetCheckpointFrequency()), nil
}

func isCheckpoint(ledger int32) bool {
return ledger%64 == 0
func getCheckpointCounter(ledger uint32) uint32 {
return checkpointManager.GetCheckpoint(uint32(ledger)) /
checkpointManager.GetCheckpointFrequency()
}
28 changes: 16 additions & 12 deletions exp/lighthorizon/services/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (
)

func TestItGetsTransactionsByAccount(tt *testing.T) {
// l=1586111, t=1, o=1
// cursor = 6812294872829953, checkpoint=24782
cursor := int64(6812294872829953)
// l=1586045, t=1, o=1
// cursor = 6812011404988417, checkpoint=24781

cursor := int64(6812011404988417)
ctx := context.Background()
passphrase := "White New England clam chowder"
archive, store := mockArchiveAndIndex(ctx, passphrase)
Expand All @@ -36,9 +37,10 @@ func TestItGetsTransactionsByAccount(tt *testing.T) {
}

func TestItGetsTransactionsByAccountAndPageLimit(tt *testing.T) {
// l=1586111, t=1, o=1
// cursor = 6812294872829953, checkpoint=24782
cursor := int64(6812294872829953)
// l=1586045, t=1, o=1
// cursor = 6812011404988417, checkpoint=24781

cursor := int64(6812011404988417)
ctx := context.Background()
passphrase := "White New England clam chowder"
archive, store := mockArchiveAndIndex(ctx, passphrase)
Expand All @@ -61,9 +63,10 @@ func TestItGetsTransactionsByAccountAndPageLimit(tt *testing.T) {
}

func TestItGetsOperationsByAccount(tt *testing.T) {
// l=1586111, t=1, o=1
// cursor = 6812294872829953, checkpoint=24782
cursor := int64(6812294872829953)
// l=1586045, t=1, o=1
// cursor = 6812011404988417, checkpoint=24781

cursor := int64(6812011404988417)
ctx := context.Background()
passphrase := "White New England clam chowder"
archive, store := mockArchiveAndIndex(ctx, passphrase)
Expand All @@ -84,9 +87,10 @@ func TestItGetsOperationsByAccount(tt *testing.T) {
}

func TestItGetsOperationsByAccountAndPageLimit(tt *testing.T) {
// l=1586111, t=1, o=1
// cursor = 6812294872829953, checkpoint=24782
cursor := int64(6812294872829953)
// l=1586045, t=1, o=1
// cursor = 6812011404988417, checkpoint=24781

cursor := int64(6812011404988417)
ctx := context.Background()
passphrase := "White New England clam chowder"
archive, store := mockArchiveAndIndex(ctx, passphrase)
Expand Down

0 comments on commit 62370cf

Please sign in to comment.