Skip to content
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

horizon: Test consistency and coverage of effect typenames (and cleanup unused effects) #3562

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 33 additions & 32 deletions protocols/horizon/effects/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const (
// account flags, either clearing or setting.
EffectAccountFlagsUpdated EffectType = 6 // from set_options

// unused
// EffectAccountInflationDestinationUpdated effects occur when an account changes its
// inflation destination.
EffectAccountInflationDestinationUpdated EffectType = 7 // from set_options
Expand Down Expand Up @@ -92,14 +93,13 @@ const (

// trading effects

// unused
// EffectOfferCreated occurs when an account offers to trade an asset
EffectOfferCreated EffectType = 30 // from manage_offer, creat_passive_offer

// EffectOfferCreated EffectType = 30 // from manage_offer, creat_passive_offer
// EffectOfferRemoved occurs when an account removes an offer
EffectOfferRemoved EffectType = 31 // from manage_offer, creat_passive_offer, path_payment

// EffectOfferRemoved EffectType = 31 // from manage_offer, creat_passive_offer, path_payment
// EffectOfferUpdated occurs when an offer is updated by the offering account.
EffectOfferUpdated EffectType = 32 // from manage_offer, creat_passive_offer, path_payment
// EffectOfferUpdated EffectType = 32 // from manage_offer, creat_passive_offer, path_payment

// EffectTrade occurs when a trade is initiated because of a path payment or
// offer operation.
Expand Down Expand Up @@ -208,33 +208,34 @@ var EffectTypeNames = map[EffectType]string{
EffectTrustlineAuthorizedToMaintainLiabilities: "trustline_authorized_to_maintain_liabilities",
EffectTrustlineDeauthorized: "trustline_deauthorized",
EffectTrustlineFlagsUpdated: "trustline_flags_updated",
EffectOfferCreated: "offer_created",
EffectOfferRemoved: "offer_removed",
EffectOfferUpdated: "offer_updated",
EffectTrade: "trade",
EffectDataCreated: "data_created",
EffectDataRemoved: "data_removed",
EffectDataUpdated: "data_updated",
EffectSequenceBumped: "sequence_bumped",
EffectClaimableBalanceCreated: "claimable_balance_created",
EffectClaimableBalanceClaimed: "claimable_balance_claimed",
EffectClaimableBalanceClaimantCreated: "claimable_balance_claimant_created",
EffectAccountSponsorshipCreated: "account_sponsorship_created",
EffectAccountSponsorshipUpdated: "account_sponsorship_updated",
EffectAccountSponsorshipRemoved: "account_sponsorship_removed",
EffectTrustlineSponsorshipCreated: "trustline_sponsorship_created",
EffectTrustlineSponsorshipUpdated: "trustline_sponsorship_updated",
EffectTrustlineSponsorshipRemoved: "trustline_sponsorship_removed",
EffectDataSponsorshipCreated: "data_sponsorship_created",
EffectDataSponsorshipUpdated: "data_sponsorship_updated",
EffectDataSponsorshipRemoved: "data_sponsorship_removed",
EffectClaimableBalanceSponsorshipCreated: "claimable_balance_sponsorship_created",
EffectClaimableBalanceSponsorshipUpdated: "claimable_balance_sponsorship_updated",
EffectClaimableBalanceSponsorshipRemoved: "claimable_balance_sponsorship_removed",
EffectSignerSponsorshipCreated: "signer_sponsorship_created",
EffectSignerSponsorshipUpdated: "signer_sponsorship_updated",
EffectSignerSponsorshipRemoved: "signer_sponsorship_removed",
EffectClaimableBalanceClawedBack: "claimable_balance_clawed_back",
// unused
// EffectOfferCreated: "offer_created",
// EffectOfferRemoved: "offer_removed",
// EffectOfferUpdated: "offer_updated",
EffectTrade: "trade",
EffectDataCreated: "data_created",
EffectDataRemoved: "data_removed",
EffectDataUpdated: "data_updated",
EffectSequenceBumped: "sequence_bumped",
EffectClaimableBalanceCreated: "claimable_balance_created",
EffectClaimableBalanceClaimed: "claimable_balance_claimed",
EffectClaimableBalanceClaimantCreated: "claimable_balance_claimant_created",
EffectAccountSponsorshipCreated: "account_sponsorship_created",
EffectAccountSponsorshipUpdated: "account_sponsorship_updated",
EffectAccountSponsorshipRemoved: "account_sponsorship_removed",
EffectTrustlineSponsorshipCreated: "trustline_sponsorship_created",
EffectTrustlineSponsorshipUpdated: "trustline_sponsorship_updated",
EffectTrustlineSponsorshipRemoved: "trustline_sponsorship_removed",
EffectDataSponsorshipCreated: "data_sponsorship_created",
EffectDataSponsorshipUpdated: "data_sponsorship_updated",
EffectDataSponsorshipRemoved: "data_sponsorship_removed",
EffectClaimableBalanceSponsorshipCreated: "claimable_balance_sponsorship_created",
EffectClaimableBalanceSponsorshipUpdated: "claimable_balance_sponsorship_updated",
EffectClaimableBalanceSponsorshipRemoved: "claimable_balance_sponsorship_removed",
EffectSignerSponsorshipCreated: "signer_sponsorship_created",
EffectSignerSponsorshipUpdated: "signer_sponsorship_updated",
EffectSignerSponsorshipRemoved: "signer_sponsorship_removed",
EffectClaimableBalanceClawedBack: "claimable_balance_clawed_back",
}

// Base provides the common structure for any effect resource effect.
Expand Down
9 changes: 4 additions & 5 deletions services/horizon/internal/db2/history/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,13 @@ const (

// trading effects

// unused
// EffectOfferCreated occurs when an account offers to trade an asset
EffectOfferCreated EffectType = 30 // from manage_offer, creat_passive_offer

// EffectOfferCreated EffectType = 30 // from manage_offer, creat_passive_offer
// EffectOfferRemoved occurs when an account removes an offer
EffectOfferRemoved EffectType = 31 // from manage_offer, create_passive_offer, path_payment

// EffectOfferRemoved EffectType = 31 // from manage_offer, create_passive_offer, path_payment
// EffectOfferUpdated occurs when an offer is updated by the offering account.
EffectOfferUpdated EffectType = 32 // from manage_offer, creat_passive_offer, path_payment
// EffectOfferUpdated EffectType = 32 // from manage_offer, creat_passive_offer, path_payment

// EffectTrade occurs when a trade is initiated because of a path payment or
// offer operation.
Expand Down
58 changes: 31 additions & 27 deletions services/horizon/internal/resourceadapter/effects.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,34 @@ var EffectTypeNames = map[history.EffectType]string{
history.EffectTrustlineAuthorizedToMaintainLiabilities: "trustline_authorized_to_maintain_liabilities",
history.EffectTrustlineDeauthorized: "trustline_deauthorized",
history.EffectTrustlineFlagsUpdated: "trustline_flags_updated",
history.EffectOfferCreated: "offer_created",
history.EffectOfferRemoved: "offer_removed",
history.EffectOfferUpdated: "offer_updated",
history.EffectTrade: "trade",
history.EffectDataCreated: "data_created",
history.EffectDataRemoved: "data_removed",
history.EffectDataUpdated: "data_updated",
history.EffectSequenceBumped: "sequence_bumped",
history.EffectClaimableBalanceCreated: "claimable_balance_created",
history.EffectClaimableBalanceClaimantCreated: "claimable_balance_claimant_created",
history.EffectClaimableBalanceClaimed: "claimable_balance_claimed",
history.EffectAccountSponsorshipCreated: "account_sponsorship_created",
history.EffectAccountSponsorshipUpdated: "account_sponsorship_updated",
history.EffectAccountSponsorshipRemoved: "account_sponsorship_removed",
history.EffectTrustlineSponsorshipCreated: "trustline_sponsorship_created",
history.EffectTrustlineSponsorshipUpdated: "trustline_sponsorship_updated",
history.EffectTrustlineSponsorshipRemoved: "trustline_sponsorship_removed",
history.EffectDataSponsorshipCreated: "data_sponsorship_created",
history.EffectDataSponsorshipUpdated: "data_sponsorship_updated",
history.EffectDataSponsorshipRemoved: "data_sponsorship_removed",
history.EffectClaimableBalanceSponsorshipCreated: "claimable_balance_sponsorship_created",
history.EffectClaimableBalanceSponsorshipUpdated: "claimable_balance_sponsorship_updated",
history.EffectClaimableBalanceSponsorshipRemoved: "claimable_balance_sponsorship_removed",
history.EffectSignerSponsorshipCreated: "signer_sponsorship_created",
history.EffectSignerSponsorshipUpdated: "signer_sponsorship_updated",
history.EffectSignerSponsorshipRemoved: "signer_sponsorship_removed",
history.EffectClaimableBalanceClawedBack: "claimable_balance_clawed_back",
// unused
// history.EffectOfferCreated: "offer_created",
// history.EffectOfferRemoved: "offer_removed",
// history.EffectOfferUpdated: "offer_updated",
history.EffectTrade: "trade",
history.EffectDataCreated: "data_created",
history.EffectDataRemoved: "data_removed",
history.EffectDataUpdated: "data_updated",
history.EffectSequenceBumped: "sequence_bumped",
history.EffectClaimableBalanceCreated: "claimable_balance_created",
history.EffectClaimableBalanceClaimantCreated: "claimable_balance_claimant_created",
history.EffectClaimableBalanceClaimed: "claimable_balance_claimed",
history.EffectAccountSponsorshipCreated: "account_sponsorship_created",
history.EffectAccountSponsorshipUpdated: "account_sponsorship_updated",
history.EffectAccountSponsorshipRemoved: "account_sponsorship_removed",
history.EffectTrustlineSponsorshipCreated: "trustline_sponsorship_created",
history.EffectTrustlineSponsorshipUpdated: "trustline_sponsorship_updated",
history.EffectTrustlineSponsorshipRemoved: "trustline_sponsorship_removed",
history.EffectDataSponsorshipCreated: "data_sponsorship_created",
history.EffectDataSponsorshipUpdated: "data_sponsorship_updated",
history.EffectDataSponsorshipRemoved: "data_sponsorship_removed",
history.EffectClaimableBalanceSponsorshipCreated: "claimable_balance_sponsorship_created",
history.EffectClaimableBalanceSponsorshipUpdated: "claimable_balance_sponsorship_updated",
history.EffectClaimableBalanceSponsorshipRemoved: "claimable_balance_sponsorship_removed",
history.EffectSignerSponsorshipCreated: "signer_sponsorship_created",
history.EffectSignerSponsorshipUpdated: "signer_sponsorship_updated",
history.EffectSignerSponsorshipRemoved: "signer_sponsorship_removed",
history.EffectClaimableBalanceClawedBack: "claimable_balance_clawed_back",
}

// NewEffect creates a new effect resource from the provided database representation
Expand Down Expand Up @@ -247,6 +248,9 @@ func NewEffect(
e := effects.ClaimableBalanceClawedBack{Base: basev}
err = row.UnmarshalDetails(&e)
result = e
case history.EffectAccountRemoved:
// there is no explicit data structure for account removed
fallthrough
default:
result = basev
}
Expand Down
40 changes: 40 additions & 0 deletions services/horizon/internal/resourceadapter/effects_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resourceadapter

import (
"context"
"encoding/json"
"testing"

Expand All @@ -12,6 +13,45 @@ import (
"github.com/stretchr/testify/assert"
)

func TestNewEffectAllEffectsCovered(t *testing.T) {
for typ, s := range EffectTypeNames {
if typ == history.EffectAccountRemoved || typ == history.EffectAccountInflationDestinationUpdated {
// these effects use the base representation
continue
}
e := history.Effect{
Type: typ,
}
result, err := NewEffect(context.TODO(), e, history.Ledger{})
assert.NoError(t, err, s)
// it shouldn't be a base type
_, ok := result.(effects.Base)
assert.False(t, ok, s)
}

// verify that the check works for an unknown effect
e := history.Effect{
Type: 20000,
}
result, err := NewEffect(context.TODO(), e, history.Ledger{})
assert.NoError(t, err)
_, ok := result.(effects.Base)
assert.True(t, ok)
}

func TestEffectTypeNamesAreConsistentWithAdapterTypeNames(t *testing.T) {
for typ, s := range EffectTypeNames {
s2, ok := effects.EffectTypeNames[effects.EffectType(typ)]
assert.True(t, ok, s)
assert.Equal(t, s, s2)
}
for typ, s := range effects.EffectTypeNames {
s2, ok := EffectTypeNames[history.EffectType(typ)]
assert.True(t, ok, s)
assert.Equal(t, s, s2)
}
}

func TestNewEffect_EffectTrustlineAuthorizedToMaintainLiabilities(t *testing.T) {
tt := assert.New(t)
ctx, _ := test.ContextWithLogBuffer()
Expand Down