Skip to content

Commit

Permalink
Thor client (#818)
Browse files Browse the repository at this point in the history
* feat: add thorclient

* refactor: remove roundTripper

* refactor: change null check

* clean: remove commented code

* feat: add account revision and pending tx

* fix: add licence headers and fix linter issue

* refactor: rename package

* refactor: change revision type to string

* refactor: rename GetLogs and GetTransfers to FilterEvents and FilterTransfers

* refactor: change FilterEvents and FilterTransactions request type to EventFilter

* Adding common.EventWrapper to handle channel errors

* tweak

* update rawclient + update account tests

* tidy up names

* update tests

* pr comments

* adding raw tx

* Tidy up method names and calls

* options client

* tweaks

* pr comments

* Update thorclient/common/common.go

Co-authored-by: libotony <[email protected]>

* pr comments

* Adding Subscriptions

* Pr comments

* adjust func orders

* pr comments

* changing subscribe to use the channel close vs multiple channels

* adding go-doc

* no error after unsubscribe

* pr comments

* checking status code is 2xx

* fix: change FilterTransfers argument

---------

Co-authored-by: otherview <[email protected]>
Co-authored-by: libotony <[email protected]>
  • Loading branch information
3 people authored Oct 29, 2024
1 parent 175050c commit b6380d5
Show file tree
Hide file tree
Showing 21 changed files with 2,318 additions and 282 deletions.
4 changes: 2 additions & 2 deletions api/accounts/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (a *Accounts) handleGetCode(w http.ResponseWriter, req *http.Request) error
return err
}

return utils.WriteJSON(w, map[string]string{"code": hexutil.Encode(code)})
return utils.WriteJSON(w, &GetCodeResult{Code: hexutil.Encode(code)})
}

func (a *Accounts) getAccount(addr thor.Address, header *block.Header, state *state.State) (*Account, error) {
Expand Down Expand Up @@ -164,7 +164,7 @@ func (a *Accounts) handleGetStorage(w http.ResponseWriter, req *http.Request) er
if err != nil {
return err
}
return utils.WriteJSON(w, map[string]string{"value": storage.String()})
return utils.WriteJSON(w, &GetStorageResult{Value: storage.String()})
}

