Skip to content

Commit

Permalink
Do not verify nfts
Browse files Browse the repository at this point in the history
  • Loading branch information
0Tech committed Sep 1, 2023
1 parent eee7b93 commit e85305f
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions x/collection/keeper/invariants.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,41 @@ import (
)

const (
totalSupplyInvariant = "total-supply"
totalFTSupplyInvariant = "total-ft-supply"
)

func RegisterInvariants(ir sdk.InvariantRegistry, k Keeper) {
for name, invariant := range map[string]func(k Keeper) sdk.Invariant{
totalSupplyInvariant: TotalSupplyInvariant,
totalFTSupplyInvariant: TotalFTSupplyInvariant,
} {
ir.RegisterRoute(collection.ModuleName, name, invariant(k))
}
}

func TotalSupplyInvariant(k Keeper) sdk.Invariant {
func TotalFTSupplyInvariant(k Keeper) sdk.Invariant {
return func(ctx sdk.Context) (string, bool) {
// cache, we don't want to write changes
ctx, _ = ctx.CacheContext()

invalidClassIDs := map[string][]string{}
invalidFTClassIDs := map[string][]string{}
k.iterateContracts(ctx, func(contract collection.Contract) (stop bool) {
supplies := map[string]sdk.Int{}
k.iterateContractSupplies(ctx, contract.Id, func(classID string, amount sdk.Int) (stop bool) {
if err := collection.ValidateLegacyFTClassID(classID); err != nil {
return false
}

supplies[classID] = amount

return false
})

k.iterateContractBalances(ctx, contract.Id, func(address sdk.AccAddress, balance collection.Coin) (stop bool) {
classID := collection.SplitTokenID(balance.TokenId)
if err := collection.ValidateLegacyFTClassID(classID); err != nil {
return false
}

amount, ok := supplies[classID]
if !ok {
amount = sdk.ZeroInt()
Expand All @@ -43,34 +52,34 @@ func TotalSupplyInvariant(k Keeper) sdk.Invariant {
return false
})

invalidClassIDsCandidate := []string{}
invalidFTClassIDsCandidate := []string{}
for classID, supply := range supplies {
if !supply.IsZero() {
invalidClassIDsCandidate = append(invalidClassIDsCandidate, classID)
invalidFTClassIDsCandidate = append(invalidFTClassIDsCandidate, classID)
}
}

if len(invalidClassIDsCandidate) != 0 {
invalidClassIDs[contract.Id] = invalidClassIDsCandidate
if len(invalidFTClassIDsCandidate) != 0 {
invalidFTClassIDs[contract.Id] = invalidFTClassIDsCandidate
}

return false
})

broken := len(invalidClassIDs) != 0
broken := len(invalidFTClassIDs) != 0
msg := "no violation found"
if broken {
concatenated := []string{}
delimiter := ":"
for contractID, classIDs := range invalidClassIDs {
for contractID, classIDs := range invalidFTClassIDs {
for _, classID := range classIDs {
concatenated = append(concatenated, contractID+delimiter+classID)
}
}

msg = "violation found on following classIDs: " + strings.Join(concatenated, ", ")
msg = "violation found on following ft classIDs: " + strings.Join(concatenated, ", ")
}

return sdk.FormatInvariant(collection.ModuleName, totalSupplyInvariant, msg), broken
return sdk.FormatInvariant(collection.ModuleName, totalFTSupplyInvariant, msg), broken
}
}

0 comments on commit e85305f

Please sign in to comment.