Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thor client #818

Merged
merged 40 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
80c7a62
feat: add thorclient
paologalligit Aug 12, 2024
2638d21
refactor: remove roundTripper
paologalligit Aug 12, 2024
09de0ec
refactor: change null check
paologalligit Aug 12, 2024
71db025
clean: remove commented code
paologalligit Aug 12, 2024
83524a1
feat: add account revision and pending tx
paologalligit Aug 13, 2024
dc9e53c
fix: add licence headers and fix linter issue
paologalligit Aug 13, 2024
9892bad
refactor: rename package
paologalligit Aug 13, 2024
81824ae
refactor: change revision type to string
paologalligit Aug 13, 2024
8788f8c
refactor: rename GetLogs and GetTransfers to FilterEvents and FilterT…
paologalligit Aug 13, 2024
06a58de
refactor: change FilterEvents and FilterTransactions request type to …
paologalligit Aug 13, 2024
5764771
Merge remote-tracking branch 'origin/master' into paolo/feat/client
otherview Aug 14, 2024
e829f16
Adding common.EventWrapper to handle channel errors
otherview Aug 14, 2024
a521ce5
tweak
otherview Aug 16, 2024
88cadba
Merge remote-tracking branch 'origin/master' into paolo/feat/client
otherview Aug 19, 2024
aa94aa1
update rawclient + update account tests
otherview Aug 19, 2024
a9d37ef
tidy up names
otherview Aug 19, 2024
72858c8
update tests
otherview Aug 19, 2024
6ab2443
pr comments
otherview Aug 20, 2024
d2f6c8b
adding raw tx
otherview Aug 20, 2024
c667542
Merge remote-tracking branch 'origin/master' into paolo/feat/client
otherview Aug 23, 2024
e3ebf93
Tidy up method names and calls
otherview Aug 26, 2024
7a9e102
options client
otherview Aug 26, 2024
44b61b9
tweaks
otherview Aug 29, 2024
ba08cff
pr comments
otherview Aug 29, 2024
c721582
Update thorclient/common/common.go
otherview Aug 30, 2024
c3bc7a3
pr comments
otherview Aug 30, 2024
2119256
Merge branch 'paolo/feat/client' of github.com:vechain/thor into paol…
otherview Aug 30, 2024
875b728
Adding Subscriptions
otherview Sep 2, 2024
39e3237
Pr comments
otherview Sep 2, 2024
0ec320d
adjust func orders
libotony Sep 3, 2024
8d78663
pr comments
otherview Sep 3, 2024
279a674
Merge branch 'paolo/feat/client' of github.com:vechain/thor into paol…
otherview Sep 3, 2024
b2153fa
changing subscribe to use the channel close vs multiple channels
otherview Sep 5, 2024
d86f0f2
adding go-doc
otherview Sep 5, 2024
a2569e0
no error after unsubscribe
libotony Sep 12, 2024
b753aea
pr comments
otherview Sep 13, 2024
6fa1b1b
Merge remote-tracking branch 'origin/master' into paolo/feat/client
otherview Sep 13, 2024
d2604d2
checking status code is 2xx
paologalligit Oct 11, 2024
c1f1159
Merge conflicts
paologalligit Oct 11, 2024
a1e92c3
fix: change FilterTransfers argument
paologalligit Oct 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
237 changes: 119 additions & 118 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
61 changes: 33 additions & 28 deletions api/blocks/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package blocks_test

import (
"encoding/json"
"io"
"math"
"math/big"
"net/http"
Expand All @@ -20,6 +19,7 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vechain/thor/v2/api/blocks"
"github.com/vechain/thor/v2/block"
"github.com/vechain/thor/v2/chain"
Expand All @@ -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 @@ -60,14 +67,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 @@ -77,7 +86,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 @@ -87,7 +97,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")
otherview marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err)
finalized := new(blocks.JSONCollapsedBlock)
if err := json.Unmarshal(res, &finalized); err != nil {
t.Fatal(err)
Expand All @@ -100,7 +111,8 @@ func testGetFinalizedBlock(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 @@ -110,14 +122,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 @@ -128,18 +143,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 Expand Up @@ -239,16 +257,3 @@ func checkExpandedBlock(t *testing.T, expBl *block.Block, actBl *blocks.JSONExpa
assert.Equal(t, tx.ID(), actBl.Transactions[i].ID, "txid should be equal")
}
}

func httpGet(t *testing.T, url string) ([]byte, int) {
res, err := http.Get(url) // nolint:gosec
if err != nil {
t.Fatal(err)
}
r, err := io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
}
return r, res.StatusCode
}
Loading