Repository navigation
Add getTransactions differential test suite #976
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
399046d
add getTransactions view-walk differential suite
cjonas9 5216e33
refactor to-be-shared methods out of differential test
cjonas9 21eb423
fix positional zipping regression hole
cjonas9 254533b
fix unique hash invariant hole in test
cjonas9 f2765ab
split assertSame into self + assertSameError to check error parity
cjonas9 648e65b
replace deleted ledgerReaderTx.GetLedger usage
cjonas9 d5de7a2
fix test suite header comment
cjonas9 b06e6ad
fix corpus gaps on four remaining versions
cjonas9 6618b33
match legacy cursor bytes for client-built cursors + sweep operation …
cjonas9 66feea2
add cursor and request-shape error parity cells
cjonas9 8de90a6
add 3 extra cases to getTx cursor test
cjonas9 1c3fe2a
minor comment fixes in differential test
cjonas9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,362 @@ | ||
| package methods | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "slices" | ||
| "testing" | ||
|
|
||
| "github.com/creachadair/jrpc2" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/stellar/go-stellar-sdk/network" | ||
| protocol "github.com/stellar/go-stellar-sdk/protocols/rpc" | ||
| "github.com/stellar/go-stellar-sdk/support/log" | ||
| "github.com/stellar/go-stellar-sdk/xdr" | ||
|
|
||
| "github.com/stellar/stellar-rpc/cmd/stellar-rpc/internal/host" | ||
| "github.com/stellar/stellar-rpc/cmd/stellar-rpc/internal/rpcv1/sqlitedb" | ||
| ) | ||
|
|
||
| // Machinery shared by the view-migration differential suites: the two-sided | ||
| // comparator, the cursor-chain driver, the corpus builders and sqlite seeding. | ||
| // Each suite freezes its own reference path and assembles its own corpus. | ||
|
|
||
| // diffFormats are the two response encodings every differential sweeps. | ||
| var diffFormats = []string{"", protocol.FormatJSON} | ||
|
|
||
| // rpcMethod is the shape both sides of a differential share. | ||
| type rpcMethod[Req, Resp any] func(context.Context, Req) (Resp, error) | ||
|
|
||
| // differential pairs a frozen reference implementation with the path under | ||
| // test. Both are called with the same request and must agree byte for byte. | ||
| type differential[Req, Resp any] struct { | ||
| want rpcMethod[Req, Resp] // the frozen reference | ||
| got rpcMethod[Req, Resp] // the path under test | ||
| } | ||
|
|
||
| // assertSame runs both sides over req and asserts their responses serialize | ||
| // identically. Both sides must succeed: a reference error means a broken | ||
| // corpus cell, or a case that belongs in assertSameError. | ||
| func (d differential[Req, Resp]) assertSame(t *testing.T, req Req) Resp { | ||
| t.Helper() | ||
| wantResp, wantErr := d.want(context.TODO(), req) | ||
| require.NoError(t, wantErr, "reference path errored: broken corpus cell, or a case for assertSameError") | ||
| gotResp, gotErr := d.got(context.TODO(), req) | ||
| require.NoError(t, gotErr) | ||
| requireSameJSON(t, wantResp, gotResp) | ||
| return gotResp | ||
| } | ||
|
|
||
| // assertSameError runs both sides over a request both are expected to reject | ||
| // and asserts they fail identically: same message and, for a *jrpc2.Error, | ||
| // the same code. Error behavior is wire-visible, so it is a differential | ||
| // axis of its own. | ||
| func (d differential[Req, Resp]) assertSameError(t *testing.T, req Req) { | ||
| t.Helper() | ||
| _, wantErr := d.want(context.TODO(), req) | ||
| require.Error(t, wantErr, "the reference path accepted a request assertSameError expects it to reject") | ||
| _, gotErr := d.got(context.TODO(), req) | ||
| require.Error(t, gotErr) | ||
| require.Equal(t, wantErr.Error(), gotErr.Error()) | ||
| var wantRPC, gotRPC *jrpc2.Error | ||
| if errors.As(wantErr, &wantRPC) { | ||
| require.ErrorAs(t, gotErr, &gotRPC) | ||
| require.Equal(t, wantRPC.Code, gotRPC.Code) | ||
| } | ||
| } | ||
|
|
||
| // assertSameChain pages both sides from first, each following its OWN cursors | ||
| // through next (which returns false on the last page), and returns the page count. | ||
| func (d differential[Req, Resp]) assertSameChain( | ||
| t *testing.T, first Req, next func(Resp) (Req, bool), | ||
| ) int { | ||
| t.Helper() | ||
| wantReq, gotReq := first, first | ||
| pages := 0 | ||
| for { | ||
| wantResp, err := d.want(context.TODO(), wantReq) | ||
| require.NoError(t, err, "reference page %d", pages) | ||
| gotResp, err := d.got(context.TODO(), gotReq) | ||
| require.NoError(t, err, "page %d", pages) | ||
| requireSameJSON(t, wantResp, gotResp, "page %d", pages) | ||
|
|
||
| pages++ | ||
| require.Less(t, pages, 200, "paging did not terminate") | ||
|
|
||
| // The pages are identical, so both sides make the same call here. | ||
| var more bool | ||
| if wantReq, more = next(wantResp); !more { | ||
| return pages | ||
| } | ||
| gotReq, _ = next(gotResp) | ||
| } | ||
| } | ||
|
|
||
| // requireSameJSON asserts want and got serialize to the same bytes. | ||
| func requireSameJSON(t *testing.T, want, got any, msgAndArgs ...any) { | ||
| t.Helper() | ||
| wantJSON, err := json.Marshal(want) | ||
| require.NoError(t, err) | ||
| gotJSON, err := json.Marshal(got) | ||
| require.NoError(t, err) | ||
| // Byte equality, not JSONEq: a field that appears, vanishes or reorders must fail. | ||
| require.Equal(t, string(wantJSON), string(gotJSON), msgAndArgs...) //nolint:testifylint // see above | ||
| } | ||
|
|
||
| // seedDifferentialDB writes corpus, a contiguous ledger run, and its events into a fresh sqlite store. | ||
| func seedDifferentialDB(t *testing.T, corpus []xdr.LedgerCloseMeta) *sqlitedb.DB { | ||
| t.Helper() | ||
| require.NotEmpty(t, corpus) | ||
| testDB := NewTestDB(t) | ||
| rw := sqlitedb.NewReadWriter(log.DefaultLogger, testDB, host.MakeNoOpDaemon(), 1000, passphrase) | ||
| first := corpus[0].LedgerSequence() | ||
| for i, lcm := range corpus { | ||
| require.Equal(t, first+uint32(i), lcm.LedgerSequence(), "the corpus must be one contiguous run") | ||
| tx, err := rw.NewTx(t.Context()) | ||
| require.NoError(t, err) | ||
| require.NoError(t, tx.LedgerWriter().InsertLedger(lcm)) | ||
| require.NoError(t, tx.EventWriter().InsertEvents(lcm)) | ||
| require.NoError(t, tx.Commit(lcm, nil)) | ||
| } | ||
| return testDB | ||
| } | ||
|
|
||
| // | ||
| // ---- corpus builders ---- | ||
| // | ||
|
|
||
| // diffTxSpec is one transaction in a corpus ledger: an envelope shape paired | ||
| // with an apply-processing meta. | ||
| type diffTxSpec struct { | ||
| envelope xdr.TransactionEnvelope | ||
| meta xdr.TransactionMeta | ||
| succeeds bool | ||
| } | ||
|
|
||
| func diffSym(name string) xdr.ScVal { | ||
| sym := xdr.ScSymbol(name) | ||
| return xdr.ScVal{Type: xdr.ScValTypeScvSymbol, Sym: &sym} | ||
| } | ||
|
|
||
| // diffEvent builds a V0-bodied event. A nil id is an event with no contract. | ||
| func diffEvent( | ||
| typ xdr.ContractEventType, id *xdr.ContractId, data xdr.ScVal, topics ...xdr.ScVal, | ||
| ) xdr.ContractEvent { | ||
| return xdr.ContractEvent{ | ||
| ContractId: id, | ||
| Type: typ, | ||
| Body: xdr.ContractEventBody{ | ||
| V: 0, | ||
| V0: &xdr.ContractEventV0{Topics: topics, Data: data}, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // diffContractEvent is the one-topic contract event the transactions corpus | ||
| // sprinkles everywhere an event will do. | ||
| func diffContractEvent() xdr.ContractEvent { | ||
| val := diffSym("COUNTER") | ||
| id := xdr.ContractId{7} | ||
| return diffEvent(xdr.ContractEventTypeContract, &id, val, val) | ||
| } | ||
|
|
||
| func diffTxEvent(stage xdr.TransactionEventStage, event xdr.ContractEvent) xdr.TransactionEvent { | ||
| return xdr.TransactionEvent{Stage: stage, Event: event} | ||
| } | ||
|
|
||
| // diffClassicEnvelope is a plain (non-Soroban) v1 envelope: Tx.Ext stays at | ||
| // discriminant 0, which is what makes it classic. | ||
| func diffClassicEnvelope(acctSeq uint32) xdr.TransactionEnvelope { | ||
| env, err := xdr.NewTransactionEnvelope(xdr.EnvelopeTypeEnvelopeTypeTx, xdr.TransactionV1Envelope{ | ||
| Tx: xdr.Transaction{ | ||
| Fee: 1, | ||
| SeqNum: xdr.SequenceNumber(acctSeq), | ||
| SourceAccount: xdr.MustMuxedAddress("MA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVAAAAAAAAAAAAAJLK"), | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| return env | ||
| } | ||
|
|
||
| // diffV0Envelope is a pre-protocol-13 TX_V0 envelope: an ed25519 source, no Ext. | ||
| func diffV0Envelope(acctSeq uint32) xdr.TransactionEnvelope { | ||
| env, err := xdr.NewTransactionEnvelope(xdr.EnvelopeTypeEnvelopeTypeTxV0, xdr.TransactionV0Envelope{ | ||
| Tx: xdr.TransactionV0{ | ||
| SourceAccountEd25519: xdr.Uint256{1}, | ||
| Fee: 1, | ||
| SeqNum: xdr.SequenceNumber(acctSeq), | ||
| }, | ||
| }) | ||
| if err != nil { | ||
| panic(err) | ||
| } | ||
| return env | ||
| } | ||
|
|
||
| // diffFeeBumpEnvelope wraps inner in a fee bump. | ||
| func diffFeeBumpEnvelope(inner xdr.TransactionEnvelope) xdr.TransactionEnvelope { | ||
| return xdr.TransactionEnvelope{ | ||
| Type: xdr.EnvelopeTypeEnvelopeTypeTxFeeBump, | ||
| FeeBump: &xdr.FeeBumpTransactionEnvelope{ | ||
| Tx: xdr.FeeBumpTransaction{ | ||
| FeeSource: xdr.MustMuxedAddress("MA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVAAAAAAAAAAAAAJLK"), | ||
| Fee: 200, | ||
| InnerTx: xdr.FeeBumpTransactionInnerTx{ | ||
| Type: xdr.EnvelopeTypeEnvelopeTypeTx, | ||
| V1: inner.V1, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // diffMetaV0 is the early-pubnet meta the legacy reader rejects and the view serves. | ||
| func diffMetaV0() xdr.TransactionMeta { | ||
| return xdr.TransactionMeta{V: 0, Operations: &[]xdr.OperationMeta{}} | ||
| } | ||
|
|
||
| func diffMetaV1() xdr.TransactionMeta { | ||
| return xdr.TransactionMeta{V: 1, V1: &xdr.TransactionMetaV1{Operations: []xdr.OperationMeta{}}} | ||
| } | ||
|
|
||
| // diffMetaV2 is the protocol 13 to 19 meta: no events anywhere. | ||
| func diffMetaV2() xdr.TransactionMeta { | ||
| return xdr.TransactionMeta{V: 2, V2: &xdr.TransactionMetaV2{Operations: []xdr.OperationMeta{}}} | ||
| } | ||
|
|
||
| // diffMetaV3NoSoroban is the straggler corner: a V3 meta with no SorobanMeta | ||
| // at all. Paired with a Soroban envelope it is the one shape where the SDK's | ||
| // view extractor historically disagreed with the parsed reader on | ||
| // operation-slice arity, so the transactions corpus pins it deliberately. | ||
| func diffMetaV3NoSoroban() xdr.TransactionMeta { | ||
| return xdr.TransactionMeta{V: 3, Operations: &[]xdr.OperationMeta{}, V3: &xdr.TransactionMetaV3{}} | ||
| } | ||
|
|
||
| func diffMetaV3WithEvents(events []xdr.ContractEvent, diags []xdr.DiagnosticEvent) xdr.TransactionMeta { | ||
| return xdr.TransactionMeta{ | ||
| V: 3, | ||
| Operations: &[]xdr.OperationMeta{}, | ||
| V3: &xdr.TransactionMetaV3{SorobanMeta: &xdr.SorobanTransactionMeta{ | ||
| Events: events, | ||
| DiagnosticEvents: diags, | ||
| ReturnValue: diffSym("COUNTER"), | ||
| }}, | ||
| } | ||
| } | ||
|
|
||
| func diffMetaV4(ops []xdr.OperationMetaV2, txEvents []xdr.TransactionEvent, | ||
| diags []xdr.DiagnosticEvent, | ||
| ) xdr.TransactionMeta { | ||
| return xdr.TransactionMeta{V: 4, V4: &xdr.TransactionMetaV4{ | ||
| Operations: ops, | ||
| Events: txEvents, | ||
| DiagnosticEvents: diags, | ||
| }} | ||
| } | ||
|
|
||
| // diffResultFor builds the TransactionResultPair for spec: a fee bump carries | ||
| // an inner result pair, a plain transaction does not. | ||
| func diffResultFor(t *testing.T, spec diffTxSpec) xdr.TransactionResultPair { | ||
| t.Helper() | ||
| hash, err := network.HashTransactionInEnvelope(spec.envelope, NetworkPassphrase) | ||
| require.NoError(t, err) | ||
|
|
||
| code := xdr.TransactionResultCodeTxSuccess | ||
| if !spec.succeeds { | ||
| code = xdr.TransactionResultCodeTxBadSeq | ||
| } | ||
| opResults := []xdr.OperationResult{} | ||
| res := xdr.TransactionResultResult{Code: code, Results: &opResults} | ||
|
|
||
| if spec.envelope.Type == xdr.EnvelopeTypeEnvelopeTypeTxFeeBump { | ||
| innerHash, ierr := network.HashTransactionInEnvelope(xdr.TransactionEnvelope{ | ||
| Type: xdr.EnvelopeTypeEnvelopeTypeTx, V1: spec.envelope.FeeBump.Tx.InnerTx.V1, | ||
| }, NetworkPassphrase) | ||
| require.NoError(t, ierr) | ||
| outer := xdr.TransactionResultCodeTxFeeBumpInnerSuccess | ||
| if !spec.succeeds { | ||
| outer = xdr.TransactionResultCodeTxFeeBumpInnerFailed | ||
| } | ||
| res = xdr.TransactionResultResult{ | ||
| Code: outer, | ||
| InnerResultPair: &xdr.InnerTransactionResultPair{ | ||
| TransactionHash: innerHash, | ||
| Result: xdr.InnerTransactionResult{ | ||
| FeeCharged: 100, | ||
| Result: xdr.InnerTransactionResultResult{Code: code, Results: &opResults}, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| return xdr.TransactionResultPair{ | ||
| TransactionHash: hash, | ||
| Result: xdr.TransactionResult{FeeCharged: 100, Result: res}, | ||
| } | ||
| } | ||
|
|
||
| // diffLCM assembles a LedgerCloseMeta of wire version 0, 1 or 2 at sequence | ||
| // seq holding specs in apply order; no specs is an empty ledger. Every version | ||
| // matters: V0 carries a plain TransactionSet, and V1 and V2 differ in their | ||
| // TxProcessing element type, so the view dispatcher walks each with different code. | ||
| func diffLCM(t *testing.T, version int32, seq uint32, specs ...diffTxSpec) xdr.LedgerCloseMeta { | ||
| t.Helper() | ||
| envs := diffTxSetEnvelopes(specs) | ||
| header := xdr.LedgerHeaderHistoryEntry{Header: xdr.LedgerHeader{ | ||
| ScpValue: xdr.StellarValue{CloseTime: xdr.TimePoint(ledgerCloseTime(seq))}, | ||
| LedgerSeq: xdr.Uint32(seq), | ||
| }} | ||
| txSet := xdr.GeneralizedTransactionSet{V: 1, V1TxSet: &xdr.TransactionSetV1{ | ||
| PreviousLedgerHash: xdr.Hash{1}, | ||
| Phases: []xdr.TransactionPhase{diffClassicPhase(envs)}, | ||
| }} | ||
|
|
||
| if version == 2 { | ||
| proc := make([]xdr.TransactionResultMetaV1, 0, len(specs)) | ||
| for _, spec := range specs { | ||
| proc = append(proc, xdr.TransactionResultMetaV1{Result: diffResultFor(t, spec), TxApplyProcessing: spec.meta}) | ||
| } | ||
| return xdr.LedgerCloseMeta{V: 2, V2: &xdr.LedgerCloseMetaV2{ | ||
| LedgerHeader: header, TxSet: txSet, TxProcessing: proc, | ||
| }} | ||
| } | ||
| // V0 and V1 share the TxProcessing element type. | ||
| proc := make([]xdr.TransactionResultMeta, 0, len(specs)) | ||
| for _, spec := range specs { | ||
| proc = append(proc, xdr.TransactionResultMeta{Result: diffResultFor(t, spec), TxApplyProcessing: spec.meta}) | ||
| } | ||
| if version == 0 { | ||
| return xdr.LedgerCloseMeta{V: 0, V0: &xdr.LedgerCloseMetaV0{ | ||
| LedgerHeader: header, | ||
| TxSet: xdr.TransactionSet{PreviousLedgerHash: xdr.Hash{1}, Txs: envs}, | ||
| TxProcessing: proc, | ||
| }} | ||
| } | ||
| return xdr.LedgerCloseMeta{V: 1, V1: &xdr.LedgerCloseMetaV1{ | ||
| LedgerHeader: header, TxSet: txSet, TxProcessing: proc, | ||
| }} | ||
| } | ||
|
|
||
| // diffTxSetEnvelopes is specs' envelopes in TxSet order: reversed, since real | ||
| // ledgers do not keep apply order there, so a positional/zip pairing | ||
| // regression fails on every multi-transaction ledger. | ||
| func diffTxSetEnvelopes(specs []diffTxSpec) []xdr.TransactionEnvelope { | ||
| envs := make([]xdr.TransactionEnvelope, 0, len(specs)) | ||
| for _, spec := range slices.Backward(specs) { | ||
| envs = append(envs, spec.envelope) | ||
| } | ||
| return envs | ||
| } | ||
|
|
||
| // diffClassicPhase is a V0 phase of one fee group holding envs. | ||
| func diffClassicPhase(envs []xdr.TransactionEnvelope) xdr.TransactionPhase { | ||
| components := []xdr.TxSetComponent{{ | ||
| Type: xdr.TxSetComponentTypeTxsetCompTxsMaybeDiscountedFee, | ||
| TxsMaybeDiscountedFee: &xdr.TxSetComponentTxsMaybeDiscountedFee{Txs: envs}, | ||
| }} | ||
| return xdr.TransactionPhase{V: 0, V0Components: &components} | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.