aboutsummaryrefslogtreecommitdiffstats
path: root/dex
diff options
context:
space:
mode:
authorSonic <sonic@dexon.org>2019-01-18 11:55:51 +0800
committerWei-Ning Huang <w@byzantine-lab.io>2019-06-12 17:27:21 +0800
commit1c01b079c42bf410997b800c8e97b699e3ce0a93 (patch)
tree00626fcfc3bd1a4f63fd23efeb7c36f5498cbc9a /dex
parent65045b14287ee4f789a491ae542a58f1f7d559b6 (diff)
downloadgo-tangerine-1c01b079c42bf410997b800c8e97b699e3ce0a93.tar
go-tangerine-1c01b079c42bf410997b800c8e97b699e3ce0a93.tar.gz
go-tangerine-1c01b079c42bf410997b800c8e97b699e3ce0a93.tar.bz2
go-tangerine-1c01b079c42bf410997b800c8e97b699e3ce0a93.tar.lz
go-tangerine-1c01b079c42bf410997b800c8e97b699e3ce0a93.tar.xz
go-tangerine-1c01b079c42bf410997b800c8e97b699e3ce0a93.tar.zst
go-tangerine-1c01b079c42bf410997b800c8e97b699e3ce0a93.zip
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.
Diffstat (limited to 'dex')
-rw-r--r--dex/app.go25
-rw-r--r--dex/app_test.go12
2 files changed, 13 insertions, 24 deletions
diff --git a/dex/app.go b/dex/app.go
index 3ea8953f5..e4fac8c7f 100644
--- a/dex/app.go
+++ b/dex/app.go
@@ -298,10 +298,7 @@ func (d *DexconApp) PrepareWitness(consensusHeight uint64) (witness coreTypes.Wi
return witness, fmt.Errorf("current height < consensus height")
}
- witnessData, err := rlp.EncodeToBytes(&types.WitnessData{
- Root: witnessBlock.Root(),
- ReceiptHash: witnessBlock.ReceiptHash(),
- })
+ witnessData, err := rlp.EncodeToBytes(witnessBlock.Hash())
if err != nil {
return
}
@@ -314,8 +311,8 @@ func (d *DexconApp) PrepareWitness(consensusHeight uint64) (witness coreTypes.Wi
// VerifyBlock verifies if the payloads are valid.
func (d *DexconApp) VerifyBlock(block *coreTypes.Block) coreTypes.BlockVerifyStatus {
- var witnessData types.WitnessData
- err := rlp.DecodeBytes(block.Witness.Data, &witnessData)
+ var witnessBlockHash common.Hash
+ err := rlp.DecodeBytes(block.Witness.Data, &witnessBlockHash)
if err != nil {
log.Error("Failed to RLP decode witness data", "error", err)
return coreTypes.VerifyInvalidBlock
@@ -329,23 +326,19 @@ func (d *DexconApp) VerifyBlock(block *coreTypes.Block) coreTypes.BlockVerifySta
b := d.blockchain.GetBlockByNumber(block.Witness.Height)
if b == nil {
- log.Error("Can not get block by height %v", block.Witness.Height)
+ log.Error("Can not get block by height", "height", block.Witness.Height)
return coreTypes.VerifyInvalidBlock
}
- if b.Root() != witnessData.Root {
- log.Error("Witness root not correct expect %v but %v", b.Root(), witnessData.Root)
+ if b.Hash() != witnessBlockHash {
+ log.Error("Witness block hash not match",
+ "expect", b.Hash().String(), "got", witnessBlockHash.String())
return coreTypes.VerifyInvalidBlock
}
- if b.ReceiptHash() != witnessData.ReceiptHash {
- log.Error("Witness receipt hash not correct expect %v but %v", b.ReceiptHash(), witnessData.ReceiptHash)
- return coreTypes.VerifyInvalidBlock
- }
-
- _, err = d.blockchain.StateAt(witnessData.Root)
+ _, err = d.blockchain.StateAt(b.Root())
if err != nil {
- log.Error("Get state by root %v error: %v", witnessData.Root, err)
+ log.Error("Get state by root %v error: %v", b.Root(), err)
return coreTypes.VerifyInvalidBlock
}
diff --git a/dex/app_test.go b/dex/app_test.go
index 7d665a0af..34b31d3d3 100644
--- a/dex/app_test.go
+++ b/dex/app_test.go
@@ -119,18 +119,14 @@ func TestPrepareWitness(t *testing.T) {
t.Fatalf("unexpeted witness height %v", witness.Height)
}
- var witnessData types.WitnessData
- err = rlp.DecodeBytes(witness.Data, &witnessData)
+ var witnessBlockHash common.Hash
+ err = rlp.DecodeBytes(witness.Data, &witnessBlockHash)
if err != nil {
t.Fatalf("rlp decode error: %v", err)
}
- if witnessData.Root != currentBlock.Root() {
- t.Fatalf("expect root %v but %v", currentBlock.Root(), witnessData.Root)
- }
-
- if witnessData.ReceiptHash != currentBlock.ReceiptHash() {
- t.Fatalf("expect receipt hash %v but %v", currentBlock.ReceiptHash(), witnessData.ReceiptHash)
+ if witnessBlockHash != currentBlock.Hash() {
+ t.Fatalf("expect root %v but %v", currentBlock.Hash(), witnessBlockHash)
}
if _, err := dex.app.PrepareWitness(999); err == nil {