From 76664cdbf37f008865586c4d716602cc00bf8398 Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Thu, 10 Oct 2024 00:58:19 +0200 Subject: [PATCH] Refactor ethCall method to rpcCall --- bchain/basechain.go | 4 +-- bchain/coins/blockchain.go | 8 +++--- bchain/coins/eth/contract.go | 14 +++++----- bchain/coins/eth/stakingpool.go | 2 +- bchain/types.go | 2 +- blockbook-api.ts | 4 +-- build/tools/typescriptify/typescriptify.go | 4 +-- docs/env.md | 2 +- server/public_ethereumtype_test.go | 6 ++--- server/public_test.go | 6 ++--- server/websocket.go | 30 +++++++++++----------- server/ws_types.go | 4 +-- static/test-websocket.html | 24 ++++++++--------- tests/dbtestdata/fakechain_ethereumtype.go | 4 +-- 14 files changed, 57 insertions(+), 57 deletions(-) diff --git a/bchain/basechain.go b/bchain/basechain.go index df494531..c84f907f 100644 --- a/bchain/basechain.go +++ b/bchain/basechain.go @@ -77,7 +77,7 @@ func (b *BaseChain) EthereumTypeGetStakingPoolsData(addrDesc AddressDescriptor) return nil, errors.New("not supported") } -// EthereumTypeEthCall calls eth_call with given data and to address -func (b *BaseChain) EthereumTypeEthCall(data, to, from string) (string, error) { +// EthereumTypeRpcCall calls eth_call with given data and to address +func (b *BaseChain) EthereumTypeRpcCall(data, to, from string) (string, error) { return "", errors.New("not supported") } diff --git a/bchain/coins/blockchain.go b/bchain/coins/blockchain.go index cdc877ca..f5dcdc03 100644 --- a/bchain/coins/blockchain.go +++ b/bchain/coins/blockchain.go @@ -342,10 +342,10 @@ func (c *blockChainWithMetrics) EthereumTypeGetStakingPoolsData(addrDesc bchain. return c.b.EthereumTypeGetStakingPoolsData(addrDesc) } -// EthereumTypeEthCall calls eth_call with given data and to address -func (c *blockChainWithMetrics) EthereumTypeEthCall(data, to, from string) (v string, err error) { - defer func(s time.Time) { c.observeRPCLatency("EthereumTypeEthCall", s, err) }(time.Now()) - return c.b.EthereumTypeEthCall(data, to, from) +// EthereumTypeRpcCall calls eth_call with given data and to address +func (c *blockChainWithMetrics) EthereumTypeRpcCall(data, to, from string) (v string, err error) { + defer func(s time.Time) { c.observeRPCLatency("EthereumTypeRpcCall", s, err) }(time.Now()) + return c.b.EthereumTypeRpcCall(data, to, from) } type mempoolWithMetrics struct { diff --git a/bchain/coins/eth/contract.go b/bchain/coins/eth/contract.go index 0d3bd237..efc7439a 100644 --- a/bchain/coins/eth/contract.go +++ b/bchain/coins/eth/contract.go @@ -273,8 +273,8 @@ func contractGetTransfersFromTx(tx *bchain.RpcTransaction) (bchain.TokenTransfer return r, nil } -// EthereumTypeEthCall calls eth_call with given data and to address -func (b *EthereumRPC) EthereumTypeEthCall(data, to, from string) (string, error) { +// EthereumTypeRpcCall calls eth_call with given data and to address +func (b *EthereumRPC) EthereumTypeRpcCall(data, to, from string) (string, error) { ctx, cancel := context.WithTimeout(context.Background(), b.Timeout) defer cancel() var r string @@ -294,7 +294,7 @@ func (b *EthereumRPC) EthereumTypeEthCall(data, to, from string) (string, error) func (b *EthereumRPC) fetchContractInfo(address string) (*bchain.ContractInfo, error) { var contract bchain.ContractInfo - data, err := b.EthereumTypeEthCall(contractNameSignature, address, "") + data, err := b.EthereumTypeRpcCall(contractNameSignature, address, "") if err != nil { // ignore the error from the eth_call - since geth v1.9.15 they changed the behavior // and returning error "execution reverted" for some non contract addresses @@ -305,14 +305,14 @@ func (b *EthereumRPC) fetchContractInfo(address string) (*bchain.ContractInfo, e } name := strings.TrimSpace(parseSimpleStringProperty(data)) if name != "" { - data, err = b.EthereumTypeEthCall(contractSymbolSignature, address, "") + data, err = b.EthereumTypeRpcCall(contractSymbolSignature, address, "") if err != nil { // glog.Warning(errors.Annotatef(err, "Contract SymbolSignature %v", address)) return nil, nil // return nil, errors.Annotatef(err, "erc20SymbolSignature %v", address) } symbol := strings.TrimSpace(parseSimpleStringProperty(data)) - data, _ = b.EthereumTypeEthCall(contractDecimalsSignature, address, "") + data, _ = b.EthereumTypeRpcCall(contractDecimalsSignature, address, "") // if err != nil { // glog.Warning(errors.Annotatef(err, "Contract DecimalsSignature %v", address)) // // return nil, errors.Annotatef(err, "erc20DecimalsSignature %v", address) @@ -345,7 +345,7 @@ func (b *EthereumRPC) EthereumTypeGetErc20ContractBalance(addrDesc, contractDesc addr := hexutil.Encode(addrDesc)[2:] contract := hexutil.Encode(contractDesc) req := contractBalanceOfSignature + "0000000000000000000000000000000000000000000000000000000000000000"[len(addr):] + addr - data, err := b.EthereumTypeEthCall(req, contract, "") + data, err := b.EthereumTypeRpcCall(req, contract, "") if err != nil { return nil, err } @@ -369,7 +369,7 @@ func (b *EthereumRPC) GetTokenURI(contractDesc bchain.AddressDescriptor, tokenID } // try ERC721 tokenURI method and ERC1155 uri method for _, method := range []string{erc721TokenURIMethodSignature, erc1155URIMethodSignature} { - data, err := b.EthereumTypeEthCall(method+id, address, "") + data, err := b.EthereumTypeRpcCall(method+id, address, "") if err == nil && data != "" { uri := parseSimpleStringProperty(data) // try to sanitize the URI returned from the contract diff --git a/bchain/coins/eth/stakingpool.go b/bchain/coins/eth/stakingpool.go index 4a40057a..8b7cf7b8 100644 --- a/bchain/coins/eth/stakingpool.go +++ b/bchain/coins/eth/stakingpool.go @@ -61,7 +61,7 @@ func isZeroBigInt(b *big.Int) bool { func (b *EthereumRPC) everstakeBalanceTypeContractCall(signature, addr, contract string) (string, error) { req := signature + "0000000000000000000000000000000000000000000000000000000000000000"[len(addr):] + addr - return b.EthereumTypeEthCall(req, contract, "") + return b.EthereumTypeRpcCall(req, contract, "") } func (b *EthereumRPC) everstakeContractCallSimpleNumeric(signature, addr, contract string) (*big.Int, error) { diff --git a/bchain/types.go b/bchain/types.go index ee29be9d..3fd2dcfe 100644 --- a/bchain/types.go +++ b/bchain/types.go @@ -335,7 +335,7 @@ type BlockChain interface { EthereumTypeGetErc20ContractBalance(addrDesc, contractDesc AddressDescriptor) (*big.Int, error) EthereumTypeGetSupportedStakingPools() []string EthereumTypeGetStakingPoolsData(addrDesc AddressDescriptor) ([]StakingPoolData, error) - EthereumTypeEthCall(data, to, from string) (string, error) + EthereumTypeRpcCall(data, to, from string) (string, error) GetTokenURI(contractDesc AddressDescriptor, tokenID *big.Int) (string, error) } diff --git a/blockbook-api.ts b/blockbook-api.ts index ca722a37..295acff9 100644 --- a/blockbook-api.ts +++ b/blockbook-api.ts @@ -448,12 +448,12 @@ export interface WsMempoolFiltersReq { fromTimestamp: number; M?: number; } -export interface WsEthCallReq { +export interface WsRpcCallReq { from?: string; to: string; data: string; } -export interface WsEthCallRes { +export interface WsRpcCallRes { data: string; } export interface MempoolTxidFilterEntries { diff --git a/build/tools/typescriptify/typescriptify.go b/build/tools/typescriptify/typescriptify.go index 5e24f620..069572cb 100644 --- a/build/tools/typescriptify/typescriptify.go +++ b/build/tools/typescriptify/typescriptify.go @@ -60,8 +60,8 @@ func main() { t.Add(server.WsFiatRatesForTimestampsReq{}) t.Add(server.WsFiatRatesTickersListReq{}) t.Add(server.WsMempoolFiltersReq{}) - t.Add(server.WsEthCallReq{}) - t.Add(server.WsEthCallRes{}) + t.Add(server.WsRpcCallReq{}) + t.Add(server.WsRpcCallRes{}) t.Add(bchain.MempoolTxidFilterEntries{}) err := t.ConvertToFile("blockbook-api.ts") diff --git a/docs/env.md b/docs/env.md index 98383453..8d95dc98 100644 --- a/docs/env.md +++ b/docs/env.md @@ -8,4 +8,4 @@ Some behavior of Blockbook can be modified by environment variables. The variabl - `COINGECKO_API_KEY` or `_COINGECKO_API_KEY` - API key for making requests to CoinGecko in the paid tier. -- `_ALLOWED_ETH_CALL_CONTRACTS` - Contract addresses for which `ethCall` websocket requests can be made, as a comma-separated list. If omitted, `ethCall` is enabled for all addresses. +- `_ALLOWED_RPC_CALL_TO` - Addresses to which `rpcCall` websocket requests can be made, as a comma-separated list. If omitted, `rpcCall` is enabled for all addresses. diff --git a/server/public_ethereumtype_test.go b/server/public_ethereumtype_test.go index b7a6c6c9..5a953533 100644 --- a/server/public_ethereumtype_test.go +++ b/server/public_ethereumtype_test.go @@ -143,10 +143,10 @@ var websocketTestsEthereumType = []websocketTest{ want: `{"id":"0","data":{"name":"Fakecoin","shortcut":"FAKE","network":"FAKE","decimals":18,"version":"unknown","bestHeight":4321001,"bestHash":"0x2b57e15e93a0ed197417a34c2498b7187df79099572c04a6b6e6ff418f74e6ee","block0Hash":"","testnet":true,"backend":{"version":"001001","subversion":"/Fakecoin:0.0.1/"}}}`, }, { - name: "websocket ethCall", + name: "websocket rpcCall", req: websocketReq{ - Method: "ethCall", - Params: WsEthCallReq{ + Method: "rpcCall", + Params: WsRpcCallReq{ To: "0xcdA9FC258358EcaA88845f19Af595e908bb7EfE9", Data: "0x4567", }, diff --git a/server/public_test.go b/server/public_test.go index 9b8ed215..01e34e9b 100644 --- a/server/public_test.go +++ b/server/public_test.go @@ -1485,10 +1485,10 @@ var websocketTestsBitcoinType = []websocketTest{ want: `{"id":"43","data":{"P":0,"M":1,"zeroedKey":false,"blockFilter":""}}`, }, { - name: "websocket ethCall", + name: "websocket rpcCall", req: websocketReq{ - Method: "ethCall", - Params: WsEthCallReq{ + Method: "rpcCall", + Params: WsRpcCallReq{ To: "0x123", Data: "0x456", }, diff --git a/server/websocket.go b/server/websocket.go index 4a0745b4..f9093eeb 100644 --- a/server/websocket.go +++ b/server/websocket.go @@ -71,7 +71,7 @@ type WebsocketServer struct { fiatRatesSubscriptions map[string]map[*websocketChannel]string fiatRatesTokenSubscriptions map[*websocketChannel][]string fiatRatesSubscriptionsLock sync.Mutex - allowedEthCallContracts map[string]struct{} + allowedRpcCallTo map[string]struct{} } // NewWebsocketServer creates new websocket interface to blockbook and returns its handle @@ -107,13 +107,13 @@ func NewWebsocketServer(db *db.RocksDB, chain bchain.BlockChain, mempool bchain. fiatRatesSubscriptions: make(map[string]map[*websocketChannel]string), fiatRatesTokenSubscriptions: make(map[*websocketChannel][]string), } - envEthCall := os.Getenv(strings.ToUpper(is.CoinShortcut) + "_ALLOWED_ETH_CALL_CONTRACTS") - if envEthCall != "" { - s.allowedEthCallContracts = make(map[string]struct{}) - for _, c := range strings.Split(envEthCall, ",") { - s.allowedEthCallContracts[strings.ToLower(c)] = struct{}{} + envRpcCall := os.Getenv(strings.ToUpper(is.CoinShortcut) + "_ALLOWED_RPC_CALL_TO") + if envRpcCall != "" { + s.allowedRpcCallTo = make(map[string]struct{}) + for _, c := range strings.Split(envRpcCall, ",") { + s.allowedRpcCallTo[strings.ToLower(c)] = struct{}{} } - glog.Info("Support of ethCall for these contracts: ", envEthCall) + glog.Info("Support of rpcCall for these contracts: ", envRpcCall) } return s, nil } @@ -401,11 +401,11 @@ var requestHandlers = map[string]func(*WebsocketServer, *websocketChannel, *WsRe } return }, - "ethCall": func(s *WebsocketServer, c *websocketChannel, req *WsReq) (rv interface{}, err error) { - r := WsEthCallReq{} + "rpcCall": func(s *WebsocketServer, c *websocketChannel, req *WsReq) (rv interface{}, err error) { + r := WsRpcCallReq{} err = json.Unmarshal(req.Params, &r) if err == nil { - rv, err = s.ethCall(&r) + rv, err = s.rpcCall(&r) } return }, @@ -765,18 +765,18 @@ func (s *WebsocketServer) getBlockFiltersBatch(r *WsBlockFiltersBatchReq) (res i }, nil } -func (s *WebsocketServer) ethCall(r *WsEthCallReq) (*WsEthCallRes, error) { - if s.allowedEthCallContracts != nil { - _, ok := s.allowedEthCallContracts[strings.ToLower(r.To)] +func (s *WebsocketServer) rpcCall(r *WsRpcCallReq) (*WsRpcCallRes, error) { + if s.allowedRpcCallTo != nil { + _, ok := s.allowedRpcCallTo[strings.ToLower(r.To)] if !ok { return nil, errors.New("Not supported") } } - data, err := s.chain.EthereumTypeEthCall(r.Data, r.To, r.From) + data, err := s.chain.EthereumTypeRpcCall(r.Data, r.To, r.From) if err != nil { return nil, err } - return &WsEthCallRes{Data: data}, nil + return &WsRpcCallRes{Data: data}, nil } type subscriptionResponse struct { diff --git a/server/ws_types.go b/server/ws_types.go index 248e1d32..f49f02ad 100644 --- a/server/ws_types.go +++ b/server/ws_types.go @@ -139,12 +139,12 @@ type WsFiatRatesTickersListReq struct { Token string `json:"token,omitempty"` } -type WsEthCallReq struct { +type WsRpcCallReq struct { From string `json:"from,omitempty"` To string `json:"to"` Data string `json:"data"` } -type WsEthCallRes struct { +type WsRpcCallRes struct { Data string `json:"data"` } diff --git a/static/test-websocket.html b/static/test-websocket.html index 4f2cc0f6..e4e2b22b 100644 --- a/static/test-websocket.html +++ b/static/test-websocket.html @@ -536,18 +536,18 @@ }); } - function ethCall() { - const from = document.getElementById('ethCallFrom').value.trim(); - const to = document.getElementById('ethCallTo').value.trim(); - const data = document.getElementById('ethCallData').value.trim(); - const method = 'ethCall'; + function rpcCall() { + const from = document.getElementById('rpcCallFrom').value.trim(); + const to = document.getElementById('rpcCallTo').value.trim(); + const data = document.getElementById('rpcCallData').value.trim(); + const method = 'rpcCall'; const params = { from, to, data, }; send(method, params, function (result) { - document.getElementById('ethCallResult').innerText = JSON.stringify( + document.getElementById('rpcCallResult').innerText = JSON.stringify( result, ).replace(/,/g, ', '); }); @@ -1118,8 +1118,8 @@
@@ -1127,7 +1127,7 @@
-
+
diff --git a/tests/dbtestdata/fakechain_ethereumtype.go b/tests/dbtestdata/fakechain_ethereumtype.go index 09abdfe9..a8469271 100644 --- a/tests/dbtestdata/fakechain_ethereumtype.go +++ b/tests/dbtestdata/fakechain_ethereumtype.go @@ -134,8 +134,8 @@ func (c *fakeBlockChainEthereumType) EthereumTypeGetErc20ContractBalance(addrDes return big.NewInt(1000000000 + int64(addrDesc[0])*1000 + int64(contractDesc[0])), nil } -// EthereumTypeEthCall calls eth_call with given data and to address -func (c *fakeBlockChainEthereumType) EthereumTypeEthCall(data, to, from string) (string, error) { +// EthereumTypeRpcCall calls eth_call with given data and to address +func (c *fakeBlockChainEthereumType) EthereumTypeRpcCall(data, to, from string) (string, error) { return data + "abcd", nil }