forked from FourthState/plasma-mvp-sidechain
-
Notifications
You must be signed in to change notification settings - Fork 3
/
handler.go
53 lines (46 loc) · 1.61 KB
/
handler.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
package auth
import (
db "github.com/FourthState/plasma-mvp-sidechain/db"
types "github.com/FourthState/plasma-mvp-sidechain/types"
utils "github.com/FourthState/plasma-mvp-sidechain/utils"
sdk "github.com/cosmos/cosmos-sdk/types"
"reflect"
)
func NewHandler(uk db.UTXOKeeper, txIndex *uint16) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
switch msg := msg.(type) {
case types.SpendMsg:
return handleSpendMsg(ctx, uk, msg, txIndex)
default:
errMsg := "Unrecognized Msg type: " + reflect.TypeOf(msg).Name()
return sdk.ErrUnknownRequest(errMsg).Result()
}
}
}
// Handle SpendMsg.
// Spends inputs, creates new outputs
func handleSpendMsg(ctx sdk.Context, uk db.UTXOKeeper, msg types.SpendMsg, txIndex *uint16) sdk.Result {
position1 := types.Position{msg.Blknum1, msg.Txindex1, msg.Oindex1, msg.DepositNum1}
inputAddr1 := msg.Owner1
utxo1 := uk.UM.GetUTXO(ctx, inputAddr1, position1)
uk.SpendUTXO(ctx, inputAddr1, position1)
var position2 types.Position
var utxo2 types.UTXO
inputAddr2 := msg.Owner2
if !utils.ZeroAddress(inputAddr2) {
position2 = types.Position{msg.Blknum2, msg.Txindex2, msg.Oindex2, msg.DepositNum2}
utxo2 = uk.UM.GetUTXO(ctx, inputAddr2, position2)
uk.SpendUTXO(ctx, inputAddr2, position2)
}
oldUTXOs := [2]types.UTXO{utxo1, utxo2}
uk.RecieveUTXO(ctx, msg.Newowner1, msg.Denom1, oldUTXOs, 0, *txIndex)
if !utils.ZeroAddress(msg.Newowner2) {
uk.RecieveUTXO(ctx, msg.Newowner2, msg.Denom2, oldUTXOs, 1, *txIndex)
}
// Increment txIndex
if !ctx.IsCheckTx() {
(*txIndex)++
}
// TODO: add some tags so we can search it!
return sdk.Result{} // TODO
}