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

feat: Introduce --namespace-version CLI flag #1585

Merged
merged 17 commits into from
Apr 19, 2023

Conversation

amityadav0
Copy link
Contributor

@amityadav0 amityadav0 commented Mar 31, 2023

Overview

Introduce --namespace-version CLI flag

I have introduced namespace flag. Currently this supports namespace version zero only.
I think data structure for storing namespace versions can be updated. Maybe a map with version details.
For example:

Key: Version[0], Value: { NamespaceVersionPrefixSize: ... , NamespaceVersionIDSize: ... }
Key: Version[1], Value: { NamespaceVersionPrefixSize: ... , NamespaceVersionIDSize: ... }

instead of constants

	NamespaceVersionZero = uint8(0)
	NamespaceVersionZeroPrefixSize = 22
	NamespaceVersionZeroIDSize = NamespaceIDSize - NamespaceVersionZeroPrefixSize

Then it would be possible to switch versions easily from cli. same for share version.

Please let me know if this makes sense or am I missing something.

@MSevey MSevey requested review from a team and cmwaters and removed request for a team March 31, 2023 13:25
@rootulp rootulp changed the title Introduce --namespace-version CLI flag Introduce --namespace-version CLI flag Mar 31, 2023
@rootulp rootulp changed the title Introduce --namespace-version CLI flag feat: Introduce --namespace-version CLI flag Mar 31, 2023
Copy link
Collaborator

@rootulp rootulp left a comment

Choose a reason for hiding this comment

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

Sorry for the delay in reviewing @amityadav0 but thanks for working on this!

x/blob/client/cli/payforblob.go Outdated Show resolved Hide resolved
x/blob/client/cli/payforblob.go Outdated Show resolved Hide resolved
Co-authored-by: Rootul P <rootulp@gmail.com>
@MSevey MSevey requested a review from a team April 5, 2023 08:46
@amityadav0
Copy link
Contributor Author

Thanks for the review @rootulp. I have updated the PR.

@codecov-commenter
Copy link

codecov-commenter commented Apr 5, 2023

Codecov Report

Merging #1585 (8cb9afe) into main (e905143) will decrease coverage by 0.13%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##             main    #1585      +/-   ##
==========================================
- Coverage   49.17%   49.04%   -0.13%     
==========================================
  Files          84       85       +1     
  Lines        4948     4963      +15     
==========================================
+ Hits         2433     2434       +1     
- Misses       2277     2290      +13     
- Partials      238      239       +1     

see 5 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@rootulp
Copy link
Collaborator

rootulp commented Apr 5, 2023

Thanks for updating the PR! This overall LGTM. I had two optional suggestions that I applied on top of this PR. I made the changes myself b/c I forgot to include them in the previous round of PR feedback and I didn't want to block the PR on addressing them.

rootulp
rootulp previously approved these changes Apr 5, 2023
Copy link
Member

@evan-forbes evan-forbes left a comment

Choose a reason for hiding this comment

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

Left a few minor blocking questions/change, thanks @amityadav0 !!

x/blob/client/cli/payforblob.go Outdated Show resolved Hide resolved
x/blob/client/testutil/integration_test.go Show resolved Hide resolved
@@ -57,10 +60,24 @@ func CmdPayForBlob() *cobra.Command {
}

flags.AddTxFlagsToCmd(cmd)
cmd.PersistentFlags().String(FlagNamespaceVersion, "0", "Specify the namespace version (default is 0)")
Copy link
Member

Choose a reason for hiding this comment

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

we shouldn't write defaults, since they're filled in for us by cobra

Suggested change
cmd.PersistentFlags().String(FlagNamespaceVersion, "0", "Specify the namespace version (default is 0)")
cmd.PersistentFlags().String(FlagNamespaceVersion, "0", "Specify the namespace version")

Copy link
Collaborator

Choose a reason for hiding this comment

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

@evan-forbes where are they filled in? After this suggestion the help doc looks like:

$ ./build/celestia-appd tx blob PayForBlobs --help
...
      --namespace-version uint8   Specify the namespace version

Copy link
Member

Choose a reason for hiding this comment

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

hmmm I'm not sure that's a good question. the rest get filled in for example https://github.com/celestiaorg/cosmos-sdk/blob/f19e20459b8bcf195167ad256de5a6ca6edc41f5/server/start.go#L159-L161

--address string                                  Listen address (default "tcp://0.0.0.0:26658")
--home string                                     The application home directory (default "/home/evan/.celestia-app")

Co-authored-by: Evan Forbes <42654277+evan-forbes@users.noreply.github.com>
Copy link
Collaborator

@rootulp rootulp left a comment

Choose a reason for hiding this comment

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

2 small nits based on the last change to uint8 flag

x/blob/client/cli/payforblob.go Outdated Show resolved Hide resolved
x/blob/client/cli/payforblob.go Outdated Show resolved Hide resolved
amityadav0 and others added 2 commits April 10, 2023 21:19
Co-authored-by: Rootul P <rootulp@gmail.com>
Co-authored-by: Rootul P <rootulp@gmail.com>
@MSevey MSevey requested a review from a team April 10, 2023 15:49
rootulp
rootulp previously approved these changes Apr 10, 2023
Copy link
Member

