Skip to content

Commit

Permalink
update rawclient + update account tests
Browse files Browse the repository at this point in the history
  • Loading branch information
otherview committed Aug 19, 2024
1 parent 88cadba commit df91afd
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 78 deletions.
115 changes: 56 additions & 59 deletions api/accounts/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"bytes"
"encoding/json"
"fmt"

"io"
"math/big"
"net/http"
Expand All @@ -22,6 +23,7 @@ import (
"github.com/ethereum/go-ethereum/crypto"
"github.com/gorilla/mux"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
ABI "github.com/vechain/thor/v2/abi"
"github.com/vechain/thor/v2/api/accounts"
"github.com/vechain/thor/v2/block"
Expand All @@ -32,6 +34,8 @@ 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"
tccommon "github.com/vechain/thor/v2/thorclient/common"
"github.com/vechain/thor/v2/tx"
)

Expand Down Expand Up @@ -105,38 +109,42 @@ var invalidNumberRevision = "4294967296"
var acc *accounts.Accounts
var ts *httptest.Server

var tclient *thorclient.Client

func TestAccount(t *testing.T) {
initAccountServer(t)
defer ts.Close()

tclient = thorclient.New(ts.URL)
for name, tt := range map[string]func(*testing.T){
"getAccount": getAccount,
"getAccountWithNonExisitingRevision": getAccountWithNonExisitingRevision,
"getAccountWithGenesisRevision": getAccountWithGenesisRevision,
"getAccountWithFinalizedRevision": getAccountWithFinalizedRevision,
"getCode": getCode,
"getCodeWithNonExistingRevision": getCodeWithNonExistingRevision,
"getStorage": getStorage,
"getStorageWithNonExistingRevision": getStorageWithNonExistingRevision,
"deployContractWithCall": deployContractWithCall,
"callContract": callContract,
"callContractWithNonExisitingRevision": callContractWithNonExisitingRevision,
"batchCall": batchCall,
"batchCallWithNonExisitingRevision": batchCallWithNonExisitingRevision,
"getAccount": getAccount,
"getAccountWithNonExistingRevision": getAccountWithNonExistingRevision,
"getAccountWithGenesisRevision": getAccountWithGenesisRevision,
"getAccountWithFinalizedRevision": getAccountWithFinalizedRevision,
"getCode": getCode,
"getCodeWithNonExistingRevision": getCodeWithNonExistingRevision,
"getStorage": getStorage,
"getStorageWithNonExistingRevision": getStorageWithNonExistingRevision,
"deployContractWithCall": deployContractWithCall,
"callContract": callContract,
"callContractWithNonExistingRevision": callContractWithNonExistingRevision,
"batchCall": batchCall,
"batchCallWithNonExistingRevision": batchCallWithNonExistingRevision,
} {
t.Run(name, tt)
}
}

