Skip to content

Commit

Permalink
add nonce to BookKeeping transaction
Browse files Browse the repository at this point in the history
add nonce to BookKeeping transaction to solve the bookKeeping
Overwrite problem.

Signed-off-by: luodanwg <[email protected]>
  • Loading branch information
Arbio5zt authored and dreamfly281 committed Jul 26, 2017
1 parent 933bbdd commit fe0e9ab
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 46 deletions.
9 changes: 5 additions & 4 deletions consensus/dbft/dbftService.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down
76 changes: 36 additions & 40 deletions core/ledger/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import (
"DNA/core/contract/program"
sig "DNA/core/signature"
tx "DNA/core/transaction"
"DNA/core/transaction/payload"
"DNA/crypto"
. "DNA/errors"
"DNA/vm"
"io"
"time"
)

const BlockVersion uint32 = 0
const GenesisNonce uint64 = 2083236893

type Block struct {
Blockdata *Blockdata
Transactions []*tx.Transaction
Expand Down Expand Up @@ -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
}
Expand Down
16 changes: 14 additions & 2 deletions core/transaction/payload/BookKeeping.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
package payload

import "io"
import (
"DNA/common/serialization"
"io"
)

type BookKeeping struct {
Nonce uint64
}

func (a *BookKeeping) Data() []byte {
return []byte{0}
}

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
}

2 changes: 2 additions & 0 deletions core/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions net/httpjsonrpc/TransPayloadToHex.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type PayloadInfo interface{}

//implement PayloadInfo define BookKeepingInfo
type BookKeepingInfo struct {
Nonce uint64
}

//implement PayloadInfo define DeployCodeInfo
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit fe0e9ab

Please sign in to comment.