2017-07-06 07:00:54 -07:00
|
|
|
package base
|
2017-06-29 12:21:57 -07:00
|
|
|
|
2017-07-30 14:51:57 -07:00
|
|
|
import (
|
|
|
|
"strings"
|
2017-06-30 04:55:25 -07:00
|
|
|
|
2017-07-30 14:51:57 -07:00
|
|
|
abci "github.com/tendermint/abci/types"
|
|
|
|
wire "github.com/tendermint/go-wire"
|
|
|
|
"github.com/tendermint/go-wire/data"
|
2017-07-06 05:23:38 -07:00
|
|
|
|
2017-07-30 14:51:57 -07:00
|
|
|
"github.com/tendermint/basecoin"
|
|
|
|
"github.com/tendermint/basecoin/stack"
|
|
|
|
"github.com/tendermint/basecoin/state"
|
|
|
|
)
|
2017-06-29 12:21:57 -07:00
|
|
|
|
2017-07-30 14:51:57 -07:00
|
|
|
//nolint
|
|
|
|
const (
|
|
|
|
NameMultiplexer = "mplx"
|
|
|
|
)
|
2017-06-29 12:21:57 -07:00
|
|
|
|
2017-07-30 14:51:57 -07:00
|
|
|
// Multiplexer grabs a MultiTx and sends them sequentially down the line
|
|
|
|
type Multiplexer struct {
|
|
|
|
stack.PassInitState
|
|
|
|
stack.PassInitValidate
|
|
|
|
}
|
2017-06-29 12:21:57 -07:00
|
|
|
|
2017-07-30 14:51:57 -07:00
|
|
|
// Name of the module - fulfills Middleware interface
|
|
|
|
func (Multiplexer) Name() string {
|
|
|
|
return NameMultiplexer
|
|
|
|
}
|
2017-06-29 12:21:57 -07:00
|
|
|
|
2017-07-30 14:51:57 -07:00
|
|
|
var _ stack.Middleware = Multiplexer{}
|
2017-06-29 12:21:57 -07:00
|
|
|
|
2017-07-30 14:51:57 -07:00
|
|
|
// CheckTx splits the input tx and checks them all - fulfills Middlware interface
|
|
|
|
func (Multiplexer) CheckTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx, next basecoin.Checker) (res basecoin.CheckResult, err error) {
|
2017-08-03 12:40:45 -07:00
|
|
|
if mtx, ok := tx.Unwrap().(MultiTx); ok {
|
2017-07-30 14:51:57 -07:00
|
|
|
return runAllChecks(ctx, store, mtx.Txs, next)
|
|
|
|
}
|
|
|
|
return next.CheckTx(ctx, store, tx)
|
|
|
|
}
|
2017-06-29 12:21:57 -07:00
|
|
|
|
2017-07-30 14:51:57 -07:00
|
|
|
// DeliverTx splits the input tx and checks them all - fulfills Middlware interface
|
|
|
|
func (Multiplexer) DeliverTx(ctx basecoin.Context, store state.SimpleDB, tx basecoin.Tx, next basecoin.Deliver) (res basecoin.DeliverResult, err error) {
|
2017-08-03 12:40:45 -07:00
|
|
|
if mtx, ok := tx.Unwrap().(MultiTx); ok {
|
2017-07-30 14:51:57 -07:00
|
|
|
return runAllDelivers(ctx, store, mtx.Txs, next)
|
|
|
|
}
|
|
|
|
return next.DeliverTx(ctx, store, tx)
|
|
|
|
}
|
2017-06-29 12:21:57 -07:00
|
|
|
|
2017-07-30 14:51:57 -07:00
|
|
|
func runAllChecks(ctx basecoin.Context, store state.SimpleDB, txs []basecoin.Tx, next basecoin.Checker) (res basecoin.CheckResult, err error) {
|
|
|
|
// store all results, unless anything errors
|
|
|
|
rs := make([]basecoin.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
|
|
|
|
}
|
2017-06-29 12:21:57 -07:00
|
|
|
|
2017-07-30 14:51:57 -07:00
|
|
|
func runAllDelivers(ctx basecoin.Context, store state.SimpleDB, txs []basecoin.Tx, next basecoin.Deliver) (res basecoin.DeliverResult, err error) {
|
|
|
|
// store all results, unless anything errors
|
|
|
|
rs := make([]basecoin.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 []basecoin.CheckResult) basecoin.CheckResult {
|
|
|
|
datas := make([]data.Bytes, len(all))
|
|
|
|
logs := make([]string, len(all))
|
|
|
|
var allocated, payments uint
|
|
|
|
for i, r := range all {
|
|
|
|
datas[i] = r.Data
|
|
|
|
logs[i] = r.Log
|
|
|
|
allocated += r.GasAllocated
|
|
|
|
payments += r.GasPayment
|
|
|
|
}
|
|
|
|
return basecoin.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 []basecoin.DeliverResult) basecoin.DeliverResult {
|
|
|
|
datas := make([]data.Bytes, len(all))
|
|
|
|
logs := make([]string, len(all))
|
|
|
|
var used uint
|
|
|
|
var diffs []*abci.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 basecoin.DeliverResult{
|
|
|
|
Data: wire.BinaryBytes(datas),
|
|
|
|
Log: strings.Join(logs, "\n"),
|
|
|
|
GasUsed: used,
|
|
|
|
Diff: diffs,
|
|
|
|
}
|
|
|
|
}
|