Skip to content

Commit

Permalink
Merge pull request #369 from iotaledger/feat/embedded-api
Browse files Browse the repository at this point in the history
Using the embedded API inside the iota.go objects instead of passing a provider
  • Loading branch information
muXxer authored Sep 27, 2023
2 parents a245826 + 51c2aea commit d4aa80b
Show file tree
Hide file tree
Showing 39 changed files with 78 additions and 115 deletions.
2 changes: 1 addition & 1 deletion components/dashboard/explorer_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func createExplorerBlock(block *model.Block, cachedBlock *blocks.Block, metadata
TransactionID: func() string {
if isBasic && basicBlock.Payload != nil && basicBlock.Payload.PayloadType() == iotago.PayloadTransaction {
tx, _ := basicBlock.Payload.(*iotago.Transaction)
id, _ := tx.ID(lo.PanicOnErr(deps.Protocol.APIForVersion(iotaBlk.ProtocolVersion)))
id, _ := tx.ID()

return id.ToHex()
}
Expand Down
2 changes: 1 addition & 1 deletion components/dashboard/jsonresponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ type Transaction struct {

// NewTransaction returns a Transaction from the given ledgerstate.Transaction.
func NewTransaction(iotaTx *iotago.Transaction) *Transaction {
txID, err := iotaTx.ID(deps.Protocol.CurrentAPI())
txID, err := iotaTx.ID()
if err != nil {
return nil
}
Expand Down
8 changes: 3 additions & 5 deletions components/dashboard/visualizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type tipinfo struct {
// }

func sendVertex(blk *blocks.Block, confirmed bool) {
modelBlk, _ := model.BlockFromBlock(blk.ProtocolBlock(), deps.Protocol.APIForSlot(blk.ID().Index()))
modelBlk, _ := model.BlockFromBlock(blk.ProtocolBlock())
tx, isTx := modelBlk.Transaction()

broadcastWsBlock(&wsblk{MsgTypeVertex, &vertex{
Expand All @@ -54,8 +54,7 @@ func sendVertex(blk *blocks.Block, confirmed bool) {
IsTx: isTx,
IsTxAccepted: func() bool {
if isTx {
api := lo.PanicOnErr(deps.Protocol.APIForVersion(blk.ProtocolBlock().ProtocolVersion))
txMetadata, exists := deps.Protocol.MainEngineInstance().Ledger.MemPool().TransactionMetadata(lo.PanicOnErr(tx.ID(api)))
txMetadata, exists := deps.Protocol.MainEngineInstance().Ledger.MemPool().TransactionMetadata(lo.PanicOnErr(tx.ID()))
if exists {
return txMetadata.IsAccepted()
}
Expand Down Expand Up @@ -88,8 +87,7 @@ func runVisualizer(component *app.Component) {

tx, hasTx := block.Transaction()
if hasTx {
api := lo.PanicOnErr(deps.Protocol.APIForVersion(block.ProtocolBlock().ProtocolVersion))
txMetadata, exists := deps.Protocol.MainEngineInstance().Ledger.MemPool().TransactionMetadata(lo.PanicOnErr(tx.ID(api)))
txMetadata, exists := deps.Protocol.MainEngineInstance().Ledger.MemPool().TransactionMetadata(lo.PanicOnErr(tx.ID()))
if exists {
txMetadata.OnAccepted(func() {
sendTxAccepted(block.ID(), true)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/iotaledger/hive.go/stringify v0.0.0-20230926122307-d671b36a4a65
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20230925153303-c7fbe63a0ab4
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230925152824-4bfa09b8c132
github.com/iotaledger/iota.go/v4 v4.0.0-20230926162636-b083397f465c
github.com/iotaledger/iota.go/v4 v4.0.0-20230926171759-340cfc7c2478
github.com/labstack/echo/v4 v4.11.1
github.com/labstack/gommon v0.4.0
github.com/libp2p/go-libp2p v0.30.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ github.com/iotaledger/inx-app v1.0.0-rc.3.0.20230925153303-c7fbe63a0ab4 h1:pbw/e
github.com/iotaledger/inx-app v1.0.0-rc.3.0.20230925153303-c7fbe63a0ab4/go.mod h1:KbmEuxbhax3fyVrxF4RjBD1/MWLFxHLNDFBnDYUzok4=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230925152824-4bfa09b8c132 h1:YHvgNY3/TRx84UxqizkFe7vVUxAMQB2DOuEL8wjHxpg=
github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230925152824-4bfa09b8c132/go.mod h1:DIFr5lt73HLIyn/Lg2jtzfakwhIT0mMZjMFFji3GXeI=
github.com/iotaledger/iota.go/v4 v4.0.0-20230926162636-b083397f465c h1:VuCRlEpzrQSBf00P6YVW+ykKjVed1kV3bsmiiFrdCyc=
github.com/iotaledger/iota.go/v4 v4.0.0-20230926162636-b083397f465c/go.mod h1:vkwlPaUyG+VjTGaqEMVB7UXB8/dFoPduwSqJ1PZZA6c=
github.com/iotaledger/iota.go/v4 v4.0.0-20230926171759-340cfc7c2478 h1:sAjTMFvLwABCiYSh4JJehpwY5Tz20BgzGtgVbkwpyf4=
github.com/iotaledger/iota.go/v4 v4.0.0-20230926171759-340cfc7c2478/go.mod h1:wR9xBbsofns9hFyRHFZ2bDYIb861qsfmQPVMBKcPvDo=
github.com/ipfs/boxo v0.10.0 h1:tdDAxq8jrsbRkYoF+5Rcqyeb91hgWe2hp7iLu7ORZLY=
github.com/ipfs/boxo v0.10.0/go.mod h1:Fg+BnfxZ0RPzR0nOodzdIq3A7KgoWAOWsEIImrIQdBM=
github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=
Expand Down
8 changes: 4 additions & 4 deletions pkg/blockfactory/blockissuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (i *BlockIssuer) CreateValidationBlock(ctx context.Context, issuerAccount A
}

// Make sure we only create syntactically valid blocks.
modelBlock, err := model.BlockFromBlock(block, api, serix.WithValidation())
modelBlock, err := model.BlockFromBlock(block, serix.WithValidation())
if err != nil {
return nil, ierrors.Wrap(err, "error serializing block to model block")
}
Expand Down Expand Up @@ -210,7 +210,7 @@ func (i *BlockIssuer) CreateBlock(ctx context.Context, issuerAccount Account, op
}

// Make sure we only create syntactically valid blocks.
modelBlock, err := model.BlockFromBlock(block, api, serix.WithValidation())
modelBlock, err := model.BlockFromBlock(block, serix.WithValidation())
if err != nil {
return nil, ierrors.Wrap(err, "error serializing block to model block")
}
Expand Down Expand Up @@ -353,7 +353,7 @@ func (i *BlockIssuer) AttachBlock(ctx context.Context, iotaBlock *iotago.Protoco
issuerAccount := optIssuerAccount[0]
iotaBlock.IssuerID = issuerAccount.ID()

signature, signatureErr := iotaBlock.Sign(apiForVesion, iotago.NewAddressKeysForEd25519Address(issuerAccount.Address().(*iotago.Ed25519Address), issuerAccount.PrivateKey()))
signature, signatureErr := iotaBlock.Sign(iotago.NewAddressKeysForEd25519Address(issuerAccount.Address().(*iotago.Ed25519Address), issuerAccount.PrivateKey()))
if signatureErr != nil {
return iotago.EmptyBlockID(), ierrors.Wrapf(ErrBlockAttacherInvalidBlock, "%w", signatureErr)
}
Expand All @@ -369,7 +369,7 @@ func (i *BlockIssuer) AttachBlock(ctx context.Context, iotaBlock *iotago.Protoco
}
}

modelBlock, err := model.BlockFromBlock(iotaBlock, apiForVesion)
modelBlock, err := model.BlockFromBlock(iotaBlock)
if err != nil {
return iotago.EmptyBlockID(), ierrors.Wrap(err, "error serializing block to model block")
}
Expand Down
35 changes: 11 additions & 24 deletions pkg/model/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"

"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/serializer/v2/serix"
iotago "github.com/iotaledger/iota.go/v4"
)
Expand All @@ -18,9 +17,8 @@ type Block struct {
protocolBlock *iotago.ProtocolBlock
}

func newBlock(blockID iotago.BlockID, iotaBlock *iotago.ProtocolBlock, data []byte, api iotago.API) (*Block, error) {
func newBlock(blockID iotago.BlockID, iotaBlock *iotago.ProtocolBlock, data []byte) (*Block, error) {
block := &Block{
api: api,
blockID: blockID,
data: data,
protocolBlock: iotaBlock,
Expand All @@ -29,18 +27,18 @@ func newBlock(blockID iotago.BlockID, iotaBlock *iotago.ProtocolBlock, data []by
return block, nil
}

func BlockFromBlock(protocolBlock *iotago.ProtocolBlock, api iotago.API, opts ...serix.Option) (*Block, error) {
data, err := api.Encode(protocolBlock, opts...)
func BlockFromBlock(protocolBlock *iotago.ProtocolBlock, opts ...serix.Option) (*Block, error) {
data, err := protocolBlock.API.Encode(protocolBlock, opts...)
if err != nil {
return nil, err
}

blockID, err := protocolBlock.ID(api)
blockID, err := protocolBlock.ID()
if err != nil {
return nil, err
}

return newBlock(blockID, protocolBlock, data, api)
return newBlock(blockID, protocolBlock, data)
}

func BlockFromIDAndBytes(blockID iotago.BlockID, data []byte, api iotago.API, opts ...serix.Option) (*Block, error) {
Expand All @@ -49,31 +47,21 @@ func BlockFromIDAndBytes(blockID iotago.BlockID, data []byte, api iotago.API, op
return nil, err
}

return newBlock(blockID, protocolBlock, data, api)
return newBlock(blockID, protocolBlock, data)
}

func BlockFromBytes(data []byte, apiProvider iotago.APIProvider, opts ...serix.Option) (*Block, error) {
version, _, err := iotago.VersionFromBytes(data)
func BlockFromBytes(data []byte, apiProvider iotago.APIProvider) (*Block, error) {
iotaBlock, _, err := iotago.ProtocolBlockFromBytes(apiProvider)(data)
if err != nil {
return nil, ierrors.Wrap(err, "failed to determine version")
}

apiForVersion, err := apiProvider.APIForVersion(version)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to get API for version %d", version)
}

iotaBlock := new(iotago.ProtocolBlock)
if _, err := apiForVersion.Decode(data, iotaBlock, opts...); err != nil {
return nil, err
}

blockID, err := iotaBlock.ID(apiForVersion)
blockID, err := iotaBlock.ID()
if err != nil {
return nil, err
}

return newBlock(blockID, iotaBlock, data, apiForVersion)
return newBlock(blockID, iotaBlock, data)
}

func (blk *Block) ID() iotago.BlockID {
Expand Down Expand Up @@ -133,7 +121,7 @@ func (blk *Block) String() string {

func (blk *Block) WorkScore() iotago.WorkScore {
if _, isBasic := blk.BasicBlock(); isBasic {
workScore, err := blk.ProtocolBlock().WorkScore(blk.api.ProtocolParameters().WorkScoreStructure())
workScore, err := blk.ProtocolBlock().WorkScore()
if err != nil {
panic(err)
}
Expand All @@ -144,5 +132,4 @@ func (blk *Block) WorkScore() iotago.WorkScore {
// else this is a validator block and should have workScore Zero
// TODO: deal with validator blocks with issue #236
return iotago.WorkScore(0)

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

block, err := model.BlockFromBytes(blockData, p.apiProvider, serix.WithValidation())
block, err := model.BlockFromBytes(blockData, p.apiProvider)
if err != nil {
p.Events.Error.Trigger(ierrors.Wrap(err, "failed to deserialize block"), id)
return
Expand Down
4 changes: 2 additions & 2 deletions pkg/protocol/engine/accounts/accountsledger/testsuite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ func (t *TestSuite) ApplySlotActions(slotIndex iotago.SlotIndex, rmc iotago.Mana
}

func (t *TestSuite) createBlockWithRMC(accountID iotago.AccountID, index iotago.SlotIndex, rmc iotago.Mana) *blocks.Block {
innerBlock := tpkg.RandBasicBlockWithIssuerAndRMC(accountID, rmc)
innerBlock := tpkg.RandBasicBlockWithIssuerAndRMC(tpkg.TestAPI, accountID, rmc)
innerBlock.IssuingTime = tpkg.TestAPI.TimeProvider().SlotStartTime(index)
modelBlock, err := model.BlockFromBlock(innerBlock, tpkg.TestAPI)
modelBlock, err := model.BlockFromBlock(innerBlock)

require.NoError(t.T, err)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ func (t *TestFramework) AddFutureAttestation(issuerAlias string, attestationAlia
Build()
require.NoError(t.test, err)

block.MustID(t.testAPI).RegisterAlias(attestationAlias)
block.MustID().RegisterAlias(attestationAlias)
att := iotago.NewAttestation(t.testAPI, block)
t.attestationsByAlias.Set(attestationAlias, att)

modelBlock, err := model.BlockFromBlock(block, t.testAPI)
modelBlock, err := model.BlockFromBlock(block)
require.NoError(t.test, err)

t.Instance.AddAttestationFromValidationBlock(blocks.NewBlock(modelBlock))
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 @@ -171,7 +171,7 @@ func (b *Booker) inheritConflicts(block *blocks.Block) (conflictIDs ds.Set[iotag
case iotago.ShallowLikeParentType:
// Check whether the parent contains a conflicting TX,
// otherwise reference is invalid and the block should be marked as invalid as well.
if tx, hasTx := parentBlock.Transaction(); !hasTx || !parentBlock.PayloadConflictIDs().Has(lo.PanicOnErr(tx.ID(b.apiProvider.APIForSlot(parentBlock.ID().Index())))) {
if tx, hasTx := parentBlock.Transaction(); !hasTx || !parentBlock.PayloadConflictIDs().Has(lo.PanicOnErr(tx.ID())) {
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 @@ -161,7 +161,7 @@ func (c *CommitmentFilter) evaluateBlock(block *blocks.Block) {

return
}
signingMessage, err := block.ProtocolBlock().SigningMessage(blockAPI)
signingMessage, err := block.ProtocolBlock().SigningMessage()
if err != nil {
c.events.BlockFiltered.Trigger(&commitmentfilter.BlockFilteredEvent{
Block: block,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,7 @@ func (t *TestFramework) AddRMCData(slotIndex iotago.SlotIndex, rmcData iotago.Ma
// q: how to get an engine block.Block from protocol block

func (t *TestFramework) processBlock(alias string, block *iotago.ProtocolBlock) {
apiForVersion, err := t.apiProvider.APIForVersion(block.ProtocolVersion)
require.NoError(t.Test, err)

modelBlock, err := model.BlockFromBlock(block, apiForVersion)
require.NoError(t.Test, err)

modelBlock.ID().RegisterAlias(alias)
t.CommitmentFilter.ProcessPreFilteredBlock(blocks.NewBlock(modelBlock))
}

func (t *TestFramework) processBlockWithAPI(alias string, block *iotago.ProtocolBlock, api iotago.API) {
modelBlock, err := model.BlockFromBlock(block, api)
modelBlock, err := model.BlockFromBlock(block)
require.NoError(t.Test, err)

modelBlock.ID().RegisterAlias(alias)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (t *TestFramework) CreateBlock(alias string, issuerAlias string, parents ..
Build()
require.NoError(t, err)

modelBlock, err := model.BlockFromBlock(block, tpkg.TestAPI)
modelBlock, err := model.BlockFromBlock(block)
require.NoError(t, err)

blocksBlock := blocks.NewBlock(modelBlock)
Expand Down
5 changes: 1 addition & 4 deletions pkg/protocol/engine/filter/blockfilter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ func NewTestFramework(t *testing.T, apiProvider iotago.APIProvider, optsFilter .
}

func (t *TestFramework) processBlock(alias string, block *iotago.ProtocolBlock) error {
apiForVersion, err := t.apiProvider.APIForVersion(block.ProtocolVersion)
require.NoError(t.Test, err)

modelBlock, err := model.BlockFromBlock(block, apiForVersion, serix.WithValidation())
modelBlock, err := model.BlockFromBlock(block, serix.WithValidation())
if err != nil {
return err
}
Expand Down
6 changes: 2 additions & 4 deletions pkg/protocol/engine/ledger/ledger/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,14 @@ func (l *Ledger) executeStardustVM(_ context.Context, stateTransition mempool.Tr
}
resolvedInputs.RewardsInputSet = rewardInputSet

api := l.apiProvider.APIForSlot(tx.Essence.CreationSlot)

vmParams := &iotagovm.Params{
API: api,
API: tx.API,
}
if err = stardust.NewVirtualMachine().Execute(tx, vmParams, resolvedInputs); err != nil {
return nil, err
}

outputSet, err := tx.OutputsSet(api)
outputSet, err := tx.OutputsSet()
if err != nil {
return nil, err
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/protocol/engine/mempool/tests/testframework.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/iotaledger/iota-core/pkg/protocol/engine/mempool"
"github.com/iotaledger/iota-core/pkg/protocol/engine/mempool/conflictdag"
iotago "github.com/iotaledger/iota.go/v4"
"github.com/iotaledger/iota.go/v4/tpkg"
)

type TestFramework struct {
Expand Down Expand Up @@ -60,7 +59,7 @@ func (t *TestFramework) CreateTransaction(alias string, referencedStates []strin
t.transactionByAlias[alias] = transaction

// register the transaction ID alias
transactionID, transactionIDErr := transaction.ID(tpkg.TestAPI)
transactionID, transactionIDErr := transaction.ID()
require.NoError(t.test, transactionIDErr, "failed to retrieve transaction ID of transaction with alias '%s'", alias)
transactionID.RegisterAlias(alias)

Expand Down Expand Up @@ -157,7 +156,7 @@ func (t *TestFramework) TransactionID(alias string) iotago.TransactionID {
transaction, transactionExists := t.transactionByAlias[alias]
require.True(t.test, transactionExists, "transaction with alias '%s' does not exist", alias)

transactionID, transactionIDErr := transaction.ID(tpkg.TestAPI)
transactionID, transactionIDErr := transaction.ID()
require.NoError(t.test, transactionIDErr, "failed to retrieve transaction ID of transaction with alias '%s'", alias)

return transactionID
Expand Down
2 changes: 1 addition & 1 deletion pkg/protocol/engine/mempool/tests/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewTransaction(outputCount uint16, inputs ...*iotago.UTXOInput) *Transactio
}
}

func (t *Transaction) ID(_ iotago.API) (iotago.TransactionID, error) {
func (t *Transaction) ID() (iotago.TransactionID, error) {
return t.id, nil
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/protocol/engine/mempool/tests/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/iotaledger/hive.go/ierrors"
ledgertests "github.com/iotaledger/iota-core/pkg/protocol/engine/ledger/tests"
"github.com/iotaledger/iota-core/pkg/protocol/engine/mempool"
"github.com/iotaledger/iota.go/v4/tpkg"
)

func VM(_ context.Context, inputTransaction mempool.Transaction, _ []mempool.OutputState, _ mempool.ContextState) (outputs []mempool.OutputState, err error) {
Expand All @@ -20,7 +19,7 @@ func VM(_ context.Context, inputTransaction mempool.Transaction, _ []mempool.Out
}

for i := uint16(0); i < transaction.outputCount; i++ {
id, err := transaction.ID(tpkg.TestAPI)
id, err := transaction.ID()
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/protocol/engine/mempool/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

type Transaction interface {
// ID returns the identifier of the Transaction.
ID(iotago.API) (iotago.TransactionID, error)
ID() (iotago.TransactionID, error)

// Inputs returns the inputs of the Transaction.
Inputs() ([]*iotago.UTXOInput, error)
Expand Down
2 changes: 1 addition & 1 deletion pkg/protocol/engine/mempool/v1/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (m *MemPool[VoteRank]) storeTransaction(transaction mempool.Transaction, bl
return nil, false, ierrors.Errorf("blockID %d is older than last evicted slot %d", blockID.Index(), m.lastEvictedSlot)
}

newTransaction, err := NewTransactionWithMetadata(m.apiProvider.APIForSlot(blockID.Index()), transaction)
newTransaction, err := NewTransactionWithMetadata(transaction)
if err != nil {
return nil, false, ierrors.Errorf("failed to create transaction metadata: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/protocol/engine/mempool/v1/transaction_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ type TransactionMetadata struct {
*inclusionFlags
}

func NewTransactionWithMetadata(api iotago.API, transaction mempool.Transaction) (*TransactionMetadata, error) {
transactionID, transactionIDErr := transaction.ID(api)
func NewTransactionWithMetadata(transaction mempool.Transaction) (*TransactionMetadata, error) {
transactionID, transactionIDErr := transaction.ID()
if transactionIDErr != nil {
return nil, ierrors.Errorf("failed to retrieve transaction ID: %w", transactionIDErr)
}
Expand Down
Loading

0 comments on commit d4aa80b

Please sign in to comment.