Skip to content

Commit

Permalink
fix: add some fixes and UT (#3)
Browse files Browse the repository at this point in the history
* faucet: revert change

* eth: added UT for transaction propagation

* internal/eth: fixed merging side effect

* accounts: copy signer

* Added Quorum tag for the UT
  • Loading branch information
achraf17 authored Nov 17, 2021
1 parent fb1d7c1 commit ab5a991
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 8 deletions.
6 changes: 4 additions & 2 deletions accounts/abi/bind/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,15 @@ func NewKeyStoreTransactorWithChainID(keystore *keystore.KeyStore, account accou
if chainID == nil {
return nil, ErrNoChainID
}
signer := types.LatestSignerForChainID(chainID)
latestSigner := types.LatestSignerForChainID(chainID)
return &TransactOpts{
From: account.Address,
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
if address != account.Address {
return nil, ErrNotAuthorized
}
// Quorum
signer := latestSigner
if tx.IsPrivate() {
signer = types.QuorumPrivateTxSigner{}
}
Expand All @@ -160,14 +161,15 @@ func NewKeyedTransactorWithChainID(key *ecdsa.PrivateKey, chainID *big.Int) (*Tr
if chainID == nil {
return nil, ErrNoChainID
}
signer := types.LatestSignerForChainID(chainID)
latestSigner := types.LatestSignerForChainID(chainID)
return &TransactOpts{
From: keyAddr,
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
if address != keyAddr {
return nil, ErrNotAuthorized
}
// Quorum
signer := latestSigner
if tx.IsPrivate() {
signer = types.QuorumPrivateTxSigner{}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/faucet/faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -851,9 +851,9 @@ func authFacebook(url string) (string, string, common.Address, error) {
// Facebook recently changed their desktop webpage to use AJAX for loading post
// content, so switch over to the mobile site for now. Will probably end up having
// to use the API eventually.
//crawl := strings.Replace(url, "www.facebook.com", "m.facebook.com", 1)
crawl := strings.Replace(url, "www.facebook.com", "m.facebook.com", 1)

res, err := http.Get(url)
res, err := http.Get(crawl)
if err != nil {
return "", "", common.Address{}, err
}
Expand Down
82 changes: 82 additions & 0 deletions eth/handler_eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"math/big"
"math/rand"
"sync"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -462,6 +463,87 @@ func testTransactionPropagation(t *testing.T, protocol uint) {
}
}

// Quorum
// Tests that transactions get propagated to all peers using TransactionMessages and not PooledTransactionHashesMsg
func TestQuorumTransactionPropagation64(t *testing.T) { testQuorumTransactionPropagation(t, 64) }
func TestQuorumTransactionPropagation65(t *testing.T) { testQuorumTransactionPropagation(t, 65) }

func testQuorumTransactionPropagation(t *testing.T, protocol uint) {
t.Parallel()

numberOfPeers := 10

// Create a source handler to send transactions from and a number of sinks
// to receive them. We need multiple sinks since a one-to-one peering would
// broadcast all transactions without announcement.
source := newTestHandler()
defer source.close()

sinks := make([]*testHandler, numberOfPeers)
for i := 0; i < len(sinks); i++ {
sinks[i] = newTestHandler()
defer sinks[i].close()

sinks[i].handler.acceptTxs = 1 // mark synced to accept transactions
}

// create transactions
// Fill the source pool with transactions and wait for them at the sinks
txs := make([]*types.Transaction, 1024)
for nonce := range txs {
tx := types.NewTransaction(uint64(nonce), common.Address{}, big.NewInt(0), 100000, big.NewInt(0), nil)
tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)

txs[nonce] = tx
}

// WaitGroup to make sure peers are registered before adding transactions to pool
wgPeersRegistered := sync.WaitGroup{}
wgPeersRegistered.Add(numberOfPeers * 2)

// WaitGroup to make sure messages were shared to all peers
wgExpectPeerMessages := sync.WaitGroup{}
wgExpectPeerMessages.Add(numberOfPeers)
// Interconnect all the sink handlers with the source handler
for i, sink := range sinks {
sink := sink // Closure for gorotuine below

sourcePipe, sinkPipe := p2p.MsgPipe()
defer sourcePipe.Close()
defer sinkPipe.Close()

sourcePeer := eth.NewPeer(protocol, p2p.NewPeer(enode.ID{byte(i)}, "", nil), sourcePipe, source.txpool)
sinkPeer := eth.NewPeer(protocol, p2p.NewPeer(enode.ID{0}, "", nil), sinkPipe, sink.txpool)
defer sourcePeer.Close()
defer sinkPeer.Close()

go source.handler.runEthPeer(sourcePeer, func(peer *eth.Peer) error {
wgPeersRegistered.Done()
// handle using the normal way
return eth.Handle((*ethHandler)(source.handler), peer)
})
go sink.handler.runEthPeer(sinkPeer, func(peer *eth.Peer) error {
wgPeersRegistered.Done()
// intercept the received messages to make sure is the p2p type we are looking for
for e := peer.ExpectPeerMessage(uint64(eth.TransactionsMsg), txs); e != nil; {
t.Errorf("tx announce received on pre eth/65. errorL %s", e)
return e
}
wgExpectPeerMessages.Done()
return nil
})
}
wgPeersRegistered.Wait()

// add txs to pool
source.txpool.AddRemotes(txs)

// wait until all messages are handled
wgExpectPeerMessages.Wait()
}

// End Quorum

// Tests that post eth protocol handshake, clients perform a mutual checkpoint
// challenge to validate each other's chains. Hash mismatches, or missing ones
// during a fast sync should lead to the peer getting dropped.
Expand Down
4 changes: 4 additions & 0 deletions eth/protocols/eth/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,10 @@ func (p *Peer) ExpectRequestHeadersByNumber(origin uint64, amount int, skip int,
return p2p.ExpectMsg(p.rw, GetBlockHeadersMsg, req)
}

func (p *Peer) ExpectPeerMessage(code uint64, content types.Transactions) error {
return p2p.ExpectMsg(p.rw, code, content)
}

// RequestBodies fetches a batch of blocks' bodies corresponding to the hashes
// specified.
func (p *Peer) RequestBodies(hashes []common.Hash) error {
Expand Down
11 changes: 7 additions & 4 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1974,11 +1974,14 @@ func SubmitTransaction(ctx context.Context, b Backend, tx *types.Transaction, pr
// Ensure only eip155 signed transactions are submitted if EIP155Required is set.
return common.Hash{}, errors.New("only replay-protected (EIP-155) transactions allowed over RPC")
}
if err := b.SendTx(ctx, tx); err != nil {
return common.Hash{}, err
}
// Print a log with full tx details for manual investigations and interventions
signer := types.MakeSigner(b.ChainConfig(), b.CurrentBlock().Number())
// Quorum
var signer types.Signer
if tx.IsPrivate() {
signer = types.QuorumPrivateTxSigner{}
} else {
signer = types.MakeSigner(b.ChainConfig(), b.CurrentBlock().Number())
}
from, err := types.Sender(signer, tx)
if err != nil {
return common.Hash{}, err
Expand Down

0 comments on commit ab5a991

Please sign in to comment.