package core import ( "bytes" "crypto/sha256" "encoding/hex" "testing" "github.com/CityOfZion/neo-go/pkg/core/transaction" "github.com/CityOfZion/neo-go/pkg/util" "github.com/stretchr/testify/assert" ) func TestDecodeBlock(t *testing.T) { var ( rawBlock = "00000000b7def681f0080262aa293071c53b41fc3146b196067243700b68acd059734fd19543108bf9ddc738cbee2ed1160f153aa0d057f062de0aa3cbb64ba88735c23d43667e59543f050095df82b02e324c5ff3812db982f3b0089a21a278988efeec6a027b2501fd450140113ac66657c2f544e8ad13905fcb2ebaadfef9502cbefb07960fbe56df098814c223dcdd3d0efa0b43a9459e654d948516dcbd8b370f50fbecfb8b411d48051a408500ce85591e516525db24065411f6a88f43de90fa9c167c2e6f5af43bc84e65e5a4bb174bc83a19b6965ff10f476b1b151ae15439a985f33916abc6822b0bb140f4aae522ffaea229987a10d01beec826c3b9a189fe02aa82680581b78f3df0ea4d3f93ca8ea35ffc90f15f7db9017f92fafd9380d9ba3237973cf4313cf626fc40e30e50e3588bd047b39f478b59323868cd50c7ab54355d8245bf0f1988d37528f9bbfc68110cf917debbdbf1f4bdd02cdcccdc3269fdf18a6c727ee54b6934d840e43918dd1ec6123550ec37a513e72b34b2c2a3baa510dec3037cbef2fa9f6ed1e7ccd1f3f6e19d4ce2c0919af55249a970c2685217f75a5589cf9e54dff8449af155210209e7fd41dfb5c2f8dc72eb30358ac100ea8c72da18847befe06eade68cebfcb9210327da12b5c40200e9f65569476bbff2218da4f32548ff43b6387ec1416a231ee821034ff5ceeac41acf22cd5ed2da17a6df4dd8358fcb2bfb1a43208ad0feaab2746b21026ce35b29147ad09e4afe4ec4a7319095f08198fa8babbe3c56e970b143528d2221038dddc06ce687677a53d54f096d2591ba2302068cf123c1f2d75c2dddc542557921039dafd8571a641058ccc832c5e2111ea39b09c0bde36050914384f7a48bce9bf92102d02b1873a0863cd042cc717da31cea0d7cf9db32b74d4c72c01b0011503e2e2257ae01000095df82b000000000" rawBlockHash = "922ba0c0d06afbeec4c50b0541a29153feaa46c5d7304e7bf7f40870d9f3aeb0" rawBlockPrevHash = "d14f7359d0ac680b7043720696b14631fc413bc5713029aa620208f081f6deb7" rawBlockIndex = 343892 rawBlockTimestamp = 1501455939 rawBlockConsensusData = 6866918707944415125 ) rawBlockBytes, err := hex.DecodeString(rawBlock) if err != nil { t.Fatal(err) } block := &Block{} if err := block.DecodeBinary(bytes.NewReader(rawBlockBytes)); err != nil { t.Fatal(err) } assert.Equal(t, uint32(rawBlockIndex), block.Index) assert.Equal(t, uint32(rawBlockTimestamp), block.Timestamp) assert.Equal(t, uint64(rawBlockConsensusData), block.ConsensusData) assert.Equal(t, rawBlockPrevHash, block.PrevHash.String()) assert.Equal(t, rawBlockHash, block.Hash().String()) } func TestHashBlockEqualsHashHeader(t *testing.T) { block := newBlock(0) assert.Equal(t, block.Hash(), block.Header().Hash()) } func TestBlockVerify(t *testing.T) { block := newBlock( 0, newTX(transaction.MinerType), newTX(transaction.IssueType), ) if !block.Verify(false) { t.Fatal("block should be verified") } block.Transactions = []*transaction.Transaction{ {Type: transaction.IssueType}, {Type: transaction.MinerType}, } if block.Verify(false) { t.Fatal("block should not by verified") } block.Transactions = []*transaction.Transaction{ {Type: transaction.MinerType}, {Type: transaction.MinerType}, } if block.Verify(false) { t.Fatal("block should not by verified") } } func newBlockBase() BlockBase { return BlockBase{ Version: 0, PrevHash: sha256.Sum256([]byte("a")), MerkleRoot: sha256.Sum256([]byte("b")), Timestamp: 999, Index: 1, ConsensusData: 1111, NextConsensus: util.Uint160{}, Script: &transaction.Witness{ VerificationScript: []byte{0x0}, InvocationScript: []byte{0x1}, }, } }