Skip to content

Commit

Permalink
Fix: v3 halt condition fix (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMarstonConnell authored Aug 3, 2023
2 parents f537ff0 + 0960b55 commit 7d5969c
Show file tree
Hide file tree
Showing 8 changed files with 287 additions and 66 deletions.
4 changes: 4 additions & 0 deletions proto/canine_chain/storage/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ message GenesisState {
repeated FidCid fid_cid_list = 5 [ (gogoproto.nullable) = false ];
repeated StoragePaymentInfo payment_info_list = 7 [ (gogoproto.nullable) = false ];
repeated Collateral collateral_list = 8 [ (gogoproto.nullable) = false ];
repeated ActiveProviders active_providers_list = 9 [ (gogoproto.nullable) = false ];
repeated ReportForm report_forms = 10 [ (gogoproto.nullable) = false ];
repeated AttestationForm attest_forms = 11 [ (gogoproto.nullable) = false ];


// this line is used by starport scaffolding # genesis/proto/state
}
15 changes: 9 additions & 6 deletions scripts/test-attest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -136,52 +136,55 @@ sleep 60

echo "starting file uploads"

canined tx storage buy-storage jkl10k05lmc88q5ft3lm00q30qkd9x6654h3lejnct 720h 3000000000 ujkl --from charlie -y

sleep 10

curl -v -F sender=jkl10k05lmc88q5ft3lm00q30qkd9x6654h3lejnct -F file=@./scripts/dummy_data/1.png http://localhost:3330/upload

sleep 10

canined tx storage sign-contract jklc107a4hj35fg4jlcapl9h3y7rhw4p24pjtunv8hwwg2hp9dcwatgwsw229ql --from charlie -y --pay-upfront
canined tx storage sign-contract jklc107a4hj35fg4jlcapl9h3y7rhw4p24pjtunv8hwwg2hp9dcwatgwsw229ql --from charlie -y

sleep 30

curl -v -F sender=jkl10k05lmc88q5ft3lm00q30qkd9x6654h3lejnct -F file=@./scripts/dummy_data/3.png http://localhost:3331/upload

sleep 10

canined tx storage sign-contract jklc1mnjchx4g27929y5wfm6luvx86lhca688vqh3vtts6hjllfupgvdqgmr04k --from charlie -y --pay-upfront
canined tx storage sign-contract jklc1mnjchx4g27929y5wfm6luvx86lhca688vqh3vtts6hjllfupgvdqgmr04k --from charlie -y

sleep 30

curl -v -F sender=jkl10k05lmc88q5ft3lm00q30qkd9x6654h3lejnct -F file=@./scripts/dummy_data/4.png http://localhost:3332/upload

sleep 10

canined tx storage sign-contract jklc14y6hk074svd8dyjg5g6c2xzkcfv4ge0w2ey96plr98k4pyepk5xq46wjkn --from charlie -y --pay-upfront
canined tx storage sign-contract jklc14y6hk074svd8dyjg5g6c2xzkcfv4ge0w2ey96plr98k4pyepk5xq46wjkn --from charlie -y

sleep 30

curl -v -F sender=jkl10k05lmc88q5ft3lm00q30qkd9x6654h3lejnct -F file=@./scripts/dummy_data/test.txt http://localhost:3333/upload

sleep 10

canined tx storage sign-contract jklc102jpqmfj5w9pz555zfjd6e9v5nfcnjsy4vh08qhlmrnedrg5jvwqluwrl8 --from charlie -y --pay-upfront
canined tx storage sign-contract jklc102jpqmfj5w9pz555zfjd6e9v5nfcnjsy4vh08qhlmrnedrg5jvwqluwrl8 --from charlie -y

sleep 30

curl -v -F sender=jkl10k05lmc88q5ft3lm00q30qkd9x6654h3lejnct -F file=@./scripts/dummy_data/2.png http://localhost:3334/upload

sleep 10

canined tx storage sign-contract jklc16xdn85h9fel3ruawc3dxjdu7xtkdfp23j66fkkl7dc4277t03rds2rf7g7 --from charlie -y --pay-upfront
canined tx storage sign-contract jklc16xdn85h9fel3ruawc3dxjdu7xtkdfp23j66fkkl7dc4277t03rds2rf7g7 --from charlie -y

sleep 30

curl -v -F sender=jkl10k05lmc88q5ft3lm00q30qkd9x6654h3lejnct -F file=@./scripts/dummy_data/5.svg http://localhost:3335/upload

sleep 10

canined tx storage sign-contract jklc1g80djchxzjxztkff98wrelh86ha0alhwu0j9ce84sqvljy8vpddsg637q5 --from charlie -y --pay-upfront
canined tx storage sign-contract jklc1g80djchxzjxztkff98wrelh86ha0alhwu0j9ce84sqvljy8vpddsg637q5 --from charlie -y

sleep 30

Expand Down
20 changes: 20 additions & 0 deletions x/storage/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
for _, elem := range genState.CollateralList {
k.SetCollateral(ctx, elem)
}

// Set all the attestations
for _, elem := range genState.AttestForms {
k.SetAttestationForm(ctx, elem)
}

// Set all the reports
for _, elem := range genState.ReportForms {
k.SetReportForm(ctx, elem)
}

// Set all the active providers
for _, elem := range genState.ActiveProvidersList {
k.SetActiveProviders(ctx, elem)
}

// this line is used by starport scaffolding # genesis/module/init
k.SetParams(ctx, genState.Params)
}
Expand All @@ -56,6 +72,10 @@ func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
genesis.FidCidList = k.GetAllFidCid(ctx)
genesis.PaymentInfoList = k.GetAllStoragePaymentInfo(ctx)
genesis.CollateralList = k.GetAllCollateral(ctx)
genesis.ActiveProvidersList = k.GetAllActiveProviders(ctx)
genesis.ReportForms = k.GetAllReport(ctx)
genesis.AttestForms = k.GetAllAttestation(ctx)

