@@ -39,6 +39,8 @@ const (
39
39
runTxModeDeliver runTxMode = iota
40
40
)
41
41
42
+ type RunMsg func (ctx sdk.Context , msgs []sdk.Msg ) sdk.Result
43
+
42
44
// BaseApp reflects the ABCI application implementation.
43
45
type BaseApp struct {
44
46
// initialized on creation
@@ -53,13 +55,15 @@ type BaseApp struct {
53
55
// must be set
54
56
txDecoder sdk.TxDecoder // unmarshal []byte into sdk.Tx
55
57
anteHandler sdk.AnteHandler // ante handler for fee and auth
58
+ feeRefundHandler sdk.FeeRefundHandler // fee handler for fee refund
56
59
57
60
// may be nil
58
61
initChainer sdk.InitChainer // initialize state with validators and state blob
59
62
beginBlocker sdk.BeginBlocker // logic to run before any txs
60
63
endBlocker sdk.EndBlocker // logic to run after all txs, and to determine valset changes
61
64
addrPeerFilter sdk.PeerFilter // filter peers by address and port
62
65
pubkeyPeerFilter sdk.PeerFilter // filter peers by public key
66
+ runMsg RunMsg
63
67
64
68
//--------------------
65
69
// Volatile
@@ -135,6 +139,13 @@ func (app *BaseApp) MountStore(key sdk.StoreKey, typ sdk.StoreType) {
135
139
app .cms .MountStoreWithDB (key , typ , nil )
136
140
}
137
141
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
+
138
149
// Set the txDecoder function
139
150
func (app * BaseApp ) SetTxDecoder (txDecoder sdk.TxDecoder ) {
140
151
app .txDecoder = txDecoder
@@ -177,6 +188,9 @@ func (app *BaseApp) SetEndBlocker(endBlocker sdk.EndBlocker) {
177
188
func (app * BaseApp ) SetAnteHandler (ah sdk.AnteHandler ) {
178
189
app .anteHandler = ah
179
190
}
191
+ func (app * BaseApp ) SetFeeRefundHandler (fh sdk.FeeRefundHandler ) {
192
+ app .feeRefundHandler = fh
193
+ }
180
194
func (app * BaseApp ) SetAddrPeerFilter (pf sdk.PeerFilter ) {
181
195
app .addrPeerFilter = pf
182
196
}
@@ -185,6 +199,10 @@ func (app *BaseApp) SetPubKeyPeerFilter(pf sdk.PeerFilter) {
185
199
}
186
200
func (app * BaseApp ) Router () Router { return app .router }
187
201
202
+ func (app * BaseApp ) SetRunMsg (runMsg RunMsg ) {
203
+ app .runMsg = runMsg
204
+ }
205
+
188
206
// load latest application version
189
207
func (app * BaseApp ) LoadLatestVersion (mainKey sdk.StoreKey ) error {
190
208
err := app .cms .LoadLatestVersion ()
@@ -438,8 +456,41 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg
438
456
439
457
// Implements ABCI
440
458
func (app * BaseApp ) CheckTx (txBytes []byte ) (res abci.ResponseCheckTx ) {
441
- // Decode the Tx.
459
+
442
460
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
+
443
494
var tx , err = app .txDecoder (txBytes )
444
495
if err != nil {
445
496
result = err .Result ()
@@ -524,6 +575,10 @@ func (app *BaseApp) getContextForAnte(mode runTxMode, txBytes []byte) (ctx sdk.C
524
575
525
576
// Iterates through msgs and executes them
526
577
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
+
527
582
// accumulate results
528
583
logs := make ([]string , 0 , len (msgs ))
529
584
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
589
644
// meter so we initialize upfront.
590
645
var gasWanted int64
591
646
ctx := app .getContextForAnte (mode , txBytes )
592
-
647
+ ctxWithNoCache := ctx
593
648
defer func () {
594
649
if r := recover (); r != nil {
595
650
switch rType := r .(type ) {
@@ -603,7 +658,17 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk
603
658
}
604
659
605
660
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
+ }
607
672
}()
608
673
609
674
var msgs = tx .GetMsgs ()
@@ -621,9 +686,10 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk
621
686
}
622
687
if ! newCtx .IsZero () {
623
688
ctx = newCtx
689
+ ctxWithNoCache = newCtx
624
690
}
625
691
626
- gasWanted = result .GasWanted
692
+ gasWanted = anteResult .GasWanted
627
693
}
628
694
629
695
// 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
637
703
638
704
ctx = ctx .WithMultiStore (msCache )
639
705
result = app .runMsgs (ctx , msgs )
640
- result .GasWanted = gasWanted
641
706
642
707
// only update state if all messages pass and we're not in a simulation
643
708
if result .IsOK () && mode != runTxModeSimulate {
0 commit comments