func getAccount(t *testing.T) {
_, statusCode := httpGet(t, ts.URL+"/accounts/"+invalidAddr)
_, statusCode, err := tclient.RawClient().RawHTTPGet("/accounts/" + invalidAddr)
require.NoError(t, err)
assert.Equal(t, http.StatusBadRequest, statusCode, "bad address")

_, statusCode = httpGet(t, ts.URL+"/accounts/"+addr.String()+"?revision="+invalidNumberRevision)
_, statusCode, err = tclient.RawClient().RawHTTPGet("/accounts/" + addr.String() + "?revision=" + invalidNumberRevision)

Check failure on line 143 in api/accounts/accounts_test.go

View workflow job for this annotation

GitHub Actions / Lint / golangci-lint

ineffectual assignment to err (ineffassign)
assert.Equal(t, http.StatusBadRequest, statusCode, "bad revision")

//revision is optional defaut `best`
res, statusCode := httpGet(t, ts.URL+"/accounts/"+addr.String())
//revision is optional default `best`
res, statusCode, err := tclient.RawClient().RawHTTPGet("/accounts/" + addr.String())

Check failure on line 147 in api/accounts/accounts_test.go

View workflow job for this annotation

GitHub Actions / Lint / golangci-lint

ineffectual assignment to err (ineffassign)
var acc accounts.Account
if err := json.Unmarshal(res, &acc); err != nil {
t.Fatal(err)
Expand All @@ -145,17 +153,19 @@ func getAccount(t *testing.T) {
assert.Equal(t, http.StatusOK, statusCode, "OK")
}

func getAccountWithNonExisitingRevision(t *testing.T) {
func getAccountWithNonExistingRevision(t *testing.T) {
revision64Len := "0x00000000851caf3cfdb6e899cf5958bfb1ac3413d346d43539627e6be7ec1b4a"

res, statusCode := httpGet(t, ts.URL+"/accounts/"+addr.String()+"?revision="+revision64Len)
res, statusCode, err := tclient.RawClient().RawHTTPGet("/accounts/" + addr.String() + "?revision=" + revision64Len)
require.NoError(t, err)

assert.Equal(t, http.StatusBadRequest, statusCode, "bad revision")
assert.Equal(t, "revision: leveldb: not found\n", string(res), "revision not found")
}

func getAccountWithGenesisRevision(t *testing.T) {
res, statusCode := httpGet(t, ts.URL+"/accounts/"+addr.String()+"?revision="+genesisBlock.Header().ID().String())
res, statusCode, err := tclient.RawClient().RawHTTPGet("/accounts/" + addr.String() + "?revision=" + genesisBlock.Header().ID().String())
require.NoError(t, err)
assert.Equal(t, http.StatusOK, statusCode, "bad revision")

var acc accounts.Account
Expand All @@ -175,10 +185,13 @@ func getAccountWithGenesisRevision(t *testing.T) {
}

func getAccountWithFinalizedRevision(t *testing.T) {
soloAddress := "0xf077b491b355E64048cE21E3A6Fc4751eEeA77fa"
soloAddress := thor.MustParseAddress("0xf077b491b355E64048cE21E3A6Fc4751eEeA77fa")

genesisAccount := httpGetAccount(t, soloAddress+"?revision="+genesisBlock.Header().ID().String())
finalizedAccount := httpGetAccount(t, soloAddress+"?revision=finalized")
genesisAccount, err := tclient.GetAccountForRevision(&soloAddress, genesisBlock.Header().ID().String())
require.NoError(t, err)

finalizedAccount, err := tclient.GetAccountForRevision(&soloAddress, tccommon.FinalizedRevision)
require.NoError(t, err)

genesisEnergy := (*big.Int)(&genesisAccount.Energy)
finalizedEnergy := (*big.Int)(&finalizedAccount.Energy)
Expand All @@ -187,14 +200,17 @@ func getAccountWithFinalizedRevision(t *testing.T) {
}

func getCode(t *testing.T) {
_, statusCode := httpGet(t, ts.URL+"/accounts/"+invalidAddr+"/code")
_, statusCode, err := tclient.RawClient().RawHTTPGet("/accounts/" + invalidAddr + "/code")
require.NoError(t, err)
assert.Equal(t, http.StatusBadRequest, statusCode, "bad address")

_, statusCode = httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/code?revision="+invalidNumberRevision)
_, statusCode, err = tclient.RawClient().RawHTTPGet("/accounts/" + contractAddr.String() + "/code?revision=" + invalidNumberRevision)
require.NoError(t, err)
assert.Equal(t, http.StatusBadRequest, statusCode, "bad revision")

//revision is optional defaut `best`
res, statusCode := httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/code")
res, statusCode, err := tclient.RawClient().RawHTTPGet("/accounts/" + contractAddr.String() + "/code")
require.NoError(t, err)
var code map[string]string
if err := json.Unmarshal(res, &code); err != nil {
t.Fatal(err)
Expand All @@ -210,24 +226,29 @@ func getCode(t *testing.T) {
func getCodeWithNonExistingRevision(t *testing.T) {
revision64Len := "0x00000000851caf3cfdb6e899cf5958bfb1ac3413d346d43539627e6be7ec1b4a"

res, statusCode := httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/code?revision="+revision64Len)
res, statusCode, err := tclient.RawClient().RawHTTPGet("/accounts/" + contractAddr.String() + "/code?revision=" + revision64Len)
require.NoError(t, err)

assert.Equal(t, http.StatusBadRequest, statusCode, "bad revision")
assert.Equal(t, "revision: leveldb: not found\n", string(res), "revision not found")
}

func getStorage(t *testing.T) {
_, statusCode := httpGet(t, ts.URL+"/accounts/"+invalidAddr+"/storage/"+storageKey.String())
_, statusCode, err := tclient.RawClient().RawHTTPGet("/accounts/" + invalidAddr + "/storage/" + storageKey.String())
require.NoError(t, err)
assert.Equal(t, http.StatusBadRequest, statusCode, "bad address")

_, statusCode = httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/storage/"+invalidBytes32)
_, statusCode, err = tclient.RawClient().RawHTTPGet("/accounts/" + contractAddr.String() + "/storage/" + invalidBytes32)
require.NoError(t, err)
assert.Equal(t, http.StatusBadRequest, statusCode, "bad storage key")

_, statusCode = httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/storage/"+storageKey.String()+"?revision="+invalidNumberRevision)
_, statusCode, err = tclient.RawClient().RawHTTPGet("/accounts/" + contractAddr.String() + "/storage/" + storageKey.String() + "?revision=" + invalidNumberRevision)
require.NoError(t, err)
assert.Equal(t, http.StatusBadRequest, statusCode, "bad revision")

//revision is optional defaut `best`
res, statusCode := httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/storage/"+storageKey.String())
res, statusCode, err := tclient.RawClient().RawHTTPGet("/accounts/" + contractAddr.String() + "/storage/" + storageKey.String())
require.NoError(t, err)
var value map[string]string
if err := json.Unmarshal(res, &value); err != nil {
t.Fatal(err)
Expand All @@ -243,7 +264,8 @@ func getStorage(t *testing.T) {
func getStorageWithNonExistingRevision(t *testing.T) {
revision64Len := "0x00000000851caf3cfdb6e899cf5958bfb1ac3413d346d43539627e6be7ec1b4a"

res, statusCode := httpGet(t, ts.URL+"/accounts/"+contractAddr.String()+"/storage/"+storageKey.String()+"?revision="+revision64Len)
res, statusCode, err := tclient.RawClient().RawHTTPGet("/accounts/" + contractAddr.String() + "/storage/" + storageKey.String() + "?revision=" + revision64Len)
require.NoError(t, err)

assert.Equal(t, http.StatusBadRequest, statusCode, "bad revision")
assert.Equal(t, "revision: leveldb: not found\n", string(res), "revision not found")
Expand Down Expand Up @@ -405,7 +427,7 @@ func callContract(t *testing.T) {
assert.Equal(t, a+b, ret)
}

func callContractWithNonExisitingRevision(t *testing.T) {
func callContractWithNonExistingRevision(t *testing.T) {
revision64Len := "0x00000000851caf3cfdb6e899cf5958bfb1ac3413d346d43539627e6be7ec1b4a"

res, statusCode := httpPost(t, ts.URL+"/accounts/"+contractAddr.String()+"?revision="+revision64Len, nil)
Expand Down Expand Up @@ -529,7 +551,7 @@ func batchCall(t *testing.T) {
assert.Equal(t, http.StatusForbidden, statusCode)
}

func batchCallWithNonExisitingRevision(t *testing.T) {
func batchCallWithNonExistingRevision(t *testing.T) {
revision64Len := "0x00000000851caf3cfdb6e899cf5958bfb1ac3413d346d43539627e6be7ec1b4a"

res, statusCode := httpPost(t, ts.URL+"/accounts/*?revision="+revision64Len, nil)
Expand All @@ -554,28 +576,3 @@ func httpPost(t *testing.T, url string, body interface{}) ([]byte, int) {
}
return r, res.StatusCode
}

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
}

func httpGetAccount(t *testing.T, path string) *accounts.Account {
res, statusCode := httpGet(t, ts.URL+"/accounts/"+path)
var acc accounts.Account
if err := json.Unmarshal(res, &acc); err != nil {
t.Fatal(err)
}

assert.Equal(t, http.StatusOK, statusCode, "get account failed")

return &acc
}
4 changes: 4 additions & 0 deletions thorclient/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
"github.com/vechain/thor/v2/thor"
)

const (
FinalizedRevision = "finalized"
)

var (
NotFoundErr = fmt.Errorf("not found")
Not200StatusErr = fmt.Errorf("not 200 status code")
Expand Down
4 changes: 2 additions & 2 deletions thorclient/httpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ func (c *Client) RawHTTPPost(url string, calldata interface{}) ([]byte, error) {
return c.httpPOST(c.url+url, calldata)
}

func (c *Client) RawHTTPGet(url string) ([]byte, error) {
return c.httpGET(c.url + url)
func (c *Client) RawHTTPGet(url string) ([]byte, int, error) {
return c.rawHTTPRequest("GET", c.url+url, nil)
}

func (c *Client) GetPeers() ([]*node.PeerStats, error) {
Expand Down
5 changes: 3 additions & 2 deletions thorclient/httpclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,11 @@ func TestClient_RawHTTPGet(t *testing.T) {
defer ts.Close()

client := NewClient(ts.URL)
response, err := client.RawHTTPGet(url)
response, statusCode, err := client.RawHTTPGet(url)

assert.NoError(t, err)
assert.Equal(t, expectedResponse, response)
assert.Equal(t, http.StatusOK, statusCode)
}

func TestClient_GetPeers(t *testing.T) {
Expand Down Expand Up @@ -414,7 +415,7 @@ func TestClient_Errors(t *testing.T) {
{
name: "RawHTTPGet",
path: "/test",
function: func(client *Client) ([]byte, error) { return client.RawHTTPGet("/test") },
function: func(client *Client) ([]byte, int, error) { return client.RawHTTPGet("/test") },
},
{
name: "GetPeers",
Expand Down
22 changes: 15 additions & 7 deletions thorclient/httpclient/http_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,20 @@ import (
)

func (c *Client) httpRequest(method, url string, payload io.Reader) ([]byte, error) {
body, statusCode, err := c.rawHTTPRequest(method, url, payload)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, fmt.Errorf("http error - Status Code %d - %s - %w", statusCode, body, common.Not200StatusErr)
}
return body, nil
}

func (c *Client) rawHTTPRequest(method, url string, payload io.Reader) ([]byte, int, error) {
req, err := http.NewRequest(method, url, payload)
if err != nil {
return nil, fmt.Errorf("error creating request: %w", err)
return nil, 0, fmt.Errorf("error creating request: %w", err)
}

if method == "POST" {
Expand All @@ -28,19 +39,16 @@ func (c *Client) httpRequest(method, url string, payload io.Reader) ([]byte, err

resp, err := c.c.Do(req)
if err != nil {
return nil, fmt.Errorf("error performing request: %w", err)
return nil, 0, fmt.Errorf("error performing request: %w", err)
}
defer resp.Body.Close()

responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %w", err)
return nil, 0, fmt.Errorf("error reading response body: %w", err)
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http error - Status Code %d - %s - %w", resp.StatusCode, responseBody, common.Not200StatusErr)
}
return responseBody, nil
return responseBody, resp.StatusCode, nil
}

func (c *Client) httpGET(url string) ([]byte, error) {
Expand Down
8 changes: 0 additions & 8 deletions thorclient/thorclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,6 @@ func (c *Client) GetBestBlock() (blocks *blocks.JSONExpandedBlock, err error) {
return c.GetExpandedBlock("best")
}

func (c *Client) RawHTTPPost(url string, calldata interface{}) ([]byte, error) {
return c.conn.RawHTTPPost(url, calldata)
}

func (c *Client) RawHTTPGet(url string) ([]byte, error) {
return c.conn.RawHTTPGet(url)
}

func (c *Client) GetTransaction(id *thor.Bytes32) (*transactions.Transaction, error) {
return c.conn.GetTransaction(id, false)
}
Expand Down

0 comments on commit df91afd

Please sign in to comment.