Skip to content

Commit

Permalink
feature: cmd for closest bhs block (#10799)
Browse files Browse the repository at this point in the history
* feature: cmd for closest bhs block

* use closest block in batch backwards mode if start block is not provided

* chore: additional logging

---------

Co-authored-by: jinhoonbang <[email protected]>
  • Loading branch information
makramkd and jinhoonbang authored Sep 27, 2023
1 parent 57fec36 commit 7b1ae85
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
28 changes: 22 additions & 6 deletions core/scripts/vrfv2/testnet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,12 +418,19 @@ func main() {
}

if *startBlock == -1 {
tx, err2 := bhs.StoreEarliest(e.Owner)
helpers.PanicErr(err2)
receipt := helpers.ConfirmTXMined(context.Background(), e.Ec, tx, e.ChainID, "Store Earliest")
// storeEarliest will store receipt block number minus 256 which is the earliest block
// the blockhash() instruction will work on.
*startBlock = receipt.BlockNumber.Int64() - 256
closestBlock, err2 := scripts.ClosestBlock(e, common.HexToAddress(*batchAddr), uint64(*endBlock), uint64(*batchSize))
// found a block with blockhash stored that's more recent that end block
if err2 == nil {
*startBlock = int64(closestBlock)
} else {
fmt.Println("encountered error while looking for closest block:", err2)
tx, err2 := bhs.StoreEarliest(e.Owner)
helpers.PanicErr(err2)
receipt := helpers.ConfirmTXMined(context.Background(), e.Ec, tx, e.ChainID, "Store Earliest")
// storeEarliest will store receipt block number minus 256 which is the earliest block
// the blockhash() instruction will work on.
*startBlock = receipt.BlockNumber.Int64() - 256
}
}

// Check if the provided start block is in the BHS. If it's not, print out an appropriate
Expand Down Expand Up @@ -476,6 +483,7 @@ func main() {
helpers.PanicErr(err)

fmt.Println("received receipt, continuing")
fmt.Println("there are", len(blockRange)-j, "blocks left to store")
}
fmt.Println("done")
case "latest-head":
Expand Down Expand Up @@ -1330,6 +1338,14 @@ func main() {
blockNumber := cmd.Int("block-number", -1, "block number")
helpers.ParseArgs(cmd, os.Args[2:])
_ = helpers.CalculateLatestBlockHeader(e, *blockNumber)
case "closest-block":
cmd := flag.NewFlagSet("closest-block", flag.ExitOnError)
blockNumber := cmd.Uint64("block-number", 0, "block number")
batchBHSAddress := cmd.String("batch-bhs-address", "", "address of the batch blockhash store")
batchSize := cmd.Uint64("batch-size", 100, "batch size")
helpers.ParseArgs(cmd, os.Args[2:], "block-number", "batch-bhs-address")
_, err := scripts.ClosestBlock(e, common.HexToAddress(*batchBHSAddress), *blockNumber, *batchSize)
helpers.PanicErr(err)
case "wrapper-universe-deploy":
scripts.DeployWrapperUniverse(e)
default:
Expand Down
41 changes: 40 additions & 1 deletion core/scripts/vrfv2/testnet/scripts/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package scripts
import (
"context"
"encoding/hex"
"errors"
"fmt"
"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/vrf_load_test_with_metrics"
"math/big"

"github.com/smartcontractkit/chainlink/v2/core/gethwrappers/generated/vrf_load_test_with_metrics"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"

helpers "github.com/smartcontractkit/chainlink/core/scripts/common"
Expand Down Expand Up @@ -212,3 +215,39 @@ func EoaLoadTestConsumerWithMetricsDeploy(e helpers.Environment, consumerCoordin
helpers.PanicErr(err)
return helpers.ConfirmContractDeployed(context.Background(), e.Ec, tx, e.ChainID)
}

func ClosestBlock(e helpers.Environment, batchBHSAddress common.Address, blockMissingBlockhash uint64, batchSize uint64) (uint64, error) {
batchBHS, err := batch_blockhash_store.NewBatchBlockhashStore(batchBHSAddress, e.Ec)
if err != nil {
return 0, err
}
startBlock := blockMissingBlockhash + 1
endBlock := startBlock + batchSize
for {
latestBlock, err := e.Ec.HeaderByNumber(context.Background(), nil)
if err != nil {
return 0, err
}
if latestBlock.Number.Uint64() < endBlock {
return 0, errors.New("closest block with blockhash not found")
}
var blockRange []*big.Int
for i := startBlock; i <= endBlock; i++ {
blockRange = append(blockRange, big.NewInt(int64(i)))
}
fmt.Println("Searching range", startBlock, "-", endBlock, "inclusive")
hashes, err := batchBHS.GetBlockhashes(nil, blockRange)
if err != nil {
return 0, err
}
for i, hash := range hashes {
if hash != (common.Hash{}) {
fmt.Println("found closest block:", startBlock+uint64(i), "hash:", hexutil.Encode(hash[:]))
fmt.Println("distance from missing block:", startBlock+uint64(i)-blockMissingBlockhash)
return startBlock + uint64(i), nil
}
}
startBlock = endBlock + 1
endBlock = startBlock + batchSize
}
}

0 comments on commit 7b1ae85

Please sign in to comment.