From f2275c3089da05c489a2adaa3b1829eb67d49caf Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Fri, 13 Jan 2017 04:27:07 -0500 Subject: [PATCH] successful vote plugin tests --- app/app.go | 6 +++ plugins/vote/vote.go | 92 +++++++++++++++++++++++++++++++++++ plugins/vote/vote_test.go | 100 ++++++++++++++++++++++++++++++++++++++ tests/common.go | 14 ++++++ tests/tmsp/main.go | 20 ++------ 5 files changed, 215 insertions(+), 17 deletions(-) create mode 100644 plugins/vote/vote.go create mode 100644 plugins/vote/vote_test.go diff --git a/app/app.go b/app/app.go index 98fcee879b..c4e5964739 100644 --- a/app/app.go +++ b/app/app.go @@ -17,9 +17,11 @@ const ( PluginTypeByteBase = 0x01 PluginTypeByteEyes = 0x02 + PluginTypeByteVote = 0x03 PluginNameBase = "base" PluginNameEyes = "eyes" + PluginNameVote = "vote" ) type Basecoin struct { @@ -45,6 +47,10 @@ func (app *Basecoin) Info() string { return Fmt("Basecoin v%v", version) } +func (app *Basecoin) RegisterPlugin(typeByte byte, name string, plugin types.Plugin) { + app.plugins.RegisterPlugin(typeByte, name, plugin) +} + // TMSP::SetOption func (app *Basecoin) SetOption(key string, value string) (log string) { PluginName, key := splitKey(key) diff --git a/plugins/vote/vote.go b/plugins/vote/vote.go new file mode 100644 index 0000000000..3f75e3447f --- /dev/null +++ b/plugins/vote/vote.go @@ -0,0 +1,92 @@ +package vote + +import ( + "github.com/tendermint/basecoin/types" + "github.com/tendermint/go-wire" + tmsp "github.com/tendermint/tmsp/types" +) + +type Vote struct { + bb *ballotBox +} + +type ballotBox struct { + issue string + votesYes int + votesNo int +} + +type Tx struct { + voteYes bool +} + +func NewVoteInstance(issue string) Vote { + return Vote{ + &ballotBox{ + issue: issue, + votesYes: 0, + votesNo: 0, + }, + } +} + +func (app Vote) SetOption(store types.KVStore, key string, value string) (log string) { + return "" +} + +//because no coins are being exchanged ctx is unused +func (app Vote) RunTx(store types.KVStore, ctx types.CallContext, txBytes []byte) (res tmsp.Result) { + + // Decode tx + var tx Tx + err := wire.ReadBinaryBytes(txBytes, &tx) + if err != nil { + return tmsp.ErrBaseEncodingError.AppendLog("Error decoding tx: " + err.Error()) + } + + //Read the ballotBox from the store + kvBytes := store.Get([]byte(app.bb.issue)) + var tempBB ballotBox + + //does the issue already exist? + if kvBytes != nil { + err := wire.ReadBinaryBytes(kvBytes, &tempBB) + if err != nil { + return tmsp.ErrBaseEncodingError.AppendLog("Error decoding BallotBox: " + err.Error()) + } + } else { + + //TODO add extra fee for opening new issue + + tempBB = ballotBox{ + issue: app.bb.issue, + votesYes: 0, + votesNo: 0, + } + issueBytes := wire.BinaryBytes(struct{ ballotBox }{tempBB}) + store.Set([]byte(app.bb.issue), issueBytes) + } + + //Write the updated ballotBox to the store + if tx.voteYes { + tempBB.votesYes += 1 + } else { + tempBB.votesNo += 1 + } + issueBytes := wire.BinaryBytes(struct{ ballotBox }{tempBB}) + store.Set([]byte(app.bb.issue), issueBytes) + + return tmsp.OK +} + +//unused +func (app Vote) InitChain(store types.KVStore, vals []*tmsp.Validator) { +} + +func (app Vote) BeginBlock(store types.KVStore, height uint64) { +} + +func (app Vote) EndBlock(store types.KVStore, height uint64) []*tmsp.Validator { + var diffs []*tmsp.Validator + return diffs +} diff --git a/plugins/vote/vote_test.go b/plugins/vote/vote_test.go new file mode 100644 index 0000000000..1d6181cc80 --- /dev/null +++ b/plugins/vote/vote_test.go @@ -0,0 +1,100 @@ +package vote + +import ( + "fmt" + "testing" + + "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" + eyescli "github.com/tendermint/merkleeyes/client" +) + +func TestVote(t *testing.T) { + //base initialization + eyesCli := eyescli.NewLocalClient() + chainID := "test_chain_id" + bcApp := app.NewBasecoin(eyesCli) + bcApp.SetOption("base/chainID", chainID) + fmt.Println(bcApp.Info()) + + //account initialization + test1PrivAcc := tests.PrivAccountFromSecret("test1") + + // Seed Basecoin with account + test1Acc := test1PrivAcc.Account + test1Acc.Balance = types.Coins{{"", 1000}} + fmt.Println(bcApp.SetOption("base/account", string(wire.JSONBytes(test1Acc)))) + + //vote initialization + votePlugin := NewVoteInstance("humanRights") + var typeByte byte = app.PluginTypeByteVote + bcApp.RegisterPlugin( + typeByte, + app.PluginNameVote, + votePlugin, + ) + + //commit + res := bcApp.Commit() + if res.IsErr() { + Exit(Fmt("Failed Commit: %v", res.Error())) + } + + //transaction sequence number + seqNum := 1 + + //Construct, Sign, Write function variable + CSW := func(fees, sendCoins int64) { + // Construct an AppTx signature + tx := &types.AppTx{ + Fee: fees, + Gas: 0, + Type: typeByte, + Input: tests.MakeInput(test1Acc.PubKey, types.Coins{{"", sendCoins}}, seqNum), + Data: wire.BinaryBytes(struct{ Tx }{Tx{voteYes: true}}), //a vote for human rights + } + + // Sign request + signBytes := tx.SignBytes(chainID) + fmt.Printf("Sign bytes: %X\n", signBytes) + sig := test1PrivAcc.PrivKey.Sign(signBytes) + tx.Input.Signature = sig + fmt.Printf("Signed TX bytes: %X\n", wire.BinaryBytes(struct{ types.Tx }{tx})) + + // Write request + txBytes := wire.BinaryBytes(struct{ types.Tx }{tx}) + res = bcApp.AppendTx(txBytes) + fmt.Println(res) + + if res.IsOK() { + seqNum += 1 + } + } + + //Test a basic send, no fees + CSW(0, 1) + if res.IsErr() { + Exit(Fmt("Failed: %v", res.Error())) + } + + //Test fee prevented transaction + CSW(2, 1) + if res.IsOK() { + Exit(Fmt("expected bad transaction")) + } + + //Test equal fees + CSW(2, 2) + if res.IsErr() { + Exit(Fmt("Failed: %v", res.Error())) + } + + //Test more send coins than fees + CSW(2, 3) + if res.IsErr() { + Exit(Fmt("Failed: %v", res.Error())) + } +} diff --git a/tests/common.go b/tests/common.go index 1a9f3aee35..5e0d4ac32d 100644 --- a/tests/common.go +++ b/tests/common.go @@ -44,3 +44,17 @@ func RandAccounts(num int, minAmount int64, maxAmount int64) []types.PrivAccount return privAccs } + +//make input term for the AppTx or SendTx Types +func MakeInput(pubKey crypto.PubKey, coins types.Coins, sequence int) types.TxInput { + input := types.TxInput{ + Address: pubKey.Address(), + PubKey: pubKey, + Coins: coins, + Sequence: sequence, + } + if sequence > 1 { + input.PubKey = nil + } + return input +} diff --git a/tests/tmsp/main.go b/tests/tmsp/main.go index eea1cbe803..26d7d6dbae 100644 --- a/tests/tmsp/main.go +++ b/tests/tmsp/main.go @@ -7,7 +7,6 @@ import ( "github.com/tendermint/basecoin/tests" "github.com/tendermint/basecoin/types" . "github.com/tendermint/go-common" - "github.com/tendermint/go-crypto" "github.com/tendermint/go-wire" eyescli "github.com/tendermint/merkleeyes/client" ) @@ -42,7 +41,7 @@ func testSendTx() { Fee: 0, Gas: 0, Inputs: []types.TxInput{ - makeInput(test1PrivAcc.Account.PubKey, types.Coins{{"", 1}}, 1), + tests.MakeInput(test1PrivAcc.Account.PubKey, types.Coins{{"", 1}}, 1), }, Outputs: []types.TxOutput{ types.TxOutput{ @@ -100,7 +99,7 @@ func testSequence() { Fee: 2, Gas: 2, Inputs: []types.TxInput{ - makeInput(test1Acc.PubKey, types.Coins{{"", 1000002}}, sequence), + tests.MakeInput(test1Acc.PubKey, types.Coins{{"", 1000002}}, sequence), }, Outputs: []types.TxOutput{ types.TxOutput{ @@ -150,7 +149,7 @@ func testSequence() { Fee: 2, Gas: 2, Inputs: []types.TxInput{ - makeInput(privAccountA.Account.PubKey, types.Coins{{"", 3}}, privAccountASequence+1), + tests.MakeInput(privAccountA.Account.PubKey, types.Coins{{"", 3}}, privAccountASequence+1), }, Outputs: []types.TxOutput{ types.TxOutput{ @@ -174,16 +173,3 @@ func testSequence() { } } } - -func makeInput(pubKey crypto.PubKey, coins types.Coins, sequence int) types.TxInput { - input := types.TxInput{ - Address: pubKey.Address(), - PubKey: pubKey, - Coins: coins, - Sequence: sequence, - } - if sequence > 1 { - input.PubKey = nil - } - return input -}