Skip to content

Commit

Permalink
Avoid re-encoding and re-hashing the blocks when received over the ne…
Browse files Browse the repository at this point in the history
…twork
  • Loading branch information
alexsporn committed Mar 22, 2024
1 parent c202a21 commit 28eb213
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ require (
github.com/iotaledger/hive.go/stringify v0.0.0-20240320122938-13a946cf3c7a
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20240307101848-db58eb9353ec
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240307100839-48553e1d2022
github.com/iotaledger/iota.go/v4 v4.0.0-20240321174445-4e586367e5bd
github.com/iotaledger/iota.go/v4 v4.0.0-20240322111205-845f859ca28c
github.com/labstack/echo/v4 v4.11.4
github.com/labstack/gommon v0.4.2
github.com/libp2p/go-libp2p v0.33.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240307100839-48553e1d2022 h1:I178Sa
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240307100839-48553e1d2022/go.mod h1:jTFxIWiMUdAwO263jlJCSWcNLqEkgYEVOFXfjp5aNJM=
github.com/iotaledger/iota-crypto-demo v0.0.0-20240320124000-d02f37a4fdff h1:Do8fakxvFaj7dLckoo/z+mRyBdZo8QvT8HcgnQlG2Sg=
github.com/iotaledger/iota-crypto-demo v0.0.0-20240320124000-d02f37a4fdff/go.mod h1:aVEutEWFnhDNJBxtVuzy2BeTN+8FAlnR83k7hKV0CFE=
github.com/iotaledger/iota.go/v4 v4.0.0-20240321174445-4e586367e5bd h1:GD6XJA52pPknOsMTBNXC/6VB/vtxrKVW2CDdeLRt0eQ=
github.com/iotaledger/iota.go/v4 v4.0.0-20240321174445-4e586367e5bd/go.mod h1:qn/63CB0/jE1em6ewqDSiz+ovS+E/os7K5b7g2pmJFg=
github.com/iotaledger/iota.go/v4 v4.0.0-20240322111205-845f859ca28c h1:xnaAczQXgcm4FL/z6q/r5WqUsyxqsUcs/hEdikQjjQ0=
github.com/iotaledger/iota.go/v4 v4.0.0-20240322111205-845f859ca28c/go.mod h1:qn/63CB0/jE1em6ewqDSiz+ovS+E/os7K5b7g2pmJFg=
github.com/ipfs/boxo v0.18.0 h1:MOL9/AgoV3e7jlVMInicaSdbgralfqSsbkc31dZ9tmw=
github.com/ipfs/boxo v0.18.0/go.mod h1:pIZgTWdm3k3pLF9Uq6MB8JEcW07UDwNJjlXW1HELW80=
github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=
Expand Down
27 changes: 18 additions & 9 deletions pkg/model/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,35 +31,44 @@ func BlockFromBlock(block *iotago.Block, opts ...serix.Option) (*Block, error) {
return nil, err
}

blockID, err := block.ID()
blockIdentifier, err := iotago.BlockIdentifierFromBlockBytes(data)
if err != nil {
return nil, err
}

return newBlock(blockID, block, data)
return newBlock(block.IDWithBlockIdentifier(blockIdentifier), block, data)
}

func BlockFromIDAndBytes(blockID iotago.BlockID, data []byte, api iotago.API, opts ...serix.Option) (*Block, error) {
block := new(iotago.Block)
if _, err := api.Decode(data, block, opts...); err != nil {
// BlockFromIDAndBytes creates a new Block from the given blockID and the serialized block data.
// This is used when loading a block back from storage where we have both the blockID and the bytes available.
func BlockFromIDAndBytes(blockID iotago.BlockID, data []byte, api iotago.API) (*Block, error) {
block, _, err := iotago.BlockFromBytes(iotago.SingleVersionProvider(api))(data)
if err != nil {
return nil, err
}

return newBlock(blockID, block, data)
}

func BlockFromBytes(data []byte, apiProvider iotago.APIProvider) (*Block, error) {
iotaBlock, _, err := iotago.BlockFromBytes(apiProvider)(data)
// BlockFromBlockIdentifierAndBytes creates a new Block from the given blockIdentifier and the serialized block data.
// This is used when receiving blocks from the network where we pre-compute the blockIdentifier for filtering duplicates.
func BlockFromBlockIdentifierAndBytes(blockIdentifier iotago.Identifier, data []byte, apiProvider iotago.APIProvider) (*Block, error) {
block, _, err := iotago.BlockFromBytes(apiProvider)(data)
if err != nil {
return nil, err
}

blockID, err := iotaBlock.ID()
return newBlock(block.IDWithBlockIdentifier(blockIdentifier), block, data)
}

// BlockFromBytes creates a new Block from the serialized block data.
func BlockFromBytes(data []byte, apiProvider iotago.APIProvider) (*Block, error) {
blockIdentifier, err := iotago.BlockIdentifierFromBlockBytes(data)
if err != nil {
return nil, err
}

return newBlock(blockID, iotaBlock, data)
return BlockFromBlockIdentifierAndBytes(blockIdentifier, data, apiProvider)
}

func BlockFromBytesFunc(apiProvider iotago.APIProvider) func(data []byte) (*Block, int, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/network/protocols/core/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (p *Protocol) onBlock(blockData []byte, id peer.ID) {
return
}

block, err := model.BlockFromBytes(blockData, p.apiProvider)
block, err := model.BlockFromBlockIdentifierAndBytes(blockIdentifier, blockData, p.apiProvider)
if err != nil {
p.Events.Error.Trigger(ierrors.Wrap(err, "failed to deserialize block"), id)
return
Expand Down

0 comments on commit 28eb213

Please sign in to comment.