From ce1fe72607ad1f4bd8e04805c1c71511cb1e593d Mon Sep 17 00:00:00 2001 From: dauTT <30392990+dauTT@users.noreply.github.com> Date: Mon, 25 Mar 2019 02:04:54 +0100 Subject: [PATCH] Finalized size calculation methodology (#215) - Simplified Transactioner interface - Added size calculation test - Added utility methods in the address pkg: FromUint160, Uint160Decode --- pkg/chain/chaindb.go | 6 +- pkg/wire/payload/block_test.go | 92 ++++++++++++++++++++++++++++ pkg/wire/payload/transaction/base.go | 21 ++----- pkg/wire/util/address/address.go | 19 ++++++ 4 files changed, 118 insertions(+), 20 deletions(-) diff --git a/pkg/chain/chaindb.go b/pkg/chain/chaindb.go index 9b16b366a..5f1a7124e 100644 --- a/pkg/chain/chaindb.go +++ b/pkg/chain/chaindb.go @@ -98,7 +98,7 @@ func (c *Chaindb) saveTx(tx transaction.Transactioner, txIndex uint32, blockHash if err != nil { return err } - err = txTable.Put(txHash.Bytes(), tx.Bytes()) + err = txTable.Put(txHash.Bytes(), tx.BaseTx().Bytes()) if err != nil { return err } @@ -118,7 +118,7 @@ func (c *Chaindb) saveTx(tx transaction.Transactioner, txIndex uint32, blockHash // Save all of the utxos in a transaction // We do this additional save so that we can form a utxo database // and know when a transaction is a double spend. - utxos := tx.UTXOs() + utxos := tx.BaseTx().Outputs for utxoIndex, utxo := range utxos { err := c.saveUTXO(utxo, uint16(utxoIndex), txHash.Bytes(), blockHash) if err != nil { @@ -135,7 +135,7 @@ func (c *Chaindb) saveTx(tx transaction.Transactioner, txIndex uint32, blockHash // We do this so that once an output has been spent // It will be removed from the utxo database and cannot be spent again // If the output was never in the utxo database, this function will return an error - txos := tx.TXOs() + txos := tx.BaseTx().Inputs for _, txo := range txos { err := c.removeUTXO(txo) if err != nil { diff --git a/pkg/wire/payload/block_test.go b/pkg/wire/payload/block_test.go index 88c479c81..0736ff90d 100644 --- a/pkg/wire/payload/block_test.go +++ b/pkg/wire/payload/block_test.go @@ -5,6 +5,8 @@ import ( "encoding/hex" "testing" + "github.com/CityOfZion/neo-go/pkg/wire/util/address" + "github.com/CityOfZion/neo-go/pkg/wire/payload/transaction" "github.com/stretchr/testify/assert" ) @@ -78,3 +80,93 @@ func TestBlockDecodeEncode(t *testing.T) { assert.Equal(t, rawtx, hex.EncodeToString(buf.Bytes())) } +func TestBlockSizeCalculation(t *testing.T) { + // block taken from mainnet: 0006d3ff96e269f599eb1b5c5a527c218439e498dcc65b63794591bbcdc0516b + // The Size in golang is given by counting the number of bytes of an object. (len(Bytes)) + // its implementation is different from the corresponding C# and python implentation. But the result should + // should be the same.In this test we provide more details then necessary because in case of failure we can easily debug the + // root cause of the size calculation missmatch. + + rawBlock := "00000000ba33df12e8adbf38b6039e79ee91fdb8b1519e2e6154cb59c0653c81769288f4a22492109b7a84077ed7226c28612eb61428ea9ded9bdc952cdfc13deb4172ef85d1115b0bb62300abff35093d19a14a59e75d652b5d3827bf04c165bbe9ef95cca4bf5501fd45014012afae6df64195041e4764b57caa9e27fc2cfc596833163904136ec95816d104b44b3737d0e9f6b1b4445cd3b6a5cc80f6b0935675bc44dba44415eb309832b3404dc95bcf85e4635556a1d618e4ce947b26972992ed74788df5f9501b850ac0b40b7112d1ff30e4ade00369e16f0d13932d1ba76725e7682db072f8e2cd7752b840d12bb7dd45dd3b0e2098db5c67b6de55b7c40164937491fcaca1239b25860251224ead23ab232add78ccccd347239eae50ffc98f50b2a84c60ec5c3d284647a7406fabf6ca241b759af6b71080c0dfad7395632e989226a7e52f8cd2c133aeb2226e6e1aea47666fd81f578405a9f9bbd9d0bc523c3a44d7a5099ddc649feabe5f406188b8ee478731a89beeb76fdbd108eb0071b8f2b8678f40c5a1f387a491314336783255dee8cc5af4bf914dfeaacecc318fc13e02262658e39e8ce0631941b1f1552102486fd15702c4490a26703112a5cc1d0923fd697a33406bd5a1c00e0013b09a7021024c7b7fb6c310fccf1ba33b082519d82964ea93868d676662d4a59ad548df0e7d2102aaec38470f6aad0042c6e877cfd8087d2676b0f516fddd362801b9bd3936399e2103b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c2103b8d9d5771d8f513aa0869b9cc8d50986403b78c6da36890638c3d46a5adce04a2102ca0e27697b9c248f6f16e085fd0061e26f44da85b58ee835c110caa5ec3ba5542102df48f60e8f3e01c48ff40b9b7f1310d7a8b2a193188befe1c2e3df740e89509357ae140000abff350900000000d101530800e1f5050000000014d8dd86f6d91eb2add2f2d8afeda2184ed94ac33314288017f85d80b889fe02beb5ff203ed9ef538f1653c1087472616e7366657267cf9472821400ceb06ca780c2a937fec5bbec51b900000000000000000220288017f85d80b889fe02beb5ff203ed9ef538f16f0153135323738393433373039313963623362646632650000014140b61b1a8d220c28633fa1a43ef02d334731b16013778664bc28db838d9ab8cfa64f9134fe952ad8a8eb8a6dd9d864055301bcdba4177fd6ee0f52b3f096db2fe4232102bca5c56af0f11e7042f5eaf3d8b2767feb3a8e3ba5668b00e6ea21cb7a215689ac8000000177de54907d16326ff29c3fcd4892afae32043e87ca844929157857093632c4010000019b7cffdaa674beae0f930ebe6085af9093e5fe56b34a5c220ccdcf6efc336fc500e9a435000000000849ee84e1f3daaf226589c05de2bf8bcae8c94001414092ba4569663dacd95921318658f8f40662bcff61fdcbbe08da0938a6c93a6d1075b76c86fa5454ca41f762d3b955b8d6755b79ccaf52754169b69e8904f166f8232102e019359f675526fc8505198647e31ed3044ccb0e5cc2ea22fb3bed5420cdf687ac800001ff076e656f2d6f6e6501f3617958f115913192d1de50c8c5ac2c411a4177c7801554d2bf69c27dac86c7010002e72d286979ee6cb1b7e65dfddfb2e384100b8d148e7758de42e4168b71792c606400000000000000915fe29d2d3847bce516f31fcd33f0fb1d90573be72d286979ee6cb1b7e65dfddfb2e384100b8d148e7758de42e4168b71792c6034ed1a0000000000c85c8dca1fbb7473522109cecaf3acac2e27afc60141406a5ad8c2b6e3783184703d22f3bee39a8b0b6f81477bff61833090361cd52ca5160d66215e6044c31aaed44304a28273c65c9ba736cc75341b45fb18995a6c922321039eae6f12690848807983df6accc1b2929de8582e772be6b3ac084a02a576272eacd10187084d3198000000000008660ceb7bc700000014b3a766ac60afa2990d9251db08138fd1facf07ed0800e3598f01000000209b7cffdaa674beae0f930ebe6085af9093e5fe56b34a5c220ccdcf6efc336fc514e420bbe1e5bcdfa1b43e97a57380a6cd8fddfd4c56c1096d616b654f6666657267bd097b2fcf70e1fd30a5c3ef51e662feeafeba0100000000000000000120e420bbe1e5bcdfa1b43e97a57380a6cd8fddfd4c01659c1aa6f2f42f7d0548a9680df197d4dda767434490e8bed3e25dbbac5206cd000001e72d286979ee6cb1b7e65dfddfb2e384100b8d148e7758de42e4168b71792c6001000000000000007335f929546270b8f811a0f9427b5712457107e70241400f871fcc8bb58d110ea7b1a4c34390542a925203b147bd68137c9edc79044a657cd5dc00514270d65a77a97c814d1f5090054b6c35f1cde698f765538ccf290c2321023056f0a219758bcd503ff4123a589962003331cb9e14168d649ae7426e3ec26eac4140a4036b311b31e0620fcde6e83ed29e6c7e7fcadf59dcac1ccae352a83608c8117dcd36ca64d5a2ce041b2274481ce65b6d30b463d1ca87a0269177d034ad218d232102a1e6ed9a5cff73ad33b7896465af8e9206eab9c8c75502868b783deb64f232eaacd10179207dcc40f47af5b26c52342d5292eb741b7beaa0d58a9a4a1563441fd0f40c30e1349b7cffdaa674beae0f930ebe6085af9093e5fe56b34a5c220ccdcf6efc336fc5b3a766ac60afa2990d9251db08138fd1facf07ed52c10b63616e63656c4f6666657267bd097b2fcf70e1fd30a5c3ef51e662feeafeba01000000000000000001202e5596a5c23eff8907aa180201c5a6f53c041dd101a9822ade57f6f9b6e999be06961af808137f3c9051b6c674bcc9e8aec8c1b6f1000001e72d286979ee6cb1b7e65dfddfb2e384100b8d148e7758de42e4168b71792c6001000000000000007335f929546270b8f811a0f9427b5712457107e7024140b5b58794f8c4f2493a2cff25aa7546a79b6f9e1f279911b2d15a07926084705aeaf64d18454f40feda8ddb9096745eb2e16a208320e9122007fac350892cb3d823210308ec2156f3366339c54c59e4c0342888665abdd76ccbd6b2020961225ccfa3f4ac41404f947ca69b7a2ec0926989f30f3d3d986d488b7c01270eee56326b2f0be856353448062a5d2d73ce34bfdacec3af42b082f1c5186fc063b38c321c1a1eb37157232102a1e6ed9a5cff73ad33b7896465af8e9206eab9c8c75502868b783deb64f232eaacd101792084e25c75cef1e92d39333408dae6e31799d6316b2d908aa094d0dc18f137484c349b7cffdaa674beae0f930ebe6085af9093e5fe56b34a5c220ccdcf6efc336fc5b3a766ac60afa2990d9251db08138fd1facf07ed52c10b63616e63656c4f6666657267bd097b2fcf70e1fd30a5c3ef51e662feeafeba01000000000000000001202e5596a5c23eff8907aa180201c5a6f53c041dd101ead2059c2cf4101cb02a1dae5875dc993189d047a84f05c080318779453b3c0e000001e72d286979ee6cb1b7e65dfddfb2e384100b8d148e7758de42e4168b71792c6001000000000000007335f929546270b8f811a0f9427b5712457107e7024140c4c924181d2b1516467f20fd3cf4b75ce5dd75d916318b77cd9ebd4bf9a3fb3bef47151adb01e0828bc127db6859f11890f86bc5bae2980cba5fcefba0d7edf523210308ec2156f3366339c54c59e4c0342888665abdd76ccbd6b2020961225ccfa3f4ac41407beaabc59bbc4db241aca812b624befcc595d3f8299e996a7d660decd3cb162afc6d810d5e3001aae5874c42b7031b1445f8562c209a2e2726ca1316dffd8867232102a1e6ed9a5cff73ad33b7896465af8e9206eab9c8c75502868b783deb64f232eaacd101530800e1f5050000000014d8dd86f6d91eb2add2f2d8afeda2184ed94ac33314288017f85d80b889fe02beb5ff203ed9ef538f1653c1087472616e7366657267f91d6b7085db7c5aaf09f19eeec1ca3c0db2c6ec00000000000000000220288017f85d80b889fe02beb5ff203ed9ef538f16f015313532373839343337373637356464383030353031000001414061609a0460a3ccfe1a9cb5db9f75811e08d52328f291a1b848ec607718be0a37206a90e3a81908c4b71ec859b684e493c088e640b2e2d471bd370aae50bdf160232102bca5c56af0f11e7042f5eaf3d8b2767feb3a8e3ba5668b00e6ea21cb7a215689ac80000001128218ba3c40a03066c862b0eaad5c06a39a1ae653e08f69e70f528ec4e18dc60500019b7cffdaa674beae0f930ebe6085af9093e5fe56b34a5c220ccdcf6efc336fc500a3e11100000000db2395d79fbd27d7b93b62cccd0fe0afc15ff80b0141404d953b03a1f53911fb1524a9f12126790e5e8468c05ed24ac3f71b881623510fcbe2f5497a4e8d1f1d1748774b9b3487be5f45dbd91bfb697a27ae2d2f2d9bb32321030ab39b99d8675cd9bd90aaec37cba964297cc817078d33e508ab11f1d245c068acd101792017a323cf2b0c650243be29d1b2c3ca69c141fdd7d72a36242c225f175c818a46349b7cffdaa674beae0f930ebe6085af9093e5fe56b34a5c220ccdcf6efc336fc5b3a766ac60afa2990d9251db08138fd1facf07ed52c10b63616e63656c4f6666657267bd097b2fcf70e1fd30a5c3ef51e662feeafeba01000000000000000001202e5596a5c23eff8907aa180201c5a6f53c041dd1010a190ead75c8235b2db69d9eb00040209ce719c3450d90bca893bcfcdbe5e58c000001e72d286979ee6cb1b7e65dfddfb2e384100b8d148e7758de42e4168b71792c6001000000000000007335f929546270b8f811a0f9427b5712457107e7024140b1dbd06a250ee76dc773e7efc915caea48732ca10f813d1027073dbba57032752c0ac7ed30fdb8b18df0abaf5831bd86e444785445253099b46c271676c4a16423210308ec2156f3366339c54c59e4c0342888665abdd76ccbd6b2020961225ccfa3f4ac4140a3b256ac1dd57de358bc1a45b632b3dd41f07d4c6d6e9ffe33fd0cf7c44e29673100bc8a8760727b46bf96642d6dd4a9c4edf657fb2a27b83f564a5b91408ad9232102a1e6ed9a5cff73ad33b7896465af8e9206eab9c8c75502868b783deb64f232eaacd1014e03a0860114d023de91710f63a0259e8d95d1f6563e1572783c14b18f53a903d7873b1453a748c3f80787eca2e30f53c1087472616e7366657267187fc13bec8ff0906c079e7f4cc8276709472913000000000000000004fee36e656f2d6f6e652d696e766f6b653a7b22636f6e7472616374223a22307831333239343730393637323763383463376639653037366339306630386665633362633137663138222c226d6574686f64223a227472616e73666572222c22706172616d73223a5b5b2266726f6d222c22307830666533613265633837303766386333343861373533313433623837643730336139353338666231225d2c5b22746f222c22307833633738373231353365353666366431393538643965323561303633306637313931646532336430225d2c5b2276616c7565222c22302e303031225d5d7dff076e656f2d6f6e65ff0a3233313635343031383420b18f53a903d7873b1453a748c3f80787eca2e30f0000014140910c4b3be37fe09052215f99ef546342c21ded22ef862c165ff0da3ff1087274d0fafb5bbac25aa927dc477b239df732d95afe782a46caeaca669ae096659f13232102c2dbc83931d5e550b95ceab8a94c6af37735fe2aa4e9fb217bce46001937b2f1acd1014e03a0860114de33c5d07f933c0f90da952cdfe2677d3f9a24d714f26e3a75379dc20c8642f3f11e20af76b12065f453c1087472616e7366657267aa67d0447c61bdddc4d1690d2269d772f9c37795000000000000000004fee36e656f2d6f6e652d696e766f6b653a7b22636f6e7472616374223a22307839353737633366393732643736393232306436396431633464646264363137633434643036376161222c226d6574686f64223a227472616e73666572222c22706172616d73223a5b5b2266726f6d222c22307866343635323062313736616632303165663166333432383630636332396433373735336136656632225d2c5b22746f222c22307864373234396133663764363765326466326339356461393030663363393337666430633533336465225d2c5b2276616c7565222c22302e303031225d5d7dff076e656f2d6f6e65ff0933363133393630393120f26e3a75379dc20c8642f3f11e20af76b12065f40000014140b1630c1546547df4b69fdf6c68fcbf9a3f1de2787d0ea7237aea639ab0a227a092147e47194595c0d960df0d209773da32808da767c15730926bcf844ed12f702321021a9f5ed87fe58e7a366c20975d4112698e4c0ccb3ba9cbce0400a482ecf99b67acd1014e03a08601148ba89ee5ab5c4975e0e12f88a8ce4aa8928108c9145e5c739e5d4b5a29af596c2cc162f1facded25fe53c1087472616e7366657267952d12a025325e56a4cb3ba2d469b1e23c7c77a0000000000000000004fee36e656f2d6f6e652d696e766f6b653a7b22636f6e7472616374223a22307861303737376333636532623136396434613233626362613435363565333232356130313232643935222c226d6574686f64223a227472616e73666572222c22706172616d73223a5b5b2266726f6d222c22307866653235656463646661663136326331326336633539616632393561346235643965373335633565225d2c5b22746f222c22307863393038383139326138346163656138383832666531653037353439356361626535396561383862225d2c5b2276616c7565222c22302e303031225d5d7dff076e656f2d6f6e65ff0a33383237333138333238205e5c739e5d4b5a29af596c2cc162f1facded25fe000001414050f556b29417eb44b66b283a72ff33b697f49b2b6d1520eb4fe5a9974632c1b1586b3e7f0aafac4707d65f89daa86592c8ad98c78016769a5de904330d02d2ed23210201008fe0ffcdab73b598c89c6ae2b46d90de38287abd7dd50a325d0bfb2469d5acd1014e03a086011444d65fda3f2062502c03c2bfd85c700a0d046fae149d12fcef2c830d73a570e9b89857962fcf3a619a53c1087472616e7366657267187fc13bec8ff0906c079e7f4cc8276709472913000000000000000004fee36e656f2d6f6e652d696e766f6b653a7b22636f6e7472616374223a22307831333239343730393637323763383463376639653037366339306630386665633362633137663138222c226d6574686f64223a227472616e73666572222c22706172616d73223a5b5b2266726f6d222c22307839613631336163663266393635373938623865393730613537333064383332636566666331323964225d2c5b22746f222c22307861653666303430643061373035636438626663323033326335303632323033666461356664363434225d2c5b2276616c7565222c22302e303031225d5d7dff076e656f2d6f6e65ff0a31353931373638373437209d12fcef2c830d73a570e9b89857962fcf3a619a00000141409ec506aab7045da733d02c8c9ebaa615d9aa02fa8c4d8eb35ad08a3b4843167cdd059094f492f77e68efa2b66d87305e13b3f08a326cf4b3dadaa29aaa64b30a23210275699ef1532219ee8e9d2d8a75b6b96b7cb4d5f9788391de31daceebed9ac8edacd1014e03a086011444bb85601b2d8c5247a1999dfb18ee7928e10cdf1479ed35e989051c8f2404b869c17ce75912de78b953c1087472616e7366657267aa67d0447c61bdddc4d1690d2269d772f9c37795000000000000000004fee36e656f2d6f6e652d696e766f6b653a7b22636f6e7472616374223a22307839353737633366393732643736393232306436396431633464646264363137633434643036376161222c226d6574686f64223a227472616e73666572222c22706172616d73223a5b5b2266726f6d222c22307862393738646531323539653737636331363962383034323438663163303538396539333565643739225d2c5b22746f222c22307864663063653132383739656531386662396439396131343735323863326431623630383562623434225d2c5b2276616c7565222c22302e303031225d5d7dff076e656f2d6f6e65ff0a333738313933373030382079ed35e989051c8f2404b869c17ce75912de78b9000001414081a2d21af431df3345478836ae24dc7714ce647daa1daac3a99ed17645a8c86a981d86c5d2280af45259a1cf4a85c115ecbe8ff5a398c0bea23c218c0c8c26f02321020be10b4bddffe752a7cfaa16d1718ce6da460608ca41ad9bf7ff66dc5f60c860acd10179201322db68df23cee8da600e7cf5875a8d919bf8e318bbe7afbcd1fdbd5758d67b349b7cffdaa674beae0f930ebe6085af9093e5fe56b34a5c220ccdcf6efc336fc5b3a766ac60afa2990d9251db08138fd1facf07ed52c10b63616e63656c4f6666657267bd097b2fcf70e1fd30a5c3ef51e662feeafeba01000000000000000001202e5596a5c23eff8907aa180201c5a6f53c041dd10181c3670591e333a19a14aae556d211f3490163380c1f28ef55b0bb0e4a8aec1a000001e72d286979ee6cb1b7e65dfddfb2e384100b8d148e7758de42e4168b71792c6001000000000000007335f929546270b8f811a0f9427b5712457107e7024140dd2aa6075329f43375655c83b04d343974fb13908888fa6b242b599c789c1cf635b530b6834093332720f93d185b450884951acfb261aec61fe9057efbdeb4b523210308ec2156f3366339c54c59e4c0342888665abdd76ccbd6b2020961225ccfa3f4ac4140f152dc9454ebf518bc0ab8192543add5844ad7007177986a3dd038db313740ea72abb5d49866caa4d470d75182a7e715fd2b9a6e345ef2568bf82e92878743db232102a1e6ed9a5cff73ad33b7896465af8e9206eab9c8c75502868b783deb64f232eaac8000000198f4ec54f46fd86c0955accb7bda7ff06ffc10c45651206fbcbfeabe78c597750000019b7cffdaa674beae0f930ebe6085af9093e5fe56b34a5c220ccdcf6efc336fc500e40b54020000002fb7e583c973498fef06f317a9762507e7ac0306014140c4a7388365a4e8a93e2229fd617805902773a8ef7354e95487819eec59379a6a8fb045ed126025db151336e2393eaf75dd524f1ebe872f92185f8d1a6a71a6a5232102b37eaec8631a5bb8579d4ba268e2fbc9c81b555f08558bba6eccb9d1448332c5ac02000155810e77f1dd89622915f3fceb051ac8abeaa2e7ea7a2c38e0744137c3c8982b0000000001e72d286979ee6cb1b7e65dfddfb2e384100b8d148e7758de42e4168b71792c605e1502000000000024fdf844d637cf5a60a9744f1ab8ab8d6f2c18cf0141407dafb244fceee305d477892a21ca8cbd31906fbcc233e0bfccd0fe91d4f6dafdd067f7301cd3e8c7c6197f71615df4b7cedb987eb3ebff79ad729bb8768e1e50232102f8a4a73deefad4c114039b854cc4e974c409f30720e2567d15dea2204b8c45f7acd1012000c108776974686472617769bd097b2fcf70e1fd30a5c3ef51e662feeafeba01000000000000000003a15100000000000000000000000000000000000000000000000000000000000000a232e125258b7db0a0dffde5bd03b2b859253538ab000000000000000000000000a48098c835c493eb3b1967e44150630cc6435e564e00000000000000000000000001dfeee6c60db6403b5538e86120f01e60dcea749c73ce36b9058f050be8b7d2c9000001e72d286979ee6cb1b7e65dfddfb2e384100b8d148e7758de42e4168b71792c600100000000000000bd097b2fcf70e1fd30a5c3ef51e662feeafeba010102000000d101530800e1f5050000000014d8dd86f6d91eb2add2f2d8afeda2184ed94ac33314288017f85d80b889fe02beb5ff203ed9ef538f1653c1087472616e7366657267f56c89be8bfcdec617e2402b5c3fd5b6d71b820d00000000000000000220288017f85d80b889fe02beb5ff203ed9ef538f16f0153135323738393433383433393264623331653662630000014140654af3a0cc69faa3dd42ff76c4012aa9c72e269bba004d6e910f195e33b2ecae980be4531a3677f27d3c90f4196632790997078bca4f8471c6db43b55928c3ef232102bca5c56af0f11e7042f5eaf3d8b2767feb3a8e3ba5668b00e6ea21cb7a215689ac" + rawBlockBytes, _ := hex.DecodeString(rawBlock) + + b := Block{} + + r := bytes.NewReader(rawBlockBytes) + b.Decode(r) + + expected := []struct { + ID string + Type string + Size int + Version int + InputsLen int + OutputsLen int + AttributesLen int + WitnessesLen int + }{ // 20 trans + {ID: "f59b04d8e6526684b94b5f8cdbdf691feaff5d45e9aa8e2325a668f1b9130786", Type: "MinerTransaction", Size: 10, Version: 0, InputsLen: 0, OutputsLen: 0, AttributesLen: 0, WitnessesLen: 0}, + {ID: "7463345f771e70019185d72fa5bd00fbb4f26735daae398ecc6540419332d81e", Type: "InvocationTransaction", Size: 244, Version: 1, InputsLen: 0, OutputsLen: 0, AttributesLen: 2, WitnessesLen: 1}, + {ID: "cf3aeda21d320ec9b49d322f2b88fea21aa7e9bf243c1e02dfe08f5cc82b74b0", Type: "ContractTransaction", Size: 202, Version: 0, InputsLen: 1, OutputsLen: 1, AttributesLen: 0, WitnessesLen: 1}, + {ID: "07e502d13ae6255cfabbc9ee2f78a48fc1c43a4f7f713f128342db721bc01af5", Type: "ContractTransaction", Size: 271, Version: 0, InputsLen: 1, OutputsLen: 2, AttributesLen: 1, WitnessesLen: 1}, + {ID: "0de0baf53136c188bdd179fed9530dfb7dd80697fd59e47ffe294db4f421eb67", Type: "InvocationTransaction", Size: 469, Version: 1, InputsLen: 1, OutputsLen: 1, AttributesLen: 1, WitnessesLen: 2}, + {ID: "233c8b00ab6a43aafae7fcc2be47fc46493185bb3376160b5809cb745aee3329", Type: "InvocationTransaction", Size: 455, Version: 1, InputsLen: 1, OutputsLen: 1, AttributesLen: 1, WitnessesLen: 2}, + {ID: "8bf3ae0c692fc830753029fcb6575625ea8181b444cffcbe38404a28b77b3856", Type: "InvocationTransaction", Size: 455, Version: 1, InputsLen: 1, OutputsLen: 1, AttributesLen: 1, WitnessesLen: 2}, + {ID: "c5285e1460191e1ca7fc07e3c26c5facebb033d56b63b7a41ebf11f2a1cb4306", Type: "InvocationTransaction", Size: 244, Version: 1, InputsLen: 0, OutputsLen: 0, AttributesLen: 2, WitnessesLen: 1}, + {ID: "dce8b5f6dc093a910e405a230e9b7d546688d411cf960c8a1cc7d386d89b56d6", Type: "ContractTransaction", Size: 202, Version: 0, InputsLen: 1, OutputsLen: 1, AttributesLen: 0, WitnessesLen: 1}, + {ID: "4cce087cadfa99c2adeaaf1916ada025db124cef8f05d4535b0ad8047ef7d29e", Type: "InvocationTransaction", Size: 455, Version: 1, InputsLen: 1, OutputsLen: 1, AttributesLen: 1, WitnessesLen: 2}, + {ID: "67df57a20c9d3b2942925f2c66fdc15a21be2c229a22122f6acbdac4dd10bf0a", Type: "InvocationTransaction", Size: 466, Version: 1, InputsLen: 0, OutputsLen: 0, AttributesLen: 4, WitnessesLen: 1}, + {ID: "ad51030b30e016293caed92781b3bb3f993f86c15ab1153582f658d603fe23db", Type: "InvocationTransaction", Size: 465, Version: 1, InputsLen: 0, OutputsLen: 0, AttributesLen: 4, WitnessesLen: 1}, + {ID: "1db2d62ad3530f1ae6ca7bd95e766beaff97058681f0e203d8744d7bba065012", Type: "InvocationTransaction", Size: 466, Version: 1, InputsLen: 0, OutputsLen: 0, AttributesLen: 4, WitnessesLen: 1}, + {ID: "7d7bb6f0db6a71aca85fc9267fa6a59654b00b5f778a39c27214c68f11950f61", Type: "InvocationTransaction", Size: 466, Version: 1, InputsLen: 0, OutputsLen: 0, AttributesLen: 4, WitnessesLen: 1}, + {ID: "1d534dcf1ce63a9ea9328eab309891ea2a0a5cb11e95cabf22860ee1fb649521", Type: "InvocationTransaction", Size: 466, Version: 1, InputsLen: 0, OutputsLen: 0, AttributesLen: 4, WitnessesLen: 1}, + {ID: "8f4c9089871a4ad0076b27c061395079e0862f685f27e4bc01b7bac67b0cf8d0", Type: "InvocationTransaction", Size: 455, Version: 1, InputsLen: 1, OutputsLen: 1, AttributesLen: 1, WitnessesLen: 2}, + {ID: "52b4653fca02e8042092456490036f0b9b18b339f65d9c334e7e9d2b4599f8db", Type: "ContractTransaction", Size: 202, Version: 0, InputsLen: 1, OutputsLen: 1, AttributesLen: 0, WitnessesLen: 1}, + {ID: "2b4854b1f46c9af0eb06587fd375355adfeea3c8f5921295421251af93d737e1", Type: "ClaimTransaction", Size: 203, Version: 0, InputsLen: 0, OutputsLen: 1, AttributesLen: 0, WitnessesLen: 1}, + {ID: "61e95e5b14625e897423670dfc3babf021d6b99ca2a73203dd1ac2604a2daadf", Type: "InvocationTransaction", Size: 244, Version: 1, InputsLen: 1, OutputsLen: 1, AttributesLen: 3, WitnessesLen: 1}, + {ID: "b361dfec8c2cde980b340d2c3ec63cecaea634f91b6d76f24a586aa60fbde483", Type: "InvocationTransaction", Size: 244, Version: 1, InputsLen: 0, OutputsLen: 0, AttributesLen: 2, WitnessesLen: 1}, + } + + for i, tx := range b.Txs { + txID, err := tx.ID() + assert.Equal(t, nil, err) + assert.Equal(t, expected[i].ID, txID.ReverseString()) + + assert.Equal(t, expected[i].Size, len(tx.BaseTx().Bytes())) + assert.Equal(t, expected[i].Type, tx.BaseTx().Type.String()) + assert.Equal(t, expected[i].Version, int(tx.BaseTx().Version)) + assert.Equal(t, expected[i].InputsLen, len(tx.BaseTx().Inputs)) + assert.Equal(t, expected[i].OutputsLen, len(tx.BaseTx().Outputs)) + assert.Equal(t, expected[i].AttributesLen, len(tx.BaseTx().Attributes)) + assert.Equal(t, expected[i].WitnessesLen, len(tx.BaseTx().Witnesses)) + } + + assert.Equal(t, len(expected), len(b.Txs)) + + // Block specific tests + assert.Equal(t, 0, int(b.Version)) + assert.Equal(t, "f4889276813c65c059cb54612e9e51b1b8fd91ee799e03b638bfade812df33ba", b.PrevHash.ReverseString()) + assert.Equal(t, "ef7241eb3dc1df2c95dc9bed9dea2814b62e61286c22d77e07847a9b109224a2", b.MerkleRoot.ReverseString()) + assert.Equal(t, 1527894405, int(b.Timestamp)) + assert.Equal(t, 2340363, int(b.Index)) + + nextConsensus, err := address.FromUint160(b.NextConsensus) + assert.Equal(t, nil, err) + assert.Equal(t, "APyEx5f4Zm4oCHwFWiSTaph1fPBxZacYVR", nextConsensus) + + assert.Equal(t, "4012afae6df64195041e4764b57caa9e27fc2cfc596833163904136ec95816d104b44b3737d0e9f6b1b4445cd3b6a5cc80f6b0935675bc44dba44415eb309832b3404dc95bcf85e4635556a1d618e4ce947b26972992ed74788df5f9501b850ac0b40b7112d1ff30e4ade00369e16f0d13932d1ba76725e7682db072f8e2cd7752b840d12bb7dd45dd3b0e2098db5c67b6de55b7c40164937491fcaca1239b25860251224ead23ab232add78ccccd347239eae50ffc98f50b2a84c60ec5c3d284647a7406fabf6ca241b759af6b71080c0dfad7395632e989226a7e52f8cd2c133aeb2226e6e1aea47666fd81f578405a9f9bbd9d0bc523c3a44d7a5099ddc649feabe5f406188b8ee478731a89beeb76fdbd108eb0071b8f2b8678f40c5a1f387a491314336783255dee8cc5af4bf914dfeaacecc318fc13e02262658e39e8ce0631941b1", hex.EncodeToString(b.Witness.InvocationScript)) + assert.Equal(t, "552102486fd15702c4490a26703112a5cc1d0923fd697a33406bd5a1c00e0013b09a7021024c7b7fb6c310fccf1ba33b082519d82964ea93868d676662d4a59ad548df0e7d2102aaec38470f6aad0042c6e877cfd8087d2676b0f516fddd362801b9bd3936399e2103b209fd4f53a7170ea4444e0cb0a6bb6a53c2bd016926989cf85f9b0fba17a70c2103b8d9d5771d8f513aa0869b9cc8d50986403b78c6da36890638c3d46a5adce04a2102ca0e27697b9c248f6f16e085fd0061e26f44da85b58ee835c110caa5ec3ba5542102df48f60e8f3e01c48ff40b9b7f1310d7a8b2a193188befe1c2e3df740e89509357ae", hex.EncodeToString(b.Witness.VerificationScript)) + assert.Equal(t, "0006d3ff96e269f599eb1b5c5a527c218439e498dcc65b63794591bbcdc0516b", b.Hash.ReverseString()) + + bb, err := b.Bytes() + assert.Equal(t, nil, err) + + // test size of the block + assert.Equal(t, 7360, len(bb)) + + buf := new(bytes.Buffer) + + b.Encode(buf) + + assert.Equal(t, rawBlock, hex.EncodeToString(buf.Bytes())) +} diff --git a/pkg/wire/payload/transaction/base.go b/pkg/wire/payload/transaction/base.go index 541e958e4..567e907d8 100644 --- a/pkg/wire/payload/transaction/base.go +++ b/pkg/wire/payload/transaction/base.go @@ -18,11 +18,8 @@ type decodeExclusiveFields func(br *util.BinReader) type Transactioner interface { Encode(w io.Writer) error Decode(r io.Reader) error + BaseTx() *Base ID() (util.Uint256, error) - Bytes() []byte - UTXOs() []*Output - TXOs() []*Input - Witness() []*Witness } // Base transaction is the template for all other transactions @@ -199,17 +196,7 @@ func (b *Base) Bytes() []byte { return buf.Bytes() } -// UTXOs returns the outputs in the tx -func (b *Base) UTXOs() []*Output { - return b.Outputs -} - -// TXOs returns the inputs in the tx -func (b *Base) TXOs() []*Input { - return b.Inputs -} - -// Witness returns the witnesses in the tx -func (b *Base) Witness() []*Witness { - return b.Witnesses +// BaseTx returns the Base object in a transaction +func (b *Base) BaseTx() *Base { + return b } diff --git a/pkg/wire/util/address/address.go b/pkg/wire/util/address/address.go index 84daf5345..12f09c63a 100644 --- a/pkg/wire/util/address/address.go +++ b/pkg/wire/util/address/address.go @@ -4,6 +4,7 @@ import ( "encoding/hex" "github.com/CityOfZion/neo-go/pkg/crypto/base58" + "github.com/CityOfZion/neo-go/pkg/wire/util" ) // ToScriptHash converts an address to a script hash @@ -17,3 +18,21 @@ func ToScriptHash(address string) string { scriptHash := (decodedAddressAsHex[2:42]) return scriptHash } + +// FromUint160 returns the "NEO address" from the given +// Uint160. +func FromUint160(u util.Uint160) (string, error) { + // Dont forget to prepend the Address version 0x17 (23) A + b := append([]byte{0x17}, u.Bytes()...) + return base58.CheckEncode(b) +} + +// Uint160Decode attempts to decode the given NEO address string +// into an Uint160. +func Uint160Decode(s string) (u util.Uint160, err error) { + b, err := base58.CheckDecode(s) + if err != nil { + return u, err + } + return util.Uint160DecodeBytes(b[1:21]) +}