Skip to content

Commit

Permalink
Merge pull request #51 from irisnet/irisnet/develop
Browse files Browse the repository at this point in the history
Release/v0.23.0-iris1
  • Loading branch information
wukongcheng authored Aug 15, 2018
2 parents ad65531 + 199d4fc commit c69a00d
Show file tree
Hide file tree
Showing 77 changed files with 2,433 additions and 313 deletions.
35 changes: 20 additions & 15 deletions Gopkg.lock

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

3 changes: 2 additions & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@

[[override]]
name = "github.com/tendermint/tendermint"
version = "=v0.22.6"
source = "https://github.com/irisnet/tendermint.git"
version = "=v0.22.6-iris1"

[[constraint]]
name = "github.com/bartekn/go-bip39"
Expand Down
75 changes: 70 additions & 5 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const (
runTxModeDeliver runTxMode = iota
)

type RunMsg func(ctx sdk.Context, msgs []sdk.Msg) sdk.Result

// BaseApp reflects the ABCI application implementation.
type BaseApp struct {
// initialized on creation
Expand All @@ -53,13 +55,15 @@ type BaseApp struct {
// must be set
txDecoder sdk.TxDecoder // unmarshal []byte into sdk.Tx
anteHandler sdk.AnteHandler // ante handler for fee and auth
feeRefundHandler sdk.FeeRefundHandler // fee handler for fee refund

// may be nil
initChainer sdk.InitChainer // initialize state with validators and state blob
beginBlocker sdk.BeginBlocker // logic to run before any txs
endBlocker sdk.EndBlocker // logic to run after all txs, and to determine valset changes
addrPeerFilter sdk.PeerFilter // filter peers by address and port
pubkeyPeerFilter sdk.PeerFilter // filter peers by public key
runMsg RunMsg

//--------------------
// Volatile
Expand Down Expand Up @@ -135,6 +139,13 @@ func (app *BaseApp) MountStore(key sdk.StoreKey, typ sdk.StoreType) {
app.cms.MountStoreWithDB(key, typ, nil)
}

//////////////////// iris/cosmos-sdk begin ///////////////////////////
func (app *BaseApp) GetKVStore(key sdk.StoreKey) sdk.KVStore {
return app.cms.GetKVStore(key)
}

//////////////////// iris/cosmos-sdk end ///////////////////////////

// Set the txDecoder function
func (app *BaseApp) SetTxDecoder(txDecoder sdk.TxDecoder) {
app.txDecoder = txDecoder
Expand Down Expand Up @@ -177,6 +188,9 @@ func (app *BaseApp) SetEndBlocker(endBlocker sdk.EndBlocker) {
func (app *BaseApp) SetAnteHandler(ah sdk.AnteHandler) {
app.anteHandler = ah
}
func (app *BaseApp) SetFeeRefundHandler(fh sdk.FeeRefundHandler) {
app.feeRefundHandler = fh
}
func (app *BaseApp) SetAddrPeerFilter(pf sdk.PeerFilter) {
app.addrPeerFilter = pf
}
Expand All @@ -185,6 +199,10 @@ func (app *BaseApp) SetPubKeyPeerFilter(pf sdk.PeerFilter) {
}
func (app *BaseApp) Router() Router { return app.router }

func (app *BaseApp) SetRunMsg(runMsg RunMsg) {
app.runMsg = runMsg
}

// load latest application version
func (app *BaseApp) LoadLatestVersion(mainKey sdk.StoreKey) error {
err := app.cms.LoadLatestVersion()
Expand Down Expand Up @@ -438,8 +456,41 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg

// Implements ABCI
func (app *BaseApp) CheckTx(txBytes []byte) (res abci.ResponseCheckTx) {
// Decode the Tx.

var result sdk.Result

//////////////////// iris/cosmos-sdk begin ///////////////////////////

upgradeKey := sdk.NewKVStoreKey("upgrade")
store := app.cms.GetStore(upgradeKey)

if store != nil {
kvStore, ok := store.(sdk.KVStore)
if ok {
bz := kvStore.Get([]byte("d"))
if len(bz) == 1 && bz[0] == byte(1) {
result = sdk.NewError(sdk.CodespaceUndefined, sdk.CodeOutOfService, "").Result()

return abci.ResponseCheckTx{
Code: uint32(result.Code),
Data: result.Data,
Log: result.Log,
GasWanted: result.GasWanted,
GasUsed: result.GasUsed,
Fee: cmn.KI64Pair{
[]byte(result.FeeDenom),
result.FeeAmount,
},
Tags: result.Tags,
}
}
}
}

//////////////////// iris/cosmos-sdk end ///////////////////////////

// Decode the Tx.

var tx, err = app.txDecoder(txBytes)
if err != nil {
result = err.Result()
Expand Down Expand Up @@ -524,6 +575,10 @@ func (app *BaseApp) getContextForAnte(mode runTxMode, txBytes []byte) (ctx sdk.C

// Iterates through msgs and executes them
func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg) (result sdk.Result) {
if app.runMsg != nil {
return app.runMsg(ctx, msgs)
}

// accumulate results
logs := make([]string, 0, len(msgs))
var data []byte // NOTE: we just append them all (?!)
Expand Down Expand Up @@ -589,7 +644,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk
// meter so we initialize upfront.
var gasWanted int64
ctx := app.getContextForAnte(mode, txBytes)

ctxWithNoCache := ctx
defer func() {
if r := recover(); r != nil {
switch rType := r.(type) {
Expand All @@ -603,7 +658,17 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk
}

result.GasWanted = gasWanted
result.GasUsed = ctx.GasMeter().GasConsumed()
result.GasUsed = ctxWithNoCache.GasMeter().GasConsumed()

// Refund unspent fee
if app.feeRefundHandler != nil {
err := app.feeRefundHandler(ctxWithNoCache, tx, result)
if err != nil {
result = sdk.ErrInternal(err.Error()).Result()
result.GasWanted = gasWanted
result.GasUsed = ctxWithNoCache.GasMeter().GasConsumed()
}
}
}()

var msgs = tx.GetMsgs()
Expand All @@ -621,9 +686,10 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk
}
if !newCtx.IsZero() {
ctx = newCtx
ctxWithNoCache = newCtx
}

gasWanted = result.GasWanted
gasWanted = anteResult.GasWanted
}

// Keep the state in a transient CacheWrap in case processing the messages
Expand All @@ -637,7 +703,6 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk

ctx = ctx.WithMultiStore(msCache)
result = app.runMsgs(ctx, msgs)
result.GasWanted = gasWanted

// only update state if all messages pass and we're not in a simulation
if result.IsOK() && mode != runTxModeSimulate {
Expand Down
22 changes: 11 additions & 11 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ func TestCheckTx(t *testing.T) {
nTxs := int64(5)

// TODO: can remove this once CheckTx doesnt process msgs.
app.Router().AddRoute(typeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) sdk.Result { return sdk.Result{} })
app.Router().AddRoute(typeMsgCounter, []*sdk.KVStoreKey{capKey}, func(ctx sdk.Context, msg sdk.Msg) sdk.Result { return sdk.Result{} })

app.InitChain(abci.RequestInitChain{})

Expand Down Expand Up @@ -479,7 +479,7 @@ func TestDeliverTx(t *testing.T) {

// test increments in the handler
deliverKey := []byte("deliver-key")
app.Router().AddRoute(typeMsgCounter, handlerMsgCounter(t, capKey, deliverKey))
app.Router().AddRoute(typeMsgCounter, []*sdk.KVStoreKey{capKey}, handlerMsgCounter(t, capKey, deliverKey))

nBlocks := 3
txPerHeight := 5
Expand Down Expand Up @@ -515,8 +515,8 @@ func TestMultiMsgDeliverTx(t *testing.T) {
// increment the msg counter
deliverKey := []byte("deliver-key")
deliverKey2 := []byte("deliver-key2")
app.Router().AddRoute(typeMsgCounter, handlerMsgCounter(t, capKey, deliverKey))
app.Router().AddRoute(typeMsgCounter2, handlerMsgCounter(t, capKey, deliverKey2))
app.Router().AddRoute(typeMsgCounter, []*sdk.KVStoreKey{capKey}, handlerMsgCounter(t, capKey, deliverKey))
app.Router().AddRoute(typeMsgCounter2, []*sdk.KVStoreKey{capKey}, handlerMsgCounter(t, capKey, deliverKey2))

// run a multi-msg tx
// with all msgs the same type
Expand Down Expand Up @@ -575,15 +575,15 @@ func TestConcurrentCheckDeliver(t *testing.T) {
// Simulate() and Query("/app/simulate", txBytes) should give
// the same results.
func TestSimulateTx(t *testing.T) {
app, _, _ := setupBaseApp(t)
app, capKey, _ := setupBaseApp(t)

gasConsumed := int64(5)
app.SetAnteHandler(func(ctx sdk.Context, tx sdk.Tx) (newCtx sdk.Context, res sdk.Result, abort bool) {
newCtx = ctx.WithGasMeter(sdk.NewGasMeter(gasConsumed))
return
})

app.Router().AddRoute(typeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
app.Router().AddRoute(typeMsgCounter, []*sdk.KVStoreKey{capKey}, func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
ctx.GasMeter().ConsumeGas(gasConsumed, "test")
return sdk.Result{GasUsed: ctx.GasMeter().GasConsumed()}
})
Expand Down Expand Up @@ -630,9 +630,9 @@ func TestSimulateTx(t *testing.T) {
// TODO: add more

func TestRunInvalidTransaction(t *testing.T) {
app, _, _ := setupBaseApp(t)
app, capKey, _ := setupBaseApp(t)
app.SetAnteHandler(func(ctx sdk.Context, tx sdk.Tx) (newCtx sdk.Context, res sdk.Result, abort bool) { return })
app.Router().AddRoute(typeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) (res sdk.Result) { return })
app.Router().AddRoute(typeMsgCounter, []*sdk.KVStoreKey{capKey}, func(ctx sdk.Context, msg sdk.Msg) (res sdk.Result) { return })

app.BeginBlock(abci.RequestBeginBlock{})

Expand Down Expand Up @@ -700,7 +700,7 @@ func TestRunInvalidTransaction(t *testing.T) {

// Test that transactions exceeding gas limits fail
func TestTxGasLimits(t *testing.T) {
app, _, _ := setupBaseApp(t)
app, capKey, _ := setupBaseApp(t)

gasGranted := int64(10)
app.SetAnteHandler(func(ctx sdk.Context, tx sdk.Tx) (newCtx sdk.Context, res sdk.Result, abort bool) {
Expand Down Expand Up @@ -732,7 +732,7 @@ func TestTxGasLimits(t *testing.T) {
}
return
})
app.Router().AddRoute(typeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
app.Router().AddRoute(typeMsgCounter, []*sdk.KVStoreKey{capKey}, func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
count := msg.(msgCounter).Counter
ctx.GasMeter().ConsumeGas(count, "counter-handler")
return sdk.Result{}
Expand Down Expand Up @@ -794,7 +794,7 @@ func TestQuery(t *testing.T) {
return
})

app.Router().AddRoute(typeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
app.Router().AddRoute(typeMsgCounter, []*sdk.KVStoreKey{capKey}, func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
store := ctx.KVStore(capKey)
store.Set(key, value)
return sdk.Result{}
Expand Down
Loading

0 comments on commit c69a00d

Please sign in to comment.