-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/hash #5
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
Open
harshu4
wants to merge
14
commits into
dev
Choose a base branch
from
feature/hash
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feature/hash #5
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3b0c644
added hash
harshu4 9e6183f
solved bug
harshu4 d50ce4d
solved sort
harshu4 4e95bc8
added logs
harshu4 46e9234
more print
harshu4 e5a00ce
added code to remove previous summary transaction in prepare proposal
harshu4 a960e4b
added debug to check number of tx in mempool
harshu4 fa7b4e1
changed the timeout commit to 1s
harshu4 95a19ed
changed the logic to add verificationtx block number
harshu4 e8bc071
xoring the transaction removed sorting more efficient
harshu4 1a034c7
added checking the block before pushing transactions again and again
harshu4 738363b
added checking the block before pushing transactions again and again
harshu4 3153014
added wait for the nft verifier to sync, via channels, added a new tr…
harshu4 ee45768
changed directory structure to match dev
harshu4 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
) | ||
|
||
|
@@ -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 | ||
|
@@ -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") | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
|
@@ -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} | ||
|
@@ -271,7 +446,7 @@ func (app *VerificationApp) FinalizeBlock(_ context.Context, req *abcitypes.Requ | |
// } | ||
// } | ||
// } | ||
|
||
app.Currblockno = req.Height | ||
return &abcitypes.ResponseFinalizeBlock{ | ||
TxResults: txs, | ||
ValidatorUpdates: validatorupdates, | ||
|
@@ -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() | ||
|
@@ -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 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?