From b48b6a1c011dd4c3389f3eda781fb1a05c0068ec Mon Sep 17 00:00:00 2001 From: Alexey Kiselev Date: Wed, 23 Jun 2021 16:35:22 +0300 Subject: [PATCH 1/3] No basic price check for Exchange transaction versions above 2 --- pkg/proto/transactions_test.go | 24 ++++++++++++++++++++++++ pkg/proto/transactions_with_proofs.go | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/pkg/proto/transactions_test.go b/pkg/proto/transactions_test.go index a2cd645c8f..df783bfb25 100644 --- a/pkg/proto/transactions_test.go +++ b/pkg/proto/transactions_test.go @@ -2377,6 +2377,17 @@ func newSignedOrderV1(t *testing.T, sender, matcher crypto.PublicKey, amountAsse return *o } +func newSignedOrderV4(t *testing.T, sender, matcher crypto.PublicKey, amountAsset, priceAsset OptionalAsset, ot OrderType, price, amount, ts, exp, fee uint64, sID, sSig string) OrderV4 { + id, err := crypto.NewDigestFromBase58(sID) + require.NoError(t, err) + sig, err := crypto.NewSignatureFromBase58(sSig) + require.NoError(t, err) + o := NewUnsignedOrderV4(sender, matcher, amountAsset, priceAsset, ot, price, amount, ts, exp, fee, OptionalAsset{}) + o.ID = &id + o.Proofs = NewProofsFromSignature(&sig) + return *o +} + func TestExchangeWithSigFromMainNet(t *testing.T) { tests := []struct { matcher string @@ -2720,6 +2731,19 @@ func TestExchangeWithProofsValidations(t *testing.T) { } } +func TestExchangeV3PriceValidation(t *testing.T) { + buySender, _ := crypto.NewPublicKeyFromBase58("HFw9wjsuaeZ6w8HqJeyxz9RUXJN8krLpgjwtPrqyicr") + sellSender, _ := crypto.NewPublicKeyFromBase58("BvJEWY79uQEFetuyiZAF5U4yjPioMj9J6ZrF9uTNfe3E") + mpk, _ := crypto.NewPublicKeyFromBase58("BvJEWY79uQEFetuyiZAF5U4yjPioMj9J6ZrF9uTNfe3E") + aa, _ := NewOptionalAssetFromString("3JmaWyFqWo8YSA8x3DXCBUW7veesxacvKx19dMv7wTMg") + pa, _ := NewOptionalAssetFromString("25FEqEjRkqK6yCkiT7Lz6SAYz7gUFCtxfCChnrVFD5AT") + sbo := newSignedOrderV4(t, buySender, mpk, *aa, *pa, Buy, 1000000, 800000000, 1624445095222, 1626950695222, 300000, "3fdNTCQ7o2TvN8eDV3m7J9aSLxcUitwN2SMZpn1irSXX", "3aKUz8boZingH8r18grL8Rst5RyGVnESaQtuEoV5piUnvJKNf67xFwFpPpmfiuAuud1AAzj94xYNw1MKkmJaBicR") + sso := newSignedOrderV4(t, sellSender, mpk, *aa, *pa, Sell, 1000000, 800000000, 1624445095267, 1626950695267, 300000, "81Xc8YP1Ev2bqvSLgN5k3ent6Fr7rnEdCg8x2DH5twqX", "4VQmM6QB8yaQ1AChNNkVH5EvVKenS8YG7YqXK9SsjWAnjJm5xvd48kW2akwcEbhgzqqGMDtS2AmeGSfpEcHEMYGU") + tx := NewUnsignedExchangeWithProofs(3, &sbo, &sso, 100000000, 800000000, 100, 100, 300000, 1624445095293) + _, err := tx.Validate() + assert.NoError(t, err) +} + func TestExchangeWithProofsFromTestNet(t *testing.T) { tests := []struct { matcher string diff --git a/pkg/proto/transactions_with_proofs.go b/pkg/proto/transactions_with_proofs.go index ed2d98ff13..8bface6f26 100644 --- a/pkg/proto/transactions_with_proofs.go +++ b/pkg/proto/transactions_with_proofs.go @@ -1441,7 +1441,7 @@ func (tx *ExchangeWithProofs) Validate() (Transaction, error) { if err != nil { return tx, err } - if tx.Price > bo.GetPrice() || tx.Price < so.GetPrice() { + if tx.Version < 3 && tx.Price > bo.GetPrice() || tx.Price < so.GetPrice() { if tx.Price > bo.GetPrice() { return tx, errors.Errorf("invalid price: tx.Price %d > bo.GetPrice() %d", tx.Price, bo.GetPrice()) } From 9398b3ba844c8134ea7c07c46c425f53c8031470 Mon Sep 17 00:00:00 2001 From: Alexey Kiselev Date: Wed, 23 Jun 2021 19:51:29 +0300 Subject: [PATCH 2/3] Constant added to track topmost Ride version. --- pkg/proto/transactions_with_proofs.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pkg/proto/transactions_with_proofs.go b/pkg/proto/transactions_with_proofs.go index ed2d98ff13..239c1d02a0 100644 --- a/pkg/proto/transactions_with_proofs.go +++ b/pkg/proto/transactions_with_proofs.go @@ -51,6 +51,8 @@ const ( maxArguments = 22 maxFunctionNameBytes = 255 maxInvokeScriptWithProofsBytes = 5 * 1024 + + topRideVersion = 5 ) // IssueWithProofs is a transaction to issue new asset, second version. @@ -186,7 +188,7 @@ func validContentType(t byte) bool { // version in range [0, 5) func validStdVersion(v byte) bool { - return v < 5 + return v <= topRideVersion } func (tx *IssueWithProofs) Validate() (Transaction, error) { @@ -209,7 +211,7 @@ func (tx *IssueWithProofs) Validate() (Transaction, error) { if !validContentType(tx.Script[1]) { return tx, errors.Errorf("Invalid content type of script: %d", tx.Script[1]) } - if tx.Script[2] > 4 { // 4 is current max script version + if tx.Script[2] > topRideVersion { return tx, errors.Errorf("Invalid version of script: %d", tx.Script[2]) } @@ -3152,7 +3154,7 @@ func (tx *DataWithProofs) bodyUnmarshalBinary(data []byte) error { if err != nil { return errors.Errorf("failed to extract type of data entry") } - switch DataValueType(t) { + switch t { case DataInteger: var ie IntegerDataEntry err = ie.UnmarshalBinary(data) @@ -4281,7 +4283,7 @@ func (tx *InvokeScriptWithProofs) Clone() *InvokeScriptWithProofs { return out } -//NewUnsignedSetAssetScriptWithProofs creates new unsigned SetAssetScriptWithProofs transaction. +//NewUnsignedInvokeScriptWithProofs creates new unsigned InvokeScriptWithProofs transaction. func NewUnsignedInvokeScriptWithProofs(v, chain byte, senderPK crypto.PublicKey, scriptRecipient Recipient, call FunctionCall, payments ScriptPayments, feeAsset OptionalAsset, fee, timestamp uint64) *InvokeScriptWithProofs { return &InvokeScriptWithProofs{ Type: InvokeScriptTransaction, @@ -4756,7 +4758,7 @@ func (tx *UpdateAssetInfoWithProofs) MarshalBinary() ([]byte, error) { return nil, errors.New("binary format is not defined for UpdateAssetInfoTransaction") } -func (tx *UpdateAssetInfoWithProofs) UnmarshalBinary(data []byte, scheme Scheme) error { +func (tx *UpdateAssetInfoWithProofs) UnmarshalBinary(_ []byte, _ Scheme) error { return errors.New("binary format is not defined for UpdateAssetInfoTransaction") } From dae41974aa93b4ed1429946bd4907a7be44a9bda Mon Sep 17 00:00:00 2001 From: Alexey Kiselev Date: Thu, 24 Jun 2021 09:07:12 +0300 Subject: [PATCH 3/3] Script version check function renamed and reused. --- pkg/proto/transactions_with_proofs.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/pkg/proto/transactions_with_proofs.go b/pkg/proto/transactions_with_proofs.go index 3f37c5213b..5836d84fd8 100644 --- a/pkg/proto/transactions_with_proofs.go +++ b/pkg/proto/transactions_with_proofs.go @@ -186,8 +186,7 @@ func validContentType(t byte) bool { return t >= 1 && t <= 3 } -// version in range [0, 5) -func validStdVersion(v byte) bool { +func validScriptVersion(v byte) bool { return v <= topRideVersion } @@ -200,10 +199,9 @@ func (tx *IssueWithProofs) Validate() (Transaction, error) { return tx, err } if tx.NonEmptyScript() { - if !validStdVersion(tx.Script[0]) { + if !validScriptVersion(tx.Script[0]) { return tx, errors.Errorf("Invalid version of script: %d", tx.Script[0]) } - if tx.Script[0] == 0 { // version byte if len(tx.Script) <= 2 { return tx, errors.Errorf("Illegal length of script: %d", len(tx.Script)) @@ -211,12 +209,10 @@ func (tx *IssueWithProofs) Validate() (Transaction, error) { if !validContentType(tx.Script[1]) { return tx, errors.Errorf("Invalid content type of script: %d", tx.Script[1]) } - if tx.Script[2] > topRideVersion { + if !validScriptVersion(tx.Script[2]) { return tx, errors.Errorf("Invalid version of script: %d", tx.Script[2]) } - } - if !tx.Script.IsValidChecksum() { return tx, errors.Errorf("Invalid checksum: %+v", []byte(tx.Script)) }