-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
319 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package indexer | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
"math/big" | ||
|
||
"github.com/xmtp/xmtpd/pkg/blockchain" | ||
"github.com/xmtp/xmtpd/pkg/db/queries" | ||
) | ||
|
||
type ChainReorgHandler interface { | ||
FindReorgPoint(detectedAt uint64) (uint64, []byte, error) | ||
} | ||
|
||
type ReorgHandler struct { | ||
ctx context.Context | ||
client blockchain.ChainClient | ||
queries *queries.Queries | ||
} | ||
|
||
var ( | ||
ErrNoBlocksFound = errors.New("no blocks found") | ||
ErrGetBlock = errors.New("failed to get block") | ||
) | ||
|
||
// TODO: Make this configurable? | ||
const BLOCK_RANGE_SIZE uint64 = 1000 | ||
|
||
func NewChainReorgHandler( | ||
ctx context.Context, | ||
client blockchain.ChainClient, | ||
queries *queries.Queries, | ||
) *ReorgHandler { | ||
return &ReorgHandler{ | ||
ctx: ctx, | ||
client: client, | ||
queries: queries, | ||
} | ||
} | ||
|
||
// TODO: When reorg range has been calculated, alert clients (TBD) | ||
func (r *ReorgHandler) FindReorgPoint(detectedAt uint64) (uint64, []byte, error) { | ||
startBlock, endBlock := blockRange(detectedAt) | ||
|
||
for { | ||
storedBlocks, err := r.queries.GetBlocksInRange( | ||
r.ctx, | ||
queries.GetBlocksInRangeParams{ | ||
StartBlock: startBlock, | ||
EndBlock: endBlock, | ||
}, | ||
) | ||
if err != nil && !errors.Is(err, sql.ErrNoRows) { | ||
return 0, nil, fmt.Errorf("failed to get stored blocks: %w", err) | ||
} | ||
|
||
if len(storedBlocks) == 0 || errors.Is(err, sql.ErrNoRows) { | ||
if startBlock == 0 { | ||
return 0, nil, ErrNoBlocksFound | ||
} | ||
|
||
startBlock, endBlock = blockRange(startBlock) | ||
continue | ||
} | ||
|
||
oldestBlock := storedBlocks[0] | ||
chainBlock, err := r.client.BlockByNumber(r.ctx, big.NewInt(int64(oldestBlock.BlockNumber))) | ||
if err != nil { | ||
return 0, nil, fmt.Errorf("%w %d: %v", ErrGetBlock, oldestBlock.BlockNumber, err) | ||
} | ||
|
||
// Oldest block doesn't match, reorg happened earlier in the chain | ||
if !bytes.Equal(oldestBlock.BlockHash, chainBlock.Hash().Bytes()) { | ||
if startBlock == 0 { | ||
return 0, nil, ErrNoBlocksFound | ||
} | ||
|
||
startBlock, endBlock = blockRange(startBlock) | ||
continue | ||
} | ||
|
||
// Oldest block matches, reorg happened in this range | ||
return r.searchInRange(storedBlocks) | ||
} | ||
} | ||
|
||
func (r *ReorgHandler) searchInRange(blocks []queries.GetBlocksInRangeRow) (uint64, []byte, error) { | ||
left, right := 0, len(blocks)-1 | ||
for left <= right { | ||
mid := (left + right) / 2 | ||
block := blocks[mid] | ||
|
||
chainBlock, err := r.client.BlockByNumber( | ||
r.ctx, | ||
big.NewInt(int64(block.BlockNumber)), | ||
) | ||
if err != nil { | ||
return 0, nil, fmt.Errorf("%w %d: %v", ErrGetBlock, block.BlockNumber, err) | ||
} | ||
|
||
if bytes.Equal(block.BlockHash, chainBlock.Hash().Bytes()) { | ||
// Found a matching block, check if next block differs to confirm reorg point | ||
if mid < len(blocks)-1 { | ||
nextBlock := blocks[mid+1] | ||
nextChainBlock, err := r.client.BlockByNumber( | ||
r.ctx, | ||
big.NewInt(int64(nextBlock.BlockNumber)), | ||
) | ||
if err != nil { | ||
return 0, nil, fmt.Errorf("%w %d: %v", ErrGetBlock, nextBlock.BlockNumber, err) | ||
} | ||
|
||
if !bytes.Equal(nextBlock.BlockHash, nextChainBlock.Hash().Bytes()) { | ||
return block.BlockNumber, chainBlock.Hash().Bytes(), nil | ||
} | ||
} else if mid == len(blocks)-1 { | ||
return block.BlockNumber, chainBlock.Hash().Bytes(), nil | ||
} | ||
|
||
// If next block doesn't differ, search upper half | ||
left = mid + 1 | ||
} else { | ||
// If chainBlock differs, search lower half | ||
right = mid - 1 | ||
} | ||
} | ||
|
||
// TODO: This should never happen, start from 0? | ||
return 0, nil, fmt.Errorf("reorg point not found") | ||
} | ||
|
||
func blockRange(from uint64) (startBlock uint64, endBlock uint64) { | ||
endBlock = from | ||
|
||
if endBlock > BLOCK_RANGE_SIZE { | ||
startBlock = endBlock - BLOCK_RANGE_SIZE | ||
} | ||
|
||
return startBlock, endBlock | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package indexer | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_blockRange(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
from uint64 | ||
wantStartBlock uint64 | ||
wantEndBlock uint64 | ||
}{ | ||
{ | ||
name: "block range with subtraction", | ||
from: 1001, | ||
wantStartBlock: 1, | ||
wantEndBlock: 1001, | ||
}, | ||
{ | ||
name: "block range without subtraction", | ||
from: 500, | ||
wantStartBlock: 0, | ||
wantEndBlock: 500, | ||
}, | ||
{ | ||
name: "block range zero", | ||
from: 0, | ||
wantStartBlock: 0, | ||
wantEndBlock: 0, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
startBlock, endBlock := blockRange(tt.from) | ||
assert.Equal(t, tt.wantStartBlock, startBlock) | ||
assert.Equal(t, tt.wantEndBlock, endBlock) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.