How to use FromString method of hash Package

Best Syzkaller code snippet using hash.FromString

transaction-storage_int_test.go

Source:transaction-storage_int_test.go Github

copy

Full Screen

...25 defer helper.Cleanup(currency, db)26 s := NewTransactionStorage(db)27 ctx := context.Background()28 transactions := []*model.Transaction{29 {Hash: "op5AGD3VrzgdzwTk7eNMGYEoQS6Zcsz6PWyYMk5kNvqSumDZReW", BlockNumber: helper.FromUint64(560500), SourceAddress: helper.FromString("tz1SYq214SCBy9naR6cvycQsYcUGpBqQAE8d"), DestinationAddress: helper.FromString("tz1ihCKcZ8iRxK1NX35u5xXvGRvnDVCvfPu1"), Amount: big.NewInt(10), Timestamp: nowRounded(), CreatedAt: nowRounded()},30 {Hash: "ooXh2FstoqHnXD9Kqu7CVWtrs8VNVN2u3XyCnked7v38kjKVdyQ", BlockNumber: helper.FromUint64(560501), SourceAddress: helper.FromString("tz1bY8g2N558B2SoyriM5WeGsXSWtaf6qHP2"), DestinationAddress: helper.FromString("tz1Y6YwdGnrdTZ2NCiA6PDmRDk3BJUXXySrS"), Amount: big.NewInt(20), Timestamp: nowRounded(), CreatedAt: nowRounded()},31 {Hash: "op3WBRzqfayJEbv7ApBkTBjHfqxRSoEwrpm16SjMn8wrUXiPjPc", BlockNumber: helper.FromUint64(560502), SourceAddress: helper.FromString("tz1ihCKcZ8iRxK1NX35u5xXvGRvnDVCvfPu1"), DestinationAddress: helper.FromString("tz1TTFaFiW7xywionjQe9wxUrtk3cgHUDKix"), Amount: big.NewInt(30), Timestamp: nowRounded(), CreatedAt: nowRounded()},32 {Hash: "oohNnXjgArNuBSU3HQRkd9RDmaaUE9LpZne6qDdPfeV6ddNh971", BlockNumber: helper.FromUint64(560503), SourceAddress: helper.FromString("tz1MTRbdWuVQh4ZyYnSkp7x2t8oXQHFHu9nR"), DestinationAddress: helper.FromString("tz1hkzS6pnfnHv9KzX1nbtqXVqUkzcem8FJs"), Amount: big.NewInt(40), Timestamp: nowRounded(), CreatedAt: nowRounded()},33 {Hash: "oocNzaGtRSa8VduCVYmSZBLqQvuPbV978aFtTNGPRbc3Y7Stbob", BlockNumber: helper.FromUint64(560504), SourceAddress: helper.FromString("tz1MXjdb684ByEP5qUn5J7EMub7Sr8eBziDe"), DestinationAddress: helper.FromString("tz1Pb2py4QrhS1u3KFqb6amZz87fCCcoLLFz"), Amount: big.NewInt(50), Timestamp: nowRounded(), CreatedAt: nowRounded()},34 {Hash: "onu4xNr7NTUxGHPRGMQQrm5CD3CncDHLFvgNnHcWxkRn7QSdaDJ", BlockNumber: helper.FromUint64(560505), SourceAddress: helper.FromString("tz1SiPXX4MYGNJNDsRc7n8hkvUqFzg8xqF9m"), DestinationAddress: helper.FromString("tz1Ti7JwCpVHXcxwLTo4nBf3h6ivosNsD5uJ"), Amount: big.NewInt(60), Timestamp: nowRounded(), CreatedAt: nowRounded()},35 {Hash: "ooUsUE2SCwZCMwjmEgSSP8s3u2LhLtAG45j8hN2FHNEPLLULuss", BlockNumber: helper.FromUint64(560506), SourceAddress: helper.FromString("tz1XFTtQKCUfZkE8nWpJEdFgy3PADSUio9fA"), DestinationAddress: helper.FromString("tz1ebZv2hxy1ABATkkgEajfgkjX9RuNJtTnY"), Amount: big.NewInt(70), Timestamp: nowRounded(), CreatedAt: nowRounded()},36 {Hash: "opPZPWPvvsKScmYxwSKTymxx8Gw7gjhjaCNU4QboZeuJ8vaiAnU", BlockNumber: helper.FromUint64(560507), SourceAddress: helper.FromString("tz1VwmmesDxud2BJEyDKUTV5T5VEP8tGBKGD"), DestinationAddress: helper.FromString("tz1gNjyzyT8L6WgNS4AdNMppsSFw76J4aDvT"), Amount: big.NewInt(80), Timestamp: nowRounded(), CreatedAt: nowRounded()},37 {Hash: "ooJdn5tdzhC8YZSsKdphaYN1Jc7nS3GHNZ4eikz4Vzp1q6HS5d1", BlockNumber: helper.FromUint64(560508), SourceAddress: helper.FromString("tz1YCiftUM16FriwePPRx6V8A15ugAM5SXtr"), DestinationAddress: helper.FromString("tz1QF3YZLCVohjw2NLUEzKqswAh1TQt9MMhq"), Amount: big.NewInt(90), Timestamp: nowRounded(), CreatedAt: nowRounded()},38 {Hash: "ootwyMERpZfjAxgVbcBXfnLX5CeFPbKd13nMaP51G963gqkfrm2", BlockNumber: helper.FromUint64(560509), SourceAddress: helper.FromString("tz1bY8g2N558B2SoyriM5WeGsXSWtaf6qHP2"), DestinationAddress: helper.FromString("tz1bDhCGNZLQw1QXgf6MCzo6EtAVSGkqEB11"), Amount: big.NewInt(100), Timestamp: nowRounded(), CreatedAt: nowRounded()},39 }40 err := s.CreateTransactions(ctx, transactions)41 require.Nil(t, err)42}43func TestCreateTransactionsConflict(t *testing.T) {44 var db = helper.Setup(currency)45 defer helper.Cleanup(currency, db)46 s := NewTransactionStorage(db)47 ctx := context.Background()48 q := `49INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, pinned, timestamp, created_at) VALUES ('op5AGD3VrzgdzwTk7eNMGYEoQS6Zcsz6PWyYMk5kNvqSumDZReW', 0, -1, '10', true, 2, '0abcdef', false, NOW(), NOW());50INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, pinned, timestamp, created_at) VALUES ('ooXh2FstoqHnXD9Kqu7CVWtrs8VNVN2u3XyCnked7v38kjKVdyQ', 0, -1, '20', true, 2, '1abcdef', false, NOW(), NOW());51INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, pinned, timestamp, created_at) VALUES ('op3WBRzqfayJEbv7ApBkTBjHfqxRSoEwrpm16SjMn8wrUXiPjPc', 0, -1, '30', true, 2, '2abcdef', false, NOW(), NOW());52INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, pinned, timestamp, created_at) VALUES ('oohNnXjgArNuBSU3HQRkd9RDmaaUE9LpZne6qDdPfeV6ddNh971', 0, -1, '40', true, 2, '3abcdef', false, NOW(), NOW());53INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, pinned, timestamp, created_at) VALUES ('oocNzaGtRSa8VduCVYmSZBLqQvuPbV978aFtTNGPRbc3Y7Stbob', 0, -1, '50', true, 2, '4abcdef', false, NOW(), NOW());54INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, pinned, timestamp, created_at) VALUES ('onu4xNr7NTUxGHPRGMQQrm5CD3CncDHLFvgNnHcWxkRn7QSdaDJ', 0, -1, '60', true, 2, '5abcdef', false, NOW(), NOW());55INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, pinned, timestamp, created_at) VALUES ('ooUsUE2SCwZCMwjmEgSSP8s3u2LhLtAG45j8hN2FHNEPLLULuss', 0, -1, '70', true, 2, '6abcdef', false, NOW(), NOW());56INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, pinned, timestamp, created_at) VALUES ('opPZPWPvvsKScmYxwSKTymxx8Gw7gjhjaCNU4QboZeuJ8vaiAnU', 0, -1, '80', true, 2, '7abcdef', false, NOW(), NOW());57INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, pinned, timestamp, created_at) VALUES ('ooJdn5tdzhC8YZSsKdphaYN1Jc7nS3GHNZ4eikz4Vzp1q6HS5d1', 0, -1, '90', true, 2, '8abcdef', false, NOW(), NOW());58INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, pinned, timestamp, created_at) VALUES ('ootwyMERpZfjAxgVbcBXfnLX5CeFPbKd13nMaP51G963gqkfrm2', 0, -1, '100', true, 2, '9abcdef', false, NOW(), NOW());59`60 _, err := db.ExecContext(ctx, q)61 require.Nil(t, err)62 // Broadcasts are in the storage, now we insert transactions with same hashes.63 transactions := []*model.Transaction{64 {Hash: "op5AGD3VrzgdzwTk7eNMGYEoQS6Zcsz6PWyYMk5kNvqSumDZReW", BlockNumber: helper.FromUint64(560500), Amount: big.NewInt(10), Timestamp: nowRounded(), CreatedAt: nowRounded(), Status: common_model.SUCCESS.String()},65 {Hash: "ooXh2FstoqHnXD9Kqu7CVWtrs8VNVN2u3XyCnked7v38kjKVdyQ", BlockNumber: helper.FromUint64(560501), Amount: big.NewInt(20), Timestamp: nowRounded(), CreatedAt: nowRounded(), Status: common_model.SUCCESS.String()},66 {Hash: "op3WBRzqfayJEbv7ApBkTBjHfqxRSoEwrpm16SjMn8wrUXiPjPc", BlockNumber: helper.FromUint64(560502), Amount: big.NewInt(30), Timestamp: nowRounded(), CreatedAt: nowRounded(), Status: common_model.SUCCESS.String()},67 {Hash: "oohNnXjgArNuBSU3HQRkd9RDmaaUE9LpZne6qDdPfeV6ddNh971", BlockNumber: helper.FromUint64(560503), Amount: big.NewInt(40), Timestamp: nowRounded(), CreatedAt: nowRounded(), Status: common_model.SUCCESS.String()},68 {Hash: "oocNzaGtRSa8VduCVYmSZBLqQvuPbV978aFtTNGPRbc3Y7Stbob", BlockNumber: helper.FromUint64(560504), Amount: big.NewInt(50), Timestamp: nowRounded(), CreatedAt: nowRounded(), Status: common_model.SUCCESS.String()},69 {Hash: "onu4xNr7NTUxGHPRGMQQrm5CD3CncDHLFvgNnHcWxkRn7QSdaDJ", BlockNumber: helper.FromUint64(560505), Amount: big.NewInt(60), Timestamp: nowRounded(), CreatedAt: nowRounded(), Status: common_model.SUCCESS.String()},70 {Hash: "ooUsUE2SCwZCMwjmEgSSP8s3u2LhLtAG45j8hN2FHNEPLLULuss", BlockNumber: helper.FromUint64(560506), Amount: big.NewInt(70), Timestamp: nowRounded(), CreatedAt: nowRounded(), Status: common_model.SUCCESS.String()},71 {Hash: "opPZPWPvvsKScmYxwSKTymxx8Gw7gjhjaCNU4QboZeuJ8vaiAnU", BlockNumber: helper.FromUint64(560507), Amount: big.NewInt(80), Timestamp: nowRounded(), CreatedAt: nowRounded(), Status: common_model.SUCCESS.String()},72 {Hash: "ooJdn5tdzhC8YZSsKdphaYN1Jc7nS3GHNZ4eikz4Vzp1q6HS5d1", BlockNumber: helper.FromUint64(560508), Amount: big.NewInt(90), Timestamp: nowRounded(), CreatedAt: nowRounded(), Status: common_model.SUCCESS.String()},73 {Hash: "ootwyMERpZfjAxgVbcBXfnLX5CeFPbKd13nMaP51G963gqkfrm2", BlockNumber: helper.FromUint64(560509), Amount: big.NewInt(100), Timestamp: nowRounded(), CreatedAt: nowRounded(), Status: common_model.SUCCESS.String()},74 }75 err = s.CreateTransactions(ctx, transactions)76 require.Nil(t, err)77 // Check that the transactions data has been updated.78 for _, transaction := range transactions {79 storedTransactions, err := s.GetTransactions(ctx, []string{transaction.Hash})80 require.Nil(t, err)81 require.Len(t, storedTransactions, 1)82 require.Equal(t, storedTransactions[0].BlockNumber, transaction.BlockNumber)83 require.Equal(t, storedTransactions[0].Status, "success")84 require.Equal(t, storedTransactions[0].Timestamp, transaction.Timestamp)85 }86}87func TestGetTransaction(t *testing.T) {88 var db = helper.Setup(currency)89 defer helper.Cleanup(currency, db)90 s := NewTransactionStorage(db)91 ctx := context.Background()92 q := `93INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('op5AGD3VrzgdzwTk7eNMGYEoQS6Zcsz6PWyYMk5kNvqSumDZReW', 0, 500000, '10', true, 1, '0abcdef', 'message 0', false, NOW(), NOW());94INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('ooXh2FstoqHnXD9Kqu7CVWtrs8VNVN2u3XyCnked7v38kjKVdyQ', 0, 500001, '20', false, 1, '1abcdef', 'message 1', false, NOW(), NOW());95INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('op3WBRzqfayJEbv7ApBkTBjHfqxRSoEwrpm16SjMn8wrUXiPjPc', 0, 500002, '30', true, 1, '2abcdef', 'message 2', false, NOW(), NOW());96INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('oohNnXjgArNuBSU3HQRkd9RDmaaUE9LpZne6qDdPfeV6ddNh971', 0, 500003, '40', false, 1, '3abcdef', 'message 3', false, NOW(), NOW());97INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('oocNzaGtRSa8VduCVYmSZBLqQvuPbV978aFtTNGPRbc3Y7Stbob', 0, 500004, '50', true, 1, '4abcdef', 'message 4', false, NOW(), NOW());98INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('onu4xNr7NTUxGHPRGMQQrm5CD3CncDHLFvgNnHcWxkRn7QSdaDJ', 0, 500005, '60', false, 0, '5abcdef', 'message 5', false, NOW(), NOW());99INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('ooUsUE2SCwZCMwjmEgSSP8s3u2LhLtAG45j8hN2FHNEPLLULuss', 0, 500006, '70', true, 1, '6abcdef', 'message 6', false, NOW(), NOW());100INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('opPZPWPvvsKScmYxwSKTymxx8Gw7gjhjaCNU4QboZeuJ8vaiAnU', 0, 500007, '80', false, 2, '7abcdef', 'message 7', false, NOW(), NOW());101INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('ooJdn5tdzhC8YZSsKdphaYN1Jc7nS3GHNZ4eikz4Vzp1q6HS5d1', 0, 500008, '90', true, 3, '8abcdef', 'message 8', false, NOW(), NOW());102INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('ootwyMERpZfjAxgVbcBXfnLX5CeFPbKd13nMaP51G963gqkfrm2', 0, -1, '100', false, 4, NULL, NULL, false, NOW(), NOW());103`104 _, err := db.ExecContext(ctx, q)105 require.Nil(t, err)106 tests := []struct {107 hash string108 blockNumber *uint64109 broadcasted bool110 rawTx *string111 status string112 message *string113 timestamp *time.Time114 createdAt *time.Time115 }{116 {hash: "op5AGD3VrzgdzwTk7eNMGYEoQS6Zcsz6PWyYMk5kNvqSumDZReW", blockNumber: helper.FromUint64(500000), broadcasted: true, rawTx: helper.FromString("0abcdef"), status: "success", message: helper.FromString("message 0"), timestamp: nowRounded(), createdAt: nowRounded()},117 {hash: "ooXh2FstoqHnXD9Kqu7CVWtrs8VNVN2u3XyCnked7v38kjKVdyQ", blockNumber: helper.FromUint64(500001), broadcasted: false, rawTx: helper.FromString("1abcdef"), status: "success", message: helper.FromString("message 1"), timestamp: nowRounded(), createdAt: nowRounded()},118 {hash: "op3WBRzqfayJEbv7ApBkTBjHfqxRSoEwrpm16SjMn8wrUXiPjPc", blockNumber: helper.FromUint64(500002), broadcasted: true, rawTx: helper.FromString("2abcdef"), status: "success", message: helper.FromString("message 2"), timestamp: nowRounded(), createdAt: nowRounded()},119 {hash: "oohNnXjgArNuBSU3HQRkd9RDmaaUE9LpZne6qDdPfeV6ddNh971", blockNumber: helper.FromUint64(500003), broadcasted: false, rawTx: helper.FromString("3abcdef"), status: "success", message: helper.FromString("message 3"), timestamp: nowRounded(), createdAt: nowRounded()},120 {hash: "oocNzaGtRSa8VduCVYmSZBLqQvuPbV978aFtTNGPRbc3Y7Stbob", blockNumber: helper.FromUint64(500004), broadcasted: true, rawTx: helper.FromString("4abcdef"), status: "success", message: helper.FromString("message 4"), timestamp: nowRounded(), createdAt: nowRounded()},121 {hash: "onu4xNr7NTUxGHPRGMQQrm5CD3CncDHLFvgNnHcWxkRn7QSdaDJ", blockNumber: helper.FromUint64(500005), broadcasted: false, rawTx: helper.FromString("5abcdef"), status: "new", message: helper.FromString("message 5"), timestamp: nowRounded(), createdAt: nowRounded()},122 {hash: "ooUsUE2SCwZCMwjmEgSSP8s3u2LhLtAG45j8hN2FHNEPLLULuss", blockNumber: helper.FromUint64(500006), broadcasted: true, rawTx: helper.FromString("6abcdef"), status: "success", message: helper.FromString("message 6"), timestamp: nowRounded(), createdAt: nowRounded()},123 {hash: "opPZPWPvvsKScmYxwSKTymxx8Gw7gjhjaCNU4QboZeuJ8vaiAnU", blockNumber: helper.FromUint64(500007), broadcasted: false, rawTx: helper.FromString("7abcdef"), status: "pending", message: helper.FromString("message 7"), timestamp: nowRounded(), createdAt: nowRounded()},124 {hash: "ooJdn5tdzhC8YZSsKdphaYN1Jc7nS3GHNZ4eikz4Vzp1q6HS5d1", blockNumber: helper.FromUint64(500008), broadcasted: true, rawTx: helper.FromString("8abcdef"), status: "temporary_failure", message: helper.FromString("message 8"), timestamp: nowRounded(), createdAt: nowRounded()},125 {hash: "ootwyMERpZfjAxgVbcBXfnLX5CeFPbKd13nMaP51G963gqkfrm2", blockNumber: nil, broadcasted: false, rawTx: nil, status: "invalid", message: nil, timestamp: nowRounded(), createdAt: nowRounded()},126 }127 for _, test := range tests {128 transactions, err := s.GetTransactions(ctx, []string{test.hash})129 require.Nil(t, err)130 require.Len(t, transactions, 1)131 require.Equal(t, test.hash, transactions[0].Hash)132 require.Equal(t, test.blockNumber, transactions[0].BlockNumber)133 require.Equal(t, test.broadcasted, transactions[0].Broadcasted)134 require.Equal(t, test.rawTx, transactions[0].RawTransaction)135 require.Equal(t, test.status, transactions[0].Status)136 if transactions[0].Status == "success" {137 require.Nil(t, transactions[0].Message)138 } else {139 require.Equal(t, test.message, transactions[0].Message)140 }141 }142 // GetTransaction(h) when transaction with hash h is not in the database return an empty result.143 transactions, err := s.GetTransactions(ctx, []string{"012345679abcdef"})144 require.Nil(t, err)145 require.Equal(t, []*model.Transaction{}, transactions)146}147func TestGarbageCollectTransactions(t *testing.T) {148 var db = helper.Setup(currency)149 defer helper.Cleanup(currency, db)150 s := NewTransactionStorage(db)151 ctx := context.Background()152 q := `153INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('op5AGD3VrzgdzwTk7eNMGYEoQS6Zcsz6PWyYMk5kNvqSumDZReW', 0, 500000, '10', false, 1, '0abcdef', 'message 0', false, TIMESTAMPTZ '2019-09-01 02:03:04', TIMESTAMPTZ '2019-10-01 02:03:04');154INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('ooXh2FstoqHnXD9Kqu7CVWtrs8VNVN2u3XyCnked7v38kjKVdyQ', 0, 500001, '20', false, 1, '1abcdef', 'message 1', false, TIMESTAMPTZ '2019-09-02 02:03:04', TIMESTAMPTZ '2019-11-02 02:03:04');155INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('op3WBRzqfayJEbv7ApBkTBjHfqxRSoEwrpm16SjMn8wrUXiPjPc', 0, 500002, '30', false, 1, '2abcdef', 'message 2', false, TIMESTAMPTZ '2019-09-03 02:03:04', TIMESTAMPTZ '2019-10-03 02:03:04');156INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('oohNnXjgArNuBSU3HQRkd9RDmaaUE9LpZne6qDdPfeV6ddNh971', 0, 500003, '40', false, 1, '3abcdef', 'message 3', false, TIMESTAMPTZ '2019-09-04 02:03:04', TIMESTAMPTZ '2019-10-04 02:03:04');157INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('oocNzaGtRSa8VduCVYmSZBLqQvuPbV978aFtTNGPRbc3Y7Stbob', 0, 500004, '50', true, 1, '4abcdef', 'message 4', false, TIMESTAMPTZ '2019-09-05 02:03:04', TIMESTAMPTZ '2019-10-05 02:03:04');158INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('onu4xNr7NTUxGHPRGMQQrm5CD3CncDHLFvgNnHcWxkRn7QSdaDJ', 0, 500005, '60', false, 0, '5abcdef', 'message 5', false, TIMESTAMPTZ '2019-09-06 02:03:04', TIMESTAMPTZ '2019-10-06 02:03:04');159INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('ooUsUE2SCwZCMwjmEgSSP8s3u2LhLtAG45j8hN2FHNEPLLULuss', 0, 500006, '70', false, 1, '6abcdef', 'message 6', false, TIMESTAMPTZ '2019-09-07 02:03:04', TIMESTAMPTZ '2019-10-07 02:03:04');160INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('opPZPWPvvsKScmYxwSKTymxx8Gw7gjhjaCNU4QboZeuJ8vaiAnU', 0, 500007, '80', false, 2, '7abcdef', 'message 7', false, TIMESTAMPTZ '2019-09-08 02:03:04', TIMESTAMPTZ '2019-11-08 02:03:04');161INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('ooJdn5tdzhC8YZSsKdphaYN1Jc7nS3GHNZ4eikz4Vzp1q6HS5d1', 0, 500008, '90', false, 3, '8abcdef', 'message 8', false, TIMESTAMPTZ '2019-09-09 02:03:04', TIMESTAMPTZ '2019-10-09 02:03:04');162INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('ootwyMERpZfjAxgVbcBXfnLX5CeFPbKd13nMaP51G963gqkfrm2', 0, -1, '100', false, 4, NULL, NULL, false, TIMESTAMPTZ '2019-09-10 02:03:04', TIMESTAMPTZ '2019-10-10 02:03:04');163`164 _, err := db.ExecContext(ctx, q)165 require.Nil(t, err)166 beforeBlock := uint64(500006)167 err = s.GarbageCollectTransactions(ctx, beforeBlock)168 require.Nil(t, err)169 var tests = []struct {170 hash string171 deleted bool172 }{173 {hash: "op5AGD3VrzgdzwTk7eNMGYEoQS6Zcsz6PWyYMk5kNvqSumDZReW", deleted: true},174 {hash: "ooXh2FstoqHnXD9Kqu7CVWtrs8VNVN2u3XyCnked7v38kjKVdyQ", deleted: true},175 {hash: "op3WBRzqfayJEbv7ApBkTBjHfqxRSoEwrpm16SjMn8wrUXiPjPc", deleted: true},176 {hash: "oohNnXjgArNuBSU3HQRkd9RDmaaUE9LpZne6qDdPfeV6ddNh971", deleted: true},177 {hash: "oocNzaGtRSa8VduCVYmSZBLqQvuPbV978aFtTNGPRbc3Y7Stbob", deleted: false}, // Broadcasted.178 {hash: "onu4xNr7NTUxGHPRGMQQrm5CD3CncDHLFvgNnHcWxkRn7QSdaDJ", deleted: true},179 {hash: "ooUsUE2SCwZCMwjmEgSSP8s3u2LhLtAG45j8hN2FHNEPLLULuss", deleted: true},180 {hash: "opPZPWPvvsKScmYxwSKTymxx8Gw7gjhjaCNU4QboZeuJ8vaiAnU", deleted: false}, // Block number.181 {hash: "ooJdn5tdzhC8YZSsKdphaYN1Jc7nS3GHNZ4eikz4Vzp1q6HS5d1", deleted: false}, // Block number.182 {hash: "ootwyMERpZfjAxgVbcBXfnLX5CeFPbKd13nMaP51G963gqkfrm2", deleted: false}, // Block number.183 }184 for _, test := range tests {185 res, err := s.GetTransactions(ctx, []string{test.hash})186 require.Nil(t, err)187 if test.deleted {188 require.Equal(t, []*model.Transaction{}, res)189 }190 }191}192func TestDumpPendingBroadcasts(t *testing.T) {193 var db = helper.Setup(currency)194 defer helper.Cleanup(currency, db)195 s := NewTransactionStorage(db)196 ctx := context.Background()197 q := `198INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('op5AGD3VrzgdzwTk7eNMGYEoQS6Zcsz6PWyYMk5kNvqSumDZReW', 0, 500000, '10', true, 0, '0abcdef', 'message 0', false, NOW(), NOW());199INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('ooXh2FstoqHnXD9Kqu7CVWtrs8VNVN2u3XyCnked7v38kjKVdyQ', 0, 500001, '20', true, 1, '1abcdef', 'message 1', false, NOW(), NOW());200INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('op3WBRzqfayJEbv7ApBkTBjHfqxRSoEwrpm16SjMn8wrUXiPjPc', 0, 500002, '30', true, 2, '2abcdef', 'message 2', false, NOW(), NOW());201INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('oohNnXjgArNuBSU3HQRkd9RDmaaUE9LpZne6qDdPfeV6ddNh971', 0, 500003, '40', true, 3, '3abcdef', 'message 3', false, NOW(), NOW());202INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('oocNzaGtRSa8VduCVYmSZBLqQvuPbV978aFtTNGPRbc3Y7Stbob', 0, 500004, '50', true, 4, '4abcdef', 'message 4', false, NOW(), NOW());203INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('onu4xNr7NTUxGHPRGMQQrm5CD3CncDHLFvgNnHcWxkRn7QSdaDJ', 0, 500005, '60', true, 2, '5abcdef', 'message 5', false, NOW(), NOW());204INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('ooUsUE2SCwZCMwjmEgSSP8s3u2LhLtAG45j8hN2FHNEPLLULuss', 0, 500006, '70', true, 2, '6abcdef', 'message 6', false, NOW(), NOW());205INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('opPZPWPvvsKScmYxwSKTymxx8Gw7gjhjaCNU4QboZeuJ8vaiAnU', 0, 500007, '80', true, 3, '7abcdef', 'message 7', false, NOW(), NOW());206INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('ooJdn5tdzhC8YZSsKdphaYN1Jc7nS3GHNZ4eikz4Vzp1q6HS5d1', 0, 500008, '90', false, 3, '8abcdef', 'message 8', false, NOW(), NOW());207INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('ootwyMERpZfjAxgVbcBXfnLX5CeFPbKd13nMaP51G963gqkfrm2', 0, -1, '100', false, 4, NULL, NULL, false, NOW(), NOW());208`209 _, err := db.ExecContext(ctx, q)210 require.Nil(t, err)211 expectedBroadcasts := map[string]struct{}{212 "op5AGD3VrzgdzwTk7eNMGYEoQS6Zcsz6PWyYMk5kNvqSumDZReW": struct{}{},213 "op3WBRzqfayJEbv7ApBkTBjHfqxRSoEwrpm16SjMn8wrUXiPjPc": struct{}{},214 "oohNnXjgArNuBSU3HQRkd9RDmaaUE9LpZne6qDdPfeV6ddNh971": struct{}{},215 "onu4xNr7NTUxGHPRGMQQrm5CD3CncDHLFvgNnHcWxkRn7QSdaDJ": struct{}{},216 "ooUsUE2SCwZCMwjmEgSSP8s3u2LhLtAG45j8hN2FHNEPLLULuss": struct{}{},217 "opPZPWPvvsKScmYxwSKTymxx8Gw7gjhjaCNU4QboZeuJ8vaiAnU": struct{}{},218 }219 time.Sleep(time.Second) // Needed otherwise the query with as of system time return nothing.220 systemTime := time.Now()221 // Get first 3 broadcasts.222 limit, offset := uint64(3), uint64(0)223 broadcasts, count, err := s.DumpPendingBroadcasts(ctx, limit, offset, systemTime)224 require.Nil(t, err)225 require.Equal(t, uint64(6), count)226 for _, broadcast := range broadcasts {227 require.Contains(t, expectedBroadcasts, broadcast.Hash)228 delete(expectedBroadcasts, broadcast.Hash)229 }230 // Get next 2 broadcasts.231 limit, offset = 10, 3232 broadcasts, count, err = s.DumpPendingBroadcasts(ctx, limit, offset, systemTime)233 require.Nil(t, err)234 require.Equal(t, uint64(6), count)235 for _, broadcast := range broadcasts {236 require.Contains(t, expectedBroadcasts, broadcast.Hash)237 delete(expectedBroadcasts, broadcast.Hash)238 }239 // All broadcasts should be returned, and thus deleted from the map.240 require.Len(t, expectedBroadcasts, 0)241}242func TestDumpTransactions(t *testing.T) {243 var db = helper.Setup(currency)244 defer helper.Cleanup(currency, db)245 s := NewTransactionStorage(db)246 ctx := context.Background()247 q := `248INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('op5AGD3VrzgdzwTk7eNMGYEoQS6Zcsz6PWyYMk5kNvqSumDZReW', 0, 500000, '10', true, 1, '0abcdef', 'message 0', true, NOW(), NOW());249INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('ooXh2FstoqHnXD9Kqu7CVWtrs8VNVN2u3XyCnked7v38kjKVdyQ', 0, 500001, '20', false, 1, '1abcdef', 'message 1', true, NOW(), NOW());250INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('op3WBRzqfayJEbv7ApBkTBjHfqxRSoEwrpm16SjMn8wrUXiPjPc', 0, 500002, '30', true, 1, '2abcdef', 'message 2', true, NOW(), NOW());251INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('oohNnXjgArNuBSU3HQRkd9RDmaaUE9LpZne6qDdPfeV6ddNh971', 0, 500003, '40', false, 1, '3abcdef', 'message 3', true, NOW(), NOW());252INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('oocNzaGtRSa8VduCVYmSZBLqQvuPbV978aFtTNGPRbc3Y7Stbob', 0, 500004, '50', true, 1, '4abcdef', 'message 4', false, NOW(), NOW());253INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('onu4xNr7NTUxGHPRGMQQrm5CD3CncDHLFvgNnHcWxkRn7QSdaDJ', 0, 500005, '60', false, 0, '5abcdef', 'message 5', true, NOW(), NOW());254INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('ooUsUE2SCwZCMwjmEgSSP8s3u2LhLtAG45j8hN2FHNEPLLULuss', 0, 500006, '70', true, 1, '6abcdef', 'message 6', false, NOW(), NOW());255INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('opPZPWPvvsKScmYxwSKTymxx8Gw7gjhjaCNU4QboZeuJ8vaiAnU', 0, 500007, '80', false, 2, '7abcdef', 'message 7', true, NOW(), NOW());256INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('ooJdn5tdzhC8YZSsKdphaYN1Jc7nS3GHNZ4eikz4Vzp1q6HS5d1', 0, 500008, '90', true, 3, '8abcdef', 'message 8', true, NOW(), NOW());257INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('ootwyMERpZfjAxgVbcBXfnLX5CeFPbKd13nMaP51G963gqkfrm2', 0, -1, '100', false, 4, NULL, NULL, true, NOW(), NOW());258`259 _, err := db.ExecContext(ctx, q)260 require.Nil(t, err)261 expectedSubTransactions := map[string]struct{}{262 "op5AGD3VrzgdzwTk7eNMGYEoQS6Zcsz6PWyYMk5kNvqSumDZReW": struct{}{},263 "ooXh2FstoqHnXD9Kqu7CVWtrs8VNVN2u3XyCnked7v38kjKVdyQ": struct{}{},264 "op3WBRzqfayJEbv7ApBkTBjHfqxRSoEwrpm16SjMn8wrUXiPjPc": struct{}{},265 "oohNnXjgArNuBSU3HQRkd9RDmaaUE9LpZne6qDdPfeV6ddNh971": struct{}{},266 "onu4xNr7NTUxGHPRGMQQrm5CD3CncDHLFvgNnHcWxkRn7QSdaDJ": struct{}{},267 "opPZPWPvvsKScmYxwSKTymxx8Gw7gjhjaCNU4QboZeuJ8vaiAnU": struct{}{},268 "ooJdn5tdzhC8YZSsKdphaYN1Jc7nS3GHNZ4eikz4Vzp1q6HS5d1": struct{}{},269 "ootwyMERpZfjAxgVbcBXfnLX5CeFPbKd13nMaP51G963gqkfrm2": struct{}{},270 }271 time.Sleep(time.Second) // Needed otherwise the query with as of system time return nothing.272 systemTime := time.Now()273 // Get first 5 transactions.274 limit, offset := uint64(5), uint64(0)275 transactions, count, err := s.DumpPinnedTransactions(ctx, limit, offset, systemTime)276 require.Nil(t, err)277 require.Equal(t, uint64(8), count)278 for _, transaction := range transactions {279 require.Contains(t, expectedSubTransactions, transaction.Hash)280 delete(expectedSubTransactions, transaction.Hash)281 }282 // Get next 2 transactions.283 limit, offset = 2, 5284 transactions, count, err = s.DumpPinnedTransactions(ctx, limit, offset, systemTime)285 require.Nil(t, err)286 require.Equal(t, uint64(8), count)287 for _, transaction := range transactions {288 require.Contains(t, expectedSubTransactions, transaction.Hash)289 delete(expectedSubTransactions, transaction.Hash)290 }291 // Get next transaction.292 limit, offset = 10, 7293 transactions, count, err = s.DumpPinnedTransactions(ctx, limit, offset, systemTime)294 require.Nil(t, err)295 require.Equal(t, uint64(8), count)296 for _, transaction := range transactions {297 require.Contains(t, expectedSubTransactions, transaction.Hash)298 delete(expectedSubTransactions, transaction.Hash)299 }300 // All broadcasts should be returned, and thus deleted from the map.301 require.Len(t, expectedSubTransactions, 0)302}303func TestDeleteBlockTransactions(t *testing.T) {304 var db = helper.Setup(currency)305 defer helper.Cleanup(currency, db)306 s := NewTransactionStorage(db)307 ctx := context.Background()308 q := `309INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('op5AGD3VrzgdzwTk7eNMGYEoQS6Zcsz6PWyYMk5kNvqSumDZReW', 0, 500000, '10', true, 1, '0abcdef', 'message 0', true, NOW(), NOW());310INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('ooXh2FstoqHnXD9Kqu7CVWtrs8VNVN2u3XyCnked7v38kjKVdyQ', 0, 500000, '20', false, 1, '1abcdef', 'message 1', true, NOW(), NOW());311INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('op3WBRzqfayJEbv7ApBkTBjHfqxRSoEwrpm16SjMn8wrUXiPjPc', 0, 500000, '30', true, 1, '2abcdef', 'message 2', true, NOW(), NOW());312INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('oohNnXjgArNuBSU3HQRkd9RDmaaUE9LpZne6qDdPfeV6ddNh971', 0, 500003, '40', false, 1, '3abcdef', 'message 3', true, NOW(), NOW());313INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('oocNzaGtRSa8VduCVYmSZBLqQvuPbV978aFtTNGPRbc3Y7Stbob', 0, 500000, '50', true, 1, '4abcdef', 'message 4', false, NOW(), NOW());314INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('onu4xNr7NTUxGHPRGMQQrm5CD3CncDHLFvgNnHcWxkRn7QSdaDJ', 0, 500005, '60', false, 0, '5abcdef', 'message 5', true, NOW(), NOW());315INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('ooUsUE2SCwZCMwjmEgSSP8s3u2LhLtAG45j8hN2FHNEPLLULuss', 0, 500006, '70', true, 1, '6abcdef', 'message 6', false, NOW(), NOW());316INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('opPZPWPvvsKScmYxwSKTymxx8Gw7gjhjaCNU4QboZeuJ8vaiAnU', 0, 500000, '80', false, 2, '7abcdef', 'message 7', true, NOW(), NOW());317INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('ooJdn5tdzhC8YZSsKdphaYN1Jc7nS3GHNZ4eikz4Vzp1q6HS5d1', 0, 500008, '90', true, 3, '8abcdef', 'message 8', true, NOW(), NOW());318INSERT INTO xtz_tx (hash, idx, block_number, amount, broadcasted, status, rawtx, message, pinned, timestamp, created_at) VALUES ('ootwyMERpZfjAxgVbcBXfnLX5CeFPbKd13nMaP51G963gqkfrm2', 0, -1, '100', false, 4, NULL, NULL, true, NOW(), NOW());319`320 _, err := db.ExecContext(ctx, q)321 require.Nil(t, err)322 var count int323 err = db.Get(&count, "SELECT count(*) from xtz_tx")324 require.Nil(t, err)325 require.Equal(t, 10, count)326 err = s.DeleteBlockTransactions(ctx, 500000)327 require.Nil(t, err)328 err = db.Get(&count, "SELECT count(*) from xtz_tx")329 require.Nil(t, err)330 require.Equal(t, 5, count)331}332func nowRounded() *time.Time {333 t := time.Now().UTC().Round(time.Second)334 return &t335}336// ///////////////////////////337// OLDs interations tests338// ///////////////////////////339func TestIntLBPut(t *testing.T) {340 var db = helper.Setup(currency)341 defer helper.Cleanup(currency, db)342 s := NewTransactionStorage(db)343 var (344 ctx = context.Background()345 transactions = randomEntries(100)346 )347 require.Nil(t, s.CreateTransactions(ctx, transactions))348 for _, transaction := range transactions {349 var rep, err = s.GetTransactions(ctx, []string{transaction.Hash})350 require.Nil(t, err)351 require.Len(t, rep, 1)352 require.Equal(t, transaction.Hash, rep[0].Hash)353 require.Equal(t, transaction.BlockNumber, rep[0].BlockNumber)354 require.Equal(t, transaction.DestinationAddress, rep[0].DestinationAddress)355 require.Equal(t, transaction.SourceAddress, rep[0].SourceAddress)356 require.Equal(t, transaction.Amount, rep[0].Amount)357 require.Equal(t, transaction.Fee, rep[0].Fee)358 require.False(t, rep[0].Pinned)359 require.False(t, rep[0].Broadcasted)360 require.Nil(t, rep[0].RawTransaction)361 require.Equal(t, "success", rep[0].Status)362 require.Nil(t, rep[0].Message)363 require.True(t, time.Now().After(*rep[0].CreatedAt))364 }365}366func TestIntLBGet(t *testing.T) {367 var db = helper.Setup(currency)368 defer helper.Cleanup(currency, db)369 s := NewTransactionStorage(db)370 var (371 ctx = context.Background()372 transactions = randomEntries(10)373 )374 // Put transactions in store.375 require.Nil(t, s.CreateTransactions(ctx, transactions))376 // Get Tx377 {378 for _, transaction := range transactions {379 var rep, err = s.GetTransactions(ctx, []string{transaction.Hash})380 require.Nil(t, err)381 require.Len(t, rep, 1)382 require.Equal(t, transaction.Hash, rep[0].Hash)383 require.Equal(t, transaction.BlockNumber, rep[0].BlockNumber)384 require.Equal(t, transaction.DestinationAddress, rep[0].DestinationAddress)385 require.Equal(t, transaction.SourceAddress, rep[0].SourceAddress)386 require.Equal(t, transaction.Amount, rep[0].Amount)387 require.Equal(t, transaction.Fee, rep[0].Fee)388 require.Equal(t, transaction.Timestamp, rep[0].Timestamp)389 require.False(t, rep[0].Pinned)390 require.False(t, rep[0].Broadcasted)391 require.Nil(t, rep[0].RawTransaction)392 require.Equal(t, "success", rep[0].Status)393 require.Nil(t, rep[0].Message)394 require.True(t, time.Now().After(*rep[0].CreatedAt))395 }396 }397}398func TestIntLBGetBetweenBlocks(t *testing.T) {399 var db = helper.Setup(currency)400 defer helper.Cleanup(currency, db)401 s := NewTransactionStorage(db)402 var (403 ctx = context.Background()404 address1 = helper.FromString(randomHexString(20))405 address2 = helper.FromString(randomHexString(20))406 address3 = helper.FromString(randomHexString(20))407 transactions = []*model.Transaction{408 {Hash: randomHexString(32), Amount: big.NewInt(0), BlockNumber: helper.FromUint64(0), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address2, Timestamp: helper.FromTimestamp(time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC))},409 {Hash: randomHexString(32), Amount: big.NewInt(0), BlockNumber: helper.FromUint64(1), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address2, Timestamp: helper.FromTimestamp(time.Date(2018, time.February, 0, 0, 0, 0, 0, time.UTC))},410 {Hash: randomHexString(32), Amount: big.NewInt(0), BlockNumber: helper.FromUint64(2), Fee: big.NewInt(0), DestinationAddress: address2, SourceAddress: address3, Timestamp: helper.FromTimestamp(time.Date(2018, time.March, 0, 0, 0, 0, 0, time.UTC))},411 {Hash: randomHexString(32), Amount: big.NewInt(0), BlockNumber: helper.FromUint64(3), Fee: big.NewInt(0), DestinationAddress: address3, SourceAddress: address3, Timestamp: helper.FromTimestamp(time.Date(2018, time.April, 0, 0, 0, 0, 0, time.UTC))},412 {Hash: randomHexString(32), Amount: big.NewInt(0), BlockNumber: helper.FromUint64(4), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address2, Timestamp: helper.FromTimestamp(time.Date(2018, time.May, 0, 0, 0, 0, 0, time.UTC))},413 {Hash: randomHexString(32), Amount: big.NewInt(0), BlockNumber: helper.FromUint64(5), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address2, Timestamp: helper.FromTimestamp(time.Date(2018, time.June, 0, 0, 0, 0, 0, time.UTC))},414 {Hash: randomHexString(32), Amount: big.NewInt(0), BlockNumber: helper.FromUint64(6), Fee: big.NewInt(0), DestinationAddress: address3, SourceAddress: address3, Timestamp: helper.FromTimestamp(time.Date(2018, time.July, 0, 0, 0, 0, 0, time.UTC))},415 {Hash: randomHexString(32), Amount: big.NewInt(0), BlockNumber: helper.FromUint64(7), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address2, Timestamp: helper.FromTimestamp(time.Date(2018, time.August, 0, 0, 0, 0, 0, time.UTC))},416 {Hash: randomHexString(32), Amount: big.NewInt(0), BlockNumber: helper.FromUint64(8), Fee: big.NewInt(0), DestinationAddress: address2, SourceAddress: address2, Timestamp: helper.FromTimestamp(time.Date(2018, time.September, 0, 0, 0, 0, 0, time.UTC))},417 {Hash: randomHexString(32), Amount: big.NewInt(0), BlockNumber: helper.FromUint64(9), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address2, Timestamp: helper.FromTimestamp(time.Date(2018, time.October, 0, 0, 0, 0, 0, time.UTC))},418 }419 )420 // Put transactions in store.421 require.Nil(t, s.CreateTransactions(ctx, transactions))422 var tsts = []struct {423 address *string424 start uint64425 end uint64426 expectedNbrTx int427 }{428 {address: address1, start: 0, end: 0, expectedNbrTx: 1},429 {address: address1, start: 0, end: 1, expectedNbrTx: 2},430 {address: address1, start: 0, end: 2, expectedNbrTx: 2},431 {address: address1, start: 0, end: 3, expectedNbrTx: 2},432 {address: address1, start: 0, end: 4, expectedNbrTx: 3},433 {address: address1, start: 0, end: 5, expectedNbrTx: 4},434 {address: address1, start: 0, end: 6, expectedNbrTx: 4},435 {address: address1, start: 0, end: 7, expectedNbrTx: 5},436 {address: address1, start: 0, end: 8, expectedNbrTx: 5},437 {address: address1, start: 0, end: 9, expectedNbrTx: 6},438 {address: address2, start: 0, end: 0, expectedNbrTx: 1},439 {address: address2, start: 0, end: 1, expectedNbrTx: 2},440 {address: address2, start: 0, end: 2, expectedNbrTx: 3},441 {address: address2, start: 0, end: 3, expectedNbrTx: 3},442 {address: address2, start: 0, end: 4, expectedNbrTx: 4},443 {address: address2, start: 0, end: 5, expectedNbrTx: 5},444 {address: address2, start: 0, end: 6, expectedNbrTx: 5},445 {address: address2, start: 0, end: 7, expectedNbrTx: 6},446 {address: address2, start: 0, end: 8, expectedNbrTx: 7},447 {address: address2, start: 0, end: 9, expectedNbrTx: 8},448 {address: address3, start: 0, end: 0, expectedNbrTx: 0},449 {address: address3, start: 0, end: 1, expectedNbrTx: 0},450 {address: address3, start: 0, end: 2, expectedNbrTx: 1},451 {address: address3, start: 0, end: 3, expectedNbrTx: 2},452 {address: address3, start: 0, end: 4, expectedNbrTx: 2},453 {address: address3, start: 0, end: 5, expectedNbrTx: 2},454 {address: address3, start: 0, end: 6, expectedNbrTx: 3},455 {address: address3, start: 0, end: 7, expectedNbrTx: 3},456 {address: address3, start: 0, end: 8, expectedNbrTx: 3},457 {address: address3, start: 0, end: 9, expectedNbrTx: 3},458 }459 for _, tst := range tsts {460 var rep, totalItems, err = s.GetTransactionsBetweenBlocks(ctx, []string{*tst.address}, tst.start, tst.end, 100, 0)461 require.Nil(t, err)462 require.Equal(t, uint64(tst.expectedNbrTx), totalItems)463 require.Equal(t, tst.expectedNbrTx, len(rep), fmt.Sprintf("GetBetween date %q and %q", tst.start, tst.end))464 }465}466func TestIntLBGetBetweenDates(t *testing.T) {467 var db = helper.Setup(currency)468 defer helper.Cleanup(currency, db)469 s := NewTransactionStorage(db)470 var (471 ctx = context.Background()472 address1 = helper.FromString(randomHexString(20))473 address2 = helper.FromString(randomHexString(20))474 address3 = helper.FromString(randomHexString(20))475 transactions = []*model.Transaction{476 {Hash: randomHexString(32), Amount: big.NewInt(0), BlockNumber: helper.FromUint64(0), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address2, Timestamp: helper.FromTimestamp(time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC))},477 {Hash: randomHexString(32), Amount: big.NewInt(0), BlockNumber: helper.FromUint64(0), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address2, Timestamp: helper.FromTimestamp(time.Date(2018, time.February, 0, 0, 0, 0, 0, time.UTC))},478 {Hash: randomHexString(32), Amount: big.NewInt(0), BlockNumber: helper.FromUint64(0), Fee: big.NewInt(0), DestinationAddress: address2, SourceAddress: address3, Timestamp: helper.FromTimestamp(time.Date(2018, time.March, 0, 0, 0, 0, 0, time.UTC))},479 {Hash: randomHexString(32), Amount: big.NewInt(0), BlockNumber: helper.FromUint64(0), Fee: big.NewInt(0), DestinationAddress: address3, SourceAddress: address3, Timestamp: helper.FromTimestamp(time.Date(2018, time.April, 0, 0, 0, 0, 0, time.UTC))},480 {Hash: randomHexString(32), Amount: big.NewInt(0), BlockNumber: helper.FromUint64(2), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address2, Timestamp: helper.FromTimestamp(time.Date(2018, time.May, 0, 0, 0, 0, 0, time.UTC))},481 {Hash: randomHexString(32), Amount: big.NewInt(0), BlockNumber: helper.FromUint64(4), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address2, Timestamp: helper.FromTimestamp(time.Date(2018, time.June, 0, 0, 0, 0, 0, time.UTC))},482 {Hash: randomHexString(32), Amount: big.NewInt(0), BlockNumber: helper.FromUint64(0), Fee: big.NewInt(0), DestinationAddress: address3, SourceAddress: address3, Timestamp: helper.FromTimestamp(time.Date(2018, time.July, 0, 0, 0, 0, 0, time.UTC))},483 {Hash: randomHexString(32), Amount: big.NewInt(0), BlockNumber: helper.FromUint64(17), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address2, Timestamp: helper.FromTimestamp(time.Date(2018, time.August, 0, 0, 0, 0, 0, time.UTC))},484 {Hash: randomHexString(32), Amount: big.NewInt(0), BlockNumber: helper.FromUint64(20), Fee: big.NewInt(0), DestinationAddress: address2, SourceAddress: address2, Timestamp: helper.FromTimestamp(time.Date(2018, time.September, 0, 0, 0, 0, 0, time.UTC))},485 {Hash: randomHexString(32), Amount: big.NewInt(0), BlockNumber: helper.FromUint64(100), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address2, Timestamp: helper.FromTimestamp(time.Date(2018, time.October, 0, 0, 0, 0, 0, time.UTC))},486 }487 )488 // Put transactions in store.489 require.Nil(t, s.CreateTransactions(ctx, transactions))490 var tsts = []struct {491 address *string492 start time.Time493 end time.Time494 expectedNbrTx int495 }{496 {address: address1, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 1},497 {address: address1, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.February, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 2},498 {address: address1, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.March, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 2},499 {address: address1, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.April, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 2},500 {address: address1, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.May, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 3},501 {address: address1, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.June, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 4},502 {address: address1, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.July, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 4},503 {address: address1, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.August, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 5},504 {address: address1, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.September, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 5},505 {address: address1, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.October, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 6},506 {address: address2, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 1},507 {address: address2, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.February, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 2},508 {address: address2, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.March, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 3},509 {address: address2, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.April, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 3},510 {address: address2, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.May, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 4},511 {address: address2, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.June, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 5},512 {address: address2, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.July, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 5},513 {address: address2, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.August, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 6},514 {address: address2, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.September, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 7},515 {address: address2, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.October, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 8},516 {address: address3, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 0},517 {address: address3, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.February, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 0},518 {address: address3, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.March, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 1},519 {address: address3, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.April, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 2},520 {address: address3, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.May, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 2},521 {address: address3, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.June, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 2},522 {address: address3, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.July, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 3},523 {address: address3, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.August, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 3},524 {address: address3, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.September, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 3},525 {address: address3, start: time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC), end: time.Date(2018, time.October, 0, 0, 0, 0, 0, time.UTC), expectedNbrTx: 3},526 }527 for _, tst := range tsts {528 var rep, totalItems, err = s.GetTransactionsBetweenDates(ctx, []string{*tst.address}, tst.start, tst.end, 100, 0)529 require.Nil(t, err)530 require.Equal(t, uint64(tst.expectedNbrTx), totalItems)531 require.Equal(t, tst.expectedNbrTx, len(rep), fmt.Sprintf("GetBetween date %q and %q", tst.start, tst.end))532 }533}534func TestIntLBMarkPinned(t *testing.T) {535 var db = helper.Setup(currency)536 defer helper.Cleanup(currency, db)537 s := NewTransactionStorage(db)538 var (539 ctx = context.Background()540 address1 = helper.FromString(randomHexString(20))541 address2 = helper.FromString(randomHexString(20))542 address3 = helper.FromString(randomHexString(20))543 address4 = helper.FromString(randomHexString(20))544 address5 = helper.FromString(randomHexString(20))545 transactions = []*model.Transaction{546 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(0), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address2, Amount: big.NewInt(0)},547 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(0), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address1, Amount: big.NewInt(0)},548 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(0), Fee: big.NewInt(0), DestinationAddress: address2, SourceAddress: address3, Amount: big.NewInt(0)},549 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(0), Fee: big.NewInt(0), DestinationAddress: address3, SourceAddress: address3, Amount: big.NewInt(0)},550 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(2), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address2, Amount: big.NewInt(0)},551 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(4), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address2, Amount: big.NewInt(0)},552 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(10), Fee: big.NewInt(0), DestinationAddress: address3, SourceAddress: address3, Amount: big.NewInt(0)},553 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(17), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address2, Amount: big.NewInt(0)},554 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(20), Fee: big.NewInt(0), DestinationAddress: address2, SourceAddress: address2, Amount: big.NewInt(0)},555 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(100), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address2, Amount: big.NewInt(0)},556 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(4), Fee: big.NewInt(0), DestinationAddress: address4, SourceAddress: address2, Amount: big.NewInt(0)},557 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(10), Fee: big.NewInt(0), DestinationAddress: address4, SourceAddress: address3, Amount: big.NewInt(0)},558 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(17), Fee: big.NewInt(0), DestinationAddress: address5, SourceAddress: address2, Amount: big.NewInt(0)},559 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(20), Fee: big.NewInt(0), DestinationAddress: address5, SourceAddress: address2, Amount: big.NewInt(0)},560 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(100), Fee: big.NewInt(0), DestinationAddress: address5, SourceAddress: address2, Amount: big.NewInt(0)},561 }562 expected = []bool{563 true,564 true,565 false,566 false,567 true,568 true,569 false,570 true,571 false,572 true,573 true,574 true,575 true,576 true,577 true,578 }579 )580 // Put transactions in store.581 require.Nil(t, s.CreateTransactions(ctx, transactions))582 // MarkPinned.583 require.Nil(t, s.MarkPinned(ctx, []string{*address1, *address4, *address5}))584 // Check boolean was set in storage.585 for i, transaction := range transactions {586 var rep, err = s.GetTransactions(ctx, []string{transaction.Hash})587 require.Nil(t, err)588 require.Len(t, rep, 1)589 require.Equal(t, rep[0].Pinned, expected[i])590 }591}592func TestIntLBGarbageCollect(t *testing.T) {593 var db = helper.Setup(currency)594 defer helper.Cleanup(currency, db)595 s := NewTransactionStorage(db)596 var (597 ctx = context.Background()598 address1 = helper.FromString(randomHexString(20))599 address2 = helper.FromString(randomHexString(20))600 address3 = helper.FromString(randomHexString(20))601 transactions = []*model.Transaction{602 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(500000), Timestamp: helper.FromTimestamp(time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC)), CreatedAt: helper.FromTimestamp(time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC)), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address2, Amount: big.NewInt(0)},603 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(500000), Timestamp: helper.FromTimestamp(time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC)), CreatedAt: helper.FromTimestamp(time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC)), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address2, Amount: big.NewInt(0)},604 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(500010), Timestamp: helper.FromTimestamp(time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC)), CreatedAt: helper.FromTimestamp(time.Date(2018, time.January, 0, 0, 0, 0, 0, time.UTC)), Fee: big.NewInt(0), DestinationAddress: address2, SourceAddress: address3, Amount: big.NewInt(0)},605 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(500010), Timestamp: helper.FromTimestamp(time.Date(2018, time.February, 0, 0, 0, 0, 0, time.UTC)), CreatedAt: helper.FromTimestamp(time.Date(2018, time.February, 0, 0, 0, 0, 0, time.UTC)), Fee: big.NewInt(0), DestinationAddress: address3, SourceAddress: address3, Amount: big.NewInt(0)},606 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(500020), Timestamp: helper.FromTimestamp(time.Date(2018, time.March, 0, 0, 0, 0, 0, time.UTC)), CreatedAt: helper.FromTimestamp(time.Date(2018, time.March, 0, 0, 0, 0, 0, time.UTC)), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address2, Amount: big.NewInt(0)},607 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(500020), Timestamp: helper.FromTimestamp(time.Date(2018, time.April, 0, 0, 0, 0, 0, time.UTC)), CreatedAt: helper.FromTimestamp(time.Date(2018, time.April, 0, 0, 0, 0, 0, time.UTC)), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address2, Amount: big.NewInt(0)},608 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(500030), Timestamp: helper.FromTimestamp(time.Date(2018, time.May, 0, 0, 0, 0, 0, time.UTC)), CreatedAt: helper.FromTimestamp(time.Date(2018, time.May, 0, 0, 0, 0, 0, time.UTC)), Fee: big.NewInt(0), DestinationAddress: address3, SourceAddress: address3, Amount: big.NewInt(0)},609 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(500030), Timestamp: helper.FromTimestamp(time.Date(2018, time.June, 0, 0, 0, 0, 0, time.UTC)), CreatedAt: helper.FromTimestamp(time.Date(2018, time.June, 0, 0, 0, 0, 0, time.UTC)), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address2, Amount: big.NewInt(0)},610 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(500040), Timestamp: helper.FromTimestamp(time.Date(2018, time.July, 0, 0, 0, 0, 0, time.UTC)), CreatedAt: helper.FromTimestamp(time.Date(2018, time.July, 0, 0, 0, 0, 0, time.UTC)), Fee: big.NewInt(0), DestinationAddress: address2, SourceAddress: address2, Amount: big.NewInt(0)},611 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(500040), Timestamp: helper.FromTimestamp(time.Date(2018, time.August, 0, 0, 0, 0, 0, time.UTC)), CreatedAt: helper.FromTimestamp(time.Date(2018, time.August, 0, 0, 0, 0, 0, time.UTC)), Fee: big.NewInt(0), DestinationAddress: address1, SourceAddress: address2, Amount: big.NewInt(0)},612 }613 )614 // Put transactions in store.615 for _, transaction := range transactions {616 require.Nil(t, s.CreateTransactions(ctx, []*model.Transaction{transaction}))617 var createdAt = transaction.CreatedAt.UTC().Format("2006-01-02 15:04:05.0000")618 _, err := db.ExecContext(ctx, fmt.Sprintf("UPDATE xtz_tx SET created_at = '%s' WHERE hash = '%s'", createdAt, transaction.Hash))619 require.Nil(t, err)620 }621 // Set one tx to broadcasted, one to pinned.622 {623 _, err := db.ExecContext(context.Background(), fmt.Sprintf("UPDATE xtz_tx SET broadcasted = true WHERE hash = '%s'", transactions[2].Hash))624 require.Nil(t, err)625 _, err = db.ExecContext(context.Background(), fmt.Sprintf("UPDATE xtz_tx SET pinned = true WHERE hash = '%s'", transactions[3].Hash))626 require.Nil(t, err)627 }628 var tsts = []struct {629 beforeBlock uint64630 expectedNbrTx int631 }{632 {beforeBlock: 400000, expectedNbrTx: 10},633 {beforeBlock: 500000, expectedNbrTx: 8},634 {beforeBlock: 500010, expectedNbrTx: 8},635 {beforeBlock: 500020, expectedNbrTx: 6},636 {beforeBlock: 500030, expectedNbrTx: 4},637 {beforeBlock: 500040, expectedNbrTx: 2},638 {beforeBlock: 500050, expectedNbrTx: 2},639 }640 for _, tst := range tsts {641 require.Nil(t, s.GarbageCollectTransactions(ctx, tst.beforeBlock))642 var count int643 var rows, err = db.QueryContext(context.Background(), "SELECT count(*) from xtz_tx")644 require.Nil(t, err)645 defer rows.Close()646 require.True(t, rows.Next())647 require.Nil(t, rows.Scan(&count))648 require.Equal(t, tst.expectedNbrTx, count)649 }650}651func TestIntLBGCBroadcasts(t *testing.T) {652 var db = helper.Setup(currency)653 defer helper.Cleanup(currency, db)654 s := NewTransactionStorage(db)655 var (656 ctx = context.Background()657 tests = [10]struct {658 createdAtBlock uint64659 blockNumber *int64660 }{661 {createdAtBlock: 500000, blockNumber: helper.FromInt64(-1)},662 {createdAtBlock: 500010, blockNumber: helper.FromInt64(-1)},663 {createdAtBlock: 500020, blockNumber: helper.FromInt64(-1)},664 {createdAtBlock: 500030, blockNumber: helper.FromInt64(-1)},665 {createdAtBlock: 500040, blockNumber: helper.FromInt64(-1)},666 {createdAtBlock: 500050, blockNumber: helper.FromInt64(1)},667 {createdAtBlock: 500060, blockNumber: helper.FromInt64(-1)},668 {createdAtBlock: 500070, blockNumber: helper.FromInt64(1)},669 {createdAtBlock: 500080, blockNumber: helper.FromInt64(-1)},670 {createdAtBlock: 500090, blockNumber: helper.FromInt64(-1)},671 }672 transactions = randomBroadcastedEntries(10)673 )674 for i, tx := range transactions {675 tx.BlockNumber = helper.Uint64(tests[i].blockNumber)676 if *tests[i].blockNumber > -1 {677 require.Nil(t, s.CreateTransactions(ctx, []*model.Transaction{tx}))678 }679 require.Nil(t, s.Broadcast(ctx, tx))680 }681 for i, tx := range transactions {682 _, err := db.ExecContext(context.Background(), fmt.Sprintf("UPDATE xtz_tx SET created_at_block = %d, broadcasted = true, status = %d WHERE hash = '%s'", tests[i].createdAtBlock, common_model.PENDING, tx.Hash))683 require.Nil(t, err)684 }685 for i := range transactions {686 hashes, err := s.GetBroadcastsToGarbageCollect(ctx, tests[i].createdAtBlock)687 require.Nil(t, err)688 require.Nil(t, s.GarbageCollectBroadcasts(ctx, hashes))689 for j, tx := range transactions {690 var rep, err = s.GetTransactions(ctx, []string{tx.Hash})691 require.Nil(t, err)692 require.Len(t, rep, 1)693 if tx.BlockNumber != nil {694 require.Equal(t, "pending", rep[0].Status)695 } else if j <= i && tx.BlockNumber == nil {696 require.Equal(t, "timeout", rep[0].Status)697 }698 }699 }700}701func TestIntLBPutConflict(t *testing.T) {702 var db = helper.Setup(currency)703 defer helper.Cleanup(currency, db)704 s := NewTransactionStorage(db)705 var broadcastedTransactions = randomBroadcastedEntries(100)706 var ctx = context.Background()707 // Put transactions in store.708 require.Nil(t, s.CreateTransactions(ctx, broadcastedTransactions))709 var randomTransactions = randomEntries(100)710 for i := range broadcastedTransactions {711 randomTransactions[i].Hash = broadcastedTransactions[i].Hash712 randomTransactions[i].Amount = broadcastedTransactions[i].Amount713 }714 require.Nil(t, s.CreateTransactions(ctx, randomTransactions))715 // Check boolean was set in storage.716 for i := range randomTransactions {717 var rep, err = s.GetTransactions(ctx, []string{randomTransactions[i].Hash})718 require.Nil(t, err)719 require.Len(t, rep, 1)720 require.Equal(t, rep[0].Hash, broadcastedTransactions[i].Hash)721 require.Equal(t, rep[0].BlockNumber, randomTransactions[i].BlockNumber)722 require.Equal(t, rep[0].DestinationAddress, randomTransactions[i].DestinationAddress)723 require.Equal(t, rep[0].SourceAddress, randomTransactions[i].SourceAddress)724 require.Equal(t, rep[0].Fee, randomTransactions[i].Fee)725 require.Nil(t, rep[0].RawTransaction)726 require.False(t, rep[0].Broadcasted)727 require.Equal(t, rep[0].Status, "success")728 require.Nil(t, rep[0].Message)729 }730}731func TestIntLBUpdateBroadcast(t *testing.T) {732 var db = helper.Setup(currency)733 defer helper.Cleanup(currency, db)734 s := NewTransactionStorage(db)735 var ctx = context.Background()736 var broadcastedTransaction = randomBroadcastedEntries(1)[0]737 require.Nil(t, s.CreateTransactions(ctx, []*model.Transaction{broadcastedTransaction}))738 var (739 hash = broadcastedTransaction.Hash740 newStatus = "temporary_failure"741 newMessage = helper.FromString("Dummy message")742 broadcasterAtBlock = uint64(10000)743 )744 require.Nil(t, s.UpdateBroadcast(ctx, hash, newStatus, *newMessage, broadcasterAtBlock))745 var txs, err = s.GetTransactions(ctx, []string{hash})746 require.Nil(t, err)747 require.Len(t, txs, 1)748 require.Equal(t, txs[0].Hash, hash)749 require.Equal(t, txs[0].Status, newStatus)750 require.Equal(t, txs[0].Message, newMessage)751 require.NotNil(t, txs[0].BroadcastedAtBlock)752 require.Equal(t, *txs[0].BroadcastedAtBlock, broadcasterAtBlock)753}754func TestIntLBBroadcast(t *testing.T) {755 var db = helper.Setup(currency)756 defer helper.Cleanup(currency, db)757 s := NewTransactionStorage(db)758 var ctx = context.Background()759 now := time.Now()760 var broadcastedTransaction = randomBroadcastedEntries(1)[0]761 require.Nil(t, s.Broadcast(ctx, broadcastedTransaction))762 var (763 hash = broadcastedTransaction.Hash764 )765 var txs, err = s.GetTransactions(ctx, []string{hash})766 require.Nil(t, err)767 require.Equal(t, txs[0].Hash, hash)768 require.Equal(t, txs[0].Timestamp, broadcastedTransaction.Timestamp)769 require.Equal(t, txs[0].Status, broadcastedTransaction.Status)770 require.Equal(t, txs[0].Message, broadcastedTransaction.Message)771 require.True(t, txs[0].CreatedAt.After(now))772}773func TestIntLBBroadcastConflict(t *testing.T) {774 var db = helper.Setup(currency)775 defer helper.Cleanup(currency, db)776 s := NewTransactionStorage(db)777 var ctx = context.Background()778 var btx = randomEntries(1)[0]779 require.Nil(t, s.CreateTransactions(ctx, []*model.Transaction{btx}))780 var (781 hash = btx.Hash782 rawTx = randomHexString(64)783 )784 var stx = &model.Transaction{785 Hash: hash,786 RawTransaction: &rawTx,787 Amount: big.NewInt(rand.Int63()),788 Fee: big.NewInt(rand.Int63()),789 Broadcasted: true,790 Status: "pending",791 Message: helper.FromString("Failure"),792 }793 require.Nil(t, s.Broadcast(ctx, stx))794 var rep, err = s.GetTransactions(ctx, []string{btx.Hash})795 require.Nil(t, err)796 require.Len(t, rep, 1)797 require.Equal(t, rep[0].Hash, btx.Hash)798 require.Equal(t, rep[0].BlockNumber, btx.BlockNumber)799 require.Equal(t, rep[0].DestinationAddress, btx.DestinationAddress)800 require.Equal(t, rep[0].SourceAddress, btx.SourceAddress)801 require.Equal(t, rep[0].Fee, btx.Fee)802 require.Nil(t, rep[0].RawTransaction)803 require.Equal(t, rep[0].Broadcasted, true)804 require.Equal(t, rep[0].Status, "new")805 require.Nil(t, rep[0].Message)806}807func TestIntLBGetPendingBroadcasts(t *testing.T) {808 var db = helper.Setup(currency)809 defer helper.Cleanup(currency, db)810 s := NewTransactionStorage(db)811 var (812 ctx = context.Background()813 timeZero = time.Time{}.Add(time.Second)814 transactions = []*model.Transaction{815 {Hash: randomHexString(32), BlockNumber: nil, RawTransaction: helper.FromString(randomHexString(64)), Fee: big.NewInt(0), Amount: big.NewInt(0), Timestamp: helper.FromTimestamp(timeZero), Broadcasted: true, Status: "temporary_failure"},816 {Hash: randomHexString(32), BlockNumber: nil, RawTransaction: helper.FromString(randomHexString(64)), Fee: big.NewInt(0), Amount: big.NewInt(0), Timestamp: helper.FromTimestamp(timeZero.Add(10 * time.Second)), Broadcasted: true, Status: "pending"},817 {Hash: randomHexString(32), BlockNumber: nil, RawTransaction: helper.FromString(randomHexString(64)), Fee: big.NewInt(0), Amount: big.NewInt(0), Timestamp: helper.FromTimestamp(timeZero.Add(20 * time.Second)), Broadcasted: true, Status: "temporary_failure"},818 {Hash: randomHexString(32), BlockNumber: nil, RawTransaction: helper.FromString(randomHexString(64)), Fee: big.NewInt(0), Amount: big.NewInt(0), Timestamp: helper.FromTimestamp(timeZero.Add(30 * time.Second)), Broadcasted: true, Status: "new"},819 {Hash: randomHexString(32), BlockNumber: nil, RawTransaction: helper.FromString(randomHexString(64)), Fee: big.NewInt(0), Amount: big.NewInt(0), Timestamp: helper.FromTimestamp(timeZero), Broadcasted: false, Status: "pending"},820 {Hash: randomHexString(32), BlockNumber: nil, RawTransaction: helper.FromString(randomHexString(64)), Fee: big.NewInt(0), Amount: big.NewInt(0), Timestamp: helper.FromTimestamp(timeZero.Add(10 * time.Second)), Broadcasted: false, Status: "temporary_failure"},821 {Hash: randomHexString(32), BlockNumber: nil, RawTransaction: helper.FromString(randomHexString(64)), Fee: big.NewInt(0), Amount: big.NewInt(0), Timestamp: helper.FromTimestamp(timeZero.Add(20 * time.Second)), Broadcasted: false, Status: "temporary_failure"},822 {Hash: randomHexString(32), BlockNumber: nil, RawTransaction: helper.FromString(randomHexString(64)), Fee: big.NewInt(0), Amount: big.NewInt(0), Timestamp: helper.FromTimestamp(timeZero.Add(30 * time.Second)), Broadcasted: false, Status: "pending"},823 {Hash: randomHexString(32), BlockNumber: nil, RawTransaction: helper.FromString(randomHexString(64)), Fee: big.NewInt(0), Amount: big.NewInt(0), Timestamp: helper.FromTimestamp(timeZero), Broadcasted: true, Status: "invalid"},824 {Hash: randomHexString(32), BlockNumber: nil, RawTransaction: helper.FromString(randomHexString(64)), Fee: big.NewInt(0), Amount: big.NewInt(0), Timestamp: helper.FromTimestamp(timeZero.Add(10 * time.Second)), Broadcasted: true, Status: "invalid"},825 {Hash: randomHexString(32), BlockNumber: nil, RawTransaction: helper.FromString(randomHexString(64)), Fee: big.NewInt(0), Amount: big.NewInt(0), Timestamp: helper.FromTimestamp(timeZero.Add(20 * time.Second)), Broadcasted: false, Status: "invalid"},826 {Hash: randomHexString(32), BlockNumber: nil, RawTransaction: helper.FromString(randomHexString(64)), Fee: big.NewInt(0), Amount: big.NewInt(0), Timestamp: helper.FromTimestamp(timeZero.Add(30 * time.Second)), Broadcasted: true, Status: "invalid"},827 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(1), Fee: big.NewInt(0), Amount: big.NewInt(0), Timestamp: helper.FromTimestamp(timeZero), Broadcasted: true, Status: "temporary_failure"},828 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(1), Fee: big.NewInt(0), Amount: big.NewInt(0), Timestamp: helper.FromTimestamp(timeZero.Add(10 * time.Second)), Broadcasted: true, Status: "temporary_failure"},829 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(1), Fee: big.NewInt(0), Amount: big.NewInt(0), Timestamp: helper.FromTimestamp(timeZero.Add(20 * time.Second)), Broadcasted: true, Status: "pending"},830 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(1), Fee: big.NewInt(0), Amount: big.NewInt(0), Timestamp: helper.FromTimestamp(timeZero.Add(30 * time.Second)), Broadcasted: true, Status: "pending"},831 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(1), Fee: big.NewInt(0), Amount: big.NewInt(0), Timestamp: helper.FromTimestamp(timeZero), Broadcasted: true, Status: "invalid"},832 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(1), Fee: big.NewInt(0), Amount: big.NewInt(0), Timestamp: helper.FromTimestamp(timeZero.Add(10 * time.Second)), Broadcasted: true, Status: "invalid"},833 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(1), Fee: big.NewInt(0), Amount: big.NewInt(0), Timestamp: helper.FromTimestamp(timeZero.Add(20 * time.Second)), Broadcasted: false, Status: "invalid"},834 {Hash: randomHexString(32), BlockNumber: helper.FromUint64(1), Fee: big.NewInt(0), Amount: big.NewInt(0), Timestamp: helper.FromTimestamp(timeZero.Add(30 * time.Second)), Broadcasted: true, Status: "invalid"},835 }836 )837 // Put transactions in store.838 for _, transaction := range transactions {839 if transaction.Broadcasted && transaction.BlockNumber == nil {840 require.Nil(t, s.Broadcast(ctx, transaction))841 } else {842 if transaction.BlockNumber == nil {843 transaction.BlockNumber = helper.FromUint64(0)844 }845 require.Nil(t, s.CreateTransactions(ctx, []*model.Transaction{transaction}))846 }847 _, err := db.ExecContext(context.Background(), fmt.Sprintf("UPDATE xtz_tx SET broadcasted = %t, status = %d WHERE hash = '%s'", transaction.Broadcasted, common_model.ToStatus(transaction.Status), transaction.Hash))848 require.Nil(t, err)849 }850 var tests = []struct {851 beforeBlock uint64852 limitBy uint64853 expectedTxN int854 }{855 {beforeBlock: 500031, limitBy: 4, expectedTxN: 4},856 {beforeBlock: 500031, limitBy: 2, expectedTxN: 2},857 {beforeBlock: 500030, limitBy: 3, expectedTxN: 3},858 {beforeBlock: 500030, limitBy: 1, expectedTxN: 1},859 {beforeBlock: 500020, limitBy: 2, expectedTxN: 2},860 {beforeBlock: 500020, limitBy: 1, expectedTxN: 1},861 {beforeBlock: 500010, limitBy: 1, expectedTxN: 1},862 {beforeBlock: 500010, limitBy: 0, expectedTxN: 0},863 }864 for _, test := range tests {865 var txs, err = s.GetPendingBroadcasts(ctx, test.beforeBlock, test.limitBy)866 require.Nil(t, err)867 require.Len(t, txs, test.expectedTxN)868 }869}870// return n random chunk entries.871func randomEntries(n int) []*model.Transaction {872 var entries = make([]*model.Transaction, n)873 for i := 0; i < n; i++ {874 entries[i] = &model.Transaction{875 Hash: randomHexString(32),876 BlockNumber: helper.FromUint64(uint64(rand.Int63())),877 DestinationAddress: helper.FromString(randomHexString(20)),878 SourceAddress: helper.FromString(randomHexString(20)),879 Amount: big.NewInt(rand.Int63()),880 Fee: big.NewInt(rand.Int63()),881 Status: "success",882 CreatedAt: nowRounded(),883 }884 }885 return entries886}887// return n random chunk entries.888func randomBroadcastedEntries(n int) []*model.Transaction {889 var entries = make([]*model.Transaction, n)890 for i := 0; i < n; i++ {891 entries[i] = &model.Transaction{892 Hash: randomHexString(32),893 Amount: big.NewInt(rand.Int63()),894 Broadcasted: true,895 BlockNumber: helper.FromUint64(uint64(rand.Int63())),896 RawTransaction: helper.FromString(randomHexString(64)),897 CreatedAt: nowRounded(),898 Timestamp: nowRounded(),899 Status: "new",900 }901 }902 return entries903}904func randomHexString(nbrBytes int) string {905 var b = make([]byte, nbrBytes)906 rand.Read(b)907 return fmt.Sprintf("0x%x", b)908}...

Full Screen

Full Screen

merkle_test.go

Source:merkle_test.go Github

copy

Full Screen

...77 return h78 }79 // Block 153342 in bitcoin80 wantMerkle, _ := hex.DecodeString("dd8ee246e19ec5c77ddd46c1138e8af6a272da4dbb6500ea74a79c0bf89e2c07")81 h1, _ := hash.FromString("216404816ca6261f9206d471d0403ba49bda4264719d879819fbda9849781e62")82 h2, _ := hash.FromString("56f2602c15cb0b8e0b38e54b2961a2e541a7febbe852516cd425aa5fb72c5578")83 h3, _ := hash.FromString("0d065da59871386321c2c9b2e4b6482426bcce88600ab7f55f0d27b9916a9e0c")84 h4, _ := hash.FromString("1129038c38783f4c4241e54d9d702965b305b8d1e54c091fdd9f9df21240586e")85 h5, _ := hash.FromString("81461f9e0e093dad14d0c5fb3978431a321bf61de33512d6cc344edb86f359f3")86 h6, _ := hash.FromString("22140f4b15d76ff27d657a731fdc3040487c22ee3577c6522239d9cfbe0292ad")87 h7, _ := hash.FromString("0fa273bce5137a0dbffac068ebb6f1ebe64e6be2b00cdae5a967edeb0cd96b93")88 h8, _ := hash.FromString("cab481631e7f2f7d864a65d23c34bd357f46ecba60bb8117f55ed43232aa75e5")89 h9, _ := hash.FromString("dffea4c267fa6949111fed23b15977d5e2efa82fefd9cd5ac81e38518d2c2bef")90 h10, _ := hash.FromString("ed9f4ee5e07a47a7026725173de32efa7372243117be1aa7f60a650aef075475")91 h11, _ := hash.FromString("8822c80afa3eb84bc3603509b8b6deeee37cf771ca7b49d3dd73294e05f7b29f")92 h12, _ := hash.FromString("23ad44934167cc712b358f2a097b7316ca2b3c2f34472017273969e7c7e5cdb4")93 h13, _ := hash.FromString("c1dc3762c6a57757a9aa895b8229613d96f272f79d14c9854132b980eaa2a2c4")94 root, _ := hash.FromBytes(wantMerkle)95 hashes := []hash.Hash{96 h1, h2, h3, h4, h5, h6, h7, h8, h9, h10, h11, h12, h13,97 }98 tree := NewTreeFromHashes(hashes)99 assert.True(t, tree.Root().EqualsTo(root))100 assert.Equal(t, tree.Depth(), 4)101 fmt.Println(tree.ToString())102 assert.Contains(t, tree.ToString(), root.String())103 right, _ := hash.FromString("4a3ee07bb7baf6dfa265fa5c85a8955c8e79ddab0f70657a14df5744a103e24d")104 left, _ := hash.FromString("114799e25e6dc376d65fd5406516919e1e619b89316be91ea064a69400472d1e")105 root2 := HashMerkleBranches(&left, &right)106 assert.True(t, root.EqualsTo(*root2))107}...

