Skip to content

Commit

Permalink
Vote->Counter; Fee is types.Coin; Context has Account; Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jaekwon committed Jan 15, 2017
1 parent 5ba8250 commit 16a6680
Show file tree
Hide file tree
Showing 14 changed files with 319 additions and 286 deletions.
10 changes: 5 additions & 5 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func (app *Basecoin) Info() abci.ResponseInfo {
return abci.ResponseInfo{Data: Fmt("Basecoin v%v", version)}
}

func (app *Basecoin) RegisterPlugin(name string, plugin types.Plugin) {
app.plugins.RegisterPlugin(name, plugin)
func (app *Basecoin) RegisterPlugin(plugin types.Plugin) {
app.plugins.RegisterPlugin(plugin)
}

// TMSP::SetOption
Expand Down Expand Up @@ -144,21 +144,21 @@ func (app *Basecoin) Commit() (res abci.Result) {
// TMSP::InitChain
func (app *Basecoin) InitChain(validators []*abci.Validator) {
for _, plugin := range app.plugins.GetList() {
plugin.Plugin.InitChain(app.state, validators)
plugin.InitChain(app.state, validators)
}
}

// TMSP::BeginBlock
func (app *Basecoin) BeginBlock(height uint64) {
for _, plugin := range app.plugins.GetList() {
plugin.Plugin.BeginBlock(app.state, height)
plugin.BeginBlock(app.state, height)
}
}

// TMSP::EndBlock
func (app *Basecoin) EndBlock(height uint64) (diffs []*abci.Validator) {
for _, plugin := range app.plugins.GetList() {
moreDiffs := plugin.Plugin.EndBlock(app.state, height)
moreDiffs := plugin.EndBlock(app.state, height)
diffs = append(diffs, moreDiffs...)
}
return
Expand Down
36 changes: 18 additions & 18 deletions app/tmsp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package app
import (
"testing"

cmn "github.com/tendermint/basecoin/common"
"github.com/tendermint/basecoin/testutils"
"github.com/tendermint/basecoin/types"
. "github.com/tendermint/go-common"
cmn "github.com/tendermint/go-common"
"github.com/tendermint/go-wire"
eyescli "github.com/tendermint/merkleeyes/client"
)
Expand All @@ -17,8 +17,8 @@ func TestSendTx(t *testing.T) {
bcApp.SetOption("base/chainID", chainID)
t.Log(bcApp.Info())

test1PrivAcc := cmn.PrivAccountFromSecret("test1")
test2PrivAcc := cmn.PrivAccountFromSecret("test2")
test1PrivAcc := testutils.PrivAccountFromSecret("test1")
test2PrivAcc := testutils.PrivAccountFromSecret("test2")

// Seed Basecoin with account
test1Acc := test1PrivAcc.Account
Expand All @@ -27,15 +27,15 @@ func TestSendTx(t *testing.T) {

res := bcApp.Commit()
if res.IsErr() {
Exit(Fmt("Failed Commit: %v", res.Error()))
cmn.Exit(cmn.Fmt("Failed Commit: %v", res.Error()))
}

// Construct a SendTx signature
tx := &types.SendTx{
Fee: 0,
Gas: 0,
Fee: types.Coin{"", 0},
Inputs: []types.TxInput{
cmn.MakeInput(test1PrivAcc.Account.PubKey, types.Coins{{"", 1}}, 1),
types.NewTxInput(test1PrivAcc.Account.PubKey, types.Coins{{"", 1}}, 1),
},
Outputs: []types.TxOutput{
types.TxOutput{
Expand All @@ -57,7 +57,7 @@ func TestSendTx(t *testing.T) {
res = bcApp.DeliverTx(txBytes)
t.Log(res)
if res.IsErr() {
t.Errorf(Fmt("Failed: %v", res.Error()))
t.Errorf("Failed: %v", res.Error())
}
}

Expand All @@ -69,30 +69,30 @@ func TestSequence(t *testing.T) {
t.Log(bcApp.Info())

// Get the test account
test1PrivAcc := cmn.PrivAccountFromSecret("test1")
test1PrivAcc := testutils.PrivAccountFromSecret("test1")
test1Acc := test1PrivAcc.Account
test1Acc.Balance = types.Coins{{"", 1 << 53}}
t.Log(bcApp.SetOption("base/account", string(wire.JSONBytes(test1Acc))))

res := bcApp.Commit()
if res.IsErr() {
t.Errorf(Fmt("Failed Commit: %v", res.Error()))
t.Errorf("Failed Commit: %v", res.Error())
}

sequence := int(1)
// Make a bunch of PrivAccounts
privAccounts := cmn.RandAccounts(1000, 1000000, 0)
privAccounts := testutils.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{
Fee: 2,
Gas: 2,
Fee: types.Coin{"", 2},
Inputs: []types.TxInput{
cmn.MakeInput(test1Acc.PubKey, types.Coins{{"", 1000002}}, sequence),
types.NewTxInput(test1Acc.PubKey, types.Coins{{"", 1000002}}, sequence),
},
Outputs: []types.TxOutput{
types.TxOutput{
Expand Down Expand Up @@ -122,13 +122,13 @@ func TestSequence(t *testing.T) {

res = bcApp.Commit()
if res.IsErr() {
t.Errorf(Fmt("Failed Commit: %v", res.Error()))
t.Errorf("Failed Commit: %v", res.Error())
}

// Now send coins between these accounts
for i := 0; i < 10000; i++ {
randA := RandInt() % len(privAccounts)
randB := RandInt() % len(privAccounts)
randA := cmn.RandInt() % len(privAccounts)
randB := cmn.RandInt() % len(privAccounts)
if randA == randB {
continue
}
Expand All @@ -139,10 +139,10 @@ func TestSequence(t *testing.T) {
privAccountB := privAccounts[randB]

tx := &types.SendTx{
Fee: 2,
Gas: 2,
Fee: types.Coin{"", 2},
Inputs: []types.TxInput{
cmn.MakeInput(privAccountA.Account.PubKey, types.Coins{{"", 3}}, privAccountASequence+1),
types.NewTxInput(privAccountA.Account.PubKey, types.Coins{{"", 3}}, privAccountASequence+1),
},
Outputs: []types.TxOutput{
types.TxOutput{
Expand Down
26 changes: 19 additions & 7 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

102 changes: 102 additions & 0 deletions plugins/counter/counter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package counter

import (
"fmt"

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

type CounterPluginState struct {
Counter int
TotalCost types.Coins
}

type CounterTx struct {
Valid bool
Cost types.Coins
}

//--------------------------------------------------------------------------------

type CounterPlugin struct {
name string
}

func (cp *CounterPlugin) Name() string {
return cp.name
}

func (cp *CounterPlugin) StateKey() []byte {
return []byte(fmt.Sprintf("CounterPlugin{name=%v}.State", cp.name))
}

func NewCounterPlugin(name string) *CounterPlugin {
return &CounterPlugin{
name: name,
}
}

func (cp *CounterPlugin) SetOption(store types.KVStore, key string, value string) (log string) {
return ""
}

func (cp *CounterPlugin) RunTx(store types.KVStore, ctx types.CallContext, txBytes []byte) (res abci.Result) {

// Decode tx
var tx CounterTx
err := wire.ReadBinaryBytes(txBytes, &tx)
if err != nil {
return abci.ErrBaseEncodingError.AppendLog("Error decoding tx: " + err.Error())
}

// Validate tx
if !tx.Valid {
return abci.ErrInternalError.AppendLog("CounterTx.Valid must be true")
}
if !tx.Cost.IsValid() {
return abci.ErrInternalError.AppendLog("CounterTx.Cost is not sorted or has zero amounts")
}
if !tx.Cost.IsNonnegative() {
return abci.ErrInternalError.AppendLog("CounterTx.Cost must be nonnegative")
}

// Did the caller provide enough coins?
if !ctx.Coins.IsGTE(tx.Cost) {
return abci.ErrInsufficientFunds.AppendLog("CounterTx.Cost was not provided")
}

// TODO If there are any funds left over, return funds.
// e.g. !ctx.Coins.Minus(tx.Cost).IsZero()
// ctx.CallerAccount is synced w/ store, so just modify that and store it.

// Load CounterPluginState
var cpState CounterPluginState
cpStateBytes := store.Get(cp.StateKey())
if len(cpStateBytes) > 0 {
err = wire.ReadBinaryBytes(cpStateBytes, &cpState)
if err != nil {
return abci.ErrInternalError.AppendLog("Error decoding state: " + err.Error())
}
}

// Update CounterPluginState
cpState.Counter += 1
cpState.TotalCost = cpState.TotalCost.Plus(tx.Cost)

// Save CounterPluginState
store.Set(cp.StateKey(), wire.BinaryBytes(cpState))

return abci.OK
}

func (cp *CounterPlugin) InitChain(store types.KVStore, vals []*abci.Validator) {
}

func (cp *CounterPlugin) BeginBlock(store types.KVStore, height uint64) {
}

func (cp *CounterPlugin) EndBlock(store types.KVStore, height uint64) []*abci.Validator {
return nil
}
Loading

0 comments on commit 16a6680

Please sign in to comment.