def createAtomicSwapTx(self, callback): fromAccount = Accounts.getAccountByAddress(self.fromAddress) toMultiSigAccount = Accounts.getAccountByAddress(self.toAddress) ''' A picks a random number x ''' hashPrivKey = Crypto.generateHash(toMultiSigAccount.private) ''' A creates TX1: "Pay w BTC to <B's public key> if (x for H(x) known and signed by B) or (signed by A & B)" ''' transactionA1 = MemoryPool.createUnconfirmedAtomicSwapTransaction(fromAccount, toMultiSigAccount.address, self.value, self.gasLimit, self.gasPrice, Script.verifyAtomicSwapSignature(), self.threshold) if transactionA1 != None: ''' A creates TX2: "Pay w BTC from TX1 to <A's public key>, locked 48 hours in the future, signed by A" ''' transactionA2 = MemoryPool.createUnconfirmedAtomicSwapTransaction(fromAccount, toMultiSigAccount.address, self.value, self.gasLimit, self.gasPrice, Script.verifyAtomicSwapLock()) if transactionA2 != None: tx = transactionA1.serialize() unsignedTx = transactionA2.serialize() result = { 'tx': DataType.toHex(tx), 'unsignedTx': DataType.toHex(unsignedTx) } callback( JSONRPC.createResultObject(result, self.id) ) else: self.onFailure(callback) else: self.onFailure(callback)
def verifyTxOutputDuplicateInPool(self, transaction): for txIn in transaction.inputs: outpoint = txIn.outpoint for txId in MemoryPool.getMemoryPool(): tx = MemoryPool.getTransactionById(txId) for i in tx.inputs: if outpoint == i.outpoint: return False return True
def deploy(self, isLocal, callback): result = None fromAccount = Accounts.getAccountByAddress(self.fromAddress) if fromAccount != None: if isLocal: toAddress = Crypto.generateAddress(fromAccount.address) _script = bytearray() _script.append(Config.getIntValue("EXTENSION_SCRIPT_VERSION")) _script.extend(self.script) _script = DataType.serialize(_script) extraData = None if len(self.parameters) > 0: extraData = self.parameters extraData.append(fromAccount.address) output = Output(toAddress, _script, self.value, extraData) result = self.handleLocalScript(fromAccount, output, True) if result != None: Contracts.addContract(Contract(toAddress), False) else: result = MemoryPool.addUnconfirmedTransaction( fromAccount, None, self.value, self.gasLimit, self.gasPrice, self.script, self.parameters) try: result = DataType.toHex(result) except: pass if result != None: callback(JSONRPC.createResultObject(result, self.id)) else: self.onFailure(callback)
def call(self, isLocal, callback): result = None fromAccount = Accounts.getAccountByAddress(self.fromAddress) if fromAccount != None: if isLocal: _output = PersistentStorage.get(self.script, True) if _output == None: unspentTransactionScript = UXTO.getUnspentTransactionScript( self.script) if unspentTransactionScript != None: _output = unspentTransactionScript.output if _output != None: result = self.handleLocalScript(fromAccount, _output, False) else: result = MemoryPool.addUnconfirmedTransaction( fromAccount, self.script, self.value, self.gasLimit, self.gasPrice, None, self.parameters) try: result = DataType.toHex(result) except: pass if result != None: callback(JSONRPC.createResultObject(result, self.id)) else: self.onFailure(callback)
def __init__(self): super().__init__() self.type = MessageType.GETDATA self.inventory = [] self.chain = Chain.getChain() self.memoryPool = MemoryPool.getMemoryPool()
def sendAtomicSwapTx(self, callback): transaction = None if MemoryPool.addSignedTransaction(transaction): callback( JSONRPC.createResultObject(result, self.id) ) else: self.onFailure(callback)
def addUnconfirmedMultiSigTransaction(self, callback=None): fromAccount = Accounts.getAccountByAddress(self.fromAddress) txId = None if fromAccount != None: txId = MemoryPool.addUnconfirmedMultiSigTransaction( fromAccount, self.publicKeys, self.signatures, self.toAddress, self.value, self.gasLimit, self.gasPrice, self.threshold) if txId != None: callback(JSONRPC.createResultObject(DataType.toHex(txId), self.id)) else: self.onFailure(callback)
def signAtomicSwapTx(self, callback): transaction = Transaction() transaction.deserialize(self.unsignedTx) transaction.sign() if MemoryPool.addSignedTransaction(transaction): txId = transaction.hash() callback( JSONRPC.createResultObject(DataType.toHex(txId), self.id) ) else: self.onFailure(callback)
def onSuccess(self, callback=None): message = MessageFactory.getInstance(MessageType.GETDATA) message.addrRecv = self.addrFrom for inventory in self.inventory: if inventory.invType == InventoryType.BLOCK: if self.chain.getBlockByHash(inventory.getInvHash()) == None: message.inventory.append(inventory) elif inventory.invType == InventoryType.TRANSACTION: if MemoryPool.getTransactionById( inventory.getInvHash()) == None: message.inventory.append(inventory) if len(message.inventory) > 0: message.send()
def getNewBlock(self, address, previousHash, bits, extraNonce): previousIndexBlock = self.getIndexBlockByHash(previousHash) block = Block() gasLimit = Config.getIntValue("BLOCK_REWARDS_GAS_LIMIT") gasPrice = Config.getIntValue("BLOCK_REWARDS_GAS_PRICE") transaction = Transaction(gasLimit, gasPrice) height = previousIndexBlock.height + 1 coinbaseData = [ DataType.asString(height), DataType.asString(bits), DataType.asString(extraNonce) ] transaction.addCoinbaseInput(coinbaseData) block.transactions.append(transaction) txFees = 0 totalTxGasUsed = 0 unconfirmedTransactions = MemoryPool.getMemoryPool() for txId in unconfirmedTransactions: unconfirmedTransaction = unconfirmedTransactions[txId] block.transactions.append(unconfirmedTransaction) txFees += unconfirmedTransaction.calculateTxFee() totalTxGasUsed += unconfirmedTransaction.calculateTxGasUsed() blockRewards = Config.getDecimalValue("BLOCK_REWARDS") blockRewards = Units.toUnits(blockRewards) coinbaseValue = blockRewards + txFees script = Script.verifySignature() transaction.addOutput(address, script, coinbaseValue) transaction.hash() #Include coinbase tx gas used totalTxGasUsed += transaction.calculateTxGasUsed() block.merkleRoot = MerkleTree.getMerkleRoot(block.transactions, False) block.witnessRoot = MerkleTree.getMerkleRoot(block.transactions, True) blockGasLimit = previousIndexBlock.gasLimit + (previousIndexBlock.gasLimit * (1 / 1024)) blockGasLimit = math.ceil(blockGasLimit) block.gasLimit = blockGasLimit block.gasUsed = totalTxGasUsed block.nonce = 0 block.bits = bits block.previousHash = previousHash return block
def signMultiSigOutput(self, callback=None): signatures = [] multiSigAccount = Accounts.getAccountByAddress(self.fromAddress) if multiSigAccount != None: transaction = MemoryPool.createUnconfirmedMultiSigTransaction( multiSigAccount, self.toAddress, self.value, self.gasLimit, self.gasPrice, self.threshold) if transaction != None: for txIn in transaction.inputs: multiSigAddress = DataType.toHex(multiSigAccount.address) _publicKeys = txIn.witness[::2] _signatures = txIn.witness[1::2] for publicKey, signature in zip(_publicKeys, _signatures): publicKey = DataType.toHex(publicKey) signature = DataType.toHex(signature) signatures.append({ 'address': multiSigAddress, 'public': publicKey, 'signature': signature }) callback(JSONRPC.createResultObject(signatures, self.id))
def __init__(self): super().__init__() self.type = MessageType.MEMPOOL self.memoryPool = MemoryPool.getMemoryPool()
def onSuccess(self, callback=None): transaction = Transaction() transaction.deserialize(self.transaction) if not MemoryPool.addSignedTransaction(transaction): self.onFailure(callback)
def addBlock(self, block): if self.blkValidator.validate(block): try: self.acquireBlockLock() blockHash = block.hash() blockHashBytes = DataType.serialize(blockHash) if self.index.get(blockHashBytes) == None: if not OrphanManager.hasBlock(blockHash): bits = block.bits previousChainWork = None previousHash = block.previousHash blockGasLimit = block.gasLimit blockHeight = 0 chainHeadBlock = self.getChainHeadBlock() chainHeadIndexBlock = self.getIndexBlockByHash(self.CHAIN_HEAD_INDEX) previousIndexBlock = self.getIndexBlockByHash(previousHash) if chainHeadIndexBlock == None: chainHeadIndexBlock = IndexBlock() if previousIndexBlock != None: blockHeight = previousIndexBlock.height + 1 previousChainWork = previousIndexBlock.chainWork if blockHash == Config.getBytesValue('GENESIS_BLOCK_HASH'): previousChainWork = Config.getIntValue('GENESIS_BLOCK_CHAIN_WORK', 16) ''' For case 1, adding to main branch: ''' if previousHash == chainHeadIndexBlock.previousHash or blockHash == Config.getBytesValue('GENESIS_BLOCK_HASH'): ''' For all but the coinbase transaction, apply the following: ''' if not self.blkValidator.verifyNonCoinbaseTransactions(block): return False ''' Reject if coinbase value > sum of block creation fee and transaction fees ''' if not self.blkValidator.verifyCoinbaseValue(block): return False for transaction in block.transactions: for txIn in transaction.inputs: UXTO.removeUnspentTransactionCoin(txIn.outpoint) uxtoOutputs = [] uxtoOutputs.extend(transaction.outputs) uxtoOutputs.extend(transaction.internalOutputs) txOutputSize = 0 for txOut in uxtoOutputs: if txOut.store: txOutputSize += 1 outputIndex = 0 for txOut in uxtoOutputs: UXTO.removeStaleUnspentTransactionScript(txOut) if txOut.store: coin = Coin() coin.output = txOut coin.txOutputSize = txOutputSize coin.height = blockHeight coin.coinbase = transaction.isCoinbase() UXTO.addUnspentTransactionCoin(Outpoint(transaction.hash(), outputIndex), coin) outputIndex += 1 ''' For each transaction, "Add to wallet if mine" ''' ''' For each transaction in the block, delete any matching transaction from the transaction pool ''' MemoryPool.removeTransaction(transaction) chainHeadIndexBlock.chainWork = Bits.getChainworkFromBits(previousChainWork, bits) chainHeadIndexBlock.previousHash = blockHash chainHeadIndexBlock.gasLimit = blockGasLimit chainHeadIndexBlock.height = blockHeight self.index.set(self.CHAIN_HEAD_INDEX, chainHeadIndexBlock.serialize()) else: blockChainWork = Bits.getChainworkFromBits(previousChainWork, bits) chainHeadWork = chainHeadIndexBlock.chainWork hasNewMainChain = blockChainWork > chainHeadWork if hasNewMainChain: ''' For case 3, a side branch becoming the main branch: ''' else: ''' For case 2, adding to a side branch, we don't do anything. ''' if hasNewMainChain: ''' Find the fork block on the main branch which this side branch forks off of ''' forkBlockHash = self.searchForkBlockHash(previousIndexBlock, chainHeadIndexBlock) ''' Redefine the main branch to only go up to this fork block We will set new main chain head below ''' isNewMainChainValid = True ''' For each block on the side branch, from the child of the fork block to the leaf, add to the main branch: ''' prevBlock = self.getBlockByHash(block.previousHash) while prevBlock.hash() != forkBlockHash: ''' Do "branch" checks 3-11 ''' ''' 3) Transaction list must be non-empty ''' if not self.blkValidator.verifyTransactionsNonEmpty(prevBlock): isNewMainChainValid = False ''' 4) Block hash must satisfy claimed nBits proof of work ''' if not self.blkValidator.validateBlockBits(prevBlock.serializeHeader(), prevBlock.bits): isNewMainChainValid = False ''' 5) Block timestamp must not be more than two hours in the future ''' if not self.blkValidator.verifyFutureTimestamp(prevBlock): isNewMainChainValid = False ''' 6) First transaction must be coinbase (i.e. only 1 input, with hash=0, n=-1), the rest must not be ''' if not self.blkValidator.verifyInitialCoinbaseTransaction(prevBlock): isNewMainChainValid = False ''' 7) For each transaction, apply "tx" checks 2-4 2) Make sure neither in or out lists are empty 3) Size in bytes <= TRANSACTION_SIZE_LIMIT 4) Each output value, as well as the total, must be in legal money range ''' for transaction in prevBlock.transactions: if not self.txValidator.verifyInputOutputNonEmpty(transaction): isNewMainChainValid = False if not self.txValidator.verifyTransactionSizeLimit(transaction): isNewMainChainValid = False if not self.txValidator.verifyAllowedOutputValueRange(transaction): isNewMainChainValid = False ''' 8) For the coinbase (first) transaction, scriptSig length must be 2-100 ''' if not self.blkValidator.verifyCoinbaseWitnessLength(transaction): isNewMainChainValid = False ''' 9) Reject if sum of transaction sig opcounts > MAX_BLOCK_SIGOPS ''' if not self.blkValidator.verifyMaxBlockSigOps(transaction): isNewMainChainValid = False ''' 10) Verify Merkle hash ''' if not self.blkValidator.verifyMerkleHash(prevBlock): isNewMainChainValid = False ''' Verify Witness hash ''' if not self.blkValidator.verifyWitnessHash(prevBlock): isNewMainChainValid = False ''' 11) Check if prev block (matching prev hash) is in main branch or side branches. If not, add this to orphan blocks, then query peer we got this from for 1st missing orphan block in prev chain; done with block ''' if blockHash != Config.getBytesValue('GENESIS_BLOCK_HASH') and self.getBlockByHash(prevBlock.previousHash) == None: OrphanManager.addBlock(prevBlock) isNewMainChainValid = False ''' For all but the coinbase transaction, apply the following: ''' if not self.blkValidator.verifyNonCoinbaseTransactions(prevBlock): isNewMainChainValid = False ''' Reject if coinbase value > sum of block creation fee and transaction fees ''' if not self.blkValidator.verifyCoinbaseValue(prevBlock): isNewMainChainValid = False ''' (If we have not rejected): ''' if not isNewMainChainValid: break ''' For each transaction, "Add to wallet if mine" ''' prevBlock = self.getBlockByHash(prevBlock.previousHash) ''' If we reject at any point, leave the main branch as what it was originally, done with block ''' if isNewMainChainValid: chainHeadIndexBlock.chainWork = blockChainWork chainHeadIndexBlock.previousHash = blockHash chainHeadIndexBlock.gasLimit = blockGasLimit chainHeadIndexBlock.height = blockHeight self.index.set(self.CHAIN_HEAD_INDEX, chainHeadIndexBlock.serialize()) ''' For each block in the old main branch, from the leaf down to the child of the fork block: ''' oldBlock = chainHeadBlock while oldBlock.hash() != forkBlockHash: ''' For each non-coinbase transaction in the block: ''' for transaction in oldBlock.transactions: if not transaction.isCoinbase(): ''' Apply "tx" checks 2-9 ''' isTxValid = True ''' 2) Make sure neither in or out lists are empty ''' if not self.txValidator.verifyInputOutputNonEmpty(transaction): isTxValid = False ''' 3) Size in bytes <= TRANSACTION_SIZE_LIMIT ''' if not self.txValidator.verifyTransactionSizeLimit(transaction): isTxValid = False ''' 4) Each output value, as well as the total, must be in legal money range ''' if not self.txValidator.verifyAllowedOutputValueRange(transaction): isTxValid = False ''' 5) Make sure none of the inputs have hash=0, n=-1 (coinbase transactions) ''' if not self.txValidator.verifyInputsNonCoinbase(transaction): isTxValid = False ''' 6) size in bytes >= 100[2] ''' if not self.txValidator.verifyTransactionRequiredSize(transaction): isTxValid = False ''' sig opcount <= 2[3] 3) The number of signature operands in the signature (no, that is not redundant) for standard transactions will never exceed two 7) Reject "nonstandard" transactions: scriptSig doing anything other than pushing numbers on the stack, or script not matching the two usual forms[4] ''' if not self.txValidator.verifyAddress(transaction): isTxValid = False if not self.txValidator.verifyExtraData(transaction): isTxValid = False if not self.txValidator.verifyScript(transaction): isTxValid = False if not self.txValidator.verifyWitness(transaction): isTxValid = False ''' 8) Reject if we already have matching tx in the pool, except in step 8, only look in the transaction pool for duplicates, not the main branch ''' if not self.txValidator.verifyTransactionDuplicateInPool(transaction): isTxValid = False ''' 9) For each input, if the referenced output exists in any other tx in the pool, reject this transaction ''' if not self.txValidator.verifyTxOutputDuplicateInPool(transaction): isTxValid = False ''' Add to transaction pool if accepted, else go on to next transaction ''' if isTxValid: MemoryPool.addSignedTransaction(transaction) outputIndex = 0 for txOut in transaction.outputs: UXTO.removeUnspentTransactionCoin(Outpoint(transaction.hash(), outputIndex)) outputIndex += 1 oldBlock = self.getBlockByHash(oldBlock.previousHash) ''' For each block in the new main branch, from the child of the fork node to the leaf: ''' newMainBranchBlocks = [] prevBlock = block while prevBlock.hash() != forkBlockHash: newMainBranchBlocks.insert(0, prevBlock) prevBlock = self.getBlockByHash(prevBlock.previousHash) for newMainBranchBlock in newMainBranchBlocks: newMainBranchBlockHash = newMainBranchBlock.hash() newMainBranchIndexBlock = None if newMainBranchBlockHash == blockHash: newMainBranchIndexBlock = chainHeadIndexBlock else: newMainBranchIndexBlock = self.getIndexBlockByHash(newMainBranchBlockHash) for transaction in newMainBranchBlock.transactions: for txIn in transaction.inputs: UXTO.removeUnspentTransactionCoin(txIn.outpoint) uxtoOutputs = [] uxtoOutputs.extend(transaction.outputs) uxtoOutputs.extend(transaction.internalOutputs) txOutputSize = 0 for txOut in uxtoOutputs: if txOut.store: txOutputSize += 1 outputIndex = 0 for txOut in uxtoOutputs: UXTO.removeStaleUnspentTransactionScript(txOut) if txOut.store: coin = Coin() coin.output = txOut coin.txOutputSize = txOutputSize coin.height = newMainBranchIndexBlock.height coin.coinbase = transaction.isCoinbase() UXTO.addUnspentTransactionCoin(Outpoint(transaction.hash(), outputIndex), coin) outputIndex += 1 ''' For each transaction, "Add to wallet if mine" ''' ''' For each transaction in the block, delete any matching transaction from the transaction pool ''' MemoryPool.removeTransaction(transaction) ''' (If we have not rejected): ''' self.storage.set(blockHashBytes, block.serialize()) blockHeightKey = "{0}{1}{2}".format(self.BLOCK_HEIGHT_KEY, '_', blockHeight) blockHeightKey = DataType.serialize(blockHeightKey) self.index.set(blockHeightKey, blockHashBytes) indexBlock = IndexBlock() indexBlock.chainWork = Bits.getChainworkFromBits(previousChainWork, bits) indexBlock.previousHash = previousHash indexBlock.gasLimit = blockGasLimit indexBlock.height = blockHeight self.index.set(blockHashBytes, indexBlock.serialize()) ''' Relay block to our peers ''' Sync.inv(InventoryType.BLOCK, blockHash) ''' For each orphan block for which this block is its prev, run all these steps (including this one) recursively on that orphan ''' self.syncOrphanBlocks() return True finally: self.releaseBlockLock() ''' For each orphan block for which this block is its prev, run all these steps (including this one) recursively on that orphan ''' self.syncOrphanBlocks() ''' If we rejected, the block is not counted as part of the main branch ''' return False
def verifyTransactionDuplicateInPool(self, transaction): txId = transaction.hash() if txId in MemoryPool.getMemoryPool(): return False return True
def validate(self, transaction): if transaction == None: return False ''' The transaction’s syntax and data structure must be correct ''' if transaction.version == None or transaction.version != Config.getIntValue( 'TRANSACTION_VERSION'): return False if not self.verifyInputOutputNonEmpty(transaction): return False for txIn in transaction.inputs: ''' Check outpoint ''' if txIn.outpoint == None: return False outpoint = txIn.outpoint if outpoint.txId == None or len( outpoint.txId) != Config.getIntValue('HASH_LEN'): return False if outpoint.outputIndex == None or outpoint.outputIndex < 0: return False ''' Check Output Value ''' for txOut in transaction.outputs: if txOut.value == None: return False if txOut.hasExtScript(): if not txOut.value >= 0: return False elif not txOut.value > 0: return False ''' Check tx gas price and gas limit ''' if transaction.gasPrice == None or transaction.gasPrice < 0: return False if transaction.gasLimit == None or transaction.gasLimit < 0: return False ''' Internal Outputs are created from the result of a smart contract. Only the RVM should create them. ''' if len(transaction.internalOutputs) > 0: return False if not self.verifyAddress(transaction): return False if not self.verifyExtraData(transaction): return False if not self.verifyScript(transaction): return False if not self.verifyWitness(transaction): return False if not self.verifyTransactionSizeLimit(transaction): return False ''' Check for duplicate outpoints. ''' outpoints = set() for txIn in transaction.inputs: if txIn.outpoint in outpoints: return False outpoints.add(txIn.outpoint) if not self.verifyAllowedOutputValueRange(transaction): return False if not self.verifyInputsNonCoinbase(transaction): return False if not self.verifyTransactionRequiredSize(transaction): return False if not self.verifyTransactionDuplicateInPool(transaction): return False ''' or txId in a block in the main branch ''' txId = transaction.hash() try: UXTOStorage.uxto.open() with UXTOStorage.uxto.db.begin() as uxto: cursor = uxto.cursor(db=UXTOStorage.uxto.subDb) while cursor.next(): outpointBytes = cursor.key() outpoint = Outpoint() outpoint.deserialize(outpointBytes) if txId == outpoint.txId: return False except IOError: Log.error( 'Unable to determine if txId is in a block in the main branch.' ) finally: UXTOStorage.uxto.close() if not self.verifyTxOutputDuplicateInPool(transaction): return False ''' For each input, look in the main branch and the transaction pool to find the referenced output transaction. If the output transaction is missing for any input, this will be an orphan transaction. Add to the orphan transactions, if a matching transaction is not in there already. ''' for txIn in transaction.inputs: outpoint = txIn.outpoint if not UXTO.hasUnspentTransactionCoin(outpoint): tx = MemoryPool.getTransactionById(outpoint.txId) if tx == None or len(tx.outputs) - 1 < outpoint.outputIndex: OrphanManager.addTransaction(transaction) return False if not self.verifyCoinbaseMaturity(transaction): return False if not self.verifyUxtoReferencedOutput(transaction): return False if not self.verifyAllowedInputValueRange(transaction): return False if not self.verifySumInputOutputValues(transaction): return False if not self.verifyUnlockingScripts(transaction): return False ''' 7) Note that when the transaction is accepted into the memory pool, an additional check is made to ensure that the coinbase value does not exceed the transaction fees plus the expected BTC value (25BTC as of this writing). ''' if transaction.isCoinbase(): txOut = transaction.outputs blockRewards = Config.getDecimalValue("BLOCK_REWARDS") blockRewards = Units.toUnits(blockRewards) if txOut.value > blockRewards: return False return True