Skip to content

Commit

Permalink
Add BlockNumber() to RPC client (#985)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszcl authored Jun 11, 2024
1 parent 5064fc0 commit adbe671
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions client/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,38 +171,46 @@ type CurrentBlockResponse struct {
Result string `json:"result"`
}

func (m *RPCClient) GethSetHead(blocksBack int) error {
// Call "eth_blockNumber" to get the current block number
func (m *RPCClient) BlockNumber() (int64, error) {
rInt, err := rand.Int()
if err != nil {
return err
return -1, err
}
payload := map[string]interface{}{
"jsonrpc": "2.0",
"method": "eth_blockNumber",
"params": []interface{}{},
"id": rInt,
}
bn, err := m.client.R().SetBody(payload).Post(m.URL)
resp, err := m.client.R().SetBody(payload).Post(m.URL)
if err != nil {
return errors.Wrap(err, "eth_blockNumber")
return -1, errors.Wrap(err, "eth_blockNumber")
}

var res *CurrentBlockResponse
if err := json.Unmarshal(bn.Body(), &res); err != nil {
return err
var blockNumberResp *CurrentBlockResponse
if err := json.Unmarshal(resp.Body(), &blockNumberResp); err != nil {
return -1, err
}
bn, err := strconv.ParseInt(blockNumberResp.Result[2:], 16, 64)
if err != nil {
return -1, err
}
decimalLastBlock, err := strconv.ParseInt(res.Result[2:], 16, 64)
return bn, nil
}

func (m *RPCClient) GethSetHead(blocksBack int) error {
decimalLastBlock, err := m.BlockNumber()
if err != nil {
return err
}
moveToBlock := decimalLastBlock - int64(blocksBack)
moveToBlockHex := strconv.FormatInt(moveToBlock, 16)

rInt, err = rand.Int()
rInt, err := rand.Int()
if err != nil {
return err
}
payload = map[string]interface{}{
payload := map[string]interface{}{
"jsonrpc": "2.0",
"method": "debug_setHead",
"params": []interface{}{fmt.Sprintf("0x%s", moveToBlockHex)},
Expand Down

0 comments on commit adbe671

Please sign in to comment.