2016-03-24 11:27:44 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/tendermint/basecoin/app"
|
|
|
|
"github.com/tendermint/basecoin/tests"
|
|
|
|
"github.com/tendermint/basecoin/types"
|
|
|
|
. "github.com/tendermint/go-common"
|
|
|
|
"github.com/tendermint/go-wire"
|
2016-03-24 14:10:17 -07:00
|
|
|
govtypes "github.com/tendermint/governmint/types"
|
2016-03-24 11:27:44 -07:00
|
|
|
eyescli "github.com/tendermint/merkleeyes/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2016-03-28 09:35:19 -07:00
|
|
|
//testSendTx()
|
|
|
|
//testGov()
|
|
|
|
testSequence()
|
2016-03-24 14:10:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func testSendTx() {
|
2016-03-24 11:27:44 -07:00
|
|
|
eyesCli := eyescli.NewLocalClient()
|
|
|
|
bcApp := app.NewBasecoin(eyesCli)
|
|
|
|
fmt.Println(bcApp.Info())
|
|
|
|
|
|
|
|
tPriv := tests.PrivAccountFromSecret("test")
|
|
|
|
tPriv2 := tests.PrivAccountFromSecret("test2")
|
|
|
|
|
|
|
|
// Seed Basecoin with account
|
|
|
|
tAcc := tPriv.Account
|
|
|
|
tAcc.Balance = 1000
|
2016-03-27 12:47:50 -07:00
|
|
|
fmt.Println(bcApp.SetOption("base/chainID", "test_chain_id"))
|
|
|
|
fmt.Println(bcApp.SetOption("base/account", string(wire.JSONBytes(tAcc))))
|
2016-03-24 11:27:44 -07:00
|
|
|
|
|
|
|
// Construct a SendTx signature
|
|
|
|
tx := &types.SendTx{
|
|
|
|
Inputs: []types.TxInput{
|
|
|
|
types.TxInput{
|
|
|
|
Address: tPriv.Account.PubKey.Address(),
|
|
|
|
PubKey: tPriv.Account.PubKey, // TODO is this needed?
|
|
|
|
Amount: 1,
|
|
|
|
Sequence: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Outputs: []types.TxOutput{
|
|
|
|
types.TxOutput{
|
|
|
|
Address: tPriv2.Account.PubKey.Address(),
|
|
|
|
Amount: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign request
|
|
|
|
signBytes := tx.SignBytes("test_chain_id")
|
2016-03-27 12:47:50 -07:00
|
|
|
fmt.Printf("Sign bytes: %X\n", signBytes)
|
2016-03-24 11:27:44 -07:00
|
|
|
sig := tPriv.PrivKey.Sign(signBytes)
|
|
|
|
tx.Inputs[0].Signature = sig
|
|
|
|
//fmt.Println("tx:", tx)
|
2016-03-27 12:47:50 -07:00
|
|
|
fmt.Printf("Signed TX bytes: %X\n", wire.BinaryBytes(tx))
|
2016-03-24 11:27:44 -07:00
|
|
|
|
|
|
|
// Write request
|
|
|
|
txBytes := wire.BinaryBytes(tx)
|
|
|
|
res := bcApp.AppendTx(txBytes)
|
|
|
|
fmt.Println(res)
|
2016-03-24 14:10:17 -07:00
|
|
|
if res.IsErr() {
|
|
|
|
Exit(Fmt("Failed: %v", res.Error()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testGov() {
|
|
|
|
eyesCli := eyescli.NewLocalClient()
|
|
|
|
bcApp := app.NewBasecoin(eyesCli)
|
|
|
|
fmt.Println(bcApp.Info())
|
|
|
|
|
|
|
|
tPriv := tests.PrivAccountFromSecret("test")
|
|
|
|
|
|
|
|
// Seed Basecoin with admin using PrivAccount
|
|
|
|
tAcc := tPriv.Account
|
|
|
|
adminEntity := govtypes.Entity{
|
|
|
|
ID: "",
|
|
|
|
PubKey: tAcc.PubKey,
|
|
|
|
}
|
2016-03-27 12:47:50 -07:00
|
|
|
log := bcApp.SetOption("gov/admin", string(wire.JSONBytes(adminEntity)))
|
2016-03-24 14:10:17 -07:00
|
|
|
if log != "Success" {
|
|
|
|
Exit(Fmt("Failed to set option: %v", log))
|
|
|
|
}
|
|
|
|
// TODO test proposals or something.
|
2016-03-24 11:27:44 -07:00
|
|
|
}
|
2016-03-28 09:35:19 -07:00
|
|
|
|
|
|
|
func testSequence() {
|
|
|
|
eyesCli := eyescli.NewLocalClient()
|
|
|
|
bcApp := app.NewBasecoin(eyesCli)
|
|
|
|
chainID := "test_chain_id"
|
|
|
|
|
|
|
|
// Get the root account
|
|
|
|
root := tests.PrivAccountFromSecret("test")
|
|
|
|
rootAcc := root.Account
|
|
|
|
rootAcc.Balance = 1 << 53
|
|
|
|
fmt.Println(bcApp.SetOption("base/chainID", "test_chain_id"))
|
|
|
|
fmt.Println(bcApp.SetOption("base/account", string(wire.JSONBytes(rootAcc))))
|
|
|
|
|
|
|
|
sequence := int(1)
|
|
|
|
// Make a bunch of PrivAccounts
|
|
|
|
privAccounts := tests.RandAccounts(1000, 1000000, 0)
|
|
|
|
privAccountSequences := make(map[string]int)
|
|
|
|
|
|
|
|
// Send coins to each account
|
|
|
|
for i := 0; i < len(privAccounts); i++ {
|
|
|
|
privAccount := privAccounts[i]
|
|
|
|
tx := &types.SendTx{
|
|
|
|
Inputs: []types.TxInput{
|
|
|
|
types.TxInput{
|
|
|
|
Address: root.Account.PubKey.Address(),
|
|
|
|
PubKey: root.Account.PubKey, // TODO is this needed?
|
|
|
|
Amount: 1000002,
|
|
|
|
Sequence: sequence,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Outputs: []types.TxOutput{
|
|
|
|
types.TxOutput{
|
|
|
|
Address: privAccount.Account.PubKey.Address(),
|
|
|
|
Amount: 1000000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
sequence += 1
|
|
|
|
|
|
|
|
// Sign request
|
|
|
|
signBytes := tx.SignBytes(chainID)
|
|
|
|
sig := root.PrivKey.Sign(signBytes)
|
|
|
|
tx.Inputs[0].Signature = sig
|
2016-03-29 14:25:17 -07:00
|
|
|
// fmt.Printf("ADDR: %X -> %X\n", tx.Inputs[0].Address, tx.Outputs[0].Address)
|
2016-03-28 09:35:19 -07:00
|
|
|
|
|
|
|
// Write request
|
|
|
|
txBytes := wire.BinaryBytes(tx)
|
2016-03-29 14:25:17 -07:00
|
|
|
res := bcApp.AppendTx(txBytes)
|
2016-03-28 09:35:19 -07:00
|
|
|
if res.IsErr() {
|
|
|
|
Exit("AppendTx error: " + res.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-29 14:25:17 -07:00
|
|
|
fmt.Println("-------------------- RANDOM SENDS --------------------")
|
|
|
|
|
2016-03-28 09:35:19 -07:00
|
|
|
// Now send coins between these accounts
|
|
|
|
for {
|
|
|
|
randA := RandInt() % len(privAccounts)
|
|
|
|
randB := RandInt() % len(privAccounts)
|
|
|
|
if randA == randB {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
privAccountA := privAccounts[randA]
|
|
|
|
privAccountASequence := privAccountSequences[privAccountA.Account.PubKey.KeyString()]
|
|
|
|
privAccountSequences[privAccountA.Account.PubKey.KeyString()] = privAccountASequence + 1
|
|
|
|
privAccountB := privAccounts[randB]
|
|
|
|
|
|
|
|
tx := &types.SendTx{
|
|
|
|
Inputs: []types.TxInput{
|
|
|
|
types.TxInput{
|
|
|
|
Address: privAccountA.Account.PubKey.Address(),
|
|
|
|
PubKey: privAccountA.Account.PubKey,
|
|
|
|
Amount: 3,
|
|
|
|
Sequence: privAccountASequence + 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Outputs: []types.TxOutput{
|
|
|
|
types.TxOutput{
|
|
|
|
Address: privAccountB.Account.PubKey.Address(),
|
|
|
|
Amount: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign request
|
|
|
|
signBytes := tx.SignBytes(chainID)
|
|
|
|
sig := privAccountA.PrivKey.Sign(signBytes)
|
|
|
|
tx.Inputs[0].Signature = sig
|
2016-03-29 14:25:17 -07:00
|
|
|
// fmt.Printf("ADDR: %X -> %X\n", tx.Inputs[0].Address, tx.Outputs[0].Address)
|
2016-03-28 09:35:19 -07:00
|
|
|
|
|
|
|
// Write request
|
|
|
|
txBytes := wire.BinaryBytes(tx)
|
|
|
|
res := bcApp.AppendTx(txBytes)
|
|
|
|
if res.IsErr() {
|
|
|
|
Exit("AppendTx error: " + res.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|