Skip to content

Commit

Permalink
Cleanup after tx fetching changes
Browse files Browse the repository at this point in the history
- unused API methods are removed
- some unusued code is removed too
- API docs are updated

That's just a portion of clean up that should be done,
but the rest of it will probably happen in different PR
with changes to the way how we watch to chain updates.
  • Loading branch information
rasom committed Feb 9, 2020
1 parent 0b6ad66 commit dc80cb0
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 137 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.43.1
0.43.2
79 changes: 4 additions & 75 deletions services/wallet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,91 +25,20 @@ To correctly start the service two values need to be changed in the config:
API
----------

#### wallet_getTransfers

Returns avaiable transfers in a given range.

##### Parameters

- `start`: `BIGINT` - start of the range
- `end`: `BIGINT` - end of the range. if nil query will return all transfers from start.

##### Examples

```json
{"jsonrpc":"2.0","id":14,"method":"wallet_getTransfers","params":[0,20]}
{"jsonrpc":"2.0","id":14,"method":"wallet_getTransfers","params":[0,null]}
{"jsonrpc":"2.0","id":13,"method":"wallet_getTransfers","params":[0]}
```

##### Returns

List of objects like:

```
[
{
"id": "0xac14e5fb9a81fd7d0517e51e23c4f3a8040459bfe0c4bee97b813db2d0438e2e",
"type": "eth",
"blockNumber": "0x1",
"blockhash": "0x1471b02682f2308ce74314d89009251afb1f2d5dedc6835d069b1ad6edf98257",
"timestamp": "0x5d25a873",
"gasPrice": "0xa",
"gasLimit": "0xf4240",
"gasUsed": "0x5208",
"nonce": "0x0",
"input": "0x",
"txStatus": "0x1",
"txHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"value": "0xde0b6b3a7640000",
"from": "0xd1c9bfa31ae8c085ba4672b165151245b9bfc25e",
"to": "0x9dfc85106d84405a83271c2fe0cdfc1ca311a1f5",
"contract": "0x0000000000000000000000000000000000000000"
},
{
"id": "0x2629ee5f443d558ee4ae9e1cf202d76c04e262051b8d8acde7b766bb9d95068e",
"type": "erc20",
"blockNumber": "0x2",
"blockhash": "0x046ad915b86a5eaa6026c8cdd09ea2f09fd3e603dd6e1ea86e8318f4a4b7d4e0",
"timestamp": "0x5d25a88a",
"gasPrice": "0x1",
"gasLimit": "0xb0b8",
"gasUsed": "0xb0b8",
"nonce": "0x1",
"txStatus": "0x1",
"input": "0xa9059cbb000000000000000000000000f759c6683dfc5dad899eb86529dfaf4d0b25af1b0000000000000000000000000000000000000000000000000000000000000064",
"txHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"value": "0x64",
"from": "0xbd691e87d65b2857de55ac44598161ea135f73f6",
"to": "0xf759c6683dfc5dad899eb86529dfaf4d0b25af1b",
"contract": "0xd2439b0e20823e1e4c08df2d19c3b6a4c5f8f2d1"
}
]
```

##### Examples

```json
{"jsonrpc":"2.0","id":14,"method":"wallet_getTransfers","params":[0,20]}
{"jsonrpc":"2.0","id":14,"method":"wallet_getTransfers","params":[0,null]}
{"jsonrpc":"2.0","id":13,"method":"wallet_getTransfers","params":[0]}
```

#### wallet_getTransfersByAddress

Returns avaiable transfers in a given range.

##### Parameters

- `address`: `HEX` - ethereum address encoded in hex
- `start`: `BIGINT` - start of the range
- `end`: `BIGINT` - end of the range. if nil query will return all transfers from start.
- `toBlock`: `BIGINT` - end of the range. if nil query will return last transfers.
- `limit`: `BIGINT` - limit of returned transfers.

##### Examples

```json
{"jsonrpc":"2.0","id":7,"method":"wallet_getTransfersByAddress","params":["0xb81a6845649fa8c042dfaceb3f7a684873406993","0x0"]}
{"jsonrpc":"2.0","id":7,"method":"wallet_getTransfersByAddress","params":["0xb81a6845649fa8c042dfaceb3f7a684873406993","0x0","0x5"]}
```

##### Returns
Expand Down Expand Up @@ -204,4 +133,4 @@ were found.
]
}
}
```
```
67 changes: 10 additions & 57 deletions services/wallet/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,69 +24,22 @@ type API struct {
s *Service
}

// GetTransfers returns transfers in range of blocks. If `end` is nil all transfers from `start` will be returned.
// TODO(dshulyak) benchmark loading many transfers from database. We can avoid json unmarshal/marshal if we will
// read header, tx and receipt as a raw json.
func (api *API) GetTransfers(ctx context.Context, start, end *hexutil.Big) ([]TransferView, error) {
log.Debug("call to get transfers", "start", start, "end", end)
if start == nil {
return nil, errors.New("start of the query must be provided. use 0 if you want to load all transfers")
}
if api.s.db == nil {
return nil, ErrServiceNotInitialized
}
rst, err := api.s.db.GetTransfers((*big.Int)(start), (*big.Int)(end))
if err != nil {
return nil, err
}
log.Debug("result from database for transfers", "start", start, "end", end, "len", len(rst))
return castToTransferViews(rst), nil
}

