initial add gaia

working
This commit is contained in:
rigelrozanski
2018-03-28 19:01:49 +02:00
parent 76d66aba3b
commit 266a8392d3
21 changed files with 3767 additions and 0 deletions
+97
View File
@@ -0,0 +1,97 @@
package main
import (
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client/commands"
"github.com/cosmos/cosmos-sdk/client/commands/commits"
"github.com/cosmos/cosmos-sdk/client/commands/keys"
"github.com/cosmos/cosmos-sdk/client/commands/proxy"
"github.com/cosmos/cosmos-sdk/client/commands/query"
rpccmd "github.com/cosmos/cosmos-sdk/client/commands/rpc"
txcmd "github.com/cosmos/cosmos-sdk/client/commands/txs"
authcmd "github.com/cosmos/cosmos-sdk/modules/auth/commands"
basecmd "github.com/cosmos/cosmos-sdk/modules/base/commands"
coincmd "github.com/cosmos/cosmos-sdk/modules/coin/commands"
feecmd "github.com/cosmos/cosmos-sdk/modules/fee/commands"
ibccmd "github.com/cosmos/cosmos-sdk/modules/ibc/commands"
noncecmd "github.com/cosmos/cosmos-sdk/modules/nonce/commands"
rolecmd "github.com/cosmos/cosmos-sdk/modules/roles/commands"
stakecmd "github.com/cosmos/gaia/modules/stake/commands"
)
// clientCmd is the entry point for this binary
var clientCmd = &cobra.Command{
Use: "client",
Short: "Gaia light client",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
func prepareClientCommands() {
commands.AddBasicFlags(clientCmd)
// Prepare queries
query.RootCmd.AddCommand(
// These are default parsers, but optional in your app (you can remove key)
query.TxQueryCmd,
query.KeyQueryCmd,
coincmd.AccountQueryCmd,
noncecmd.NonceQueryCmd,
rolecmd.RoleQueryCmd,
ibccmd.IBCQueryCmd,
//stakecmd.CmdQueryValidator,
stakecmd.CmdQueryCandidates,
stakecmd.CmdQueryCandidate,
stakecmd.CmdQueryDelegatorBond,
stakecmd.CmdQueryDelegatorCandidates,
)
// set up the middleware
txcmd.Middleware = txcmd.Wrappers{
feecmd.FeeWrapper{},
rolecmd.RoleWrapper{},
noncecmd.NonceWrapper{},
basecmd.ChainWrapper{},
authcmd.SigWrapper{},
}
txcmd.Middleware.Register(txcmd.RootCmd.PersistentFlags())
// you will always want this for the base send command
txcmd.RootCmd.AddCommand(
// This is the default transaction, optional in your app
coincmd.SendTxCmd,
coincmd.CreditTxCmd,
// this enables creating roles
rolecmd.CreateRoleTxCmd,
// these are for handling ibc
ibccmd.RegisterChainTxCmd,
ibccmd.UpdateChainTxCmd,
ibccmd.PostPacketTxCmd,
stakecmd.CmdDeclareCandidacy,
stakecmd.CmdEditCandidacy,
stakecmd.CmdDelegate,
stakecmd.CmdUnbond,
)
clientCmd.AddCommand(
proxy.RootCmd,
lineBreak,
txcmd.RootCmd,
query.RootCmd,
rpccmd.RootCmd,
lineBreak,
keys.RootCmd,
commands.InitCmd,
commands.ResetCmd,
commits.RootCmd,
lineBreak,
)
}
+50
View File
@@ -0,0 +1,50 @@
package main
import (
"os"
"github.com/spf13/cobra"
"github.com/tendermint/tmlibs/cli"
basecmd "github.com/cosmos/cosmos-sdk/server/commands"
"github.com/cosmos/gaia/version"
)
// GaiaCmd is the entry point for this binary
var (
GaiaCmd = &cobra.Command{
Use: "gaia",
Short: "The Cosmos Network delegation-game test",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}
lineBreak = &cobra.Command{Run: func(*cobra.Command, []string) {}}
)
func main() {
// disable sorting
cobra.EnableCommandSorting = false
// add commands
prepareNodeCommands()
prepareRestServerCommands()
prepareClientCommands()
GaiaCmd.AddCommand(
nodeCmd,
restServerCmd,
clientCmd,
lineBreak,
version.VersionCmd,
//auto.AutoCompleteCmd,
)
// prepare and add flags
basecmd.SetUpRoot(GaiaCmd)
executor := cli.PrepareMainCmd(GaiaCmd, "GA", os.ExpandEnv("$HOME/.cosmos-gaia-cli"))
executor.Execute()
}
+69
View File
@@ -0,0 +1,69 @@
package main
import (
"github.com/spf13/cobra"
abci "github.com/tendermint/abci/types"
sdk "github.com/cosmos/cosmos-sdk"
"github.com/cosmos/cosmos-sdk/modules/auth"
"github.com/cosmos/cosmos-sdk/modules/base"
"github.com/cosmos/cosmos-sdk/modules/coin"
"github.com/cosmos/cosmos-sdk/modules/fee"
"github.com/cosmos/cosmos-sdk/modules/ibc"
"github.com/cosmos/cosmos-sdk/modules/nonce"
"github.com/cosmos/cosmos-sdk/modules/roles"
basecmd "github.com/cosmos/cosmos-sdk/server/commands"
"github.com/cosmos/cosmos-sdk/stack"
"github.com/cosmos/cosmos-sdk/state"
"github.com/cosmos/gaia/modules/stake"
)
// nodeCmd is the entry point for this binary
var nodeCmd = &cobra.Command{
Use: "node",
Short: "The Cosmos Network delegation-game blockchain test",
Run: func(cmd *cobra.Command, args []string) { cmd.Help() },
}
func prepareNodeCommands() {
basecmd.Handler = stack.New(
base.Logger{},
stack.Recovery{},
auth.Signatures{},
base.Chain{},
stack.Checkpoint{OnCheck: true},
nonce.ReplayCheck{},
).
IBC(ibc.NewMiddleware()).
Apps(
roles.NewMiddleware(),
fee.NewSimpleFeeMiddleware(coin.Coin{"fermion", 0}, fee.Bank),
stack.Checkpoint{OnDeliver: true},
).
Dispatch(
coin.NewHandler(),
stack.WrapHandler(roles.NewHandler()),
stack.WrapHandler(ibc.NewHandler()),
stake.NewHandler(),
)
nodeCmd.AddCommand(
basecmd.GetInitCmd("fermion", []string{"stake/allowed_bond_denom/fermion"}),
basecmd.GetTickStartCmd(sdk.TickerFunc(tickFn)),
basecmd.UnsafeResetAllCmd,
)
}
// Tick - Called every block even if no transaction, process all queues,
// validator rewards, and calculate the validator set difference
func tickFn(ctx sdk.Context, store state.SimpleDB) (change []*abci.Validator, err error) {
// first need to prefix the store, at this point it's a global store
store = stack.PrefixedStore(stake.Name(), store)
// execute Tick
change, err = stake.Tick(ctx, store)
return
}
+89
View File
@@ -0,0 +1,89 @@
package main
import (
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/tmlibs/cli"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/commands"
rest "github.com/cosmos/cosmos-sdk/client/rest"
coinrest "github.com/cosmos/cosmos-sdk/modules/coin/rest"
noncerest "github.com/cosmos/cosmos-sdk/modules/nonce/rest"
rolerest "github.com/cosmos/cosmos-sdk/modules/roles/rest"
stakerest "github.com/cosmos/gaia/modules/stake/rest"
)
const defaultAlgo = "ed25519"
var (
restServerCmd = &cobra.Command{
Use: "rest-server",
Short: "REST client for gaia commands",
Long: `Gaiaserver presents a nice (not raw hex) interface to the gaia blockchain structure.`,
RunE: func(cmd *cobra.Command, args []string) error {
return cmdRestServer(cmd, args)
},
}
flagPort = "port"
)
func prepareRestServerCommands() {
commands.AddBasicFlags(restServerCmd)
restServerCmd.PersistentFlags().IntP(flagPort, "p", 8998, "port to run the server on")
}
func cmdRestServer(cmd *cobra.Command, args []string) error {
router := mux.NewRouter()
rootDir := viper.GetString(cli.HomeFlag)
keyMan := client.GetKeyManager(rootDir)
serviceKeys := rest.NewServiceKeys(keyMan)
serviceTxs := rest.NewServiceTxs(commands.GetNode())
routeRegistrars := []func(*mux.Router) error{
// rest.Keys handlers
serviceKeys.RegisterCRUD,
// Coin handlers (Send, Query, SearchSent)
coinrest.RegisterAll,
// Roles createRole handler
rolerest.RegisterCreateRole,
// Gaia sign transactions handler
serviceKeys.RegisterSignTx,
// Gaia post transaction handler
serviceTxs.RegisterPostTx,
// Nonce query handler
noncerest.RegisterQueryNonce,
// Staking query handlers
stakerest.RegisterQueryCandidate,
stakerest.RegisterQueryCandidates,
stakerest.RegisterQueryDelegatorBond,
stakerest.RegisterQueryDelegatorCandidates,
// Staking tx builders
stakerest.RegisterDelegate,
stakerest.RegisterUnbond,
}
for _, routeRegistrar := range routeRegistrars {
if err := routeRegistrar(router); err != nil {
log.Fatal(err)
}
}
addr := fmt.Sprintf(":%d", viper.GetInt(flagPort))
log.Printf("Serving on %q", addr)
return http.ListenAndServe(addr, router)
}
+275
View File
@@ -0,0 +1,275 @@
#!/bin/bash
set -u
# These global variables are required for common.sh
SERVER_EXE="gaia node"
CLIENT_EXE="gaia client"
ACCOUNTS=(jae ethan bucky rigel igor)
RICH=${ACCOUNTS[0]}
DELEGATOR=${ACCOUNTS[2]}
POOR=${ACCOUNTS[4]}
BASE_DIR=$HOME/stake_test
BASE_DIR2=$HOME/stake_test2
SERVER1=$BASE_DIR/server
SERVER2=$BASE_DIR2/server
oneTimeSetUp() {
#[ "$2" ] || echo "missing parameters, line=${LINENO}" ; exit 1;
# These are passed in as args
CHAIN_ID="stake_test"
# TODO Make this more robust
if [ "$BASE_DIR" == "$HOME/" ]; then
echo "Must be called with argument, or it will wipe your home directory"
exit 1
fi
rm -rf $BASE_DIR 2>/dev/null
mkdir -p $BASE_DIR
if [ "$BASE_DIR2" == "$HOME/" ]; then
echo "Must be called with argument, or it will wipe your home directory"
exit 1
fi
rm -rf $BASE_DIR2 2>/dev/null
mkdir -p $BASE_DIR2
# Set up client - make sure you use the proper prefix if you set
# a custom CLIENT_EXE
export BC_HOME=${BASE_DIR}/client
prepareClient
# start the node server
set +u ; initServer $BASE_DIR $CHAIN_ID ; set -u
if [ $? != 0 ]; then return 1; fi
set +u ; initClient $CHAIN_ID ; set -u
if [ $? != 0 ]; then return 1; fi
printf "...Testing may begin!\n\n\n"
}
oneTimeTearDown() {
kill -9 $PID_SERVER2 >/dev/null 2>&1
set +u ; quickTearDown ; set -u
}
# Ex Usage: checkCandidate $PUBKEY $EXPECTED_VOTING_POWER
checkCandidate() {
CANDIDATE=$(${CLIENT_EXE} query candidate --pubkey=$1)
if ! assertTrue "line=${LINENO}, bad query" $?; then
return 1
fi
assertEquals "line=${LINENO}, proper voting power" "$2" $(echo $CANDIDATE | jq .data.voting_power)
return $?
}
# Ex Usage: checkCandidate $PUBKEY
checkCandidateEmpty() {
CANDIDATE=$(${CLIENT_EXE} query candidate --pubkey=$1 2>/dev/null)
if ! assertFalse "line=${LINENO}, expected empty query" $?; then
return 1
fi
}
# Ex Usage: checkCandidate $DELEGATOR_ADDR $PUBKEY $EXPECTED_SHARES
checkDelegatorBond() {
BOND=$(${CLIENT_EXE} query delegator-bond --delegator-address=$1 --pubkey=$2)
if ! assertTrue "line=${LINENO}, account must exist" $?; then
return 1
fi
assertEquals "line=${LINENO}, proper bond amount" "$3" $(echo $BOND | jq .data.Shares)
return $?
}
# Ex Usage: checkCandidate $DELEGATOR_ADDR $PUBKEY
checkDelegatorBondEmpty() {
BOND=$(${CLIENT_EXE} query delegator-bond --delegator-address=$1 --pubkey=$2 2>/dev/null)
if ! assertFalse "line=${LINENO}, expected empty query" $?; then
return 1
fi
}
#______________________________________________________________________________________
test00GetAccount() {
SENDER=$(getAddr $RICH)
RECV=$(getAddr $POOR)
assertFalse "line=${LINENO}, requires arg" "${CLIENT_EXE} query account"
set +u ; checkAccount $SENDER "9007199254740992" ; set -u
ACCT2=$(${CLIENT_EXE} query account $RECV 2>/dev/null)
assertFalse "line=${LINENO}, has no genesis account" $?
}
test01SendTx() {
assertFalse "line=${LINENO}, missing dest" "${CLIENT_EXE} tx send --amount=992fermion --sequence=1"
assertFalse "line=${LINENO}, bad password" "echo foo | ${CLIENT_EXE} tx send --amount=992fermion --sequence=1 --to=$RECV --name=$RICH"
TX=$(echo qwertyuiop | ${CLIENT_EXE} tx send --amount=992fermion --sequence=1 --to=$RECV --name=$RICH)
txSucceeded $? "$TX" "$RECV"
HASH=$(echo $TX | jq .hash | tr -d \")
TX_HEIGHT=$(echo $TX | jq .height)
set +u
checkAccount $SENDER "9007199254740000" $TX_HEIGHT
# make sure 0x prefix also works
checkAccount "0x$SENDER" "9007199254740000" $TX_HEIGHT
checkAccount $RECV "992" $TX_HEIGHT
# Make sure tx is indexed
checkSendTx $HASH $TX_HEIGHT $SENDER "992"
set -u
}
test02DeclareCandidacy() {
# the premise of this test is to run a second validator (from rich) and then bond and unbond some tokens
# first create a second node to run and connect to the system
# init the second node
SERVER_LOG2=$BASE_DIR2/node2.log
GENKEY=$(${CLIENT_EXE} keys get ${RICH} | awk '{print $2}')
${SERVER_EXE} init $GENKEY --chain-id $CHAIN_ID --home=$SERVER2 >>$SERVER_LOG2
if [ $? != 0 ]; then return 1; fi
# copy in the genesis from the first initialization to the new server
cp $SERVER1/genesis.json $SERVER2/genesis.json
# point the new config to the old server location
rm $SERVER2/config.toml
echo 'proxy_app = "tcp://127.0.0.1:46668"
moniker = "anonymous"
fast_sync = true
db_backend = "leveldb"
log_level = "state:info,*:error"
[rpc]
laddr = "tcp://0.0.0.0:46667"
[p2p]
laddr = "tcp://0.0.0.0:46666"
seeds = "0.0.0.0:46656"' >$SERVER2/config.toml
# start the second node
${SERVER_EXE} start --home=$SERVER2 >>$SERVER_LOG2 2>&1 &
sleep 1
PID_SERVER2=$!
disown
if ! ps $PID_SERVER2 >/dev/null; then
echo "**FAILED**"
cat $SERVER_LOG2
return 1
fi
# get the pubkey of the second validator
PK2=$(cat $SERVER2/priv_validator.json | jq -r .pub_key.data)
CAND_ADDR=$(getAddr $POOR)
TX=$(echo qwertyuiop | ${CLIENT_EXE} tx declare-candidacy --sequence=1 --amount=10fermion --name=$POOR --pubkey=$PK2 --moniker=rigey)
if [ $? != 0 ]; then return 1; fi
HASH=$(echo $TX | jq .hash | tr -d \")
TX_HEIGHT=$(echo $TX | jq .height)
set +u ; checkAccount $CAND_ADDR "982" $TX_HEIGHT ; set -u
checkCandidate $PK2 "10"
checkDelegatorBond $CAND_ADDR $PK2 "10"
}
test03Delegate() {
# send some coins to a delegator
DELA_ADDR=$(getAddr $DELEGATOR)
TX=$(echo qwertyuiop | ${CLIENT_EXE} tx send --sequence=2 --amount=15fermion --to=$DELA_ADDR --name=$RICH)
txSucceeded $? "$TX" "$DELA_ADDR"
TX_HEIGHT=$(echo $TX | jq .height)
set +u ; checkAccount $DELA_ADDR "15" $TX_HEIGHT ; set -u
# delegate some coins to the new
TX=$(echo qwertyuiop | ${CLIENT_EXE} tx delegate --sequence=1 --amount=10fermion --name=$DELEGATOR --pubkey=$PK2)
if [ $? != 0 ]; then return 1; fi
TX_HEIGHT=$(echo $TX | jq .height)
set +u ; checkAccount $DELA_ADDR "5" $TX_HEIGHT ; set -u
checkCandidate $PK2 "20"
checkDelegatorBond $DELA_ADDR $PK2 "10"
TX=$(echo qwertyuiop | ${CLIENT_EXE} tx delegate --sequence=2 --amount=3fermion --name=$DELEGATOR --pubkey=$PK2)
if [ $? != 0 ]; then return 1; fi
TX_HEIGHT=$(echo $TX | jq .height)
set +u ; checkAccount $DELA_ADDR "2" $TX_HEIGHT ; set -u
checkCandidate $PK2 "23"
checkDelegatorBond $DELA_ADDR $PK2 "13"
# attempt a delegation without enough funds
# NOTE the sequence number still increments here because it will fail
# only during DeliverTx - however this should be updated (TODO) in new
# SDK when we can fail in CheckTx
TX=$(echo qwertyuiop | ${CLIENT_EXE} tx delegate --sequence=3 --amount=3fermion --name=$DELEGATOR --pubkey=$PK2 2>/dev/null)
if [ $? == 0 ]; then return 1; fi
TX_HEIGHT=$(echo $TX | jq .height)
set +u ; checkAccount $DELA_ADDR "2" $TX_HEIGHT ; set -u
checkCandidate $PK2 "23"
checkDelegatorBond $DELA_ADDR $PK2 "13"
# perform the final delegation which should empty the delegators account
TX=$(echo qwertyuiop | ${CLIENT_EXE} tx delegate --sequence=4 --amount=2fermion --name=$DELEGATOR --pubkey=$PK2)
if [ $? != 0 ]; then return 1; fi
TX_HEIGHT=$(echo $TX | jq .height)
set +u ; checkAccount $DELA_ADDR "null" $TX_HEIGHT ; set -u #empty account is null
checkCandidate $PK2 "25"
}
test04Unbond() {
# unbond from the delegator a bit
TX=$(echo qwertyuiop | ${CLIENT_EXE} tx unbond --sequence=5 --shares=10 --name=$DELEGATOR --pubkey=$PK2)
TX_HEIGHT=$(echo $TX | jq .height)
set +u ; checkAccount $DELA_ADDR "10" $TX_HEIGHT ; set -u
checkCandidate $PK2 "15"
checkDelegatorBond $DELA_ADDR $PK2 "5"
# attempt to unbond more shares than exist
TX=$(echo qwertyuiop | ${CLIENT_EXE} tx unbond --sequence=6 --shares=10 --name=$DELEGATOR --pubkey=$PK2 2>/dev/null)
if [ $? == 0 ]; then return 1; fi
TX_HEIGHT=$(echo $TX | jq .height)
set +u ; checkAccount $DELA_ADDR "10" $TX_HEIGHT ; set -u
checkCandidate $PK2 "15"
checkDelegatorBond $DELA_ADDR $PK2 "5"
# unbond entirely from the delegator
TX=$(echo qwertyuiop | ${CLIENT_EXE} tx unbond --sequence=6 --shares=5 --name=$DELEGATOR --pubkey=$PK2)
TX_HEIGHT=$(echo $TX | jq .height)
set +u ; checkAccount $DELA_ADDR "15" $TX_HEIGHT ; set -u
checkCandidate $PK2 "10"
checkDelegatorBondEmpty $DELA_ADDR $PK2
# unbond a bit from the owner
TX=$(echo qwertyuiop | ${CLIENT_EXE} tx unbond --sequence=2 --shares=5 --name=$POOR --pubkey=$PK2)
TX_HEIGHT=$(echo $TX | jq .height)
set +u ; checkAccount $CAND_ADDR "987" $TX_HEIGHT ; set -u
checkCandidate $PK2 "5"
checkDelegatorBond $CAND_ADDR $PK2 "5"
# attempt to unbond more shares than exist
TX=$(echo qwertyuiop | ${CLIENT_EXE} tx unbond --sequence=3 --shares=10 --name=$POOR --pubkey=$PK2 2>/dev/null)
if [ $? == 0 ]; then return 1; fi
TX_HEIGHT=$(echo $TX | jq .height)
set +u ; checkAccount $CAND_ADDR "987" $TX_HEIGHT ; set -u
checkCandidate $PK2 "5"
checkDelegatorBond $CAND_ADDR $PK2 "5"
# unbond entirely from the validator
TX=$(echo qwertyuiop | ${CLIENT_EXE} tx unbond --sequence=3 --shares=5 --name=$POOR --pubkey=$PK2)
TX_HEIGHT=$(echo $TX | jq .height)
set +u ; checkAccount $CAND_ADDR "992" $TX_HEIGHT ; set -u
checkCandidateEmpty $PK2
checkDelegatorBondEmpty $CAND_ADDR $PK2
}
# Load common then run these tests with shunit2!
CLI_DIR=$GOPATH/src/github.com/cosmos/gaia/vendor/github.com/cosmos/cosmos-sdk/tests/cli
. $CLI_DIR/common.sh
. $CLI_DIR/shunit2