Skip to content

Commit

Permalink
Merge branch 'master' into robert/snapshot-hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessio Treglia authored Sep 11, 2020
2 parents bec9f67 + c94fd4a commit 69ad410
Show file tree
Hide file tree
Showing 39 changed files with 862 additions and 314 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
.mod
.sum
- name: Run cosmovisor tests
run: cd cosmovisor; go test .
run: cd cosmovisor; make
if: "env.GIT_DIFF != ''"
split-test-files:
runs-on: ubuntu-latest
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ build-simd: go.sum
build-simd-linux: go.sum
LEDGER_ENABLED=false GOOS=linux GOARCH=amd64 $(MAKE) build-simd

.PHONY: build build-simd build-simd-linux
cosmovisor:
$(MAKE) -C cosmovisor cosmovisor

.PHONY: build build-simd build-simd-linux cosmovisor

mocks: $(MOCKS_DIR)
mockgen -source=client/account_retriever.go -package mocks -destination tests/mocks/account_retriever.go
Expand Down
19 changes: 18 additions & 1 deletion baseapp/abci.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package baseapp

import (
"crypto/sha256"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -81,9 +82,25 @@ func (app *BaseApp) InitChain(req abci.RequestInitChain) (res abci.ResponseInitC
}
}

// In the case of a new chain, AppHash will be the hash of an empty string.
// During an upgrade, it'll be the hash of the last committed block.
var appHash []byte
if !app.LastCommitID().IsZero() {
appHash = app.LastCommitID().Hash
} else {
// $ echo -n '' | sha256sum
// e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
emptyHash := sha256.Sum256([]byte{})
appHash = emptyHash[:]
}

// NOTE: We don't commit, but BeginBlock for block `initial_height` starts from this
// deliverState.
return res
return abci.ResponseInitChain{
ConsensusParams: res.ConsensusParams,
Validators: res.Validators,
AppHash: appHash,
}
}

// Info implements the ABCI interface.
Expand Down
11 changes: 10 additions & 1 deletion baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,16 @@ func TestInitChainer(t *testing.T) {
require.Nil(t, err)
require.Equal(t, int64(0), app.LastBlockHeight())

app.InitChain(abci.RequestInitChain{AppStateBytes: []byte("{}"), ChainId: "test-chain-id"}) // must have valid JSON genesis file, even if empty
initChainRes := app.InitChain(abci.RequestInitChain{AppStateBytes: []byte("{}"), ChainId: "test-chain-id"}) // must have valid JSON genesis file, even if empty

// The AppHash returned by a new chain is the sha256 hash of "".
// $ echo -n '' | sha256sum
// e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
require.Equal(
t,
[]byte{0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55},
initChainRes.AppHash,
)

// assert that chainID is set correctly in InitChain
chainID := app.deliverState.ctx.ChainID()
Expand Down
9 changes: 8 additions & 1 deletion client/grpc/simulate/simulate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/grpc/simulate"
"github.com/cosmos/cosmos-sdk/client/tx"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/std"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -98,9 +100,14 @@ func (s IntegrationTestSuite) TestSimulateService() {
)
txBuilder.SetSignatures(sigV2)

any, ok := txBuilder.(codectypes.IntoAny)
s.Require().True(ok)
cached := any.AsAny().GetCachedValue()
txTx, ok := cached.(*txtypes.Tx)
s.Require().True(ok)
res, err := s.queryClient.Simulate(
context.Background(),
&simulate.SimulateRequest{Tx: txBuilder.GetProtoTx()},
&simulate.SimulateRequest{Tx: txTx},
)
s.Require().NoError(err)

Expand Down
2 changes: 1 addition & 1 deletion client/rpc/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func ValidatorCommand() *cobra.Command {

cmd.Flags().StringP(flags.FlagNode, "n", "tcp://localhost:26657", "Node to connect to")
cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)")
cmd.Flags().Int(flags.FlagPage, 0, "Query a specific page of paginated results")
cmd.Flags().Int(flags.FlagPage, rest.DefaultPage, "Query a specific page of paginated results")
cmd.Flags().Int(flags.FlagLimit, 100, "Query number of results returned per page")

