mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 17:33:47 +00:00
swarm, ethclient: Add version test for http api
Clear redundant addition of BlockNumber in ethclient
This commit is contained in:
parent
690522b09d
commit
88ed2d4b2d
3 changed files with 41 additions and 18 deletions
|
|
@ -76,12 +76,6 @@ type rpcBlock struct {
|
||||||
UncleHashes []common.Hash `json:"uncles"`
|
UncleHashes []common.Hash `json:"uncles"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ec *Client) BlockNumber(ctx context.Context) (big.Int, error) {
|
|
||||||
number := &big.Int{}
|
|
||||||
err := ec.c.CallContext(ctx, &number, "eth_blockNumber")
|
|
||||||
return *number, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ec *Client) getBlock(ctx context.Context, method string, args ...interface{}) (*types.Block, error) {
|
func (ec *Client) getBlock(ctx context.Context, method string, args ...interface{}) (*types.Block, error) {
|
||||||
var raw json.RawMessage
|
var raw json.RawMessage
|
||||||
err := ec.c.CallContext(ctx, &raw, method, args...)
|
err := ec.c.CallContext(ctx, &raw, method, args...)
|
||||||
|
|
|
||||||
|
|
@ -23,24 +23,17 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
|
||||||
"github.com/ethereum/go-ethereum/swarm/api"
|
"github.com/ethereum/go-ethereum/swarm/api"
|
||||||
swarm "github.com/ethereum/go-ethereum/swarm/api/client"
|
swarm "github.com/ethereum/go-ethereum/swarm/api/client"
|
||||||
"github.com/ethereum/go-ethereum/swarm/storage"
|
"github.com/ethereum/go-ethereum/swarm/storage"
|
||||||
"github.com/ethereum/go-ethereum/swarm/testutil"
|
"github.com/ethereum/go-ethereum/swarm/testutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
log.Root().SetHandler(log.CallerFileHandler(log.LvlFilterHandler(log.LvlTrace, log.StreamHandler(os.Stderr, log.TerminalFormat(true)))))
|
|
||||||
}
|
|
||||||
|
|
||||||
// \TODO if create -> get -> update -> get, the last get with return 1.1 because 1.2 retrieve is still pending
|
|
||||||
func TestBzzResource(t *testing.T) {
|
func TestBzzResource(t *testing.T) {
|
||||||
srv := testutil.NewTestSwarmServer(t)
|
srv := testutil.NewTestSwarmServer(t)
|
||||||
defer srv.Close()
|
defer srv.Close()
|
||||||
|
|
@ -65,6 +58,7 @@ func TestBzzResource(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
t.Fatalf("err %s", resp.Status)
|
t.Fatalf("err %s", resp.Status)
|
||||||
}
|
}
|
||||||
|
|
@ -75,7 +69,6 @@ func TestBzzResource(t *testing.T) {
|
||||||
if !bytes.Equal(b, []byte(keybyteshash)) {
|
if !bytes.Equal(b, []byte(keybyteshash)) {
|
||||||
t.Fatalf("resource update hash mismatch, expected '%s' got '%s'", keybyteshash, b)
|
t.Fatalf("resource update hash mismatch, expected '%s' got '%s'", keybyteshash, b)
|
||||||
}
|
}
|
||||||
resp.Body.Close()
|
|
||||||
|
|
||||||
// get latest update (1.1) through resource directly
|
// get latest update (1.1) through resource directly
|
||||||
url = fmt.Sprintf("%s/bzz-resource:/%x", srv.URL, keybytes)
|
url = fmt.Sprintf("%s/bzz-resource:/%x", srv.URL, keybytes)
|
||||||
|
|
@ -83,6 +76,7 @@ func TestBzzResource(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
t.Fatalf("err %s", resp.Status)
|
t.Fatalf("err %s", resp.Status)
|
||||||
}
|
}
|
||||||
|
|
@ -93,7 +87,6 @@ func TestBzzResource(t *testing.T) {
|
||||||
if !bytes.Equal(databytes, b) {
|
if !bytes.Equal(databytes, b) {
|
||||||
t.Fatalf("Expected body '%x', got '%x'", databytes, b)
|
t.Fatalf("Expected body '%x', got '%x'", databytes, b)
|
||||||
}
|
}
|
||||||
resp.Body.Close()
|
|
||||||
|
|
||||||
// update 2
|
// update 2
|
||||||
url = fmt.Sprintf("%s/bzz-resource:/%x", srv.URL, keybytes)
|
url = fmt.Sprintf("%s/bzz-resource:/%x", srv.URL, keybytes)
|
||||||
|
|
@ -102,6 +95,7 @@ func TestBzzResource(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
t.Fatalf("Update returned %d", resp.Status)
|
t.Fatalf("Update returned %d", resp.Status)
|
||||||
}
|
}
|
||||||
|
|
@ -112,6 +106,7 @@ func TestBzzResource(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
t.Fatalf("err %s", resp.Status)
|
t.Fatalf("err %s", resp.Status)
|
||||||
}
|
}
|
||||||
|
|
@ -122,7 +117,42 @@ func TestBzzResource(t *testing.T) {
|
||||||
if !bytes.Equal(data, b) {
|
if !bytes.Equal(data, b) {
|
||||||
t.Fatalf("Expected body '%x', got '%x'", data, b)
|
t.Fatalf("Expected body '%x', got '%x'", data, b)
|
||||||
}
|
}
|
||||||
resp.Body.Close()
|
|
||||||
|
// get latest update (1.2) with specified period
|
||||||
|
url = fmt.Sprintf("%s/bzz-resource:/%x/1", srv.URL, keybytes)
|
||||||
|
resp, err = http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
t.Fatalf("err %s", resp.Status)
|
||||||
|
}
|
||||||
|
b, err = ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(data, b) {
|
||||||
|
t.Fatalf("Expected body '%x', got '%x'", data, b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// get first update (1.1) with specified period and version
|
||||||
|
url = fmt.Sprintf("%s/bzz-resource:/%x/1/1", srv.URL, keybytes)
|
||||||
|
resp, err = http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
t.Fatalf("err %s", resp.Status)
|
||||||
|
}
|
||||||
|
b, err = ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if !bytes.Equal(databytes, b) {
|
||||||
|
t.Fatalf("Expected body '%x', got '%x'", databytes, b)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBzzGetPath(t *testing.T) {
|
func TestBzzGetPath(t *testing.T) {
|
||||||
|
|
|
||||||
|
|
@ -625,8 +625,7 @@ func (self *ResourceHandler) Close() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *ResourceHandler) GetBlock() (uint64, error) {
|
func (self *ResourceHandler) GetBlock() (uint64, error) {
|
||||||
ctx, cancel := context.WithCancel(self.ctx)
|
ctx, _ := context.WithCancel(self.ctx)
|
||||||
defer cancel()
|
|
||||||
blockheader, err := self.ethClient.HeaderByNumber(ctx, nil)
|
blockheader, err := self.ethClient.HeaderByNumber(ctx, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue