The invention relates to the technical field of block chains, in particular to a method and a system for realizing consensus based on a smart contract. Comprising the steps of compiling a smart contract, setting and executing a corresponding consensus algorithm in a node program, defining a block header information interface chained on the smart contract, carrying out transaction packaging to generate a block, replacing a consensus, issuing a new smart contract and the like. According to the method designed by the invention, the smart contract is independently compiled at each node of the block chain, and the consensus type data and the corresponding consensus algorithm are set, so that the consensus can be conveniently realized at the block nodes according to the triggering conditions of the smart contract, and the flexible replacement of the consensus can be realized at each node, thereby reducing the upgrading difficulty of the block and improving the upgrading efficiency of the block. Waste of computing resources is reduced, and possible inadaptability after consensus replacement is reduced; the system can safely and stably realize the consensus at the node, also provides a channel for replacing the consensus algorithm, can improve the operation speed, improves the intelligence of the contract, and reduces the execution cost.