forked from qtumproject/janus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eth_blockNumber.go
51 lines (43 loc) · 1.33 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/kaonone/eth-rpc-gate/pkg/eth"
"github.com/kaonone/eth-rpc-gate/pkg/kaon"
"github.com/labstack/echo"
)
// ProxyETHBlockNumber implements ETHProxy
type ProxyETHBlockNumber struct {
*kaon.Kaon
}
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) {
kaonresp, err := p.Kaon.GetBlockCount(c.Request().Context())
if err != nil {
if retries > 0 && strings.Contains(err.Error(), kaon.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())
}
// kaon res -> eth res
return p.ToResponse(kaonresp), nil
}
func (p *ProxyETHBlockNumber) ToResponse(kaonresp *kaon.GetBlockCountResponse) *eth.BlockNumberResponse {
hexVal := hexutil.EncodeBig(kaonresp.Int)
ethresp := eth.BlockNumberResponse(hexVal)
return ðresp
}