Skip to content

Commit c69a00d

Browse files
authored
Merge pull request #51 from irisnet/irisnet/develop
Release/v0.23.0-iris1
2 parents ad65531 + 199d4fc commit c69a00d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+2433
-313
lines changed

Gopkg.lock

Lines changed: 20 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262

6363
[[override]]
6464
name = "github.com/tendermint/tendermint"
65-
version = "=v0.22.6"
65+
source = "https://github.com/irisnet/tendermint.git"
66+
version = "=v0.22.6-iris1"
6667

6768
[[constraint]]
6869
name = "github.com/bartekn/go-bip39"

baseapp/baseapp.go

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ const (
3939
runTxModeDeliver runTxMode = iota
4040
)
4141

42+
type RunMsg func(ctx sdk.Context, msgs []sdk.Msg) sdk.Result
43+
4244
// BaseApp reflects the ABCI application implementation.
4345
type BaseApp struct {
4446
// initialized on creation
@@ -53,13 +55,15 @@ type BaseApp struct {
5355
// must be set
5456
txDecoder sdk.TxDecoder // unmarshal []byte into sdk.Tx
5557
anteHandler sdk.AnteHandler // ante handler for fee and auth
58+
feeRefundHandler sdk.FeeRefundHandler // fee handler for fee refund
5659

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

6468
//--------------------
6569
// Volatile
@@ -135,6 +139,13 @@ func (app *BaseApp) MountStore(key sdk.StoreKey, typ sdk.StoreType) {
135139
app.cms.MountStoreWithDB(key, typ, nil)
136140
}
137141

142+
//////////////////// iris/cosmos-sdk begin ///////////////////////////
143+
func (app *BaseApp) GetKVStore(key sdk.StoreKey) sdk.KVStore {
144+
return app.cms.GetKVStore(key)
145+
}
146+
147+
//////////////////// iris/cosmos-sdk end ///////////////////////////
148+
138149
// Set the txDecoder function
139150
func (app *BaseApp) SetTxDecoder(txDecoder sdk.TxDecoder) {
140151
app.txDecoder = txDecoder
@@ -177,6 +188,9 @@ func (app *BaseApp) SetEndBlocker(endBlocker sdk.EndBlocker) {
177188
func (app *BaseApp) SetAnteHandler(ah sdk.AnteHandler) {
178189
app.anteHandler = ah
179190
}
191+
func (app *BaseApp) SetFeeRefundHandler(fh sdk.FeeRefundHandler) {
192+
app.feeRefundHandler = fh
193+
}
180194
func (app *BaseApp) SetAddrPeerFilter(pf sdk.PeerFilter) {
181195
app.addrPeerFilter = pf
182196
}
@@ -185,6 +199,10 @@ func (app *BaseApp) SetPubKeyPeerFilter(pf sdk.PeerFilter) {
185199
}
186200
func (app *BaseApp) Router() Router { return app.router }
187201

202+
func (app *BaseApp) SetRunMsg(runMsg RunMsg) {
203+
app.runMsg = runMsg
204+
}
205+
188206
// load latest application version
189207
func (app *BaseApp) LoadLatestVersion(mainKey sdk.StoreKey) error {
190208
err := app.cms.LoadLatestVersion()
@@ -438,8 +456,41 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg
438456

439457
// Implements ABCI
440458
func (app *BaseApp) CheckTx(txBytes []byte) (res abci.ResponseCheckTx) {
441-
// Decode the Tx.
459+
442460
var result sdk.Result
461+
462+
//////////////////// iris/cosmos-sdk begin ///////////////////////////
463+
464+
upgradeKey := sdk.NewKVStoreKey("upgrade")
465+
store := app.cms.GetStore(upgradeKey)
466+
467+
if store != nil {
468+
kvStore, ok := store.(sdk.KVStore)
469+
if ok {
470+
bz := kvStore.Get([]byte("d"))
471+
if len(bz) == 1 && bz[0] == byte(1) {
472+
result = sdk.NewError(sdk.CodespaceUndefined, sdk.CodeOutOfService, "").Result()
473+
474+
return abci.ResponseCheckTx{
475+
Code: uint32(result.Code),
476+
Data: result.Data,
477+
Log: result.Log,
478+
GasWanted: result.GasWanted,
479+
GasUsed: result.GasUsed,
480+
Fee: cmn.KI64Pair{
481+
[]byte(result.FeeDenom),
482+
result.FeeAmount,
483+
},
484+
Tags: result.Tags,
485+
}
486+
}
487+
}
488+
}
489+
490+
//////////////////// iris/cosmos-sdk end ///////////////////////////
491+
492+
// Decode the Tx.
493+
443494
var tx, err = app.txDecoder(txBytes)
444495
if err != nil {
445496
result = err.Result()
@@ -524,6 +575,10 @@ func (app *BaseApp) getContextForAnte(mode runTxMode, txBytes []byte) (ctx sdk.C
524575

525576
// Iterates through msgs and executes them
526577
func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg) (result sdk.Result) {
578+
if app.runMsg != nil {
579+
return app.runMsg(ctx, msgs)
580+
}
581+
527582
// accumulate results
528583
logs := make([]string, 0, len(msgs))
529584
var data []byte // NOTE: we just append them all (?!)
@@ -589,7 +644,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk
589644
// meter so we initialize upfront.
590645
var gasWanted int64
591646
ctx := app.getContextForAnte(mode, txBytes)
592-
647+
ctxWithNoCache := ctx
593648
defer func() {
594649
if r := recover(); r != nil {
595650
switch rType := r.(type) {
@@ -603,7 +658,17 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk
603658
}
604659

605660
result.GasWanted = gasWanted
606-
result.GasUsed = ctx.GasMeter().GasConsumed()
661+
result.GasUsed = ctxWithNoCache.GasMeter().GasConsumed()
662+
663+
// Refund unspent fee
664+
if app.feeRefundHandler != nil {
665+
err := app.feeRefundHandler(ctxWithNoCache, tx, result)
666+
if err != nil {
667+
result = sdk.ErrInternal(err.Error()).Result()
668+
result.GasWanted = gasWanted
669+
result.GasUsed = ctxWithNoCache.GasMeter().GasConsumed()
670+
}
671+
}
607672
}()
608673

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

626-
gasWanted = result.GasWanted
692+
gasWanted = anteResult.GasWanted
627693
}
628694

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

638704
ctx = ctx.WithMultiStore(msCache)
639705
result = app.runMsgs(ctx, msgs)
640-
result.GasWanted = gasWanted
641706

642707
// only update state if all messages pass and we're not in a simulation
643708
if result.IsOK() && mode != runTxModeSimulate {

baseapp/baseapp_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ func TestCheckTx(t *testing.T) {
440440
nTxs := int64(5)
441441

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

445445
app.InitChain(abci.RequestInitChain{})
446446

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

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

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

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

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

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

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

637637
app.BeginBlock(abci.RequestBeginBlock{})
638638

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

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

705705
gasGranted := int64(10)
706706
app.SetAnteHandler(func(ctx sdk.Context, tx sdk.Tx) (newCtx sdk.Context, res sdk.Result, abort bool) {
@@ -732,7 +732,7 @@ func TestTxGasLimits(t *testing.T) {
732732
}
733733
return
734734
})
735-
app.Router().AddRoute(typeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
735+
app.Router().AddRoute(typeMsgCounter, []*sdk.KVStoreKey{capKey}, func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
736736
count := msg.(msgCounter).Counter
737737
ctx.GasMeter().ConsumeGas(count, "counter-handler")
738738
return sdk.Result{}
@@ -794,7 +794,7 @@ func TestQuery(t *testing.T) {
794794
return
795795
})
796796

797-
app.Router().AddRoute(typeMsgCounter, func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
797+
app.Router().AddRoute(typeMsgCounter, []*sdk.KVStoreKey{capKey}, func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
798798
store := ctx.KVStore(capKey)
799799
store.Set(key, value)
800800
return sdk.Result{}

0 commit comments

Comments
 (0)