How blockchain verifies transaction amount is valid - bitcoin

I'm new to blockchain. I understand that blockchain keeps records of all transactions and each transaction is signed with private key. However, why cannot anyone enter an arbitrary amount of Bitcoin transaction? Say, address a only has 1 Bitcoin, but its owner can create a transaction of 100 Bitcoins and still sign it. What is Bitcoin's mechanism to verify the outgoing and incoming amounts of a transaction?

Bitcoin's blockchain contains a historical record of all transactions which have ever occured on it. Clients can certainly choose to store less, and the blockchain can be pruned by not storing transactions which have already been spent long-ago.
Bitcoin addresses don't technically have a "balance" in the sense of a traditional bank ledger. Instead, an address has the ability to spend transactions which were sent to it.
To delve into technical details, let's look at the address 1PkCAVKjPz1YK7iJwT8xTLxBXR1av8dL98 (which I own).
I received a very small transaction of 0.004 BTC recently, in the transaction with the TxID 432794be2e056275cafb0eeb7ab59a24444dd4c9e00cd9702a49c2a655a3e705.
The (hex-encoded) raw data of this transaction is: 0100000001e9a24c1d1b8d10b13482cdcbbb90d894577292c4d0c0c1427411fb9d82ea710c010000006b483045022100d9a5433c1381b39b7e02b0b0f042990e7c16cfea252b05ccfef2e85c2dab2a6f022057c7def782fe3b0d7e5e0eae277d2a5890844da7d72309817a2dac22a6307c6001210390d78cb0c1d34d4417db7e0a9a9f125a689dc29dc2197a01a5f827a20f870f62ffffffff01801a0600000000001976a914f97df8f593e0056d337c274fd81a163f47a17d3788ac00000000
Which in its human-readable form is:
{
"txid": "432794be2e056275cafb0eeb7ab59a24444dd4c9e00cd9702a49c2a655a3e705",
"size": 192,
"version": 1,
"locktime": 0,
"vin": [
{
"txid": "0c71ea829dfb117442c1c0d0c492725794d890bbcbcd8234b1108d1b1d4ca2e9",
"vout": 1,
"scriptSig": {
"asm": "3045022100d9a5433c1381b39b7e02b0b0f042990e7c16cfea252b05ccfef2e85c2dab2a6f022057c7def782fe3b0d7e5e0eae277d2a5890844da7d72309817a2dac22a6307c60[ALL] 0390d78cb0c1d34d4417db7e0a9a9f125a689dc29dc2197a01a5f827a20f870f62",
"hex": "483045022100d9a5433c1381b39b7e02b0b0f042990e7c16cfea252b05ccfef2e85c2dab2a6f022057c7def782fe3b0d7e5e0eae277d2a5890844da7d72309817a2dac22a6307c6001210390d78cb0c1d34d4417db7e0a9a9f125a689dc29dc2197a01a5f827a20f870f62"
},
"sequence": 4294967295
}
],
"vout": [
{
"value": 0.00400000,
"n": 0,
"scriptPubKey": {
"asm": "OP_DUP OP_HASH160 f97df8f593e0056d337c274fd81a163f47a17d37 OP_EQUALVERIFY OP_CHECKSIG",
"hex": "76a914f97df8f593e0056d337c274fd81a163f47a17d3788ac",
"reqSigs": 1,
"type": "pubkeyhash",
"addresses": [
"1PkCAVKjPz1YK7iJwT8xTLxBXR1av8dL98"
]
}
}
]
}
So the address 1PkCAVKjPz1YK7iJwT8xTLxBXR1av8dL98 is able to "spend" the transaction 432794be2e056275cafb0eeb7ab59a24444dd4c9e00cd9702a49c2a655a3e705.
The output value of that transaction is 0.004 BTC, so I can't make a Bitcoin transaction which attempts to spend more. However, let's try to do it anyway.
I'll create a raw transaction which attempts to output 0.01 BTC to 1MgLu9L7ftmGQM84xhKYKw8pTXiSANwggs from the transaction with an output balance of 0.004 BTC:
bitcoin-rpc createrawtransaction '[{"txid":"432794be2e056275cafb0eeb7ab59a24444dd4c9e00cd9702a49c2a655a3e705","vout":0}]' '{"1MgLu9L7ftmGQM84xhKYKw8pTXiSANwggs":0.01}'
Returns the raw transaction:
010000000105e7a355a6c2492a70d90ce0c9d44d44249ab57aeb0efbca7562052ebe9427430000000000ffffffff0140420f00000000001976a914e2d3595bd0a55c16f4b19f5cd996568dd7e811f688ac00000000
I can then sign the transaction:
bitcoin-rpc signrawtransaction 010000000105e7a355a6c2492a70d90ce0c9d44d44249ab57aeb0efbca7562052ebe9427430000000000ffffffff0140420f00000000001976a914e2d3595bd0a55c16f4b19f5cd996568dd7e811f688ac00000000
which returns:
{
"hex": "010000000105e7a355a6c2492a70d90ce0c9d44d44249ab57aeb0efbca7562052ebe942743000000006b483045022100ce3fad8ccdee48f1fe9060ef81624d3bbe721293feb8ee06a96751e65b9c423e0220106a3e80d5fdf93df5dbf037d8cfd32af70a405586e12294c937308a3c57b10e012102f2acb810346866908108dd86462ee5400b15786739f5e908711d2d15d9dd2238ffffffff0140420f00000000001976a914e2d3595bd0a55c16f4b19f5cd996568dd7e811f688ac00000000",
"complete": true
}
And I can take that returned hex, which is a validly-formatted transaction, and submit it to the network:
bitcoin-rpc sendrawtransaction 010000000105e7a355a6c2492a70d90ce0c9d44d44249ab57aeb0efbca7562052ebe942743000000006b483045022100ce3fad8ccdee48f1fe9060ef81624d3bbe721293feb8ee06a96751e65b9c423e0220106a3e80d5fdf93df5dbf037d8cfd32af70a405586e12294c937308a3c57b10e012102f2acb810346866908108dd86462ee5400b15786739f5e908711d2d15d9dd2238ffffffff0140420f00000000001976a914e2d3595bd0a55c16f4b19f5cd996568dd7e811f688ac00000000
Which gives me the error:
66: insufficient priority (code -26)
This is a client-side error, but if I were to successfully broadcast the raw transaction to the network, other peers would simply look up the referenced (or "spent") transaction 432794be2e056275cafb0eeb7ab59a24444dd4c9e00cd9702a49c2a655a3e705 and see that the output total of my new transaction is greater than the output total of the transaction I'm attempting to spend.
There is one exception to this rule: coinbase transactions generate Bitcoins for miners, and thus are allowed to output the correct block subsidy (originally 50 BTC, but currently 12.5 BTC after the halving about a month and a half ago) plus the transaction fees of all of the transactions contained within the block.

