forked from FourthState/plasma-mvp-sidechain
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.go
102 lines (81 loc) · 2.52 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
package app
import (
auth "github.com/FourthState/plasma-mvp-sidechain/auth"
plasmaDB "github.com/FourthState/plasma-mvp-sidechain/db"
types "github.com/FourthState/plasma-mvp-sidechain/types"
bam "github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/abci/types"
"github.com/tendermint/go-amino"
crypto "github.com/tendermint/go-crypto"
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
//rlp "github.com/ethereum/go-ethereum/rlp"
)
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
txHash []byte
}
func NewChildChain(logger log.Logger, db dbm.DB) *ChildChain {
cdc := MakeCodec()
var app = &ChildChain{
BaseApp: bam.NewBaseApp(appName, cdc, logger, db),
cdc: cdc,
txIndex: new(uint16),
feeAmount: new(uint64),
capKeyMainStore: sdk.NewKVStoreKey("main"),
txHash: nil,
}
// 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("txs", auth.NewHandler(UTXOKeeper, app.txIndex))
// set the BaseApp txDecoder to use txDecoder with RLP
app.SetTxDecoder(app.txDecoder)
app.MountStoresIAVL(app.capKeyMainStore)
// 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) txDecoder(txBytes []byte) (sdk.Tx, sdk.Error) {
// TODO: implement method with RLP
var tx = types.BaseTx{}
// BaseTx is struct for Msg wrapped with authentication data
err := app.cdc.UnmarshalBinary(txBytes, &tx)
if err != nil {
return nil, sdk.ErrTxDecode("")
}
return tx, nil
}
func (app *ChildChain) endBlocker(ctx sdk.Context, req abci.RequestEndBlock) {
header := ctx.BlockHeader()
app.txHash = header.GetDataHash()
}
func MakeCodec() *amino.Codec {
cdc := amino.NewCodec()
cdc.RegisterInterface((*sdk.Msg)(nil), nil)
types.RegisterAmino(cdc)
crypto.RegisterAmino(cdc)
return cdc
}