Skip to content

Commit

Permalink
Fixing the blockchain always return "find the TXN" error
Browse files Browse the repository at this point in the history
The store database will always return a valid transaction pointer
even there is error happened or the transaction doesn't existed in fact.
and the blockchain will return true by only judge the pointer is valid
or not. It will lead to the result that all the TXN ID query will success
even the transaction doesn't existed at all

Other tiny print log modification and debug log clean up

Signed-off-by: Yanbo Li <[email protected]>
  • Loading branch information
dreamfly281 committed Mar 23, 2017
1 parent 11a5526 commit a4f6aa0
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 35 deletions.
11 changes: 8 additions & 3 deletions common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package common

import (
"bytes"
"fmt"
"encoding/binary"
_ "io"

"math/rand"
"golang.org/x/crypto/ripemd160"
"crypto/sha256"
. "GoOnchain/errors"
"GoOnchain/common/log"
"errors"
"io"
)
Expand All @@ -27,8 +29,11 @@ func ToCodeHash(code []byte) (Uint160,error){
}

func GetNonce() uint64 {
//TODO: GetNonce()
return 0
Trace()
// Fixme replace with the real random number generator
nonce := uint64(rand.Uint32())<<32 + uint64(rand.Uint32())
log.Debug(fmt.Sprintf("The new nonce is: 0x%x", nonce))
return nonce
}

func IntToBytes(n int) []byte {
Expand Down
8 changes: 6 additions & 2 deletions common/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
"time"
)

const (
PRINTLEVEL = 0
)

const (
debugLog = iota
infoLog
Expand All @@ -25,7 +29,7 @@ var (
levels = map[int]string{
debugLog: "DEBUG",
infoLog: "INFO",
warnLog: "WARN",
warnLog: "WARN",
errorLog: "ERROR",
fatalLog: "FATAL",
}
Expand Down Expand Up @@ -171,7 +175,7 @@ func CreatePrintLog(path string) {
if err != nil {
fmt.Printf("%s\n", err.Error)
}
var printlevel int = 1
var printlevel int = PRINTLEVEL
writers := []io.Writer{
logfile,
os.Stdout,
Expand Down
32 changes: 18 additions & 14 deletions consensus/dbft/dbftService.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,16 @@ func NewDbftService(client *cl.Client,logDictionary string,localNet net.Neter) *
}
}

func (ds *DbftService) AddTransaction(TX *tx.Transaction) error{
func (ds *DbftService) AddTransaction(TX *tx.Transaction) error {
Trace()

hasTx := ledger.DefaultLedger.Blockchain.ContainsTransaction(TX.Hash())
verifyTx := va.VerifyTransaction(TX,ledger.DefaultLedger,ds.context.GetTransactionList())
checkPolicy := ds.CheckPolicy(TX)
if hasTx || (verifyTx != nil) || (checkPolicy != nil) {

con.Log(fmt.Sprintf("reject tx: %s",TX.Hash()))
log.Debug("The return value is HasTx: ", hasTx, " verifyTX: ", verifyTx, " checkPolicy: ", checkPolicy)
if hasTx || (verifyTx != nil) || (checkPolicy != nil) {
log.Warn(fmt.Sprintf("Reject tx: %v", TX.Hash()))
ds.RequestChangeView()
return errors.New("Transcation is invalid.")
}
Expand All @@ -77,8 +78,8 @@ func (ds *DbftService) AddTransaction(TX *tx.Transaction) error{
return NewDetailErr(err,ErrNoCode,"[DbftService] ,GetMinerAddress failed")
}

if minerAddress == ds.context.NextMiner{
con.Log("send perpare response")
if minerAddress == ds.context.NextMiner {
log.Debug("Send perpare response")
ds.context.State |= SignatureSent
miner,err:=ds.Client.GetAccount(ds.context.Miners[ds.context.MinerIndex])
if err != nil {
Expand Down Expand Up @@ -142,13 +143,13 @@ func (ds *DbftService) CheckSignatures() error{
return nil
}

func (ds *DbftService) CreateBookkeepingTransaction(txs map[Uint256]*tx.Transaction,nonce uint64) *tx.Transaction {
func (ds *DbftService) CreateBookkeepingTransaction(nonce uint64) *tx.Transaction {
Trace()
return &tx.Transaction{
TxType: tx.BookKeeping,
PayloadVersion: 0x2,
Payload: &payload.MinerPayload{},
Nonce:uint64(0),
Nonce: nonce,
Attributes: []*tx.TxAttribute{},
UTXOInputs:[]*tx.UTXOTxInput{},
BalanceInputs:[]*tx.BalanceTxInput{},
Expand Down Expand Up @@ -326,7 +327,7 @@ func (ds *DbftService) NewTransactionPayload(transaction *tx.Transaction) error{
return ds.AddTransaction(transaction)
}

func (ds *DbftService) PrepareRequestReceived(payload *msg.ConsensusPayload,message *PrepareRequest) {
func (ds *DbftService) PrepareRequestReceived(payload *msg.ConsensusPayload, message *PrepareRequest) {
Trace()
log.Info(fmt.Sprintf("Prepare Request Received: height=%d View=%d index=%d tx=%d",payload.Height,message.ViewNumber(),payload.MinerIndex,len(message.TransactionHashes)))

Expand All @@ -339,7 +340,8 @@ func (ds *DbftService) PrepareRequestReceived(payload *msg.ConsensusPayload,mess
if uint32(payload.MinerIndex) != ds.context.PrimaryIndex {
fmt.Println("PrepareRequestReceived uint32(payload.MinerIndex)=",uint32(payload.MinerIndex))
fmt.Println("PrepareRequestReceived ds.context.PrimaryIndex=",ds.context.PrimaryIndex)
return }
return
}
header,err := ledger.DefaultLedger.Blockchain.GetHeader(ds.context.PrevHash)
if err != nil {
fmt.Println("PrepareRequestReceived GetHeader failed with ds.context.PrevHash",ds.context.PrevHash)
Expand All @@ -364,8 +366,9 @@ func (ds *DbftService) PrepareRequestReceived(payload *msg.ConsensusPayload,mess
ds.context.Transactions = make(map[Uint256]*tx.Transaction)

Trace()
if _,err := va.VerifySignature(ds.context.MakeHeader(),ds.context.Miners[payload.MinerIndex],message.Signature); err != nil {
fmt.Println("PrepareRequestReceived VerifySignature failed.",err)
_, err = va.VerifySignature(ds.context.MakeHeader(), ds.context.Miners[payload.MinerIndex], message.Signature)
if err != nil {
log.Warn("PrepareRequestReceived VerifySignature failed.", err)
return
}

Expand All @@ -374,8 +377,9 @@ func (ds *DbftService) PrepareRequestReceived(payload *msg.ConsensusPayload,mess
ds.context.Signatures[payload.MinerIndex] = message.Signature
Trace()
if err := ds.AddTransaction(message.BookkeepingTransaction); err != nil {
fmt.Println("PrepareRequestReceived AddTransaction failed",err)
return }
log.Warn("PrepareRequestReceived AddTransaction failed", err)
return
}
Trace()
mempool := ds.localNet.GetMemoryPool()
for _, hash := range ds.context.TransactionHashes[1:] {
Expand Down Expand Up @@ -485,7 +489,7 @@ func (ds *DbftService) Timeout() {
ds.context.Nonce = GetNonce()
transactions := ds.localNet.GetMemoryPool() //TODO: add policy

txBookkeeping := ds.CreateBookkeepingTransaction(transactions, ds.context.Nonce)
txBookkeeping := ds.CreateBookkeepingTransaction(ds.context.Nonce)
transactions[txBookkeeping.Hash()] = txBookkeeping

if ds.context.TransactionHashes == nil {
Expand Down
2 changes: 1 addition & 1 deletion core/ledger/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (bc *Blockchain) SaveBlock(block *Block) error {

func (bc *Blockchain) ContainsTransaction(hash Uint256) bool {
//TODO: implement error catch
tx ,_ := DefaultLedger.Store.GetTransaction(hash)
tx, _ := DefaultLedger.Store.GetTransaction(hash)
if tx != nil{
return true
}
Expand Down
23 changes: 13 additions & 10 deletions core/store/LevelDBStore/LevelDBStore.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"GoOnchain/core/contract/program"
. "GoOnchain/core/asset"
"GoOnchain/common/serialization"
"GoOnchain/common/log"
"bytes"
"fmt"
tx "GoOnchain/core/transaction"
Expand Down Expand Up @@ -313,30 +314,32 @@ func (bd *LevelDBStore) GetNextBlockHash(hash []byte) common.Uint256 {
*/

func (bd *LevelDBStore) GetTransaction(hash Uint256) (*tx.Transaction, error) {

fmt.Printf( "GetTransaction Hash: %x\n", hash )

Trace()
log.Debug(fmt.Sprintf("GetTransaction Hash: %x\n", hash))
t := new(tx.Transaction)
err := bd.getTx( t, hash )
err := bd.getTx(t, hash)

if err != nil {
return nil, err
}

return t,err
return t, err
}

func (bd *LevelDBStore) getTx(tx *tx.Transaction, hash Uint256) error {
fmt.Printf( "getTx Hash: %x\n", hash )

prefix := []byte{ byte(DATA_Transaction) }
tHash,err_get := bd.Get( append(prefix,hash.ToArray()...) )
fmt.Printf( "getTx Data: %x\n", tHash )
tHash, err_get := bd.Get( append(prefix,hash.ToArray()...) )
fmt.Printf("getTx Data: %x\n", tHash)
if ( err_get != nil ) {
//TODO: implement error process
log.Warn("Get TX from DB error")
return err_get
}

r := bytes.NewReader(tHash)

// get height
height,err := serialization.ReadUint32(r)
height, err := serialization.ReadUint32(r)
fmt.Printf( "tx height: %d\n", height )

// Deserialize Transaction
Expand Down
15 changes: 11 additions & 4 deletions core/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@ func (tx *Transaction) SerializeUnsigned(w io.Writer) error {
}
tx.Payload.Serialize(w)
//nonce
//serialization.WriteVarUint(w, tx.Nonce)
serialization.WriteVarUint(w, tx.Nonce)
//[]*txAttribute
err := serialization.WriteVarUint(w, uint64(len(tx.Attributes)))
if err != nil {
return NewDetailErr(err, ErrNoCode, "Transaction item txAttribute length serialization failed.")
}
if len(tx.Attributes)>0 {
if len(tx.Attributes) > 0 {
for _, attr := range tx.Attributes {
attr.Serialize(w)
}
Expand All @@ -115,7 +115,7 @@ func (tx *Transaction) SerializeUnsigned(w io.Writer) error {
if err != nil {
return NewDetailErr(err, ErrNoCode, "Transaction item UTXOInputs length serialization failed.")
}
if len(tx.UTXOInputs)>0 {
if len(tx.UTXOInputs) > 0 {
for _, utxo := range tx.UTXOInputs {
utxo.Serialize(w)
}
Expand All @@ -135,7 +135,7 @@ func (tx *Transaction) SerializeUnsigned(w io.Writer) error {
if err != nil {
return NewDetailErr(err, ErrNoCode, "Transaction item Outputs length serialization failed.")
}
if len(tx.Outputs)>0 {
if len(tx.Outputs) > 0 {
for _, output := range tx.Outputs {
output.Serialize(w)
}
Expand Down Expand Up @@ -205,6 +205,13 @@ func (tx *Transaction) DeserializeUnsignedWithoutType(r io.Reader) error {
// tx.Payload.Deserialize(r)
// }
//attributes

nonce, err := serialization.ReadVarUint(r, 0)
if err != nil {
return errors.New("Parse nonce error")
}
tx.Nonce = nonce

Len, err := serialization.ReadVarUint(r, 0)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion net/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func InitNode() Tmper {
}

func rmNode(node *node) {
fmt.Printf("Remove node %s\n", node.addr)
log.Debug(fmt.Sprintf("Remove unused/deuplicate node: 0x%0x", node.id))
}

// TODO pass pointer to method only need modify it
Expand Down

0 comments on commit a4f6aa0

Please sign in to comment.