Skip to content
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
207 changes: 203 additions & 4 deletions internal/bft/abci/abci.go → bft/abci/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ package verificationApp
import (
"context"
"encoding/base64"
"fmt"
"sort"

validatorpass_tracker "github.com/Openmesh-Network/nft-authorise/tracker"
abcitypes "github.com/cometbft/cometbft/abci/types"
nm "github.com/cometbft/cometbft/node"
comettype "github.com/cometbft/cometbft/types"
"github.com/dgraph-io/badger/v3"
help "github.com/openmesh-network/core/bft/helper"
"google.golang.org/protobuf/proto"

// "math/rand"
"crypto/sha256"

crypt "github.com/cometbft/cometbft/proto/tendermint/crypto"
"github.com/openmesh-network/core/bft/types"
"github.com/openmesh-network/core/collector"
"github.com/openmesh-network/core/internal/bft/types"
log "github.com/openmesh-network/core/internal/logger"
)

Expand All @@ -22,6 +30,11 @@ type VerificationApp struct {
assignedRequests []collector.Request
validatorPriorities [][]collector.Request
validatorFreeThisRound []bool
Node *nm.Node
CurrentMempool []comettype.Tx
Currblockno int64
PolygonCheckpoint uint64
Tracker *validatorpass_tracker.Tracker
}