I know this post is already old but there is a complete list for validating a bitcoin transcation:
https://en.bitcoin.it/wiki/Protocol_rules#.22tx.22_messages

Maybe this link on how bitcoin transactions work will help you. Look at the section called "What if the input and output amounts don’t match?"
Also, since Blockchain uses a distributed ledger, all nodes will validate a transaction before it is accepted. Furthermore there should be auditors on the chain that make sure fraudulent activities don't happen. Hope this helps.

Related

In my js file for testing .I call a send transaction to the smart contract, so what is the difference between value and gas :

it("allows a manager to make a payment request f(createRequest) ", async ()=> {
await campaign.methods.createRequest('buy beer', accounts[2], '100').send({
from: accounts[0],
gas: '1000000'
});
it('Contribute money from another account & checks whether it is approved or not', async () =>{
await campaign.methods.contribute().send({
from: accounts[1],
value: '200'
});
I want to know deciding factors, when to use gas and when to use value?
value is the amount of the native token that you send with the transaction.
Network
Native token
Ethereum
ETH
Binance Smart Chain
BNB
Tron
TRX
It's expressed in the smallest non-divisible unit. In case of ETH, that's wei. 1 ETH is 10^18 wei.
So as per your example, when you set the value to 200, you're going to send along 0.0000000000000002 ETH to the contract with the execution of the contribute() function.
An example use of the value is when a contract wants to sell you a token for 0.1 ETH. In this case, you set the value to 0.1 ETH while executing the contract's buy() function.
The value does NOT replace the gas fee:
gas is the amount of the fee that you send along with the transaction. For better explanation, what gas is, there's a great post on the Ethereum StackExchange.
But in short - gas is a way of payment for the execution of the smart contract function.
The minimal amount of gas required to execute the function can be usually calculated using the web3 estimateGas() method (there are some exceptions when the estimate is incorrect or impossible to calculate).
Depending on the gasPrice (that's either calculated automatically from recent data or you can overwrite it manually), the total transaction fee is calculated in the native token (e.g. ETH).

How frequently are the Azure Storage Queue metrics updated?

I observed that it took about 6 hours from the time of setting up Diagnostics (the newer offering still in preview) for the Queue Message Count metric to move from 0 to the actual total number of messages in queue. The other capacity metrics Queue Capacity and Queue Count took about 1 hour to reflect actual values.
Can anyone shed light on how these metrics are updated? It would be good to know how to predict the accuracy of the graphs.
I am concerned because if the latency of these metrics is typically this large then an alert based on queue metrics could take too long to raise.
Update:
Platform metrics are created by Azure resources and give you visibility into their health and performance. Each type of resource creates a distinct set of metrics without any configuration required. Platform metrics are collected from Azure resources at one-minute frequency unless specified otherwise in the metric's definition.
And 'Queue Message Count' is platform metrics.
So it should update the data every 1 minute.
But it didn't. And this is not a problem that only occur on portal. Even you use rest api to get the QueueMessageCount, it still not update after 1 minute:
https://management.azure.com/subscriptions/xxx-xxx-xxx-xxx-xxx/resourceGroups/0730BowmanWindow/providers/Microsoft.Storage/storageAccounts/0730bowmanwindow/queueServices/default/providers/microsoft.insights/metrics?interval=PT1H&metricnames=QueueMessageCount&aggregation=Average&top=100&orderby=Average&api-version=2018-01-01&metricnamespace=Microsoft.Storage/storageAccounts/queueServices
{
"cost": 59,
"timespan": "2021-05-17T08:57:56Z/2021-05-17T09:57:56Z",
"interval": "PT1H",
"value": [
{
"id": "/subscriptions/xxx-xxx-xxx-xxx-xxx/resourceGroups/0730BowmanWindow/providers/Microsoft.Storage/storageAccounts/0730bowmanwindow/queueServices/default/providers/Microsoft.Insights/metrics/QueueMessageCount",
"type": "Microsoft.Insights/metrics",
"name": {
"value": "QueueMessageCount",
"localizedValue": "Queue Message Count"
},
"displayDescription": "The number of unexpired queue messages in the storage account.",
"unit": "Count",
"timeseries": [
{
"metadatavalues": [],
"data": [
{
"timeStamp": "2021-05-17T08:57:00Z",
"average": 1.0
}
]
}
],
"errorCode": "Success"
}
],
"namespace": "Microsoft.Storage/storageAccounts/queueServices",
"resourceregion": "centralus"
}
This may be an issue that needs to be reported to the azure team. It is so slow, it even loses its practicality. I think send an alert based on this is a bad thing(it’s too slow).
Maybe you can design you own logic by code to check the QueueMessageCount.
Just a sample(C#):
1, Get Queues
Then get all of the queue names.
2, Get Properties
Then get the number of the message in each queue.
3, sum the obtained numbers.
4, send custom alert.
Original Answer:
At first, after I send message to one queue in queue storage, the 'Queue Message Count' also remains stubbornly at zero on my side, but a few hours later it can get the 'Queue Message Count':
I thought it would be a bug, but it seems to work well now.

Azure Function Apps - maintain max batch size with maxDequeueCount

I have following host file:
{
"version": "2.0",
"extensions": {
"queues": {
"maxPollingInterval": "00:00:02",
"visibilityTimeout": "00:00:30",
"batchSize": 16,
"maxDequeueCount": 3,
"newBatchThreshold": 8
}
}
}
I would expect with setup there could never be more than batchSize+newBatchThreshold number of instances running. But I realized when messages are dequed they are run instantly and not just added to the back of the queue. This means you can end up with a very high amount of instances causing a lot of 429 (to many requests). Is there anyway to configure the function app to just add the dequeded messages to the back of the queue?
It was not related to dequeueCount. The problem was because it was a consumption plan, and then you cant control the amount of instances. After chaning to a Standard plan it worked as expected.

How does full node verify transaction if all it has bitcoin address and no public key

If it’s impossible to get public address from bitcoin address. How can a full node performing the transaction verify that the transaction is coming from authorised user.
The core of bitcoin is he language, Bitcoin Script, this is a language not Turing complete because is without loop.
The node bitcoin doesn't need the public key for spending the bitcoin, but a transaction input have an unlocked script (known as ScriptSig) this transaction can unlock a previous output transaction with have a locked script(Know as Script pubkey), for unlocked the node executed in the stack the ScriptSig + ScrptPubKey, if return true the transaction is spendable otherwise no.
an example
if you have two transaction
Input:
Previous tx: f5d8ee39a430901c91a5917b9f2dc19d6d1a0e9cea205b009ca73dd04470b9a6
Index: 0
scriptSig: 304502206e21798a42fae0e854281abd38bacd1aeed3ee3738d9e1446618c4571d10
90db022100e2ac980643b0b82c0e88ffdfec6b64e3e6ba35e7ba5fdd7d5d6cc8d25c6b241501
Output:
Value: 5000000000
scriptPubKey: OP_DUP OP_HASH160 404371705fa9bd789a2fcd52d2c580b65d35549d
OP_EQUALVERIFY OP_CHECKSIG
How bitcoin execute the script
Complete script
304502206e21798a42fae0e854281abd38bacd1aeed3ee3738d9e1446618c4571d10 OP_DUP OP_HASH160 404371705fa9bd789a2fcd52d2c580b65d35549d OP_EQUALVERIFY OP_CHECKSIG
if you push the script inside this ide and run it you can see how bitcoin run the script.
In this example the ide returned false because the transaction output cannot be sent with her input
The ScriptPubKey(most of the time) contains the pubKey
The ScriptSing contains the Signature of the private key
This are all informations for work the simple node bitcoin.
Now I have and question for you.
You say
impossible to get public address from bitcoin address.
what does it mean?

bitcoin-cli: how to create a wallet and utxo address

I am a relative newbie in bitcoin and blockchain and hope you can help me with some of the questions.
So I launched a "regtest" network and generated 101 blocks using
bitcoin-cli -regtest generate 101
Now, if I launch 'bitcoin-cli -regtest getaddressesbyaccount ""', I get the public address of my default account:
[
"mwpKJNJ4UZL7yFyj53RSVcwauGAK84UvV2"
]
And of course, I should not have any other accounts as for now.
When I launch 'bitcoin-cli -regtest listunspent':
[
{
"txid": "694030f8638318c8c54054515ec716159edc494b14234885deb48f294b75a2fe",
"vout": 0,
"address": "n1queZpweTHjrMLvwSmcfrrJSQjsrYG3nG",
"scriptPubKey": "21038cadb266ed1ae6c474f5c1b74fc5f6790eacde843a673a16cfc924a100f2a679ac",
"amount": 50.00000000,
"confirmations": 101,
"spendable": true,
"solvable": true,
"safe": true
}
]
First question:
I understand that the only transaction listed by "listunspent" is UTXO,
meaning this is a transaction what I received to my address "n1queZpweTHjrMLvwSmcfrrJSQjsrYG3nG" with 50 BTC as amount.
Where this address comes from? By what bitcoin-cli command I can see/find it in my wallet?
Second question:
How can I create a new wallet with some balances and switch between them ( using bitcoin-cli )?
Basically, I would like to be able to test my app using bitcoin-cli - I need to be able to create wallets, switch between them and send btc between the addresses.
Coinbase coins can't be transferred until 100 blocks after they were created.
(Why did you generate "101" blocks specifically?)
So, the amount in your wallet you see is from the first block you mined. You can verify that by bitcoin-cli -regtest getblock "<hash of first block>" which you had got in return to the generate 101 command you ran earlier (an array of 101 block hashes).
Try the following
generate one more block bitcoin-cli -regtest generate 1
now listunspent and you should see 2 utxos instead of 1.
Depending on what you want to test, maybe simply creating a new address and sending money to it is enough for you?
[Edit]
Shut down core properly.
Rename your wallet.dat file
When you restart, a new wallet(wallet.dat) will be created. You can use them by supplying -wallet arg to bitcoin-qt
For example, if you are on linux:
Create 4 wallets by starting bitcoin core, stopping bitcoin core and then renaming the wallet.dat in your ~/.bitcoin folder (then repeating the process). For example, run this process 4 times to generate :
mywallet.dat
wifeswallet.dat
kidswallet.dat
businesswallet.dat
Then, in linux, in your .bashrc :
alias mywallet="bitcoin-qt -wallet=~/.bitcoin/mywallet.dat"
alias wifeswallet="bitcoin-qt -wallet=~/.bitcoin/wifeswallet.dat"
alias kidswallet="bitcoin-qt -wallet=~/.bitcoin/kidswallet.dat"
alias businesswallet="bitcoin-qt -wallet=~/.bitcoin/businesswallet.dat"