Skip to content

Commit

Permalink
- proper error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
StrathCole committed Nov 30, 2023
1 parent 2432c68 commit 8a5f0c6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 26 deletions.
28 changes: 19 additions & 9 deletions x/taxexemption/keeper/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,48 @@ import (
)

func HandleAddTaxExemptionZoneProposal(ctx sdk.Context, k Keeper, p *types.AddTaxExemptionZoneProposal) error {
k.AddTaxExemptionZone(ctx, types.Zone{Name: p.Zone, Outgoing: p.Outgoing, Incoming: p.Incoming, CrossZone: p.CrossZone})
err := k.AddTaxExemptionZone(ctx, types.Zone{Name: p.Zone, Outgoing: p.Outgoing, Incoming: p.Incoming, CrossZone: p.CrossZone})
if err != nil {
return err
}

for _, address := range p.Addresses {
k.AddTaxExemptionAddress(ctx, p.Zone, address)
err := k.AddTaxExemptionAddress(ctx, p.Zone, address)
if err != nil {
return err
}
}

return nil
}

func HandleRemoveTaxExemptionZoneProposal(ctx sdk.Context, k Keeper, p *types.RemoveTaxExemptionZoneProposal) error {
k.RemoveTaxExemptionZone(ctx, p.Zone)

return nil
return k.RemoveTaxExemptionZone(ctx, p.Zone)
}

func HandleModifyTaxExemptionZoneProposal(ctx sdk.Context, k Keeper, p *types.ModifyTaxExemptionZoneProposal) error {
k.ModifyTaxExemptionZone(ctx, types.Zone{Name: p.Zone, Outgoing: p.Outgoing, Incoming: p.Incoming, CrossZone: p.CrossZone})
err := k.ModifyTaxExemptionZone(ctx, types.Zone{Name: p.Zone, Outgoing: p.Outgoing, Incoming: p.Incoming, CrossZone: p.CrossZone})

return nil
return err
}

func HandleAddTaxExemptionAddressProposal(ctx sdk.Context, k Keeper, p *types.AddTaxExemptionAddressProposal) error {
for _, address := range p.Addresses {
k.AddTaxExemptionAddress(ctx, p.Zone, address)
err := k.AddTaxExemptionAddress(ctx, p.Zone, address)
if err != nil {
return err
}
}

return nil
}

func HandleRemoveTaxExemptionAddressProposal(ctx sdk.Context, k Keeper, p *types.RemoveTaxExemptionAddressProposal) error {
for _, address := range p.Addresses {
k.RemoveTaxExemptionAddress(ctx, p.Zone, address)
err := k.RemoveTaxExemptionAddress(ctx, p.Zone, address)
if err != nil {
return err
}
}

return nil
Expand Down
42 changes: 27 additions & 15 deletions x/taxexemption/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}

func (k Keeper) GetTaxExemptionZone(ctx sdk.Context, zoneName string) types.Zone {
func (k Keeper) GetTaxExemptionZone(ctx sdk.Context, zoneName string) (types.Zone, error) {
// Ensure the storeKey is properly set up in the Keeper
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.TaxExemptionZonePrefix)

Expand All @@ -50,7 +50,7 @@ func (k Keeper) GetTaxExemptionZone(ctx sdk.Context, zoneName string) types.Zone

// Check if the zone exists
if !store.Has(key) {
panic(types.ErrNoSuchTaxExemptionZone.Wrapf("zone = %s", zoneName))
return types.Zone{}, types.ErrNoSuchTaxExemptionZone.Wrapf("zone = %s", zoneName)
}

// Get the zone
Expand All @@ -60,11 +60,11 @@ func (k Keeper) GetTaxExemptionZone(ctx sdk.Context, zoneName string) types.Zone
var zone types.Zone
k.cdc.MustUnmarshal(bz, &zone)

return zone
return zone, nil
}

// Tax exemption zone list
func (k Keeper) AddTaxExemptionZone(ctx sdk.Context, zone types.Zone) {
func (k Keeper) AddTaxExemptionZone(ctx sdk.Context, zone types.Zone) error {
// Ensure the storeKey is properly set up in the Keeper
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.TaxExemptionZonePrefix)

Expand All @@ -76,9 +76,11 @@ func (k Keeper) AddTaxExemptionZone(ctx sdk.Context, zone types.Zone) {

// Store the marshaled zone under its name key
store.Set(key, marshaledZone)

return nil
}

func (k Keeper) ModifyTaxExemptionZone(ctx sdk.Context, zone types.Zone) {
func (k Keeper) ModifyTaxExemptionZone(ctx sdk.Context, zone types.Zone) error {
// Ensure the storeKey is properly set up in the Keeper
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.TaxExemptionZonePrefix)

Expand All @@ -87,14 +89,16 @@ func (k Keeper) ModifyTaxExemptionZone(ctx sdk.Context, zone types.Zone) {

// Check if the zone exists
if !store.Has(key) {
panic(types.ErrNoSuchTaxExemptionZone.Wrapf("zone = %s", zone.Name))
return types.ErrNoSuchTaxExemptionZone.Wrapf("zone = %s", zone.Name)
}

// Marshal the zone struct to binary format
marshaledZone := k.cdc.MustMarshal(&zone)

// Store the marshaled zone under its name key
store.Set(key, marshaledZone)

return nil
}

func (k Keeper) RemoveTaxExemptionZone(ctx sdk.Context, zoneName string) error {
Expand Down Expand Up @@ -126,10 +130,10 @@ func (k Keeper) RemoveTaxExemptionZone(ctx sdk.Context, zoneName string) error {
}

// AddTaxExemptionAddress associates an address with a tax exemption zone
func (k Keeper) AddTaxExemptionAddress(ctx sdk.Context, zone string, address string) {
func (k Keeper) AddTaxExemptionAddress(ctx sdk.Context, zone string, address string) error {
// Validate the address format
if _, err := sdk.AccAddressFromBech32(address); err != nil {
panic(err)
return err
}

store := prefix.NewStore(ctx.KVStore(k.storeKey), types.TaxExemptionListPrefix)
Expand All @@ -142,22 +146,24 @@ func (k Keeper) AddTaxExemptionAddress(ctx sdk.Context, zone string, address str

// If the address is already associated with a different zone, raise an error
if existingZone != zone {
panic(fmt.Errorf("address %s is already associated with a different zone: %s", address, existingZone))
return fmt.Errorf("address %s is already associated with a different zone: %s", address, existingZone)
}
// If it's the same zone, no action needed
return
return nil
}

// If the address is not associated with any zone, associate it with the new zone
// Marshal using standard Go marshaling to bytes
store.Set(addressKey, []byte(zone))

return nil
}

// RemoveTaxExemptionAddress removes an address from the tax exemption list
func (k Keeper) RemoveTaxExemptionAddress(ctx sdk.Context, zone string, address string) {
func (k Keeper) RemoveTaxExemptionAddress(ctx sdk.Context, zone string, address string) error {
// Validate the address format
if _, err := sdk.AccAddressFromBech32(address); err != nil {
panic(err)
return err
}

store := prefix.NewStore(ctx.KVStore(k.storeKey), types.TaxExemptionListPrefix)
Expand All @@ -166,15 +172,17 @@ func (k Keeper) RemoveTaxExemptionAddress(ctx sdk.Context, zone string, address
// Check if the address is already associated with a zone
bz := store.Get(addressKey)
if bz == nil {
panic(fmt.Errorf("address %s is not associated with any zone", address))
return fmt.Errorf("address %s is not associated with any zone", address)
}

// If the address is associated with a different zone, raise an error
if string(bz) != zone {
panic(fmt.Errorf("address %s is associated with a different zone: %s", address, string(bz)))
return fmt.Errorf("address %s is associated with a different zone: %s", address, string(bz))
}

store.Delete(addressKey)

return nil
}

// IsExemptedFromTax returns true if the transaction between sender and all recipients
Expand Down Expand Up @@ -228,7 +236,11 @@ func (k Keeper) checkAndCacheZone(ctx sdk.Context, store prefix.Store, address s
return zone, true
}

zone := k.GetTaxExemptionZone(ctx, zoneName)
zone, err := k.GetTaxExemptionZone(ctx, zoneName)
if err != nil {
return types.Zone{}, false
}

zoneCache[zoneName] = zone
return zone, true
}
Expand Down
4 changes: 2 additions & 2 deletions x/taxexemption/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ func TestTaxExemptionList(t *testing.T) {
input := CreateTestInput(t)

require.False(t, input.TaxExemptionKeeper.IsExemptedFromTax(input.Ctx, "", ""))
require.Panics(t, func() { input.TaxExemptionKeeper.AddTaxExemptionAddress(input.Ctx, "", "") })
require.Panics(t, func() { input.TaxExemptionKeeper.RemoveTaxExemptionAddress(input.Ctx, "", "") })
require.Error(t, input.TaxExemptionKeeper.AddTaxExemptionAddress(input.Ctx, "", ""))
require.Error(t, input.TaxExemptionKeeper.RemoveTaxExemptionAddress(input.Ctx, "", ""))

pubKey := secp256k1.GenPrivKey().PubKey()
pubKey2 := secp256k1.GenPrivKey().PubKey()
Expand Down

0 comments on commit 8a5f0c6

Please sign in to comment.