Skip to content
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
6 changes: 3 additions & 3 deletions backend/indexer/internal/indexer/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (i *Indexer) getBlockWithTransactions(height uint64, withTxs bool) (*types.

// getBlocksWithTransactions fetches blocks in batch using a single RPC call
func (i *Indexer) getBlocksWithTransactions(from, to uint64, withTxs bool) ([]*types.EthBlock, error) {
if to - from > 100 {
if to-from > 100 {
return nil, fmt.Errorf("max batch size is 100")
}

Expand Down Expand Up @@ -153,7 +153,7 @@ func (i *Indexer) getTransactionsReceipts(hash []string) ([]*types.TransactionRe

// Process receipts
receipts := make([]*types.TransactionReceipt, 0, len(hash))
for i := 0; i < 2 * len(hash); i = i + 2 {
for i := 0; i < 2*len(hash); i = i + 2 {
// Process transaction receipt
// if can't fetch tx receipt, skip
if rpcResponses[i].Error != nil {
Expand Down Expand Up @@ -224,4 +224,4 @@ func processMessageCid(data interface{}) (string, error) {
}

return response.Cid, nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,4 @@ func (p *PostgresDB) CleanupFinalizedProofSets(ctx context.Context, currentBlock
return fmt.Errorf("failed to cleanup finalized proof sets: %w", err)
}
return nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,13 @@ func (h *FaultRecordHandler) HandleEvent(ctx context.Context, eventLog *types.Lo
return fmt.Errorf("failed to store provider: %w", err)
}
}

// Get challenged roots
challengedRoots, err := h.findChallengedRoots(ctx, setId, big.NewInt(challengeEpoch), uint64(totalLeaves))
challengedRoots, err := h.findChallengedRoots(ctx, setId, big.NewInt(challengeEpoch), uint64(totalLeaves), big.NewInt(int64(blockNumber-1)))
if err != nil {
return fmt.Errorf("failed to find challenged roots: %w", err)
}
fmt.Printf("[Fault Record] challenged roots: %v\n", challengedRoots)

// Use a map to deduplicate root IDs
uniqueRootIds := make(map[int64]bool)
Expand All @@ -200,7 +201,7 @@ func (h *FaultRecordHandler) HandleEvent(ctx context.Context, eventLog *types.Lo
// Update root stats
root, err := h.db.FindRoot(ctx, setId.Int64(), rootId.Int64())
if err != nil {
return fmt.Errorf("failed to find root: %w", err)
return fmt.Errorf("[Fault Record] failed to find root (%d, %d): %w", setId.Int64(), rootId.Int64(), err)
}

if root != nil {
Expand Down Expand Up @@ -255,9 +256,10 @@ func getUint256FromData(data string, offset int) (*big.Int, error) {
func (h *FaultRecordHandler) findChallengedRoots(
ctx context.Context,
proofSetID, nextChallengeEpoch *big.Int, totalLeaves uint64,
blockNumber *big.Int,
) ([]int64, error) {

callOpts := &bind.CallOpts{Context: ctx}
callOpts := &bind.CallOpts{Context: ctx, BlockNumber: blockNumber}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its easy to directly specify blockNumber as callOpts


// Fetch chain randomness from the Filecoin beacon at nextChallengeEpoch
seedInt, err := h.pdpVerifier.GetRandomness(callOpts, nextChallengeEpoch)
Expand Down Expand Up @@ -339,4 +341,3 @@ func padTo32Bytes(b []byte) []byte {
copy(out[32-len(b):], b)
return out
}

Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (h *PossessionProvenHandler) HandleEvent(ctx context.Context, eventLog *typ
timestamp := time.Unix(int64(eventLog.Timestamp), 0)

dbEventData, err := json.Marshal(map[string]interface{}{
"setId": setId.String(),
"setId": setId.String(),
"challenges": challenges,
})
if err != nil {
Expand All @@ -76,15 +76,15 @@ func (h *PossessionProvenHandler) HandleEvent(ctx context.Context, eventLog *typ
BlockNumber: blockNumber,
BlockHash: eventLog.BlockHash,
},
SetId: setId.Int64(),
Name: "PossessionProven",
Data: poolType.JSONText(dbEventData),
Removed: eventLog.Removed,
Address: eventLog.Address,
LogIndex: hexToInt64(eventLog.LogIndex),
SetId: setId.Int64(),
Name: "PossessionProven",
Data: poolType.JSONText(dbEventData),
Removed: eventLog.Removed,
Address: eventLog.Address,
LogIndex: hexToInt64(eventLog.LogIndex),
TransactionHash: eventLog.TransactionHash,
Topics: eventLog.Topics,
CreatedAt: timestamp,
Topics: eventLog.Topics,
CreatedAt: timestamp,
}

// Store event log
Expand Down Expand Up @@ -121,7 +121,7 @@ func (h *PossessionProvenHandler) HandleEvent(ctx context.Context, eventLog *typ
for rootId := range uniqueRootIds {
root, err := h.db.FindRoot(ctx, setId.Int64(), rootId)
if err != nil {
return fmt.Errorf("failed to find root: %w", err)
return fmt.Errorf("[Possession Proven] failed to find root: %w", err)
}

if root != nil {
Expand Down Expand Up @@ -222,7 +222,7 @@ func parseProofs(input string) ([]ProofData, error) {
}

proofsData, ok := decodedData[1].([]struct {
Leaf [32]uint8 `json:"leaf"`
Leaf [32]uint8 `json:"leaf"`
Proof [][32]uint8 `json:"proof"`
})
if !ok {
Expand All @@ -248,44 +248,44 @@ func parseProofs(input string) ([]ProofData, error) {
// parseChallenges parses RootIdAndOffset array from event data
func parseChallenges(data string) ([]RootIdAndOffset, error) {
// Remove "0x" prefix if present
if len(data) > 2 && data[:2] == "0x" {
data = data[2:]
}
// Decode hex string to bytes
rawData, err := hex.DecodeString(data)
if err != nil {
return nil, fmt.Errorf("failed to decode hex: %v", err)
}
// First 32 bytes (offset to array)
offset := new(big.Int).SetBytes(rawData[:32])
if offset.Uint64() != 32 { // Should be 32 (0x20)
return nil, fmt.Errorf("invalid offset: %v", offset)
}
// Next 32 bytes (array length)
length := new(big.Int).SetBytes(rawData[32:64])
arrayLen := length.Uint64()
// Parse each RootIdAndOffset struct
result := make([]RootIdAndOffset, arrayLen)
for i := uint64(0); i < arrayLen; i++ {
startIdx := 64 + (i * 64) // Each struct takes 64 bytes (2 * 32)
// Parse rootId
rootId := new(big.Int).SetBytes(rawData[startIdx : startIdx+32])
// Parse offset
offset := new(big.Int).SetBytes(rawData[startIdx+32 : startIdx+64])
result[i] = RootIdAndOffset{
RootId: rootId,
Offset: offset,
}
}
return result, nil
if len(data) > 2 && data[:2] == "0x" {
data = data[2:]
}

// Decode hex string to bytes
rawData, err := hex.DecodeString(data)
if err != nil {
return nil, fmt.Errorf("failed to decode hex: %v", err)
}

// First 32 bytes (offset to array)
offset := new(big.Int).SetBytes(rawData[:32])
if offset.Uint64() != 32 { // Should be 32 (0x20)
return nil, fmt.Errorf("invalid offset: %v", offset)
}

// Next 32 bytes (array length)
length := new(big.Int).SetBytes(rawData[32:64])
arrayLen := length.Uint64()

// Parse each RootIdAndOffset struct
result := make([]RootIdAndOffset, arrayLen)
for i := uint64(0); i < arrayLen; i++ {
startIdx := 64 + (i * 64) // Each struct takes 64 bytes (2 * 32)

// Parse rootId
rootId := new(big.Int).SetBytes(rawData[startIdx : startIdx+32])

// Parse offset
offset := new(big.Int).SetBytes(rawData[startIdx+32 : startIdx+64])

result[i] = RootIdAndOffset{
RootId: rootId,
Offset: offset,
}
}

return result, nil
}

// encodeProofToBytes encodes a merkle proof array into a single byte slice
Expand All @@ -309,4 +309,4 @@ func encodeProofToBytes(proof [][]byte) []byte {
}

return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ type RootsRemovedHandler struct {
db Database
}


func NewRootsRemovedHandler(db Database) *RootsRemovedHandler {
return &RootsRemovedHandler{
BaseHandler: NewBaseHandler(HandlerTypeEvent),
db: db,
}
}


// RootsRemovedHandler handles RootsRemoved events
func (h *RootsRemovedHandler) HandleEvent(ctx context.Context, eventLog *types.Log, tx *types.Transaction) error {
// Parse setId from topics
Expand All @@ -45,7 +43,7 @@ func (h *RootsRemovedHandler) HandleEvent(ctx context.Context, eventLog *types.L
offsetToArrayData := new(big.Int).SetBytes(data[:32]).Uint64()

// Extract array length from 32 bytes
arrayLen := new(big.Int).SetBytes(data[offsetToArrayData:offsetToArrayData+32]).Uint64()
arrayLen := new(big.Int).SetBytes(data[offsetToArrayData : offsetToArrayData+32]).Uint64()

if len(data) < int(offsetToArrayData+(arrayLen*32)) {
return fmt.Errorf("invalid data length for rootIds array")
Expand All @@ -63,7 +61,7 @@ func (h *RootsRemovedHandler) HandleEvent(ctx context.Context, eventLog *types.L

// Store event log
dbEventData, err := json.Marshal(map[string]interface{}{
"setId": setId.String(),
"setId": setId.String(),
"root_ids": rootIds,
})
if err != nil {
Expand Down Expand Up @@ -102,9 +100,9 @@ func (h *RootsRemovedHandler) HandleEvent(ctx context.Context, eventLog *types.L
// First get the root to get its raw_size
root, err := h.db.FindRoot(ctx, setId.Int64(), rootIdInt)
if err != nil {
return fmt.Errorf("failed to find root: %w", err)
return fmt.Errorf("[Roots Removed] failed to find root: %w", err)
}

if root != nil {
totalDataSize.Add(totalDataSize, big.NewInt(root.RawSize))

Expand Down Expand Up @@ -166,4 +164,4 @@ func (h *RootsRemovedHandler) HandleEvent(ctx context.Context, eventLog *types.L
}

return nil
}
}