Skip to content

Commit

Permalink
[add] start compile
Browse files Browse the repository at this point in the history
  • Loading branch information
Tepleton committed Sep 25, 2019
1 parent 2f8c915 commit 69ce4f3
Show file tree
Hide file tree
Showing 51 changed files with 4,984 additions and 67 deletions.
6 changes: 4 additions & 2 deletions Makefile
Expand Up @@ -3,7 +3,9 @@ GOTOOLS = github.com/mitchellh/gox \
github.com/rigelrozanski/shelldown/cmd/shelldown
TUTORIALS=$(shell find docs/guide -name "*md" -type f)

EXAMPLES := counter eyes basecoin
# EXAMPLES := counter eyes basecoin
EXAMPLES := eyes

INSTALL_EXAMPLES := $(addprefix install_,${EXAMPLES})
TEST_EXAMPLES := $(addprefix testex_,${EXAMPLES})

Expand Down Expand Up @@ -34,7 +36,7 @@ benchmark:
test: test_unit test_cli

test_unit:
@go test `glide novendor`
@go test `glide novendor | grep -v _attic | grep -v examples`

test_cli: $(TEST_EXAMPLES)
# sudo apt-get install jq
Expand Down
6 changes: 6 additions & 0 deletions _attic/bonus/doc.go
@@ -0,0 +1,6 @@
/**
Package bonus is a temporary home for various functionalities
that were removed for complexity, but may be added back in
later.
**/
package bonus
118 changes: 118 additions & 0 deletions _attic/bonus/helpers.go
@@ -0,0 +1,118 @@
package bonus

import (
wrsp "github.com/tepleton/wrsp/types"

sdk "github.com/tepleton/tepleton-sdk"
"github.com/tepleton/tepleton-sdk/errors"
"github.com/tepleton/tepleton-sdk/state"
)

//nolint
const (
NameVal = "val"
NamePrice = "price"

TypeValChange = NameVal + "/change"
ByteValChange = 0xfe

TypePriceShow = NamePrice + "/show"
BytePriceShow = 0xfd
)

func init() {
sdk.TxMapper.
RegisterImplementation(ValChangeTx{}, TypeValChange, ByteValChange).
RegisterImplementation(PriceShowTx{}, TypePriceShow, BytePriceShow)
}

//--------------------------------
// Setup tx and handler for validation test cases

type ValSetHandler struct {
sdk.NopCheck
sdk.NopInitState
sdk.NopInitValidate
}

var _ sdk.Handler = ValSetHandler{}

func (ValSetHandler) Name() string {
return NameVal
}

func (ValSetHandler) DeliverTx(ctx sdk.Context, store state.SimpleDB,
tx sdk.Tx) (res sdk.DeliverResult, err error) {
change, ok := tx.Unwrap().(ValChangeTx)
if !ok {
return res, errors.ErrUnknownTxType(tx)
}
res.Diff = change.Diff
return
}

type ValChangeTx struct {
Diff []*wrsp.Validator
}

func (v ValChangeTx) Wrap() sdk.Tx {
return sdk.Tx{v}
}

func (v ValChangeTx) ValidateBasic() error { return nil }

//--------------------------------
// Setup tx and handler for testing checktx fees/gas

// PriceData is the data we ping back
var PriceData = []byte{0xCA, 0xFE}

// PriceHandler returns checktx results based on the input
type PriceHandler struct {
sdk.NopInitState
sdk.NopInitValidate
}

var _ sdk.Handler = PriceHandler{}

func (PriceHandler) Name() string {
return NamePrice
}

func (PriceHandler) CheckTx(ctx sdk.Context, store state.SimpleDB,
tx sdk.Tx) (res sdk.CheckResult, err error) {
price, ok := tx.Unwrap().(PriceShowTx)
if !ok {
return res, errors.ErrUnknownTxType(tx)
}
res.GasAllocated = price.GasAllocated
res.GasPayment = price.GasPayment
res.Data = PriceData
return
}

