Skip to content

Commit

Permalink
Merge pull request #15 from dappledger/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
needkane authored Feb 11, 2020
2 parents 441b285 + d055e26 commit 0332cac
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
2 changes: 1 addition & 1 deletion abi/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,4 @@ func (abi *ABI) MethodById(sigdata []byte) (*Method, error) {
}
}
return nil, fmt.Errorf("no method with id: %#x", sigdata[:4])
}
}
25 changes: 22 additions & 3 deletions call.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (

"github.com/dappledger/ann-go-sdk/common"
"github.com/dappledger/ann-go-sdk/rlp"
"github.com/dappledger/ann-go-sdk/rpc"
"github.com/dappledger/ann-go-sdk/types"
)

Expand All @@ -40,7 +39,7 @@ const (

func (gs *GoSDK) getNonce(addr string) (uint64, error) {
if !common.IsHexAddress(addr) {
return 0, fmt.Errorf("Invalid address(is not hex) %s", addr)
return 0, fmt.Errorf("invalid address(is not hex) %s", addr)
}
if strings.Index(addr, "0x") == 0 {
addr = addr[2:]
Expand All @@ -58,6 +57,26 @@ func (gs *GoSDK) getNonce(addr string) (uint64, error) {
return *nonce, nil
}

func (gs *GoSDK) getPendingNonce(addr string) (uint64, error) {
if !common.IsHexAddress(addr) {
return 0, fmt.Errorf("invalid address(is not hex) %s", addr)
}
if strings.Index(addr, "0x") == 0 {
addr = addr[2:]
}

address := common.Hex2Bytes(addr)
query := append([]byte{types.QueryType_Pending_Nonce}, address...)
rpcResult := new(types.ResultQuery)
err := gs.sendTxCall("query", query, rpcResult)
if err != nil {
return 0, err
}
nonce := new(uint64)
rlp.DecodeBytes(rpcResult.Result.Data, nonce)
return *nonce, nil
}

func (gs *GoSDK) receipt(hashstr string) (*types.ReceiptForDisplay, error) {
if strings.Index(hashstr, "0x") == 0 {
hashstr = hashstr[2:]
Expand Down Expand Up @@ -159,7 +178,7 @@ func (gs *GoSDK) getTxByHash(hash []byte) (*ResultTransaction, *RPCTransaction,

func (gs *GoSDK) getTransactionsHashByHeight(height uint64) (hashs []string, total int, err error) {
res := new(types.ResultBlock)
clientJSON := rpc.NewClientJSONRPC(gs.rpcAddr)
clientJSON := gs.NewClientJsonRPC()
var _params []interface{}
_params = []interface{}{height}
_, err = clientJSON.Call("block", _params, res)
Expand Down
2 changes: 1 addition & 1 deletion common/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ func MutateByteSlice(bytez []byte) []byte {
bytez = append(bytez[:pos], bytez[pos+1:]...)
}
return bytez
}
}
2 changes: 1 addition & 1 deletion common/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@ func WriteFileAtomic(filePath string, newBytes []byte, mode os.FileMode) error {
// Move filePath.new to filePath
err = os.Rename(filePath+".new", filePath)
return err
}
}
34 changes: 34 additions & 0 deletions example/za_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,37 @@ func PayloadTxSignature(payloadTx *sdk.Tx) (string, error) {

return common.Bytes2Hex(txBytes), nil
}

func TestPendingNonce(t *testing.T) {
client := sdk.New("localhost:46657", sdk.ZaCryptoType)

var txsHash []string
for i := 0; i < 10; i++ {
nonce, err := client.PendingNonce(accAddr)
assert.Nil(t, err)

var arg = sdk.Tx{
AccountBase: sdk.AccountBase{
PrivKey: accPriv,
Nonce: nonce,
},
Payload: "value"+ fmt.Sprintf("%v", i),
}

sig, err := PayloadTxSignature(&arg)
assert.Nil(t, err)
// async
txHash, err := client.TranscationSignatureAsync(sig)
assert.Nil(t, err)

txsHash = append(txsHash, txHash)
}

time.Sleep(3 * time.Second)

for n := 0; n < 10; n++ {
value, err := client.TransactionPayLoad(txsHash[n])
assert.Nil(t, err)
assert.Equal(t, "value"+ fmt.Sprintf("%v", n), value)
}
}
52 changes: 50 additions & 2 deletions gosdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
package sdk

import (
"crypto/tls"
"fmt"
"math/big"
"net/http"
"net/url"
"strings"

"github.com/dappledger/ann-go-sdk/common"
"github.com/dappledger/ann-go-sdk/types"
Expand All @@ -31,19 +36,58 @@ const (
type GoSDK struct {
rpcAddr string
cryptoType CyrptoType
client *http.Client
}

func (gs *GoSDK) Url() string {
return gs.rpcAddr
}

//Deprecated, use NewSDk instead
func New(rpcAddr string, cryptoType CyrptoType) *GoSDK {
return &GoSDK{
rpcAddr,
cryptoType,
rpcAddr: rpcAddr,
cryptoType: cryptoType,
client: nil,
}
}

func NewSDk(rpcAddr string, cryptoType CyrptoType) (*GoSDK, error) {
var client *http.Client
parts := strings.SplitN(rpcAddr, "://", 2)
if len(parts) == 1 {
//tcp
} else if len(parts) != 2 {
return nil, fmt.Errorf("unsupported rpc %v", rpcAddr)
} else {
switch parts[0] {
case "tcp":
case "http":
_, err := url.Parse(rpcAddr)
if err != nil {
return nil, err
}
client = http.DefaultClient
case "https":
_, err := url.Parse(rpcAddr)
if err != nil {
return nil, err
}
client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
}
}
fmt.Println("with client")
return &GoSDK{
rpcAddr: rpcAddr,
cryptoType: cryptoType,
client: client,
}, nil
}

func (gs *GoSDK) JsonRPCCall(method string, params []byte, result interface{}) error {
return gs.sendTxCall(method, params, result)
}
Expand All @@ -59,6 +103,10 @@ func (gs *GoSDK) Nonce(addr string) (uint64, error) {
return gs.getNonce(addr)
}

func (gs *GoSDK) PendingNonce(addr string) (uint64, error) {
return gs.getPendingNonce(addr)
}

func (gs *GoSDK) CheckHealth() (bool, error) {
return gs.checkHealth()
}
Expand Down
2 changes: 1 addition & 1 deletion kv_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,4 @@ func (gs *GoSDK) kvTxSigned(tx, funcType string) (string, error) {
return "", err
}
return rpcResult.TxHash, nil
}
}
13 changes: 11 additions & 2 deletions rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,23 @@
package sdk

import (
"fmt"
"time"

"github.com/dappledger/ann-go-sdk/rpc"
"github.com/dappledger/ann-go-sdk/types"
)

func (gs *GoSDK) NewClientJsonRPC() *rpc.ClientJSONRPC {
if gs.client != nil {
fmt.Println("with client")
return rpc.NewClientJSONRPCWithHTTPClient(gs.rpcAddr, gs.client)
}
return rpc.NewClientJSONRPC(gs.rpcAddr)
}

func (gs *GoSDK) sendTxCall(method string, params []byte, result interface{}) error {
clientJSON := rpc.NewClientJSONRPC(gs.rpcAddr)
clientJSON := gs.NewClientJsonRPC()

var _params []interface{}
if len(params) > 0 {
Expand All @@ -41,7 +50,7 @@ func (gs *GoSDK) LastHeight() (int64, error) {
}

func (gs *GoSDK) Block(height int64) (*types.ResultBlock, error) {
clientJSON := rpc.NewClientJSONRPC(gs.rpcAddr)
clientJSON := gs.NewClientJsonRPC()

b := &types.ResultBlock{}

Expand Down
72 changes: 71 additions & 1 deletion rpc/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package rpc

import (
"bytes"
"crypto/tls"
"encoding/hex"
"encoding/json"
"errors"
Expand Down Expand Up @@ -55,11 +56,62 @@ func makeHTTPClient(remoteAddr string) (string, *http.Client) {
address, dialer := makeHTTPDialer(remoteAddr)
return "http://" + address, &http.Client{
Transport: &http.Transport{
Dial: dialer,
Dial: dialer,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
}

// network - name of the network (for example, "tcp", "unix")
// s - rest of the address (for example, "192.0.2.1:25", "[2001:db8::1]:80")
// TODO: Deprecate support for IP:PORT or /path/to/socket
func parseRemoteAddr(remoteAddr string) (network string, s string, err error) {
parts := strings.SplitN(remoteAddr, "://", 2)
var protocol, address string
switch len(parts) {
case 1:
// default to tcp if nothing specified
protocol, address = "tcp", remoteAddr
case 2:
protocol, address = parts[0], parts[1]
default:
return "", "", fmt.Errorf("invalid addr: %s", remoteAddr)
}

return protocol, address, nil
}

func toClientAddress(remoteAddr string) (string, error) {
clientProtocol, trimmedAddress, err := toClientAddrAndParse(remoteAddr)
if err != nil {
return "", err
}
return clientProtocol + "://" + trimmedAddress, nil
}

// protocol - client's protocol (for example, "http", "https", "wss", "ws", "tcp")
// trimmedS - rest of the address (for example, "192.0.2.1:25", "[2001:db8::1]:80") with "/" replaced with "."
func toClientAddrAndParse(remoteAddr string) (network string, trimmedS string, err error) {
protocol, address, err := parseRemoteAddr(remoteAddr)
if err != nil {
return "", "", err
}

// protocol to use for http operations, to support both http and https
var clientProtocol string
// default to http for unknown protocols (ex. tcp)
switch protocol {
case "http", "https":
clientProtocol = protocol
default:
clientProtocol = "http"
}

// replace / with . for http requests (kvstore domain)
trimmedAddress := strings.Replace(address, "/", ".", -1)
return clientProtocol, trimmedAddress, nil
}

//------------------------------------------------------------------------------------

type Client interface {
Expand All @@ -81,6 +133,24 @@ func NewClientJSONRPC(remote string) *ClientJSONRPC {
}
}

// NewJSONRPCClientWithHTTPClient returns a JSONRPCClient pointed at the given address using a custom http client
// The function panics if the provided client is nil or remote is invalid.
func NewClientJSONRPCWithHTTPClient(remote string, client *http.Client) *ClientJSONRPC {
if client == nil {
panic("nil http.Client provided")
}

clientAddress, err := toClientAddress(remote)
if err != nil {
panic(fmt.Sprintf("invalid remote %s: %s", remote, err))
}

return &ClientJSONRPC{
address: clientAddress,
client: client,
}
}

func (c *ClientJSONRPC) Call(method string, params []interface{}, result interface{}) (interface{}, error) {
return c.call(method, params, result)
}
Expand Down
1 change: 1 addition & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ const (
QueryTypeContractByHeight QueryType = 10
QueryType_Key QueryType = 11
QueryType_Key_Prefix QueryType = 12
QueryType_Pending_Nonce QueryType = 13
)

0 comments on commit 0332cac

Please sign in to comment.