cosmos-sdk/tests/tendermint/main.go

141 lines
3.6 KiB
Go
Raw Normal View History

package main
import (
"fmt"
2016-02-14 13:12:38 -08:00
"time"
"github.com/gorilla/websocket"
2016-02-16 12:29:54 -08:00
"github.com/tendermint/basecoin/types"
2017-03-16 01:01:29 -07:00
wire "github.com/tendermint/go-wire"
2017-04-27 09:52:47 -07:00
_ "github.com/tendermint/tendermint/rpc/core/types" // Register RPCResponse > Result types
"github.com/tendermint/tendermint/rpc/lib/client"
"github.com/tendermint/tendermint/rpc/lib/types"
cmn "github.com/tendermint/tmlibs/common"
)
func main() {
2016-03-28 09:35:19 -07:00
// ws := rpcclient.NewWSClient("127.0.0.1:46657", "/websocket")
ws := rpcclient.NewWSClient("192.168.99.100:46657", "/websocket")
chainID := "test_chain_id"
_, err := ws.Start()
if err != nil {
cmn.Exit(err.Error())
}
2016-02-14 13:12:38 -08:00
var counter = 0
// Read a bunch of responses
go func() {
for {
res, ok := <-ws.ResultsCh
if !ok {
break
}
fmt.Println(counter, "res:", cmn.Blue(string(res)))
}
}()
// Get the root account
root := types.PrivAccountFromSecret("test")
2016-03-20 03:00:43 -07:00
sequence := int(0)
// Make a bunch of PrivAccounts
privAccounts := types.RandAccounts(1000, 1000000, 0)
2016-02-08 15:01:26 -08:00
privAccountSequences := make(map[string]int)
// Send coins to each account
for i := 0; i < len(privAccounts); i++ {
privAccount := privAccounts[i]
2016-03-15 15:01:53 -07:00
tx := &types.SendTx{
2016-03-20 03:00:43 -07:00
Inputs: []types.TxInput{
types.TxInput{
Address: root.Account.PubKey.Address(),
PubKey: root.Account.PubKey, // TODO is this needed?
2016-04-01 15:19:07 -07:00
Coins: types.Coins{{"", 1000002}},
Sequence: sequence,
},
},
2016-03-20 03:00:43 -07:00
Outputs: []types.TxOutput{
types.TxOutput{
Address: privAccount.Account.PubKey.Address(),
2016-04-01 15:19:07 -07:00
Coins: types.Coins{{"", 1000000}},
},
},
}
sequence += 1
// Sign request
2016-03-28 09:35:19 -07:00
signBytes := tx.SignBytes(chainID)
sig := root.Sign(signBytes)
tx.Inputs[0].Signature = sig
//fmt.Println("tx:", tx)
// Write request
2016-03-30 13:20:55 -07:00
txBytes := wire.BinaryBytes(struct{ types.Tx }{tx})
request, err := rpctypes.MapToRequest("fakeid", "broadcast_tx_sync", map[string]interface{}{"tx": txBytes})
if err != nil {
cmn.Exit("cannot encode request: " + err.Error())
}
reqBytes := wire.JSONBytes(request)
//fmt.Print(".")
err = ws.WriteMessage(websocket.TextMessage, reqBytes)
if err != nil {
cmn.Exit("writing websocket request: " + err.Error())
}
}
2016-02-08 15:01:26 -08:00
// Now send coins between these accounts
for {
2016-02-14 13:12:38 -08:00
counter += 1
time.Sleep(time.Millisecond * 10)
randA := cmn.RandInt() % len(privAccounts)
randB := cmn.RandInt() % len(privAccounts)
2016-02-08 15:01:26 -08:00
if randA == randB {
continue
}
privAccountA := privAccounts[randA]
2016-03-20 03:00:43 -07:00
privAccountASequence := privAccountSequences[privAccountA.Account.PubKey.KeyString()]
privAccountSequences[privAccountA.Account.PubKey.KeyString()] = privAccountASequence + 1
2016-02-08 15:01:26 -08:00
privAccountB := privAccounts[randB]
2016-03-15 15:01:53 -07:00
tx := &types.SendTx{
2016-03-20 03:00:43 -07:00
Inputs: []types.TxInput{
types.TxInput{
Address: privAccountA.Account.PubKey.Address(),
PubKey: privAccountA.Account.PubKey,
2016-04-01 15:19:07 -07:00
Coins: types.Coins{{"", 3}},
2016-03-28 09:35:19 -07:00
Sequence: privAccountASequence + 1,
2016-02-08 15:01:26 -08:00
},
},
2016-03-20 03:00:43 -07:00
Outputs: []types.TxOutput{
types.TxOutput{
Address: privAccountB.Account.PubKey.Address(),
2016-04-01 15:19:07 -07:00
Coins: types.Coins{{"", 1}},
2016-02-08 15:01:26 -08:00
},
},
}
// Sign request
2016-03-28 09:35:19 -07:00
signBytes := tx.SignBytes(chainID)
sig := privAccountA.Sign(signBytes)
tx.Inputs[0].Signature = sig
2016-02-08 15:01:26 -08:00
//fmt.Println("tx:", tx)
// Write request
2016-03-30 13:20:55 -07:00
txBytes := wire.BinaryBytes(struct{ types.Tx }{tx})
request, err := rpctypes.MapToRequest("fakeid", "broadcast_tx_sync", map[string]interface{}{"tx": txBytes})
if err != nil {
cmn.Exit("cannot encode request: " + err.Error())
}
2016-02-08 15:01:26 -08:00
reqBytes := wire.JSONBytes(request)
//fmt.Print(".")
err = ws.WriteMessage(websocket.TextMessage, reqBytes)
2016-02-08 15:01:26 -08:00
if err != nil {
cmn.Exit("writing websocket request: " + err.Error())
}
2016-02-08 15:01:26 -08:00
}
ws.Stop()
}