Skip to content

Commit

Permalink
app.GetState and fix paytovote test
Browse files Browse the repository at this point in the history
  • Loading branch information
ebuchman committed Feb 3, 2017
1 parent 8ce10e2 commit f033c19
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 43 deletions.
4 changes: 2 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const (

type Basecoin struct {
eyesCli *eyes.Client
state *sm.State
cacheState *sm.State
state *sm.State // for block processing (deliver tx, etc.)
cacheState *sm.State // just for checktx, derived from state on commit
plugins *types.Plugins
}

Expand Down
18 changes: 8 additions & 10 deletions plugins/paytovote/paytovote.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package paytovote

import (
"fmt"

abci "github.com/tendermint/abci/types"
"github.com/tendermint/basecoin/state"
"github.com/tendermint/basecoin/types"
cmn "github.com/tendermint/go-common"
"github.com/tendermint/go-wire"
)

Expand Down Expand Up @@ -66,24 +65,24 @@ func NewVoteTxBytes(issue string, voteTypeByte byte) []byte {
type P2VIssue struct {
Issue string
FeePerVote types.Coins
votesFor int
votesAgainst int
VotesFor int
VotesAgainst int
}

func newP2VIssue(issue string, feePerVote types.Coins) P2VIssue {
return P2VIssue{
Issue: issue,
FeePerVote: feePerVote,
votesFor: 0,
votesAgainst: 0,
VotesFor: 0,
VotesAgainst: 0,
}
}

func IssueKey(issue string) []byte {
//The state key is defined as only being affected by effected issue
// aka. if multiple paytovote plugins are initialized
// then all will have access to the same issue vote counts
return []byte(fmt.Sprintf("P2VPlugin{issue=%v}.State", issue))
return []byte(cmn.Fmt("P2VPlugin{issue=%v}.State", issue))
}

func getIssue(store types.KVStore, issue string) (p2vIssue P2VIssue, err error) {
Expand Down Expand Up @@ -215,14 +214,13 @@ func (p2v *P2VPlugin) runTxVote(store types.KVStore, ctx types.CallContext, txBy
//Transaction Logic
switch tx.VoteTypeByte {
case TypeByteVoteFor:
p2vIssue.votesFor += 1
p2vIssue.VotesFor += 1
case TypeByteVoteAgainst:
p2vIssue.votesAgainst += 1
p2vIssue.VotesAgainst += 1
default:
return abci.ErrInternalError.AppendLog("P2VTx.VoteTypeByte was not recognized")
}

fmt.Println(p2vIssue.votesFor)
// Save P2VIssue, charge fee, return
store.Set(IssueKey(tx.Issue), wire.BinaryBytes(p2vIssue))
chargeFee(store, ctx, p2vIssue.FeePerVote)
Expand Down
48 changes: 17 additions & 31 deletions plugins/paytovote/paytovote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import (
"testing"

"github.com/stretchr/testify/assert"
abci "github.com/tendermint/abci/types"

"github.com/tendermint/basecoin/app"
//cmds "github.com/tendermint/basecoin/cmd/basecoin/commands"
"github.com/tendermint/basecoin/state"
"github.com/tendermint/basecoin/testutils"
"github.com/tendermint/basecoin/types"

abci "github.com/tendermint/abci/types"
cmn "github.com/tendermint/go-common"
"github.com/tendermint/go-wire"
eyescli "github.com/tendermint/merkleeyes/client"
)
Expand All @@ -34,6 +37,7 @@ func TestP2VPlugin(t *testing.T) {
startBal := types.Coins{{"", 1000}, {"issueToken", 1000}, {"voteToken", 1000}}
test1Acc.Balance = startBal
bcApp.SetOption("base/account", string(wire.JSONBytes(test1Acc)))
bcApp.Commit()

deliverTx := func(gas int64,
fee types.Coin,
Expand Down Expand Up @@ -62,19 +66,9 @@ func TestP2VPlugin(t *testing.T) {

testBalance := func(expected types.Coins) {

acc := state.GetAccount(store, test1Acc.PubKey.Address())

//TODO debug testBalance (acc returns nil, bad store?)
/*acc, err := cmds.GetAcc(cmds.NodeFlag.Value, test1Acc.PubKey.Address())
if err != nil {
t.Errorf(cmds.NodeFlag.Value)
t.Errorf("error retrieving account for account balance check: %v", err.Error())
return
}*/

acc := state.GetAccount(bcApp.GetState(), test1Acc.PubKey.Address())
if acc == nil {
t.Errorf("nil account when trying compare balance")
return
panic("nil account when trying compare balance")
}

bal := acc.Balance
Expand All @@ -87,36 +81,35 @@ func TestP2VPlugin(t *testing.T) {
balStr += " " + bal[i].String()
}

t.Errorf("bad balance expected %v, got %v", expStr, balStr)
panic(cmn.Fmt("bad balance expected %v, got %v", expStr, balStr))
}
}

//test for an issue that shouldn't exist
testNoIssue := func(issue string) {
_, err := getIssue(store, issue)
_, err := getIssue(bcApp.GetState(), issue)
if err == nil {
t.Errorf("issue that shouldn't exist was found, issue: %v", issue)
panic(cmn.Fmt("issue that shouldn't exist was found, issue: %v", issue))
}
}

//test for an issue that should exist
testIssue := func(issue string, expFor, expAgainst int) {
p2vIssue, err := getIssue(store, issue)
p2vIssue, err := getIssue(bcApp.GetState(), issue)

return //TODO fix these tests, bad store being accessed
// return //TODO fix these tests, bad store being accessed

//test for errors
if err != nil {
t.Errorf("error loading issue %v for issue test, error: %v", issue, err.Error())
return
panic(cmn.Fmt("error loading issue %v for issue test, error: %v", issue, err.Error()))
}

if p2vIssue.votesFor != expFor {
t.Errorf("expected %v votes-for, got %v votes-for, for issue %v", expFor, p2vIssue.votesFor, issue)
if p2vIssue.VotesFor != expFor {
panic(cmn.Fmt("expected %v votes-for, got %v votes-for, for issue %v", expFor, p2vIssue.VotesFor, issue))
}

if p2vIssue.votesAgainst != expAgainst {
t.Errorf("expected %v votes-against, got %v votes-against, for issue %v", expAgainst, p2vIssue.votesAgainst, issue)
if p2vIssue.VotesAgainst != expAgainst {
panic(cmn.Fmt("expected %v votes-against, got %v votes-against, for issue %v", expAgainst, p2vIssue.VotesAgainst, issue))
}
}

Expand All @@ -130,53 +123,46 @@ func TestP2VPlugin(t *testing.T) {
res := deliverTx(0, types.Coin{}, types.Coins{{"", 1}, {"issueToken", 1}, {"voteToken", 2}}, 1,
NewCreateIssueTxBytes(issue1, types.Coins{{"voteToken", 2}}, types.Coins{{"issueToken", 1}}))
assert.True(t, res.IsOK(), res.String())
bcApp.Commit()
testBalance(startBal.Minus(types.Coins{{"issueToken", 1}}))
testIssue(issue1, 0, 0)

// Test a basic votes
res = deliverTx(0, types.Coin{}, types.Coins{{"", 1}, {"issueToken", 1}, {"voteToken", 2}}, 2,
NewVoteTxBytes(issue1, TypeByteVoteFor))
assert.True(t, res.IsOK(), res.String())
bcApp.Commit()
testBalance(startBal.Minus(types.Coins{{"issueToken", 1}, {"voteToken", 2}}))
testIssue(issue1, 1, 0)

res = deliverTx(0, types.Coin{}, types.Coins{{"", 1}, {"issueToken", 1}, {"voteToken", 2}}, 3,
NewVoteTxBytes(issue1, TypeByteVoteAgainst))
assert.True(t, res.IsOK(), res.String())
bcApp.Commit()
testBalance(startBal.Minus(types.Coins{{"issueToken", 1}, {"voteToken", 4}}))
testIssue(issue1, 1, 1)

// Test prevented voting on non-existent issue
res = deliverTx(0, types.Coin{}, types.Coins{{"", 1}, {"issueToken", 1}, {"voteToken", 2}}, 4,
NewVoteTxBytes(issue2, TypeByteVoteFor))
assert.True(t, res.IsErr(), res.String())
bcApp.Commit()
testBalance(startBal.Minus(types.Coins{{"issueToken", 1}, {"voteToken", 4}}))
testNoIssue(issue2)

// Test prevented duplicate issue generation
res = deliverTx(0, types.Coin{}, types.Coins{{"", 1}, {"issueToken", 1}, {"voteToken", 2}}, 5,
NewCreateIssueTxBytes(issue1, types.Coins{{"voteToken", 1}}, types.Coins{{"issueToken", 1}}))
assert.True(t, res.IsErr(), res.String())
bcApp.Commit()
testBalance(startBal.Minus(types.Coins{{"issueToken", 1}, {"voteToken", 4}}))

// Test prevented issue generation from insufficient funds
res = deliverTx(0, types.Coin{}, types.Coins{{"", 1}, {"issueToken", 1}, {"voteToken", 2}}, 6,
NewCreateIssueTxBytes(issue2, types.Coins{{"voteToken", 1}}, types.Coins{{"issueToken", 2}}))
assert.True(t, res.IsErr(), res.String())
bcApp.Commit()
testBalance(startBal.Minus(types.Coins{{"issueToken", 1}, {"voteToken", 4}}))
testNoIssue(issue2)

// Test prevented voting from insufficient funds
res = deliverTx(0, types.Coin{}, types.Coins{{"", 1}, {"issueToken", 1}, {"voteToken", 1}}, 7,
NewVoteTxBytes(issue1, TypeByteVoteFor))
assert.True(t, res.IsErr(), res.String())
bcApp.Commit()
testBalance(startBal.Minus(types.Coins{{"issueToken", 1}, {"voteToken", 4}}))
testIssue(issue1, 1, 1)
}

0 comments on commit f033c19

Please sign in to comment.