Skip to content

Commit

Permalink
Merge branch 'master' into save-snapshot-state-hash
Browse files Browse the repository at this point in the history
  • Loading branch information
nickeskov authored Dec 15, 2023
2 parents 62b45a6 + 2bd26f5 commit 09334b7
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 24 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -54,7 +54,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2
uses: github/codeql-action/autobuild@v3

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -68,4 +68,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
uses: github/codeql-action/analyze@v3
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ jobs:
run: make itest

- name: Upload itest logs
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
if: failure()
with:
name: itest_logs
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
# with '-no-fail' we let the report trigger content trigger a failure using the GitHub Security features.
args: "-no-fail -fmt sarif -out gosec.sarif ./..."
- name: Upload SARIF file for GitHub Advanced Security Dashboard
uses: github/codeql-action/upload-sarif@v2
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: gosec.sarif

Expand Down Expand Up @@ -54,7 +54,7 @@ jobs:
fi
EOF
- name: Upload SARIF file for GitHub Advanced Security Dashboard
uses: github/codeql-action/upload-sarif@v2
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: semgrep.sarif

Expand Down
8 changes: 4 additions & 4 deletions pkg/api/metamask/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,14 @@ func (s RPCService) Eth_EstimateGas(req estimateGasRequest) (string, error) {
}
}

txKind, err := proto.GuessEthereumTransactionKind(data)
txKind, err := proto.GuessEthereumTransactionKindType(data)
if err != nil {
return "", errors.Errorf("failed to guess ethereum tx kind, %v", err)
}
switch txKind {
case proto.EthereumTransferWavesKind:
case proto.EthereumTransferWavesKindType:
return uint64ToHexString(proto.MinFee), nil
case proto.EthereumTransferAssetsKind:
case proto.EthereumTransferAssetsKindType:
fee := proto.MinFee
assetID := (*proto.AssetID)(req.To)

Expand All @@ -191,7 +191,7 @@ func (s RPCService) Eth_EstimateGas(req estimateGasRequest) (string, error) {
fee += proto.MinFeeScriptedAsset
}
return uint64ToHexString(uint64(fee)), nil
case proto.EthereumInvokeKind:
case proto.EthereumInvokeKindType:
return uint64ToHexString(proto.MinFeeInvokeScript), nil
default:
return "", errors.Errorf("unexpected ethereum tx kind")
Expand Down
16 changes: 14 additions & 2 deletions pkg/proto/eth_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,21 @@ type EthereumTxData interface {
}

type EthereumTransactionKind interface {
Type() EthereumTransactionKindType
String() string
DecodedData() *ethabi.DecodedCallData
}

type EthereumTransferWavesTxKind struct {
}
type EthereumTransferWavesTxKind struct{}

func NewEthereumTransferWavesTxKind() *EthereumTransferWavesTxKind {
return &EthereumTransferWavesTxKind{}
}

func (k *EthereumTransferWavesTxKind) Type() EthereumTransactionKindType {
return EthereumTransferWavesKindType
}

func (k *EthereumTransferWavesTxKind) DecodedData() *ethabi.DecodedCallData {
return nil
}
Expand All @@ -115,6 +119,10 @@ func NewEthereumTransferAssetsErc20TxKind(decodedData ethabi.DecodedCallData, as
return &EthereumTransferAssetsErc20TxKind{Asset: asset, decodedData: decodedData, Arguments: arguments}
}

func (k *EthereumTransferAssetsErc20TxKind) Type() EthereumTransactionKindType {
return EthereumTransferAssetsKindType
}

func (k *EthereumTransferAssetsErc20TxKind) DecodedData() *ethabi.DecodedCallData {
return &k.decodedData
}
Expand All @@ -131,6 +139,10 @@ func NewEthereumInvokeScriptTxKind(decodedData ethabi.DecodedCallData) *Ethereum
return &EthereumInvokeScriptTxKind{decodedData: decodedData}
}

func (k *EthereumInvokeScriptTxKind) Type() EthereumTransactionKindType {
return EthereumInvokeKindType
}

func (k *EthereumInvokeScriptTxKind) DecodedData() *ethabi.DecodedCallData {
return &k.decodedData
}
Expand Down
24 changes: 13 additions & 11 deletions pkg/proto/eth_tx_kind_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@ import (
"github.com/wavesplatform/gowaves/pkg/ride/ast"
)

type EthereumTransactionKindType byte

const (
EthereumTransferWavesKind = iota + 1
EthereumTransferAssetsKind
EthereumInvokeKind
EthereumTransferWavesKindType EthereumTransactionKindType = iota + 1
EthereumTransferAssetsKindType
EthereumInvokeKindType
)