type StatsView struct {
BlocksStats map[int64]int64 `json:"blocksStats"`
TransfersCount int64 `json:"transfersCount"`
}

// GetTransfersFromBlock
func (api *API) GetTransfersFromBlock(ctx context.Context, address common.Address, block *hexutil.Big) ([]TransferView, error) {
log.Debug("[WalletAPI:: GetTransfersFromBlock] get transfers from block", "address", address, "block", block)
// GetTransfersByAddress returns transfers for a single address
func (api *API) GetTransfersByAddress(ctx context.Context, address common.Address, toBlock, limit *hexutil.Big) ([]TransferView, error) {
log.Debug("[WalletAPI:: GetTransfersByAddress] get transfers for an address", "address", address, "block", toBlock, "limit", limit)
if api.s.db == nil {
log.Error("[WalletAPI:: GetTransfersByAddress] db is not initialized")
return nil, ErrServiceNotInitialized
}

blocksByAddress := make(map[common.Address][]*big.Int)
blocksByAddress[address] = []*big.Int{block.ToInt()}

txCommand := &loadTransfersCommand{
accounts: []common.Address{address},
db: api.s.db,
chain: api.s.reactor.chain,
client: api.s.client,
blocksByAddress: blocksByAddress,
var toBlockBN *big.Int
if toBlock != nil {
toBlockBN = toBlock.ToInt()
}

err := txCommand.Command()(ctx)
if err != nil {
return nil, err
}

rst, err := api.s.db.GetTransfersInRange(address, block.ToInt(), block.ToInt())
if err != nil {
return nil, err
}

return castToTransferViews(rst), nil
}

// GetTransfersByAddress returns transfers for a single address
func (api *API) GetTransfersByAddress(ctx context.Context, address common.Address, beforeBlock, limit *hexutil.Big) ([]TransferView, error) {
log.Info("call to get transfers for an address", "address", address, "block", beforeBlock, "limit", limit)
if api.s.db == nil {
return nil, ErrServiceNotInitialized
}
rst, err := api.s.db.GetTransfersByAddress(address, beforeBlock.ToInt(), limit.ToInt().Int64())
rst, err := api.s.db.GetTransfersByAddress(address, toBlockBN, limit.ToInt().Int64())
if err != nil {
log.Error("[WalletAPI:: GetTransfersByAddress] can't fetch transfers", "err", err)
return nil, err
}

Expand Down Expand Up @@ -142,7 +95,7 @@ func (api *API) GetTransfersByAddress(ctx context.Context, address common.Addres
if err != nil {
return nil, err
}
rst, err = api.s.db.GetTransfersByAddress(address, beforeBlock.ToInt(), limit.ToInt().Int64())
rst, err = api.s.db.GetTransfersByAddress(address, toBlockBN, limit.ToInt().Int64())
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions services/wallet/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,11 @@ func (db *Database) GetTransfersInRange(address common.Address, start, end *big.
}

// GetTransfersByAddress loads transfers for a given address between two blocks.
func (db *Database) GetTransfersByAddress(address common.Address, fromBlock *big.Int, limit int64) (rst []Transfer, err error) {
func (db *Database) GetTransfersByAddress(address common.Address, toBlock *big.Int, limit int64) (rst []Transfer, err error) {
query := newTransfersQuery().
FilterNetwork(db.network).
FilterAddress(address).
FilterEnd(fromBlock).
FilterEnd(toBlock).
FilterLoaded(1).
Limit(limit)

Expand All @@ -250,7 +250,7 @@ func (db *Database) GetTransfersByAddress(address common.Address, fromBlock *big
return query.Scan(rows)
}

// GetTransfersByAddress loads transfers for a given address between two blocks.
// GetBlocksByAddress loads blocks for a given address.
func (db *Database) GetBlocksByAddress(address common.Address, limit int) (rst []*big.Int, err error) {
query := `SELECT blk_number FROM blocks
WHERE address = ? AND network_id = ? AND loaded = 0
Expand Down
2 changes: 1 addition & 1 deletion services/wallet/transfers_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (q *transfersQuery) FilterBlockHash(blockHash common.Hash) *transfersQuery
}

func (q *transfersQuery) Limit(pageSize int64) *transfersQuery {
q.buf.WriteString(" ORDER BY blk_number DESC ")
q.buf.WriteString(" ORDER BY blk_number DESC, hash ASC ")
q.buf.WriteString(" LIMIT ?")
q.args = append(q.args, pageSize)
return q
Expand Down

0 comments on commit dc80cb0

Please sign in to comment.