return cmd
Expand Down
1 change: 0 additions & 1 deletion client/tx/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

// ConvertTxToStdTx converts a transaction to the legacy StdTx format
func ConvertTxToStdTx(codec *codec.LegacyAmino, tx signing.Tx) (types.StdTx, error) {

if stdTx, ok := tx.(types.StdTx); ok {
return stdTx, nil
}
Expand Down
19 changes: 7 additions & 12 deletions client/tx/legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,21 @@ package tx_test
import (
"testing"

"github.com/stretchr/testify/suite"

"github.com/cosmos/cosmos-sdk/simapp/params"

"github.com/cosmos/cosmos-sdk/x/auth/signing"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"github.com/cosmos/cosmos-sdk/client"
tx2 "github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/cosmos/cosmos-sdk/std"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
types3 "github.com/cosmos/cosmos-sdk/x/auth/types"

signing2 "github.com/cosmos/cosmos-sdk/types/tx/signing"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
"github.com/cosmos/cosmos-sdk/types"
signing2 "github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/cosmos/cosmos-sdk/x/auth/tx"
types3 "github.com/cosmos/cosmos-sdk/x/auth/types"
types2 "github.com/cosmos/cosmos-sdk/x/bank/types"
)

Expand Down
14 changes: 13 additions & 1 deletion client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
sim "github.com/cosmos/cosmos-sdk/client/grpc/simulate"
"github.com/cosmos/cosmos-sdk/client/input"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
)
Expand Down Expand Up @@ -265,7 +267,17 @@ func BuildSimTx(txf Factory, msgs ...sdk.Msg) ([]byte, error) {
return nil, err
}

simReq := sim.SimulateRequest{Tx: txb.GetProtoTx()}
any, ok := txb.(codectypes.IntoAny)
if !ok {
return nil, fmt.Errorf("cannot simulate tx that cannot be wrapped into any")
}
cached := any.AsAny().GetCachedValue()
protoTx, ok := cached.(*tx.Tx)
if !ok {
return nil, fmt.Errorf("cannot simulate amino tx")
}

simReq := sim.SimulateRequest{Tx: protoTx}

return simReq.Marshal()
}
Expand Down
5 changes: 2 additions & 3 deletions client/tx/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ import (
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)

func NewTestTxConfig() client.TxConfig {
_, cdc := simapp.MakeCodecs()
return types.StdTxConfig{Cdc: cdc}
cfg := simapp.MakeEncodingConfig()
return cfg.TxConfig
}

