-
Notifications
You must be signed in to change notification settings - Fork 15
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
Encode each attestation by version when sending over the wire #351
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package core | ||
|
||
import ( | ||
"encoding/binary" | ||
"encoding/json" | ||
|
||
"github.com/libp2p/go-libp2p/core/peer" | ||
|
@@ -14,6 +15,7 @@ import ( | |
"github.com/iotaledger/hive.go/runtime/options" | ||
"github.com/iotaledger/hive.go/runtime/syncutils" | ||
"github.com/iotaledger/hive.go/runtime/workerpool" | ||
"github.com/iotaledger/hive.go/serializer/v2/marshalutil" | ||
"github.com/iotaledger/hive.go/serializer/v2/serix" | ||
"github.com/iotaledger/iota-core/pkg/model" | ||
"github.com/iotaledger/iota-core/pkg/network" | ||
|
@@ -73,16 +75,16 @@ func (p *Protocol) SendSlotCommitment(cm *model.Commitment, to ...peer.ID) { | |
} | ||
|
||
func (p *Protocol) SendAttestations(cm *model.Commitment, attestations []*iotago.Attestation, merkleProof *merklehasher.Proof[iotago.Identifier], to ...peer.ID) { | ||
var iotagoAPI iotago.API | ||
if len(attestations) > 0 { | ||
// TODO: there are multiple attestations potentially spanning multiple epochs/versions, we need to use the correct API for each one | ||
iotagoAPI = lo.PanicOnErr(p.apiProvider.APIForVersion(attestations[0].ProtocolVersion)) | ||
} else { | ||
iotagoAPI = p.apiProvider.APIForSlot(cm.Index()) // we need an api to serialize empty slices as well | ||
encodedAttestations := marshalutil.New() | ||
encodedAttestations.WriteUint32(uint32(len(attestations))) | ||
for _, att := range attestations { | ||
iotagoAPI := lo.PanicOnErr(p.apiProvider.APIForVersion(att.ProtocolVersion)) | ||
encodedAttestations.WriteBytes(lo.PanicOnErr(iotagoAPI.Encode(att))) | ||
} | ||
|
||
p.network.Send(&nwmodels.Packet{Body: &nwmodels.Packet_Attestations{Attestations: &nwmodels.Attestations{ | ||
Commitment: cm.Data(), | ||
Attestations: lo.PanicOnErr(iotagoAPI.Encode(attestations)), | ||
Attestations: encodedAttestations.Bytes(), | ||
MerkleProof: lo.PanicOnErr(json.Marshal(merkleProof)), | ||
}}}, to...) | ||
} | ||
|
@@ -200,10 +202,37 @@ func (p *Protocol) onAttestations(commitmentBytes []byte, attestationsBytes []by | |
return | ||
} | ||
|
||
var attestations []*iotago.Attestation | ||
// TODO: there could be multiple versions of attestations in the same packet | ||
if _, err := lo.PanicOnErr(p.apiProvider.APIForVersion(iotago.Version(commitmentBytes[0]))).Decode(attestationsBytes, &attestations, serix.WithValidation()); err != nil { | ||
p.Events.Error.Trigger(ierrors.Wrap(err, "failed to deserialize attestations"), id) | ||
if len(attestationsBytes) < 4 { | ||
p.Events.Error.Trigger(ierrors.Errorf("failed to deserialize attestations, invalid attestation count"), id) | ||
|
||
return | ||
} | ||
|
||
attestationCount := binary.LittleEndian.Uint32(attestationsBytes[0:4]) | ||
Comment on lines
+205
to
+211
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checking for the constant, using |
||
readOffset := 4 | ||
attestations := make([]*iotago.Attestation, attestationCount) | ||
for i := uint32(0); i < attestationCount; i++ { | ||
version := iotago.Version(attestationsBytes[readOffset]) | ||
apiForVersion, err := p.apiProvider.APIForVersion(version) | ||
if err != nil { | ||
p.Events.Error.Trigger(ierrors.Wrapf(err, "failed to deserialize attestations for commitment %s", cm.ID()), id) | ||
|
||
return | ||
} | ||
|
||
attestation := new(iotago.Attestation) | ||
consumed, err := apiForVersion.Decode(attestationsBytes[readOffset:], attestation, serix.WithValidation()) | ||
if err != nil { | ||
p.Events.Error.Trigger(ierrors.Wrap(err, "failed to deserialize attestations"), id) | ||
|
||
return | ||
} | ||
|
||
readOffset += consumed | ||
attestations[i] = attestation | ||
} | ||
if readOffset != len(attestationsBytes) { | ||
p.Events.Error.Trigger(ierrors.Errorf("failed to deserialize attestations: %d bytes remaining", len(attestationsBytes)-readOffset), id) | ||
|
||
return | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of marshalutil we could use
stream.WriteCollection
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a bytes buffer does not implement WriteSeeker, so could not use it here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok then let's implement something like this https://stackoverflow.com/a/73679110 in the context of #284 so that we're able to use a bytes buffer with the stream package and replace this later