func (a *Accounts) handleCallContract(w http.ResponseWriter, req *http.Request) error {
Expand Down
193 changes: 119 additions & 74 deletions api/accounts/accounts_test.go

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions api/accounts/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ type CallData struct {
Caller *thor.Address `json:"caller"`
}

type GetCodeResult struct {
Code string `json:"code"`
}

type GetStorageResult struct {
Value string `json:"value"`
}

type CallResult struct {
Data string `json:"data"`
Events []*transactions.Event `json:"events"`
Expand Down
49 changes: 34 additions & 15 deletions api/blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,26 @@ import (
"github.com/vechain/thor/v2/packer"
"github.com/vechain/thor/v2/state"
"github.com/vechain/thor/v2/thor"
"github.com/vechain/thor/v2/thorclient"
"github.com/vechain/thor/v2/tx"
)

var genesisBlock *block.Block
var blk *block.Block
var ts *httptest.Server
const (
invalidBytes32 = "0x000000000000000000000000000000000000000000000000000000000000000g" // invalid bytes32
)

var invalidBytes32 = "0x000000000000000000000000000000000000000000000000000000000000000g" //invlaid bytes32
var (
genesisBlock *block.Block
blk *block.Block
ts *httptest.Server
tclient *thorclient.Client
)

func TestBlock(t *testing.T) {
initBlockServer(t)
defer ts.Close()

tclient = thorclient.New(ts.URL)
for name, tt := range map[string]func(*testing.T){
"testBadQueryParams": testBadQueryParams,
"testInvalidBlockID": testInvalidBlockID,
Expand All @@ -61,14 +68,16 @@ func TestBlock(t *testing.T) {

func testBadQueryParams(t *testing.T) {
badQueryParams := "?expanded=1"
res, statusCode := httpGet(t, ts.URL+"/blocks/best"+badQueryParams)
res, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/best" + badQueryParams)
require.NoError(t, err)

assert.Equal(t, http.StatusBadRequest, statusCode)
assert.Equal(t, "expanded: should be boolean", strings.TrimSpace(string(res)))
}

func testGetBestBlock(t *testing.T) {
res, statusCode := httpGet(t, ts.URL+"/blocks/best")
res, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/best")
require.NoError(t, err)
rb := new(blocks.JSONCollapsedBlock)
if err := json.Unmarshal(res, &rb); err != nil {
t.Fatal(err)
Expand All @@ -78,7 +87,8 @@ func testGetBestBlock(t *testing.T) {
}

func testGetBlockByHeight(t *testing.T) {
res, statusCode := httpGet(t, ts.URL+"/blocks/1")
res, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/1")
require.NoError(t, err)
rb := new(blocks.JSONCollapsedBlock)
if err := json.Unmarshal(res, &rb); err != nil {
t.Fatal(err)
Expand All @@ -88,7 +98,8 @@ func testGetBlockByHeight(t *testing.T) {
}

func testGetFinalizedBlock(t *testing.T) {
res, statusCode := httpGet(t, ts.URL+"/blocks/finalized")
res, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/finalized")
require.NoError(t, err)
finalized := new(blocks.JSONCollapsedBlock)
if err := json.Unmarshal(res, &finalized); err != nil {
t.Fatal(err)
Expand All @@ -101,7 +112,8 @@ func testGetFinalizedBlock(t *testing.T) {
}

func testGetJustifiedBlock(t *testing.T) {
res, statusCode := httpGet(t, ts.URL+"/blocks/justified")
res, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/justified")
require.NoError(t, err)
justified := new(blocks.JSONCollapsedBlock)
require.NoError(t, json.Unmarshal(res, &justified))

Expand All @@ -111,7 +123,8 @@ func testGetJustifiedBlock(t *testing.T) {
}

func testGetBlockByID(t *testing.T) {
res, statusCode := httpGet(t, ts.URL+"/blocks/"+blk.Header().ID().String())
res, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/" + blk.Header().ID().String())
require.NoError(t, err)
rb := new(blocks.JSONCollapsedBlock)
if err := json.Unmarshal(res, rb); err != nil {
t.Fatal(err)
Expand All @@ -121,14 +134,17 @@ func testGetBlockByID(t *testing.T) {
}

func testGetBlockNotFound(t *testing.T) {
res, statusCode := httpGet(t, ts.URL+"/blocks/0x00000000851caf3cfdb6e899cf5958bfb1ac3413d346d43539627e6be7ec1b4a")
res, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/0x00000000851caf3cfdb6e899cf5958bfb1ac3413d346d43539627e6be7ec1b4a")
require.NoError(t, err)

assert.Equal(t, http.StatusOK, statusCode)
assert.Equal(t, "null", strings.TrimSpace(string(res)))
}

func testGetExpandedBlockByID(t *testing.T) {
res, statusCode := httpGet(t, ts.URL+"/blocks/"+blk.Header().ID().String()+"?expanded=true")
res, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/" + blk.Header().ID().String() + "?expanded=true")
require.NoError(t, err)

rb := new(blocks.JSONExpandedBlock)
if err := json.Unmarshal(res, rb); err != nil {
t.Fatal(err)
Expand All @@ -139,18 +155,21 @@ func testGetExpandedBlockByID(t *testing.T) {

func testInvalidBlockNumber(t *testing.T) {
invalidNumberRevision := "4294967296" //invalid block number
_, statusCode := httpGet(t, ts.URL+"/blocks/"+invalidNumberRevision)
_, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/" + invalidNumberRevision)
require.NoError(t, err)
assert.Equal(t, http.StatusBadRequest, statusCode)
}

func testInvalidBlockID(t *testing.T) {
_, statusCode := httpGet(t, ts.URL+"/blocks/"+invalidBytes32)
_, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/" + invalidBytes32)
require.NoError(t, err)
assert.Equal(t, http.StatusBadRequest, statusCode)
}

func testGetBlockWithRevisionNumberTooHigh(t *testing.T) {
revisionNumberTooHigh := strconv.FormatUint(math.MaxUint64, 10)
res, statusCode := httpGet(t, ts.URL+"/blocks/"+revisionNumberTooHigh)
res, statusCode, err := tclient.RawHTTPClient().RawHTTPGet("/blocks/" + revisionNumberTooHigh)
require.NoError(t, err)

assert.Equal(t, http.StatusBadRequest, statusCode)
assert.Equal(t, "revision: block number out of max uint32", strings.TrimSpace(string(res)))
Expand Down
Loading

0 comments on commit b6380d5

Please sign in to comment.