// this line is used by starport scaffolding # genesis/module/export

return genesis
Expand Down
2 changes: 1 addition & 1 deletion x/storage/keeper/msg_server_attest.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (k Keeper) RequestAttestation(ctx sdk.Context, cid string, creator string)
return nil, sdkerrors.Wrapf(types.ErrInvalidLengthQuery, "not enough providers online")
}

rand.Seed(ctx.BlockTime().UnixNano())
rand.Seed(ctx.BlockHeight())

attestations := make([]*types.Attestation, params.AttestFormSize)

Expand Down
6 changes: 1 addition & 5 deletions x/storage/keeper/msg_server_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package keeper
import (
"context"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/tendermint/tendermint/libs/rand"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/jackalLabs/canine-chain/v3/x/storage/types"
)

Expand Down Expand Up @@ -88,8 +86,6 @@ func (k Keeper) RequestReport(ctx sdk.Context, cid string) ([]string, error) {
return nil, sdkerrors.Wrapf(types.ErrInvalidLengthQuery, "not enough providers online")
}

rand.Seed(ctx.BlockTime().UnixNano())

attestations := make([]*types.Attestation, params.AttestFormSize)

providerAddresses := make([]string, params.AttestFormSize)
Expand Down
8 changes: 5 additions & 3 deletions x/storage/keeper/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ func (k Keeper) GetActiveProviders(ctx sdk.Context, filterAddress string) []type

i64Size := int64(size)

rand.Seed(ctx.BlockHeight())
r := rand.NewRand() // creating a new random generator to ensure no interference

r.Seed(ctx.BlockHeight())

for i := 0; i < rounds; i++ {
x := rand.Int63n(i64Size)
y := rand.Int63n(i64Size)
x := r.Int63n(i64Size)
y := r.Int63n(i64Size)

providers[x], providers[y] = providers[y], providers[x]
}
Expand Down
33 changes: 19 additions & 14 deletions x/storage/keeper/rewards.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,27 +147,41 @@ func (k Keeper) manageDealReward(ctx sdk.Context, deal types.ActiveDeals, networ
}

func (k Keeper) loopDeals(ctx sdk.Context, allDeals []types.ActiveDeals, networkSize sdk.Dec, balance sdk.Coin) {
currentBlock := ctx.BlockHeight()
for _, deal := range allDeals {
end, err := strconv.ParseInt(deal.Endblock, 10, 64)
if err != nil {
ctx.Logger().Error(err.Error())
continue
}
ctx.Logger().Info(fmt.Sprintf("Is %d < %d?", end, ctx.BlockHeight()))
if end < ctx.BlockHeight() {
ctx.Logger().Info(fmt.Sprintf("%d < %d = true", end, ctx.BlockHeight()))

info, found := k.GetStoragePaymentInfo(ctx, deal.Signee)
if !found {
info, found := k.GetStoragePaymentInfo(ctx, deal.Signee)
if !found { // user has no storage plan, we'll check if the deal was made with no plan before removing it

if end == 0 { // the deal was made with a plan yet the user has no plan, remove it
ctx.Logger().Debug(fmt.Sprintf("Removing %s due to no payment info", deal.Cid))
cerr := k.CanContract(ctx, deal.Cid, deal.Signee)
if cerr != nil {
ctx.Logger().Error(cerr.Error())
}
continue
}
}

if end == 0 { // for deals that were made with a subscription, we remove them if there is not enough space in the plan
if info.SpaceUsed > info.SpaceAvailable { // remove file if the user doesn't have enough space
ctx.Logger().Debug(fmt.Sprintf("Removing %s for space used", deal.Cid))
err := k.CanContract(ctx, deal.Cid, deal.Signee)
if err != nil {
ctx.Logger().Error(err.Error())
}
continue
}
}

if currentBlock > end && end > 0 { // check if end block has passed and was made with a timed storage deal
ctx.Logger().Info(fmt.Sprintf("deal has expired at %d", ctx.BlockHeight()))

grace := info.End.Add(time.Hour * 24 * 30)
if grace.Before(ctx.BlockTime()) {
ctx.Logger().Debug(fmt.Sprintf("Removing %s after grace period", deal.Cid))
Expand All @@ -177,15 +191,6 @@ func (k Keeper) loopDeals(ctx sdk.Context, allDeals []types.ActiveDeals, network
}
continue
}

if info.SpaceUsed > info.SpaceAvailable { // remove file if the user doesn't have enough space
ctx.Logger().Debug(fmt.Sprintf("Removing %s for space used", deal.Cid))
err := k.CanContract(ctx, deal.Cid, deal.Signee)
if err != nil {
ctx.Logger().Error(err.Error())
}
continue
}
}

err = k.manageDealReward(ctx, deal, networkSize, balance)
Expand Down
Loading

0 comments on commit 7d5969c

Please sign in to comment.