forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Vote->Counter; Fee is types.Coin; Context has Account; Cleanup
- Loading branch information
Showing
14 changed files
with
319 additions
and
286 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.