Full Screen

Full Screen

hash_test.go

Source:hash_test.go Github

copy

Full Screen

...7 "github.com/stretchr/testify/require"8)9func TestSetUp(t *testing.T) {10 // check supported hashes11 sha256, err := hash.FromString("SHA256")12 require.NotNil(t, sha256, "missing SHA256")13 require.Nil(t, err, "should return no error")14 sha512, err := hash.FromString("SHA512")15 require.NotNil(t, sha512, "missing SHA512")16 require.Nil(t, err, "should return no error")17 blake2s, err := hash.FromString("BLAKE2s")18 require.NotNil(t, blake2s, "missing BLAKE2s")19 require.Nil(t, err, "should return no error")20 blake2b, err := hash.FromString("BLAKE2b")21 require.NotNil(t, blake2b, "missing BLAKE2b")22 require.Nil(t, err, "should return no error")23 // check return empty24 yy, err := hash.FromString("yy")25 require.Nil(t, yy, "yy does not exist, yet")26 require.NotNil(t, err, "should return an error")27 require.Equal(t, len("BLAKE2b, BLAKE2s, SHA256, SHA512"),28 len(hash.SupportedHashes()),29 "hash BLAKE2b, BLAKE2s, SHA256, SHA512 should be returned")30}31func TestHash(t *testing.T) {32 message := []byte("noise")33 testParams := []struct {34 name string35 hashLen int36 blockLen int37 digest string38 }{39 {"SHA256", 32, 64,40 "efe2a5c09e6d49d7eb735c9875f77404be5b887bb3f56378038968d4e3ff8198"},41 {"SHA512", 64, 128,42 "639536fe3055ba1c7e6a5fc53219f61cc6560b2045d36efa52da5e8547518" +43 "980545af2fa2db7a5b5c422d882d0bd8effb5b288b1d6fe0389253ec7" +44 "863bce113e"},45 {"BLAKE2s", 32, 64,46 "b21550f9332cb8f61ac92955e23447b18ad3452d71dd698f0190cb88718cf6da"},47 {"BLAKE2b", 64, 128,48 "d26cfaa295a94914bdedd0ceb2b955c80e4f1e7b33b37be4a16e9717021b76" +49 "091a12d9931e6de1bfe8844a41bcadc0bc0e0269886f0f11e83104a4f0" +50 "f92e20ad"},51 }52 for _, tt := range testParams {53 name := "test hash " + tt.name54 t.Run(name, func(t *testing.T) {55 h, err := hash.FromString(tt.name)56 hf := h.New()57 // no error58 require.Nil(t, err, "no error should be returned")59 // hash name60 require.Equal(t, tt.name, h.String(), "name mismatch")61 // block len62 require.Equal(t, tt.blockLen, h.BlockLen(), "block len mismatch")63 // hash len64 require.Equal(t, tt.hashLen, h.HashLen(), "hash len mismatch")65 // hash output66 _, _ = hf.Write(message)67 hashDigest := hex.EncodeToString(hf.Sum(nil))68 require.Equal(t, tt.digest, hashDigest, "hash digest is wrong")69 // test reset70 _, _ = hf.Write(message)71 hashDigest = hex.EncodeToString(hf.Sum(nil))72 require.NotEqual(t, tt.digest, hashDigest, "hash digest is wrong")73 hf.Reset()74 _, _ = hf.Write(message)75 hashDigest = hex.EncodeToString(hf.Sum(nil))76 require.Equal(t, tt.digest, hashDigest, "hash digest is wrong")77 })78 }79}80func ExampleFromString() {81 // load hash sha25682 sha256, _ := hash.FromString("SHA256")83 fmt.Println(sha256)84 // load hash sha51285 sha512, _ := hash.FromString("SHA512")86 fmt.Println(sha512)87 // load hash blake2s88 blake2s, _ := hash.FromString("BLAKE2s")89 fmt.Println(blake2s)90 // load hash blake2b91 blake2b, _ := hash.FromString("BLAKE2b")92 fmt.Println(blake2b)93}...

Full Screen

Full Screen

FromString

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 h := sha256.New()4 h.Write([]byte("test"))5 bs := h.Sum(nil)6 fmt.Println(bs)7}8import (9func main() {10 h := sha256.New()11 bs := h.Sum([]byte("test"))12 fmt.Println(bs)13}14import (15func main() {16 bs := sha256.Sum256([]byte("test"))17 fmt.Println(bs)18}19import (20func main() {21 bs := sha256.Sum224([]byte("test"))22 fmt.Println(bs)23}

Full Screen

Full Screen

FromString

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 h := crc32.NewIEEE()4 h.Write([]byte("test"))5 v := h.Sum32()6 fmt.Println(v)7 h.Reset()8 h.Write([]byte("test"))9 if v2 := h.Sum32(); v != v2 {10 fmt.Printf("checksum error %d != %d11 }12 h.Reset()13 h.Write([]byte("test"))14 v3 := crc32.ChecksumIEEE([]byte("test"))15 if v != v3 {16 fmt.Printf("checksum error %d != %d17 }18 h.Reset()19 h.Write([]byte("test"))20 v4 := crc32.ChecksumIEEE([]byte("test"))21 if v != v4 {22 fmt.Printf("checksum error %d != %d23 }24}25import (26func main() {27 h := crc32.NewIEEE()28 h.Write([]byte("test"))29 v := h.Sum32()30 fmt.Println(v)31 h.Reset()32 h.Write([]byte("test"))33 if v2 := h.Sum32(); v != v2 {34 fmt.Printf("checksum error %d != %d35 }36 h.Reset()37 h.Write([]byte("test"))38 v3 := crc32.ChecksumIEEE([]byte("test"))39 if v != v3 {40 fmt.Printf("checksum error %d != %d41 }42 h.Reset()43 h.Write([]byte("test"))44 v4 := crc32.ChecksumIEEE([]byte("test"))45 if v != v4 {46 fmt.Printf("checksum error %d != %d47 }48}49import (50func main() {51 h := crc32.NewIEEE()52 h.Write([]byte("test"))53 v := h.Sum32()54 fmt.Println(v)55 h.Reset()56 h.Write([]byte("test"))57 if v2 := h.Sum32(); v != v2 {58 fmt.Printf("checksum error %d != %d

Full Screen

Full Screen

FromString

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 h := crc32.NewIEEE()4 s := []byte("test string")5 h.Write(s)6 v := h.Sum32()7 fmt.Println(v)8}9import (10func main() {11 h := crc32.NewIEEE()12 s := []byte("test string")13 h.Write(s)14 v := h.Sum32()15 fmt.Println(v)16}17import (18func main() {19 h := crc32.NewIEEE()20 s := []byte("test string")21 h.Write(s)22 v := h.Sum32()23 fmt.Println(v)24}25import (26func main() {27 h := crc32.NewIEEE()28 s := []byte("test string")29 h.Write(s)30 v := h.Sum32()31 fmt.Println(v)32}

Full Screen

Full Screen

FromString

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 h := sha256.New()4 h.Write([]byte("test"))5 bs := h.Sum(nil)6 fmt.Println(bs)7}8import (9func main() {10 h := sha256.New()11 h.Write([]byte("test"))12 bs := h.Sum(nil)13 fmt.Println(bs)14 str := fmt.Sprintf("%x", bs)15 fmt.Println(str)16}17import (18func main() {19 h := sha256.New()20 h.Write([]byte("test"))21 bs := h.Sum(nil)22 fmt.Println(bs)23 str := fmt.Sprintf("%x", bs)24 fmt.Println(str)25 str2 := fmt.Sprintf("%T", str)26 fmt.Println(str2)27}

Full Screen

Full Screen

FromString

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "crypto/md5"3func main() {4 fmt.Printf("%x\n", md5.Sum([]byte(data)))5}6import "fmt"7import "crypto/md5"8func main() {9 h := md5.New()10 h.Write([]byte(data))11 fmt.Printf("%x\n", h.Sum(nil))12}13import "fmt"14import "crypto/md5"15func main() {16 h := md5.New()17 h.Write([]byte(data))18 fmt.Printf("%x\n", h.Sum([]byte("")))19}20import "fmt"21import "crypto/md5"22func main() {23 h := md5.New()24 h.Write([]byte(data))25 fmt.Printf("%x\n", h.Sum([]byte("hello")))26}27import "fmt"28import "crypto/md5"29func main() {30 h := md5.New()31 h.Write([]byte(data))32 fmt.Printf("%x\n", h.Sum([]byte("hello")))33}34import "fmt"35import "crypto/md5"

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Syzkaller automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful