Skip to content

Commit

Permalink
added chainID in transaction structure (also transaction must be sign…
Browse files Browse the repository at this point in the history
…ed with chain id now)
  • Loading branch information
miiu96 committed Jun 29, 2020
1 parent 18cff0c commit 2f9781a
Show file tree
Hide file tree
Showing 42 changed files with 410 additions and 126 deletions.
22 changes: 12 additions & 10 deletions api/mock/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ import (

// Facade is the mock implementation of a node router handler
type Facade struct {
ShouldErrorStart bool
ShouldErrorStop bool
TpsBenchmarkHandler func() *statistics.TpsBenchmark
GetHeartbeatsHandler func() ([]data.PubKeyHeartbeat, error)
BalanceHandler func(string) (*big.Int, error)
GetAccountHandler func(address string) (state.UserAccountHandler, error)
GenerateTransactionHandler func(sender string, receiver string, value *big.Int, code string) (*transaction.Transaction, error)
GetTransactionHandler func(hash string) (*transaction.ApiTransactionResult, error)
CreateTransactionHandler func(nonce uint64, value string, receiverHex string, senderHex string, gasPrice uint64, gasLimit uint64, data string, signatureHex string) (*transaction.Transaction, []byte, error)
ShouldErrorStart bool
ShouldErrorStop bool
TpsBenchmarkHandler func() *statistics.TpsBenchmark
GetHeartbeatsHandler func() ([]data.PubKeyHeartbeat, error)
BalanceHandler func(string) (*big.Int, error)
GetAccountHandler func(address string) (state.UserAccountHandler, error)
GenerateTransactionHandler func(sender string, receiver string, value *big.Int, code string) (*transaction.Transaction, error)
GetTransactionHandler func(hash string) (*transaction.ApiTransactionResult, error)
CreateTransactionHandler func(nonce uint64, value string, receiverHex string, senderHex string, gasPrice uint64,
gasLimit uint64, data string, signatureHex string, chainID string) (*transaction.Transaction, []byte, error)
ValidateTransactionHandler func(tx *transaction.Transaction) error
SendBulkTransactionsHandler func(txs []*transaction.Transaction) (uint64, error)
ExecuteSCQueryHandler func(query *process.SCQuery) (*vmcommon.VMOutput, error)
Expand Down Expand Up @@ -95,8 +96,9 @@ func (f *Facade) CreateTransaction(
gasLimit uint64,
data string,
signatureHex string,
chainID string,
) (*transaction.Transaction, []byte, error) {
return f.CreateTransactionHandler(nonce, value, receiverHex, senderHex, gasPrice, gasLimit, data, signatureHex)
return f.CreateTransactionHandler(nonce, value, receiverHex, senderHex, gasPrice, gasLimit, data, signatureHex, chainID)
}

// GetTransaction is the mock implementation of a handler's GetTransaction method
Expand Down
6 changes: 5 additions & 1 deletion api/transaction/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// TxService interface defines methods that can be used from `elrondFacade` context variable
type TxService interface {
CreateTransaction(nonce uint64, value string, receiver string, sender string, gasPrice uint64,
gasLimit uint64, data string, signatureHex string) (*transaction.Transaction, []byte, error)
gasLimit uint64, data string, signatureHex string, chainID string) (*transaction.Transaction, []byte, error)
ValidateTransaction(tx *transaction.Transaction) error
SendBulkTransactions([]*transaction.Transaction) (uint64, error)
GetTransaction(hash string) (*transaction.ApiTransactionResult, error)
Expand Down Expand Up @@ -49,6 +49,7 @@ type SendTxRequest struct {
GasPrice uint64 `form:"gasPrice" json:"gasPrice"`
GasLimit uint64 `form:"gasLimit" json:"gasLimit"`
Signature string `form:"signature" json:"signature"`
ChainID string `form:"chainID" json:"chainID"`
}

//TxResponse represents the structure on which the response will be validated against
Expand Down Expand Up @@ -93,6 +94,7 @@ func SendTransaction(c *gin.Context) {
gtx.GasLimit,
gtx.Data,
gtx.Signature,
gtx.ChainID,
)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("%s: %s", errors.ErrTxGenerationFailed.Error(), err.Error())})
Expand Down Expand Up @@ -147,6 +149,7 @@ func SendMultipleTransactions(c *gin.Context) {
receivedTx.GasLimit,
receivedTx.Data,
receivedTx.Signature,
receivedTx.ChainID,
)
if err != nil {
continue
Expand Down Expand Up @@ -222,6 +225,7 @@ func ComputeTransactionGasLimit(c *gin.Context) {
gtx.GasLimit,
gtx.Data,
gtx.Signature,
gtx.ChainID,
)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("%s: %s", errors.ErrValidation.Error(), err.Error())})
Expand Down
14 changes: 8 additions & 6 deletions api/transaction/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ func TestSendTransaction_ErrorWhenFacadeSendTransactionError(t *testing.T) {
errorString := "send transaction error"

facade := mock.Facade{
CreateTransactionHandler: func(nonce uint64, value string, receiverHex string, senderHex string, gasPrice uint64, gasLimit uint64, data string, signatureHex string) (t *tr.Transaction, i []byte, err error) {
CreateTransactionHandler: func(_ uint64, _ string, _ string, _ string, _ uint64, _ uint64, _ string, _ string, _ string,
) (*tr.Transaction, []byte, error) {
return nil, nil, nil
},
SendBulkTransactionsHandler: func(txs []*tr.Transaction) (u uint64, err error) {
Expand Down Expand Up @@ -227,8 +228,8 @@ func TestSendTransaction_ReturnsSuccessfully(t *testing.T) {
hexTxHash := "deadbeef"

facade := mock.Facade{
CreateTransactionHandler: func(nonce uint64, value string, receiverHex string, senderHex string, gasPrice uint64,
gasLimit uint64, data string, signatureHex string) (t *tr.Transaction, i []byte, err error) {
CreateTransactionHandler: func(_ uint64, _ string, _ string, _ string, _ uint64, _ uint64, _ string, _ string, _ string,
) (*tr.Transaction, []byte, error) {
txHash, _ := hex.DecodeString(hexTxHash)
return nil, txHash, nil
},
Expand Down Expand Up @@ -306,8 +307,8 @@ func TestSendMultipleTransactions_OkPayloadShouldWork(t *testing.T) {
sendBulkTxsWasCalled := false

facade := mock.Facade{
CreateTransactionHandler: func(nonce uint64, value string, receiverHex string, senderHex string, gasPrice uint64,
gasLimit uint64, data string, signatureHex string) (*tr.Transaction, []byte, error) {
CreateTransactionHandler: func(_ uint64, _ string, _ string, _ string, _ uint64, _ uint64, _ string, _ string, _ string,
) (*tr.Transaction, []byte, error) {
createTxWasCalled = true
return &tr.Transaction{}, make([]byte, 0), nil
},
Expand Down Expand Up @@ -356,7 +357,8 @@ func TestComputeTransactionGasLimit(t *testing.T) {
expectedGasLimit := uint64(37)

facade := mock.Facade{
CreateTransactionHandler: func(_ uint64, _ string, _ string, _ string, _ uint64, _ uint64, _ string, _ string) (*tr.Transaction, []byte, error) {
CreateTransactionHandler: func(_ uint64, _ string, _ string, _ string, _ uint64, _ uint64, _ string, _ string, _ string,
) (*tr.Transaction, []byte, error) {
return &tr.Transaction{}, nil, nil
},
ComputeTransactionGasLimitHandler: func(tx *tr.Transaction) (uint64, error) {
Expand Down
2 changes: 2 additions & 0 deletions cmd/node/factory/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ func newShardInterceptorContainerFactory(
WhiteListerVerifiedTxs: whiteListerVerifiedTxs,
AntifloodHandler: network.InputAntifloodHandler,
NonceConverter: dataCore.Uint64ByteSliceConverter,
ChainID: dataCore.ChainID,
}
interceptorContainerFactory, err := interceptorscontainer.NewShardInterceptorsContainerFactory(shardInterceptorsContainerFactoryArgs)
if err != nil {
Expand Down Expand Up @@ -821,6 +822,7 @@ func newMetaInterceptorContainerFactory(
WhiteListerVerifiedTxs: whiteListerVerifiedTxs,
AntifloodHandler: network.InputAntifloodHandler,
NonceConverter: dataCore.Uint64ByteSliceConverter,
ChainID: dataCore.ChainID,
}
interceptorContainerFactory, err := interceptorscontainer.NewMetaInterceptorsContainerFactory(metaInterceptorsContainerFactoryArgs)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions cmd/node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1959,6 +1959,7 @@ func createHardForkTrigger(
InputAntifloodHandler: network.InputAntifloodHandler,
OutputAntifloodHandler: network.OutputAntifloodHandler,
ValidityAttester: process.BlockTracker,
ChainID: coreData.ChainID,
}
hardForkExportFactory, err := exportFactory.NewExportHandlerFactory(argsExporter)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion data/transaction/proto/transaction.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ message Transaction {
uint64 GasPrice = 7 [(gogoproto.jsontag) = "gasPrice,omitempty"];
uint64 GasLimit = 8 [(gogoproto.jsontag) = "gasLimit,omitempty"];
bytes Data = 9 [(gogoproto.jsontag) = "data,omitempty"];
bytes Signature = 10 [(gogoproto.jsontag) = "signature,omitempty"];
bytes ChainID = 10 [(gogoproto.jsontag) = "chainID"];
bytes Signature = 11 [(gogoproto.jsontag) = "signature,omitempty"];
}
2 changes: 2 additions & 0 deletions data/transaction/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type frontendTransaction struct {
GasLimit uint64 `json:"gasLimit"`
Data string `json:"data,omitempty"`
Signature string `json:"signature,omitempty"`
ChainID string `json:"chainID"`
}

// GetDataForSigning returns the serialized transaction having an empty signature field
Expand All @@ -88,6 +89,7 @@ func (tx *Transaction) GetDataForSigning(encoder Encoder, marshalizer Marshalize
SenderUsername: tx.SndUserName,
ReceiverUsername: tx.RcvUserName,
Data: string(tx.Data),
ChainID: string(tx.ChainID),
}

return marshalizer.Marshal(ftx)
Expand Down
123 changes: 91 additions & 32 deletions data/transaction/transaction.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dataRetriever/txpool/memorytests/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ func TestShardedTxPool_MemoryFootprint(t *testing.T) {
journals = append(journals, runScenario(t, newScenario(100, 1, core.MegabyteSize, "1_0"), memoryAssertion{90, 100}, memoryAssertion{0, 1}))
journals = append(journals, runScenario(t, newScenario(10000, 1, 10240, "1_0"), memoryAssertion{96, 128}, memoryAssertion{0, 4}))
journals = append(journals, runScenario(t, newScenario(10, 10000, 1000, "1_0"), memoryAssertion{96, 128}, memoryAssertion{16, 24}))
journals = append(journals, runScenario(t, newScenario(150000, 1, 128, "1_0"), memoryAssertion{50, 60}, memoryAssertion{30, 36}))
journals = append(journals, runScenario(t, newScenario(1, 150000, 128, "1_0"), memoryAssertion{50, 60}, memoryAssertion{30, 36}))
journals = append(journals, runScenario(t, newScenario(150000, 1, 128, "1_0"), memoryAssertion{50, 65}, memoryAssertion{30, 36}))
journals = append(journals, runScenario(t, newScenario(1, 150000, 128, "1_0"), memoryAssertion{50, 65}, memoryAssertion{30, 36}))

for _, journal := range journals {
journal.displayFootprintsSummary()
Expand Down
Loading

0 comments on commit 2f9781a

Please sign in to comment.