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

Use a single cosmos codec #1480

Merged
merged 20 commits into from
Nov 8, 2019
Merged

Use a single cosmos codec #1480

merged 20 commits into from
Nov 8, 2019

Conversation

antho1404
Copy link
Member

dependency: #1462

By using the auth.NewAnteHandler we let cosmos manage all the sequence numbers (so #1462 will work), but also tokens and fees for transactions (it seems but I didn't look at it a lot yet).

By doing I realized that the ABCI messages should be pure data (as they are unmarshalled by the post validation step) so they cannot contain the codec as it was initially designed.

There is now a specific codec for each SDK that registers all the types needed for this SDK.

I also removed all the mempool.ErrTxInCache as this shouldn't happen anymore and if it happens that's an issue we need to fix and not hide with an "xxx already exists".

@antho1404 antho1404 added bug Something isn't working enhancement New feature or request labels Nov 6, 2019
@antho1404 antho1404 added this to the next milestone Nov 6, 2019
@antho1404 antho1404 changed the title Use cosmos module to manage accounts WIP: Use cosmos module to manage accounts Nov 6, 2019
@krhubert
Copy link
Contributor

krhubert commented Nov 6, 2019

I also removed all the mempool.ErrTxInCache as this shouldn't happen anymore and if it happens that's an issue we need to fix and not hide with an "xxx already exists".

Just wonder if two clients ask for the account sequence number at the same time, they might get the same seq number. The sequence number is only increased in one place (https://github.com/cosmos/cosmos-sdk/blob/master/x/auth/ante/sigverify.go#L252) - on sig validation. So my question is are you sure the two clients get new sequence when they retrive account at the same time.

sdk/instance/codec.go Outdated Show resolved Hide resolved
// ModuleCdc is the codec for the module
var ModuleCdc = codec.New()

func init() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we replace all init function with explicit call?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's following the guidelines of cosmos. Also, we need to make sure that the codec is initialized even if the backend is not yet created. We could have some sync of the messages before we initialize the backend.

We could find a way to remove the init and have a singleton pattern but that will be complex for no real advantage.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's following the guidelines of cosmos

Yes, i think cosmos is not the right place to get best pracitices.

Also, we need to make sure that the codec is initialized even if the backend

That's easy, we can call InitCodes inside main function.

Liek for me init should be avoided at all cost, because it changes the workflow of execution. There are only few places where it should be used (like goto, or brake/continue outer loops)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, i think cosmos is not the right place to get best pracitices

Maybe not but protobuf or gogoproto might be ;)
Anyway we could have a function that the is called on every package and make sure that this is called at the beginning of main but at the end that's similar to init.

@antho1404 antho1404 force-pushed the fix/codec-ante-handler branch from da7dfec to 58cf150 Compare November 7, 2019 05:20
@antho1404 antho1404 force-pushed the fix/codec-ante-handler branch 2 times, most recently from a88566d to 05279de Compare November 7, 2019 09:16
@antho1404 antho1404 changed the title WIP: Use cosmos module to manage accounts Use cosmos module to manage accounts Nov 7, 2019
@antho1404 antho1404 changed the title Use cosmos module to manage accounts Use cosmos module to manage accounts and use single codec Nov 7, 2019
This was referenced Nov 7, 2019
@antho1404 antho1404 self-assigned this Nov 7, 2019
Comment on lines +8 to +41
// RegisterConcrete https://godoc.org/github.com/tendermint/go-amino#Codec.RegisterConcrete
func RegisterConcrete(o interface{}, name string, copts *amino.ConcreteOptions) {
Codec.RegisterConcrete(o, name, copts)
}

// RegisterInterface https://godoc.org/github.com/tendermint/go-amino#Codec.RegisterInterface
func RegisterInterface(ptr interface{}, iopts *amino.InterfaceOptions) {
Codec.RegisterInterface(ptr, iopts)
}

// MustMarshalJSON https://godoc.org/github.com/tendermint/go-amino#Codec.MustMarshalJSON
func MustMarshalJSON(o interface{}) []byte {
return Codec.MustMarshalJSON(o)
}

// MarshalJSON https://godoc.org/github.com/tendermint/go-amino#Codec.MarshalJSON
func MarshalJSON(o interface{}) ([]byte, error) {
return Codec.MarshalJSON(o)
}

// UnmarshalJSON https://godoc.org/github.com/tendermint/go-amino#Codec.UnmarshalJSON
func UnmarshalJSON(bz []byte, ptr interface{}) error {
return Codec.UnmarshalJSON(bz, ptr)
}

// UnmarshalBinaryBare https://godoc.org/github.com/tendermint/go-amino#Codec.UnmarshalBinaryBare
func UnmarshalBinaryBare(bz []byte, ptr interface{}) error {
return Codec.UnmarshalBinaryBare(bz, ptr)
}

// MarshalBinaryBare https://godoc.org/github.com/tendermint/go-amino#Codec.MarshalBinaryBare
func MarshalBinaryBare(o interface{}) ([]byte, error) {
return Codec.MarshalBinaryBare(o)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need those functions? What are the purpose of those?

Why caller just can't use

codec.Codec.XXX

Copy link
Contributor

Choose a reason for hiding this comment

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

What would be good to see for example is shortcut for

cosmostypes.MustSortJSON(codec.MustMarshalJSON(msg))

But it's an option

Copy link
Member Author

Choose a reason for hiding this comment

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

we can use codec.Codec.XXX I just find it more convenient to have the functions directly like an alternative codec (that in fact uses another codec).

Like that, if we want we can put this Codec private or directly implement the un/marshal directly in the functions etc...

Copy link
Contributor

Choose a reason for hiding this comment

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

Still, I'm not convinced to that. I don't see pros of "hiding" that codec method.

Like that, if we want we can put this Codec private

So let's put the codec as private in the first place ;) But then you'll probably find that is not convenient to use :P

antho1404 and others added 4 commits November 7, 2019 18:24
# Conflicts:
#	database/instance_db.go
#	database/instance_db_test.go
#	database/ownership_db.go
#	database/ownership_db_test.go
#	database/service_db.go
#	database/service_db_test.go
#	sdk/execution/execution_test.go
#	sdk/instance/backend.go
#	sdk/ownership/backend.go
#	sdk/service/backend.go
@NicolasMahe NicolasMahe force-pushed the fix/codec-ante-handler branch from 4197ddb to 68aba4d Compare November 8, 2019 05:31
@NicolasMahe NicolasMahe merged commit 938526f into dev Nov 8, 2019
@NicolasMahe NicolasMahe deleted the fix/codec-ante-handler branch November 8, 2019 08:04
@NicolasMahe NicolasMahe mentioned this pull request Nov 18, 2019
@NicolasMahe NicolasMahe changed the title Use cosmos module to manage accounts and use single codec Use a single cosmos codec Nov 26, 2019
@NicolasMahe NicolasMahe added the release:fix Pull requests that fix something label Nov 26, 2019
@NicolasMahe NicolasMahe mentioned this pull request Nov 26, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request release:fix Pull requests that fix something
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants