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

Problem: no well-tested implementation of subscriptions module (fix #295) #381

Merged
merged 3 commits into from
Mar 19, 2021

Conversation

yihuang
Copy link
Collaborator

@yihuang yihuang commented Feb 23, 2021

Solution:
- implement basic calendar and crontab logic
- implement the module

👮🏻👮🏻👮🏻 !!!! REFERENCE THE PROBLEM YOUR ARE SOLVING IN THE PR TITLE AND DESCRIBE YOUR SOLUTION HERE !!!! DO NOT FORGET !!!! 👮🏻👮🏻👮🏻

PR Checklist:

  • Have you read the CONTRIBUTING.md?
  • Does your PR follow the C4 patch requirements?
  • Have you rebased your work on top of the latest master?
  • Have you checked your code compiles? (make)
  • Have you included tests for any non-trivial functionality?
  • Have you checked your code passes the unit tests? (make test)
  • Have you checked your code formatting is correct? (go fmt)
  • Have you checked your basic code style is fine? (golangci-lint run)
  • If you added any dependencies, have you checked they do not contain any known vulnerabilities? (go list -json -m all | nancy sleuth)
  • If your changes affect the client infrastructure, have you run the integration test?
  • If your changes affect public APIs, does your PR follow the C4 evolution of public contracts?
  • If your code changes public APIs, have you incremented the crate version numbers and documented your changes in the CHANGELOG.md?
  • If you are contributing for the first time, please read the agreement in CONTRIBUTING.md now and add a comment to this pull request stating that your PR is in accordance with the Developer's Certificate of Origin.

Thank you for your code, it's appreciated! :)


This change is Reviewable

@yihuang
Copy link
Collaborator Author

yihuang commented Feb 23, 2021

TODO:

  • add integration test
  • collect gas fees for create subscription
  • incentive for deleting plan to reclaim storage?
  • analyze overflow issue of arithmetic, consistent sign-ness of integers, constraints on user inputs.
  • cronspec shortcuts
  • Timezone

@codecov-io
Copy link

codecov-io commented Feb 23, 2021

Codecov Report

Merging #381 (e68844c) into master (431f8d8) will increase coverage by 10.84%.
The diff coverage is 26.82%.

Impacted file tree graph

@@             Coverage Diff             @@
##           master     #381       +/-   ##
===========================================
+ Coverage   12.26%   23.10%   +10.84%     
===========================================
  Files          31       55       +24     
  Lines        4779     8742     +3963     
===========================================
+ Hits          586     2020     +1434     
- Misses       3935     6154     +2219     
- Partials      258      568      +310     
Flag Coverage Δ
integration_tests 20.96% <22.06%> (+8.70%) ⬆️
unit_tests 11.38% <11.84%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
app/app.go 3.67% <0.00%> (ø)
app/genesis.go 0.00% <0.00%> (ø)
app/test_helpers.go 0.00% <0.00%> (ø)
x/subscription/types/codec.go 0.00% <ø> (ø)
x/subscription/types/genesis.go 0.00% <0.00%> (ø)
x/subscription/types/msgs.go 12.31% <0.00%> (ø)
x/subscription/types/params.go 0.00% <ø> (ø)
x/supply/types/query.pb.gw.go 1.13% <ø> (+0.51%) ⬆️
x/subscription/types/query.pb.gw.go 0.38% <0.38%> (ø)
x/subscription/types/genesis.pb.go 0.69% <0.69%> (ø)
... and 50 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 431f8d8...e68844c. Read the comment docs.

@lgtm-com
Copy link

lgtm-com bot commented Feb 24, 2021

This pull request introduces 4 alerts when merging b87e296 into 8764b74 - view on LGTM.com

new alerts:

  • 4 for Incorrect conversion between integer types

@yihuang yihuang force-pushed the subscription-impl branch 2 times, most recently from 1e01a29 to e62c7cc Compare February 24, 2021 03:30
@lgtm-com
Copy link

lgtm-com bot commented Feb 24, 2021

This pull request introduces 4 alerts when merging e62c7cc into 8764b74 - view on LGTM.com

new alerts:

  • 4 for Incorrect conversion between integer types

@lgtm-com
Copy link

lgtm-com bot commented Feb 24, 2021

This pull request introduces 4 alerts when merging 2b8692f into 8764b74 - view on LGTM.com

new alerts:

  • 4 for Incorrect conversion between integer types

proto/subscription/v1beta1/subscription.proto Outdated Show resolved Hide resolved
x/subscription/client/cli/tx.go Outdated Show resolved Hide resolved
@lgtm-com
Copy link

lgtm-com bot commented Feb 26, 2021

This pull request introduces 4 alerts when merging 1d075c0 into 91e5405 - view on LGTM.com

new alerts:

  • 4 for Incorrect conversion between integer types

@yihuang yihuang force-pushed the subscription-impl branch 2 times, most recently from 49b3529 to b5dbefc Compare February 26, 2021 02:15
@lgtm-com
Copy link

lgtm-com bot commented Feb 26, 2021

This pull request introduces 4 alerts when merging b5dbefc into 91e5405 - view on LGTM.com

new alerts:

  • 4 for Incorrect conversion between integer types

@lgtm-com
Copy link

lgtm-com bot commented Feb 26, 2021

This pull request introduces 4 alerts when merging 4ae9efd into 91e5405 - view on LGTM.com

new alerts:

  • 4 for Incorrect conversion between integer types

@lgtm-com
Copy link

lgtm-com bot commented Feb 26, 2021

This pull request introduces 4 alerts when merging 0d42e76 into 66b3503 - view on LGTM.com

new alerts:

  • 4 for Incorrect conversion between integer types

@tomtau tomtau requested a review from JayT106 March 1, 2021 03:54
@tomtau
Copy link
Contributor

tomtau commented Mar 1, 2021

there are a few conflicts with the latest master

@yihuang
Copy link
Collaborator Author

yihuang commented Mar 1, 2021

there are a few conflicts with the latest master

done

@yihuang
Copy link
Collaborator Author

yihuang commented Mar 6, 2021

Question for the KVStore in Keeper, I guess it uses the KVStore of the cacheMultiStore, so when the keeper sets the data with keys to the KVStore, when these data will be flushed into the disk?

it's here, the abci commit event happens once for each block, at the end of block processing.

@yihuang
Copy link
Collaborator Author

yihuang commented Mar 8, 2021

could you squash into one commit?

done

@leejw51crypto
Copy link
Contributor

leejw51crypto commented Mar 8, 2021

  1. message MsgCreateSubscriptionResponse {}
    how about returning subscription id? like plan_id when creating plan?

  2. tzoffset as int32?
    Plan, MsgCreatePlan,
    uint32 tzoffset = 7; // in seconds
    how about int32? ,
    because it can be UTC plus or minus ?

Comment on lines 15 to 31
option (google.api.http).get = "/chainmain/subscription/v1beta1/plan/{plan_id}";
}

