def from_str(cls, s): k = decode(s) addrbyte, data, check0 = k[0], k[1:-4], k[-4:] check1 = ser_uint256(Hash(addrbyte + data))[:4] if check0 != check1: raise Base58ChecksumError('Checksum mismatch: expected %r, calculated %r' % (check0, check1)) return cls(data, ord(addrbyte))
def single_delete(chaindb,height): batch = leveldb.WriteBatch() heightidx = ChainDb.HeightIdx() heightidx.deserialize(chaindb.height(str(height))) blkhash = heightidx.blocks[0] strBlkHash = hex(blkhash).replace('0x','').replace('L','') ser_hash = ser_uint256(blkhash) batch.Delete('blocks:'+ser_hash) batch.Delete('height:'+str(height)) chaindb.db.Write(batch)
def single_delete(chaindb, height): batch = leveldb.WriteBatch() heightidx = ChainDb.HeightIdx() heightidx.deserialize(chaindb.height(str(height))) blkhash = heightidx.blocks[0] strBlkHash = hex(blkhash).replace('0x', '').replace('L', '') ser_hash = ser_uint256(blkhash) batch.Delete('blocks:' + ser_hash) batch.Delete('height:' + str(height)) chaindb.db.Write(batch)
def CheckSig(sig, pubkey, script, txTo, inIdx, hashtype): key = CKey() key.set_pubkey(pubkey) if len(sig) == 0: return False if hashtype == 0: hashtype = ord(sig[-1]) elif hashtype != ord(sig[-1]): return False sig = sig[:-1] tup = SignatureHash(script, txTo, inIdx, hashtype) return key.verify(ser_uint256(tup[0]), sig)
def delete_blocks(chaindb, start, end): batch = leveldb.WriteBatch() for height in xrange(start, end): heightidx = ChainDb.HeightIdx() heightidx.deserialize(chaindb.height(str(height))) blkhash = heightidx.blocks[0] strBlkHash = hex(blkhash).replace('0x', '').replace('L', '') ser_hash = ser_uint256(blkhash) batch.Delete('blocks:' + ser_hash) batch.Delete('height:' + str(height)) print "deleted: %064x %d" % (blkhash, height) batch.Put('misc:height:', str(start - 1)) chaindb.db.Write(batch)
def delete_blocks(chaindb,start,end): batch = leveldb.WriteBatch() for height in xrange(start,end): heightidx = ChainDb.HeightIdx() heightidx.deserialize(chaindb.height(str(height))) blkhash = heightidx.blocks[0] strBlkHash = hex(blkhash).replace('0x','').replace('L','') ser_hash = ser_uint256(blkhash) batch.Delete('blocks:'+ser_hash) batch.Delete('height:'+str(height)) print "deleted: %064x %d" % (blkhash,height) print start batch.Put('misc:height:',str(start-1)) chaindb.db.Write(batch)
def EvalScript(stack, scriptIn, txTo, inIdx, hashtype): altstack = [] vfExec = [] script = CScript(scriptIn) while script.pc < script.pend: if not script.getop(): return False sop = script.sop fExec = CheckExec(vfExec) if fExec and sop.op <= OP_PUSHDATA4: stack.append(sop.data) continue elif fExec and sop.op == OP_1NEGATE or ((sop.op >= OP_1) and (sop.op <= OP_16)): v = sop.op - (OP_1 - 1) stack.append(bn2vch(v)) elif fExec and sop.op in ISA_BINOP: if not BinOp(sop.op, stack): return False elif fExec and sop.op in ISA_UNOP: if not UnaryOp(sop.op, stack): return False elif fExec and sop.op == OP_2DROP: if len(stack) < 2: return False stack.pop() stack.pop() elif fExec and sop.op == OP_2DUP: if len(stack) < 2: return False v1 = stack[-2] v2 = stack[-1] stack.append(v1) stack.append(v2) elif fExec and sop.op == OP_2OVER: if len(stack) < 4: return False v1 = stack[-4] v2 = stack[-3] stack.append(v1) stack.append(v2) elif fExec and sop.op == OP_2SWAP: if len(stack) < 4: return False tmp = stack[-4] stack[-4] = stack[-2] stack[-2] = tmp tmp = stack[-3] stack[-3] = stack[-1] stack[-1] = tmp elif fExec and sop.op == OP_3DUP: if len(stack) < 3: return False v1 = stack[-3] v2 = stack[-2] v3 = stack[-1] stack.append(v1) stack.append(v2) stack.append(v3) elif fExec and sop.op == OP_CHECKMULTISIG or sop.op == OP_CHECKMULTISIGVERIFY: tmpScript = CScript(script.vch[script.pbegincodehash:script.pend]) ok = CheckMultiSig(sop.op, tmpScript, stack, txTo, inIdx, hashtype) if not ok: return False elif fExec and sop.op == OP_CHECKSIG or sop.op == OP_CHECKSIGVERIFY: if len(stack) < 2: return False vchPubKey = stack.pop() vchSig = stack.pop() tmpScript = CScript(script.vch[script.pbegincodehash:script.pend]) # FIXME: find-and-delete vchSig ok = CheckSig(vchSig, vchPubKey, tmpScript, txTo, inIdx, hashtype) if ok: if sop.op != OP_CHECKSIGVERIFY: stack.append(b"\x01") else: if sop.op == OP_CHECKSIGVERIFY: return False stack.append(b"\x00") elif fExec and sop.op == OP_CODESEPARATOR: script.pbegincodehash = script.pc elif fExec and sop.op == OP_DEPTH: bn = len(stack) stack.append(bn2vch(bn)) elif fExec and sop.op == OP_DROP: if len(stack) < 1: return False stack.pop() elif fExec and sop.op == OP_DUP: if len(stack) < 1: return False v = stack[-1] stack.append(v) elif sop.op == OP_ELSE: if len(vfExec) == 0: return false vfExec[-1] = not vfExec[-1] elif sop.op == OP_ENDIF: if len(vfExec) == 0: return false vfExec.pop() elif fExec and sop.op == OP_EQUAL or sop.op == OP_EQUALVERIFY: if len(stack) < 2: return False v1 = stack.pop() v2 = stack.pop() is_equal = (v1 == v2) if is_equal: stack.append(b"\x01") else: stack.append(b"\x00") if sop.op == OP_EQUALVERIFY: if is_equal: stack.pop() else: return False elif fExec and sop.op == OP_FROMALTSTACK: if len(altstack) < 1: return False v = altstack.pop() stack.append(v) elif fExec and sop.op == OP_HASH160: if len(stack) < 1: return False stack.append(ser_uint160(Hash160(stack.pop()))) elif fExec and sop.op == OP_HASH256: if len(stack) < 1: return False stack.append(ser_uint256(Hash(stack.pop()))) elif sop.op == OP_IF or sop.op == OP_NOTIF: val = False if fExec: if len(stack) < 1: return False vch = stack.pop() val = CastToBool(vch) if sop.op == OP_NOTIF: val = not val vfExec.append(val) elif fExec and sop.op == OP_IFDUP: if len(stack) < 1: return False vch = stack[-1] if CastToBool(vch): stack.append(vch) elif fExec and sop.op == OP_NIP: if len(stack) < 2: return False del stack[-2] elif fExec and sop.op == OP_NOP or (sop.op >= OP_NOP1 and sop.op <= OP_NOP10): pass elif fExec and sop.op == OP_OVER: if len(stack) < 2: return False vch = stack[-2] stack.append(vch) elif fExec and sop.op == OP_PICK or sop.op == OP_ROLL: if len(stack) < 2: return False n = CastToBigNum(stack.pop()) if n < 0 or n >= len(stack): return False vch = stack[-n-1] if sop.op == OP_ROLL: del stack[-n-1] stack.append(vch) elif fExec and sop.op == OP_RETURN: return False elif fExec and sop.op == OP_RIPEMD160: if len(stack) < 1: return False h = hashlib.new('ripemd160') h.update(stack.pop()) stack.append(h.digest()) elif fExec and sop.op == OP_ROT: if len(stack) < 3: return False tmp = stack[-3] stack[-3] = stack[-2] stack[-2] = tmp tmp = stack[-2] stack[-2] = stack[-1] stack[-1] = tmp elif fExec and sop.op == OP_SIZE: if len(stack) < 1: return False bn = len(stack[-1]) stack.append(bn2vch(bn)) elif fExec and sop.op == OP_SHA256: if len(stack) < 1: return False stack.append(hashlib.sha256(stack.pop()).digest()) elif fExec and sop.op == OP_SWAP: if len(stack) < 2: return False tmp = stack[-2] stack[-2] = stack[-1] stack[-1] = tmp elif fExec and sop.op == OP_TOALTSTACK: if len(stack) < 1: return False v = stack.pop() altstack.append(v) elif fExec and sop.op == OP_TUCK: if len(stack) < 2: return False vch = stack[-1] stack.insert(len(stack) - 2, vch) elif fExec and sop.op == OP_VERIFY: if len(stack) < 1: return False v = CastToBool(stack[-1]) if v: stack.pop() else: return False elif fExec and sop.op == OP_WITHIN: if len(stack) < 3: return False bn3 = CastToBigNum(stack.pop()) bn2 = CastToBigNum(stack.pop()) bn1 = CastToBigNum(stack.pop()) v = (bn2 <= bn1) and (bn1 < bn3) if v: stack.append(b"\x01") else: stack.append(b"\x00") elif fExec: #print("Unsupported opcode", OPCODE_NAMES[sop.op]) return False return True
def __str__(self): vs = chr(self.nVersion) + self check = ser_uint256(Hash(vs))[0:4] return encode(vs + check)
def EvalScript(stack, scriptIn, txTo, inIdx, hashtype): altstack = [] vfExec = [] script = CScript(scriptIn) while script.pc < script.pend: if not script.getop(): return False sop = script.sop fExec = CheckExec(vfExec) if fExec and sop.op <= OP_PUSHDATA4: stack.append(sop.data) continue elif fExec and sop.op == OP_1NEGATE or ((sop.op >= OP_1) and (sop.op <= OP_16)): v = sop.op - (OP_1 - 1) stack.append(bn2vch(v)) elif fExec and sop.op in ISA_BINOP: if not BinOp(sop.op, stack): return False elif fExec and sop.op in ISA_UNOP: if not UnaryOp(sop.op, stack): return False elif fExec and sop.op == OP_2DROP: if len(stack) < 2: return False stack.pop() stack.pop() elif fExec and sop.op == OP_2DUP: if len(stack) < 2: return False v1 = stack[-2] v2 = stack[-1] stack.append(v1) stack.append(v2) elif fExec and sop.op == OP_2OVER: if len(stack) < 4: return False v1 = stack[-4] v2 = stack[-3] stack.append(v1) stack.append(v2) elif fExec and sop.op == OP_2SWAP: if len(stack) < 4: return False tmp = stack[-4] stack[-4] = stack[-2] stack[-2] = tmp tmp = stack[-3] stack[-3] = stack[-1] stack[-1] = tmp elif fExec and sop.op == OP_3DUP: if len(stack) < 3: return False v1 = stack[-3] v2 = stack[-2] v3 = stack[-1] stack.append(v1) stack.append(v2) stack.append(v3) elif fExec and sop.op == OP_CHECKMULTISIG or sop.op == OP_CHECKMULTISIGVERIFY: tmpScript = CScript(script.vch[script.pbegincodehash:script.pend]) ok = CheckMultiSig(sop.op, tmpScript, stack, txTo, inIdx, hashtype) if not ok: return False elif fExec and sop.op == OP_CHECKSIG or sop.op == OP_CHECKSIGVERIFY: if len(stack) < 2: return False vchPubKey = stack.pop() vchSig = stack.pop() tmpScript = CScript(script.vch[script.pbegincodehash:script.pend]) # FIXME: find-and-delete vchSig ok = CheckSig(vchSig, vchPubKey, tmpScript, txTo, inIdx, hashtype) if ok: if sop.op != OP_CHECKSIGVERIFY: stack.append(b"\x01") else: if sop.op == OP_CHECKSIGVERIFY: return False stack.append(b"\x00") elif fExec and sop.op == OP_CODESEPARATOR: script.pbegincodehash = script.pc elif fExec and sop.op == OP_DEPTH: bn = len(stack) stack.append(bn2vch(bn)) elif fExec and sop.op == OP_DROP: if len(stack) < 1: return False stack.pop() elif fExec and sop.op == OP_DUP: if len(stack) < 1: return False v = stack[-1] stack.append(v) elif sop.op == OP_ELSE: if len(vfExec) == 0: return false vfExec[-1] = not vfExec[-1] elif sop.op == OP_ENDIF: if len(vfExec) == 0: return false vfExec.pop() elif fExec and sop.op == OP_EQUAL or sop.op == OP_EQUALVERIFY: if len(stack) < 2: return False v1 = stack.pop() v2 = stack.pop() is_equal = (v1 == v2) if is_equal: stack.append(b"\x01") else: stack.append(b"\x00") if sop.op == OP_EQUALVERIFY: if is_equal: stack.pop() else: return False elif fExec and sop.op == OP_FROMALTSTACK: if len(altstack) < 1: return False v = altstack.pop() stack.append(v) elif fExec and sop.op == OP_HASH160: if len(stack) < 1: return False stack.append(ser_uint160(Hash160(stack.pop()))) elif fExec and sop.op == OP_HASH256: if len(stack) < 1: return False stack.append(ser_uint256(Hash(stack.pop()))) elif sop.op == OP_IF or sop.op == OP_NOTIF: val = False if fExec: if len(stack) < 1: return False vch = stack.pop() val = CastToBool(vch) if sop.op == OP_NOTIF: val = not val vfExec.append(val) elif fExec and sop.op == OP_IFDUP: if len(stack) < 1: return False vch = stack[-1] if CastToBool(vch): stack.append(vch) elif fExec and sop.op == OP_NIP: if len(stack) < 2: return False del stack[-2] elif fExec and sop.op == OP_NOP or (sop.op >= OP_NOP1 and sop.op <= OP_NOP10): pass elif fExec and sop.op == OP_OVER: if len(stack) < 2: return False vch = stack[-2] stack.append(vch) elif fExec and sop.op == OP_PICK or sop.op == OP_ROLL: if len(stack) < 2: return False n = CastToBigNum(stack.pop()) if n < 0 or n >= len(stack): return False vch = stack[-n - 1] if sop.op == OP_ROLL: del stack[-n - 1] stack.append(vch) elif fExec and sop.op == OP_RETURN: return False elif fExec and sop.op == OP_RIPEMD160: if len(stack) < 1: return False h = hashlib.new('ripemd160') h.update(stack.pop()) stack.append(h.digest()) elif fExec and sop.op == OP_ROT: if len(stack) < 3: return False tmp = stack[-3] stack[-3] = stack[-2] stack[-2] = tmp tmp = stack[-2] stack[-2] = stack[-1] stack[-1] = tmp elif fExec and sop.op == OP_SIZE: if len(stack) < 1: return False bn = len(stack[-1]) stack.append(bn2vch(bn)) elif fExec and sop.op == OP_SHA256: if len(stack) < 1: return False stack.append(hashlib.sha256(stack.pop()).digest()) elif fExec and sop.op == OP_SWAP: if len(stack) < 2: return False tmp = stack[-2] stack[-2] = stack[-1] stack[-1] = tmp elif fExec and sop.op == OP_TOALTSTACK: if len(stack) < 1: return False v = stack.pop() altstack.append(v) elif fExec and sop.op == OP_TUCK: if len(stack) < 2: return False vch = stack[-1] stack.insert(len(stack) - 2, vch) elif fExec and sop.op == OP_VERIFY: if len(stack) < 1: return False v = CastToBool(stack[-1]) if v: stack.pop() else: return False elif fExec and sop.op == OP_WITHIN: if len(stack) < 3: return False bn3 = CastToBigNum(stack.pop()) bn2 = CastToBigNum(stack.pop()) bn1 = CastToBigNum(stack.pop()) v = (bn2 <= bn1) and (bn1 < bn3) if v: stack.append(b"\x01") else: stack.append(b"\x00") elif fExec: #print("Unsupported opcode", OPCODE_NAMES[sop.op]) return False return True
log.write("FROMTX %064x" % (txfrom.sha256,)) log.write(txfrom.__repr__()) log.write("TOTX %064x" % (tx.sha256,)) log.write(tx.__repr__()) return False return True for height in xrange(1): # for height in xrange(end_height): if height < start_height: continue heightidx = HeightIdx() heightidx.deserialize(str(height)) blkhash = heightidx.blocks[0] ser_hash = ser_uint256(blkhash) f = cStringIO.StringIO(chaindb.getblock(ser_hash)) block = CBlock() block.deserialize(f) start_time = time.time() for tx_tmp in block.vtx: if tx_tmp.is_coinbase(): print "Tx is coinbase" continue scanned_tx += 1 if not scan_tx(tx_tmp):
log.write("TX %064x/%d failed" % (tx.sha256, i)) log.write("FROMTX %064x" % (txfrom.sha256, )) log.write(txfrom.__repr__()) log.write("TOTX %064x" % (tx.sha256, )) log.write(tx.__repr__()) return False return True for height in xrange(end_height): if height < start_height: continue heightidx = ChainDb.HeightIdx() heightidx.deserialize(chaindb.height[str(height)]) blkhash = heightidx.blocks[0] ser_hash = ser_uint256(blkhash) f = cStringIO.StringIO(chaindb.blocks[ser_hash]) block = CBlock() block.deserialize(f) start_time = time.time() for tx_tmp in block.vtx: if tx_tmp.is_coinbase(): continue scanned_tx += 1 if not scan_tx(tx_tmp): failures += 1