How smart contract work

Smart contract is firstly defined as 'a computerized transaction protocol that executes the terms of a contract' by Nick Szabo.
It translates contractual clauses(collateral, bonding, etc) into code, and embedding them into property (hardware, or software) that can self-enforce them, so as to minimize the need for trusted intermediaries between transacting parties, and the occurence of malicious or accidental exceptions.
Within the blockchain context, smart contracts are scripts stored on the blockchain. (They can be thought of as roughly analogous to stored procedures in relational database management systems.) Since they reside on the blockchain, they have a unique address. We trigger a smart contract by addressing a transaction to it. It then executes independently and automatically in a prescribed manner on every node in the network, according to the data that was included in the triggering trasaction. (This implies that every node in a smart contract-enabled blockchain is running a virtual machine (VM), and that the blockchain network acts as a distributed VM.)
Smart contracts allow us to have general purpose computations occur on the chain. Where they excel however, is when they are tasked with managing data-driven interactions between entities on the network. Let us unpack this statement with an example. Consider a blockchain network where Alice, Bob, and Carol participate, and where digital assets of type X and Y are being traded.Bob deploys a smart contract on the network that defines: (a)a "deposit" function allowing his to deposit units of X into the contract, (b) a "trade" function that sends back 1 unit of X(from the contract's own deposits) for every 5 units of Y it receives, and (c) a "withdraw" function that allows Bob to withdraw all the assets that the contract holds.
Note that the "deposit" and "withdraw" functions are written so that only Bob(via his key) can call them, becuase this is what Bob decided, and also what makes sense for our example; they could have been written so that they can be called successfully by any user on the network.
Bob sends a transaction to that smart contract's address, calling its "deposit" function and moving 3 units of X to the contract. This transaction is recorded on the blockchain. Alice, who owns 12 units of Y, then sends a transaction that moves 10 units of Y to the contract's

你可能感兴趣的:(How smart contract work)