forked from qtumproject/janus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eth_blockNumber.go
51 lines (43 loc) · 1.32 KB
/
eth_blockNumber.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package transformer
import (
"strings"
"time"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/labstack/echo"
"github.com/qtumproject/janus/pkg/eth"
"github.com/qtumproject/janus/pkg/qtum"
)
// ProxyETHBlockNumber implements ETHProxy
type ProxyETHBlockNumber struct {
*qtum.Qtum
}
func (p *ProxyETHBlockNumber) Method() string {
return "eth_blockNumber"
}
func (p *ProxyETHBlockNumber) Request(_ *eth.JSONRPCRequest, c echo.Context) (interface{}, eth.JSONRPCError) {
return p.request(c, 5)
}
func (p *ProxyETHBlockNumber) request(c echo.Context, retries int) (*eth.BlockNumberResponse, eth.JSONRPCError) {
qtumresp, err := p.Qtum.GetBlockCount(c.Request().Context())
if err != nil {
if retries > 0 && strings.Contains(err.Error(), qtum.ErrTryAgain.Error()) {
ctx := c.Request().Context()
t := time.NewTimer(500 * time.Millisecond)
select {
case <-ctx.Done():
return nil, eth.NewCallbackError(err.Error())
case <-t.C:
// fallthrough
}
return p.request(c, retries-1)
}
return nil, eth.NewCallbackError(err.Error())
}
// qtum res -> eth res
return p.ToResponse(qtumresp), nil
}
func (p *ProxyETHBlockNumber) ToResponse(qtumresp *qtum.GetBlockCountResponse) *eth.BlockNumberResponse {
hexVal := hexutil.EncodeBig(qtumresp.Int)
ethresp := eth.BlockNumberResponse(hexVal)
return ðresp
}