diff options
author | Wei-Ning Huang <w@dexon.org> | 2018-09-12 11:47:27 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-12 11:47:27 +0800 |
commit | 743983c82b601e200fa53d4aa8973f83ff628d29 (patch) | |
tree | 25ba7f6974233e062e8a8824ac702ac2c715f5a9 | |
parent | 0e8fda250804b9c46232287a18af05e7ccf5bf72 (diff) | |
download | dexon-consensus-743983c82b601e200fa53d4aa8973f83ff628d29.tar dexon-consensus-743983c82b601e200fa53d4aa8973f83ff628d29.tar.gz dexon-consensus-743983c82b601e200fa53d4aa8973f83ff628d29.tar.bz2 dexon-consensus-743983c82b601e200fa53d4aa8973f83ff628d29.tar.lz dexon-consensus-743983c82b601e200fa53d4aa8973f83ff628d29.tar.xz dexon-consensus-743983c82b601e200fa53d4aa8973f83ff628d29.tar.zst dexon-consensus-743983c82b601e200fa53d4aa8973f83ff628d29.zip |
core: types: use []byte for block Payload type (#101)
Change payload type to []byte instead of [][]byte to make it more
generic. The user of the consensus core should marshal the payload into
a byte slice by themselves.
-rw-r--r-- | core/consensus.go | 4 | ||||
-rw-r--r-- | core/crypto.go | 2 | ||||
-rw-r--r-- | core/interfaces.go | 2 | ||||
-rw-r--r-- | core/nonblocking-application.go | 8 | ||||
-rw-r--r-- | core/nonblocking-application_test.go | 4 | ||||
-rw-r--r-- | core/test/app.go | 6 | ||||
-rw-r--r-- | core/types/block.go | 9 | ||||
-rw-r--r-- | simulation/app.go | 6 |
8 files changed, 19 insertions, 22 deletions
diff --git a/core/consensus.go b/core/consensus.go index 91ca657..0bc7c7d 100644 --- a/core/consensus.go +++ b/core/consensus.go @@ -517,7 +517,7 @@ func (con *Consensus) PrepareBlock(b *types.Block, con.rbModule.prepareBlock(b) b.Timestamp = proposeTime - b.Payloads = con.app.PreparePayloads(b.Position) + b.Payload = con.app.PreparePayload(b.Position) b.Hash, err = hashBlock(b) if err != nil { return @@ -535,7 +535,7 @@ func (con *Consensus) PrepareGenesisBlock(b *types.Block, if err = con.checkPrepareBlock(b, proposeTime); err != nil { return } - if len(b.Payloads) != 0 { + if len(b.Payload) != 0 { err = ErrGenesisBlockNotEmpty return } diff --git a/core/crypto.go b/core/crypto.go index 656dd74..3fe39ac 100644 --- a/core/crypto.go +++ b/core/crypto.go @@ -66,7 +66,7 @@ func hashBlock(block *types.Block) (common.Hash, error) { if err != nil { return common.Hash{}, err } - payloadHash := crypto.Keccak256Hash(block.Payloads...) + payloadHash := crypto.Keccak256Hash(block.Payload) hash := crypto.Keccak256Hash( block.ProposerID.Hash[:], diff --git a/core/interfaces.go b/core/interfaces.go index 080db20..c8f57e9 100644 --- a/core/interfaces.go +++ b/core/interfaces.go @@ -30,7 +30,7 @@ import ( // consensus core. type Application interface { // PreparePayload is called when consensus core is preparing a block. - PreparePayloads(position types.Position) [][]byte + PreparePayload(position types.Position) []byte // VerifyPayloads verifies if the payloads are valid. VerifyPayloads(payloads [][]byte) bool diff --git a/core/nonblocking-application.go b/core/nonblocking-application.go index 1e20fa4..7af12d4 100644 --- a/core/nonblocking-application.go +++ b/core/nonblocking-application.go @@ -120,10 +120,10 @@ func (app *nonBlockingApplication) wait() { app.running.Wait() } -// PreparePayloads cannot be non-blocking. -func (app *nonBlockingApplication) PreparePayloads( - position types.Position) [][]byte { - return app.app.PreparePayloads(position) +// PreparePayload cannot be non-blocking. +func (app *nonBlockingApplication) PreparePayload( + position types.Position) []byte { + return app.app.PreparePayload(position) } // VerifyPayloads cannot be non-blocking. diff --git a/core/nonblocking-application_test.go b/core/nonblocking-application_test.go index 1968445..a1dd727 100644 --- a/core/nonblocking-application_test.go +++ b/core/nonblocking-application_test.go @@ -47,8 +47,8 @@ func newSlowApp(sleep time.Duration) *slowApp { } } -func (app *slowApp) PreparePayloads(_ types.Position) [][]byte { - return [][]byte{} +func (app *slowApp) PreparePayload(_ types.Position) []byte { + return []byte{} } func (app *slowApp) VerifyPayloads(_ [][]byte) bool { diff --git a/core/test/app.go b/core/test/app.go index 60b74ac..76f1a10 100644 --- a/core/test/app.go +++ b/core/test/app.go @@ -102,9 +102,9 @@ func NewApp() *App { } } -// PreparePayloads implements Application interface. -func (app *App) PreparePayloads(position types.Position) [][]byte { - return [][]byte{} +// PreparePayload implements Application interface. +func (app *App) PreparePayload(position types.Position) []byte { + return []byte{} } // VerifyPayloads implements Application. diff --git a/core/types/block.go b/core/types/block.go index 3585a97..06712b1 100644 --- a/core/types/block.go +++ b/core/types/block.go @@ -63,7 +63,7 @@ type Block struct { Position Position `json:"position"` Timestamp time.Time `json:"timestamps"` Acks map[common.Hash]struct{} `json:"acks"` - Payloads [][]byte `json:"payloads"` + Payload []byte `json:"payload"` Signature crypto.Signature `json:"signature"` CRSSignature crypto.Signature `json:"crs_signature"` @@ -96,11 +96,8 @@ func (b *Block) Clone() (bcopy *Block) { for k, v := range b.Acks { bcopy.Acks[k] = v } - bcopy.Payloads = make([][]byte, len(b.Payloads)) - for k, v := range b.Payloads { - bcopy.Payloads[k] = make([]byte, len(v)) - copy(bcopy.Payloads[k], v) - } + bcopy.Payload = make([]byte, len(b.Payload)) + copy(bcopy.Payload, b.Payload) return } diff --git a/simulation/app.go b/simulation/app.go index c312204..40aa32b 100644 --- a/simulation/app.go +++ b/simulation/app.go @@ -94,9 +94,9 @@ func (a *simApp) getAckedBlocks(ackHash common.Hash) (output common.Hashes) { return } -// PreparePayloads implements core.Application. -func (a *simApp) PreparePayloads(position types.Position) [][]byte { - return [][]byte{} +// PreparePayload implements core.Application. +func (a *simApp) PreparePayload(position types.Position) []byte { + return []byte{} } // StronglyAcked is called when a block is strongly acked by DEXON |