Skip to content

Commit

Permalink
Implement more block & transaction related JSON-RPC endpoints
Browse files Browse the repository at this point in the history
These include:
- eth_getBlockByHash
- eth_getBlockTransactionCountByHash
- eth_getBlockTransactionCountByNumber
- eth_getTransactionByBlockHashAndIndex
- eth_getTransactionByBlockNumberAndIndex
  • Loading branch information
m-Peter committed Feb 7, 2024
1 parent 036e395 commit 99170a1
Show file tree
Hide file tree
Showing 6 changed files with 417 additions and 107 deletions.
95 changes: 53 additions & 42 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,25 +341,22 @@ func (s *BlockChainAPI) GetTransactionByBlockHashAndIndex(
blockHash common.Hash,
index hexutil.Uint,
) *RPCTransaction {
to := common.HexToAddress("0xf02c1c8e6114b1dbe8937a39260b5b0a374432bb")
txIndex := uint64(64)
block, err := s.Store.GetBlockByHash(ctx, blockHash)
if err != nil {
return nil
}

tx := &RPCTransaction{
BlockHash: (*common.Hash)(&blockHash),
BlockNumber: (*hexutil.Big)(big.NewInt(6139707)),
From: common.HexToAddress("0xa7d9ddbe1f17865597fbd27ec712455208b6b76d"),
Gas: hexutil.Uint64(50000),
GasPrice: (*hexutil.Big)(big.NewInt(20000000000)),
Hash: common.HexToHash("0x88df016429689c079f3b2f6ad39fa052532c56795b733da78a91ebe6a713944b"),
Input: hexutil.Bytes("0x68656c6c6f21"),
Nonce: hexutil.Uint64(21),
To: &to,
TransactionIndex: (*hexutil.Uint64)(&txIndex),
Value: (*hexutil.Big)(big.NewInt(4290000000000000)),
V: (*hexutil.Big)(big.NewInt(37)),
R: (*hexutil.Big)(big.NewInt(150)),
S: (*hexutil.Big)(big.NewInt(250)),
highestIndex := len(block.TransactionHashes) - 1
if index > hexutil.Uint(highestIndex) {
return nil
}

txHash := common.HexToHash(block.TransactionHashes[index])
tx, err := s.GetTransactionByHash(ctx, txHash)
if err != nil {
return nil
}

return tx
}

Expand All @@ -370,26 +367,22 @@ func (s *BlockChainAPI) GetTransactionByBlockNumberAndIndex(
blockNumber rpc.BlockNumber,
index hexutil.Uint,
) *RPCTransaction {
blockHash := common.HexToHash("0x1d59ff54b1eb26b013ce3cb5fc9dab3705b415a67127a003c3e61eb445bb8df2")
to := common.HexToAddress("0xf02c1c8e6114b1dbe8937a39260b5b0a374432bb")
txIndex := uint64(64)
block, err := s.Store.GetBlockByNumber(ctx, uint64(blockNumber))
if err != nil {
return nil
}

tx := &RPCTransaction{
BlockHash: (*common.Hash)(&blockHash),
BlockNumber: (*hexutil.Big)(big.NewInt(6139707)),
From: common.HexToAddress("0xa7d9ddbe1f17865597fbd27ec712455208b6b76d"),
Gas: hexutil.Uint64(50000),
GasPrice: (*hexutil.Big)(big.NewInt(20000000000)),
Hash: common.HexToHash("0x88df016429689c079f3b2f6ad39fa052532c56795b733da78a91ebe6a713944b"),
Input: hexutil.Bytes("0x68656c6c6f21"),
Nonce: hexutil.Uint64(21),
To: &to,
TransactionIndex: (*hexutil.Uint64)(&txIndex),
Value: (*hexutil.Big)(big.NewInt(4290000000000000)),
V: (*hexutil.Big)(big.NewInt(37)),
R: (*hexutil.Big)(big.NewInt(150)),
S: (*hexutil.Big)(big.NewInt(250)),
highestIndex := len(block.TransactionHashes) - 1
if index > hexutil.Uint(highestIndex) {
return nil
}

txHash := common.HexToHash(block.TransactionHashes[index])
tx, err := s.GetTransactionByHash(ctx, txHash)
if err != nil {
return nil
}

return tx
}

Expand Down Expand Up @@ -474,26 +467,34 @@ func (s *BlockChainAPI) GetBlockByHash(
fullTx bool,
) (map[string]interface{}, error) {
block := map[string]interface{}{}

blockExecutedPayload, err := s.Store.GetBlockByHash(ctx, hash)
if err != nil {
return block, err
}

block["number"] = hexutil.Uint64(blockExecutedPayload.Height)
block["hash"] = blockExecutedPayload.Hash
block["parentHash"] = blockExecutedPayload.ParentBlockHash
block["receiptsRoot"] = blockExecutedPayload.ReceiptRoot
block["transactions"] = blockExecutedPayload.TransactionHashes

block["difficulty"] = "0x4ea3f27bc"
block["extraData"] = "0x476574682f4c5649562f76312e302e302f6c696e75782f676f312e342e32"
block["gasLimit"] = "0x1388"
block["gasUsed"] = "0x0"
block["hash"] = "0xdc0818cf78f21a8e70579cb46a43643f78291264dda342ae31049421c82d21ae"
block["logsBloom"] = "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
block["miner"] = "0xbb7b8287f3f0a933474a79eae42cbca977791171"
block["mixHash"] = "0x4fffe9ae21f1c9e15207b1f472d5bbdd68c9595d461666602f2be20daf5e7843"
block["nonce"] = "0x689056015818adbe"
block["number"] = "0x1b4"
block["parentHash"] = "0xe99e022112df268087ea7eafaf4790497fd21dbeeb6bd7a1721df161a6657a54"
block["receiptsRoot"] = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
block["sha3Uncles"] = "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347"
block["size"] = "0x220"
block["stateRoot"] = "0xddc8b0234c2e0cad087c8b389aa7ef01f7d79b2570bccb77ce48648aa61c904d"
block["timestamp"] = "0x55ba467c"
block["totalDifficulty"] = "0x78ed983323d"
block["transactions"] = []string{}
block["transactionsRoot"] = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
block["uncles"] = []string{}

return block, nil
}

Expand Down Expand Up @@ -558,7 +559,12 @@ func (s *BlockChainAPI) GetBlockTransactionCountByHash(
ctx context.Context,
blockHash common.Hash,
) *hexutil.Uint {
count := hexutil.Uint(100522)
block, err := s.Store.GetBlockByHash(ctx, blockHash)
if err != nil {
return nil
}

count := hexutil.Uint(len(block.TransactionHashes))
return &count
}

Expand All @@ -568,7 +574,12 @@ func (s *BlockChainAPI) GetBlockTransactionCountByNumber(
ctx context.Context,
blockNumber rpc.BlockNumber,
) *hexutil.Uint {
count := hexutil.Uint(522)
block, err := s.Store.GetBlockByNumber(ctx, uint64(blockNumber))
if err != nil {
return nil
}

count := hexutil.Uint(len(block.TransactionHashes))
return &count
}

Expand Down
Loading

0 comments on commit 99170a1

Please sign in to comment.