Skip to content

Commit

Permalink
Include OutputMetadata in output response
Browse files Browse the repository at this point in the history
  • Loading branch information
jkrvivian committed Sep 19, 2023
1 parent 456ba64 commit a2550f7
Showing 1 changed file with 25 additions and 73 deletions.
98 changes: 25 additions & 73 deletions components/inx/server_utxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,28 @@ import (
)

func NewLedgerOutput(o *utxoledger.Output) (*inx.LedgerOutput, error) {
return &inx.LedgerOutput{
latestCommitment := deps.Protocol.MainEngineInstance().SyncManager.LatestCommitment()

l := &inx.LedgerOutput{
OutputId: inx.NewOutputId(o.OutputID()),
BlockId: inx.NewBlockId(o.BlockID()),
SlotBooked: uint64(o.SlotBooked()),
SlotCreated: uint64(o.SlotCreated()),
Output: &inx.RawOutput{
Data: o.Bytes(),
},
}, nil
}

includedSlotIndex := o.SlotBooked()
if includedSlotIndex <= latestCommitment.Index() {
includedCommitment, err := deps.Protocol.MainEngineInstance().Storage.Commitments().Load(includedSlotIndex)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to load commitment with index: %d", includedSlotIndex)
}
l.CommitmentIdIncluded = inx.NewCommitmentId(includedCommitment.ID())

Check failure on line 36 in components/inx/server_utxo.go

View workflow job for this annotation

GitHub Actions / Unit tests -race

l.CommitmentIdIncluded undefined (type *"github.com/iotaledger/inx/go".LedgerOutput has no field or method CommitmentIdIncluded)

Check failure on line 36 in components/inx/server_utxo.go

View workflow job for this annotation

GitHub Actions / Unit tests

l.CommitmentIdIncluded undefined (type *"github.com/iotaledger/inx/go".LedgerOutput has no field or method CommitmentIdIncluded)

Check failure on line 36 in components/inx/server_utxo.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] components/inx/server_utxo.go#L36

l.CommitmentIdIncluded undefined (type *"github.com/iotaledger/inx/go".LedgerOutput has no field or method CommitmentIdIncluded)
Raw output
components/inx/server_utxo.go:36:5: l.CommitmentIdIncluded undefined (type *"github.com/iotaledger/inx/go".LedgerOutput has no field or method CommitmentIdIncluded)

Check failure on line 36 in components/inx/server_utxo.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] components/inx/server_utxo.go#L36

l.CommitmentIdIncluded undefined (type *"github.com/iotaledger/inx/go".LedgerOutput has no field or method CommitmentIdIncluded)
Raw output
components/inx/server_utxo.go:36:5: l.CommitmentIdIncluded undefined (type *"github.com/iotaledger/inx/go".LedgerOutput has no field or method CommitmentIdIncluded)
}

return l, nil
}

func NewLedgerSpent(s *utxoledger.Spent) (*inx.LedgerSpent, error) {
Expand All @@ -38,6 +51,16 @@ func NewLedgerSpent(s *utxoledger.Spent) (*inx.LedgerSpent, error) {
SlotSpent: uint64(s.SlotIndexSpent()),
}

latestCommitment := deps.Protocol.MainEngineInstance().SyncManager.LatestCommitment()
spentSlotIndex := s.SlotIndexSpent()
if spentSlotIndex <= latestCommitment.Index() {
spentCommitment, err := deps.Protocol.MainEngineInstance().Storage.Commitments().Load(spentSlotIndex)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to load commitment with index: %d", spentSlotIndex)
}
l.CommitmentIdSpent = inx.NewCommitmentId(spentCommitment.ID())

Check failure on line 61 in components/inx/server_utxo.go

View workflow job for this annotation

GitHub Actions / Unit tests -race

l.CommitmentIdSpent undefined (type *"github.com/iotaledger/inx/go".LedgerSpent has no field or method CommitmentIdSpent)

Check failure on line 61 in components/inx/server_utxo.go

View workflow job for this annotation

GitHub Actions / Unit tests

l.CommitmentIdSpent undefined (type *"github.com/iotaledger/inx/go".LedgerSpent has no field or method CommitmentIdSpent)

Check failure on line 61 in components/inx/server_utxo.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] components/inx/server_utxo.go#L61

l.CommitmentIdSpent undefined (type *"github.com/iotaledger/inx/go".LedgerSpent has no field or method CommitmentIdSpent) (typecheck)
Raw output
components/inx/server_utxo.go:61:5: l.CommitmentIdSpent undefined (type *"github.com/iotaledger/inx/go".LedgerSpent has no field or method CommitmentIdSpent) (typecheck)
package inx

