fix: get starting indexed block

This commit is contained in:
Gaze
2024-04-15 15:01:50 +07:00
parent 0ba272b8f1
commit 591bbe1614
4 changed files with 23 additions and 2 deletions

View File

@@ -3,6 +3,7 @@ package runes
import (
"bytes"
"encoding/hex"
"log/slog"
"slices"
"strconv"
"strings"
@@ -13,6 +14,8 @@ import (
"github.com/gaze-network/indexer-network/core/types"
"github.com/gaze-network/indexer-network/modules/runes/internal/entity"
"github.com/gaze-network/indexer-network/modules/runes/internal/runes"
"github.com/gaze-network/indexer-network/pkg/logger"
"github.com/gaze-network/indexer-network/pkg/logger/slogx"
"github.com/gaze-network/uint128"
"github.com/samber/lo"
)
@@ -66,6 +69,7 @@ func (p *Processor) getHashPayload(header types.BlockHeader) ([]byte, error) {
}
sb.Write(bytes)
}
logger.Debug("event hash payload", slog.Int("height", int(header.Height)), slogx.String("payload", sb.String()))
return []byte(sb.String()), nil
}

View File

@@ -5,6 +5,7 @@ import "github.com/btcsuite/btcd/chaincfg/chainhash"
type IndexedBlock struct {
Height int64
Hash chainhash.Hash
PrevHash chainhash.Hash
EventHash chainhash.Hash
CumulativeEventHash chainhash.Hash
}

View File

@@ -721,18 +721,28 @@ func (p *Processor) createIndexedBlock(ctx context.Context, header types.BlockHe
if err != nil {
return errors.Wrap(err, "failed to calculate event hash")
}
indexedBlock, err := p.runesDg.GetIndexedBlockByHeight(ctx, header.Height)
prevIndexedBlock, err := p.runesDg.GetIndexedBlockByHeight(ctx, header.Height-1)
if err != nil && errors.Is(err, errs.NotFound) && header.Height-1 == startingBlockHeader[p.network].Height {
prevIndexedBlock = &entity.IndexedBlock{
Height: startingBlockHeader[p.network].Height,
Hash: startingBlockHeader[p.network].Hash,
EventHash: chainhash.Hash{},
CumulativeEventHash: chainhash.Hash{},
}
err = nil
}
if err != nil {
if errors.Is(err, errs.NotFound) {
return errors.Errorf("indexed block not found for height %d. Indexed block must be created for every Bitcoin block", header.Height)
}
return errors.Wrap(err, "failed to get indexed block by height")
}
cumulativeEventHash := chainhash.DoubleHashH(append(indexedBlock.CumulativeEventHash[:], eventHash[:]...))
cumulativeEventHash := chainhash.DoubleHashH(append(prevIndexedBlock.CumulativeEventHash[:], eventHash[:]...))
if err := p.runesDg.CreateIndexedBlock(ctx, &entity.IndexedBlock{
Height: header.Height,
Hash: header.Hash,
PrevHash: header.PrevBlock,
EventHash: eventHash,
CumulativeEventHash: cumulativeEventHash,
}); err != nil {

View File

@@ -592,6 +592,10 @@ func mapIndexedBlockModelToType(src gen.RunesIndexedBlock) (*entity.IndexedBlock
if err != nil {
return nil, errors.Wrap(err, "failed to parse block hash")
}
prevBlockHash, err := chainhash.NewHashFromStr(src.PrevHash)
if err != nil {
return nil, errors.Wrap(err, "failed to parse prev block hash")
}
eventHash, err := chainhash.NewHashFromStr(src.EventHash)
if err != nil {
return nil, errors.Wrap(err, "failed to parse event hash")
@@ -603,6 +607,7 @@ func mapIndexedBlockModelToType(src gen.RunesIndexedBlock) (*entity.IndexedBlock
return &entity.IndexedBlock{
Height: int64(src.Height),
Hash: *hash,
PrevHash: *prevBlockHash,
EventHash: *eventHash,
CumulativeEventHash: *cumulativeEventHash,
}, nil
@@ -612,6 +617,7 @@ func mapIndexedBlockTypeToParams(src entity.IndexedBlock) (gen.CreateIndexedBloc
return gen.CreateIndexedBlockParams{
Height: int32(src.Height),
Hash: src.Hash.String(),
PrevHash: src.PrevHash.String(),
EventHash: src.EventHash.String(),
CumulativeEventHash: src.CumulativeEventHash.String(),
}, nil