@evan-forbes evan-forbes left a comment

Choose a reason for hiding this comment

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

Missed this first time around! sorry about that @amityadav0 thanks again for your patience

func getNamespace(namespaceID []byte, namespaceVersion uint8) (appns.Namespace, error) {
switch namespaceVersion {
case appns.NamespaceVersionZero:
return appns.New(namespaceVersion, append(appns.NamespaceVersionZeroPrefix, namespaceID...))
Copy link
Member

Choose a reason for hiding this comment

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

we should never append directly to a global, as it could cause tons of bugs

we need to first make an empty slice of appropriate capacity, and then append

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think we are appending to global value. Append function will return a value here which is passed to appns.New()
z = append(x, y)
This line won't change x or y.

Copy link
Member

Choose a reason for hiding this comment

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

the issue is that as a rule of thumb, we should never do
z = append(x, y)
only ever
z = append(z, x, y)

this can, and has, caused quite a few really tricky bugs in the past
https://www.calhoun.io/why-are-slices-sometimes-altered-when-passed-by-value-in-go/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it. Will read this in depth and update the PR. Thanks for the answer.

Copy link
Collaborator

Choose a reason for hiding this comment

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

My takeaway after reading that article is that

z = append(x, y)

is safe because the length or capacity of x will not be impacted by the above statement. After execution the global x remains unmodified. My understanding is that the copy is only necessary if a function is invoked on the global x that modifies elements of the original slice. If append is dangerous in this usage, I would expect there to be a linter to warn us about incorrect usage but I couldn't find one on https://golangci-lint.run/usage/linters/

@evan-forbes how do we enforce that append isn't misused? Additionally are there examples of just the append causing bugs:

we should never append directly to a global, as it could cause tons of bugs

Copy link
Member

Choose a reason for hiding this comment

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

I think we have some linter, but otherwise this is just good practice imo since it always fixes the issue and eliminates the reader having to think "is this an issue". Also, I still think its an issue but don't have the brain capacity at the moment to figure it out lol 😆

examples celestiaorg/celestia-core#336, celestiaorg/celestia-core#250, celestiaorg/nmt#30, there was also one in node that fixed during the barcelona onsite but I couldn't find it in a cursory search

all were caused by
z = append(x, y)

so now we should never ever ever do that no matter what because evan is too scarred

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated the code. Have created a copy of global and then used append on that copy

@MSevey MSevey requested a review from a team April 13, 2023 08:55
Comment on lines 76 to 77
namespaceVersionZeroPrefix := appns.NamespaceVersionZeroPrefix
return appns.New(namespaceVersion, append(namespaceVersionZeroPrefix, namespaceID...))
Copy link
Member

Choose a reason for hiding this comment

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

this doesn't actually avoid the issue, since slices can be treated similar to pointers. Its true that we're passing by value here, but we're passing a pointer by value, which unfortunatley still won't solve our problem

in order to avoid the underlying issue, we need to make an explicit copy of the slice
similar to

ns := make([]byte, 0, appconsts.NamespaceSize)
ns = append(ns, append(prefix, namespaceID)...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess now I get it.
with above example prefix may still be changed

nsp := make([]byte, 0, appns.NamespaceVersionZeroPrefixSize)
nsp = append(nsp, appns.NamespaceVersionZeroPrefix...)
return appns.New(namespaceVersion, append(nsp, namespaceID...))

I have updated the code to this

@MSevey MSevey requested a review from a team April 13, 2023 15:19
evan-forbes
evan-forbes previously approved these changes Apr 17, 2023
Copy link
Member

@evan-forbes evan-forbes left a comment

Choose a reason for hiding this comment

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

made super minor change to push this over the line

thanks again @amityadav0 !! apologies this took so long

@evan-forbes evan-forbes enabled auto-merge (squash) April 17, 2023 12:49
x/blob/client/cli/payforblob.go Outdated Show resolved Hide resolved
func getNamespace(namespaceID []byte, namespaceVersion uint8) (appns.Namespace, error) {
switch namespaceVersion {
case appns.NamespaceVersionZero:
nsp := make([]byte, 0, appns.NamespaceSize)
Copy link
Collaborator

Choose a reason for hiding this comment

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

[nit]

Suggested change
nsp := make([]byte, 0, appns.NamespaceSize)
nsp := make([]byte, 0, appns.NamespaceIDSize)

Copy link
Member

Choose a reason for hiding this comment

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

why not namespace size here? aren't we using the full 33?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The ID is 32 bytes. The version is 1 byte. The total namespace is 33 bytes.

Since this variable is just the ID, only 32 bytes are necessary.

Copy link
Member

Choose a reason for hiding this comment

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

merged to clear out his PR, but we should still fix this imo if need be,

I'm confused a bit

so we should not be appending the version at all here since its added when we call Bytes()? therefore we're adding it twice?

@rootulp rootulp requested review from evan-forbes and removed request for cmwaters April 17, 2023 19:37
@rootulp rootulp added this to the Mainnet milestone Apr 17, 2023
@evan-forbes evan-forbes merged commit d88adf7 into celestiaorg:main Apr 19, 2023
24 checks passed
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.

Introduce --namespace-version CLI flag
4 participants