Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add height in transaction #415

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions cli/asset/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ func makeRegTransaction(admin, issuer *account.Account, name string, description
fmt.Println("CreateSignatureContract failed")
return "", err
}
tx, _ := transaction.NewRegisterAssetTransaction(asset, value, issuer.PubKey(), transactionContract.ProgramHash)
height, err := GetCurrBlockHeight()
if err != nil {
fmt.Println("Can not get currBlockHeight", err)
return "", err
}
tx, _ := transaction.NewRegisterAssetTransaction(asset, value, issuer.PubKey(), transactionContract.ProgramHash, height)
txAttr := transaction.NewTxAttribute(transaction.Nonce, []byte(strconv.FormatInt(rand.Int63(), 10)))
tx.Attributes = make([]*transaction.TxAttribute, 0)
tx.Attributes = append(tx.Attributes, &txAttr)
Expand All @@ -124,7 +129,12 @@ func makeIssueTransaction(issuer *account.Account, programHashStr, assetHashStr
ProgramHash: programHash,
}
outputs := []*transaction.TxOutput{issueTxOutput}
tx, _ := transaction.NewIssueAssetTransaction(outputs)
height, err := GetCurrBlockHeight()
if err != nil {
fmt.Println("Can not get currBlockHeight", err)
return "", err
}
tx, _ := transaction.NewIssueAssetTransaction(outputs, height)
txAttr := transaction.NewTxAttribute(transaction.Nonce, []byte(strconv.FormatInt(rand.Int63(), 10)))
tx.Attributes = make([]*transaction.TxAttribute, 0)
tx.Attributes = append(tx.Attributes, &txAttr)
Expand Down Expand Up @@ -217,7 +227,12 @@ func makeTransferTransaction(signer *account.Account, programHashStr, assetHashS
if expected != 0 {
return "", errors.New("transfer failed, ammount is not enough")
}
tx, _ := transaction.NewTransferAssetTransaction(inputs, outputs)
height, err := GetCurrBlockHeight()
if err != nil {
fmt.Println("Can not get currBlockHeight", err)
return "", err
}
tx, _ := transaction.NewTransferAssetTransaction(inputs, outputs, height)
txAttr := transaction.NewTxAttribute(transaction.Nonce, []byte(strconv.FormatInt(rand.Int63(), 10)))
tx.Attributes = make([]*transaction.TxAttribute, 0)
tx.Attributes = append(tx.Attributes, &txAttr)
Expand Down Expand Up @@ -248,6 +263,7 @@ func assetAction(c *cli.Context) error {

wallet := openWallet(c.String("wallet"), WalletPassword(c.String("password")))
admin, _ := wallet.GetDefaultAccount()

value := c.Int64("value")
if value == 0 {
fmt.Println("invalid value [--value]")
Expand Down Expand Up @@ -294,6 +310,7 @@ func assetAction(c *cli.Context) error {
}

func NewCommand() *cli.Command {

return &cli.Command{
Name: "asset",
Usage: "asset registration, issuance and transfer",
Expand Down
7 changes: 6 additions & 1 deletion cli/bookkeeper/bookkeeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import (
)

func makeBookkeeperTransaction(pubkey *crypto.PubKey, op bool, cert []byte, issuer *account.Account) (string, error) {
tx, _ := transaction.NewBookKeeperTransaction(pubkey, op, cert, issuer.PubKey())
height, err := GetCurrBlockHeight()
if err != nil {
fmt.Println("Can not get currBlockHeight", err)
return "", err
}
tx, _ := transaction.NewBookKeeperTransaction(pubkey, op, cert, issuer.PubKey(), height)
attr := transaction.NewTxAttribute(transaction.Nonce, []byte(strconv.FormatInt(rand.Int63(), 10)))
tx.Attributes = make([]*transaction.TxAttribute, 0)
tx.Attributes = append(tx.Attributes, &attr)
Expand Down
20 changes: 20 additions & 0 deletions cli/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"DNA/common/config"
"DNA/common/password"
"DNA/net/httpjsonrpc"

"github.com/urfave/cli"
)
Expand Down Expand Up @@ -69,3 +70,22 @@ func WalletPassword(passwd string) []byte {
return []byte(passwd)
}
}

func GetCurrBlockHeight() (uint32, error) {
resp, err := httpjsonrpc.Call(Address(), "getbestblockheight", 0, []interface{}{})

if err != nil {
fmt.Println("HTTP JSON call failed", err)
return 0, err
}
r := make(map[string]interface{})
err = json.Unmarshal(resp, &r)
if err != nil {
fmt.Println("Unmarshal JSON failed")
return 0, err
}

height := r["result"].(float64)

return uint32(height), nil
}
7 changes: 6 additions & 1 deletion cli/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,12 @@ func dataAction(c *cli.Context) error {
wallet := openWallet(c.String("wallet"), WalletPassword(c.String("password")))
admin, _ := wallet.GetDefaultAccount()

tx, _ = transaction.NewDataFileTransaction(address, name, "", admin.PubKey())
height, err := GetCurrBlockHeight()
if err != nil {
fmt.Println("Can not get currBlockHeight", err)
return err
}
tx, _ = transaction.NewDataFileTransaction(address, name, "", admin.PubKey(), height)
txAttr := transaction.NewTxAttribute(transaction.Nonce, []byte(strconv.FormatInt(rand.Int63(), 10)))
tx.Attributes = make([]*transaction.TxAttribute, 0)
tx.Attributes = append(tx.Attributes, &txAttr)
Expand Down
7 changes: 6 additions & 1 deletion cli/privpayload/privpayload.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ func makePrivacyTx(admin *account.Account, toPubkeyStr string, pload string) (st
toPk, _ := hex.DecodeString(toPubkeyStr)
toPubkey, _ := crypto.DecodePoint(toPk)

tx, _ := transaction.NewPrivacyPayloadTransaction(admin.PrivateKey, admin.PublicKey, toPubkey, payload.RawPayload, data)
height, err := GetCurrBlockHeight()
if err != nil {
fmt.Println("Can not get currBlockHeight", err)
return "", err
}
tx, _ := transaction.NewPrivacyPayloadTransaction(admin.PrivateKey, admin.PublicKey, toPubkey, payload.RawPayload, data, height)
txAttr := transaction.NewTxAttribute(transaction.Nonce, []byte(strconv.FormatInt(rand.Int63(), 10)))
tx.Attributes = make([]*transaction.TxAttribute, 0)
tx.Attributes = append(tx.Attributes, &txAttr)
Expand Down
6 changes: 2 additions & 4 deletions cli/test/test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package test

import (
"fmt"
"os"

. "DNA/cli/common"
"DNA/net/httpjsonrpc"

"fmt"
"github.com/urfave/cli"
"os"
)

func testAction(c *cli.Context) (err error) {
Expand Down
1 change: 1 addition & 0 deletions common/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Configuration struct {
Version int `json:"Version"`
SeedList []string `json:"SeedList"`
BookKeepers []string `json:"BookKeepers"` // The default book keepers' publickey
TxValidInterval uint32 `json:TxValidInterval`
HttpRestPort int `json:"HttpRestPort"`
RestCertPath string `json:"RestCertPath"`
RestKeyPath string `json:"RestKeyPath"`
Expand Down
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"032c842494feba4e3dec3b9b7d9ad080ce63c81a41f7d79d2bbb5d499d16322907",
"03d36828a99547184452276116f1b5171861931ff439a6da2316fddf1f3f428850"
],
"TxValidInterval": 50,
"HttpInfoPort": 20333,
"HttpInfoStart": true,
"HttpRestPort": 20334,
Expand Down
25 changes: 15 additions & 10 deletions consensus/dbft/dbftService.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (ds *DbftService) BlockPersistCompleted(v interface{}) {
log.Debug()
if block, ok := v.(*ledger.Block); ok {
log.Infof("persist block: %x", block.Hash())
err := ds.localNet.CleanSubmittedTransactions(block)
err := ds.localNet.CleanSubmittedTransactions(block.Transactions)
if err != nil {
log.Warn(err)
}
Expand Down Expand Up @@ -174,16 +174,19 @@ func (ds *DbftService) CreateBookkeepingTransaction(nonce uint64) *tx.Transactio
bookKeepingPayload := &payload.BookKeeping{
Nonce: uint64(time.Now().UnixNano()),
}
return &tx.Transaction{
TxType: tx.BookKeeping,
PayloadVersion: payload.BookKeepingPayloadVersion,
Payload: bookKeepingPayload,
Attributes: []*tx.TxAttribute{},
UTXOInputs: []*tx.UTXOTxInput{},
BalanceInputs: []*tx.BalanceTxInput{},
Outputs: []*tx.TxOutput{},
Programs: []*program.Program{},

trans := &tx.Transaction{
TxType: tx.BookKeeping,
Payload: bookKeepingPayload,
Attributes: []*tx.TxAttribute{},
UTXOInputs: []*tx.UTXOTxInput{},
BalanceInputs: []*tx.BalanceTxInput{},
Outputs: []*tx.TxOutput{},
Programs: []*program.Program{},
}
trans.SetPayloadVersion(payload.BookKeepingPayloadVersion)

return trans
}

func (ds *DbftService) ChangeViewReceived(payload *msg.ConsensusPayload, message *ChangeView) {
Expand Down Expand Up @@ -554,9 +557,11 @@ func (ds *DbftService) Timeout() {
//add book keeping transaction first
ds.context.Transactions = append(ds.context.Transactions, txBookkeeping)
//add transactions from transaction pool

for _, tx := range transactionsPool {
ds.context.Transactions = append(ds.context.Transactions, tx)
}

ds.context.header = nil
//build block and sign
block := ds.context.MakeHeader()
Expand Down
4 changes: 2 additions & 2 deletions core/ledger/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ func GenesisBlockInit(defaultBookKeeper []*crypto.PubKey) (*Block, error) {
}
//transaction
trans := &tx.Transaction{
TxType: tx.BookKeeping,
PayloadVersion: payload.BookKeepingPayloadVersion,
TxType: tx.BookKeeping,
Payload: &payload.BookKeeping{
Nonce: GenesisNonce,
},
Expand All @@ -180,6 +179,7 @@ func GenesisBlockInit(defaultBookKeeper []*crypto.PubKey) (*Block, error) {
Outputs: []*tx.TxOutput{},
Programs: []*program.Program{},
}
trans.SetPayloadVersion(payload.BookKeepingPayloadVersion)
//block
genesisBlock := &Block{
Blockdata: genesisBlockdata,
Expand Down
4 changes: 2 additions & 2 deletions core/ledger/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewBlockchain(height uint32) *Blockchain {
}
}

func NewBlockchainWithGenesisBlock(defaultBookKeeper []*crypto.PubKey) (*Blockchain, error) {
func NewBlockchainWithGenesisBlock(defaultBookKeeper []*crypto.PubKey, txValidinterval uint32) (*Blockchain, error) {
genesisBlock, err := GenesisBlockInit(defaultBookKeeper)
if err != nil {
return nil, NewDetailErr(err, ErrNoCode, "[Blockchain], NewBlockchainWithGenesisBlock failed.")
Expand All @@ -32,7 +32,7 @@ func NewBlockchainWithGenesisBlock(defaultBookKeeper []*crypto.PubKey) (*Blockch
hashx := genesisBlock.Hash()
genesisBlock.hash = &hashx

height, err := DefaultLedger.Store.InitLedgerStoreWithGenesisBlock(genesisBlock, defaultBookKeeper)
height, err := DefaultLedger.Store.InitLedgerStoreWithGenesisBlock(genesisBlock, defaultBookKeeper, txValidinterval)
if err != nil {
return nil, NewDetailErr(err, ErrNoCode, "[Blockchain], InitLevelDBStoreWithGenesisBlock failed.")
}
Expand Down
4 changes: 3 additions & 1 deletion core/ledger/ledgerStore.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ type ILedgerStore interface {
GetHeaderHashByHeight(height uint32) Uint256

GetBookKeeperList() ([]*crypto.PubKey, []*crypto.PubKey, error)
InitLedgerStoreWithGenesisBlock(genesisblock *Block, defaultBookKeeper []*crypto.PubKey) (uint32, error)
GetTxValidInterval() (uint32, error)

InitLedgerStoreWithGenesisBlock(genesisBlock *Block, defaultBookKeeper []*crypto.PubKey, txValidInterval uint32) (uint32, error)

GetQuantityIssued(assetid Uint256) (Fixed64, error)

Expand Down
31 changes: 27 additions & 4 deletions core/store/ChainStore/ChainStore.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ import (
)

const (
HeaderHashListCount = 2000
CleanCacheThreshold = 2
TaskChanCap = 4
HeaderHashListCount = 2000
CleanCacheThreshold = 2
TaskChanCap = 4
DefaultTxValidInterval = 50
)

var (
Expand Down Expand Up @@ -149,7 +150,10 @@ func (self *ChainStore) clearCache() {

}

func (bd *ChainStore) InitLedgerStoreWithGenesisBlock(genesisBlock *Block, defaultBookKeeper []*crypto.PubKey) (uint32, error) {
func (bd *ChainStore) InitLedgerStoreWithGenesisBlock(genesisBlock *Block, defaultBookKeeper []*crypto.PubKey, txValidInterval uint32) (uint32, error) {
if txValidInterval == 0 {
txValidInterval = DefaultTxValidInterval
}

hash := genesisBlock.Hash()
bd.headerIndex[0] = hash
Expand Down Expand Up @@ -289,6 +293,10 @@ func (bd *ChainStore) InitLedgerStoreWithGenesisBlock(genesisBlock *Block, defau
bd.st.Put(bkListKey.Bytes(), bkListValue.Bytes())
///////////////////////////////////////////////////

tvi := bytes.NewBuffer(nil)
serialization.WriteUint32(tvi, txValidInterval)
bd.st.Put([]byte{byte(SYS_TxValidInterval)}, tvi.Bytes())

// persist genesis block
bd.persist(genesisBlock)

Expand Down Expand Up @@ -629,6 +637,21 @@ func (bd *ChainStore) GetBlock(hash Uint256) (*Block, error) {
return b, nil
}

func (self *ChainStore) GetTxValidInterval() (uint32, error) {
prefix := []byte{byte(SYS_TxValidInterval)}
tvi, err := self.st.Get(prefix)
if err != nil {
return 0, err
}

txValidInterval, err := serialization.ReadUint32(bytes.NewReader(tvi))
if err != nil {
return 0, err
}

return txValidInterval, nil
}

func (self *ChainStore) GetBookKeeperList() ([]*crypto.PubKey, []*crypto.PubKey, error) {
prefix := []byte{byte(SYS_CurrentBookKeeper)}
bkListValue, err_get := self.st.Get(prefix)
Expand Down
1 change: 1 addition & 0 deletions core/store/ChainStore/DataEntryPrefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
SYS_CurrentBlock DataEntryPrefix = 0x40
// SYS_CurrentHeader DataEntryPrefix = 0x41
SYS_CurrentBookKeeper DataEntryPrefix = 0x42
SYS_TxValidInterval DataEntryPrefix = 0x43

//CONFIG
CFG_Version DataEntryPrefix = 0xf0
Expand Down
Loading