Skip to content

Commit a3342bf

Browse files
committed
add test case
1 parent df7cf1c commit a3342bf

File tree

1 file changed

+245
-0
lines changed

1 file changed

+245
-0
lines changed

ingest/ledger_change_reader_test.go

+245
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,251 @@ func TestLedgerChangeLedgerCloseMetaV2(t *testing.T) {
598598
mock.AssertExpectations(t)
599599
}
600600

601+
func TestLedgerChangeLedgerCloseMetaV2ParallelPhases(t *testing.T) {
602+
ctx := context.Background()
603+
mock := &ledgerbackend.MockDatabaseBackend{}
604+
seq := uint32(123)
605+
606+
src := xdr.MustAddress("GBXGQJWVLWOYHFLVTKWV5FGHA3LNYY2JQKM7OAJAUEQFU6LPCSEFVXON")
607+
firstTx := xdr.TransactionEnvelope{
608+
Type: xdr.EnvelopeTypeEnvelopeTypeTx,
609+
V1: &xdr.TransactionV1Envelope{
610+
Tx: xdr.Transaction{
611+
Fee: 1,
612+
SourceAccount: src.ToMuxedAccount(),
613+
},
614+
},
615+
}
616+
firstTxHash, err := network.HashTransactionInEnvelope(firstTx, network.TestNetworkPassphrase)
617+
assert.NoError(t, err)
618+
619+
src = xdr.MustAddress("GCXKG6RN4ONIEPCMNFB732A436Z5PNDSRLGWK7GBLCMQLIFO4S7EYWVU")
620+
secondTx := xdr.TransactionEnvelope{
621+
Type: xdr.EnvelopeTypeEnvelopeTypeTx,
622+
V1: &xdr.TransactionV1Envelope{
623+
Tx: xdr.Transaction{
624+
Fee: 2,
625+
SourceAccount: src.ToMuxedAccount(),
626+
},
627+
},
628+
}
629+
secondTxHash, err := network.HashTransactionInEnvelope(secondTx, network.TestNetworkPassphrase)
630+
assert.NoError(t, err)
631+
632+
tempKey := xdr.ScSymbol("TEMPKEY")
633+
persistentKey := xdr.ScSymbol("TEMPVAL")
634+
contractIDBytes, err := hex.DecodeString("df06d62447fd25da07c0135eed7557e5a5497ee7d15b7fe345bd47e191d8f577")
635+
assert.NoError(t, err)
636+
var contractID xdr.Hash
637+
copy(contractID[:], contractIDBytes)
638+
contractAddress := xdr.ScAddress{
639+
Type: xdr.ScAddressTypeScAddressTypeContract,
640+
ContractId: &contractID,
641+
}
642+
ledger := xdr.LedgerCloseMeta{
643+
V: 1,
644+
V1: &xdr.LedgerCloseMetaV1{
645+
LedgerHeader: xdr.LedgerHeaderHistoryEntry{Header: xdr.LedgerHeader{LedgerVersion: 10}},
646+
TxSet: xdr.GeneralizedTransactionSet{
647+
V: 1,
648+
V1TxSet: &xdr.TransactionSetV1{
649+
PreviousLedgerHash: xdr.Hash{1, 2, 3},
650+
Phases: []xdr.TransactionPhase{
651+
{
652+
V: 1,
653+
ParallelTxsComponent: &xdr.ParallelTxsComponent{
654+
ExecutionStages: []xdr.ParallelTxExecutionStage{
655+
{
656+
xdr.DependentTxCluster{secondTx},
657+
},
658+
{
659+
xdr.DependentTxCluster{firstTx},
660+
},
661+
},
662+
},
663+
},
664+
},
665+
},
666+
},
667+
TxProcessing: []xdr.TransactionResultMeta{
668+
{
669+
Result: xdr.TransactionResultPair{TransactionHash: firstTxHash},
670+
FeeProcessing: xdr.LedgerEntryChanges{
671+
buildChange(feeAddress, 100),
672+
buildChange(feeAddress, 200),
673+
},
674+
TxApplyProcessing: xdr.TransactionMeta{
675+
V: 3,
676+
V3: &xdr.TransactionMetaV3{
677+
Operations: []xdr.OperationMeta{
678+
{
679+
Changes: xdr.LedgerEntryChanges{
680+
buildChange(
681+
metaAddress,
682+
300,
683+
),
684+
buildChange(
685+
metaAddress,
686+
400,
687+
),
688+
689+
// Add a couple changes simulating a ledger entry extension
690+
{
691+
Type: xdr.LedgerEntryChangeTypeLedgerEntryState,
692+
State: &xdr.LedgerEntry{
693+
LastModifiedLedgerSeq: 1,
694+
Data: xdr.LedgerEntryData{
695+
Type: xdr.LedgerEntryTypeContractData,
696+
ContractData: &xdr.ContractDataEntry{
697+
Contract: contractAddress,
698+
Key: xdr.ScVal{
699+
Type: xdr.ScValTypeScvSymbol,
700+
Sym: &persistentKey,
701+
},
702+
Durability: xdr.ContractDataDurabilityPersistent,
703+
},
704+
},
705+
},
706+
},
707+
{
708+
Type: xdr.LedgerEntryChangeTypeLedgerEntryUpdated,
709+
Updated: &xdr.LedgerEntry{
710+
LastModifiedLedgerSeq: 1,
711+
Data: xdr.LedgerEntryData{
712+
Type: xdr.LedgerEntryTypeContractData,
713+
ContractData: &xdr.ContractDataEntry{
714+
Contract: xdr.ScAddress{
715+
Type: xdr.ScAddressTypeScAddressTypeContract,
716+
ContractId: &contractID,
717+
},
718+
Key: xdr.ScVal{
719+
Type: xdr.ScValTypeScvSymbol,
720+
Sym: &persistentKey,
721+
},
722+
Durability: xdr.ContractDataDurabilityPersistent,
723+
},
724+
},
725+
},
726+
},
727+
},
728+
},
729+
},
730+
},
731+
},
732+
},
733+
{
734+
Result: xdr.TransactionResultPair{TransactionHash: secondTxHash},
735+
FeeProcessing: xdr.LedgerEntryChanges{
736+
buildChange(feeAddress, 300),
737+
},
738+
TxApplyProcessing: xdr.TransactionMeta{
739+
V: 3,
740+
V3: &xdr.TransactionMetaV3{
741+
TxChangesBefore: xdr.LedgerEntryChanges{
742+
buildChange(metaAddress, 600),
743+
},
744+
Operations: []xdr.OperationMeta{
745+
{
746+
Changes: xdr.LedgerEntryChanges{
747+
buildChange(metaAddress, 700),
748+
},
749+
},
750+
},
751+
TxChangesAfter: xdr.LedgerEntryChanges{
752+
buildChange(metaAddress, 800),
753+
buildChange(metaAddress, 900),
754+
},
755+
},
756+
},
757+
},
758+
},
759+
UpgradesProcessing: []xdr.UpgradeEntryMeta{
760+
{
761+
Changes: xdr.LedgerEntryChanges{
762+
buildChange(upgradeAddress, 2),
763+
},
764+
},
765+
{
766+
Changes: xdr.LedgerEntryChanges{
767+
buildChange(upgradeAddress, 3),
768+
},
769+
},
770+
},
771+
EvictedTemporaryLedgerKeys: []xdr.LedgerKey{
772+
{
773+
Type: xdr.LedgerEntryTypeContractData,
774+
ContractData: &xdr.LedgerKeyContractData{
775+
Contract: contractAddress,
776+
Key: xdr.ScVal{
777+
Type: xdr.ScValTypeScvSymbol,
778+
Sym: &tempKey,
779+
},
780+
Durability: xdr.ContractDataDurabilityTemporary,
781+
},
782+
},
783+
},
784+
EvictedPersistentLedgerEntries: []xdr.LedgerEntry{
785+
{
786+
LastModifiedLedgerSeq: 123,
787+
Data: xdr.LedgerEntryData{
788+
Type: xdr.LedgerEntryTypeContractData,
789+
ContractData: &xdr.ContractDataEntry{
790+
Contract: contractAddress,
791+
Key: xdr.ScVal{
792+
Type: xdr.ScValTypeScvSymbol,
793+
Sym: &persistentKey,
794+
},
795+
Durability: xdr.ContractDataDurabilityTemporary,
796+
},
797+
},
798+
},
799+
},
800+
},
801+
}
802+
mock.On("GetLedger", ctx, seq).Return(ledger, nil).Once()
803+
804+
// Check the changes are as expected
805+
assertChangesEqual(t, ctx, seq, mock, []changePredicate{
806+
// First the first txn balance xfers
807+
isBalance(feeAddress, 100),
808+
isBalance(feeAddress, 200),
809+
isBalance(feeAddress, 300),
810+
isBalance(metaAddress, 300),
811+
isBalance(metaAddress, 400),
812+
// Then the first txn data entry extension
813+
isContractDataExtension(
814+
contractAddress,
815+
xdr.ScVal{
816+
Type: xdr.ScValTypeScvSymbol,
817+
Sym: &persistentKey,
818+
},
819+
5904,
820+
),
821+
822+
// Second txn transfers
823+
isBalance(metaAddress, 600),
824+
isBalance(metaAddress, 700),
825+
isBalance(metaAddress, 800),
826+
isBalance(metaAddress, 900),
827+
828+
// Evictions
829+
isContractDataEviction(
830+
contractAddress,
831+
xdr.ScVal{
832+
Type: xdr.ScValTypeScvSymbol,
833+
Sym: &persistentKey,
834+
},
835+
),
836+
837+
// Upgrades last
838+
isBalance(upgradeAddress, 2),
839+
isBalance(upgradeAddress, 3),
840+
})
841+
mock.AssertExpectations(t)
842+
843+
mock.AssertExpectations(t)
844+
}
845+
601846
func TestLedgerChangeLedgerCloseMetaV2Empty(t *testing.T) {
602847
ctx := context.Background()
603848
mock := &ledgerbackend.MockDatabaseBackend{}

0 commit comments

Comments
 (0)