func (PriceHandler) DeliverTx(ctx sdk.Context, store state.SimpleDB,
tx sdk.Tx) (res sdk.DeliverResult, err error) {
_, ok := tx.Unwrap().(PriceShowTx)
if !ok {
return res, errors.ErrUnknownTxType(tx)
}
res.Data = PriceData
return
}

// PriceShowTx lets us bounce back a given fee/gas on CheckTx
type PriceShowTx struct {
GasAllocated uint64
GasPayment uint64
}

func NewPriceShowTx(gasAllocated, gasPayment uint64) sdk.Tx {
return PriceShowTx{GasAllocated: gasAllocated, GasPayment: gasPayment}.Wrap()
}

func (p PriceShowTx) Wrap() sdk.Tx {
return sdk.Tx{p}
}

func (v PriceShowTx) ValidateBasic() error { return nil }
116 changes: 116 additions & 0 deletions _attic/bonus/multiplexer.go
@@ -0,0 +1,116 @@
package bonus

import (
"strings"

wrsp "github.com/tepleton/wrsp/types"
wire "github.com/tepleton/go-wire"
"github.com/tepleton/go-wire/data"

sdk "github.com/tepleton/tepleton-sdk"
"github.com/tepleton/tepleton-sdk/stack"
"github.com/tepleton/tepleton-sdk/state"
)

//nolint
const (
NameMultiplexer = "mplx"
)

// Multiplexer grabs a MultiTx and sends them sequentially down the line
type Multiplexer struct {
stack.PassInitState
stack.PassInitValidate
}

// Name of the module - fulfills Middleware interface
func (Multiplexer) Name() string {
return NameMultiplexer
}

var _ stack.Middleware = Multiplexer{}

// CheckTx splits the input tx and checks them all - fulfills Middlware interface
func (Multiplexer) CheckTx(ctx sdk.Context, store state.SimpleDB, tx sdk.Tx, next sdk.Checker) (res sdk.CheckResult, err error) {
if mtx, ok := tx.Unwrap().(MultiTx); ok {
return runAllChecks(ctx, store, mtx.Txs, next)
}
return next.CheckTx(ctx, store, tx)
}

// DeliverTx splits the input tx and checks them all - fulfills Middlware interface
func (Multiplexer) DeliverTx(ctx sdk.Context, store state.SimpleDB, tx sdk.Tx, next sdk.Deliver) (res sdk.DeliverResult, err error) {
if mtx, ok := tx.Unwrap().(MultiTx); ok {
return runAllDelivers(ctx, store, mtx.Txs, next)
}
return next.DeliverTx(ctx, store, tx)
}

func runAllChecks(ctx sdk.Context, store state.SimpleDB, txs []sdk.Tx, next sdk.Checker) (res sdk.CheckResult, err error) {
// store all results, unless anything errors
rs := make([]sdk.CheckResult, len(txs))
for i, stx := range txs {
rs[i], err = next.CheckTx(ctx, store, stx)
if err != nil {
return
}
}
// now combine the results into one...
return combineChecks(rs), nil
}

func runAllDelivers(ctx sdk.Context, store state.SimpleDB, txs []sdk.Tx, next sdk.Deliver) (res sdk.DeliverResult, err error) {
// store all results, unless anything errors
rs := make([]sdk.DeliverResult, len(txs))
for i, stx := range txs {
rs[i], err = next.DeliverTx(ctx, store, stx)
if err != nil {
return
}
}
// now combine the results into one...
return combineDelivers(rs), nil
}

// combines all data bytes as a go-wire array.
// joins all log messages with \n
func combineChecks(all []sdk.CheckResult) sdk.CheckResult {
datas := make([]data.Bytes, len(all))
logs := make([]string, len(all))
var allocated, payments uint64
for i, r := range all {
datas[i] = r.Data
logs[i] = r.Log
allocated += r.GasAllocated
payments += r.GasPayment
}
return sdk.CheckResult{
Data: wire.BinaryBytes(datas),
Log: strings.Join(logs, "\n"),
GasAllocated: allocated,
GasPayment: payments,
}
}

