-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor getVersionInfo and getNetwork RPC to not use HTTP endpoint /…
…info on core (#198) * Refactor getVersionInfo and getNetwork to not use HTTP endpoint /info * Fix lint errors * Fix lint errors part2 * Change log level * Fix review comments * Add benchmark and tests for getProtocolVersion
- Loading branch information
Showing
9 changed files
with
172 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package methods | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/db" | ||
) | ||
|
||
func getProtocolVersion( | ||
ctx context.Context, | ||
ledgerEntryReader db.LedgerEntryReader, | ||
ledgerReader db.LedgerReader, | ||
) (uint32, error) { | ||
latestLedger, err := ledgerEntryReader.GetLatestLedgerSequence(ctx) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
// obtain bucket size | ||
closeMeta, ok, err := ledgerReader.GetLedger(ctx, latestLedger) | ||
if err != nil { | ||
return 0, err | ||
} | ||
if !ok { | ||
return 0, fmt.Errorf("missing meta for latest ledger (%d)", latestLedger) | ||
} | ||
if closeMeta.V != 1 { | ||
return 0, fmt.Errorf("latest ledger (%d) meta has unexpected verion (%d)", latestLedger, closeMeta.V) | ||
} | ||
return uint32(closeMeta.V1.LedgerHeader.Header.LedgerVersion), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package methods | ||
|
||
import ( | ||
"context" | ||
"path" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/stellar/go/support/log" | ||
"github.com/stellar/go/xdr" | ||
|
||
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/daemon/interfaces" | ||
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/db" | ||
) | ||
|
||
func BenchmarkGetProtocolVersion(b *testing.B) { | ||
dbx := NewTestDB(b) | ||
daemon := interfaces.MakeNoOpDeamon() | ||
|
||
ledgerReader := db.NewLedgerReader(dbx) | ||
_, exists, err := ledgerReader.GetLedger(context.Background(), 1) | ||
require.NoError(b, err) | ||
assert.False(b, exists) | ||
|
||
ledgerSequence := uint32(1) | ||
tx, err := db.NewReadWriter(log.DefaultLogger, dbx, daemon, 150, 15, "passphrase").NewTx(context.Background()) | ||
require.NoError(b, err) | ||
require.NoError(b, tx.LedgerWriter().InsertLedger(createMockLedgerCloseMeta(ledgerSequence))) | ||
require.NoError(b, tx.Commit(ledgerSequence)) | ||
|
||
ledgerEntryReader := db.NewLedgerEntryReader(dbx) | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
_, err := getProtocolVersion(context.TODO(), ledgerEntryReader, ledgerReader) | ||
if err != nil { | ||
b.Fatalf("getProtocolVersion failed: %v", err) | ||
} | ||
} | ||
} | ||
|
||
func TestGetProtocolVersion(t *testing.T) { | ||
dbx := NewTestDB(t) | ||
daemon := interfaces.MakeNoOpDeamon() | ||
|
||
ledgerReader := db.NewLedgerReader(dbx) | ||
_, exists, err := ledgerReader.GetLedger(context.Background(), 1) | ||
require.NoError(t, err) | ||
assert.False(t, exists) | ||
|
||
ledgerSequence := uint32(1) | ||
tx, err := db.NewReadWriter(log.DefaultLogger, dbx, daemon, 150, 15, "passphrase").NewTx(context.Background()) | ||
require.NoError(t, err) | ||
require.NoError(t, tx.LedgerWriter().InsertLedger(createMockLedgerCloseMeta(ledgerSequence))) | ||
require.NoError(t, tx.Commit(ledgerSequence)) | ||
|
||
ledgerEntryReader := db.NewLedgerEntryReader(dbx) | ||
protocolVersion, err := getProtocolVersion(context.TODO(), ledgerEntryReader, ledgerReader) | ||
require.NoError(t, err) | ||
require.Equal(t, uint32(20), protocolVersion) | ||
} | ||
|
||
func createMockLedgerCloseMeta(ledgerSequence uint32) xdr.LedgerCloseMeta { | ||
return xdr.LedgerCloseMeta{ | ||
V: 1, | ||
V1: &xdr.LedgerCloseMetaV1{ | ||
LedgerHeader: xdr.LedgerHeaderHistoryEntry{ | ||
Hash: xdr.Hash{}, | ||
Header: xdr.LedgerHeader{ | ||
LedgerSeq: xdr.Uint32(ledgerSequence), | ||
LedgerVersion: xdr.Uint32(20), | ||
}, | ||
}, | ||
TxSet: xdr.GeneralizedTransactionSet{ | ||
V: 1, | ||
V1TxSet: &xdr.TransactionSetV1{}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func NewTestDB(tb testing.TB) *db.DB { | ||
tmp := tb.TempDir() | ||
dbPath := path.Join(tmp, "db.sqlite") | ||
db, err := db.OpenSQLiteDB(dbPath) | ||
require.NoError(tb, err) | ||
tb.Cleanup(func() { | ||
require.NoError(tb, db.Close()) | ||
}) | ||
return db | ||
} |