From 6f496754fb035bf1a630feb151f4f804070c05ba Mon Sep 17 00:00:00 2001 From: dauTT <30392990+dauTT@users.noreply.github.com> Date: Sat, 23 Mar 2019 17:52:36 +0100 Subject: [PATCH] 1) Fixed String method in Uint256 (#208) 2) Added ReverseString Method to Uint256 --- pkg/wire/payload/block_test.go | 8 ++++---- pkg/wire/payload/mheaders_test.go | 4 ++-- pkg/wire/payload/transaction/claim_test.go | 4 ++-- pkg/wire/payload/transaction/contract_test.go | 6 +++--- pkg/wire/payload/transaction/enrollment_test.go | 2 +- pkg/wire/payload/transaction/invocation_test.go | 2 +- pkg/wire/payload/transaction/miner_test.go | 2 +- pkg/wire/payload/transaction/publish_test.go | 2 +- pkg/wire/payload/transaction/register_test.go | 4 ++-- pkg/wire/payload/transaction/state_test.go | 4 ++-- pkg/wire/util/uint256.go | 5 +++++ pkg/wire/util/uint256_test.go | 4 ++-- 12 files changed, 26 insertions(+), 21 deletions(-) diff --git a/pkg/wire/payload/block_test.go b/pkg/wire/payload/block_test.go index 2de7cdb4e..88c479c81 100644 --- a/pkg/wire/payload/block_test.go +++ b/pkg/wire/payload/block_test.go @@ -46,13 +46,13 @@ func TestBlockDecodeEncode(t *testing.T) { for _, tx := range b.Txs { switch t := tx.(type) { case *transaction.Contract: - hashes = append(hashes, t.Hash.String()) + hashes = append(hashes, t.Hash.ReverseString()) case *transaction.Miner: - hashes = append(hashes, t.Hash.String()) + hashes = append(hashes, t.Hash.ReverseString()) case *transaction.Claim: - hashes = append(hashes, t.Hash.String()) + hashes = append(hashes, t.Hash.ReverseString()) case *transaction.Invocation: - hashes = append(hashes, t.Hash.String()) + hashes = append(hashes, t.Hash.ReverseString()) } } diff --git a/pkg/wire/payload/mheaders_test.go b/pkg/wire/payload/mheaders_test.go index a03f43486..7527b2908 100644 --- a/pkg/wire/payload/mheaders_test.go +++ b/pkg/wire/payload/mheaders_test.go @@ -50,7 +50,7 @@ func TestAddAndEncodeHeaders(t *testing.T) { err := msgHeaders.Headers[0].createHash() assert.Equal(t, nil, err) // Hash being correct, automatically verifies that the fields are encoded properly - assert.Equal(t, "f3c4ec44c07eccbda974f1ee34bc6654ab6d3f22cd89c2e5c593a16d6cc7e6e8", msgHeaders.Headers[0].Hash.String()) + assert.Equal(t, "f3c4ec44c07eccbda974f1ee34bc6654ab6d3f22cd89c2e5c593a16d6cc7e6e8", msgHeaders.Headers[0].Hash.ReverseString()) } @@ -70,7 +70,7 @@ func TestEncodeDecode(t *testing.T) { header := headerMsg.Headers[0] err = header.createHash() - assert.Equal(t, "f3c4ec44c07eccbda974f1ee34bc6654ab6d3f22cd89c2e5c593a16d6cc7e6e8", header.Hash.String()) + assert.Equal(t, "f3c4ec44c07eccbda974f1ee34bc6654ab6d3f22cd89c2e5c593a16d6cc7e6e8", header.Hash.ReverseString()) buf := new(bytes.Buffer) diff --git a/pkg/wire/payload/transaction/claim_test.go b/pkg/wire/payload/transaction/claim_test.go index fb049fb58..53ae8ba79 100644 --- a/pkg/wire/payload/transaction/claim_test.go +++ b/pkg/wire/payload/transaction/claim_test.go @@ -26,9 +26,9 @@ func TestEncodeDecodeClaim(t *testing.T) { assert.Equal(t, 1, int(len(c.Claims))) claim := c.Claims[0] - assert.Equal(t, "497037a4c5e0a9ea1721e06f9d5e9aec183d11f2824ece93285729370f3a1baf", claim.PrevHash.String()) + assert.Equal(t, "497037a4c5e0a9ea1721e06f9d5e9aec183d11f2824ece93285729370f3a1baf", claim.PrevHash.ReverseString()) assert.Equal(t, uint16(0), claim.PrevIndex) - assert.Equal(t, "abf142faf539c340e42722b5b34b505cf4fd73185fed775784e37c2c5ef1b866", c.Hash.String()) + assert.Equal(t, "abf142faf539c340e42722b5b34b505cf4fd73185fed775784e37c2c5ef1b866", c.Hash.ReverseString()) // Encode buf := new(bytes.Buffer) diff --git a/pkg/wire/payload/transaction/contract_test.go b/pkg/wire/payload/transaction/contract_test.go index ea0393301..17c4de77f 100644 --- a/pkg/wire/payload/transaction/contract_test.go +++ b/pkg/wire/payload/transaction/contract_test.go @@ -27,12 +27,12 @@ func TestEncodeDecodeContract(t *testing.T) { input := c.Inputs[0] - assert.Equal(t, "eec17cc828d6ede932b57e4eaf79c2591151096a7825435cd67f498f9fa98d88", input.PrevHash.String()) + assert.Equal(t, "eec17cc828d6ede932b57e4eaf79c2591151096a7825435cd67f498f9fa98d88", input.PrevHash.ReverseString()) assert.Equal(t, 0, int(input.PrevIndex)) assert.Equal(t, int64(70600000000), c.Outputs[0].Amount) - assert.Equal(t, "c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b", c.Outputs[0].AssetID.String()) + assert.Equal(t, "c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b", c.Outputs[0].AssetID.ReverseString()) assert.Equal(t, "a8666b4830229d6a1a9b80f6088059191c122d2b", c.Outputs[0].ScriptHash.String()) - assert.Equal(t, "bdf6cc3b9af12a7565bda80933a75ee8cef1bc771d0d58effc08e4c8b436da79", c.Hash.String()) + assert.Equal(t, "bdf6cc3b9af12a7565bda80933a75ee8cef1bc771d0d58effc08e4c8b436da79", c.Hash.ReverseString()) // Encode buf := new(bytes.Buffer) diff --git a/pkg/wire/payload/transaction/enrollment_test.go b/pkg/wire/payload/transaction/enrollment_test.go index cf76af8e7..b2f0a3a16 100644 --- a/pkg/wire/payload/transaction/enrollment_test.go +++ b/pkg/wire/payload/transaction/enrollment_test.go @@ -26,5 +26,5 @@ func TestEncodeDecodeEnrollment(t *testing.T) { assert.Equal(t, nil, err) assert.Equal(t, rawtx, hex.EncodeToString(buf.Bytes())) - assert.Equal(t, "988832f693785dcbcb8d5a0e9d5d22002adcbfb1eb6bbeebf8c494fff580e147", enroll.Hash.String()) + assert.Equal(t, "988832f693785dcbcb8d5a0e9d5d22002adcbfb1eb6bbeebf8c494fff580e147", enroll.Hash.ReverseString()) } diff --git a/pkg/wire/payload/transaction/invocation_test.go b/pkg/wire/payload/transaction/invocation_test.go index 82ce2fcb6..fcd6a9818 100644 --- a/pkg/wire/payload/transaction/invocation_test.go +++ b/pkg/wire/payload/transaction/invocation_test.go @@ -34,7 +34,7 @@ func TestEncodeDecodeInvoc(t *testing.T) { assert.Equal(t, "31363a30373a3032203a2030333366616431392d643638322d343035382d626437662d313563393331323434336538", hex.EncodeToString(attr2.Data)) assert.Equal(t, "050034e23004141ad842821c7341d5a32b17d7177a1750d30014ca14628c9e5bc6a9346ca6bcdf050ceabdeb2bdc774953c1087472616e736665726703e1df72015bdef1a1b9567d4700635f23b1f406f1", hex.EncodeToString(i.Script)) - assert.Equal(t, "b2a22cd9dd7636ae23e25576866cd1d9e2f3d85a85e80874441f085cd60006d1", i.Hash.String()) + assert.Equal(t, "b2a22cd9dd7636ae23e25576866cd1d9e2f3d85a85e80874441f085cd60006d1", i.Hash.ReverseString()) // Encode buf := new(bytes.Buffer) diff --git a/pkg/wire/payload/transaction/miner_test.go b/pkg/wire/payload/transaction/miner_test.go index d053ce414..fa22ebcd4 100644 --- a/pkg/wire/payload/transaction/miner_test.go +++ b/pkg/wire/payload/transaction/miner_test.go @@ -24,7 +24,7 @@ func TestEncodeDecodeMiner(t *testing.T) { assert.Equal(t, types.Miner, m.Type) assert.Equal(t, uint32(571397116), m.Nonce) - assert.Equal(t, "a1f219dc6be4c35eca172e65e02d4591045220221b1543f1a4b67b9e9442c264", m.Hash.String()) + assert.Equal(t, "a1f219dc6be4c35eca172e65e02d4591045220221b1543f1a4b67b9e9442c264", m.Hash.ReverseString()) // Encode buf := new(bytes.Buffer) diff --git a/pkg/wire/payload/transaction/publish_test.go b/pkg/wire/payload/transaction/publish_test.go index 1c9bb1de1..e27a44777 100644 --- a/pkg/wire/payload/transaction/publish_test.go +++ b/pkg/wire/payload/transaction/publish_test.go @@ -28,6 +28,6 @@ func TestEncodeDecodePublish(t *testing.T) { assert.Equal(t, nil, err) assert.Equal(t, rawtx, hex.EncodeToString(buf.Bytes())) - assert.Equal(t, "5467a1fc8723ceffa8e5ee59399b02eea1df6fbaa53768c6704b90b960d223fa", publ.Hash.String()) + assert.Equal(t, "5467a1fc8723ceffa8e5ee59399b02eea1df6fbaa53768c6704b90b960d223fa", publ.Hash.ReverseString()) } diff --git a/pkg/wire/payload/transaction/register_test.go b/pkg/wire/payload/transaction/register_test.go index d5492a6d8..da8bfdc45 100644 --- a/pkg/wire/payload/transaction/register_test.go +++ b/pkg/wire/payload/transaction/register_test.go @@ -28,7 +28,7 @@ func TestEncodeDecodeRegister(t *testing.T) { assert.Equal(t, nil, err) assert.Equal(t, rawtx, hex.EncodeToString(buf.Bytes())) - assert.Equal(t, "0c092117b4ba47b81001712425e6e7f760a637695eaf23741ba335925b195ecd", reg.Hash.String()) + assert.Equal(t, "0c092117b4ba47b81001712425e6e7f760a637695eaf23741ba335925b195ecd", reg.Hash.ReverseString()) } func TestEncodeDecodeGenesisRegister(t *testing.T) { @@ -50,5 +50,5 @@ func TestEncodeDecodeGenesisRegister(t *testing.T) { assert.Equal(t, nil, err) assert.Equal(t, rawtx, hex.EncodeToString(buf.Bytes())) - assert.Equal(t, "c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b", reg.Hash.String()) + assert.Equal(t, "c56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b", reg.Hash.ReverseString()) } diff --git a/pkg/wire/payload/transaction/state_test.go b/pkg/wire/payload/transaction/state_test.go index 209d9b9c0..d585daf76 100644 --- a/pkg/wire/payload/transaction/state_test.go +++ b/pkg/wire/payload/transaction/state_test.go @@ -25,7 +25,7 @@ func TestEncodeDecodeState(t *testing.T) { assert.Equal(t, 1, len(s.Inputs)) input := s.Inputs[0] - assert.Equal(t, "a192cbabc6d613ecfcce43fd09e9197556ca5cf7d4bd1f6c65726ea9f08441cb", input.PrevHash.String()) + assert.Equal(t, "a192cbabc6d613ecfcce43fd09e9197556ca5cf7d4bd1f6c65726ea9f08441cb", input.PrevHash.ReverseString()) assert.Equal(t, uint16(0), input.PrevIndex) assert.Equal(t, 1, len(s.Descriptors)) @@ -43,5 +43,5 @@ func TestEncodeDecodeState(t *testing.T) { assert.Equal(t, nil, err) assert.Equal(t, rawtxBytes, buf.Bytes()) - assert.Equal(t, "8abf5ebdb9a8223b12109513647f45bd3c0a6cf1a6346d56684cff71ba308724", s.Hash.String()) + assert.Equal(t, "8abf5ebdb9a8223b12109513647f45bd3c0a6cf1a6346d56684cff71ba308724", s.Hash.ReverseString()) } diff --git a/pkg/wire/util/uint256.go b/pkg/wire/util/uint256.go index e7a5e73fc..fc3fe86ca 100644 --- a/pkg/wire/util/uint256.go +++ b/pkg/wire/util/uint256.go @@ -63,6 +63,11 @@ func (u Uint256) Equals(other Uint256) bool { // String implements the stringer interface. func (u Uint256) String() string { + return hex.EncodeToString(u.Bytes()) +} + +// ReverseString displays a reverse string representation of Uint256. +func (u Uint256) ReverseString() string { return hex.EncodeToString(slice.Reverse(u.Bytes())) } diff --git a/pkg/wire/util/uint256_test.go b/pkg/wire/util/uint256_test.go index 120bee380..ae6b8a438 100644 --- a/pkg/wire/util/uint256_test.go +++ b/pkg/wire/util/uint256_test.go @@ -13,7 +13,7 @@ func TestUint256DecodeString(t *testing.T) { if err != nil { t.Fatal(err) } - assert.Equal(t, hexStr, val.Reverse().String()) + assert.Equal(t, hexStr, val.String()) } func TestUint256DecodeBytes(t *testing.T) { @@ -26,7 +26,7 @@ func TestUint256DecodeBytes(t *testing.T) { if err != nil { t.Fatal(err) } - assert.Equal(t, hexStr, val.Reverse().String()) + assert.Equal(t, hexStr, val.String()) } func TestUInt256Equals(t *testing.T) {