What is an owning contact? - ethereum

While going through solidity documentation I came across a term 'the owning contract' which i didnt understand.
A contract in the sense of Solidity is a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain. The line uint storedData; declares a state variable called storedData of type uint (unsigned integer of 256 bits). You can think of it as a single slot in a database that can be queried and altered by calling functions of the code that manages the database. In the case of Ethereum, this is always the owning contract. And in this case, the functions set and get can be used to modify or retrieve the value of the variable.

The owning contract means the contract that owns that block of storage on the Ethereum blockchain. The contract that "owns" this memory block is allowed to execute functions that read/write/delete/update the block's contents.
See this answer for a discussion of the two types of "memory/storage"

Related

In Solidity can I get data of other block other than the current one?

I hope to have something like Block(5).hashdata. There is something like that in Solidity to get the hash data of another block?
Smart contracts have only access to the current EVM state. They cannot access the historical state. Thus, you cannot access the historical state in Solidity smart contracts.
This is because Ethereum nodes do not keep, or keep only limited historical state, stored on the disk unless you run specifical archive node.

Can Ethereum transaction (calling contract function) fail for reasons other than assert()/require()/revert() or gas issues?

When running a private Ethereum network not requiring gas for transactions, can a contract function transaction fail for some "unpredictable" issue, other than explicit invocation of assert()/require()/revert(), for example dividing by 0 or some other issue with EVM or beyond EVM?
division by zero
integer overflow/underflow in Solidity 0.8+ (previous versions let the number overflow, 0.8 throws an exception)
accessing out-of-bounds array index
message call (aka internal transaction) to an address that does not implement the called function (might have been selfdestruct or changed implementation behind a proxy)
These I could think of right now. I'm sure there's more examples, generally runtime errors cause by some logical mistake.

When is a contract constructor called?

From this solidity doc I know that the constructor is called once when the contract is created. But are there other instances that the constructor is called?
I am looking for all possible cases that the constructor of a contract will be called to better understand the use of a constructor in smart contracts and the consequences of not having a constructor.
No. As it say in the docs, constructor is called only once.
When a contract is created, its constructor (a function declared with the constructor keyword) is executed once.
It would be a huge security breach if it could be called more than once, since constructor usually sets up contract ownership and other important variables.
Parity hack happened exactly because it was possible to call the "constructor" multiple times using delegatecall.

Is this a clean way to withdraw from a contract in Solidity?

I have been looking high and low about how to withdraw the funds from an Ethereum contract with no prevail. The Remix editor is giving the warning that this function may cause an infinite loop.
Gas requirement of function KOTH.cleanTheKingsChest() high: infinite. If the gas requirement of a function is higher than the block gas limit, it cannot be executed. Please avoid loops in your functions or actions that modify large areas of storage (this includes clearing or copying arrays in storage)
And...
Should I use Open-Zeppelin's safe math for this function?
function cleanTheKingsChest() public isOwner {
uint bal = address(this).balance;
address(owner).transfer(bal);
}
This will transfer all ether held by the contract to the owner’s address. There is no issue with the way you’re doing it.
The reason for the warning is because you are making a call out to another address. That address could itself be a contract with a custom defined transfer or fallback function (if no transfer method is defined). Since Remix doesn’t know what that implementation may do, it’ can’t estimate the gas usage. This isn’t a concern since transfer calls are limited to a 2100 gas stipend.
You don’t need SafeMath for that function since you’re not doing anything that can cause an overflow. However, in general, it’s a good idea to use it.

How to get transactions invoked by smart contract from blockchain

I want to get all receipt records of some address (address A) from blockchain. I use web3.eth.getBlock and web3.eth.getTransaction to get all transactions related to A.
But I find if ethers are transfered to address A by invoking 'A.send' or 'A.transfer' function in contract, I can only get a contract call transaction which from caller address to contract address. And I can't find relationship between this transaction and address A.
Is there any way to get transactions invoked by contract for A?
Thanks.
Short answer: listen to events instead of monitoring transactions.
Why can't I find the transactions "invoked by" the contract?
A contract does not invoke/create its own transaction (it doesn't have a private key to sign one). A contract can call other contracts during its own execution, as you've seen, but those calls are all executed as part of the same transaction.
The recommended way to discover executed function calls is to use events. If the contract you want to watch does not specify an event on the call you want to watch, you may be in for some heavy lifting: tracing every transaction's execution at the EVM level (something that big block explorers do to generate some of the extra info they provide).
If you are stuck tracing: you can find more about using ethereumjs-vm or geth's debug_traceTransaction on this Ethereum StackExchange Question about internal transactions.