Skip to content

Commit

Permalink
Add DAG-Root-CID header for getBlock and getTransaction responses
Browse files Browse the repository at this point in the history
  • Loading branch information
gagliardetto committed Feb 20, 2024
1 parent 47b4f23 commit 6f3b6e2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
20 changes: 10 additions & 10 deletions epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -785,11 +785,11 @@ func (ser *Epoch) FindOffsetAndSizeFromCid(ctx context.Context, cid cid.Cid) (os
return found, nil
}

func (ser *Epoch) GetBlock(ctx context.Context, slot uint64) (*ipldbindcode.Block, error) {
func (ser *Epoch) GetBlock(ctx context.Context, slot uint64) (*ipldbindcode.Block, cid.Cid, error) {
// get the slot by slot number
wantedCid, err := ser.FindCidFromSlot(ctx, slot)
if err != nil {
return nil, fmt.Errorf("failed to find CID for slot %d: %w", slot, err)
return nil, cid.Cid{}, fmt.Errorf("failed to find CID for slot %d: %w", slot, err)
}
{
doPrefetch := getValueFromContext(ctx, "prefetch")
Expand All @@ -801,14 +801,14 @@ func (ser *Epoch) GetBlock(ctx context.Context, slot uint64) (*ipldbindcode.Bloc
// get the block by CID
data, err := ser.GetNodeByCid(ctx, wantedCid)
if err != nil {
return nil, fmt.Errorf("failed to get node by cid %s: %w", wantedCid, err)
return nil, cid.Cid{}, fmt.Errorf("failed to get node by cid %s: %w", wantedCid, err)
}
// try parsing the data as a Block node.
decoded, err := iplddecoders.DecodeBlock(data)
if err != nil {
return nil, fmt.Errorf("failed to decode block with CID %s: %w", wantedCid, err)
return nil, cid.Cid{}, fmt.Errorf("failed to decode block with CID %s: %w", wantedCid, err)
}
return decoded, nil
return decoded, wantedCid, nil
}

func (ser *Epoch) GetEntryByCid(ctx context.Context, wantedCid cid.Cid) (*ipldbindcode.Entry, error) {
Expand Down Expand Up @@ -863,11 +863,11 @@ func (ser *Epoch) GetRewardsByCid(ctx context.Context, wantedCid cid.Cid) (*ipld
return decoded, nil
}

func (ser *Epoch) GetTransaction(ctx context.Context, sig solana.Signature) (*ipldbindcode.Transaction, error) {
func (ser *Epoch) GetTransaction(ctx context.Context, sig solana.Signature) (*ipldbindcode.Transaction, cid.Cid, error) {
// get the CID by signature
wantedCid, err := ser.FindCidFromSignature(ctx, sig)
if err != nil {
return nil, fmt.Errorf("failed to find CID for signature %s: %w", sig, err)
return nil, cid.Cid{}, fmt.Errorf("failed to find CID for signature %s: %w", sig, err)
}
{
doPrefetch := getValueFromContext(ctx, "prefetch")
Expand All @@ -879,12 +879,12 @@ func (ser *Epoch) GetTransaction(ctx context.Context, sig solana.Signature) (*ip
// get the transaction by CID
data, err := ser.GetNodeByCid(ctx, wantedCid)
if err != nil {
return nil, fmt.Errorf("failed to get node by cid %s: %w", wantedCid, err)
return nil, cid.Cid{}, fmt.Errorf("failed to get node by cid %s: %w", wantedCid, err)
}
// try parsing the data as a Transaction node.
decoded, err := iplddecoders.DecodeTransaction(data)
if err != nil {
return nil, fmt.Errorf("failed to decode transaction with CID %s: %w", wantedCid, err)
return nil, cid.Cid{}, fmt.Errorf("failed to decode transaction with CID %s: %w", wantedCid, err)
}
return decoded, nil
return decoded, wantedCid, nil
}
9 changes: 7 additions & 2 deletions multiepoch-getBlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (multi *MultiEpoch) handleGetBlock(ctx context.Context, conn *requestContex
}, fmt.Errorf("failed to get epoch %d: %w", epochNumber, err)
}

block, err := epochHandler.GetBlock(WithSubrapghPrefetch(ctx, true), slot)
block, blockCid, err := epochHandler.GetBlock(WithSubrapghPrefetch(ctx, true), slot)
if err != nil {
if errors.Is(err, compactindexsized.ErrNotFound) {
return &jsonrpc2.Error{
Expand All @@ -84,6 +84,11 @@ func (multi *MultiEpoch) handleGetBlock(ctx context.Context, conn *requestContex
}, fmt.Errorf("failed to get block: %w", err)
}
}
// set the headers:
{
conn.ctx.Response.Header.Set("DAG-Root-CID", blockCid.String())
}

tim.time("GetBlock")
{
prefetcherFromCar := func() error {
Expand Down Expand Up @@ -448,7 +453,7 @@ func (multi *MultiEpoch) handleGetBlock(ctx context.Context, conn *requestContex
if (parentSlot != 0 || slot == 1) && CalcEpochForSlot(parentSlot) == epochNumber {
// NOTE: if the parent is in the same epoch, we can get it from the same epoch handler as the block;
// otherwise, we need to get it from the previous epoch (TODO: implement this)
parentBlock, err := epochHandler.GetBlock(WithSubrapghPrefetch(ctx, false), parentSlot)
parentBlock, _, err := epochHandler.GetBlock(WithSubrapghPrefetch(ctx, false), parentSlot)
if err != nil {
return &jsonrpc2.Error{
Code: jsonrpc2.CodeInternalError,
Expand Down
2 changes: 1 addition & 1 deletion multiepoch-getBlockTime.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (multi *MultiEpoch) handleGetBlockTime(ctx context.Context, conn *requestCo
}, fmt.Errorf("failed to get epoch %d: %w", epochNumber, err)
}

block, err := epochHandler.GetBlock(WithSubrapghPrefetch(ctx, false), blockNum)
block, _, err := epochHandler.GetBlock(WithSubrapghPrefetch(ctx, false), blockNum)
if err != nil {
if errors.Is(err, compactindexsized.ErrNotFound) {
return &jsonrpc2.Error{
Expand Down
4 changes: 2 additions & 2 deletions multiepoch-getSignaturesForAddress.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (multi *MultiEpoch) handleGetSignaturesForAddress(ctx context.Context, conn
if blockTime, ok := blockTimeCache.m[slot]; ok {
return blockTime
}
block, err := ser.GetBlock(ctx, slot)
block, _, err := ser.GetBlock(ctx, slot)
if err != nil {
klog.Errorf("failed to get block time for slot %d: %v", slot, err)
return 0
Expand Down Expand Up @@ -158,7 +158,7 @@ func (multi *MultiEpoch) handleGetSignaturesForAddress(ctx context.Context, conn
if signaturesOnly {
return nil
}
transactionNode, err := ser.GetTransaction(ctx, sig)
transactionNode, _, err := ser.GetTransaction(ctx, sig)
if err != nil {
klog.Errorf("failed to get tx %s: %v", sig, err)
return nil
Expand Down
7 changes: 5 additions & 2 deletions multiepoch-getTransaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (multi *MultiEpoch) handleGetTransaction(ctx context.Context, conn *request
}, fmt.Errorf("failed to get handler for epoch %d: %w", epochNumber, err)
}

transactionNode, err := epochHandler.GetTransaction(WithSubrapghPrefetch(ctx, true), sig)
transactionNode, transactionCid, err := epochHandler.GetTransaction(WithSubrapghPrefetch(ctx, true), sig)
if err != nil {
if errors.Is(err, compactindexsized.ErrNotFound) {
// NOTE: solana just returns null here in case of transaction not found
Expand All @@ -167,12 +167,15 @@ func (multi *MultiEpoch) handleGetTransaction(ctx context.Context, conn *request
Message: "Internal error",
}, fmt.Errorf("failed to get Transaction: %v", err)
}
{
conn.ctx.Response.Header.Set("DAG-Root-CID", transactionCid.String())
}

var response GetTransactionResponse

response.Slot = ptrToUint64(uint64(transactionNode.Slot))
{
block, err := epochHandler.GetBlock(ctx, uint64(transactionNode.Slot))
block, _, err := epochHandler.GetBlock(ctx, uint64(transactionNode.Slot))
if err != nil {
return &jsonrpc2.Error{
Code: jsonrpc2.CodeInternalError,
Expand Down

0 comments on commit 6f3b6e2

Please sign in to comment.