Skip to content

Commit

Permalink
#273 Generate lottery results cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
hleb-albau committed Feb 26, 2019
1 parent 128009e commit 34d5d03
Show file tree
Hide file tree
Showing 5 changed files with 132,489 additions and 83 deletions.
117 changes: 117 additions & 0 deletions daemon/cmd/lottery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package cmd

import (
"bufio"
"encoding/csv"
"encoding/json"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cybercongress/cyberd/app"
"github.com/ethereum/go-ethereum/common"
"github.com/hleb-albau/ethereum-pubkey-collector/crypto"
"github.com/hleb-albau/ethereum-pubkey-collector/storage"
"github.com/spf13/cobra"
"github.com/tendermint/tendermint/libs/cli"
"io"
"io/ioutil"
"os"
"strconv"
)

func LotteryBalancesCmd(ctx *server.Context, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "lottery-balances <path-to-pou-file> <path-to-pubkeys-db>",
Short: "Initialize private validator, p2p, genesis, and application configuration files",
Long: `Initialize validators's and node's configuration files.`,
Args: cobra.ExactArgs(2),
RunE: func(_ *cobra.Command, args []string) error {

db, err := storage.OpenDb(args[1])
if err != nil {
return err
}

accs, balances, err := readPouEthAccounts(args[0])
if err != nil {
return err
}

resultMap := make(map[string]cyberAccInfo, len(accs))
for i := range accs {
ethAddr := common.HexToAddress(accs[i])
ethRawPubkey := db.GetAddressPublicKey(ethAddr)
if ethRawPubkey == nil {
println(accs[i])
continue
}
cosmosAddr := crypto.CosmosAddressFromEthKey(ethRawPubkey)
accInfo := cyberAccInfo{
Balance: balances[i],
Address: crypto.EncodeToHex(cosmosAddr, "cyber"),
}
resultMap[accs[i]] = accInfo
}

resultJson, err := json.MarshalIndent(resultMap, "", "\t")
if err != nil {
return err
}

return ioutil.WriteFile("lottery.json", resultJson, 0777)
},
}

cmd.Flags().String(cli.HomeFlag, app.DefaultNodeHome, "node's home directory")

return cmd
}

type cyberAccInfo struct {
Address string `json:"address"`
Balance int64 `json:"balance"`
}

func readPouEthAccounts(path string) ([]string, []int64, error) {
accs := make([]string, 0)
balances := make([]int64, 0)
pocFile, err := os.Open(path)
if err != nil {
return accs, balances, err
}

reader := csv.NewReader(bufio.NewReader(pocFile))
reader.Comma = ' '

for {

line, err := reader.Read()
if err == io.EOF {
break
} else if err != nil {
return accs, balances, err
}

accShare, err := strconv.ParseFloat(line[1], 64)
if err != nil {
return accs, balances, err
}
accs = append(accs, line[0])
balances = append(balances, pouShareToAmt(accShare))
}

return accs, balances, nil
}

