Skip to content

Commit

Permalink
Merge pull request hyperledger-archives#499 from benjaminbollen/relea…
Browse files Browse the repository at this point in the history
…se-0.16-remove-tendermint-common

Cleanup: introduce word256 and remove dependency on tendermint/go-common
  • Loading branch information
silasdavis authored Feb 16, 2017
2 parents 339711b + 3bcc73b commit 9112e3a
Show file tree
Hide file tree
Showing 34 changed files with 639 additions and 154 deletions.
5 changes: 3 additions & 2 deletions account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import (
"fmt"
"io"

"github.com/eris-ltd/eris-db/common/sanity"
ptypes "github.com/eris-ltd/eris-db/permission/types"
. "github.com/tendermint/go-common"

"github.com/tendermint/go-crypto"
"github.com/tendermint/go-wire"
)
Expand All @@ -42,7 +43,7 @@ func SignBytes(chainID string, o Signable) []byte {
buf, n, err := new(bytes.Buffer), new(int), new(error)
o.WriteSignBytes(chainID, buf, n, err)
if *err != nil {
PanicCrisis(err)
sanity.PanicCrisis(err)
}

return buf.Bytes()
Expand Down
9 changes: 6 additions & 3 deletions account/priv_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@
package account

import (
"fmt"

"github.com/eris-ltd/eris-db/common/sanity"

"github.com/tendermint/ed25519"
. "github.com/tendermint/go-common"
"github.com/tendermint/go-crypto"
"github.com/tendermint/go-wire"
)
Expand All @@ -49,7 +52,7 @@ func (pA *PrivAccount) Sign(chainID string, o Signable) crypto.Signature {
}

func (pA *PrivAccount) String() string {
return Fmt("PrivAccount{%X}", pA.Address)
return fmt.Sprintf("PrivAccount{%X}", pA.Address)
}

//----------------------------------------
Expand Down Expand Up @@ -90,7 +93,7 @@ func GenPrivAccountFromSecret(secret string) *PrivAccount {

func GenPrivAccountFromPrivKeyBytes(privKeyBytes []byte) *PrivAccount {
if len(privKeyBytes) != 64 {
PanicSanity(Fmt("Expected 64 bytes but got %v", len(privKeyBytes)))
sanity.PanicSanity(fmt.Sprintf("Expected 64 bytes but got %v", len(privKeyBytes)))
}
var privKeyArray [64]byte
copy(privKeyArray[:], privKeyBytes)
Expand Down
3 changes: 2 additions & 1 deletion client/cmd/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package commands
import (
"fmt"

"github.com/eris-ltd/eris-db/common/sanity"
"github.com/eris-ltd/eris-db/genesis"

"github.com/spf13/cobra"
Expand All @@ -23,7 +24,7 @@ var GenesisGenCmd = &cobra.Command{
// TODO refactor to not panic
genesisFile, err := genesis.GenerateKnown(args[0], AccountsPathFlag, ValidatorsPathFlag)
if err != nil {
panic(err)
sanity.PanicSanity(err)
}
fmt.Println(genesisFile) // may want to save somewhere instead
},
Expand Down
4 changes: 2 additions & 2 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ import (
"path"
"syscall"

"github.com/spf13/cobra"

"github.com/eris-ltd/eris-db/core"
"github.com/eris-ltd/eris-db/definitions"
"github.com/eris-ltd/eris-db/logging"
"github.com/eris-ltd/eris-db/logging/lifecycle"
"github.com/eris-ltd/eris-db/util"

"github.com/spf13/cobra"
)

const (
Expand Down
155 changes: 155 additions & 0 deletions common/random/random.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
// Copyright 2015-2017 Monax Industries Limited.
// This file is part of the Monax platform (Monax)

// Monax is free software: you can use, redistribute it and/or modify
// it only under the terms of the GNU General Public License, version
// 3, as published by the Free Software Foundation.

// Monax is distributed WITHOUT ANY WARRANTY pursuant to
// the terms of the Gnu General Public Licence, version 3, including
// (but not limited to) Clause 15 thereof. See the text of the
// GNU General Public License, version 3 for full terms.

// You should have received a copy of the GNU General Public License,
// version 3, with Monax. If not, see <http://www.gnu.org/licenses/>.

package random

import (
crand "crypto/rand"
"math/rand"
"time"

"github.com/eris-ltd/eris-db/common/sanity"
)

const (
strChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" // 62 characters
)

func init() {
b := cRandBytes(8)
var seed uint64
for i := 0; i < 8; i++ {
seed |= uint64(b[i])
seed <<= 8
}
rand.Seed(int64(seed))
}

// Constructs an alphanumeric string of given length.
func RandStr(length int) string {
chars := []byte{}
MAIN_LOOP:
for {
val := rand.Int63()
for i := 0; i < 10; i++ {
v := int(val & 0x3f) // rightmost 6 bits
if v >= 62 { // only 62 characters in strChars
val >>= 6
continue
} else {
chars = append(chars, strChars[v])
if len(chars) == length {
break MAIN_LOOP
}
val >>= 6
}
}
}

return string(chars)
}

func RandUint16() uint16 {
return uint16(rand.Uint32() & (1<<16 - 1))
}

func RandUint32() uint32 {
return rand.Uint32()
}

func RandUint64() uint64 {
return uint64(rand.Uint32())<<32 + uint64(rand.Uint32())
}

func RandUint() uint {
return uint(rand.Int())
}

func RandInt16() int16 {
return int16(rand.Uint32() & (1<<16 - 1))
}

func RandInt32() int32 {
return int32(rand.Uint32())
}

func RandInt64() int64 {
return int64(rand.Uint32())<<32 + int64(rand.Uint32())
}

func RandInt() int {
return rand.Int()
}

// Distributed pseudo-exponentially to test for various cases
func RandUint16Exp() uint16 {
bits := rand.Uint32() % 16
if bits == 0 {
return 0
}
n := uint16(1 << (bits - 1))
n += uint16(rand.Int31()) & ((1 << (bits - 1)) - 1)
return n
}

// Distributed pseudo-exponentially to test for various cases
func RandUint32Exp() uint32 {
bits := rand.Uint32() % 32
if bits == 0 {
return 0
}
n := uint32(1 << (bits - 1))
n += uint32(rand.Int31()) & ((1 << (bits - 1)) - 1)
return n
}

// Distributed pseudo-exponentially to test for various cases
func RandUint64Exp() uint64 {
bits := rand.Uint32() % 64
if bits == 0 {
return 0
}
n := uint64(1 << (bits - 1))
n += uint64(rand.Int63()) & ((1 << (bits - 1)) - 1)
return n
}

func RandFloat32() float32 {
return rand.Float32()
}

func RandTime() time.Time {
return time.Unix(int64(RandUint64Exp()), 0)
}

func RandBytes(n int) []byte {
bs := make([]byte, n)
for i := 0; i < n; i++ {
bs[i] = byte(rand.Intn(256))
}
return bs
}

// NOTE: This relies on the os's random number generator.
// For real security, we should salt that with some seed.
// See github.com/tendermint/go-crypto for a more secure reader.
func cRandBytes(numBytes int) []byte {
b := make([]byte, numBytes)
_, err := crand.Read(b)
if err != nil {
sanity.PanicCrisis(err)
}
return b
}
49 changes: 49 additions & 0 deletions common/sanity/sanity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2015-2017 Monax Industries Limited.
// This file is part of the Monax platform (Monax)

// Monax is free software: you can use, redistribute it and/or modify
// it only under the terms of the GNU General Public License, version
// 3, as published by the Free Software Foundation.

// Monax is distributed WITHOUT ANY WARRANTY pursuant to
// the terms of the Gnu General Public Licence, version 3, including
// (but not limited to) Clause 15 thereof. See the text of the
// GNU General Public License, version 3 for full terms.

// You should have received a copy of the GNU General Public License,
// version 3, with Monax. If not, see <http://www.gnu.org/licenses/>.

package sanity

import (
"fmt"
)

//--------------------------------------------------------------------------------------------------
// panic wrappers
// NOTE: [ben] Fail fast and fail hard, these are wrappers that point to code that needs
// to be addressed, but simplify finding them in the code;

// A panic resulting from a sanity check means there is a programmer error
// and some gaurantee is not satisfied.
func PanicSanity(v interface{}) {
panic(fmt.Sprintf("Paniced on a Sanity Check: %v", v))
}

// A panic here means something has gone horribly wrong, in the form of data corruption or
// failure of the operating system. In a correct/healthy system, these should never fire.
// If they do, it's indicative of a much more serious problem.
func PanicCrisis(v interface{}) {
panic(fmt.Sprintf("Paniced on a Crisis: %v", v))
}

// Indicates a failure of consensus. Someone was malicious or something has
// gone horribly wrong. These should really boot us into an "emergency-recover" mode
func PanicConsensus(v interface{}) {
panic(fmt.Sprintf("Paniced on a Consensus Failure: %v", v))
}

// For those times when we're not sure if we should panic
func PanicQ(v interface{}) {
panic(fmt.Sprintf("Paniced questionably: %v", v))
}
5 changes: 2 additions & 3 deletions manager/eris-mint/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ import (
"fmt"
"sync"

tendermint_common "github.com/tendermint/go-common"

account "github.com/eris-ltd/eris-db/account"
core_types "github.com/eris-ltd/eris-db/core/types"
definitions "github.com/eris-ltd/eris-db/definitions"
event "github.com/eris-ltd/eris-db/event"
word256 "github.com/eris-ltd/eris-db/word256"
)

// NOTE [ben] Compiler check to ensure Accounts successfully implements
Expand Down Expand Up @@ -119,7 +118,7 @@ func (this *accounts) StorageAt(address, key []byte) (*core_types.StorageItem,
storageRoot := account.StorageRoot
storageTree := state.LoadStorage(storageRoot)

_, value, _ := storageTree.Get(tendermint_common.LeftPadWord256(key).Bytes())
_, value, _ := storageTree.Get(word256.LeftPadWord256(key).Bytes())
if value == nil {
return &core_types.StorageItem{key, []byte{}}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion manager/eris-mint/eris-mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (app *ErisMint) Commit() (res tmsp.Result) {
// flush events to listeners (XXX: note issue with blocking)
app.evc.Flush()

// TODO: [ben] over the tendermint 0.6 TMSP interface we have
// TODO: [ben] over the tendermint 0.6 TMSP interface we have
// no access to the block header implemented;
// On Tendermint v0.8 load the blockheader into the application
// state and remove the fixed 2-"seconds" per block internal clock.
Expand Down
4 changes: 2 additions & 2 deletions manager/eris-mint/evm/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package vm
import (
"crypto/sha256"

"golang.org/x/crypto/ripemd160"
. "github.com/eris-ltd/eris-db/word256"

. "github.com/tendermint/go-common"
"golang.org/x/crypto/ripemd160"
)

var registeredNativeContracts = make(map[Word256]NativeContract)
Expand Down
6 changes: 3 additions & 3 deletions manager/eris-mint/evm/snative.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/hex"
"fmt"

"github.com/eris-ltd/eris-db/common/sanity"
"github.com/eris-ltd/eris-db/manager/eris-mint/evm/sha3"
ptypes "github.com/eris-ltd/eris-db/permission/types"

. "github.com/tendermint/go-common"
. "github.com/eris-ltd/eris-db/word256"
)

//------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -155,7 +155,7 @@ func set_global(appState AppState, caller *Account, args []byte, gas *int64) (ou
permNum, perm := returnTwoArgs(args)
vmAcc := appState.GetAccount(ptypes.GlobalPermissionsAddress256)
if vmAcc == nil {
PanicSanity("cant find the global permissions account")
sanity.PanicSanity("cant find the global permissions account")
}
permN := ptypes.PermFlag(Uint64FromWord256(permNum))
if !ValidPermN(permN) {
Expand Down
8 changes: 5 additions & 3 deletions manager/eris-mint/evm/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package vm
import (
"fmt"

. "github.com/tendermint/go-common"
"github.com/eris-ltd/eris-db/common/math/integral"
"github.com/eris-ltd/eris-db/common/sanity"
. "github.com/eris-ltd/eris-db/word256"
)

// Not goroutine safe
Expand Down Expand Up @@ -51,7 +53,7 @@ func (st *Stack) Push(d Word256) {
// currently only called after Sha3
func (st *Stack) PushBytes(bz []byte) {
if len(bz) != 32 {
PanicSanity("Invalid bytes size: expected 32")
sanity.PanicSanity("Invalid bytes size: expected 32")
}
st.Push(LeftPadWord256(bz))
}
Expand Down Expand Up @@ -115,7 +117,7 @@ func (st *Stack) Peek() Word256 {
func (st *Stack) Print(n int) {
fmt.Println("### stack ###")
if st.ptr > 0 {
nn := MinInt(n, st.ptr)
nn := integral.MinInt(n, st.ptr)
for j, i := 0, st.ptr-1; i > st.ptr-1-nn; i-- {
fmt.Printf("%-3d %X\n", j, st.data[i])
j += 1
Expand Down
Loading

0 comments on commit 9112e3a

Please sign in to comment.