// combines all data bytes as a go-wire array.
// joins all log messages with \n
func combineDelivers(all []sdk.DeliverResult) sdk.DeliverResult {
datas := make([]data.Bytes, len(all))
logs := make([]string, len(all))
var used uint64
var diffs []*wrsp.Validator
for i, r := range all {
datas[i] = r.Data
logs[i] = r.Log
used += r.GasUsed
if len(r.Diff) > 0 {
diffs = append(diffs, r.Diff...)
}
}
return sdk.DeliverResult{
Data: wire.BinaryBytes(datas),
Log: strings.Join(logs, "\n"),
GasUsed: used,
Diff: diffs,
}
}
87 changes: 87 additions & 0 deletions _attic/bonus/multiplexer_test.go
@@ -0,0 +1,87 @@
package bonus

import (
"testing"

sdk "github.com/tepleton/tepleton-sdk"
"github.com/tepleton/tepleton-sdk/stack"
"github.com/tepleton/tepleton-sdk/state"
"github.com/stretchr/testify/assert"
wire "github.com/tepleton/go-wire"
"github.com/tepleton/go-wire/data"
"github.com/tepleton/tmlibs/log"
)

func TestMultiplexer(t *testing.T) {
assert := assert.New(t)
msg := "diddly"
chainID := "multi-verse"
height := uint64(100)

// Generic args here...
store := state.NewMemKVStore()
ctx := stack.NewContext(chainID, height, log.NewNopLogger())

// Build the stack
app := stack.
New(Multiplexer{}).
Dispatch(
stack.WrapHandler(stack.OKHandler{Log: msg}),
stack.WrapHandler(PriceHandler{}),
)

raw := stack.NewRawTx([]byte{1, 2, 3, 4})
fail := stack.NewFailTx()
price1 := NewPriceShowTx(123, 456)
price2 := NewPriceShowTx(1000, 2000)
price3 := NewPriceShowTx(11, 0)

join := func(data ...[]byte) []byte {
return wire.BinaryBytes(data)
}

cases := [...]struct {
tx sdk.Tx
valid bool
gasAllocated uint64
gasPayment uint64
log string
data data.Bytes
}{
// test the components without multiplexer (no effect)
0: {raw, true, 0, 0, msg, nil},
1: {price1, true, 123, 456, "", PriceData},
2: {fail, false, 0, 0, "", nil},
// test multiplexer on error
3: {NewMultiTx(raw, fail, price1), false, 0, 0, "", nil},
// test combining info on multiplexer
4: {NewMultiTx(price1, raw), true, 123, 456, "\n" + msg, join(PriceData, nil)},
// add lots of prices
5: {NewMultiTx(price1, price2, price3), true, 1134, 2456, "\n\n", join(PriceData, PriceData, PriceData)},
// combine multiple logs
6: {NewMultiTx(raw, price3, raw), true, 11, 0, msg + "\n\n" + msg, join(nil, PriceData, nil)},
}

for i, tc := range cases {
cres, err := app.CheckTx(ctx, store, tc.tx)
if tc.valid {
assert.Nil(err, "%d: %+v", i, err)
assert.Equal(tc.log, cres.Log, "%d", i)
assert.Equal(tc.data, cres.Data, "%d", i)
assert.Equal(tc.gasAllocated, cres.GasAllocated, "%d", i)
assert.Equal(tc.gasPayment, cres.GasPayment, "%d", i)
} else {
assert.NotNil(err, "%d", i)
}

// make sure deliver returns error, not a panic crash
dres, err := app.DeliverTx(ctx, store, tc.tx)
if tc.valid {
assert.Nil(err, "%d: %+v", i, err)
assert.Equal(tc.log, dres.Log, "%d", i)
assert.Equal(tc.data, dres.Data, "%d", i)
} else {
assert.NotNil(err, "%d", i)
}
}
}

0 comments on commit 69ce4f3

Please sign in to comment.