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

Codegen generate serialization functions #433

Open
ranchalp opened this issue May 5, 2023 · 1 comment
Open

Codegen generate serialization functions #433

ranchalp opened this issue May 5, 2023 · 1 comment
Labels
backlog To address eventually, not necessarily now cleanup

Comments

@ranchalp
Copy link
Contributor

ranchalp commented May 5, 2023

If the codegen generated serialization functions, we could remove boilerplate code such as:

func serializePreprepareForHashing(preprepare *pbftpbtypes.Preprepare) *hasherpbtypes.HashData {

	// Encode boolean Aborted field as one byte.
	aborted := byte(0)
	if preprepare.Aborted {
		aborted = 1
	}

	// Put everything together in a slice and return it.
	// Note that we do not include the view number,
	// as the view change protocol might compare hashes of Preprepares across vies.
	return &hasherpbtypes.HashData{Data: [][]byte{preprepare.Sn.Bytes(), {aborted}, preprepare.Data}}
}

func serializeViewChangeForSigning(vc *pbftpbtypes.ViewChange) *cryptopbtypes.SignedData {
	_ = &pbftpb.ViewChange{
		View: 0,
		PSet: nil,
		QSet: nil,
	}

	// Allocate result data structure.
	data := make([][]byte, 0)

	// Encode view number.
	data = append(data, vc.View.Bytes())

	// Encode P set.
	for _, pSetEntry := range vc.PSet {
		data = append(data, pSetEntry.Sn.Bytes())
		data = append(data, pSetEntry.View.Bytes())
		data = append(data, pSetEntry.Digest)
	}

	// Encode Q set.
	for _, qSetEntry := range vc.QSet {
		data = append(data, qSetEntry.Sn.Bytes())
		data = append(data, qSetEntry.View.Bytes())
		data = append(data, qSetEntry.Digest)
	}

	return &cryptopbtypes.SignedData{Data: data}
}
@ranchalp ranchalp added backlog To address eventually, not necessarily now cleanup labels May 5, 2023
@matejpavlovic
Copy link
Contributor

That's worth considering. A simpler approach achieving a similar result would be to simply use deterministic CBOR serialization of the generated types.
Note that in certain cases, we'd still need custom serialization, when, for example, not all fields of a data structure are hashed or signed (e.g., the PBFT view number is, in some cases, excluded when hashing a preprepare message).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backlog To address eventually, not necessarily now cleanup
Projects
None yet
Development

No branches or pull requests

2 participants