const VALIDATOR_PREALLOCATED_COUNT = 2000
Expand All @@ -41,12 +54,166 @@ func (app *VerificationApp) PrepareProposal(_ context.Context, proposal *abcityp

// Will currently accept all transactions.

var result [][]byte
var othertx = [][]byte{}

log.Error("Sorting Done")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really an error or more like a debug comment?

for _, slice := range proposal.Txs {
var transaction types.Transaction
err := proto.Unmarshal(slice, &transaction)
if err != nil {
log.Error("Error unmarshaling transaction data:", err)
}

switch transaction.Type {
case types.TransactionType_VerificationTransaction:
result = append(result, slice)

case types.TransactionType_SummaryTransaction:
log.Debug("We are removing this TX")
default:
othertx = append(othertx, slice)
}

}

var xoredTx = help.XorArrays(result)

log.Debug("Merging Done")

hash := sha256.Sum256(xoredTx)
hashString := base64.StdEncoding.EncodeToString(hash[:])
transactionMessage := types.Transaction{
Owner: "trial",
Signature: "",
Type: *types.TransactionType_SummaryTransaction.Enum(),
}
transactionMessage.Data = &types.Transaction_SummaryTransactionData{
SummaryTransactionData: &types.SummaryTransactionData{
Hash: hashString,
NumTx: int64(comettype.ToTxs(proposal.Txs).Len()),
},
}
transactionBytes, err := proto.Marshal(&transactionMessage)
if err != nil {
panic(err)
}
log.Debug("Marshaling Done")
transactions := comettype.Tx(transactionBytes[:])

transactionMessage_checkpoint := types.Transaction{
Owner: "trial",
Signature: "",
Type: *types.TransactionType_PolygonCheckpointTransaction.Enum(),
}
transactionMessage_checkpoint.Data = &types.Transaction_PolygonCheckpointTransactionData{
PolygonCheckpointTransactionData: &types.PolygonCheckpointTransactionData{
Blockno: uint64(app.Tracker.LastTrackerHeight),
Blockhash: "xyz", //not a necessary field just nice to have for record keeping
},
}
transactionBytes_Checkpoint, err := proto.Marshal(&transactionMessage_checkpoint)
if err != nil {
panic(err)
}

transactions_checkpoint := comettype.Tx(transactionBytes_Checkpoint[:])
proposal.Txs = append(othertx, transactions, transactions_checkpoint)
log.Debug(proposal.Txs)
return &abcitypes.ResponsePrepareProposal{Txs: proposal.Txs}, nil
}
func (app *VerificationApp) ProcessProposal(_ context.Context, proposal *abcitypes.RequestProcessProposal) (*abcitypes.ResponseProcessProposal, error) {

// Supposedly it's bad for performance to reject crappy blocks.
// I think we should be a strict as possible, and give death penalty to misbehaving nodes basically.
total_tx := app.Node.Mempool().ReapMaxTxs(-1)
log.Debug("The size for the node is")
log.Debug(app.Node.Mempool().Size())
app.CurrentMempool = total_tx
if len(app.CurrentMempool) > 2 {
sort.Slice(app.CurrentMempool, func(i, j int) bool {
return string(app.CurrentMempool[i]) < string(app.CurrentMempool[j])
})
}
log.Debug("Sorting Done")
var result [][]byte
var othertx = [][]byte{}
for _, slice := range app.CurrentMempool {
var transaction types.Transaction
err := proto.Unmarshal(slice, &transaction)
if err != nil {
log.Error("Error unmarshaling transaction data:", err)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you really continue with the proposal if there is an error here? Shouldn't you return an error?

}
switch transaction.Type {
case types.TransactionType_VerificationTransaction:
result = append(result, slice)

default:
othertx = append(othertx, slice)
}

}

var xoredTx = help.XorArrays(result)

log.Debug("Merging Done")

hash := sha256.Sum256(xoredTx)
hashString := base64.StdEncoding.EncodeToString(hash[:])

for _, tx := range proposal.Txs {

if code := app.isValid(tx); code != 0 {
// log.Error("Error: invalid transaction index %v", i)
log.Error("A transaction got error")
return &abcitypes.ResponseProcessProposal{Status: abcitypes.ResponseProcessProposal_REJECT}, nil
} else {
var transaction types.Transaction
err := proto.Unmarshal(tx, &transaction)
if err != nil {
log.Error("Error unmarshaling transaction data:", err)
return &abcitypes.ResponseProcessProposal{Status: abcitypes.ResponseProcessProposal_REJECT}, nil
}
log.Debug(transaction.Type)
switch transaction.Type {

case types.TransactionType_SummaryTransaction:

summaryData := &types.SummaryTransactionData{}
summaryData = transaction.GetSummaryTransactionData()
// log.Debug("Resource Transaction Data:", transaction)
if err != nil {
log.Error("cannot decode them")
return &abcitypes.ResponseProcessProposal{Status: abcitypes.ResponseProcessProposal_REJECT}, nil
}
if hashString != summaryData.GetHash() {
log.Error("they are different")
return &abcitypes.ResponseProcessProposal{Status: abcitypes.ResponseProcessProposal_REJECT}, nil
}
log.Debug("they are similar")

case types.TransactionType_PolygonCheckpointTransaction:
log.Debug("polygon tx found")
checkpointData := &types.PolygonCheckpointTransactionData{}
checkpointData = transaction.GetPolygonCheckpointTransactionData()

if err != nil {
log.Error("Cannot decode tx")
return &abcitypes.ResponseProcessProposal{Status: abcitypes.ResponseProcessProposal_REJECT}, nil
}

if checkpointData.Blockno > uint64(app.Tracker.LastTrackerHeight) {
return &abcitypes.ResponseProcessProposal{Status: abcitypes.ResponseProcessProposal_REJECT}, nil
}
log.Debug("the height is okay,the proposed height is ", checkpointData.Blockno, "the current height is", app.Tracker.LastTrackerHeight)
default:
log.Debug(transaction.Type)
log.Debug("Unknown transaction type")

}
}
}
log.Debug("Signing Done")

return &abcitypes.ResponseProcessProposal{Status: abcitypes.ResponseProcessProposal_ACCEPT}, nil
}
Expand Down Expand Up @@ -157,6 +324,14 @@ func (app *VerificationApp) FinalizeBlock(_ context.Context, req *abcitypes.Requ
log.Debug("Node succesfully registered: ", len(validatorupdates))
// log.Debug("Node Registration Transaction Data:", registrationData)

case types.TransactionType_PolygonCheckpointTransaction:
polygonData := &types.PolygonCheckpointTransactionData{}
polygonData = transaction.GetPolygonCheckpointTransactionData()
app.PolygonCheckpoint = polygonData.Blockno
fmt.Println("The polygon checkpoint is ", app.PolygonCheckpoint)
txs[i] = &abcitypes.ExecTxResult{}
case types.TransactionType_SummaryTransaction:
txs[i] = &abcitypes.ExecTxResult{}
default:
log.Error("Unknown transaction type")
txs[i] = &abcitypes.ExecTxResult{Code: code}
Expand Down Expand Up @@ -271,7 +446,7 @@ func (app *VerificationApp) FinalizeBlock(_ context.Context, req *abcitypes.Requ
// }
// }
// }

app.Currblockno = req.Height
return &abcitypes.ResponseFinalizeBlock{
TxResults: txs,
ValidatorUpdates: validatorupdates,
Expand Down Expand Up @@ -315,16 +490,29 @@ func (app *VerificationApp) isValid(tx []byte) uint32 {

// Check the transaction type and handle accordingly
switch transaction.Type {
case types.TransactionType_SummaryTransaction:
return 0
case types.TransactionType_NormalTransaction:
// normalData := &types.NormalTransactionData{}
// normalData = transaction.GetNormalData()
// log.Info("Normal Transaction Data:", normalData)
return 0
case types.TransactionType_VerificationTransaction:
// verificationData := &types.VerificationTransactionData{}
// verificationData = transaction.GetVerificationData()
verificationData := &types.VerificationTransactionData{}
verificationData = transaction.GetVerificationData()
// log.Info("Verification Transaction Data:", verificationData)
if verificationData == nil {
log.Error("Error unmarshaling verification data", err)
return 1
}
if verificationData.GetHeight() == app.Currblockno {
log.Debug("the transaction will be rejected due to blockheigh", app.Currblockno, verificationData.GetHeight())
return 1

}

return 0

case types.TransactionType_ResourceTransaction:
// resourceData := &types.ResourceTransactionData{}
// resourceData = transaction.GetResourceData()
Expand All @@ -341,6 +529,17 @@ func (app *VerificationApp) isValid(tx []byte) uint32 {
// log.Debug("Resource Transaction Data:", nodeRegistrationData)
return 0

case types.TransactionType_PolygonCheckpointTransaction:
polygonData := &types.PolygonCheckpointTransactionData{}
polygonData = transaction.GetPolygonCheckpointTransactionData()

if polygonData == nil {
log.Error("Error unmarshaling resource transaction data:", err)
return 1
}
// log.Debug("Resource Transaction Data:", nodeRegistrationData)
return 0

default:
log.Error("Unknown transaction type")
return 1
Expand Down
Loading