EmerisHQ / tracelistener

UNIX named pipes-based real-time state listener for Cosmos SDK blockchains
GNU Affero General Public License v3.0
33 stars 8 forks source link

Properly split len prefixed delegation key #54

Closed DeshErBojhaa closed 2 years ago

DeshErBojhaa commented 2 years ago

Closes the double value showing after re delegation: https://github.com/allinbits/demeris-backend/issues/366

This PR does not deal with https://app.zenhub.com/workspaces/emeris-back-end-619cd009cc778000112d6259/issues/allinbits/demeris-backend/235. But the solution is very closely related. When we fix 235, we will use the prefix that we extract from the delegation key. Right now we just ignore the prefix.

Pitasi commented 2 years ago

PSA: rambling ahead, more of a reflection for the future rather than review

Since length prefix is a generic operation, I wonder if we could refactor this in a separate func that can be used in the future as well in other parsing situations.

Something like:

func LengthPrefixedData(prefixLen int, data []byte) ([]byte, error)

In this instance, LengthPrefixedData would be called with prefixLen == 1, to mark the first byte in data as the length.

I actually thought the same since the same logic was applied twice in the same function 😛 I didn't want to overthink this but in my head it would look like this (can be extended and improved):

type LengthPrefixedKey []byte

func (k LengthPrefixedKey) Length() int {
  return k[0]
}

func (k LengthPrefixedKey) Key() []byte {
  return k[1:k.Length()+1]
}

func ExampleUsage() {
  b := []byte{ 2, 1, 2 }
  k := LengthPrefixedKey(b).Key()
  fmt.Println(k)
}
gsora commented 2 years ago

Cool!

I like this a lot, if we could squeeze in variable-len in a endianess-safe way it would be great.

DeshErBojhaa commented 2 years ago

Hi, @Pitasi, @gsora loving this conversation, let's move it to 235