forked from FourthState/plasma-mvp-sidechain
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.go
143 lines (113 loc) · 3.75 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package app
import (
"encoding/json"
auth "github.com/FourthState/plasma-mvp-sidechain/auth"
plasmaDB "github.com/FourthState/plasma-mvp-sidechain/db"
"github.com/FourthState/plasma-mvp-sidechain/types"
abci "github.com/tendermint/tendermint/abci/types"
"io"
bam "github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
rlp "github.com/ethereum/go-ethereum/rlp"
"github.com/tendermint/go-amino"
crypto "github.com/tendermint/tendermint/crypto"
cmn "github.com/tendermint/tendermint/libs/common"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
tmtypes "github.com/tendermint/tendermint/types"
)
const (
appName = "plasmaChildChain"
)
// Extended ABCI application
type ChildChain struct {
*bam.BaseApp
cdc *amino.Codec
txIndex *uint16
feeAmount *uint64
// keys to access the substores
capKeyMainStore *sdk.KVStoreKey
// Manage addition and deletion of utxo's
utxoMapper types.UTXOMapper
}
func NewChildChain(logger log.Logger, db dbm.DB, traceStore io.Writer) *ChildChain {
cdc := MakeCodec()
bapp := bam.NewBaseApp(appName, cdc, logger, db)
bapp.SetCommitMultiStoreTracer(traceStore)
var app = &ChildChain{
BaseApp: bapp,
cdc: cdc,
txIndex: new(uint16),
feeAmount: new(uint64),
capKeyMainStore: sdk.NewKVStoreKey("main"),
}
// define the utxoMapper
app.utxoMapper = plasmaDB.NewUTXOMapper(
app.capKeyMainStore, // target store
cdc,
)
// UTXOKeeper to adjust spending and recieving of utxo's
UTXOKeeper := plasmaDB.NewUTXOKeeper(app.utxoMapper)
app.Router().
AddRoute("spend", auth.NewHandler(UTXOKeeper, app.txIndex))
// set the BaseApp txDecoder to use txDecoder with RLP
app.SetTxDecoder(app.txDecoder)
app.MountStoresIAVL(app.capKeyMainStore)
app.SetInitChainer(app.initChainer)
app.SetEndBlocker(app.endBlocker)
// NOTE: type AnteHandler func(ctx Context, tx Tx) (newCtx Context, result Result, abort bool)
app.SetAnteHandler(auth.NewAnteHandler(app.utxoMapper, app.txIndex, app.feeAmount))
err := app.LoadLatestVersion(app.capKeyMainStore)
if err != nil {
cmn.Exit(err.Error())
}
return app
}
func (app *ChildChain) initChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
stateJSON := req.AppStateBytes
// TODO is this now the whole genesis file?
var genesisState GenesisState
err := app.cdc.UnmarshalJSON(stateJSON, &genesisState)
if err != nil {
panic(err) // TODO https://github.com/cosmos/cosmos-sdk/issues/468
// return sdk.ErrGenesisParse("").TraceCause(err, "")
}
// load the accounts
for _, gutxo := range genesisState.UTXOs {
utxo := ToUTXO(gutxo)
app.utxoMapper.AddUTXO(ctx, utxo)
}
// load the initial stake information
return abci.ResponseInitChain{}
}
func (app *ChildChain) endBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
// reset txIndex and fee
*app.txIndex = 0
*app.feeAmount = 0
return abci.ResponseEndBlock{}
}
// RLP decodes the txBytes to a BaseTx
func (app *ChildChain) txDecoder(txBytes []byte) (sdk.Tx, sdk.Error) {
var tx = types.BaseTx{}
err := rlp.DecodeBytes(txBytes, &tx)
if err != nil {
return nil, sdk.ErrTxDecode(err.Error())
}
return tx, nil
}
func MakeCodec() *amino.Codec {
cdc := amino.NewCodec()
cdc.RegisterInterface((*sdk.Msg)(nil), nil)
cdc.RegisterConcrete(PlasmaGenTx{}, "app/PlasmaGenTx", nil)
types.RegisterAmino(cdc)
crypto.RegisterAmino(cdc)
return cdc
}
func (app *ChildChain) ExportAppStateJSON() (appState json.RawMessage, validators []tmtypes.GenesisValidator, err error) {
// TODO: Implement
// Currently non-functional, just enough to compile
tx := types.BaseTx{}
appState, err = app.cdc.MarshalJSONIndent(tx, "", "\t")
validators = []tmtypes.GenesisValidator{}
return appState, validators, err
}