// output example
/*
{
"0x9f4062f6153ff4dbf93f6a6f686ed3c906bf0684: : {
"address" : "cyber1f9yjqmxh6prsmgpcaqj8lmjnxg644n5074zznm",
"balance" : 1234
},
"0x9f4062f6153ff4dbf93f6a6f686ed3c906bf0684: : {
"address" : "cyber1f9yjqmxh6prsmgpcaqj8lmjnxg644n5074zznm",
"balance" : 1234
}
}
*/
1 change: 1 addition & 0 deletions daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func main() {
rootCmd.AddCommand(cmd.GenTxCmd(ctx, cdc))
rootCmd.AddCommand(cmd.AddGenesisAccountCmd(ctx, cdc))
rootCmd.AddCommand(cmd.GenesisCmds(ctx, cdc))
rootCmd.AddCommand(cmd.LotteryBalancesCmd(ctx, cdc))
rootCmd.AddCommand(client.NewCompletionCmd(rootCmd, true))
server.AddCommands(ctx, cdc, rootCmd, newApp, exportAppStateAndTMValidators)

Expand Down
45 changes: 4 additions & 41 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,71 +1,34 @@
module github.com/cybercongress/cyberd

require (
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/VividCortex/gohistogram v1.0.0 // indirect
github.com/ZondaX/hid-go v0.0.0-20180905224704-48b08affede2 // indirect
github.com/bartekn/go-bip39 v0.0.0-20171116152956-a05967ea095d // indirect
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/btcsuite/btcd v0.0.0-20180903232927-cff30e1d23fc // indirect
github.com/btcsuite/btcutil v0.0.0-20180706230648-ab6388e0c60a // indirect
github.com/cosmos/cosmos-sdk v0.32.0
github.com/cosmos/go-bip39 v0.0.0-20180618194314-52158e4697b8 // indirect
github.com/ethereum/go-ethereum v1.8.23 // indirect
github.com/fortytw2/leaktest v1.2.0 // indirect
github.com/go-kit/kit v0.7.0 // indirect
github.com/go-logfmt/logfmt v0.3.0 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/gogo/protobuf v1.1.0 // indirect
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect
github.com/google/gofuzz v0.0.0-20170612174753-24818f796faf // indirect
github.com/ethereum/go-ethereum v1.8.20
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/mux v1.6.2 // indirect
github.com/gorilla/websocket v1.4.0 // indirect
github.com/gxed/hashland v0.0.0-20180221191214-d9f6b97f8db2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/hleb-albau/ethereum-pubkey-collector v0.1.1-0.20190225072122-8f45df369725
github.com/ipfs/go-cid v0.9.0
github.com/jmhodges/levigo v0.0.0-20161115193449-c42d9e0ca023 // indirect
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 // indirect
github.com/magiconair/properties v1.8.0 // indirect
github.com/mattn/go-isatty v0.0.3 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 // indirect
github.com/minio/sha256-simd v0.0.0-20181005183134-51976451ce19 // indirect
github.com/mitchellh/mapstructure v1.0.0 // indirect
github.com/mr-tron/base58 v1.1.0 // indirect
github.com/multiformats/go-multibase v0.3.0 // indirect
github.com/multiformats/go-multihash v1.0.8 // indirect
github.com/onsi/gomega v1.4.2 // indirect
github.com/pelletier/go-toml v1.2.0 // indirect
github.com/pkg/errors v0.8.0
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v0.9.0-pre1.0.20180709125804-ae27198cdd90 // indirect
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 // indirect
github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e // indirect
github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273 // indirect
github.com/rakyll/statik v0.1.5 // indirect
github.com/rcrowley/go-metrics v0.0.0-20180503174638-e2704e165165 // indirect
github.com/rs/cors v1.6.0 // indirect
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 // indirect
github.com/spf13/afero v1.1.2 // indirect
github.com/spf13/cast v1.2.0 // indirect
github.com/spf13/cobra v0.0.1
github.com/spf13/jwalterweatherman v1.0.0 // indirect
github.com/spf13/pflag v1.0.2 // indirect
github.com/spf13/cobra v0.0.3
github.com/spf13/viper v1.0.0
github.com/stretchr/testify v1.2.1
github.com/syndtr/goleveldb v0.0.0-20181128100959-b001fa50d6b2 // indirect
github.com/stretchr/testify v1.2.2
github.com/tendermint/btcd v0.0.0-20180816174608-e5840949ff4f
github.com/tendermint/go-amino v0.14.1
github.com/tendermint/iavl v0.12.1 // indirect
github.com/tendermint/tendermint v0.30.1
github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc // indirect
github.com/zondax/ledger-cosmos-go v0.1.0 // indirect
golang.org/x/crypto v0.0.0-20181015023909-0c41d7ab0a0e // indirect
golang.org/x/net v0.0.0-20180911220305-26e67e76b6c3 // indirect
google.golang.org/grpc v1.15.0 // indirect
)

replace golang.org/x/crypto => github.com/tendermint/crypto v0.0.0-20180820045704-3764759f34a5
Loading

0 comments on commit 34d5d03

Please sign in to comment.