Merge branch 'develop' into davekaj/set-fee-collection-keeper

This commit is contained in:
Ethan Buchman
2018-07-02 20:43:33 -04:00
committed by GitHub
263 changed files with 9568 additions and 4500 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
package types
import abci "github.com/tendermint/abci/types"
import abci "github.com/tendermint/tendermint/abci/types"
// initialize application state at genesis
type InitChainer func(ctx Context, req abci.RequestInitChain) abci.ResponseInitChain
+3 -3
View File
@@ -5,9 +5,9 @@ import (
"errors"
"fmt"
crypto "github.com/tendermint/go-crypto"
"github.com/tendermint/tmlibs/bech32"
cmn "github.com/tendermint/tmlibs/common"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/libs/bech32"
cmn "github.com/tendermint/tendermint/libs/common"
)
//Address is a go crypto-style Address
+2 -2
View File
@@ -244,11 +244,11 @@ func (coins Coins) AmountOf(denom string) Int {
midIdx := len(coins) / 2 // 2:1, 3:1, 4:2
coin := coins[midIdx]
if denom < coin.Denom {
return Coins(coins[:midIdx]).AmountOf(denom)
return coins[:midIdx].AmountOf(denom)
} else if denom == coin.Denom {
return coin.Amount
} else {
return Coins(coins[midIdx+1:]).AmountOf(denom)
return coins[midIdx+1:].AmountOf(denom)
}
}
}
+16 -31
View File
@@ -4,11 +4,10 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestIsPositiveCoin(t *testing.T) {
assert := assert.New(t)
cases := []struct {
inputOne Coin
expected bool
@@ -20,13 +19,11 @@ func TestIsPositiveCoin(t *testing.T) {
for _, tc := range cases {
res := tc.inputOne.IsPositive()
assert.Equal(tc.expected, res)
require.Equal(t, tc.expected, res)
}
}
func TestIsNotNegativeCoin(t *testing.T) {
assert := assert.New(t)
cases := []struct {
inputOne Coin
expected bool
@@ -38,13 +35,11 @@ func TestIsNotNegativeCoin(t *testing.T) {
for _, tc := range cases {
res := tc.inputOne.IsNotNegative()
assert.Equal(tc.expected, res)
require.Equal(t, tc.expected, res)
}
}
func TestSameDenomAsCoin(t *testing.T) {
assert := assert.New(t)
cases := []struct {
inputOne Coin
inputTwo Coin
@@ -59,13 +54,11 @@ func TestSameDenomAsCoin(t *testing.T) {
for _, tc := range cases {
res := tc.inputOne.SameDenomAs(tc.inputTwo)
assert.Equal(tc.expected, res)
require.Equal(t, tc.expected, res)
}
}
func TestIsGTECoin(t *testing.T) {
assert := assert.New(t)
cases := []struct {
inputOne Coin
inputTwo Coin
@@ -79,13 +72,11 @@ func TestIsGTECoin(t *testing.T) {
for _, tc := range cases {
res := tc.inputOne.IsGTE(tc.inputTwo)
assert.Equal(tc.expected, res)
require.Equal(t, tc.expected, res)
}
}
func TestIsEqualCoin(t *testing.T) {
assert := assert.New(t)
cases := []struct {
inputOne Coin
inputTwo Coin
@@ -100,13 +91,11 @@ func TestIsEqualCoin(t *testing.T) {
for _, tc := range cases {
res := tc.inputOne.IsEqual(tc.inputTwo)
assert.Equal(tc.expected, res)
require.Equal(t, tc.expected, res)
}
}
func TestPlusCoin(t *testing.T) {
assert := assert.New(t)
cases := []struct {
inputOne Coin
inputTwo Coin
@@ -119,7 +108,7 @@ func TestPlusCoin(t *testing.T) {
for _, tc := range cases {
res := tc.inputOne.Plus(tc.inputTwo)
assert.Equal(tc.expected, res)
require.Equal(t, tc.expected, res)
}
tc := struct {
@@ -128,12 +117,10 @@ func TestPlusCoin(t *testing.T) {
expected int64
}{NewCoin("asdf", -1), NewCoin("asdf", 1), 0}
res := tc.inputOne.Plus(tc.inputTwo)
assert.Equal(tc.expected, res.Amount.Int64())
require.Equal(t, tc.expected, res.Amount.Int64())
}
func TestMinusCoin(t *testing.T) {
assert := assert.New(t)
cases := []struct {
inputOne Coin
inputTwo Coin
@@ -147,7 +134,7 @@ func TestMinusCoin(t *testing.T) {
for _, tc := range cases {
res := tc.inputOne.Minus(tc.inputTwo)
assert.Equal(tc.expected, res)
require.Equal(t, tc.expected, res)
}
tc := struct {
@@ -156,7 +143,7 @@ func TestMinusCoin(t *testing.T) {
expected int64
}{NewCoin("A", 1), NewCoin("A", 1), 0}
res := tc.inputOne.Minus(tc.inputTwo)
assert.Equal(tc.expected, res.Amount.Int64())
require.Equal(t, tc.expected, res.Amount.Int64())
}
@@ -208,8 +195,6 @@ func TestCoins(t *testing.T) {
}
func TestPlusCoins(t *testing.T) {
assert := assert.New(t)
one := NewInt(1)
zero := NewInt(0)
negone := NewInt(-1)
@@ -229,8 +214,8 @@ func TestPlusCoins(t *testing.T) {
for _, tc := range cases {
res := tc.inputOne.Plus(tc.inputTwo)
assert.True(res.IsValid())
assert.Equal(tc.expected, res)
assert.True(t, res.IsValid())
require.Equal(t, tc.expected, res)
}
}
@@ -260,9 +245,9 @@ func TestParse(t *testing.T) {
for _, tc := range cases {
res, err := ParseCoins(tc.input)
if !tc.valid {
assert.NotNil(t, err, "%s: %#v", tc.input, res)
require.NotNil(t, err, "%s: %#v", tc.input, res)
} else if assert.Nil(t, err, "%s: %+v", tc.input, err) {
assert.Equal(t, tc.expected, res)
require.Equal(t, tc.expected, res)
}
}
@@ -312,9 +297,9 @@ func TestSortCoins(t *testing.T) {
}
for _, tc := range cases {
assert.Equal(t, tc.before, tc.coins.IsValid())
require.Equal(t, tc.before, tc.coins.IsValid())
tc.coins.Sort()
assert.Equal(t, tc.after, tc.coins.IsValid())
require.Equal(t, tc.after, tc.coins.IsValid())
}
}
+2 -2
View File
@@ -6,8 +6,8 @@ import (
"github.com/golang/protobuf/proto"
abci "github.com/tendermint/abci/types"
"github.com/tendermint/tmlibs/log"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
)
/*
+10 -11
View File
@@ -3,15 +3,14 @@ package types_test
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
"github.com/cosmos/cosmos-sdk/store"
"github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/abci/types"
abci "github.com/tendermint/tendermint/abci/types"
)
type MockLogger struct {
@@ -72,21 +71,21 @@ func TestCacheContext(t *testing.T) {
ctx := defaultContext(key)
store := ctx.KVStore(key)
store.Set(k1, v1)
assert.Equal(t, v1, store.Get(k1))
assert.Nil(t, store.Get(k2))
require.Equal(t, v1, store.Get(k1))
require.Nil(t, store.Get(k2))
cctx, write := ctx.CacheContext()
cstore := cctx.KVStore(key)
assert.Equal(t, v1, cstore.Get(k1))
assert.Nil(t, cstore.Get(k2))
require.Equal(t, v1, cstore.Get(k1))
require.Nil(t, cstore.Get(k2))
cstore.Set(k2, v2)
assert.Equal(t, v2, cstore.Get(k2))
assert.Nil(t, store.Get(k2))
require.Equal(t, v2, cstore.Get(k2))
require.Nil(t, store.Get(k2))
write()
assert.Equal(t, v2, store.Get(k2))
require.Equal(t, v2, store.Get(k2))
}
func TestLogContext(t *testing.T) {
+49 -44
View File
@@ -3,9 +3,9 @@ package types
import (
"fmt"
cmn "github.com/tendermint/tmlibs/common"
cmn "github.com/tendermint/tendermint/libs/common"
abci "github.com/tendermint/abci/types"
abci "github.com/tendermint/tendermint/abci/types"
)
// ABCICodeType - combined codetype / codespace
@@ -147,49 +147,80 @@ func ErrMemoTooLarge(msg string) Error {
//----------------------------------------
// Error & sdkError
type cmnError = cmn.Error
// sdk Error type
type Error interface {
Error() string
// Implements cmn.Error
// Error() string
// Stacktrace() cmn.Error
// Trace(offset int, format string, args ...interface{}) cmn.Error
// Data() interface{}
cmnError
// convenience
TraceSDK(format string, args ...interface{}) Error
// set codespace
WithDefaultCodespace(CodespaceType) Error
Code() CodeType
Codespace() CodespaceType
ABCILog() string
ABCICode() ABCICodeType
WithDefaultCodespace(codespace CodespaceType) Error
Trace(msg string) Error
T() interface{}
Result() Result
QueryResult() abci.ResponseQuery
}
// NewError - create an error
func NewError(codespace CodespaceType, code CodeType, msg string) Error {
return newError(codespace, code, msg)
// NewError - create an error.
func NewError(codespace CodespaceType, code CodeType, format string, args ...interface{}) Error {
return newError(codespace, code, format, args...)
}
func newErrorWithRootCodespace(code CodeType, msg string) *sdkError {
return newError(CodespaceRoot, code, msg)
func newErrorWithRootCodespace(code CodeType, format string, args ...interface{}) *sdkError {
return newError(CodespaceRoot, code, format, args...)
}
func newError(codespace CodespaceType, code CodeType, msg string) *sdkError {
if msg == "" {
msg = CodeToDefaultMsg(code)
func newError(codespace CodespaceType, code CodeType, format string, args ...interface{}) *sdkError {
if format == "" {
format = CodeToDefaultMsg(code)
}
return &sdkError{
codespace: codespace,
code: code,
err: cmn.NewErrorWithT(code, msg),
cmnError: cmn.NewError(format, args...),
}
}
type sdkError struct {
codespace CodespaceType
code CodeType
err cmn.Error
cmnError
}
// Implements Error.
func (err *sdkError) WithDefaultCodespace(cs CodespaceType) Error {
codespace := err.codespace
if codespace == CodespaceUndefined {
codespace = cs
}
return &sdkError{
codespace: cs,
code: err.code,
cmnError: err.cmnError,
}
}
// Implements ABCIError.
func (err *sdkError) TraceSDK(format string, args ...interface{}) Error {
err.Trace(1, format, args...)
return err
}
// Implements ABCIError.
// Overrides err.Error.Error().
func (err *sdkError) Error() string {
return fmt.Sprintf("error{%d:%d,%#v}", err.codespace, err.code, err.err)
return fmt.Sprintf("Error{%d:%d,%#v}", err.codespace, err.code, err.cmnError)
}
// Implements ABCIError.
@@ -215,33 +246,7 @@ Code: %v
ABCICode: %v
Error: %#v
=== /ABCI Log ===
`, err.codespace, err.code, err.ABCICode(), err.err)
}
// Add tracing information with msg.
func (err *sdkError) Trace(msg string) Error {
return &sdkError{
codespace: err.codespace,
code: err.code,
err: err.err.Trace(msg),
}
}
// Implements Error.
func (err *sdkError) WithDefaultCodespace(cs CodespaceType) Error {
codespace := err.codespace
if codespace == CodespaceUndefined {
codespace = cs
}
return &sdkError{
codespace: codespace,
code: err.code,
err: err.err,
}
}
func (err *sdkError) T() interface{} {
return err.err.T()
`, err.codespace, err.code, err.ABCICode(), err.cmnError)
}
func (err *sdkError) Result() Result {
+5 -5
View File
@@ -4,7 +4,7 @@ import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var codeTypes = []CodeType{
@@ -32,11 +32,11 @@ var errFns = []errFn{
}
func TestCodeType(t *testing.T) {
assert.True(t, ABCICodeOK.IsOK())
require.True(t, ABCICodeOK.IsOK())
for _, c := range codeTypes {
msg := CodeToDefaultMsg(c)
assert.False(t, strings.HasPrefix(msg, "Unknown code"))
require.False(t, strings.HasPrefix(msg, "Unknown code"))
}
}
@@ -44,7 +44,7 @@ func TestErrFn(t *testing.T) {
for i, errFn := range errFns {
err := errFn("")
codeType := codeTypes[i]
assert.Equal(t, err.Code(), codeType)
assert.Equal(t, err.Result().Code, ToABCICode(CodespaceRoot, codeType))
require.Equal(t, err.Code(), codeType)
require.Equal(t, err.Result().Code, ToABCICode(CodespaceRoot, codeType))
}
}
+2 -1
View File
@@ -1,7 +1,8 @@
package types
// core function variable which application runs for transactions
// Handler defines the core of the state transition function of an application.
type Handler func(ctx Context, msg Msg) Result
// AnteHandler authenticates transactions, before their internal messages are handled.
// If newCtx.IsZero(), ctx is used instead.
type AnteHandler func(ctx Context, tx Tx) (newCtx Context, result Result, abort bool)
+20 -3
View File
@@ -1,6 +1,8 @@
package types
import (
"encoding/json"
"math/big"
)
@@ -46,14 +48,25 @@ func unmarshalAmino(i *big.Int, text string) (err error) {
return i.UnmarshalText([]byte(text))
}
// MarshalJSON for custom encodig scheme
// MarshalJSON for custom encoding scheme
// Must be encoded as a string for JSON precision
func marshalJSON(i *big.Int) ([]byte, error) {
return i.MarshalText()
text, err := i.MarshalText()
if err != nil {
return nil, err
}
return json.Marshal(string(text))
}
// UnmarshalJSON for custom decoding scheme
// Must be encoded as a string for JSON precision
func unmarshalJSON(i *big.Int, bz []byte) error {
return i.UnmarshalText(bz)
var text string
err := json.Unmarshal(bz, &text)
if err != nil {
return err
}
return i.UnmarshalText([]byte(text))
}
// Int wraps integer with 256 bit range bound
@@ -214,6 +227,10 @@ func (i Int) Neg() (res Int) {
return Int{neg(i.i)}
}
func (i Int) String() string {
return i.i.String()
}
// MarshalAmino defines custom encoding scheme
func (i Int) MarshalAmino() (string, error) {
if i.i == nil { // Necessary since default Uint initialization has i.i as nil
+52 -52
View File
@@ -5,107 +5,107 @@ import (
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFromInt64(t *testing.T) {
for n := 0; n < 20; n++ {
r := rand.Int63()
assert.Equal(t, r, NewInt(r).Int64())
require.Equal(t, r, NewInt(r).Int64())
}
}
func TestInt(t *testing.T) {
// Max Int = 2^255-1 = 5.789e+76
// Min Int = -(2^255-1) = -5.789e+76
assert.NotPanics(t, func() { NewIntWithDecimal(1, 76) })
require.NotPanics(t, func() { NewIntWithDecimal(1, 76) })
i1 := NewIntWithDecimal(1, 76)
assert.NotPanics(t, func() { NewIntWithDecimal(2, 76) })
require.NotPanics(t, func() { NewIntWithDecimal(2, 76) })
i2 := NewIntWithDecimal(2, 76)
assert.NotPanics(t, func() { NewIntWithDecimal(3, 76) })
require.NotPanics(t, func() { NewIntWithDecimal(3, 76) })
i3 := NewIntWithDecimal(3, 76)
assert.Panics(t, func() { NewIntWithDecimal(6, 76) })
assert.Panics(t, func() { NewIntWithDecimal(9, 80) })
require.Panics(t, func() { NewIntWithDecimal(6, 76) })
require.Panics(t, func() { NewIntWithDecimal(9, 80) })
// Overflow check
assert.NotPanics(t, func() { i1.Add(i1) })
assert.NotPanics(t, func() { i2.Add(i2) })
assert.Panics(t, func() { i3.Add(i3) })
require.NotPanics(t, func() { i1.Add(i1) })
require.NotPanics(t, func() { i2.Add(i2) })
require.Panics(t, func() { i3.Add(i3) })
assert.NotPanics(t, func() { i1.Sub(i1.Neg()) })
assert.NotPanics(t, func() { i2.Sub(i2.Neg()) })
assert.Panics(t, func() { i3.Sub(i3.Neg()) })
require.NotPanics(t, func() { i1.Sub(i1.Neg()) })
require.NotPanics(t, func() { i2.Sub(i2.Neg()) })
require.Panics(t, func() { i3.Sub(i3.Neg()) })
assert.Panics(t, func() { i1.Mul(i1) })
assert.Panics(t, func() { i2.Mul(i2) })
assert.Panics(t, func() { i3.Mul(i3) })
require.Panics(t, func() { i1.Mul(i1) })
require.Panics(t, func() { i2.Mul(i2) })
require.Panics(t, func() { i3.Mul(i3) })
assert.Panics(t, func() { i1.Neg().Mul(i1.Neg()) })
assert.Panics(t, func() { i2.Neg().Mul(i2.Neg()) })
assert.Panics(t, func() { i3.Neg().Mul(i3.Neg()) })
require.Panics(t, func() { i1.Neg().Mul(i1.Neg()) })
require.Panics(t, func() { i2.Neg().Mul(i2.Neg()) })
require.Panics(t, func() { i3.Neg().Mul(i3.Neg()) })
// Underflow check
i3n := i3.Neg()
assert.NotPanics(t, func() { i3n.Sub(i1) })
assert.NotPanics(t, func() { i3n.Sub(i2) })
assert.Panics(t, func() { i3n.Sub(i3) })
require.NotPanics(t, func() { i3n.Sub(i1) })
require.NotPanics(t, func() { i3n.Sub(i2) })
require.Panics(t, func() { i3n.Sub(i3) })
assert.NotPanics(t, func() { i3n.Add(i1.Neg()) })
assert.NotPanics(t, func() { i3n.Add(i2.Neg()) })
assert.Panics(t, func() { i3n.Add(i3.Neg()) })
require.NotPanics(t, func() { i3n.Add(i1.Neg()) })
require.NotPanics(t, func() { i3n.Add(i2.Neg()) })
require.Panics(t, func() { i3n.Add(i3.Neg()) })
assert.Panics(t, func() { i1.Mul(i1.Neg()) })
assert.Panics(t, func() { i2.Mul(i2.Neg()) })
assert.Panics(t, func() { i3.Mul(i3.Neg()) })
require.Panics(t, func() { i1.Mul(i1.Neg()) })
require.Panics(t, func() { i2.Mul(i2.Neg()) })
require.Panics(t, func() { i3.Mul(i3.Neg()) })
// Bound check
intmax := NewIntFromBigInt(new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(255), nil), big.NewInt(1)))
intmin := intmax.Neg()
assert.NotPanics(t, func() { intmax.Add(ZeroInt()) })
assert.NotPanics(t, func() { intmin.Sub(ZeroInt()) })
assert.Panics(t, func() { intmax.Add(OneInt()) })
assert.Panics(t, func() { intmin.Sub(OneInt()) })
require.NotPanics(t, func() { intmax.Add(ZeroInt()) })
require.NotPanics(t, func() { intmin.Sub(ZeroInt()) })
require.Panics(t, func() { intmax.Add(OneInt()) })
require.Panics(t, func() { intmin.Sub(OneInt()) })
// Division-by-zero check
assert.Panics(t, func() { i1.Div(NewInt(0)) })
require.Panics(t, func() { i1.Div(NewInt(0)) })
}
func TestUint(t *testing.T) {
// Max Uint = 1.15e+77
// Min Uint = 0
assert.NotPanics(t, func() { NewUintWithDecimal(5, 76) })
require.NotPanics(t, func() { NewUintWithDecimal(5, 76) })
i1 := NewUintWithDecimal(5, 76)
assert.NotPanics(t, func() { NewUintWithDecimal(10, 76) })
require.NotPanics(t, func() { NewUintWithDecimal(10, 76) })
i2 := NewUintWithDecimal(10, 76)
assert.NotPanics(t, func() { NewUintWithDecimal(11, 76) })
require.NotPanics(t, func() { NewUintWithDecimal(11, 76) })
i3 := NewUintWithDecimal(11, 76)
assert.Panics(t, func() { NewUintWithDecimal(12, 76) })
assert.Panics(t, func() { NewUintWithDecimal(1, 80) })
require.Panics(t, func() { NewUintWithDecimal(12, 76) })
require.Panics(t, func() { NewUintWithDecimal(1, 80) })
// Overflow check
assert.NotPanics(t, func() { i1.Add(i1) })
assert.Panics(t, func() { i2.Add(i2) })
assert.Panics(t, func() { i3.Add(i3) })
require.NotPanics(t, func() { i1.Add(i1) })
require.Panics(t, func() { i2.Add(i2) })
require.Panics(t, func() { i3.Add(i3) })
assert.Panics(t, func() { i1.Mul(i1) })
assert.Panics(t, func() { i2.Mul(i2) })
assert.Panics(t, func() { i3.Mul(i3) })
require.Panics(t, func() { i1.Mul(i1) })
require.Panics(t, func() { i2.Mul(i2) })
require.Panics(t, func() { i3.Mul(i3) })
// Underflow check
assert.NotPanics(t, func() { i2.Sub(i1) })
assert.NotPanics(t, func() { i2.Sub(i2) })
assert.Panics(t, func() { i2.Sub(i3) })
require.NotPanics(t, func() { i2.Sub(i1) })
require.NotPanics(t, func() { i2.Sub(i2) })
require.Panics(t, func() { i2.Sub(i3) })
// Bound check
uintmax := NewUintFromBigInt(new(big.Int).Sub(new(big.Int).Exp(big.NewInt(2), big.NewInt(256), nil), big.NewInt(1)))
uintmin := NewUint(0)
assert.NotPanics(t, func() { uintmax.Add(ZeroUint()) })
assert.NotPanics(t, func() { uintmin.Sub(ZeroUint()) })
assert.Panics(t, func() { uintmax.Add(OneUint()) })
assert.Panics(t, func() { uintmin.Sub(OneUint()) })
require.NotPanics(t, func() { uintmax.Add(ZeroUint()) })
require.NotPanics(t, func() { uintmin.Sub(ZeroUint()) })
require.Panics(t, func() { uintmax.Add(OneUint()) })
require.Panics(t, func() { uintmin.Sub(OneUint()) })
// Division-by-zero check
assert.Panics(t, func() { i1.Div(uintmin) })
require.Panics(t, func() { i1.Div(uintmin) })
}
+5 -1
View File
@@ -233,7 +233,11 @@ func (m Linear) Flush(ptr interface{}, fn func() bool) {
var i uint64
for i = top; i < length; i++ {
m.Get(i, ptr)
err := m.Get(i, ptr)
if err != nil {
// TODO: Handle with #870
panic(err)
}
m.Delete(i)
if fn() {
break
+24 -24
View File
@@ -4,12 +4,12 @@ import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
dbm "github.com/tendermint/tmlibs/db"
"github.com/tendermint/tmlibs/log"
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
abci "github.com/tendermint/abci/types"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/store"
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -41,30 +41,30 @@ func TestList(t *testing.T) {
var res S
lm.Push(val)
assert.Equal(t, uint64(1), lm.Len())
require.Equal(t, uint64(1), lm.Len())
lm.Get(uint64(0), &res)
assert.Equal(t, val, res)
require.Equal(t, val, res)
val = S{2, false}
lm.Set(uint64(0), val)
lm.Get(uint64(0), &res)
assert.Equal(t, val, res)
require.Equal(t, val, res)
val = S{100, false}
lm.Push(val)
assert.Equal(t, uint64(2), lm.Len())
require.Equal(t, uint64(2), lm.Len())
lm.Get(uint64(1), &res)
assert.Equal(t, val, res)
require.Equal(t, val, res)
lm.Delete(uint64(1))
assert.Equal(t, uint64(2), lm.Len())
require.Equal(t, uint64(2), lm.Len())
lm.Iterate(&res, func(index uint64) (brk bool) {
var temp S
lm.Get(index, &temp)
assert.Equal(t, temp, res)
require.Equal(t, temp, res)
assert.True(t, index != 1)
require.True(t, index != 1)
return
})
@@ -74,7 +74,7 @@ func TestList(t *testing.T) {
})
lm.Get(uint64(0), &res)
assert.Equal(t, S{3, true}, res)
require.Equal(t, S{3, true}, res)
}
func TestQueue(t *testing.T) {
@@ -89,13 +89,13 @@ func TestQueue(t *testing.T) {
qm.Push(val)
qm.Peek(&res)
assert.Equal(t, val, res)
require.Equal(t, val, res)
qm.Pop()
empty := qm.IsEmpty()
assert.True(t, empty)
assert.NotNil(t, qm.Peek(&res))
require.True(t, empty)
require.NotNil(t, qm.Peek(&res))
qm.Push(S{1, true})
qm.Push(S{2, true})
@@ -107,10 +107,10 @@ func TestQueue(t *testing.T) {
return
})
assert.False(t, qm.IsEmpty())
require.False(t, qm.IsEmpty())
qm.Pop()
assert.True(t, qm.IsEmpty())
require.True(t, qm.IsEmpty())
}
func TestOptions(t *testing.T) {
@@ -136,22 +136,22 @@ func TestOptions(t *testing.T) {
// Checking keys.LengthKey
err := cdc.UnmarshalBinary(store.Get(keys.LengthKey), &len)
assert.Nil(t, err)
assert.Equal(t, len, linear.Len())
require.Nil(t, err)
require.Equal(t, len, linear.Len())
// Checking keys.ElemKey
for i := 0; i < 10; i++ {
linear.Get(uint64(i), &expected)
bz := store.Get(append(keys.ElemKey, []byte(fmt.Sprintf("%020d", i))...))
err = cdc.UnmarshalBinary(bz, &actual)
assert.Nil(t, err)
assert.Equal(t, expected, actual)
require.Nil(t, err)
require.Equal(t, expected, actual)
}
linear.Pop()
err = cdc.UnmarshalBinary(store.Get(keys.TopKey), &top)
assert.Nil(t, err)
assert.Equal(t, top, linear.getTop())
require.Nil(t, err)
require.Equal(t, top, linear.getTop())
}
+27 -12
View File
@@ -38,8 +38,8 @@ func NewRat(Numerator int64, Denominator ...int64) Rat {
}
// create a rational from decimal string or integer string
func NewRatFromDecimal(decimalStr string) (f Rat, err Error) {
// precision is the number of values after the decimal point which should be read
func NewRatFromDecimal(decimalStr string, prec int) (f Rat, err Error) {
// first extract any negative symbol
neg := false
if string(decimalStr[0]) == "-" {
@@ -61,6 +61,9 @@ func NewRatFromDecimal(decimalStr string) (f Rat, err Error) {
if len(str[0]) == 0 || len(str[1]) == 0 {
return f, ErrUnknownRequest("not a decimal string")
}
if len(str[1]) > prec {
return f, ErrUnknownRequest("string has too many decimals")
}
numStr = str[0] + str[1]
len := int64(len(str[1]))
denom = new(big.Int).Exp(big.NewInt(10), big.NewInt(len), nil).Int64()
@@ -69,8 +72,20 @@ func NewRatFromDecimal(decimalStr string) (f Rat, err Error) {
}
num, errConv := strconv.Atoi(numStr)
if errConv != nil {
return f, ErrUnknownRequest(errConv.Error())
if errConv != nil && strings.HasSuffix(errConv.Error(), "value out of range") {
// resort to big int, don't make this default option for efficiency
numBig, success := new(big.Int).SetString(numStr, 10)
if success != true {
return f, ErrUnknownRequest("not a decimal string")
}
if neg {
numBig.Neg(numBig)
}
return NewRatFromBigInt(numBig, big.NewInt(denom)), nil
} else if errConv != nil {
return f, ErrUnknownRequest("not a decimal string")
}
if neg {
@@ -105,9 +120,9 @@ func NewRatFromInt(num Int, denom ...Int) Rat {
}
//nolint
func (r Rat) Num() int64 { return r.Rat.Num().Int64() } // Num - return the numerator
func (r Rat) Denom() int64 { return r.Rat.Denom().Int64() } // Denom - return the denominator
func (r Rat) IsZero() bool { return r.Num() == 0 } // IsZero - Is the Rat equal to zero
func (r Rat) Num() Int { return Int{r.Rat.Num()} } // Num - return the numerator
func (r Rat) Denom() Int { return Int{r.Rat.Denom()} } // Denom - return the denominator
func (r Rat) IsZero() bool { return r.Num().IsZero() } // IsZero - Is the Rat equal to zero
func (r Rat) Equal(r2 Rat) bool { return (r.Rat).Cmp(r2.Rat) == 0 }
func (r Rat) GT(r2 Rat) bool { return (r.Rat).Cmp(r2.Rat) == 1 } // greater than
func (r Rat) GTE(r2 Rat) bool { return !r.LT(r2) } // greater than or equal
@@ -159,20 +174,20 @@ func (r Rat) EvaluateBig() *big.Int {
return d
}
// evaluate the rational using bankers rounding
func (r Rat) Evaluate() int64 {
// RoundInt64 rounds the rational using bankers rounding
func (r Rat) RoundInt64() int64 {
return r.EvaluateBig().Int64()
}
// EvaulateInt evaludates the rational using EvaluateBig
func (r Rat) EvaluateInt() Int {
// RoundInt round the rational using bankers rounding
func (r Rat) RoundInt() Int {
return NewIntFromBigInt(r.EvaluateBig())
}
// round Rat with the provided precisionFactor
func (r Rat) Round(precisionFactor int64) Rat {
rTen := Rat{new(big.Rat).Mul(r.Rat, big.NewRat(precisionFactor, 1))}
return Rat{big.NewRat(rTen.Evaluate(), precisionFactor)}
return Rat{big.NewRat(rTen.RoundInt64(), precisionFactor)}
}
// TODO panic if negative or if totalDigits < len(initStr)???
+50 -44
View File
@@ -5,22 +5,23 @@ import (
"testing"
wire "github.com/cosmos/cosmos-sdk/wire"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNew(t *testing.T) {
assert.Equal(t, NewRat(1), NewRat(1, 1))
assert.Equal(t, NewRat(100), NewRat(100, 1))
assert.Equal(t, NewRat(-1), NewRat(-1, 1))
assert.Equal(t, NewRat(-100), NewRat(-100, 1))
assert.Equal(t, NewRat(0), NewRat(0, 1))
require.Equal(t, NewRat(1), NewRat(1, 1))
require.Equal(t, NewRat(100), NewRat(100, 1))
require.Equal(t, NewRat(-1), NewRat(-1, 1))
require.Equal(t, NewRat(-100), NewRat(-100, 1))
require.Equal(t, NewRat(0), NewRat(0, 1))
// do not allow for more than 2 variables
assert.Panics(t, func() { NewRat(1, 1, 1) })
require.Panics(t, func() { NewRat(1, 1, 1) })
}
func TestNewFromDecimal(t *testing.T) {
largeBigInt, success := new(big.Int).SetString("3109736052979742687701388262607869", 10)
require.True(t, success)
tests := []struct {
decimalStr string
expErr bool
@@ -31,7 +32,13 @@ func TestNewFromDecimal(t *testing.T) {
{"1.1", false, NewRat(11, 10)},
{"0.75", false, NewRat(3, 4)},
{"0.8", false, NewRat(4, 5)},
{"0.11111", false, NewRat(11111, 100000)},
{"0.11111", true, NewRat(1111, 10000)},
{"628240629832763.5738930323617075341", true, NewRat(3141203149163817869, 5000)},
{"621947210595948537540277652521.5738930323617075341",
true, NewRatFromBigInt(largeBigInt, big.NewInt(5000))},
{"628240629832763.5738", false, NewRat(3141203149163817869, 5000)},
{"621947210595948537540277652521.5738",
false, NewRatFromBigInt(largeBigInt, big.NewInt(5000))},
{".", true, Rat{}},
{".0", true, Rat{}},
{"1.", true, Rat{}},
@@ -41,22 +48,21 @@ func TestNewFromDecimal(t *testing.T) {
}
for _, tc := range tests {
res, err := NewRatFromDecimal(tc.decimalStr)
res, err := NewRatFromDecimal(tc.decimalStr, 4)
if tc.expErr {
assert.NotNil(t, err, tc.decimalStr)
require.NotNil(t, err, tc.decimalStr)
} else {
assert.Nil(t, err)
assert.True(t, res.Equal(tc.exp))
require.Nil(t, err, tc.decimalStr)
require.True(t, res.Equal(tc.exp), tc.decimalStr)
}
// negative tc
res, err = NewRatFromDecimal("-" + tc.decimalStr)
res, err = NewRatFromDecimal("-"+tc.decimalStr, 4)
if tc.expErr {
assert.NotNil(t, err, tc.decimalStr)
require.NotNil(t, err, tc.decimalStr)
} else {
assert.Nil(t, err)
assert.True(t, res.Equal(tc.exp.Mul(NewRat(-1))))
require.Nil(t, err, tc.decimalStr)
require.True(t, res.Equal(tc.exp.Mul(NewRat(-1))), tc.decimalStr)
}
}
}
@@ -93,9 +99,9 @@ func TestEqualities(t *testing.T) {
}
for _, tc := range tests {
assert.Equal(t, tc.gt, tc.r1.GT(tc.r2))
assert.Equal(t, tc.lt, tc.r1.LT(tc.r2))
assert.Equal(t, tc.eq, tc.r1.Equal(tc.r2))
require.Equal(t, tc.gt, tc.r1.GT(tc.r2))
require.Equal(t, tc.lt, tc.r1.LT(tc.r2))
require.Equal(t, tc.eq, tc.r1.Equal(tc.r2))
}
}
@@ -129,14 +135,14 @@ func TestArithmetic(t *testing.T) {
}
for _, tc := range tests {
assert.True(t, tc.resMul.Equal(tc.r1.Mul(tc.r2)), "r1 %v, r2 %v", tc.r1.Rat, tc.r2.Rat)
assert.True(t, tc.resAdd.Equal(tc.r1.Add(tc.r2)), "r1 %v, r2 %v", tc.r1.Rat, tc.r2.Rat)
assert.True(t, tc.resSub.Equal(tc.r1.Sub(tc.r2)), "r1 %v, r2 %v", tc.r1.Rat, tc.r2.Rat)
require.True(t, tc.resMul.Equal(tc.r1.Mul(tc.r2)), "r1 %v, r2 %v", tc.r1.Rat, tc.r2.Rat)
require.True(t, tc.resAdd.Equal(tc.r1.Add(tc.r2)), "r1 %v, r2 %v", tc.r1.Rat, tc.r2.Rat)
require.True(t, tc.resSub.Equal(tc.r1.Sub(tc.r2)), "r1 %v, r2 %v", tc.r1.Rat, tc.r2.Rat)
if tc.r2.Num() == 0 { // panic for divide by zero
assert.Panics(t, func() { tc.r1.Quo(tc.r2) })
if tc.r2.Num().IsZero() { // panic for divide by zero
require.Panics(t, func() { tc.r1.Quo(tc.r2) })
} else {
assert.True(t, tc.resDiv.Equal(tc.r1.Quo(tc.r2)), "r1 %v, r2 %v", tc.r1.Rat, tc.r2.Rat)
require.True(t, tc.resDiv.Equal(tc.r1.Quo(tc.r2)), "r1 %v, r2 %v", tc.r1.Rat, tc.r2.Rat)
}
}
}
@@ -162,8 +168,8 @@ func TestEvaluate(t *testing.T) {
}
for _, tc := range tests {
assert.Equal(t, tc.res, tc.r1.Evaluate(), "%v", tc.r1)
assert.Equal(t, tc.res*-1, tc.r1.Mul(NewRat(-1)).Evaluate(), "%v", tc.r1.Mul(NewRat(-1)))
require.Equal(t, tc.res, tc.r1.RoundInt64(), "%v", tc.r1)
require.Equal(t, tc.res*-1, tc.r1.Mul(NewRat(-1)).RoundInt64(), "%v", tc.r1.Mul(NewRat(-1)))
}
}
@@ -186,9 +192,9 @@ func TestRound(t *testing.T) {
}
for _, tc := range tests {
assert.Equal(t, tc.res, tc.r.Round(tc.precFactor), "%v", tc.r)
require.Equal(t, tc.res, tc.r.Round(tc.precFactor), "%v", tc.r)
negR1, negRes := tc.r.Mul(NewRat(-1)), tc.res.Mul(NewRat(-1))
assert.Equal(t, negRes, negR1.Round(tc.precFactor), "%v", negR1)
require.Equal(t, negRes, negR1.Round(tc.precFactor), "%v", negR1)
}
}
@@ -205,7 +211,7 @@ func TestToLeftPadded(t *testing.T) {
{NewRat(1000, 3), 12, "000000000333"},
}
for _, tc := range tests {
assert.Equal(t, tc.res, tc.rat.ToLeftPadded(tc.digits))
require.Equal(t, tc.res, tc.rat.ToLeftPadded(tc.digits))
}
}
@@ -214,13 +220,13 @@ var cdc = wire.NewCodec() //var jsonCdc JSONCodec // TODO wire.Codec
func TestZeroSerializationJSON(t *testing.T) {
r := NewRat(0, 1)
err := cdc.UnmarshalJSON([]byte(`"0/1"`), &r)
assert.Nil(t, err)
require.Nil(t, err)
err = cdc.UnmarshalJSON([]byte(`"0/0"`), &r)
assert.NotNil(t, err)
require.NotNil(t, err)
err = cdc.UnmarshalJSON([]byte(`"1/0"`), &r)
assert.NotNil(t, err)
require.NotNil(t, err)
err = cdc.UnmarshalJSON([]byte(`"{}"`), &r)
assert.NotNil(t, err)
require.NotNil(t, err)
}
func TestSerializationText(t *testing.T) {
@@ -232,7 +238,7 @@ func TestSerializationText(t *testing.T) {
var r2 = Rat{new(big.Rat)}
err = r2.UnmarshalText(bz)
require.NoError(t, err)
assert.True(t, r.Equal(r2), "original: %v, unmarshalled: %v", r, r2)
require.True(t, r.Equal(r2), "original: %v, unmarshalled: %v", r, r2)
}
func TestSerializationGoWireJSON(t *testing.T) {
@@ -243,7 +249,7 @@ func TestSerializationGoWireJSON(t *testing.T) {
var r2 Rat
err = cdc.UnmarshalJSON(bz, &r2)
require.NoError(t, err)
assert.True(t, r.Equal(r2), "original: %v, unmarshalled: %v", r, r2)
require.True(t, r.Equal(r2), "original: %v, unmarshalled: %v", r, r2)
}
func TestSerializationGoWireBinary(t *testing.T) {
@@ -254,7 +260,7 @@ func TestSerializationGoWireBinary(t *testing.T) {
var r2 Rat
err = cdc.UnmarshalBinary(bz, &r2)
require.NoError(t, err)
assert.True(t, r.Equal(r2), "original: %v, unmarshalled: %v", r, r2)
require.True(t, r.Equal(r2), "original: %v, unmarshalled: %v", r, r2)
}
type testEmbedStruct struct {
@@ -272,9 +278,9 @@ func TestEmbeddedStructSerializationGoWire(t *testing.T) {
err = cdc.UnmarshalJSON(bz, &obj2)
require.Nil(t, err)
assert.Equal(t, obj.Field1, obj2.Field1)
assert.Equal(t, obj.Field2, obj2.Field2)
assert.True(t, obj.Field3.Equal(obj2.Field3), "original: %v, unmarshalled: %v", obj, obj2)
require.Equal(t, obj.Field1, obj2.Field1)
require.Equal(t, obj.Field2, obj2.Field2)
require.True(t, obj.Field3.Equal(obj2.Field3), "original: %v, unmarshalled: %v", obj, obj2)
}
func TestRatsEqual(t *testing.T) {
@@ -292,8 +298,8 @@ func TestRatsEqual(t *testing.T) {
}
for _, tc := range tests {
assert.Equal(t, tc.eq, RatsEqual(tc.r1s, tc.r2s))
assert.Equal(t, tc.eq, RatsEqual(tc.r2s, tc.r1s))
require.Equal(t, tc.eq, RatsEqual(tc.r1s, tc.r2s))
require.Equal(t, tc.eq, RatsEqual(tc.r2s, tc.r1s))
}
}
@@ -303,7 +309,7 @@ func TestStringOverflow(t *testing.T) {
rat1 := NewRat(5164315003622678713, 4389711697696177267)
rat2 := NewRat(-3179849666053572961, 8459429845579852627)
rat3 := rat1.Add(rat2)
assert.Equal(t,
require.Equal(t,
"29728537197630860939575850336935951464/37134458148982045574552091851127630409",
rat3.String(),
)
+8 -6
View File
@@ -1,8 +1,8 @@
package types
import (
abci "github.com/tendermint/abci/types"
"github.com/tendermint/go-crypto"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
tmtypes "github.com/tendermint/tendermint/types"
)
@@ -32,6 +32,7 @@ func BondStatusToString(b BondStatus) string {
// validator for a delegated proof of stake system
type Validator interface {
GetRevoked() bool // whether the validator is revoked
GetMoniker() string // moniker of the validator
GetStatus() BondStatus // status of the validator
GetOwner() Address // owner address to receive/return validators coins
@@ -45,7 +46,7 @@ type Validator interface {
func ABCIValidator(v Validator) abci.Validator {
return abci.Validator{
PubKey: tmtypes.TM2PB.PubKey(v.GetPubKey()),
Power: v.GetPower().Evaluate(),
Power: v.GetPower().RoundInt64(),
}
}
@@ -62,9 +63,10 @@ type ValidatorSet interface {
Validator(Context, Address) Validator // get a particular validator by owner address
TotalPower(Context) Rat // total power of the validator set
Slash(Context, crypto.PubKey, int64, Rat) // slash the validator and delegators of the validator, specifying offence height & slash fraction
Revoke(Context, crypto.PubKey) // revoke a validator
Unrevoke(Context, crypto.PubKey) // unrevoke a validator
// slash the validator and delegators of the validator, specifying offence height, offence power, and slash fraction
Slash(Context, crypto.PubKey, int64, int64, Rat)
Revoke(Context, crypto.PubKey) // revoke a validator
Unrevoke(Context, crypto.PubKey) // unrevoke a validator
}
//_______________________________________________________________________________
+3 -3
View File
@@ -3,9 +3,9 @@ package types
import (
"fmt"
abci "github.com/tendermint/abci/types"
cmn "github.com/tendermint/tmlibs/common"
dbm "github.com/tendermint/tmlibs/db"
abci "github.com/tendermint/tendermint/abci/types"
cmn "github.com/tendermint/tendermint/libs/common"
dbm "github.com/tendermint/tendermint/libs/db"
)
// NOTE: These are implemented in cosmos-sdk/store.
+2 -4
View File
@@ -3,12 +3,10 @@ package types
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPrefixEndBytes(t *testing.T) {
assert := assert.New(t)
var testCases = []struct {
prefix []byte
expected []byte
@@ -24,6 +22,6 @@ func TestPrefixEndBytes(t *testing.T) {
for _, test := range testCases {
end := PrefixEndBytes(test.prefix)
assert.Equal(test.expected, end)
require.Equal(t, test.expected, end)
}
}
+1 -1
View File
@@ -1,7 +1,7 @@
package types
import (
cmn "github.com/tendermint/tmlibs/common"
cmn "github.com/tendermint/tendermint/libs/common"
)
// Type synonym for convenience
+3 -3
View File
@@ -11,13 +11,13 @@ type Msg interface {
// Must be alphanumeric or empty.
Type() string
// Get the canonical byte representation of the Msg.
GetSignBytes() []byte
// ValidateBasic does a simple validation check that
// doesn't require access to any other information.
ValidateBasic() Error
// Get the canonical byte representation of the Msg.
GetSignBytes() []byte
// Signers returns the addrs of signers that must sign.
// CONTRACT: All signatures must be present to be valid.
// CONTRACT: Returns addrs in some deterministic order.