-
Notifications
You must be signed in to change notification settings - Fork 12
/
server_issuance.go
73 lines (59 loc) · 2.42 KB
/
server_issuance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package inx
import (
"context"
"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"
iotago "github.com/iotaledger/iota.go/v4"
)
func (s *Server) RequestTips(_ context.Context, req *inx.TipsRequest) (*inx.TipsResponse, error) {
references := deps.Protocol.Engines.Main.Get().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(_ context.Context, payload *inx.RawPayload) (*inx.PayloadValidationResponse, error) {
if err := func() error {
blockPayload, unwrapErr := payload.Unwrap(deps.Protocol.CommittedAPI(), serix.WithValidation())
if unwrapErr != nil {
return unwrapErr
}
switch typedPayload := blockPayload.(type) {
case *iotago.SignedTransaction:
memPool := deps.Protocol.Engines.Main.Get().Ledger.MemPool()
inputReferences, inputsErr := memPool.VM().Inputs(typedPayload.Transaction)
if inputsErr != nil {
return inputsErr
}
loadedInputs := make([]mempool.State, 0)
for _, inputReference := range inputReferences {
metadata, metadataErr := memPool.StateMetadata(inputReference)
if metadataErr != nil {
return metadataErr
}
loadedInputs = append(loadedInputs, metadata.State())
}
executionContext, validationErr := memPool.VM().ValidateSignatures(typedPayload, loadedInputs)
if validationErr != nil {
return validationErr
}
return lo.Return2(memPool.VM().Execute(executionContext, typedPayload.Transaction))
case *iotago.TaggedData:
// TaggedData is always valid if serix decoding was successful
return nil
case *iotago.CandidacyAnnouncement:
panic("TODO: implement me")
default:
// We're switching on the Go payload type here, so we can only run into the default case
// if we added a new payload type and have not handled it above. In this case we want to panic.
panic("all supported payload types should be handled above")
}
}(); err != nil {
//nolint:nilerr // this is expected behavior
return &inx.PayloadValidationResponse{IsValid: false, Error: err.Error()}, nil
}
return &inx.PayloadValidationResponse{IsValid: true}, nil
}