Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DAG-Root-CID header for getBlock and getTransaction responses #88

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading