From 1c01b079c42bf410997b800c8e97b699e3ce0a93 Mon Sep 17 00:00:00 2001 From: Sonic Date: Fri, 18 Jan 2019 11:55:51 +0800 Subject: core, dex: use block hash as witness data (#160) Using only state root and receipt root as witness data can not protect other fields in block header, ex: bloom, difficulty, gas limit, gas used... So that everyone can manipulate these fields to create as many valid blocks at the same height as he want. Although this will not effect the state, one can spam us when syncing. Using block hash as witness data can solve this. --- core/blockchain_test.go | 38 +++++++------------------------------- 1 file changed, 7 insertions(+), 31 deletions(-) (limited to 'core/blockchain_test.go') diff --git a/core/blockchain_test.go b/core/blockchain_test.go index 45eb191f2..967d0d3c5 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -1662,20 +1662,12 @@ func TestProcessBlock(t *testing.T) { for i := 0; i < processNum; i++ { var witnessDataBytes []byte if i == 0 { - witnessData := types.WitnessData{ - Root: gspec.ToBlock(nil).Root(), - ReceiptHash: gspec.ToBlock(nil).ReceiptHash(), - } - witnessDataBytes, err = rlp.EncodeToBytes(&witnessData) + witnessDataBytes, err = rlp.EncodeToBytes(gspec.ToBlock(nil).Hash()) if err != nil { t.Fatalf("rlp encode fail: %v", err) } } else { - witnessData := types.WitnessData{ - Root: chain.CurrentBlock().Root(), - ReceiptHash: chain.CurrentBlock().ReceiptHash(), - } - witnessDataBytes, err = rlp.EncodeToBytes(&witnessData) + witnessDataBytes, err = rlp.EncodeToBytes(chain.CurrentBlock().Hash()) if err != nil { t.Fatalf("rlp encode fail: %v", err) } @@ -1723,11 +1715,7 @@ func TestProcessBlock(t *testing.T) { } // Validate witness fail with unknown block. - witnessData := types.WitnessData{ - Root: chain.CurrentBlock().Root(), - ReceiptHash: chain.CurrentBlock().ReceiptHash(), - } - witnessDataBytes, err := rlp.EncodeToBytes(&witnessData) + witnessDataBytes, err := rlp.EncodeToBytes(chain.CurrentBlock().Hash()) if err != nil { t.Fatalf("rlp encode fail: %v", err) } @@ -1746,11 +1734,7 @@ func TestProcessBlock(t *testing.T) { } // Validate witness fail with unexpected root. - witnessData = types.WitnessData{ - Root: chain.CurrentBlock().Root(), - ReceiptHash: chain.CurrentBlock().ReceiptHash(), - } - witnessDataBytes, err = rlp.EncodeToBytes(&witnessData) + witnessDataBytes, err = rlp.EncodeToBytes(chain.CurrentBlock().Hash()) if err != nil { t.Fatalf("rlp encode fail: %v", err) } @@ -1764,16 +1748,12 @@ func TestProcessBlock(t *testing.T) { Height: processNum - 1, Data: witnessDataBytes, }) - if err == nil || !strings.Contains(err.Error(), "invalid witness root") { + if err == nil || !strings.Contains(err.Error(), "invalid witness block") { t.Fatalf("not expected fail: %v", err) } // Apply transaction fail with insufficient fund. - witnessData = types.WitnessData{ - Root: chain.CurrentBlock().Root(), - ReceiptHash: chain.CurrentBlock().ReceiptHash(), - } - witnessDataBytes, err = rlp.EncodeToBytes(&witnessData) + witnessDataBytes, err = rlp.EncodeToBytes(chain.CurrentBlock().Hash()) if err != nil { t.Fatalf("rlp encode fail: %v", err) } @@ -1801,11 +1781,7 @@ func TestProcessBlock(t *testing.T) { } // Apply transaction fail with nonce too height. - witnessData = types.WitnessData{ - Root: chain.CurrentBlock().Root(), - ReceiptHash: chain.CurrentBlock().ReceiptHash(), - } - witnessDataBytes, err = rlp.EncodeToBytes(&witnessData) + witnessDataBytes, err = rlp.EncodeToBytes(chain.CurrentBlock().Hash()) if err != nil { t.Fatalf("rlp encode fail: %v", err) } -- cgit v1.2.3