Skip to content

Commit

Permalink
merge w development
Browse files Browse the repository at this point in the history
  • Loading branch information
noot committed Jun 9, 2021
2 parents 7059b87 + 146813f commit d871223
Show file tree
Hide file tree
Showing 28 changed files with 466 additions and 400 deletions.
3 changes: 2 additions & 1 deletion dot/core/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func (s *Service) HandleTransactionMessage(msg *network.TransactionMessage) erro

for _, tx := range txs {
// validate each transaction
val, err := s.rt.ValidateTransaction(append([]byte{byte(types.TxnExternal)}, tx...))
externalExt := types.Extrinsic(append([]byte{byte(types.TxnExternal)}, tx...))
val, err := s.rt.ValidateTransaction(externalExt)
if err != nil {
logger.Error("failed to validate transaction", "err", err)
return err
Expand Down
33 changes: 28 additions & 5 deletions dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import (
"github.com/ChainSafe/gossamer/lib/crypto"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/scale"
"github.com/ChainSafe/gossamer/lib/services"
"github.com/ChainSafe/gossamer/lib/transaction"

log "github.com/ChainSafe/log15"
)

Expand Down Expand Up @@ -323,6 +323,11 @@ func (s *Service) handleChainReorg(prev, curr common.Hash) error {
return err
}

// subchain contains the ancestor as well so we need to remove it.
if len(subchain) > 0 {
subchain = subchain[1:]
}

// for each block in the previous chain, re-add its extrinsics back into the pool
for _, hash := range subchain {
body, err := s.blockState.GetBlockBody(hash)
Expand All @@ -340,13 +345,30 @@ func (s *Service) handleChainReorg(prev, curr common.Hash) error {
for _, ext := range exts {
logger.Debug("validating transaction on re-org chain", "extrinsic", ext)

txv, err := s.rt.ValidateTransaction(ext)
decExt := &types.ExtrinsicData{}
err = decExt.DecodeVersion(ext)
if err != nil {
return err
}

// Inherent are not signed.
if !decExt.IsSigned() {
continue
}

encExt, err := scale.Encode(ext)
if err != nil {
return err
}

externalExt := types.Extrinsic(append([]byte{byte(types.TxnExternal)}, encExt...))
txv, err := s.rt.ValidateTransaction(externalExt)
if err != nil {
logger.Debug("failed to validate transaction", "extrinsic", ext)
logger.Debug("failed to validate transaction", "error", err, "extrinsic", ext)
continue
}

vtx := transaction.NewValidTransaction(ext, txv)
vtx := transaction.NewValidTransaction(encExt, txv)
s.transactionState.AddToPool(vtx)
}
}
Expand Down Expand Up @@ -442,7 +464,8 @@ func (s *Service) HandleSubmittedExtrinsic(ext types.Extrinsic) error {

// the transaction source is External
// validate the transaction
txv, err := s.rt.ValidateTransaction(append([]byte{byte(types.TxnExternal)}, ext...))
externalExt := types.Extrinsic(append([]byte{byte(types.TxnExternal)}, ext...))
txv, err := s.rt.ValidateTransaction(externalExt)
if err != nil {
return err
}
Expand Down
49 changes: 49 additions & 0 deletions dot/core/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/sync"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/keystore"
Expand Down Expand Up @@ -166,6 +167,54 @@ func TestHandleChainReorg_NoReorg(t *testing.T) {
require.NoError(t, err)
}

func TestHandleChainReorg_WithReorg_Trans(t *testing.T) {
s := NewTestService(t, nil)

bs := s.blockState

parent, err := bs.BestBlockHeader()
require.NoError(t, err)

block1 := sync.BuildBlock(t, s.rt, parent, nil)
err = bs.AddBlock(block1)
require.NoError(t, err)

block2 := sync.BuildBlock(t, s.rt, block1.Header, nil)
err = bs.AddBlock(block2)
require.NoError(t, err)

block3 := sync.BuildBlock(t, s.rt, block2.Header, nil)
err = bs.AddBlock(block3)
require.NoError(t, err)

block4 := sync.BuildBlock(t, s.rt, block3.Header, nil)
err = bs.AddBlock(block4)
require.NoError(t, err)

block5 := sync.BuildBlock(t, s.rt, block4.Header, nil)
err = bs.AddBlock(block5)
require.NoError(t, err)

block31 := sync.BuildBlock(t, s.rt, block2.Header, nil)
err = bs.AddBlock(block31)
require.NoError(t, err)

nonce := uint64(1)

// Add extrinsic to block `block31`
ext := createExtrinsics(t, s.rt, bs.GenesisHash(), nonce)

block41 := sync.BuildBlock(t, s.rt, block31.Header, ext)
err = bs.AddBlock(block41)
require.NoError(t, err)

err = s.handleChainReorg(block41.Header.Hash(), block5.Header.Hash())
require.NoError(t, err)

pending := s.transactionState.(*state.TransactionState).Pending()
require.Equal(t, 1, len(pending))
}

func TestHandleChainReorg_WithReorg_NoTransactions(t *testing.T) {
s := NewTestService(t, nil)
height := 5
Expand Down
1 change: 0 additions & 1 deletion dot/core/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ func NewTestService(t *testing.T, cfg *Config) *Service {
config := &network.Config{
BasePath: testDatadirPath,
Port: 7001,
RandSeed: 1,
NoBootstrap: true,
NoMDNS: true,
BlockState: stateSrvc.Block,
Expand Down
2 changes: 0 additions & 2 deletions dot/network/block_announce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ func TestHandleBlockAnnounceMessage(t *testing.T) {
config := &Config{
BasePath: basePath,
Port: 7001,
RandSeed: 1,
NoBootstrap: true,
NoMDNS: true,
}
Expand All @@ -110,7 +109,6 @@ func TestValidateBlockAnnounceHandshake(t *testing.T) {
configA := &Config{
BasePath: utils.NewTestBasePath(t, "nodeA"),
Port: 7001,
RandSeed: 1,
NoBootstrap: true,
NoMDNS: true,
}
Expand Down
1 change: 0 additions & 1 deletion dot/network/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ type Config struct {
// build checks the configuration, sets up the private key for the network service,
// and applies default values where appropriate
func (c *Config) build() error {

// check state configuration
err := c.checkState()
if err != nil {
Expand Down
37 changes: 29 additions & 8 deletions dot/network/connmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,38 @@ func (cm *ConnManager) Disconnected(n network.Network, c network.Conn) {
Addrs: addrs,
}

go func() {
for i := 0; i < maxRetries; i++ {
err := cm.host.connect(info)
if err != nil {
logger.Warn("failed to reconnect to persistent peer", "peer", c.RemotePeer(), "error", err)
time.Sleep(time.Minute)
continue
}
count := 0
retry := func() bool {
err := cm.host.connect(info)
if err != nil {
logger.Warn("failed to reconnect to persistent peer", "peer", c.RemotePeer(), "error", err)
return false
}

count++
if count > maxRetries {
return true
}
return true
}

go func() {
if retry() {
return
}

retryTimer := time.NewTicker(time.Minute)
defer retryTimer.Stop()
for {
select {
case <-cm.host.ctx.Done():
return
case <-retryTimer.C:
if retry() {
return
}
}
}
}()

// TODO: if number of peers falls below the min desired peer count, we should try to connect to previously discovered peers
Expand Down
3 changes: 0 additions & 3 deletions dot/network/connmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func TestMaxPeers(t *testing.T) {
config := &Config{
BasePath: utils.NewTestBasePath(t, fmt.Sprintf("node%d", i)),
Port: 7000 + uint32(i),
RandSeed: 1 + int64(i),
NoBootstrap: true,
NoMDNS: true,
MaxPeers: max,
Expand Down Expand Up @@ -91,7 +90,6 @@ func TestPersistentPeers(t *testing.T) {
configA := &Config{
BasePath: utils.NewTestBasePath(t, "node-a"),
Port: 7000,
RandSeed: 1,
NoBootstrap: true,
NoMDNS: true,
}
Expand All @@ -101,7 +99,6 @@ func TestPersistentPeers(t *testing.T) {
configB := &Config{
BasePath: utils.NewTestBasePath(t, "node-b"),
Port: 7001,
RandSeed: 2,
NoMDNS: true,
PersistentPeers: []string{addrs[0].String()},
}
Expand Down
30 changes: 9 additions & 21 deletions dot/network/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ func newTestDiscovery(t *testing.T, num int) []*discovery {
config := &Config{
BasePath: utils.NewTestBasePath(t, fmt.Sprintf("node%d", i)),
Port: uint32(7001 + i),
RandSeed: int64(1 + i),
NoBootstrap: true,
NoMDNS: true,
}
Expand Down Expand Up @@ -119,7 +118,6 @@ func TestBeginDiscovery(t *testing.T) {
configA := &Config{
BasePath: utils.NewTestBasePath(t, "nodeA"),
Port: 7001,
RandSeed: 1,
NoBootstrap: true,
NoMDNS: true,
}
Expand All @@ -130,21 +128,18 @@ func TestBeginDiscovery(t *testing.T) {
configB := &Config{
BasePath: utils.NewTestBasePath(t, "nodeB"),
Port: 7002,
RandSeed: 2,
NoBootstrap: true,
NoMDNS: true,
}

nodeB := createTestService(t, configB)
nodeB.noGossip = true

addrInfosB, err := nodeB.host.addrInfos()
require.NoError(t, err)

err = nodeA.host.connect(*addrInfosB[0])
addrInfoB := nodeB.host.addrInfo()
err := nodeA.host.connect(addrInfoB)
if failedToDial(err) {
time.Sleep(TestBackoffTimeout)
err = nodeA.host.connect(*addrInfosB[0])
err = nodeA.host.connect(addrInfoB)
}
require.NoError(t, err)

Expand All @@ -159,7 +154,6 @@ func TestBeginDiscovery_ThreeNodes(t *testing.T) {
configA := &Config{
BasePath: utils.NewTestBasePath(t, "nodeA"),
Port: 7001,
RandSeed: 1,
NoBootstrap: true,
NoMDNS: true,
}
Expand All @@ -170,7 +164,6 @@ func TestBeginDiscovery_ThreeNodes(t *testing.T) {
configB := &Config{
BasePath: utils.NewTestBasePath(t, "nodeB"),
Port: 7002,
RandSeed: 2,
NoBootstrap: true,
NoMDNS: true,
}
Expand All @@ -181,7 +174,6 @@ func TestBeginDiscovery_ThreeNodes(t *testing.T) {
configC := &Config{
BasePath: utils.NewTestBasePath(t, "nodeC"),
Port: 7003,
RandSeed: 3,
NoBootstrap: true,
NoMDNS: true,
}
Expand All @@ -190,24 +182,20 @@ func TestBeginDiscovery_ThreeNodes(t *testing.T) {
nodeC.noGossip = true

// connect A and B
addrInfosB, err := nodeB.host.addrInfos()
require.NoError(t, err)

err = nodeA.host.connect(*addrInfosB[0])
addrInfoB := nodeB.host.addrInfo()
err := nodeA.host.connect(addrInfoB)
if failedToDial(err) {
time.Sleep(TestBackoffTimeout)
err = nodeA.host.connect(*addrInfosB[0])
err = nodeA.host.connect(addrInfoB)
}
require.NoError(t, err)

// connect A and C
addrInfosC, err := nodeC.host.addrInfos()
require.NoError(t, err)

err = nodeA.host.connect(*addrInfosC[0])
addrInfoC := nodeC.host.addrInfo()
err = nodeA.host.connect(addrInfoC)
if failedToDial(err) {
time.Sleep(TestBackoffTimeout)
err = nodeA.host.connect(*addrInfosC[0])
err = nodeA.host.connect(addrInfoC)
}
require.NoError(t, err)

Expand Down
Loading

0 comments on commit d871223

Please sign in to comment.