rpc Plans(QueryPlansRequest) returns (QueryPlansResponse) {
option (google.api.http).get = "/chainmain/subscription/v1beta1/plans";
}

rpc Subscription(QuerySubscriptionRequest) returns (QuerySubscriptionResponse) {
option (google.api.http).get = "/chainmain/subscription/v1beta1/subscription/{subscription_id}";
}

rpc Subscriptions(QuerySubscriptionsRequest) returns (QuerySubscriptionsResponse) {
option (google.api.http).get = "/chainmain/subscription/v1beta1/subscriptions";
}

rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/chainmain/subscription/v1beta1/params";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v1 instead of v1beta1?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

if err != nil {
return err
}
// Get voting address
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

owner address instead of voting address?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


var gasPerCollection uint32
k.paramSpace.Get(ctx, types.KeyGasPerCollection, &gasPerCollection)
ctx.GasMeter().ConsumeGas(sdk.Gas(gasPerCollection), "create subscription")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in spec, it says:

ConsumeGas( count_period(create_time, expiration_time, plan.cron_spec, plan.cron_tz) * GasPerCollection )

but here, it'll just charge a fixed param gasPerCollection?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, forgot that, added now.

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeCreatePlan,
sdk.NewAttribute(types.AttributeKeyPlanID, strconv.FormatInt(int64(plan.PlanId), 10)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens in these event emitting if PlanId/SubscriptionId doesn't fit in int64?

Copy link
Collaborator Author

@yihuang yihuang Mar 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's actually a FormatUint, I just changed to that.
But still, I think when PlanId/SubscriptionId overflow, it'll wrap around, could possibly cause id conflict.
Maybe it's better to just stop creating new items when this happens?

EDIT: added id overflow detection, return error when happend.


qCycles := remDays / daysPer4Y
if qCycles == 25 {
// TODO I think this path is never reached
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe year 2074 or something like that?

Copy link
Collaborator Author

@yihuang yihuang Mar 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this one, it don't reach here too, I've tried lots of cases. 😄

@yihuang
Copy link
Collaborator Author

yihuang commented Mar 8, 2021

  1. message MsgCreateSubscriptionResponse {}
    how about returning subscription id? like plan_id when creating plan?

added

  1. tzoffset as int32?
    Plan, MsgCreatePlan,
    uint32 tzoffset = 7; // in seconds
    how about int32? ,
    because it can be UTC plus or minus ?

good idea, added now.

@JayT106
Copy link
Contributor

JayT106 commented Mar 8, 2021

Question for the KVStore in Keeper, I guess it uses the KVStore of the cacheMultiStore, so when the keeper sets the data with keys to the KVStore, when these data will be flushed into the disk?

it's here, the abci commit event happens once for each block, at the end of block processing.

Thanks!

@leejw51crypto
Copy link
Contributor

leejw51crypto commented Mar 9, 2021

abc.go, func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) {
..
these times are all utc?
could you comment about it?
BlockTime().Unix() unit is seconds?

blockTime := uint64(ctx.BlockTime().Unix())
if blockTime >= subscription.NextCollectionTime {		

@leejw51crypto
Copy link
Contributor

leejw51crypto commented Mar 9, 2021

repeated cosmos.base.v1beta1.Coin

could you modify to cosmos.base.v1.Coin?

Copy link
Contributor

@leejw51crypto leejw51crypto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@yihuang
Copy link
Collaborator Author

yihuang commented Mar 9, 2021

repeated cosmos.base.v1beta1.Coin

could you modify to cosmos.base.v1.Coin?

This namespace is defined in cosmos-sdk, I don't think we can change it ;D

@yihuang
Copy link
Collaborator Author

yihuang commented Mar 9, 2021

abc.go, func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper) {
..
these times are all utc?
could you comment about it?
BlockTime().Unix() unit is seconds?

blockTime := uint64(ctx.BlockTime().Unix())
if blockTime >= subscription.NextCollectionTime {		

Yeah, these are all unix timestamps in seconds.

Copy link
Contributor

@JayT106 JayT106 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@tomtau tomtau self-requested a review March 16, 2021 09:26
Copy link
Contributor

@tomtau tomtau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can probably merge this by Friday -- so may be good to resolve conflicts + changelog entry for unreleased 2.0.0?

@tomtau
Copy link
Contributor

tomtau commented Mar 17, 2021

@yihuang one more test if it can be added -- init a Chain with https://github.com/crypto-org-chain/chain-main/releases/tag/v1.1.0 and then cosmovisor upgrade for v2 with subscriptions? probably some handler to init storage needs to be added?

@yihuang
Copy link
Collaborator Author

yihuang commented Mar 17, 2021

@yihuang one more test if it can be added -- init a Chain with https://github.com/crypto-org-chain/chain-main/releases/tag/v1.1.0 and then cosmovisor upgrade for v2 with subscriptions? probably some handler to init storage needs to be added?

Done, reused the test_cosmovisor test case, migration seems happen smoothly.

CHANGELOG.md Outdated Show resolved Hide resolved
…rypto-org-chain#295)

Solution:
- implement basic calendar and crontab logic
- implement the module

add subscription integrationt test

check SubscriptionEnabled parameter

support timezone

fix event emmision

add README for implementation notes

fix wording in comments and namings

Change org name in proto package name

Update app/app.go

Co-authored-by: Tomas Tauber <2410580+tomtau@users.noreply.github.com>

assert plan owner's balance

add missing tzoffset to spec

Add comments for storage

Use more explicit ParseUint

validate genesis state

Custom bitset implementation

lint issues

improve coverage of calendar/cronspec/bitset

improve coverage of keeper

Add license/copyright of bitset

v1beta -> v1

update sign-ness of types in spec

quick test SecsToTM

fix proto version name and comment

change tzoffset to signed int32

return subscription_id in CreateSubscription api

multiple CountPeriods when consume gas

FormatInt -> FormatUint

handle planID/subscriptionID overflow

test count periods

add v2.0.0 in CHANGELOG

Extend cosmovior integration tests to test v2.0.0 migration
@tomtau tomtau merged commit 22f169d into crypto-org-chain:master Mar 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants