什么是隔离见证(What is Segwit)

这是我在知乎上的一个回答:

在Bitcoin的交易记录中包括了见证信息和交易信息。隔离见证就是要把见证信息(签名)和交易信息隔离,把他们记录在区块链上的不同的位置。

为什么要这样做呢?主要的好处是:

1,可以修复一个由交易延展性(transaction malleability)引起的问题。

2,可以实现闪电网络。

3,一定程度上增加一个区块里可容纳的交易数,缓解交易拥堵。

第一可以修复一个由交易延展性(transaction malleability)引起的问题:

先说一下交易延展性。比特币区块链上每笔交易记录里都包含有见证信息,交易的唯一标识(交易的哈希值)也是包括了见证信息计算出来的。由于见证算法的数学特性,任何人在拿到一个交易记录后,拿到其中的见证信息,然后可以在不需要知道私钥的情况下,很容易的拼凑出另外一个有效的见证信息。这样,他可以用拼凑出来的另外那个见证信息,拼凑上交易记录中的其他交易信息,制造出一个另外一个交易记录(哈希值不同)。如果可以让拼凑出来的交易记录先被写入区块链,那么,之前那个原始交易记录会被认为是无效的交易而失败。

这不会造成双花,也不会对区块链造成破坏,但是对原始交易记录的发起者会造成困扰,因为如果拿着原始交易记录的哈希值找不到交易的成功记录。尤其是对于一些交易所,如果没有完整的内部日志,可能无法追溯交易记录,导致攻击者利用拼凑的交易记录先成功提币,再申诉说没有提到币,要求再次提币。

隔离见证后,见证信息不再是交易记录的一部分,也就不参与交易记录哈希的计算,无法再通过修改见证信息来拼凑另外一个交易记录。

第二可以更好的实现闪电网络:

闪电网络是在比特币网络上,两个账户间创建一个交易记录,这个交易记录锁定一些比特币。我们可以认为这两个账户间创建了一个子结算网络。后续这两个账户之间的转账都在这些子结算网络(创建一系列新的交易记录,但不广播到比特币网络上)进行,其交易的真实性也是由加密算法来保证的。直到某一时刻,某个账户提出结算申请(将最新的交易记录在比特币网络上广播),最终的交易在比特币的网络上进行。如果两个没有创建子结算网络的账户希望通过闪电网络转账,如果他们之间能找到一条结算网络的路径,也可以通过一系列的交易实现低手续费交易。

由于闪电网络的具体实现需要创建一系列相互依赖的父子交易记录,需要先对子交易记录签名,然后将子交易记录交换后,再对父交易记录签名并广播。所以,有了隔离见证后,才能更完美的支持闪电网络。

第三一定程度上增加一个区块里可容纳的交易记录数,缓解交易拥堵:

这个比较好理解,见证信息占一个交易记录大约一半左右的空间,把见证信息挪走后,一个交易记录的大小会降低。

Deal with Excel reports with Pie Chart using Python

I recently created some excel reports by using python library: openpyxl

I have a ReportTemplate with pie chart. Then, I write some data into the template file so that the data can be shown in pie chart.

I can write excel in xlsx format succesfully. But I got some error dialogs prompting “excel found unreadable content” when I opened the excel file if it contains pie chart.

Although it worked well after I dismissed the error dialogs, it’s still annoying.

So I did some tricks to get rid of it.

Below are what I did:

#open excel file

wb = openpyxl.load_workbook(“ReportTemplate_New2.xlsx”)
Sheet = wb[“Report”]

#write the cell

Sheet[batchnamecol+str(batchcurrow)].value = batchname

#save it

wb.save(outputname)

#the trick is: I unzip the template and output report files, then, copy some xml #files from the output report into template. Then, zip the updated template #extracted folder as the final output.

zip_ref = zipfile.ZipFile(filename, ‘r’)
zip_ref.extractall(unzippathsource)
zip_ref.close()

target_ref = zipfile.ZipFile(“ReportTemplate_New2.xlsx”, ‘r’)
target_ref.extractall(unzippathtarget)
target_ref.close()

#I only have one sheet, so just copy three xml files

shutil.copyfile(unzippathsource+”/xl/worksheets/sheet1.xml”, unzippathtarget+”/xl/worksheets/sheet1.xml”)
shutil.copyfile(unzippathsource+”/xl/workbook.xml”, unzippathtarget+”/xl/workbook.xml”)
shutil.copyfile(unzippathsource+”/xl/sharedStrings.xml”, unzippathtarget+”/xl/sharedStrings.xml”)

shutil.make_archive(finaloutput, ‘zip’, “./” + unzippathtarget)
os.rename(finaloutput+”.zip”, finaloutput)

 

There is another way by writting these three files directory using report data. But I’m just lazy and don’t want to analyze the format of excel xml.

Hello world!

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!