cosmos-sdk/tests/tendermint/main.go

133 lines
3.2 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/tests"
"github.com/tendermint/basecoin/types"
. "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-15 15:01:53 -07:00
ws := rpcclient.NewWSClient("127.0.0.1:46657", "/websocket")
_, err := ws.Start()
if err != nil {
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
}
2016-02-14 13:12:38 -08:00
fmt.Println(counter, "res:", Blue(string(res)))
}
}()
// Get the root account
root := tests.PrivAccountFromSecret("root")
2016-03-20 03:00:43 -07:00
sequence := int(0)
// Make a bunch of PrivAccounts
privAccounts := tests.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?
Amount: 1000002,
Sequence: sequence,
},
},
2016-03-20 03:00:43 -07:00
Outputs: []types.TxOutput{
types.TxOutput{
Address: privAccount.Account.PubKey.Address(),
Amount: 1000000,
},
},
}
sequence += 1
// Sign request
signBytes := wire.BinaryBytes(tx)
sig := root.PrivKey.Sign(signBytes)
tx.Inputs[0].Signature = sig
//fmt.Println("tx:", tx)
// Write request
txBytes := wire.BinaryBytes(tx)
request := rpctypes.NewRPCRequest("fakeid", "broadcast_tx_sync", Arr(txBytes))
reqBytes := wire.JSONBytes(request)
//fmt.Print(".")
err := ws.WriteMessage(websocket.TextMessage, reqBytes)
if err != nil {
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)
2016-02-08 15:01:26 -08:00
randA := RandInt() % len(privAccounts)
randB := RandInt() % len(privAccounts)
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-02-08 15:01:26 -08:00
Amount: 3,
2016-03-20 03:00:43 -07:00
Sequence: privAccountASequence,
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(),
Amount: 1,
2016-02-08 15:01:26 -08:00
},
},
}
// Sign request
signBytes := wire.BinaryBytes(tx)
sig := privAccountA.PrivKey.Sign(signBytes)
tx.Inputs[0].Signature = sig
//fmt.Println("tx:", tx)
// Write request
txBytes := wire.BinaryBytes(tx)
request := rpctypes.NewRPCRequest("fakeid", "broadcast_tx_sync", Arr(txBytes))
reqBytes := wire.JSONBytes(request)
//fmt.Print(".")
err := ws.WriteMessage(websocket.TextMessage, reqBytes)
if err != nil {
Exit("writing websocket request: " + err.Error())
}
2016-02-08 15:01:26 -08:00
}
ws.Stop()
}