-
Notifications
You must be signed in to change notification settings - Fork 1
/
genesis.go
176 lines (144 loc) · 4.71 KB
/
genesis.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package app
import (
"encoding/json"
"errors"
"fmt"
"os"
"strconv"
"github.com/spf13/pflag"
"github.com/spf13/viper"
crypto "github.com/tendermint/tendermint/crypto"
tmtypes "github.com/tendermint/tendermint/types"
"github.com/AdityaSripal/plasma-mvp-sidechain/types"
"github.com/AdityaSripal/plasma-mvp-sidechain/x/utxo"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server"
"github.com/ethereum/go-ethereum/common"
)
// State to Unmarshal
type GenesisState struct {
Validator GenesisValidator `json:"genvalidator"`
UTXOs []GenesisUTXO `json:"UTXOs"`
}
type GenesisValidator struct {
ConsPubKey crypto.PubKey `json:"validator_pubkey"`
Address string `json:"fee_address"`
}
type GenesisUTXO struct {
Address string
Denom string
Position [4]string
}
func NewGenesisUTXO(addr string, amount string, position [4]string) GenesisUTXO {
utxo := GenesisUTXO{
Address: addr,
Denom: amount,
Position: position,
}
return utxo
}
func ToUTXO(gutxo GenesisUTXO) utxo.UTXO {
// Any failed str conversion defaults to 0
addr := common.HexToAddress(gutxo.Address)
amount, _ := strconv.ParseUint(gutxo.Denom, 10, 64)
blkNum, _ := strconv.ParseUint(gutxo.Position[0], 10, 64)
txIndex, _ := strconv.ParseUint(gutxo.Position[1], 10, 16)
oIndex, _ := strconv.ParseUint(gutxo.Position[2], 10, 8)
depNum, _ := strconv.ParseUint(gutxo.Position[3], 10, 64)
position := types.NewPlasmaPosition(blkNum, uint16(txIndex), uint8(oIndex), depNum)
return utxo.NewUTXO(addr.Bytes(), amount, "Ether", position)
}
var (
flagAddress = "address"
flagClientHome = "home-client"
flagOWK = "owk"
// UTXO amount awarded
freeEtherVal = int64(100)
// default home directories for expected binaries
DefaultCLIHome = os.ExpandEnv("$HOME/.plasmacli")
DefaultNodeHome = os.ExpandEnv("$HOME/.plasmad")
)
// get app init parameters for server init command
func PlasmaAppInit() server.AppInit {
fsAppGenTx := pflag.NewFlagSet("", pflag.ContinueOnError)
fsAppGenTx.String(flagAddress, "", "address, required")
fsAppGenTx.String(flagClientHome, DefaultCLIHome,
"home directory for the client, used for key generation")
fsAppGenTx.Bool(flagOWK, false, "overwrite the accounts created")
return server.AppInit{
AppGenState: PlasmaAppGenStateJSON,
}
}
// simple genesis tx
type PlasmaGenTx struct {
// currently takes address as string because unmarshaling Ether address fails
Address string `json:"address"`
}
// Generate a gaia genesis transaction with flags
func PlasmaAppGenTx(cdc *codec.Codec, pk crypto.PubKey) (
appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) {
addrString := viper.GetString(flagAddress)
overwrite := viper.GetBool(flagOWK)
bz, err := cdc.MarshalJSON("success")
cliPrint = json.RawMessage(bz)
appGenTx, _, validator, err = PlasmaAppGenTxNF(cdc, pk, addrString, overwrite)
return
}
// Generate a gaia genesis transaction without flags
func PlasmaAppGenTxNF(cdc *codec.Codec, pk crypto.PubKey, addr string, overwrite bool) (
appGenTx, cliPrint json.RawMessage, validator tmtypes.GenesisValidator, err error) {
var bz []byte
plasmaGenTx := PlasmaGenTx{
Address: addr,
}
bz, err = codec.MarshalJSONIndent(cdc, plasmaGenTx)
if err != nil {
return
}
appGenTx = json.RawMessage(bz)
validator = tmtypes.GenesisValidator{
PubKey: pk,
Power: 1,
}
return
}
// Create the core parameters for genesis initialization for gaia
// note that the pubkey input is this machines pubkey
func PlasmaAppGenState(cdc *codec.Codec, genDoc tmtypes.GenesisDoc, appGenTxs []json.RawMessage) (genesisState GenesisState, err error) {
if err = cdc.UnmarshalJSON(genDoc.AppState, &genesisState); err != nil {
return genesisState, err
}
if len(appGenTxs) == 0 {
err = errors.New("must provide at least genesis transaction")
return
}
// get genesis flag account information
genUTXO := make([]GenesisUTXO, len(appGenTxs))
for i, appGenTx := range appGenTxs {
var genTx PlasmaGenTx
err = cdc.UnmarshalJSON(appGenTx, &genTx)
if err != nil {
return
}
genUTXO[i] = NewGenesisUTXO(genTx.Address, "100", [4]string{"0", "0", "0", fmt.Sprintf("%d", i+1)})
}
// create the final app state
genesisState.UTXOs = genUTXO
return
}
// PlasmaAppGenState but with JSON
func PlasmaAppGenStateJSON(cdc *codec.Codec, genDoc tmtypes.GenesisDoc, appGenTxs []json.RawMessage) (appState json.RawMessage, err error) {
// create the final app state
genesisState, err := PlasmaAppGenState(cdc, genDoc, appGenTxs)
if err != nil {
return nil, err
}
appState, err = codec.MarshalJSONIndent(cdc, genesisState)
return
}
func NewDefaultGenesisState(pubkey crypto.PubKey) GenesisState {
return GenesisState{
Validator: GenesisValidator{pubkey, ""},
UTXOs: nil,
}
}