Skip to content
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

fix(dot/core): Fix handle transaction message test. #1607

Merged
merged 11 commits into from
Jun 1, 2021
7 changes: 7 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ jobs:
key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }}
restore-keys: ${{ runner.os }}-go-mod

- name: Install Subkey
run: |
wget -P $HOME/.local/bin/ https://chainbridge.ams3.digitaloceanspaces.com/subkey-v2.0.0
mv $HOME/.local/bin/subkey-v2.0.0 $HOME/.local/bin/subkey
chmod +x $HOME/.local/bin/subkey
echo "$HOME/.local/bin" >> $GITHUB_PATH

- name: Generate coverage report
run: |
go test ./... -short -coverprofile=coverage.out -covermode=atomic -timeout=20m
Expand Down
20 changes: 18 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,17 @@ jobs:

- name: Install Subkey
run: |
wget -P $HOME/.local/bin/ https://chainbridge.ams3.digitaloceanspaces.com/subkey-v2.0.0
mv $HOME/.local/bin/subkey-v2.0.0 $HOME/.local/bin/subkey
if [ "$RUNNER_OS" == "Linux" ]; then
wget -P $HOME/.local/bin/ https://chainbridge.ams3.digitaloceanspaces.com/subkey-v2.0.0
mv $HOME/.local/bin/subkey-v2.0.0 $HOME/.local/bin/subkey
elif [ "$RUNNER_OS" == "macOS" ]; then
wget -P $HOME/.local/bin/ https://chainbridge.ams3.digitaloceanspaces.com/subkey-v2.0.0-macos
mv $HOME/.local/bin/subkey-v2.0.0-macos $HOME/.local/bin/subkey
else
echo "Subkey for $RUNNER_OS is not supported"
exit 1
fi

chmod +x $HOME/.local/bin/subkey
echo "$HOME/.local/bin" >> $GITHUB_PATH

Expand Down Expand Up @@ -81,6 +90,13 @@ jobs:
key: ${{ runner.os }}-go-mod-${{ hashFiles('**/go.sum') }}
restore-keys: ${{ runner.os }}-go-mod

- name: Install Subkey
run: |
wget -P $HOME/.local/bin/ https://chainbridge.ams3.digitaloceanspaces.com/subkey-v2.0.0
mv $HOME/.local/bin/subkey-v2.0.0 $HOME/.local/bin/subkey
chmod +x $HOME/.local/bin/subkey
echo "$HOME/.local/bin" >> $GITHUB_PATH

- name: Generate coverage report
run: |
go test ./... -short -coverprofile=coverage.out -covermode=atomic -timeout=20m
Expand Down
5 changes: 2 additions & 3 deletions dot/core/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package core

import (
"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/transaction"
)

Expand All @@ -33,10 +34,8 @@ func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) erro
txs := msg.Extrinsics

