From 2d94af33246df54428537149ac4e90ee64a9b392 Mon Sep 17 00:00:00 2001 From: Piotr Macek <4007944+piotrm50@users.noreply.github.com> Date: Tue, 3 Oct 2023 17:00:55 +0200 Subject: [PATCH 1/6] Added a new inx method to validate if a transaction can be executed by the VM successfully --- components/inx/server_issuance.go | 71 +++++++++++++++++++++++ components/inx/server_tips.go | 18 ------ go.mod | 6 +- go.sum | 12 ++-- pkg/protocol/engine/ledger/ledger/vm.go | 6 +- pkg/protocol/engine/mempool/mempool.go | 2 + pkg/protocol/engine/mempool/v1/mempool.go | 4 ++ tools/evil-spammer/go.mod | 2 +- tools/evil-spammer/go.sum | 4 +- tools/gendoc/go.mod | 6 +- tools/gendoc/go.sum | 12 ++-- tools/genesis-snapshot/go.mod | 2 +- tools/genesis-snapshot/go.sum | 4 +- 13 files changed, 104 insertions(+), 45 deletions(-) create mode 100644 components/inx/server_issuance.go delete mode 100644 components/inx/server_tips.go diff --git a/components/inx/server_issuance.go b/components/inx/server_issuance.go new file mode 100644 index 000000000..ac17fea1b --- /dev/null +++ b/components/inx/server_issuance.go @@ -0,0 +1,71 @@ +package inx + +import ( + "context" + + "github.com/iotaledger/hive.go/serializer/v2/serix" + inx "github.com/iotaledger/inx/go" + "github.com/iotaledger/iota-core/pkg/protocol/engine/mempool" + iotago "github.com/iotaledger/iota.go/v4" +) + +func (s *Server) RequestTips(_ context.Context, req *inx.TipsRequest) (*inx.TipsResponse, error) { + references := deps.Protocol.MainEngineInstance().TipSelection.SelectTips(int(req.GetCount())) + + return &inx.TipsResponse{ + StrongTips: inx.NewBlockIds(references[iotago.StrongParentType]), + WeakTips: inx.NewBlockIds(references[iotago.WeakParentType]), + ShallowLikeTips: inx.NewBlockIds(references[iotago.ShallowLikeParentType]), + }, nil +} + +func (s *Server) ValidatePayload(ctx context.Context, payload *inx.RawPayload) (*inx.PayloadValidationResponse, error) { + blockPayload, unwrapErr := payload.Unwrap(deps.Protocol.CurrentAPI(), serix.WithValidation()) + if unwrapErr != nil { + //nolint:nilerr // this is expected behavior + return &inx.PayloadValidationResponse{IsValid: false, Error: unwrapErr.Error()}, nil + } + + switch payload := blockPayload.(type) { + case *iotago.SignedTransaction: + memPool := deps.Protocol.MainEngineInstance().Ledger.MemPool() + inputReferences, inputsErr := memPool.VM().Inputs(payload.Transaction) + if inputsErr != nil { + //nolint:nilerr // this is expected behavior + return &inx.PayloadValidationResponse{IsValid: false, Error: inputsErr.Error()}, nil + } + + loadedInputs := make([]mempool.State, 0) + + for _, inputReference := range inputReferences { + metadata, metadataErr := memPool.StateMetadata(inputReference) + if metadataErr != nil { + //nolint:nilerr // this is expected behavior + return &inx.PayloadValidationResponse{IsValid: false, Error: metadataErr.Error()}, nil + } + + loadedInputs = append(loadedInputs, metadata.State()) + } + + executionContext, validationErr := memPool.VM().ValidateSignatures(payload, loadedInputs) + if validationErr != nil { + //nolint:nilerr // this is expected behavior + return &inx.PayloadValidationResponse{IsValid: false, Error: validationErr.Error()}, nil + } + + _, executionErr := memPool.VM().Execute(executionContext, payload.Transaction) + if executionErr != nil { + //nolint:nilerr // this is expected behavior + return &inx.PayloadValidationResponse{IsValid: false, Error: executionErr.Error()}, nil + } + + return &inx.PayloadValidationResponse{IsValid: true}, nil + + case *iotago.TaggedData: + // TaggedData is always valid if serix decoding was successful + return &inx.PayloadValidationResponse{IsValid: true}, nil + + default: + return &inx.PayloadValidationResponse{IsValid: false, Error: "given payload type unknown"}, nil + } +} diff --git a/components/inx/server_tips.go b/components/inx/server_tips.go deleted file mode 100644 index ee107263f..000000000 --- a/components/inx/server_tips.go +++ /dev/null @@ -1,18 +0,0 @@ -package inx - -import ( - "context" - - inx "github.com/iotaledger/inx/go" - iotago "github.com/iotaledger/iota.go/v4" -) - -func (s *Server) RequestTips(_ context.Context, req *inx.TipsRequest) (*inx.TipsResponse, error) { - references := deps.Protocol.MainEngineInstance().TipSelection.SelectTips(int(req.GetCount())) - - return &inx.TipsResponse{ - StrongTips: inx.NewBlockIds(references[iotago.StrongParentType]), - WeakTips: inx.NewBlockIds(references[iotago.WeakParentType]), - ShallowLikeTips: inx.NewBlockIds(references[iotago.ShallowLikeParentType]), - }, nil -} diff --git a/go.mod b/go.mod index 265e877aa..ad29cdd90 100644 --- a/go.mod +++ b/go.mod @@ -24,9 +24,9 @@ require ( github.com/iotaledger/hive.go/runtime v0.0.0-20230929122509-67f34bfed40d github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20230929122509-67f34bfed40d github.com/iotaledger/hive.go/stringify v0.0.0-20230929122509-67f34bfed40d - github.com/iotaledger/inx-app v1.0.0-rc.3.0.20230927140518-622f63be6182 - github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230927140257-bfa0bb0af2bd - github.com/iotaledger/iota.go/v4 v4.0.0-20231003101444-5687809cd68a + github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231001095511-32be422a567e + github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231001095356-923e8f138951 + github.com/iotaledger/iota.go/v4 v4.0.0-20231003122130-2871888a9297 github.com/labstack/echo/v4 v4.11.1 github.com/labstack/gommon v0.4.0 github.com/libp2p/go-libp2p v0.30.0 diff --git a/go.sum b/go.sum index 1d30c914e..9052996eb 100644 --- a/go.sum +++ b/go.sum @@ -301,12 +301,12 @@ github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20230929122509-67f34bf github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20230929122509-67f34bfed40d/go.mod h1:IJgaaxbgKCsNat18jlJJEAxCY2oVYR3F30B+M4vJ89I= github.com/iotaledger/hive.go/stringify v0.0.0-20230929122509-67f34bfed40d h1:ekHWRypoaiCXgrJVUQS7rCewsK3FuG1gTbPxu5jYn9c= github.com/iotaledger/hive.go/stringify v0.0.0-20230929122509-67f34bfed40d/go.mod h1:FTo/UWzNYgnQ082GI9QVM9HFDERqf9rw9RivNpqrnTs= -github.com/iotaledger/inx-app v1.0.0-rc.3.0.20230927140518-622f63be6182 h1:lQiktl3Q0B+cHbVum7WzJikOEP+buw686oSrw5Unyz8= -github.com/iotaledger/inx-app v1.0.0-rc.3.0.20230927140518-622f63be6182/go.mod h1:q24QEsS887ZWJVX76w2kwSgC84KS7wIKOy1otuqZ2ZM= -github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230927140257-bfa0bb0af2bd h1:nFG3Zq/zFA4KhBYFX2IezX1C74zfE0DqCt0LrgTa9Ig= -github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230927140257-bfa0bb0af2bd/go.mod h1:c5778OnWpLq108YE+Eb2m8Ri/t/4ydV0TvI/Sy5YivQ= -github.com/iotaledger/iota.go/v4 v4.0.0-20231003101444-5687809cd68a h1:xgh1YQvLN+Y3KwX1G9/znGbCaQsfpDtpSLn8nKvaP8s= -github.com/iotaledger/iota.go/v4 v4.0.0-20231003101444-5687809cd68a/go.mod h1:+e3bsJFDr9HxmUMe+eQOLNut5wfcB/ivhJdouOJgOnE= +github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231001095511-32be422a567e h1:Mwoe7M6gI2DAjJIXmIskgnI8KdxCY1LyEEhtJCNYBsU= +github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231001095511-32be422a567e/go.mod h1:jhzexR5X8m6qcmrwt5OX477O/ZwT7Ak9sPT83ByPkAo= +github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231001095356-923e8f138951 h1:qUf1W0fE1IyZzVy3Exv0Kj+SKECXG3S26c9m2ETb07U= +github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231001095356-923e8f138951/go.mod h1:c5778OnWpLq108YE+Eb2m8Ri/t/4ydV0TvI/Sy5YivQ= +github.com/iotaledger/iota.go/v4 v4.0.0-20231003122130-2871888a9297 h1:kbpsa/gF0x6Cxu5lN9TL8u5EdaGv1P3/OKKPW0w83Og= +github.com/iotaledger/iota.go/v4 v4.0.0-20231003122130-2871888a9297/go.mod h1:+e3bsJFDr9HxmUMe+eQOLNut5wfcB/ivhJdouOJgOnE= 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= diff --git a/pkg/protocol/engine/ledger/ledger/vm.go b/pkg/protocol/engine/ledger/ledger/vm.go index 34fd045c1..43a45cf54 100644 --- a/pkg/protocol/engine/ledger/ledger/vm.go +++ b/pkg/protocol/engine/ledger/ledger/vm.go @@ -8,7 +8,7 @@ import ( "github.com/iotaledger/iota-core/pkg/protocol/engine/utxoledger" iotago "github.com/iotaledger/iota.go/v4" iotagovm "github.com/iotaledger/iota.go/v4/vm" - "github.com/iotaledger/iota.go/v4/vm/stardust" + "github.com/iotaledger/iota.go/v4/vm/nova" ) type VM struct { @@ -131,7 +131,7 @@ func (v *VM) ValidateSignatures(signedTransaction mempool.SignedTransaction, res RewardsInputSet: rewardInputSet, } - unlockedIdentities, err := stardust.NewVirtualMachine().ValidateUnlocks(signedStardustTransaction, resolvedInputs) + unlockedIdentities, err := nova.NewVirtualMachine().ValidateUnlocks(signedStardustTransaction, resolvedInputs) if err != nil { return nil, err } @@ -164,7 +164,7 @@ func (v *VM) Execute(executionContext context.Context, transaction mempool.Trans return nil, ierrors.Errorf("resolvedInputs not found in execution context") } - createdOutputs, err := stardust.NewVirtualMachine().Execute(stardustTransaction, resolvedInputs, unlockedIdentities) + createdOutputs, err := nova.NewVirtualMachine().Execute(stardustTransaction, resolvedInputs, unlockedIdentities) if err != nil { return nil, err } diff --git a/pkg/protocol/engine/mempool/mempool.go b/pkg/protocol/engine/mempool/mempool.go index d9b611009..0b4535afc 100644 --- a/pkg/protocol/engine/mempool/mempool.go +++ b/pkg/protocol/engine/mempool/mempool.go @@ -19,6 +19,8 @@ type MemPool[VoteRank conflictdag.VoteRankType[VoteRank]] interface { TransactionMetadata(id iotago.TransactionID) (transaction TransactionMetadata, exists bool) + VM() VM + InjectRequestedState(state State) TransactionMetadataByAttachment(blockID iotago.BlockID) (transaction TransactionMetadata, exists bool) diff --git a/pkg/protocol/engine/mempool/v1/mempool.go b/pkg/protocol/engine/mempool/v1/mempool.go index a831184d1..e82a2ea5a 100644 --- a/pkg/protocol/engine/mempool/v1/mempool.go +++ b/pkg/protocol/engine/mempool/v1/mempool.go @@ -88,6 +88,10 @@ func New[VoteRank conflictdag.VoteRankType[VoteRank]]( }, opts, (*MemPool[VoteRank]).setup) } +func (m *MemPool[VoteRank]) VM() mempool.VM { + return m.vm +} + // AttachSignedTransaction adds a transaction to the MemPool that was attached by the given block. func (m *MemPool[VoteRank]) AttachSignedTransaction(signedTransaction mempool.SignedTransaction, transaction mempool.Transaction, blockID iotago.BlockID) (signedTransactionMetadata mempool.SignedTransactionMetadata, err error) { storedSignedTransaction, isNewSignedTransaction, isNewTransaction, err := m.storeTransaction(signedTransaction, transaction, blockID) diff --git a/tools/evil-spammer/go.mod b/tools/evil-spammer/go.mod index 0d9e3aa96..be4d5ccb5 100644 --- a/tools/evil-spammer/go.mod +++ b/tools/evil-spammer/go.mod @@ -17,7 +17,7 @@ require ( github.com/iotaledger/hive.go/runtime v0.0.0-20230929122509-67f34bfed40d github.com/iotaledger/iota-core v0.0.0-00010101000000-000000000000 github.com/iotaledger/iota-core/tools/genesis-snapshot v0.0.0-00010101000000-000000000000 - github.com/iotaledger/iota.go/v4 v4.0.0-20231003101444-5687809cd68a + github.com/iotaledger/iota.go/v4 v4.0.0-20231003122130-2871888a9297 github.com/mr-tron/base58 v1.2.0 go.uber.org/atomic v1.11.0 ) diff --git a/tools/evil-spammer/go.sum b/tools/evil-spammer/go.sum index ddf98cf3b..6388ee5b3 100644 --- a/tools/evil-spammer/go.sum +++ b/tools/evil-spammer/go.sum @@ -195,8 +195,8 @@ github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20230929122509-67f34bf github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20230929122509-67f34bfed40d/go.mod h1:IJgaaxbgKCsNat18jlJJEAxCY2oVYR3F30B+M4vJ89I= github.com/iotaledger/hive.go/stringify v0.0.0-20230929122509-67f34bfed40d h1:ekHWRypoaiCXgrJVUQS7rCewsK3FuG1gTbPxu5jYn9c= github.com/iotaledger/hive.go/stringify v0.0.0-20230929122509-67f34bfed40d/go.mod h1:FTo/UWzNYgnQ082GI9QVM9HFDERqf9rw9RivNpqrnTs= -github.com/iotaledger/iota.go/v4 v4.0.0-20231003101444-5687809cd68a h1:xgh1YQvLN+Y3KwX1G9/znGbCaQsfpDtpSLn8nKvaP8s= -github.com/iotaledger/iota.go/v4 v4.0.0-20231003101444-5687809cd68a/go.mod h1:+e3bsJFDr9HxmUMe+eQOLNut5wfcB/ivhJdouOJgOnE= +github.com/iotaledger/iota.go/v4 v4.0.0-20231003122130-2871888a9297 h1:kbpsa/gF0x6Cxu5lN9TL8u5EdaGv1P3/OKKPW0w83Og= +github.com/iotaledger/iota.go/v4 v4.0.0-20231003122130-2871888a9297/go.mod h1:+e3bsJFDr9HxmUMe+eQOLNut5wfcB/ivhJdouOJgOnE= github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s= github.com/ipfs/go-cid v0.4.1/go.mod h1:uQHwDeX4c6CtyrFwdqyhpNcxVewur1M7l7fNU7LKwZk= github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= diff --git a/tools/gendoc/go.mod b/tools/gendoc/go.mod index ec2ff3eab..06bd92756 100644 --- a/tools/gendoc/go.mod +++ b/tools/gendoc/go.mod @@ -70,9 +70,9 @@ require ( github.com/iotaledger/hive.go/runtime v0.0.0-20230929122509-67f34bfed40d // indirect github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20230929122509-67f34bfed40d // indirect github.com/iotaledger/hive.go/stringify v0.0.0-20230929122509-67f34bfed40d // indirect - github.com/iotaledger/inx-app v1.0.0-rc.3.0.20230927140518-622f63be6182 // indirect - github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230927140257-bfa0bb0af2bd // indirect - github.com/iotaledger/iota.go/v4 v4.0.0-20231003101444-5687809cd68a // indirect + github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231001095511-32be422a567e // indirect + github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231001095356-923e8f138951 // indirect + github.com/iotaledger/iota.go/v4 v4.0.0-20231003122130-2871888a9297 // indirect github.com/ipfs/boxo v0.10.0 // indirect github.com/ipfs/go-cid v0.4.1 // indirect github.com/ipfs/go-datastore v0.6.0 // indirect diff --git a/tools/gendoc/go.sum b/tools/gendoc/go.sum index 56151f64a..5e53af302 100644 --- a/tools/gendoc/go.sum +++ b/tools/gendoc/go.sum @@ -307,12 +307,12 @@ github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20230929122509-67f34bf github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20230929122509-67f34bfed40d/go.mod h1:IJgaaxbgKCsNat18jlJJEAxCY2oVYR3F30B+M4vJ89I= github.com/iotaledger/hive.go/stringify v0.0.0-20230929122509-67f34bfed40d h1:ekHWRypoaiCXgrJVUQS7rCewsK3FuG1gTbPxu5jYn9c= github.com/iotaledger/hive.go/stringify v0.0.0-20230929122509-67f34bfed40d/go.mod h1:FTo/UWzNYgnQ082GI9QVM9HFDERqf9rw9RivNpqrnTs= -github.com/iotaledger/inx-app v1.0.0-rc.3.0.20230927140518-622f63be6182 h1:lQiktl3Q0B+cHbVum7WzJikOEP+buw686oSrw5Unyz8= -github.com/iotaledger/inx-app v1.0.0-rc.3.0.20230927140518-622f63be6182/go.mod h1:q24QEsS887ZWJVX76w2kwSgC84KS7wIKOy1otuqZ2ZM= -github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230927140257-bfa0bb0af2bd h1:nFG3Zq/zFA4KhBYFX2IezX1C74zfE0DqCt0LrgTa9Ig= -github.com/iotaledger/inx/go v1.0.0-rc.2.0.20230927140257-bfa0bb0af2bd/go.mod h1:c5778OnWpLq108YE+Eb2m8Ri/t/4ydV0TvI/Sy5YivQ= -github.com/iotaledger/iota.go/v4 v4.0.0-20231003101444-5687809cd68a h1:xgh1YQvLN+Y3KwX1G9/znGbCaQsfpDtpSLn8nKvaP8s= -github.com/iotaledger/iota.go/v4 v4.0.0-20231003101444-5687809cd68a/go.mod h1:+e3bsJFDr9HxmUMe+eQOLNut5wfcB/ivhJdouOJgOnE= +github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231001095511-32be422a567e h1:Mwoe7M6gI2DAjJIXmIskgnI8KdxCY1LyEEhtJCNYBsU= +github.com/iotaledger/inx-app v1.0.0-rc.3.0.20231001095511-32be422a567e/go.mod h1:jhzexR5X8m6qcmrwt5OX477O/ZwT7Ak9sPT83ByPkAo= +github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231001095356-923e8f138951 h1:qUf1W0fE1IyZzVy3Exv0Kj+SKECXG3S26c9m2ETb07U= +github.com/iotaledger/inx/go v1.0.0-rc.2.0.20231001095356-923e8f138951/go.mod h1:c5778OnWpLq108YE+Eb2m8Ri/t/4ydV0TvI/Sy5YivQ= +github.com/iotaledger/iota.go/v4 v4.0.0-20231003122130-2871888a9297 h1:kbpsa/gF0x6Cxu5lN9TL8u5EdaGv1P3/OKKPW0w83Og= +github.com/iotaledger/iota.go/v4 v4.0.0-20231003122130-2871888a9297/go.mod h1:+e3bsJFDr9HxmUMe+eQOLNut5wfcB/ivhJdouOJgOnE= 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= diff --git a/tools/genesis-snapshot/go.mod b/tools/genesis-snapshot/go.mod index 6a237325e..8a4a3785d 100644 --- a/tools/genesis-snapshot/go.mod +++ b/tools/genesis-snapshot/go.mod @@ -10,7 +10,7 @@ require ( github.com/iotaledger/hive.go/lo v0.0.0-20230929122509-67f34bfed40d github.com/iotaledger/hive.go/runtime v0.0.0-20230929122509-67f34bfed40d github.com/iotaledger/iota-core v0.0.0-00010101000000-000000000000 - github.com/iotaledger/iota.go/v4 v4.0.0-20231003101444-5687809cd68a + github.com/iotaledger/iota.go/v4 v4.0.0-20231003122130-2871888a9297 github.com/mr-tron/base58 v1.2.0 github.com/spf13/pflag v1.0.5 golang.org/x/crypto v0.13.0 diff --git a/tools/genesis-snapshot/go.sum b/tools/genesis-snapshot/go.sum index afe91a46a..fcf0ce336 100644 --- a/tools/genesis-snapshot/go.sum +++ b/tools/genesis-snapshot/go.sum @@ -50,8 +50,8 @@ github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20230929122509-67f34bf github.com/iotaledger/hive.go/serializer/v2 v2.0.0-rc.1.0.20230929122509-67f34bfed40d/go.mod h1:IJgaaxbgKCsNat18jlJJEAxCY2oVYR3F30B+M4vJ89I= github.com/iotaledger/hive.go/stringify v0.0.0-20230929122509-67f34bfed40d h1:ekHWRypoaiCXgrJVUQS7rCewsK3FuG1gTbPxu5jYn9c= github.com/iotaledger/hive.go/stringify v0.0.0-20230929122509-67f34bfed40d/go.mod h1:FTo/UWzNYgnQ082GI9QVM9HFDERqf9rw9RivNpqrnTs= -github.com/iotaledger/iota.go/v4 v4.0.0-20231003101444-5687809cd68a h1:xgh1YQvLN+Y3KwX1G9/znGbCaQsfpDtpSLn8nKvaP8s= -github.com/iotaledger/iota.go/v4 v4.0.0-20231003101444-5687809cd68a/go.mod h1:+e3bsJFDr9HxmUMe+eQOLNut5wfcB/ivhJdouOJgOnE= +github.com/iotaledger/iota.go/v4 v4.0.0-20231003122130-2871888a9297 h1:kbpsa/gF0x6Cxu5lN9TL8u5EdaGv1P3/OKKPW0w83Og= +github.com/iotaledger/iota.go/v4 v4.0.0-20231003122130-2871888a9297/go.mod h1:+e3bsJFDr9HxmUMe+eQOLNut5wfcB/ivhJdouOJgOnE= github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s= github.com/ipfs/go-cid v0.4.1/go.mod h1:uQHwDeX4c6CtyrFwdqyhpNcxVewur1M7l7fNU7LKwZk= github.com/klauspost/cpuid/v2 v2.2.5 h1:0E5MSMDEoAulmXNFquVs//DdoomxaoTY1kUhbc/qbZg= From ec67cbe6bd970274e4096e2b6e2590b4f9b9802e Mon Sep 17 00:00:00 2001 From: Hans Moog <3293976+hmoog@users.noreply.github.com> Date: Tue, 3 Oct 2023 17:14:55 +0200 Subject: [PATCH 2/6] Feat: cleaned up error handling --- components/inx/server_issuance.go | 73 +++++++++++++++---------------- 1 file changed, 35 insertions(+), 38 deletions(-) diff --git a/components/inx/server_issuance.go b/components/inx/server_issuance.go index ac17fea1b..ee4c42253 100644 --- a/components/inx/server_issuance.go +++ b/components/inx/server_issuance.go @@ -3,6 +3,8 @@ package inx import ( "context" + "github.com/iotaledger/hive.go/ierrors" + "github.com/iotaledger/hive.go/lo" "github.com/iotaledger/hive.go/serializer/v2/serix" inx "github.com/iotaledger/inx/go" "github.com/iotaledger/iota-core/pkg/protocol/engine/mempool" @@ -20,52 +22,47 @@ func (s *Server) RequestTips(_ context.Context, req *inx.TipsRequest) (*inx.Tips } func (s *Server) ValidatePayload(ctx context.Context, payload *inx.RawPayload) (*inx.PayloadValidationResponse, error) { - blockPayload, unwrapErr := payload.Unwrap(deps.Protocol.CurrentAPI(), serix.WithValidation()) - if unwrapErr != nil { - //nolint:nilerr // this is expected behavior - return &inx.PayloadValidationResponse{IsValid: false, Error: unwrapErr.Error()}, nil - } - - switch payload := blockPayload.(type) { - case *iotago.SignedTransaction: - memPool := deps.Protocol.MainEngineInstance().Ledger.MemPool() - inputReferences, inputsErr := memPool.VM().Inputs(payload.Transaction) - if inputsErr != nil { - //nolint:nilerr // this is expected behavior - return &inx.PayloadValidationResponse{IsValid: false, Error: inputsErr.Error()}, nil + if err := func() error { + blockPayload, unwrapErr := payload.Unwrap(deps.Protocol.CurrentAPI(), serix.WithValidation()) + if unwrapErr != nil { + return unwrapErr } - loadedInputs := make([]mempool.State, 0) + switch typedPayload := blockPayload.(type) { + case *iotago.SignedTransaction: + memPool := deps.Protocol.MainEngineInstance().Ledger.MemPool() - for _, inputReference := range inputReferences { - metadata, metadataErr := memPool.StateMetadata(inputReference) - if metadataErr != nil { - //nolint:nilerr // this is expected behavior - return &inx.PayloadValidationResponse{IsValid: false, Error: metadataErr.Error()}, nil + inputReferences, inputsErr := memPool.VM().Inputs(typedPayload.Transaction) + if inputsErr != nil { + return inputsErr } - loadedInputs = append(loadedInputs, metadata.State()) - } - - executionContext, validationErr := memPool.VM().ValidateSignatures(payload, loadedInputs) - if validationErr != nil { - //nolint:nilerr // this is expected behavior - return &inx.PayloadValidationResponse{IsValid: false, Error: validationErr.Error()}, nil - } + loadedInputs := make([]mempool.State, 0) + for _, inputReference := range inputReferences { + if metadata, metadataErr := memPool.StateMetadata(inputReference); metadataErr != nil { + return metadataErr + } else { + loadedInputs = append(loadedInputs, metadata.State()) + } + } - _, executionErr := memPool.VM().Execute(executionContext, payload.Transaction) - if executionErr != nil { - //nolint:nilerr // this is expected behavior - return &inx.PayloadValidationResponse{IsValid: false, Error: executionErr.Error()}, nil - } + executionContext, validationErr := memPool.VM().ValidateSignatures(typedPayload, loadedInputs) + if validationErr != nil { + return validationErr + } - return &inx.PayloadValidationResponse{IsValid: true}, nil + return lo.Return2(memPool.VM().Execute(executionContext, typedPayload.Transaction)) - case *iotago.TaggedData: - // TaggedData is always valid if serix decoding was successful - return &inx.PayloadValidationResponse{IsValid: true}, nil + case *iotago.TaggedData: + return nil - default: - return &inx.PayloadValidationResponse{IsValid: false, Error: "given payload type unknown"}, nil + default: + return ierrors.Errorf("unsupported payload type: %T", typedPayload) + } + }(); err != nil { + //nolint:nilerr // this is expected behavior + return &inx.PayloadValidationResponse{IsValid: false, Error: err.Error()}, nil } + + return &inx.PayloadValidationResponse{IsValid: true}, nil } From bb4d6a0e8d571cd9589e7a05304e72b936568301 Mon Sep 17 00:00:00 2001 From: Hans Moog <3293976+hmoog@users.noreply.github.com> Date: Tue, 3 Oct 2023 17:18:48 +0200 Subject: [PATCH 3/6] Feat: cleaned more --- components/inx/server_issuance.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/components/inx/server_issuance.go b/components/inx/server_issuance.go index ee4c42253..1cb2eecb6 100644 --- a/components/inx/server_issuance.go +++ b/components/inx/server_issuance.go @@ -46,13 +46,12 @@ func (s *Server) ValidatePayload(ctx context.Context, payload *inx.RawPayload) ( } } - executionContext, validationErr := memPool.VM().ValidateSignatures(typedPayload, loadedInputs) - if validationErr != nil { + if executionContext, validationErr := memPool.VM().ValidateSignatures(typedPayload, loadedInputs); validationErr != nil { return validationErr + } else { + return lo.Return2(memPool.VM().Execute(executionContext, typedPayload.Transaction)) } - return lo.Return2(memPool.VM().Execute(executionContext, typedPayload.Transaction)) - case *iotago.TaggedData: return nil From bc4c49234b40807cd707325635a6cf88b4462b76 Mon Sep 17 00:00:00 2001 From: Hans Moog <3293976+hmoog@users.noreply.github.com> Date: Tue, 3 Oct 2023 17:21:13 +0200 Subject: [PATCH 4/6] Feat: re-added comment --- components/inx/server_issuance.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/inx/server_issuance.go b/components/inx/server_issuance.go index 1cb2eecb6..967366335 100644 --- a/components/inx/server_issuance.go +++ b/components/inx/server_issuance.go @@ -21,7 +21,7 @@ func (s *Server) RequestTips(_ context.Context, req *inx.TipsRequest) (*inx.Tips }, nil } -func (s *Server) ValidatePayload(ctx context.Context, payload *inx.RawPayload) (*inx.PayloadValidationResponse, error) { +func (s *Server) ValidatePayload(payload *inx.RawPayload) (*inx.PayloadValidationResponse, error) { if err := func() error { blockPayload, unwrapErr := payload.Unwrap(deps.Protocol.CurrentAPI(), serix.WithValidation()) if unwrapErr != nil { @@ -53,6 +53,7 @@ func (s *Server) ValidatePayload(ctx context.Context, payload *inx.RawPayload) ( } case *iotago.TaggedData: + // TaggedData is always valid if serix decoding was successful return nil default: From f0c45ae651afa6aeb8aa55fc1aded3e86e3fbd5d Mon Sep 17 00:00:00 2001 From: Hans Moog <3293976+hmoog@users.noreply.github.com> Date: Tue, 3 Oct 2023 17:23:29 +0200 Subject: [PATCH 5/6] Refactor: added unused context again --- components/inx/server_issuance.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/inx/server_issuance.go b/components/inx/server_issuance.go index 967366335..376f8e0c5 100644 --- a/components/inx/server_issuance.go +++ b/components/inx/server_issuance.go @@ -21,7 +21,7 @@ func (s *Server) RequestTips(_ context.Context, req *inx.TipsRequest) (*inx.Tips }, nil } -func (s *Server) ValidatePayload(payload *inx.RawPayload) (*inx.PayloadValidationResponse, error) { +func (s *Server) ValidatePayload(_ context.Context, payload *inx.RawPayload) (*inx.PayloadValidationResponse, error) { if err := func() error { blockPayload, unwrapErr := payload.Unwrap(deps.Protocol.CurrentAPI(), serix.WithValidation()) if unwrapErr != nil { From 4fa5e495a2a248c838169dfd375dbb9fc64320e4 Mon Sep 17 00:00:00 2001 From: Hans Moog <3293976+hmoog@users.noreply.github.com> Date: Tue, 3 Oct 2023 17:32:26 +0200 Subject: [PATCH 6/6] Refactor: addressed linter issues --- components/inx/server_issuance.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/components/inx/server_issuance.go b/components/inx/server_issuance.go index 376f8e0c5..828380f08 100644 --- a/components/inx/server_issuance.go +++ b/components/inx/server_issuance.go @@ -39,19 +39,21 @@ func (s *Server) ValidatePayload(_ context.Context, payload *inx.RawPayload) (*i loadedInputs := make([]mempool.State, 0) for _, inputReference := range inputReferences { - if metadata, metadataErr := memPool.StateMetadata(inputReference); metadataErr != nil { + metadata, metadataErr := memPool.StateMetadata(inputReference) + if metadataErr != nil { return metadataErr - } else { - loadedInputs = append(loadedInputs, metadata.State()) } + + loadedInputs = append(loadedInputs, metadata.State()) } - if executionContext, validationErr := memPool.VM().ValidateSignatures(typedPayload, loadedInputs); validationErr != nil { + executionContext, validationErr := memPool.VM().ValidateSignatures(typedPayload, loadedInputs) + if validationErr != nil { return validationErr - } else { - return lo.Return2(memPool.VM().Execute(executionContext, typedPayload.Transaction)) } + return lo.Return2(memPool.VM().Execute(executionContext, typedPayload.Transaction)) + case *iotago.TaggedData: // TaggedData is always valid if serix decoding was successful return nil