diff --git a/go.mod b/go.mod index 456085170..4baa4e884 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 5142657c0..c02bf6332 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/model/block.go b/pkg/model/block.go index 5e21b6e94..a859a03a9 100644 --- a/pkg/model/block.go +++ b/pkg/model/block.go @@ -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) { diff --git a/pkg/network/protocols/core/protocol.go b/pkg/network/protocols/core/protocol.go index 458a62015..a5b540161 100644 --- a/pkg/network/protocols/core/protocol.go +++ b/pkg/network/protocols/core/protocol.go @@ -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