for _, tx := range txs {
tx := tx // pin

// validate each transaction
val, err := s.rt.ValidateTransaction(tx)
val, err := s.rt.ValidateTransaction(append([]byte{byte(types.TxnExternal)}, tx...))
if err != nil {
logger.Error("failed to validate transaction", "err", err)
return err
Expand Down
81 changes: 62 additions & 19 deletions dot/core/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,9 @@ import (
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/runtime/wasmer"
"github.com/ChainSafe/gossamer/lib/trie"

log "github.com/ChainSafe/log15"
"github.com/ChainSafe/gossamer/lib/scale"
"github.com/centrifuge/go-substrate-rpc-client/v2/signature"
ctypes "github.com/centrifuge/go-substrate-rpc-client/v2/types"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -75,40 +74,84 @@ func TestService_ProcessBlockAnnounceMessage(t *testing.T) {
require.Equal(t, expected, net.Message)
}

func TestService_HandleTransactionMessage(t *testing.T) {
// this currently fails due to not being able to call validate_transaction
func createExtrinsics(t *testing.T, rt runtime.Instance, genHash common.Hash, nonce uint64) types.Extrinsic {
t.Helper()
rawMeta, err := rt.Metadata()
require.NoError(t, err)

t.Skip()
tt := trie.NewEmptyTrie()
rt := wasmer.NewTestInstanceWithTrie(t, runtime.NODE_RUNTIME, tt, log.LvlTrace)
decoded, err := scale.Decode(rawMeta, []byte{})
require.NoError(t, err)

kp, err := sr25519.GenerateKeypair()
require.Nil(t, err)
meta := &ctypes.Metadata{}
err = ctypes.DecodeFromBytes(decoded.([]byte), meta)
require.NoError(t, err)

rv, err := rt.Version()
require.NoError(t, err)

keyring, err := keystore.NewSr25519Keyring()
require.NoError(t, err)

bob, err := ctypes.NewAddressFromHexAccountID(keyring.Bob().Public().Hex())
require.NoError(t, err)

c, err := ctypes.NewCall(meta, "Balances.transfer", bob, ctypes.NewUCompactFromUInt(12345))
require.NoError(t, err)

// Create the extrinsic
ext := ctypes.NewExtrinsic(c)

// TODO: load BABE authority key
o := ctypes.SignatureOptions{
BlockHash: ctypes.Hash(genHash),
Era: ctypes.ExtrinsicEra{IsImmortalEra: true},
GenesisHash: ctypes.Hash(genHash),
Nonce: ctypes.NewUCompactFromUInt(nonce),
SpecVersion: ctypes.U32(rv.SpecVersion()),
Tip: ctypes.NewUCompactFromUInt(0),
TransactionVersion: ctypes.U32(rv.TransactionVersion()),
}

// Sign the transaction using Alice's default account
err = ext.Sign(signature.TestKeyringPairAlice, o)
require.NoError(t, err)

extEnc, err := ctypes.EncodeToHexString(ext)
require.NoError(t, err)

extBytes := types.Extrinsic(common.MustHexToBytes(extEnc))
return extBytes
}

func TestService_HandleTransactionMessage(t *testing.T) {
kp, err := sr25519.GenerateKeypair()
require.NoError(t, err)

ks := keystore.NewGlobalKeystore()
ks.Acco.Insert(kp)

cfg := &Config{
Runtime: rt,
Keystore: ks,
TransactionState: state.NewTransactionState(),
IsBlockProducer: true,
BlockProducer: &mockBlockProducer{},
}

s := NewTestService(t, cfg)
genHash := s.blockState.GenesisHash()
header, err := types.NewHeader(genHash, common.Hash{}, common.Hash{}, big.NewInt(1), types.NewEmptyDigest())
require.NoError(t, err)

// https://github.com/paritytech/substrate/blob/5420de3face1349a97eb954ae71c5b0b940c31de/core/transaction-pool/src/tests.rs#L95
ext := []byte{1, 212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125, 142, 175, 4, 21, 22, 135, 115, 99, 38, 201, 254, 161, 126, 37, 252, 82, 135, 97, 54, 147, 201, 18, 144, 156, 178, 38, 170, 71, 148, 242, 106, 72, 69, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 216, 5, 113, 87, 87, 40, 221, 120, 247, 252, 137, 201, 74, 231, 222, 101, 85, 108, 102, 39, 31, 190, 210, 14, 215, 124, 19, 160, 180, 203, 54, 110, 167, 163, 149, 45, 12, 108, 80, 221, 65, 238, 57, 237, 199, 16, 10, 33, 185, 8, 244, 184, 243, 139, 5, 87, 252, 245, 24, 225, 37, 154, 163, 142}
// initialise block header
err = s.rt.InitializeBlock(header)
require.NoError(t, err)

msg := &network.TransactionMessage{Extrinsics: []types.Extrinsic{ext}}
extBytes := createExtrinsics(t, s.rt, genHash, 0)

msg := &network.TransactionMessage{Extrinsics: []types.Extrinsic{extBytes}}
err = s.HandleTransactionMessage(msg)
require.Nil(t, err)
require.NoError(t, err)

pending := s.transactionState.(*state.TransactionState).Pending()
require.NotEqual(t, 0, len(pending))
tx := []byte(pending[0].Extrinsic)
require.Equal(t, ext, tx)
require.Equal(t, extBytes, pending[0].Extrinsic)
}
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQL
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/koron/go-ssdp v0.0.0-20191105050749-2e1c40ed0b5d h1:68u9r4wEvL3gYg2jvAOgROwZ3H+Y3hIDk4tbbmIjcYQ=
github.com/koron/go-ssdp v0.0.0-20191105050749-2e1c40ed0b5d/go.mod h1:5Ky9EC2xfoUKUor0Hjgi2BJhCSXJfMOFlmyYrVKGQMk=
Expand Down Expand Up @@ -608,7 +607,6 @@ github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I=
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/smola/gocompat v0.2.0/go.mod h1:1B0MlxbmoZNo3h8guHp8HztB3BSYR5itql9qtVc0ypY=
github.com/spacemonkeygo/openssl v0.0.0-20181017203307-c2dcc5cca94a/go.mod h1:7AyxJNCJ7SBZ1MfVQCWD6Uqo2oubI2Eq2y2eqf+A5r0=
Expand Down
2 changes: 1 addition & 1 deletion lib/grandpa/round_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func setupGrandpa(t *testing.T, kp *ed25519.Keypair) (*Service, chan GrandpaMess
DigestHandler: &mockDigestHandler{},
Voters: voters,
Keypair: kp,
LogLvl: log.LvlTrace,
LogLvl: log.LvlInfo,
Authority: true,
Network: net,
}
Expand Down