Skip to content

Commit

Permalink
Merge branch 'mnee' of https://github.com/shruggr/1sat-indexer into mnee
Browse files Browse the repository at this point in the history
  • Loading branch information
shruggr committed Mar 1, 2025
2 parents e4bd097 + ba7c796 commit a001317
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
4 changes: 1 addition & 3 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [


{
"name": "Launch file",
"type": "go",
Expand All @@ -18,7 +16,7 @@
// "-t=b1d6decfecfebadec22c9e98109aad3e58f7e4ac799024d1e5bf3afe3b95d0f2",
// "-s=783968",
// "-m=1"
"-p=8083"
"-p=8088"
// "-tag=locks",
// "-t=9c554bf80570ba371a558127612960e885de846a607329277452e668a4be6c76",
// "-s=801000",
Expand Down
49 changes: 42 additions & 7 deletions blk/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ func BlockByHeight(ctx context.Context, height uint32) (*BlockHeader, error) {
}

func BlockByHash(ctx context.Context, hash string) (*BlockHeader, error) {
if headerState, err := GetBlockState(ctx, hash); err != nil {
return nil, err
} else {
header := &headerState.Header
header.Height = headerState.Height
return header, nil
}
}

func GetBlockState(ctx context.Context, hash string) (*BlockHeaderState, error) {
headerState := &BlockHeaderState{}
client := &http.Client{}
req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/v1/chain/header/state/%s", BLOCK_API, hash), nil)
Expand All @@ -108,17 +118,16 @@ func BlockByHash(ctx context.Context, hash string) (*BlockHeader, error) {
if err := json.NewDecoder(res.Body).Decode(headerState); err != nil {
return nil, err
}
header := &headerState.Header
header.Height = headerState.Height
// header.ChainWork = headerState.ChainWork
return header, nil
}
return headerState, nil
}

func Blocks(ctx context.Context, fromBlock uint32, count uint) ([]*BlockHeader, error) {
headers := make([]*BlockHeader, 0, count)
client := &http.Client{}
url := fmt.Sprintf("%s/api/v1/chain/header/byHeight?height=%d&count=%d", BLOCK_API, fromBlock, count)
byHash := make(map[string]*BlockHeader)
var results []*BlockHeader
if req, err := http.NewRequest("GET", url, nil); err != nil {
return nil, err
} else {
Expand All @@ -129,11 +138,37 @@ func Blocks(ctx context.Context, fromBlock uint32, count uint) ([]*BlockHeader,
defer res.Body.Close()
if err := json.NewDecoder(res.Body).Decode(&headers); err != nil {
return nil, err
} else if len(headers) == 0 {
return headers, nil
}
for _, header := range headers {
byHash[header.Hash.String()] = header
}
for i, header := range headers {
header.Height = fromBlock + uint32(i)
for i := len(headers) - 1; i >= 0; i-- {
lastHeader := headers[i]
if state, err := GetBlockState(ctx, lastHeader.Hash.String()); err != nil {
return nil, err
} else if state.State == "LONGEST_CHAIN" {
lastHeight := state.Height
results = make([]*BlockHeader, lastHeight-fromBlock+1)
block := &state.Header
block.Height = state.Height
results[block.Height-fromBlock] = block
for {
parent := block
if block = byHash[parent.PreviousBlock.String()]; block != nil {
block.Height = parent.Height - 1
results[block.Height-fromBlock] = block
} else {
return results, nil
}
}
} else {
continue
}
}
return headers, nil

return results, nil
}
}
}
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
go build -o audit.run cmd/audit/audit.go
go build -o bsv21.run cmd/bsv21/bsv21.go
go build -o ingest.run cmd/ingest/ingest.go
go build -o origin.run cmd/origin/origin.go
go build -o owners.run cmd/owner-sync/owner-sync.go
go build -o server.run cmd/server/server.go
go build -o subscribe.run cmd/subscribe/subscribe.go
go build -o bsv21.run cmd/bsv21/bsv21.go
15 changes: 5 additions & 10 deletions jb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ func LoadProof(ctx context.Context, txid string) (proof *transaction.MerklePath,
if len(prf) == 0 && JB != nil {
// start := time.Now()
url := fmt.Sprintf("%s/v1/transaction/proof/%s/bin", JUNGLEBUS, txid)
// log.Println("Requesting:", url)
inflightM.Lock()
inflight, ok := inflightMap[url]
if !ok {
Expand Down Expand Up @@ -285,22 +286,16 @@ func GetSpend(outpoint string) (spend string, err error) {
}

func BuildTxBEEF(ctx context.Context, txid string) (tx *transaction.Transaction, err error) {
loadedTransactions := map[string]*transaction.Transaction{}
log.Println("Building BEEF", txid)
if tx, err = LoadTx(ctx, txid, true); err != nil {
return nil, err
} else if tx.MerklePath == nil {
for _, in := range tx.Inputs {
if in.SourceTransaction == nil {
sourceTxid := in.SourceTXID.String()
if sourceTx, ok := loadedTransactions[sourceTxid]; !ok {
if sourceTx, err = LoadTx(ctx, sourceTxid, false); err != nil {
return nil, err
} else {
loadedTransactions[sourceTxid] = sourceTx
in.SourceTransaction = sourceTx
}
} else {
in.SourceTransaction = sourceTx
log.Println("Recursing BEEF", sourceTxid, "from", txid)
if in.SourceTransaction, err = BuildTxBEEF(ctx, sourceTxid); err != nil {
return nil, err
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion server/routes/blocks/ctrl.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func GetBlockByHeight(c *fiber.Ctx) error {
}

func GetBlockByHash(c *fiber.Ctx) error {
if block, err := blk.BlockByHash(c.Context(), c.Params("hashOrHeight")); err != nil {
if block, err := blk.BlockByHash(c.Context(), c.Params("hash")); err != nil {
return err
} else if block == nil {
return c.SendStatus(404)
Expand Down

0 comments on commit a001317

Please sign in to comment.