Skip to content

Commit

Permalink
match gsn solidity version (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimalinux committed Apr 26, 2023
1 parent 373c1f1 commit 009811c
Show file tree
Hide file tree
Showing 38 changed files with 744 additions and 749 deletions.
10 changes: 6 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/bin/
/contracts/abi
/contracts/bin
eth.key
/bin
/eth.key
/examples/mock_recipient/contracts/bin
/impls/gsnforwarder/contracts/abi
/impls/gsnforwarder/contracts/bin
/impls/mforwarder/contracts/bin
23 changes: 19 additions & 4 deletions .golangci.yaml → .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,22 @@ linters-settings:
govet:
# report about shadowed variables
check-shadowing: true
enable-all: true
disable:
- fieldalignment

revive:
rules:
- name: package-comments
disabled: true

goimports:
local-prefixes: github.com/athanorlabs/go-relayer

maligned:
# print struct with more effective memory layout or not, false by default
suggest-new: true

dupl:
# tokens count to trigger issue, 150 by default
threshold: 100
Expand All @@ -23,12 +35,12 @@ linters-settings:
# Correct spellings using locale preferences for US or UK.
# Default is to use a neutral variety of English.
# Setting locale to US will correct the British spelling of 'colour' to 'color'.

gocritic:
# Enable multiple checks by tags, run `GL_DEBUG=gocritic golangci-lint` run to see all tags and checks.
# Empty list by default. See https://github.com/go-critic/go-critic#usage -> section "Tags".
enabled-tags:
- performance

settings: # settings passed to gocritic
captLocal: # must be valid enabled check name
paramsOnly: true
Expand All @@ -37,29 +49,32 @@ linters-settings:

linters:
enable:
- bidichk
- bodyclose
- depguard
- errcheck
- exportloopref
- goconst
- gocyclo
- gofmt
- goimports
- goimports
- goprintffuncname
- gosec
- gosimple
- govet
- ineffassign
- lll
- megacheck
- megacheck
- misspell
- nolintlint
- predeclared
- revive
- staticcheck
- typecheck
- unconvert
- unparam
- varcheck
- unused
- usestdlibvars

fast: false

Expand Down
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ build:
.PHONY: lint
lint:
bash scripts/install-lint.sh
${GOPATH}/bin/golangci-lint run --timeout 2m
${GOPATH}/bin/golangci-lint run --timeout 5m

.PHONY: format
format:
go fmt ./...
test -x $(GOPATH)/bin/goimports || go install golang.org/x/tools/cmd/goimports@latest
$(GOPATH)/bin/goimports -local github.com/athanorlabs/go-relayer -w .


.PHONY: test
test:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Components:

## Requirements

- go 1.19+
- go 1.20+
- abigen: can install using `./scripts/install-abigen.sh`

## Usage
Expand Down
1 change: 1 addition & 0 deletions cmd/relayer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/urfave/cli/v2"

p2pnet "github.com/athanorlabs/go-p2p-net"

"github.com/athanorlabs/go-relayer/common"
contracts "github.com/athanorlabs/go-relayer/impls/gsnforwarder"
"github.com/athanorlabs/go-relayer/net"
Expand Down
73 changes: 32 additions & 41 deletions examples/mock_recipient/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ import (
"context"
"crypto/ecdsa"
"math/big"
"strings"
"testing"
"time"

"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
ethtypes "github.com/ethereum/go-ethereum/core/types"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/stretchr/testify/require"

"github.com/athanorlabs/go-relayer/common"
Expand All @@ -26,72 +23,64 @@ var ganachePrivateKeys = []string{
"646f1ce2fdad0e6deeeb5c7e8e5543bdde65e86029e2fd9fc169899c440a7913",
}

func setupAuth(t *testing.T) (*bind.TransactOpts, *ethclient.Client, *ecdsa.PrivateKey) {
ec, err := ethclient.Dial(common.DefaultEthEndpoint)
require.NoError(t, err)
t.Cleanup(func() {
ec.Close()
})
chainID, err := ec.ChainID(context.Background())
require.NoError(t, err)

pk, err := ethcrypto.HexToECDSA(ganachePrivateKeys[0])
require.NoError(t, err)

auth, err := bind.NewKeyedTransactorWithChainID(pk, chainID)
require.NoError(t, err)
return auth, ec, pk
}

func TestMock_Execute(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()

auth, conn, pk := setupAuth(t)
chainID, err := conn.ChainID(ctx)
pk, err := ethcrypto.HexToECDSA(ganachePrivateKeys[0])
require.NoError(t, err)

address, tx, contract, err := mforwarder.DeployMinimalForwarder(auth, conn)
ec, chainID := tests.NewEthClient(t)

txOpts := tests.NewTXOpts(t, ec, pk)
address, tx, contract, err := mforwarder.DeployMinimalForwarder(txOpts, ec)
require.NoError(t, err)
receipt := tests.MineTransaction(t, conn, tx)
receipt := tests.MineTransaction(t, ec, tx)
t.Logf("gas cost to deploy MinimalForwarder.sol: %d", receipt.GasUsed)

mockAddress, mockTx, _, err := DeployMock(auth, conn, address)
txOpts = tests.NewTXOpts(t, ec, pk)
mockAddress, mockTx, _, err := DeployMock(txOpts, ec, address)
require.NoError(t, err)
receipt = tests.MineTransaction(t, conn, mockTx)
receipt = tests.MineTransaction(t, ec, mockTx)
t.Logf("gas cost to deploy Mock.sol: %d", receipt.GasUsed)

// transfer to Mock.sol
value := big.NewInt(1000000)
fee := big.NewInt(10000)

gasPrice, err := conn.SuggestGasPrice(ctx)
gasPrice, err := ec.SuggestGasPrice(ctx)
require.NoError(t, err)
t.Logf("suggested gas price: %d", gasPrice)

transferTx := ethtypes.NewTransaction(
0,
mockAddress,
value,
100000,
gasPrice,
nil,
)
fromAddress := ethcrypto.PubkeyToAddress(*pk.Public().(*ecdsa.PublicKey))
nonce, err := ec.PendingNonceAt(ctx, fromAddress)
require.NoError(t, err)

transferTx := ethtypes.NewTx(&ethtypes.LegacyTx{
Nonce: nonce,
To: &mockAddress,
Value: value,
Gas: 100000,
GasPrice: gasPrice,
})
transferTx, err = ethtypes.SignTx(transferTx, ethtypes.LatestSignerForChainID(chainID), pk)
require.NoError(t, err)
err = conn.SendTransaction(ctx, transferTx)

err = ec.SendTransaction(ctx, transferTx)
require.NoError(t, err)
receipt, err = bind.WaitMined(ctx, conn, transferTx)

receipt, err = bind.WaitMined(ctx, ec, transferTx)
require.NoError(t, err)

require.Equal(t, ethtypes.ReceiptStatusSuccessful, receipt.Status)
t.Logf("transfer sent: %s", transferTx.Hash())

// generate ForwardRequest and sign it
key := common.NewKeyFromPrivateKey(pk)

abi, err := abi.JSON(strings.NewReader(MockABI))
abi, err := MockMetaData.GetAbi()
require.NoError(t, err)

calldata, err := abi.Pack("withdraw", value, fee)
require.NoError(t, err)

Expand Down Expand Up @@ -133,15 +122,17 @@ func TestMock_Execute(t *testing.T) {
t.Logf("verified forward request")

// execute withdraw() via forwarder
tx, err = contract.Execute(auth, *req, sig)
txOpts = tests.NewTXOpts(t, ec, pk)
tx, err = contract.Execute(txOpts, *req, sig)
require.NoError(t, err)
receipt = tests.MineTransaction(t, conn, tx)

receipt = tests.MineTransaction(t, ec, tx)
t.Logf("gas cost to call Mock.withdraw() via MinimalForwarder.execute(): %d", receipt.GasUsed)
require.Equal(t, uint64(1), receipt.Status)
require.Equal(t, 1, len(receipt.Logs))

// check that transfer worked
mockBalance, err := conn.BalanceAt(ctx, mockAddress, nil)
mockBalance, err := ec.BalanceAt(ctx, mockAddress, nil)
require.NoError(t, err)
require.Equal(t, int64(0), mockBalance.Int64())
}
Loading

0 comments on commit 009811c

Please sign in to comment.