func GuessEthereumTransactionKind(data []byte) (int64, error) {
func GuessEthereumTransactionKindType(data []byte) (EthereumTransactionKindType, error) {
if len(data) == 0 {
return EthereumTransferWavesKind, nil
return EthereumTransferWavesKindType, nil
}

selectorBytes := data
Expand All @@ -28,10 +30,10 @@ func GuessEthereumTransactionKind(data []byte) (int64, error) {
}

if ethabi.IsERC20TransferSelector(selector) {
return EthereumTransferAssetsKind, nil
return EthereumTransferAssetsKindType, nil
}

return EthereumInvokeKind, nil
return EthereumInvokeKindType, nil
}

type EthereumTransactionKindResolver interface {
Expand All @@ -53,15 +55,15 @@ func NewEthereumTransactionKindResolver(resolver ethKindResolverState, scheme Sc
}

func (e *ethTxKindResolver) ResolveTxKind(ethTx *EthereumTransaction, isBlockRewardDistributionActivated bool) (EthereumTransactionKind, error) {
txKind, err := GuessEthereumTransactionKind(ethTx.Data())
txKind, err := GuessEthereumTransactionKindType(ethTx.Data())
if err != nil {
return nil, errors.Wrap(err, "failed to guess ethereum tx kind")
}

switch txKind {
case EthereumTransferWavesKind:
case EthereumTransferWavesKindType:
return NewEthereumTransferWavesTxKind(), nil
case EthereumTransferAssetsKind:
case EthereumTransferAssetsKindType:
db := ethabi.NewErc20MethodsMap()
decodedData, err := db.ParseCallDataRide(ethTx.Data(), isBlockRewardDistributionActivated)
if err != nil {
Expand All @@ -81,7 +83,7 @@ func (e *ethTxKindResolver) ResolveTxKind(ethTx *EthereumTransaction, isBlockRew
return nil, errors.Wrap(err, "failed to get erc20 arguments from decoded data")
}
return NewEthereumTransferAssetsErc20TxKind(*decodedData, *NewOptionalAssetFromDigest(assetInfo.ID), erc20Arguments), nil
case EthereumInvokeKind:
case EthereumInvokeKindType:
scriptAddr, err := ethTx.WavesAddressTo(e.scheme)
if err != nil {
return nil, err
Expand Down
31 changes: 31 additions & 0 deletions pkg/ride/functions_proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,37 @@ func transferByID(env environment, args ...rideType) (rideType, error) {
}
return nil, errors.Wrap(err, "transferByID")
}
switch t := tx.GetTypeInfo().Type; t {
case proto.TransferTransaction:
// ok, it's transfer tx
case proto.EthereumMetamaskTransaction:
ethTx, ok := tx.(*proto.EthereumTransaction)
if !ok {
return nil, errors.Errorf("transferByID: expected ethereum transaction, got (%T)", tx)
}
kindType, ktErr := proto.GuessEthereumTransactionKindType(ethTx.Data())
if ktErr != nil {
return nil, errors.Wrap(err, "transferByID: failed to guess ethereum transaction kind type")
}
switch kindType {
case proto.EthereumTransferWavesKindType, proto.EthereumTransferAssetsKindType:
// ok, it's an ethereum transfer tx (waves or asset)
case proto.EthereumInvokeKindType:
return rideUnit{}, nil // it's not an ethereum transfer tx
default:
return rideUnit{}, errors.Errorf("transferByID: unreachable point reached in eth kind type switch")
}
case proto.GenesisTransaction, proto.PaymentTransaction, proto.IssueTransaction,
proto.ReissueTransaction, proto.BurnTransaction, proto.ExchangeTransaction,
proto.LeaseTransaction, proto.LeaseCancelTransaction, proto.CreateAliasTransaction,
proto.MassTransferTransaction, proto.DataTransaction, proto.SetScriptTransaction,
proto.SponsorshipTransaction, proto.SetAssetScriptTransaction, proto.InvokeScriptTransaction,
proto.UpdateAssetInfoTransaction, proto.InvokeExpressionTransaction:
// it's not a transfer transaction
return rideUnit{}, nil
default:
return rideUnit{}, errors.Errorf("transferByID: unreachable point reached in tx type switch")
}
obj, err := transactionToObject(env, tx)
if err != nil {
return nil, errors.Wrap(err, "transferByID")
Expand Down
123 changes: 122 additions & 1 deletion pkg/ride/functions_proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"bytes"
"encoding/base64"
"encoding/hex"
"math/big"
"strconv"
"strings"
"testing"
"time"

Expand All @@ -15,8 +18,11 @@ import (
"github.com/wavesplatform/gowaves/pkg/crypto"
"github.com/wavesplatform/gowaves/pkg/keyvalue"
"github.com/wavesplatform/gowaves/pkg/proto"
"github.com/wavesplatform/gowaves/pkg/proto/ethabi"
"github.com/wavesplatform/gowaves/pkg/ride/ast"
"github.com/wavesplatform/gowaves/pkg/ride/meta"
"github.com/wavesplatform/gowaves/pkg/types"
"github.com/wavesplatform/gowaves/pkg/util/byte_helpers"
)

var (
Expand Down Expand Up @@ -763,8 +769,123 @@ func TestBlockInfoByHeight(t *testing.T) {
}
}

func getPtr[T any](t T) *T { return &t }

func TestTransferByID(t *testing.T) {
t.SkipNow()
dApp1 := newTestAccount(t, "DAPP1") // 3MzDtgL5yw73C2xVLnLJCrT5gCL4357a4sz
sender := newTestAccount(t, "SENDER") // 3N8CkZAyS4XcDoJTJoKNuNk2xmNKmQj7myW
txID, err := crypto.NewDigestFromBase58("GemGCop1arCvTY447FLH8tDQF7knvzNCocNTHqKQBus9")
require.NoError(t, err)
assetID := txID
stubEthPK := new(proto.EthereumPublicKey)
ethTo := getPtr(proto.EthereumAddress(assetID[:proto.EthereumAddressSize]))

erc20HexData := "0xa9059cbb0000000000000000000000009a1989946ae4249aac19ac7a038d24aab03c3d8c00000000000000000000000000000000000000000000000000001cc92ad60000" //nolint:lll
erc20Data, err := hex.DecodeString(strings.TrimPrefix(erc20HexData, "0x"))
require.NoError(t, err)
callData, err := ethabi.NewErc20MethodsMap().ParseCallDataRide(erc20Data, true)
require.NoError(t, err)

rideFunctionMeta := meta.Function{
Name: "call",
Arguments: []meta.Type{meta.String},
}
callHexData := "0x3e08c22800000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000573616664730000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" //nolint:lll
invokeData, err := hex.DecodeString(strings.TrimPrefix(callHexData, "0x"))
require.NoError(t, err)
mm, err := ethabi.NewMethodsMapFromRideDAppMeta(meta.DApp{Functions: []meta.Function{rideFunctionMeta}})
require.NoError(t, err)
invokeCallData, err := mm.ParseCallDataRide(invokeData, true)
require.NoError(t, err)

testCases := []struct {
tx proto.Transaction
unit bool
}{
{
tx: byte_helpers.TransferWithProofs.Transaction.Clone(),
unit: false,
},
{
tx: byte_helpers.TransferWithSig.Transaction.Clone(),
unit: false,
},
{
tx: getPtr(proto.NewEthereumTransaction(
&proto.EthereumLegacyTx{To: ethTo, Value: big.NewInt(100500)},
proto.NewEthereumTransferWavesTxKind(),
&txID,
stubEthPK,
0,
)),
unit: false,
},
{
tx: getPtr(proto.NewEthereumTransaction(
&proto.EthereumLegacyTx{
To: ethTo,
Data: erc20Data,
},
proto.NewEthereumTransferAssetsErc20TxKind(
*callData,
proto.NewOptionalAsset(true, assetID),
ethabi.ERC20TransferArguments{Recipient: sender.address().ID(), Amount: 100500},
),
&txID,
stubEthPK,
0,
)),
unit: false,
},
{
tx: getPtr(proto.NewEthereumTransaction(
&proto.EthereumLegacyTx{
To: ethTo,
Data: invokeData,
},
proto.NewEthereumInvokeScriptTxKind(*invokeCallData),
&txID,
stubEthPK,
0,
)),
unit: true,
},
{
tx: byte_helpers.InvokeScriptWithProofs.Transaction.Clone(),
unit: true,
},
}

for i, testCase := range testCases {
t.Run(strconv.Itoa(i+1), func(t *testing.T) {
env := newTestEnv(t).withLibVersion(ast.LibV5).withComplexityLimit(ast.LibV5, 26000).
withBlockV5Activated().withProtobufTx().
withDataEntriesSizeV2().withMessageLengthV3().
withValidateInternalPayments().withThis(dApp1).
withDApp(dApp1).withSender(sender).
withInvocation("call").
withWavesBalance(dApp1, 1_00000000).withWavesBalance(sender, 1_00000000).
withTransaction(testCase.tx).
withAsset(&proto.FullAssetInfo{
AssetInfo: proto.AssetInfo{
AssetConstInfo: proto.AssetConstInfo{
ID: txID,
},
},
}).
withWrappedState()

txIDBytes := txID.Bytes()
res, tErr := transferByID(env.me, rideByteVector(txIDBytes))
assert.NoError(t, tErr)
assert.NotNil(t, res)
if testCase.unit {
assert.Equal(t, rideUnit{}, res)
} else {
assert.NotEqual(t, rideUnit{}, res)
}
})
}
}

func TestAddressToString(t *testing.T) {
Expand Down

0 comments on commit 09334b7

Please sign in to comment.