From fe0e9abd73ab03c229880795a095c6b78f59a3ad Mon Sep 17 00:00:00 2001 From: luodanwg Date: Fri, 14 Jul 2017 18:02:21 +0800 Subject: [PATCH] add nonce to BookKeeping transaction add nonce to BookKeeping transaction to solve the bookKeeping Overwrite problem. Signed-off-by: luodanwg --- consensus/dbft/dbftService.go | 9 +-- core/ledger/block.go | 76 ++++++++++++------------- core/transaction/payload/BookKeeping.go | 16 +++++- core/transaction/transaction.go | 2 + net/httpjsonrpc/TransPayloadToHex.go | 4 ++ 5 files changed, 61 insertions(+), 46 deletions(-) diff --git a/consensus/dbft/dbftService.go b/consensus/dbft/dbftService.go index ca6e8079..db2a46ad 100644 --- a/consensus/dbft/dbftService.go +++ b/consensus/dbft/dbftService.go @@ -170,13 +170,14 @@ func (ds *DbftService) CheckSignatures() error { func (ds *DbftService) CreateBookkeepingTransaction(nonce uint64) *tx.Transaction { log.Debug() - //TODO: sysfee - + bookKeepingPayload := &payload.BookKeeping{ + Nonce: uint64(time.Now().UnixNano()), + } return &tx.Transaction{ TxType: tx.BookKeeping, - PayloadVersion: 0x2, - Payload: &payload.BookKeeping{}, + PayloadVersion: tx.DefaultPayloadVersion, + Payload: bookKeepingPayload, Attributes: []*tx.TxAttribute{}, UTXOInputs: []*tx.UTXOTxInput{}, BalanceInputs: []*tx.BalanceTxInput{}, diff --git a/core/ledger/block.go b/core/ledger/block.go index 0f91ebf4..353358a3 100644 --- a/core/ledger/block.go +++ b/core/ledger/block.go @@ -7,6 +7,7 @@ import ( "DNA/core/contract/program" sig "DNA/core/signature" tx "DNA/core/transaction" + "DNA/core/transaction/payload" "DNA/crypto" . "DNA/errors" "DNA/vm" @@ -14,6 +15,9 @@ import ( "time" ) +const BlockVersion uint32 = 0 +const GenesisNonce uint64 = 2083236893 + type Block struct { Blockdata *Blockdata Transactions []*tx.Transaction @@ -144,51 +148,43 @@ func (b *Block) Type() InventoryType { } func GenesisBlockInit() (*Block, error) { - genesisBlock := new(Block) - //blockdata - genesisBlockdata := new(Blockdata) - genesisBlockdata.Version = uint32(0x00) - genesisBlockdata.PrevBlockHash = Uint256{} - genesisBlockdata.TransactionsRoot = Uint256{} - tm := time.Date(2017, time.February, 23, 0, 0, 0, 0, time.UTC) - genesisBlockdata.Timestamp = uint32(tm.Unix()) - genesisBlockdata.Height = uint32(0) - genesisBlockdata.ConsensusData = uint64(2083236893) + //getBookKeeper nextBookKeeper, err := GetBookKeeperAddress(StandbyBookKeepers) if err != nil { return nil, NewDetailErr(err, ErrNoCode, "[Block],GenesisBlockInit err with GetBookKeeperAddress") } - genesisBlockdata.NextBookKeeper = nextBookKeeper - - pg := new(program.Program) - pg.Code = []byte{'0'} - pg.Parameter = []byte{byte(vm.PUSHT)} - genesisBlockdata.Program = pg - + //blockdata + genesisBlockdata := &Blockdata{ + Version: BlockVersion, + PrevBlockHash: Uint256{}, + TransactionsRoot: Uint256{}, + Timestamp: uint32(uint32(time.Date(2017, time.February, 23, 0, 0, 0, 0, time.UTC).Unix())), + Height: uint32(0), + ConsensusData: GenesisNonce, + NextBookKeeper: nextBookKeeper, + Program: &program.Program{ + Code: []byte{}, + Parameter: []byte{byte(vm.PUSHT)}, + }, + } //transaction - trans := new(tx.Transaction) - { - trans.TxType = tx.BookKeeping - trans.PayloadVersion = byte(0) - trans.Payload = nil - trans.Attributes = nil - trans.UTXOInputs = nil - trans.BalanceInputs = nil - trans.Outputs = nil - { - programHashes := []*program.Program{} - pg := new(program.Program) - pg.Code = []byte{'0'} - pg.Parameter = []byte{byte(vm.PUSHT)} - programHashes = append(programHashes, pg) - trans.Programs = programHashes - } - } - genesisBlock.Blockdata = genesisBlockdata - - genesisBlock.Transactions = append(genesisBlock.Transactions, trans) - - //hashx := genesisBlock.Hash() + trans := &tx.Transaction{ + TxType: tx.BookKeeping, + PayloadVersion: tx.DefaultPayloadVersion, + Payload: &payload.BookKeeping{ + Nonce: GenesisNonce, + }, + Attributes: []*tx.TxAttribute{}, + UTXOInputs: []*tx.UTXOTxInput{}, + BalanceInputs: []*tx.BalanceTxInput{}, + Outputs: []*tx.TxOutput{}, + Programs: []*program.Program{}, + } + //block + genesisBlock := &Block{ + Blockdata: genesisBlockdata, + Transactions: []*tx.Transaction{trans}, + } return genesisBlock, nil } diff --git a/core/transaction/payload/BookKeeping.go b/core/transaction/payload/BookKeeping.go index 57ca0fda..14628cb1 100644 --- a/core/transaction/payload/BookKeeping.go +++ b/core/transaction/payload/BookKeeping.go @@ -1,8 +1,12 @@ package payload -import "io" +import ( + "DNA/common/serialization" + "io" +) type BookKeeping struct { + Nonce uint64 } func (a *BookKeeping) Data() []byte { @@ -10,10 +14,18 @@ func (a *BookKeeping) Data() []byte { } func (a *BookKeeping) Serialize(w io.Writer) error { + err := serialization.WriteUint64(w, a.Nonce) + if err != nil { + return err + } return nil } func (a *BookKeeping) Deserialize(r io.Reader) error { + var err error + a.Nonce, err = serialization.ReadUint64(r) + if err != nil { + return err + } return nil } - diff --git a/core/transaction/transaction.go b/core/transaction/transaction.go index b14adf80..e61bd2fa 100644 --- a/core/transaction/transaction.go +++ b/core/transaction/transaction.go @@ -16,6 +16,8 @@ import ( . "DNA/errors" ) +const DefaultPayloadVersion byte = 0x00 + //for different transaction types with different payload format //and transaction process methods type TransactionType byte diff --git a/net/httpjsonrpc/TransPayloadToHex.go b/net/httpjsonrpc/TransPayloadToHex.go index 691e5b28..c03bbe21 100644 --- a/net/httpjsonrpc/TransPayloadToHex.go +++ b/net/httpjsonrpc/TransPayloadToHex.go @@ -13,6 +13,7 @@ type PayloadInfo interface{} //implement PayloadInfo define BookKeepingInfo type BookKeepingInfo struct { + Nonce uint64 } //implement PayloadInfo define DeployCodeInfo @@ -78,6 +79,9 @@ type PrivacyPayloadInfo struct { func TransPayloadToHex(p Payload) PayloadInfo { switch object := p.(type) { case *payload.BookKeeping: + obj := new(BookKeepingInfo) + obj.Nonce = object.Nonce + return obj case *payload.BookKeeper: obj := new(BookkeeperInfo) encodedPubKey, _ := object.PubKey.EncodePoint(true)