Skip to content

Commit

Permalink
Merge pull request #865 from iotaledger/feat/reduce-block-encoding
Browse files Browse the repository at this point in the history
Avoid re-encoding blocks to calculate the ID
  • Loading branch information
muXxer authored Mar 22, 2024
2 parents c202a21 + 261ada9 commit 6adca9b
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 16 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
2 changes: 1 addition & 1 deletion pkg/protocol/engine/booker/inmemorybooker/booker.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func (b *Booker) inheritSpenders(block *blocks.Block) (spenderIDs ds.Set[iotago.

// Check whether the parent contains a conflicting TX,
// otherwise reference is invalid and the block should be marked as invalid as well.
if signedTransaction, hasTx := parentBlock.SignedTransaction(); !hasTx || !parentBlock.PayloadSpenderIDs().Has(signedTransaction.Transaction.MustID()) {
if _, hasTx := parentBlock.SignedTransaction(); !hasTx {
return nil, ierrors.Wrapf(err, "shallow like parent %s does not contain a conflicting transaction", parent.ID.String())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (f *PreSolidBlockFilter) ProcessReceivedBlock(block *model.Block, source pe
}

if _, isValidation := block.ValidationBlock(); isValidation {
blockSlot := block.ProtocolBlock().API.TimeProvider().SlotFromTime(block.ProtocolBlock().Header.IssuingTime)
blockSlot := block.ProtocolBlock().Slot()
committee, exists := f.committeeFunc(blockSlot)
if !exists {
f.events.BlockPreFiltered.Trigger(&presolidfilter.BlockPreFilteredEvent{
Expand Down
2 changes: 1 addition & 1 deletion tools/gendoc/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ require (
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20240307101848-db58eb9353ec // indirect
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20240307100839-48553e1d2022 // indirect
github.com/iotaledger/iota-crypto-demo v0.0.0-20240320124000-d02f37a4fdff // indirect
github.com/iotaledger/iota.go/v4 v4.0.0-20240321174445-4e586367e5bd // indirect
github.com/iotaledger/iota.go/v4 v4.0.0-20240322111205-845f859ca28c // indirect
github.com/ipfs/boxo v0.18.0 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/ipfs/go-datastore v0.6.0 // indirect
Expand Down
1 change: 1 addition & 0 deletions tools/gendoc/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ github.com/iotaledger/iota-crypto-demo v0.0.0-20240320124000-d02f37a4fdff h1:Do8
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/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

0 comments on commit 6adca9b

Please sign in to comment.