cosmos-sdk/tests/tendermint/main.go

136 lines
3.5 KiB
Go
Raw Normal View History

package main
import (
"fmt"
2016-02-14 13:12:38 -08:00
"time"
"github.com/gorilla/websocket"
"github.com/tendermint/basecoin/testutils"
2016-02-16 12:29:54 -08:00
"github.com/tendermint/basecoin/types"
cmn "github.com/tendermint/go-common"
"github.com/tendermint/go-rpc/client"
"github.com/tendermint/go-rpc/types"
"github.com/tendermint/go-wire"
_ "github.com/tendermint/tendermint/rpc/core/types" // Register RPCResponse > Result types
)
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 := testutils.PrivAccountFromSecret("test")
2016-03-20 03:00:43 -07:00
sequence := int(0)
// Make a bunch of PrivAccounts
privAccounts := testutils.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.PrivKey.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 := rpctypes.NewRPCRequest("fakeid", "broadcast_tx_sync", cmn.Arr(txBytes))
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)
2016-02-08 15:01:26 -08:00
sig := privAccountA.PrivKey.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 := rpctypes.NewRPCRequest("fakeid", "broadcast_tx_sync", cmn.Arr(txBytes))
2016-02-08 15:01:26 -08:00
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
}
ws.Stop()
}