2017-06-29 12:21:57 -07:00
|
|
|
package stack
|
|
|
|
|
|
|
|
import (
|
2017-06-30 04:55:25 -07:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
wire "github.com/tendermint/go-wire"
|
|
|
|
"github.com/tendermint/go-wire/data"
|
2017-07-06 05:23:38 -07:00
|
|
|
|
|
|
|
"github.com/tendermint/basecoin"
|
|
|
|
"github.com/tendermint/basecoin/state"
|
|
|
|
"github.com/tendermint/basecoin/txs"
|
2017-06-29 12:21:57 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
NameMultiplexer = "mplx"
|
|
|
|
)
|
|
|
|
|
2017-07-03 09:10:46 -07:00
|
|
|
type Multiplexer struct {
|
|
|
|
PassOption
|
|
|
|
}
|
2017-06-29 12:21:57 -07:00
|
|
|
|
|
|
|
func (_ Multiplexer) Name() string {
|
|
|
|
return NameMultiplexer
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ Middleware = Multiplexer{}
|
|
|
|
|
2017-07-06 05:23:38 -07:00
|
|
|
func (_ Multiplexer) CheckTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Checker) (res basecoin.Result, err error) {
|
2017-06-29 12:21:57 -07:00
|
|
|
if mtx, ok := tx.Unwrap().(*txs.MultiTx); ok {
|
|
|
|
return runAll(ctx, store, mtx.Txs, next.CheckTx)
|
|
|
|
}
|
|
|
|
return next.CheckTx(ctx, store, tx)
|
|
|
|
}
|
|
|
|
|
2017-07-06 05:23:38 -07:00
|
|
|
func (_ Multiplexer) DeliverTx(ctx basecoin.Context, store state.KVStore, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.Result, err error) {
|
2017-06-29 12:21:57 -07:00
|
|
|
if mtx, ok := tx.Unwrap().(*txs.MultiTx); ok {
|
|
|
|
return runAll(ctx, store, mtx.Txs, next.DeliverTx)
|
|
|
|
}
|
|
|
|
return next.DeliverTx(ctx, store, tx)
|
|
|
|
}
|
|
|
|
|
2017-07-06 05:23:38 -07:00
|
|
|
func runAll(ctx basecoin.Context, store state.KVStore, txs []basecoin.Tx, next basecoin.CheckerFunc) (res basecoin.Result, err error) {
|
2017-06-29 12:21:57 -07:00
|
|
|
// store all results, unless anything errors
|
|
|
|
rs := make([]basecoin.Result, len(txs))
|
|
|
|
for i, stx := range txs {
|
|
|
|
rs[i], err = next(ctx, store, stx)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// now combine the results into one...
|
|
|
|
return combine(rs), nil
|
|
|
|
}
|
|
|
|
|
2017-06-30 04:55:25 -07:00
|
|
|
// combines all data bytes as a go-wire array.
|
|
|
|
// joins all log messages with \n
|
|
|
|
func combine(all []basecoin.Result) basecoin.Result {
|
|
|
|
datas := make([]data.Bytes, len(all))
|
|
|
|
logs := make([]string, len(all))
|
|
|
|
for i, r := range all {
|
|
|
|
datas[i] = r.Data
|
|
|
|
logs[i] = r.Log
|
|
|
|
}
|
|
|
|
return basecoin.Result{
|
|
|
|
Data: wire.BinaryBytes(datas),
|
|
|
|
Log: strings.Join(logs, "\n"),
|
|
|
|
}
|
2017-06-29 12:21:57 -07:00
|
|
|
}
|