github.com/igggame/nebulas-go@v2.1.0+incompatible/rpc/testing/client/main.go (about)

     1  // Copyright (C) 2017 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // the go-nebulas library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU General Public License
    16  // along with the go-nebulas library.  If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  
    19  package main
    20  
    21  import (
    22  	"io"
    23  	"log"
    24  	"time"
    25  
    26  	"github.com/nebulasio/go-nebulas/crypto/keystore"
    27  
    28  	"fmt"
    29  
    30  	"github.com/nebulasio/go-nebulas/rpc"
    31  	"github.com/nebulasio/go-nebulas/rpc/pb"
    32  	"github.com/nebulasio/go-nebulas/util"
    33  	"golang.org/x/net/context"
    34  )
    35  
    36  // TODO: add command line flag.
    37  const (
    38  	//config = "../../../../config.pb.txt"
    39  	from  = "n1QZMXSZtW7BUerroSms4axNfyBGyFGkrh5"
    40  	to    = "n1Zn6iyyQRhqthmCfqGBzWfip1Wx8wEvtrJ"
    41  	value = 2
    42  )
    43  
    44  // RPC testing client.
    45  func main() {
    46  	// Set up a connection to the server.
    47  	//cfg := neblet.LoadConfig(config).Rpc
    48  	addr := fmt.Sprintf("127.0.0.1:%d", uint32(8684))
    49  	conn, err := rpc.Dial(addr)
    50  	if err != nil {
    51  		log.Fatal(err)
    52  	}
    53  	defer conn.Close()
    54  
    55  	ac := rpcpb.NewApiServiceClient(conn)
    56  	adc := rpcpb.NewAdminServiceClient(conn)
    57  
    58  	var nonce uint64
    59  
    60  	{
    61  		r, err := ac.GetNebState(context.Background(), &rpcpb.NonParamsRequest{})
    62  		if err != nil {
    63  			log.Println("GetNebState", "failed", err)
    64  		} else {
    65  			//tail := r.GetTail()
    66  			log.Println("GetNebState tail", r)
    67  		}
    68  	}
    69  
    70  	{
    71  		var val *util.Uint128
    72  		r, err := ac.GetAccountState(context.Background(), &rpcpb.GetAccountStateRequest{Address: from})
    73  		if err != nil {
    74  			log.Println("GetAccountState", from, "failed", err)
    75  		} else if val, err = util.NewUint128FromString(r.GetBalance()); err != nil {
    76  			log.Println("GetAccountState", from, "failed to get balance", err)
    77  		} else {
    78  			nonce = r.Nonce
    79  			// nonce = r.Nonce
    80  			log.Println("GetAccountState", from, "nonce", r.Nonce, "value", val)
    81  		}
    82  	}
    83  
    84  	{
    85  		var val *util.Uint128
    86  		r, err := ac.GetAccountState(context.Background(), &rpcpb.GetAccountStateRequest{Address: to})
    87  		if err != nil {
    88  			log.Println("GetAccountState", to, "failed", err)
    89  		} else if val, err = util.NewUint128FromString(r.GetBalance()); err != nil {
    90  			log.Println("GetAccountState", from, "failed to get balance", err)
    91  		} else {
    92  			// nonce = r.Nonce
    93  			log.Println("GetAccountState", to, "nonce", r.Nonce, "value", val)
    94  		}
    95  	}
    96  
    97  	//admin := rpcpb.NewAdminServiceClient(conn)
    98  
    99  	{
   100  		v, err := util.NewUint128FromInt(value)
   101  		if err != nil {
   102  			log.Println("newUint128 failed:", err)
   103  		}
   104  
   105  		_, err = adc.UnlockAccount(context.Background(), &rpcpb.UnlockAccountRequest{
   106  			Address: from, Passphrase: "passphrase", Duration: uint64(keystore.DefaultUnlockDuration),
   107  		})
   108  		if err != nil {
   109  			log.Println("UnlockAccount failed:", err)
   110  		} else {
   111  			log.Println("UnlockAccount", from)
   112  		}
   113  
   114  		r, err := adc.SendTransaction(context.Background(), &rpcpb.TransactionRequest{
   115  			From: from, To: to, Value: v.String(), Nonce: nonce + 1,
   116  			GasPrice: "2000000", GasLimit: "1000000",
   117  		})
   118  		if err != nil {
   119  			log.Println("SendTransaction failed:", err)
   120  		} else {
   121  			log.Println("SendTransaction", from, "->", to, "value", value, r)
   122  		}
   123  	}
   124  
   125  	time.Sleep(40 * time.Second)
   126  
   127  	{
   128  		var val *util.Uint128
   129  		r, err := ac.GetAccountState(context.Background(), &rpcpb.GetAccountStateRequest{Address: to})
   130  		if err != nil {
   131  			log.Println("GetAccountState", to, "failed", err)
   132  		} else if val, err = util.NewUint128FromString(r.GetBalance()); err != nil {
   133  			log.Println("GetAccountState", from, "failed to get balance", err)
   134  		} else {
   135  			nonce = r.Nonce
   136  			// nonce = r.Nonce
   137  			log.Println("GetAccountState", to, "nonce", r.Nonce, "value", val)
   138  		}
   139  	}
   140  
   141  	{
   142  		stream, err := ac.Subscribe(context.Background(), &rpcpb.SubscribeRequest{})
   143  
   144  		if err != nil {
   145  			log.Fatalf("could not subscribe: %v", err)
   146  		}
   147  		for {
   148  			reply, err := stream.Recv()
   149  			if err == io.EOF {
   150  				break
   151  			}
   152  			if err != nil {
   153  				log.Printf("failed to recv: %v", err)
   154  			}
   155  			log.Println("recv notification: ", reply.Topic, reply.Data)
   156  		}
   157  	}
   158  }