diff options
Diffstat (limited to 'ethutil')
-rw-r--r-- | ethutil/big.go | 10 | ||||
-rw-r--r-- | ethutil/common.go | 1 | ||||
-rw-r--r-- | ethutil/common_test.go | 97 | ||||
-rw-r--r-- | ethutil/config.go | 2 | ||||
-rw-r--r-- | ethutil/main_test.go | 3 | ||||
-rw-r--r-- | ethutil/path.go | 10 | ||||
-rw-r--r-- | ethutil/rlp.go | 36 | ||||
-rw-r--r-- | ethutil/rlp_test.go | 10 | ||||
-rw-r--r-- | ethutil/script_unix.go | 42 | ||||
-rw-r--r-- | ethutil/script_windows.go | 23 | ||||
-rw-r--r-- | ethutil/value.go | 2 | ||||
-rw-r--r-- | ethutil/value_test.go | 3 |
12 files changed, 110 insertions, 129 deletions
diff --git a/ethutil/big.go b/ethutil/big.go index 07d1386e1..2ff1c72d8 100644 --- a/ethutil/big.go +++ b/ethutil/big.go @@ -62,6 +62,16 @@ func S256(x *big.Int) *big.Int { } } +func FirstBitSet(v *big.Int) int { + for i := 0; i < v.BitLen(); i++ { + if v.Bit(i) > 0 { + return i + } + } + + return v.BitLen() +} + // Big to bytes // // Returns the bytes of a big integer with the size specified by **base** diff --git a/ethutil/common.go b/ethutil/common.go index 0a29cac6c..271c56fd5 100644 --- a/ethutil/common.go +++ b/ethutil/common.go @@ -84,4 +84,5 @@ var ( BigFalse = Big0 Big32 = big.NewInt(32) Big256 = big.NewInt(0xff) + Big257 = big.NewInt(257) ) diff --git a/ethutil/common_test.go b/ethutil/common_test.go index 056676765..c2b6077e9 100644 --- a/ethutil/common_test.go +++ b/ethutil/common_test.go @@ -3,36 +3,39 @@ package ethutil import ( "math/big" "os" - "testing" + + checker "gopkg.in/check.v1" ) -func TestOS(t *testing.T) { - res := IsWindows() +type CommonSuite struct{} - if res && (os.PathSeparator != '\\' || os.PathListSeparator != ';') { - t.Error("IsWindows is", res, "but path is", os.PathSeparator) - } +var _ = checker.Suite(&CommonSuite{}) + +func (s *CommonSuite) TestOS(c *checker.C) { + expwin := (os.PathSeparator == '\\' && os.PathListSeparator == ';') + res := IsWindows() - if !res && (os.PathSeparator == '\\' && os.PathListSeparator == ';') { - t.Error("IsWindows is", res, "but path is", os.PathSeparator) + if !expwin { + c.Assert(res, checker.Equals, expwin, checker.Commentf("IsWindows is", res, "but path is", os.PathSeparator)) + } else { + c.Assert(res, checker.Not(checker.Equals), expwin, checker.Commentf("IsWindows is", res, "but path is", os.PathSeparator)) } } -func TestWindonziePath(t *testing.T) { +func (s *CommonSuite) TestWindonziePath(c *checker.C) { + iswindowspath := os.PathSeparator == '\\' path := "/opt/eth/test/file.ext" res := WindonizePath(path) - iswindowspath := os.PathSeparator == '\\' - - if !iswindowspath && string(res[0]) != "/" { - t.Error("Got", res) - } + ressep := string(res[0]) - if iswindowspath && string(res[0]) == "/" { - t.Error("Got", res) + if !iswindowspath { + c.Assert(ressep, checker.Equals, "/") + } else { + c.Assert(ressep, checker.Not(checker.Equals), "/") } } -func TestCommon(t *testing.T) { +func (s *CommonSuite) TestCommon(c *checker.C) { douglas := CurrencyToString(BigPow(10, 43)) einstein := CurrencyToString(BigPow(10, 22)) ether := CurrencyToString(BigPow(10, 19)) @@ -43,57 +46,23 @@ func TestCommon(t *testing.T) { ada := CurrencyToString(BigPow(10, 4)) wei := CurrencyToString(big.NewInt(10)) - if douglas != "10 Douglas" { - t.Error("Got", douglas) - } - - if einstein != "10 Einstein" { - t.Error("Got", einstein) - } - - if ether != "10 Ether" { - t.Error("Got", ether) - } - - if finney != "10 Finney" { - t.Error("Got", finney) - } - - if szabo != "10 Szabo" { - t.Error("Got", szabo) - } - - if shannon != "10 Shannon" { - t.Error("Got", shannon) - } - - if babbage != "10 Babbage" { - t.Error("Got", babbage) - } - - if ada != "10 Ada" { - t.Error("Got", ada) - } - - if wei != "10 Wei" { - t.Error("Got", wei) - } + c.Assert(douglas, checker.Equals, "10 Douglas") + c.Assert(einstein, checker.Equals, "10 Einstein") + c.Assert(ether, checker.Equals, "10 Ether") + c.Assert(finney, checker.Equals, "10 Finney") + c.Assert(szabo, checker.Equals, "10 Szabo") + c.Assert(shannon, checker.Equals, "10 Shannon") + c.Assert(babbage, checker.Equals, "10 Babbage") + c.Assert(ada, checker.Equals, "10 Ada") + c.Assert(wei, checker.Equals, "10 Wei") } -func TestLarge(t *testing.T) { +func (s *CommonSuite) TestLarge(c *checker.C) { douglaslarge := CurrencyToString(BigPow(100000000, 43)) adalarge := CurrencyToString(BigPow(100000000, 4)) weilarge := CurrencyToString(big.NewInt(100000000)) - if douglaslarge != "10000E298 Douglas" { - t.Error("Got", douglaslarge) - } - - if adalarge != "10000E7 Einstein" { - t.Error("Got", adalarge) - } - - if weilarge != "100 Babbage" { - t.Error("Got", weilarge) - } + c.Assert(douglaslarge, checker.Equals, "10000E298 Douglas") + c.Assert(adalarge, checker.Equals, "10000E7 Einstein") + c.Assert(weilarge, checker.Equals, "100 Babbage") } diff --git a/ethutil/config.go b/ethutil/config.go index ccc7714d0..fc8fb4e3f 100644 --- a/ethutil/config.go +++ b/ethutil/config.go @@ -10,8 +10,6 @@ import ( // Config struct type ConfigManager struct { - Db Database - ExecPath string Debug bool Diff bool diff --git a/ethutil/main_test.go b/ethutil/main_test.go index 94f34677d..fd4278ce7 100644 --- a/ethutil/main_test.go +++ b/ethutil/main_test.go @@ -1,8 +1,9 @@ package ethutil import ( - checker "gopkg.in/check.v1" "testing" + + checker "gopkg.in/check.v1" ) func Test(t *testing.T) { checker.TestingT(t) } diff --git a/ethutil/path.go b/ethutil/path.go index cfbc38950..e545c8731 100644 --- a/ethutil/path.go +++ b/ethutil/path.go @@ -4,6 +4,7 @@ import ( "io/ioutil" "os" "os/user" + "path" "strings" ) @@ -11,7 +12,7 @@ func ExpandHomePath(p string) (path string) { path = p // Check in case of paths like "/something/~/something/" - if path[:2] == "~/" { + if len(path) > 1 && path[:2] == "~/" { usr, _ := user.Current() dir := usr.HomeDir @@ -58,3 +59,10 @@ func WriteFile(filePath string, content []byte) error { return nil } + +func AbsolutePath(Datadir string, filename string) string { + if path.IsAbs(filename) { + return filename + } + return path.Join(Datadir, filename) +} diff --git a/ethutil/rlp.go b/ethutil/rlp.go index 1fff2b28a..0cb0d611c 100644 --- a/ethutil/rlp.go +++ b/ethutil/rlp.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "math/big" + "reflect" ) type RlpEncode interface { @@ -97,6 +98,14 @@ var ( zeroRlp = big.NewInt(0x0) ) +func intlen(i int64) (length int) { + for i > 0 { + i = i >> 8 + length++ + } + return +} + func Encode(object interface{}) []byte { var buff bytes.Buffer @@ -128,7 +137,7 @@ func Encode(object interface{}) []byte { case byte: buff.Write(Encode(big.NewInt(int64(t)))) case *big.Int: - // Not sure how this is possible while we check for + // Not sure how this is possible while we check for nil if t == nil { buff.WriteByte(0xc0) } else { @@ -168,6 +177,31 @@ func Encode(object interface{}) []byte { } WriteSliceHeader(len(b.Bytes())) buff.Write(b.Bytes()) + default: + // This is how it should have been from the start + // needs refactoring (@fjl) + v := reflect.ValueOf(t) + switch v.Kind() { + case reflect.Slice: + var b bytes.Buffer + for i := 0; i < v.Len(); i++ { + b.Write(Encode(v.Index(i).Interface())) + } + + blen := b.Len() + if blen < 56 { + buff.WriteByte(byte(blen) + 0xc0) + } else { + ilen := byte(intlen(int64(blen))) + buff.WriteByte(ilen + 0xf7) + t := make([]byte, ilen) + for i := byte(0); i < ilen; i++ { + t[ilen-i-1] = byte(blen >> (i * 8)) + } + buff.Write(t) + } + buff.ReadFrom(&b) + } } } else { // Empty list for nil diff --git a/ethutil/rlp_test.go b/ethutil/rlp_test.go index 90057ab42..ff98d3269 100644 --- a/ethutil/rlp_test.go +++ b/ethutil/rlp_test.go @@ -7,6 +7,16 @@ import ( "testing" ) +func TestNonInterfaceSlice(t *testing.T) { + vala := []string{"value1", "value2", "value3"} + valb := []interface{}{"value1", "value2", "value3"} + resa := Encode(vala) + resb := Encode(valb) + if !bytes.Equal(resa, resb) { + t.Errorf("expected []string & []interface{} to be equal") + } +} + func TestRlpValueEncoding(t *testing.T) { val := EmptyValue() val.AppendList().Append(1).Append(2).Append(3) diff --git a/ethutil/script_unix.go b/ethutil/script_unix.go index 37e5bf91b..9250dda57 100644 --- a/ethutil/script_unix.go +++ b/ethutil/script_unix.go @@ -2,47 +2,17 @@ package ethutil -import ( - "fmt" - "strings" - - "github.com/obscuren/mutan" - "github.com/obscuren/mutan/backends" - "github.com/obscuren/serpent-go" -) +import "github.com/ethereum/serpent-go" // General compile function func Compile(script string, silent bool) (ret []byte, err error) { if len(script) > 2 { - line := strings.Split(script, "\n")[0] - - if len(line) > 1 && line[0:2] == "#!" { - switch line { - case "#!serpent": - byteCode, err := serpent.Compile(script) - if err != nil { - return nil, err - } - - return byteCode, nil - } - } else { - - compiler := mutan.NewCompiler(backend.NewEthereumBackend()) - compiler.Silent = silent - byteCode, errors := compiler.Compile(strings.NewReader(script)) - if len(errors) > 0 { - var errs string - for _, er := range errors { - if er != nil { - errs += er.Error() - } - } - return nil, fmt.Errorf("%v", errs) - } - - return byteCode, nil + byteCode, err := serpent.Compile(script) + if err != nil { + return nil, err } + + return byteCode, nil } return nil, nil diff --git a/ethutil/script_windows.go b/ethutil/script_windows.go index ef239cd51..1dedc5f60 100644 --- a/ethutil/script_windows.go +++ b/ethutil/script_windows.go @@ -2,31 +2,10 @@ package ethutil -import ( - "fmt" - "strings" - - "github.com/obscuren/mutan" - "github.com/obscuren/mutan/backends" -) - // General compile function func Compile(script string, silent bool) (ret []byte, err error) { if len(script) > 2 { - compiler := mutan.NewCompiler(backend.NewEthereumBackend()) - compiler.Silent = silent - byteCode, errors := compiler.Compile(strings.NewReader(script)) - if len(errors) > 0 { - var errs string - for _, er := range errors { - if er != nil { - errs += er.Error() - } - } - return nil, fmt.Errorf("%v", errs) - } - - return byteCode, nil + return nil, nil } return nil, nil diff --git a/ethutil/value.go b/ethutil/value.go index 6417b0008..7d4a7d98c 100644 --- a/ethutil/value.go +++ b/ethutil/value.go @@ -397,5 +397,5 @@ func (it *ValueIterator) Value() *Value { } func (it *ValueIterator) Idx() int { - return it.idx + return it.idx - 1 } diff --git a/ethutil/value_test.go b/ethutil/value_test.go index 7c58d3b56..861d35184 100644 --- a/ethutil/value_test.go +++ b/ethutil/value_test.go @@ -1,8 +1,9 @@ package ethutil import ( - checker "gopkg.in/check.v1" "math/big" + + checker "gopkg.in/check.v1" ) type ValueSuite struct{} |