diff options
author | Felix Lange <fjl@users.noreply.github.com> | 2017-02-27 05:21:51 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2017-02-27 05:21:51 +0800 |
commit | 5c8fe28b725bd9b128edceae3215132ea741641b (patch) | |
tree | 4bcbfeb1a21536edbba06d1715752999faa7f085 /common/bytes_test.go | |
parent | 50ee279f2585e9e2eee30f0e1d4a3bf5f781bd72 (diff) | |
download | go-tangerine-5c8fe28b725bd9b128edceae3215132ea741641b.tar go-tangerine-5c8fe28b725bd9b128edceae3215132ea741641b.tar.gz go-tangerine-5c8fe28b725bd9b128edceae3215132ea741641b.tar.bz2 go-tangerine-5c8fe28b725bd9b128edceae3215132ea741641b.tar.lz go-tangerine-5c8fe28b725bd9b128edceae3215132ea741641b.tar.xz go-tangerine-5c8fe28b725bd9b128edceae3215132ea741641b.tar.zst go-tangerine-5c8fe28b725bd9b128edceae3215132ea741641b.zip |
common: move big integer math to common/math (#3699)
* common: remove CurrencyToString
Move denomination values to params instead.
* common: delete dead code
* common: move big integer operations to common/math
This commit consolidates all big integer operations into common/math and
adds tests and documentation.
There should be no change in semantics for BigPow, BigMin, BigMax, S256,
U256, Exp and their behaviour is now locked in by tests.
The BigD, BytesToBig and Bytes2Big functions don't provide additional
value, all uses are replaced by new(big.Int).SetBytes().
BigToBytes is now called PaddedBigBytes, its minimum output size
parameter is now specified as the number of bytes instead of bits. The
single use of this function is in the EVM's MSTORE instruction.
Big and String2Big are replaced by ParseBig, which is slightly stricter.
It previously accepted leading zeros for hexadecimal inputs but treated
decimal inputs as octal if a leading zero digit was present.
ParseUint64 is used in places where String2Big was used to decode a
uint64.
The new functions MustParseBig and MustParseUint64 are now used in many
places where parsing errors were previously ignored.
* common: delete unused big integer variables
* accounts/abi: replace uses of BytesToBig with use of encoding/binary
* common: remove BytesToBig
* common: remove Bytes2Big
* common: remove BigTrue
* cmd/utils: add BigFlag and use it for error-checked integer flags
While here, remove environment variable processing for DirectoryFlag
because we don't use it.
* core: add missing error checks in genesis block parser
* common: remove String2Big
* cmd/evm: use utils.BigFlag
* common/math: check for 256 bit overflow in ParseBig
This is supposed to prevent silent overflow/truncation of values in the
genesis block JSON. Without this check, a genesis block that set a
balance larger than 256 bits would lead to weird behaviour in the VM.
* cmd/utils: fixup import
Diffstat (limited to 'common/bytes_test.go')
-rw-r--r-- | common/bytes_test.go | 108 |
1 files changed, 0 insertions, 108 deletions
diff --git a/common/bytes_test.go b/common/bytes_test.go index 98d402c48..fc164b13d 100644 --- a/common/bytes_test.go +++ b/common/bytes_test.go @@ -27,54 +27,6 @@ type BytesSuite struct{} var _ = checker.Suite(&BytesSuite{}) -func (s *BytesSuite) TestNumberToBytes(c *checker.C) { - // data1 := int(1) - // res1 := NumberToBytes(data1, 16) - // c.Check(res1, checker.Panics) - - var data2 float64 = 3.141592653 - exp2 := []byte{0xe9, 0x38} - res2 := NumberToBytes(data2, 16) - c.Assert(res2, checker.DeepEquals, exp2) -} - -func (s *BytesSuite) TestBytesToNumber(c *checker.C) { - datasmall := []byte{0xe9, 0x38, 0xe9, 0x38} - datalarge := []byte{0xe9, 0x38, 0xe9, 0x38, 0xe9, 0x38, 0xe9, 0x38} - - var expsmall uint64 = 0xe938e938 - var explarge uint64 = 0x0 - - ressmall := BytesToNumber(datasmall) - reslarge := BytesToNumber(datalarge) - - c.Assert(ressmall, checker.Equals, expsmall) - c.Assert(reslarge, checker.Equals, explarge) - -} - -func (s *BytesSuite) TestReadVarInt(c *checker.C) { - data8 := []byte{1, 2, 3, 4, 5, 6, 7, 8} - data4 := []byte{1, 2, 3, 4} - data2 := []byte{1, 2} - data1 := []byte{1} - - exp8 := uint64(72623859790382856) - exp4 := uint64(16909060) - exp2 := uint64(258) - exp1 := uint64(1) - - res8 := ReadVarInt(data8) - res4 := ReadVarInt(data4) - res2 := ReadVarInt(data2) - res1 := ReadVarInt(data1) - - c.Assert(res8, checker.Equals, exp8) - c.Assert(res4, checker.Equals, exp4) - c.Assert(res2, checker.Equals, exp2) - c.Assert(res1, checker.Equals, exp1) -} - func (s *BytesSuite) TestCopyBytes(c *checker.C) { data1 := []byte{1, 2, 3, 4} exp1 := []byte{1, 2, 3, 4} @@ -95,22 +47,6 @@ func (s *BytesSuite) TestIsHex(c *checker.C) { } -func (s *BytesSuite) TestParseDataString(c *checker.C) { - res1 := ParseData("hello", "world", "0x0106") - data := "68656c6c6f000000000000000000000000000000000000000000000000000000776f726c640000000000000000000000000000000000000000000000000000000106000000000000000000000000000000000000000000000000000000000000" - exp1 := Hex2Bytes(data) - c.Assert(res1, checker.DeepEquals, exp1) -} - -func (s *BytesSuite) TestParseDataBytes(c *checker.C) { - data1 := []byte{232, 212, 165, 16, 0} - exp1 := []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 232, 212, 165, 16, 0} - - res1 := ParseData(data1) - c.Assert(res1, checker.DeepEquals, exp1) - -} - func (s *BytesSuite) TestLeftPadBytes(c *checker.C) { val1 := []byte{1, 2, 3, 4} exp1 := []byte{0, 0, 0, 0, 1, 2, 3, 4} @@ -122,28 +58,6 @@ func (s *BytesSuite) TestLeftPadBytes(c *checker.C) { c.Assert(res2, checker.DeepEquals, val1) } -func (s *BytesSuite) TestFormatData(c *checker.C) { - data1 := "" - data2 := "0xa9e67e00" - data3 := "a9e67e" - data4 := "\"a9e67e00\"" - - // exp1 := []byte{} - exp2 := []byte{00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 0xa9, 0xe6, 0x7e, 00} - exp3 := []byte{00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00} - exp4 := []byte{0x61, 0x39, 0x65, 0x36, 0x37, 0x65, 0x30, 0x30, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00} - - res1 := FormatData(data1) - res2 := FormatData(data2) - res3 := FormatData(data3) - res4 := FormatData(data4) - - c.Assert(res1, checker.IsNil) - c.Assert(res2, checker.DeepEquals, exp2) - c.Assert(res3, checker.DeepEquals, exp3) - c.Assert(res4, checker.DeepEquals, exp4) -} - func (s *BytesSuite) TestRightPadBytes(c *checker.C) { val := []byte{1, 2, 3, 4} exp := []byte{1, 2, 3, 4, 0, 0, 0, 0} @@ -155,28 +69,6 @@ func (s *BytesSuite) TestRightPadBytes(c *checker.C) { c.Assert(resshrt, checker.DeepEquals, val) } -func (s *BytesSuite) TestLeftPadString(c *checker.C) { - val := "test" - exp := "\x30\x30\x30\x30" + val - - resstd := LeftPadString(val, 8) - resshrt := LeftPadString(val, 2) - - c.Assert(resstd, checker.Equals, exp) - c.Assert(resshrt, checker.Equals, val) -} - -func (s *BytesSuite) TestRightPadString(c *checker.C) { - val := "test" - exp := val + "\x30\x30\x30\x30" - - resstd := RightPadString(val, 8) - resshrt := RightPadString(val, 2) - - c.Assert(resstd, checker.Equals, exp) - c.Assert(resshrt, checker.Equals, val) -} - func TestFromHex(t *testing.T) { input := "0x01" expected := []byte{1} |