1// SPDX-License-Identifier: ISC 2// Copyright (c) 2014-2020 Bitmark Inc. 3// Use of this source code is governed by an ISC 4// license that can be found in the LICENSE file. 5 6package storage_test 7 8import ( 9 "encoding/binary" 10 "testing" 11 12 "github.com/stretchr/testify/assert" 13 14 "github.com/bitmark-inc/bitmarkd/storage" 15 "github.com/bitmark-inc/bitmarkd/transactionrecord" 16) 17 18const ( 19 number = 1234 20 21 assetID = 5 22 blockHeaderID = 2 23 blockID = 7 24 blockOwnerTXID = 3 25 blockOwnerPaymentID = 6 26 ownerDataID = 10 27 ownerListID = 1 28 ownerTXID = 9 29 ownerNextCountID = 4 30 shareQuantityID = 12 31 sharesID = 11 32 txID = 8 33) 34 35var ( 36 packedAsset = transactionrecord.Packed{assetID} 37 packedBlockHeader = transactionrecord.Packed{0, 0, 0, 0, 0, 0, 0, 0, blockHeaderID} 38 packedBlock = transactionrecord.Packed{0, 0, 0, 0, 0, 0, 0, 0, blockID} 39 packedBlockOwnerTX = transactionrecord.Packed{0, 0, 0, 0, 0, 0, 0, 0, blockOwnerTXID} 40 packedBlockOwnerPayment = transactionrecord.Packed{0, 0, 0, 0, 0, 0, 0, 0, blockOwnerPaymentID} 41 packedOwnerData = transactionrecord.Packed{0, 0, 0, 0, 0, 0, 0, 0, ownerDataID} 42 packedOwnerList = transactionrecord.Packed{0, 0, 0, 0, 0, 0, 0, 0, ownerListID} 43 packedOwnerTXID = transactionrecord.Packed{0, 0, 0, 0, 0, 0, 0, 0, ownerTXID} 44 packedOwnerNextCount = transactionrecord.Packed{0, 0, 0, 0, 0, 0, 0, 0, ownerNextCountID} 45 packedShareQuantity = transactionrecord.Packed{0, 0, 0, 0, 0, 0, 0, 0, shareQuantityID} 46 packedShares = transactionrecord.Packed{0, 0, 0, 0, 0, 0, 0, 0, sharesID} 47 packedTransaction = transactionrecord.Packed{txID} 48 49 blockNumber []byte 50) 51 52func init() { 53 blockNumber = make([]byte, 8) 54 binary.BigEndian.PutUint64(blockNumber, uint64(number)) 55 packedTransaction = transactionrecord.Packed{txID} 56 packedAsset = transactionrecord.Packed{assetID} 57} 58 59func setupTransaction() storage.Transaction { 60 trx, _ := storage.NewDBTransaction() 61 return trx 62} 63 64func TestAssetsPut(t *testing.T) { 65 trx := setupTransaction() 66 pool := storage.Pool.Assets 67 trx.Put(pool, []byte{assetID}, blockNumber, packedAsset) 68 _ = trx.Commit() 69 70 data, key := trx.GetNB(pool, []byte{assetID}) 71 72 tempData := make([]byte, 8) 73 copy(tempData, blockNumber) 74 expected := binary.BigEndian.Uint64(tempData[:]) 75 76 assert.Equal(t, expected, data, "wrong asset data") 77 assert.Equal(t, []byte{assetID}, key, "wrong asset key") 78} 79 80func TestTransactionsPut(t *testing.T) { 81 trx := setupTransaction() 82 pool := storage.Pool.Transactions 83 trx.Put(pool, []byte{txID}, blockNumber, packedTransaction) 84 _ = trx.Commit() 85 86 data, key := trx.GetNB(pool, []byte{txID}) 87 88 tempData := make([]byte, 8) 89 copy(tempData, blockNumber) 90 expected := binary.BigEndian.Uint64(tempData[:]) 91 92 assert.Equal(t, expected, data, "wrong transaction data") 93 assert.Equal(t, []byte{txID}, key, "wrong transaction key") 94} 95 96func TestBlockPut(t *testing.T) { 97 trx := setupTransaction() 98 pool := storage.Pool.Blocks 99 trx.Put(pool, []byte{blockID}, packedBlock, []byte{}) 100 _ = trx.Commit() 101 102 data, key := trx.GetNB(pool, []byte{blockID}) 103 104 tempData := make([]byte, 9) 105 copy(tempData, packedBlock) 106 expected := binary.BigEndian.Uint64(tempData[:]) 107 108 assert.Equal(t, expected, data, "wrong block data") 109 assert.Equal(t, []byte{blockID}, key, "wrong block key") 110} 111 112func TestBlockHeaderHashPut(t *testing.T) { 113 trx := setupTransaction() 114 pool := storage.Pool.BlockHeaderHash 115 trx.Put(pool, []byte{blockHeaderID}, packedBlockHeader, []byte{}) 116 _ = trx.Commit() 117 118 data, key := trx.GetNB(pool, []byte{blockHeaderID}) 119 120 tempData := make([]byte, 9) 121 copy(tempData, packedBlockHeader) 122 expected := binary.BigEndian.Uint64(tempData[:]) 123 124 assert.Equal(t, expected, data, "wrong block header data") 125 assert.Equal(t, []byte{blockHeaderID}, key, "wrong block header key") 126} 127 128func TestBlockOwnerPaymentPut(t *testing.T) { 129 trx := setupTransaction() 130 pool := storage.Pool.BlockOwnerPayment 131 trx.Put(pool, []byte{blockOwnerPaymentID}, packedBlockOwnerPayment, []byte{}) 132 _ = trx.Commit() 133 134 data, key := trx.GetNB(pool, []byte{blockOwnerPaymentID}) 135 136 tempData := make([]byte, 9) 137 copy(tempData, packedBlockOwnerPayment) 138 expected := binary.BigEndian.Uint64(tempData[:]) 139 140 assert.Equal(t, expected, data, "wrong block owner payment data") 141 assert.Equal(t, []byte{blockOwnerPaymentID}, key, "wrong block owner payment key") 142} 143 144func TestBlockOwnerTXPut(t *testing.T) { 145 trx := setupTransaction() 146 pool := storage.Pool.BlockOwnerPayment 147 trx.Put(pool, []byte{blockOwnerTXID}, packedBlockOwnerTX, []byte{}) 148 _ = trx.Commit() 149 150 data, key := trx.GetNB(pool, []byte{blockOwnerTXID}) 151 152 tempData := make([]byte, 9) 153 copy(tempData, packedBlockOwnerTX) 154 expected := binary.BigEndian.Uint64(tempData[:]) 155 156 assert.Equal(t, expected, data, "wrong block owner tx data") 157 assert.Equal(t, []byte{blockOwnerTXID}, key, "wrong block owner tx key") 158} 159 160func TestOwnerNextCountPut(t *testing.T) { 161 trx := setupTransaction() 162 pool := storage.Pool.OwnerNextCount 163 trx.Put(pool, []byte{ownerNextCountID}, packedOwnerNextCount, []byte{}) 164 _ = trx.Commit() 165 166 data, key := trx.GetNB(pool, []byte{ownerNextCountID}) 167 168 tempData := make([]byte, 9) 169 copy(tempData, packedOwnerNextCount) 170 expected := binary.BigEndian.Uint64(tempData[:]) 171 172 assert.Equal(t, expected, data, "wrong owner next count data") 173 assert.Equal(t, []byte{ownerNextCountID}, key, "wrong owner next count key") 174} 175 176func TestOwnerListPut(t *testing.T) { 177 trx := setupTransaction() 178 pool := storage.Pool.OwnerList 179 trx.Put(pool, []byte{ownerListID}, packedOwnerList, []byte{}) 180 _ = trx.Commit() 181 182 data, key := trx.GetNB(pool, []byte{ownerListID}) 183 184 tempData := make([]byte, 9) 185 copy(tempData, packedOwnerList) 186 expected := binary.BigEndian.Uint64(tempData[:]) 187 188 assert.Equal(t, expected, data, "wrong owner list data") 189 assert.Equal(t, []byte{ownerListID}, key, "wrong owner list key") 190} 191 192func TestOwnerTXIDPut(t *testing.T) { 193 trx := setupTransaction() 194 pool := storage.Pool.OwnerTxIndex 195 trx.Put(pool, []byte{ownerTXID}, packedOwnerTXID, []byte{}) 196 _ = trx.Commit() 197 198 data, key := trx.GetNB(pool, []byte{ownerTXID}) 199 200 tempData := make([]byte, 9) 201 copy(tempData, packedOwnerTXID) 202 expected := binary.BigEndian.Uint64(tempData[:]) 203 204 assert.Equal(t, expected, data, "wrong owner tx id data") 205 assert.Equal(t, []byte{ownerTXID}, key, "wrong owner tx id key") 206} 207 208func TestOwnerDataPut(t *testing.T) { 209 trx := setupTransaction() 210 pool := storage.Pool.OwnerData 211 trx.Put(pool, []byte{ownerDataID}, packedOwnerData, []byte{}) 212 _ = trx.Commit() 213 214 data, key := trx.GetNB(pool, []byte{ownerDataID}) 215 216 tempData := make([]byte, 9) 217 copy(tempData, packedOwnerData) 218 expected := binary.BigEndian.Uint64(tempData[:]) 219 220 assert.Equal(t, expected, data, "wrong owner data") 221 assert.Equal(t, []byte{ownerDataID}, key, "wrong owner data key") 222} 223 224func TestSharePut(t *testing.T) { 225 trx := setupTransaction() 226 pool := storage.Pool.Shares 227 trx.Put(pool, []byte{sharesID}, packedShares, []byte{}) 228 _ = trx.Commit() 229 230 data, key := trx.GetNB(pool, []byte{sharesID}) 231 232 tempData := make([]byte, 9) 233 copy(tempData, packedShares) 234 expected := binary.BigEndian.Uint64(tempData[:]) 235 236 assert.Equal(t, expected, data, "wrong share data") 237 assert.Equal(t, []byte{sharesID}, key, "wrong share key") 238} 239 240func TestShareQuantityPut(t *testing.T) { 241 trx := setupTransaction() 242 pool := storage.Pool.ShareQuantity 243 trx.Put(pool, []byte{shareQuantityID}, packedShareQuantity, []byte{}) 244 _ = trx.Commit() 245 246 data, key := trx.GetNB(pool, []byte{shareQuantityID}) 247 248 tempData := make([]byte, 9) 249 copy(tempData, packedShareQuantity) 250 expected := binary.BigEndian.Uint64(tempData[:]) 251 252 assert.Equal(t, expected, data, "wrong share quantity data") 253 assert.Equal(t, []byte{shareQuantityID}, key, "wrong share quantity key") 254} 255