func TestCalculateGas(t *testing.T) {
Expand Down
3 changes: 0 additions & 3 deletions client/tx_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package client

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
)
Expand Down Expand Up @@ -36,8 +35,6 @@ type (
// also know how to encode itself.
TxBuilder interface {
GetTx() signing.Tx
// GetProtoTx returns the tx as a proto.Message.
GetProtoTx() *tx.Tx

SetMsgs(msgs ...sdk.Msg) error
SetSignatures(signatures ...signingtypes.SignatureV2) error
Expand Down
7 changes: 6 additions & 1 deletion codec/types/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (any *Any) Pack(x proto.Message) error {
func UnsafePackAny(x interface{}) *Any {
if msg, ok := x.(proto.Message); ok {
any, err := NewAnyWithValue(msg)
if err != nil {
if err == nil {
return any
}
}
Expand All @@ -107,3 +107,8 @@ func (any *Any) GetCachedValue() interface{} {
func (any *Any) ClearCachedValue() {
any.cachedValue = nil
}

// IntoAny represents a type that can be wrapped into an Any.
type IntoAny interface {
AsAny() *Any
}
2 changes: 1 addition & 1 deletion codec/types/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (any *Any) UnmarshalAmino(bz []byte) error {
return nil
}

func (any Any) MarshalJSON() ([]byte, error) {
func (any *Any) MarshalJSON() ([]byte, error) {
ac := any.compat
if ac == nil {
return nil, anyCompatError("JSON marshal", any)
Expand Down
12 changes: 12 additions & 0 deletions cosmovisor/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/make -f


all: cosmovisor test

cosmovisor:
go build -mod=readonly ./cmd/cosmovisor

test:
go test -mod=readonly -race ./...

.PHONY: all cosmovisor test
17 changes: 11 additions & 6 deletions cosmovisor/args.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package cosmovisor

import (
"errors"
"fmt"
"net/url"
"os"
"path/filepath"

"github.com/pkg/errors"
)

const (
Expand Down Expand Up @@ -80,8 +80,7 @@ func (cfg *Config) CurrentBin() (string, error) {
}

// and return the binary
dest = filepath.Join(dest, "bin", cfg.Name)
return dest, nil
return filepath.Join(dest, "bin", cfg.Name), nil
}

// GetConfigFromEnv will read the environmental variables into a config
Expand All @@ -91,15 +90,19 @@ func GetConfigFromEnv() (*Config, error) {
Home: os.Getenv("DAEMON_HOME"),
Name: os.Getenv("DAEMON_NAME"),
}

if os.Getenv("DAEMON_ALLOW_DOWNLOAD_BINARIES") == "true" {
cfg.AllowDownloadBinaries = true
}

if os.Getenv("DAEMON_RESTART_AFTER_UPGRADE") == "true" {
cfg.RestartAfterUpgrade = true
}

if err := cfg.validate(); err != nil {
return nil, err
}

return cfg, nil
}

Expand All @@ -110,6 +113,7 @@ func (cfg *Config) validate() error {
if cfg.Name == "" {
return errors.New("DAEMON_NAME is not set")
}

if cfg.Home == "" {
return errors.New("DAEMON_HOME is not set")
}
Expand All @@ -121,10 +125,11 @@ func (cfg *Config) validate() error {
// ensure the root directory exists
info, err := os.Stat(cfg.Root())
if err != nil {
return errors.Wrap(err, "cannot stat home dir")
return fmt.Errorf("cannot stat home dir: %w", err)
}

if !info.IsDir() {
return errors.Errorf("%s is not a directory", info.Name())
return fmt.Errorf("%s is not a directory", info.Name())
}

return nil
Expand Down
9 changes: 4 additions & 5 deletions cosmovisor/cmd/main.go → cosmovisor/cmd/cosmovisor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"fmt"
"os"

cosmovisor "github.com/cosmos/cosmos-sdk/cosmovisor"
"github.com/cosmos/cosmos-sdk/cosmovisor"
)

func main() {
err := Run(os.Args[1:])
if err != nil {
fmt.Printf("%+v\n", err)
if err := Run(os.Args[1:]); err != nil {
fmt.Fprintf(os.Stderr, "%+v\n", err)
os.Exit(1)
}
}
Expand All @@ -21,8 +20,8 @@ func Run(args []string) error {
if err != nil {
return err
}
doUpgrade, err := cosmovisor.LaunchProcess(cfg, args, os.Stdout, os.Stderr)

doUpgrade, err := cosmovisor.LaunchProcess(cfg, args, os.Stdout, os.Stderr)
// if RestartAfterUpgrade, we launch after a successful upgrade (only condition LaunchProcess returns nil)
for cfg.RestartAfterUpgrade && err == nil && doUpgrade {
doUpgrade, err = cosmovisor.LaunchProcess(cfg, args, os.Stdout, os.Stderr)
Expand Down
1 change: 0 additions & 1 deletion cosmovisor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ go 1.14
require (
github.com/hashicorp/go-getter v1.4.1
github.com/otiai10/copy v1.2.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.6.1
)
2 changes: 0 additions & 2 deletions cosmovisor/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
github.com/otiai10/mint v1.3.1 h1:BCmzIS3n71sGfHB5NMNDB3lHYPz8fWSkCAErHed//qc=
github.com/otiai10/mint v1.3.1/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
Loading

0 comments on commit 69ad410

Please sign in to comment.