Check failure on line 61 in components/inx/server_utxo.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] components/inx/server_utxo.go#L61

l.CommitmentIdSpent undefined (type *"github.com/iotaledger/inx/go".LedgerSpent has no field or method CommitmentIdSpent)) (typecheck)
Raw output
components/inx/server_utxo.go:61:5: l.CommitmentIdSpent undefined (type *"github.com/iotaledger/inx/go".LedgerSpent has no field or method CommitmentIdSpent)) (typecheck)
	"github.com/iotaledger/iota-core/components/inx"
	^
}

return l, nil
}

Expand Down Expand Up @@ -165,21 +188,6 @@ func (s *Server) ReadUnspentOutputs(_ *inx.NoParams, srv inx.INX_ReadUnspentOutp
return err
}

func (s *Server) ReadOutputMetadata(_ context.Context, id *inx.OutputId) (*inx.OutputMetadata, error) {
outputID := id.Unwrap()

output, spent, err := deps.Protocol.MainEngineInstance().Ledger.OutputOrSpent(outputID)
if err != nil {
return nil, err
}

if spent != nil {
return newSpentMetadataResponse(spent)
}

return newOutputMetadataResponse(output)
}

func (s *Server) ListenToLedgerUpdates(req *inx.SlotRangeRequest, srv inx.INX_ListenToLedgerUpdatesServer) error {
createLedgerUpdatePayloadAndSend := func(slot iotago.SlotIndex, outputs utxoledger.Outputs, spents utxoledger.Spents) error {
// Send Begin
Expand Down Expand Up @@ -331,59 +339,3 @@ func (s *Server) ListenToLedgerUpdates(req *inx.SlotRangeRequest, srv inx.INX_Li

return innerErr
}

func newOutputMetadataResponse(output *utxoledger.Output) (*inx.OutputMetadata, error) {
latestCommitment := deps.Protocol.MainEngineInstance().SyncManager.LatestCommitment()

metadata := &inx.OutputMetadata{
BlockId: inx.NewBlockId(output.BlockID()),
TransactionId: inx.NewTransactionId(output.OutputID().TransactionID()),
OutputIndex: uint32(output.OutputID().Index()),
IsSpent: false,
LatestCommitmentId: inx.NewCommitmentId(latestCommitment.ID()),
}

includedSlotIndex := output.SlotBooked()
if includedSlotIndex <= latestCommitment.Index() {
includedCommitment, err := deps.Protocol.MainEngineInstance().Storage.Commitments().Load(includedSlotIndex)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to load commitment with index: %d", includedSlotIndex)
}
metadata.CommitmentIdIncluded = inx.NewCommitmentId(includedCommitment.ID())
}

return metadata, nil
}

func newSpentMetadataResponse(spent *utxoledger.Spent) (*inx.OutputMetadata, error) {
latestCommitment := deps.Protocol.MainEngineInstance().SyncManager.LatestCommitment()

metadata := &inx.OutputMetadata{
BlockId: inx.NewBlockId(spent.BlockID()),
TransactionId: inx.NewTransactionId(spent.OutputID().TransactionID()),
OutputIndex: uint32(spent.OutputID().Index()),
IsSpent: true,
TransactionIdSpent: inx.NewTransactionId(spent.TransactionIDSpent()),
LatestCommitmentId: inx.NewCommitmentId(latestCommitment.ID()),
}

includedSlotIndex := spent.Output().SlotBooked()
if includedSlotIndex <= latestCommitment.Index() {
includedCommitment, err := deps.Protocol.MainEngineInstance().Storage.Commitments().Load(includedSlotIndex)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to load commitment with index: %d", includedSlotIndex)
}
metadata.CommitmentIdIncluded = inx.NewCommitmentId(includedCommitment.ID())
}

spentSlotIndex := spent.SlotIndexSpent()
if spentSlotIndex <= latestCommitment.Index() {
spentCommitment, err := deps.Protocol.MainEngineInstance().Storage.Commitments().Load(spentSlotIndex)
if err != nil {
return nil, ierrors.Wrapf(err, "failed to load commitment with index: %d", spentSlotIndex)
}
metadata.CommitmentIdSpent = inx.NewCommitmentId(spentCommitment.ID())
}

return metadata, nil
}

0 comments on commit a2550f7

Please sign in to comment.