Skip to content
This repository has been archived by the owner on Jun 17, 2022. It is now read-only.

Commit

Permalink
trusted transactions
Browse files Browse the repository at this point in the history
Merge pull request #472 from devintegral3/add-local-transactions-for-node
  • Loading branch information
a.guzev committed Apr 14, 2020
2 parents 0f09e34 + 525df63 commit 91537c5
Show file tree
Hide file tree
Showing 23 changed files with 274 additions and 77 deletions.
5 changes: 2 additions & 3 deletions cmd/tx-storm/acc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
ethparams "github.com/ethereum/go-ethereum/params"

"github.com/Fantom-foundation/go-lachesis/crypto"
"github.com/Fantom-foundation/go-lachesis/lachesis/params"
Expand All @@ -32,7 +31,7 @@ func MakeAcc(n uint) *Acc {
}
}

func (a *Acc) TransactionTo(b *Acc, nonce uint, amount *big.Int) *types.Transaction {
func (a *Acc) TransactionTo(b *Acc, nonce uint, amount *big.Int, chainId uint) *types.Transaction {
tx := types.NewTransaction(
uint64(nonce),
*b.Addr,
Expand All @@ -44,7 +43,7 @@ func (a *Acc) TransactionTo(b *Acc, nonce uint, amount *big.Int) *types.Transact

signed, err := types.SignTx(
tx,
types.NewEIP155Signer(ethparams.AllEthashProtocolChanges.ChainID),
types.NewEIP155Signer(big.NewInt(int64(chainId))),
a.Key,
)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions cmd/tx-storm/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ import (
"gopkg.in/urfave/cli.v1"
)

var BlockChainIDFlag = cli.IntFlag{
Name: "chain-id",
Usage: "chain id for sign transactions",
Value: 4003, // Chain id for fakenet
}

func getChainId(ctx *cli.Context) (chainid uint) {
chainid = uint(ctx.GlobalInt(BlockChainIDFlag.Name))
return
}

var AccsStartFlag = cli.IntFlag{
Name: "accs-start",
Usage: "offset of predefined fake accounts",
Expand Down
4 changes: 3 additions & 1 deletion cmd/tx-storm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var (
func init() {
// Flags.
flags = []cli.Flag{
BlockChainIDFlag,
NumberFlag,
AccsStartFlag,
AccsCountFlag,
Expand Down Expand Up @@ -80,8 +81,9 @@ func generatorMain(ctx *cli.Context) error {
num, ofTotal := getNumber(ctx)
maxTxnsPerSec := getTxnsRate(ctx)
accsFrom, accsCount := getTestAccs(ctx)
chainId := getChainId(ctx)

tt := newThreads(url, num, ofTotal, maxTxnsPerSec, accsFrom, accsCount)
tt := newThreads(url, num, ofTotal, maxTxnsPerSec, accsFrom, accsCount, chainId)
tt.SetName("Threads")
tt.Start()

Expand Down
3 changes: 2 additions & 1 deletion cmd/tx-storm/threads.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func newThreads(
num, ofTotal uint,
maxTxnsPerSec uint,
accsFrom, accsCount uint,
chainId uint,
) *threads {
if num >= ofTotal {
panic("num is a generator number of total generators count")
Expand All @@ -56,7 +57,7 @@ func newThreads(
tt.Log.Info("Will use", "accounts", accs, "from", offset, "to", accs+offset)

for i := range tt.generators {
tt.generators[i] = newTxGenerator(uint(i), accsOnThread, offset)
tt.generators[i] = newTxGenerator(uint(i), accsOnThread, offset, chainId)
offset += accsOnThread
tt.generators[i].SetName(fmt.Sprintf("Generator%d", i))
}
Expand Down
8 changes: 6 additions & 2 deletions cmd/tx-storm/txgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type Transaction struct {
}

type generator struct {
chainId uint

accs []*Acc
offset uint

Expand All @@ -30,8 +32,10 @@ type generator struct {
logger.Instance
}

func newTxGenerator(num, accs, offset uint) *generator {
func newTxGenerator(num, accs, offset uint, chainId uint) *generator {
g := &generator{
chainId: chainId,

accs: make([]*Acc, accs),
offset: offset,

Expand Down Expand Up @@ -118,7 +122,7 @@ func (g *generator) generate(position uint) *Transaction {
amount := big.NewInt(1e6)

tx := &Transaction{
Raw: from.TransactionTo(to, nonce, amount),
Raw: from.TransactionTo(to, nonce, amount, g.chainId),
Info: meta.NewInfo(a, b),
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/tx-storm/txgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

func TestGenerator(t *testing.T) {
t.Skip("example only")
g := newTxGenerator(0, 20, 0)
g := newTxGenerator(0, 20, 0, 4003)
for i := 0; i < 2*len(g.accs); i++ {
tx := g.Yield()
t.Log(tx.Info.String(), tx.Raw.Nonce(), tx.Raw.Value())
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile.lachesis
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM golang:1.13-alpine as builder

RUN apk add --no-cache make gcc musl-dev linux-headers git

WORKDIR /go/go-lachesis
COPY . .

Expand Down
2 changes: 1 addition & 1 deletion docker/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ tx-storm:
-f docker/Dockerfile.tx-storm -t "tx-storm:$(TAG)" .

test_accs.json:
./_test_accs.sh
./_test_accs.sh
1 change: 0 additions & 1 deletion docker/txstorm-on.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ do
NODE=node$i
NAME=txgen$i

i=$((i+1))
docker run -d --rm \
--net=${NETWORK} --name=${NAME} \
--cpus=${LIMIT_CPU} --blkio-weight=${LIMIT_IO} \
Expand Down
25 changes: 23 additions & 2 deletions ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"github.com/Fantom-foundation/go-lachesis/inter"
"github.com/Fantom-foundation/go-lachesis/inter/idx"
lachesisparams "github.com/Fantom-foundation/go-lachesis/lachesis/params"
"github.com/Fantom-foundation/go-lachesis/utils"
)

const (
Expand Down Expand Up @@ -381,6 +382,24 @@ func (s *PrivateAccountAPI) SendTransaction(ctx context.Context, args SendTxArgs
return SubmitTransaction(ctx, s.b, signed)
}

// SendTrustedTransaction will create a trusted transaction from the given arguments and
// tries to sign it with the key associated with args.To. If the given passwd isn't
// able to decrypt the key it fails.
func (s *PrivateAccountAPI) SendTrustedTransaction(ctx context.Context, args SendTxArgs, passwd string) (common.Hash, error) {
if args.Nonce == nil {
// Hold the addresse's mutex around signing to prevent concurrent assignment of
// the same nonce to multiple accounts.
s.nonceLock.LockAddr(args.From)
defer s.nonceLock.UnlockAddr(args.From)
}
signed, err := s.signTransaction(ctx, &args, passwd)
if err != nil {
log.Warn("Failed transaction send attempt", "from", args.From, "to", args.To, "value", args.Value.ToInt(), "err", err)
return common.Hash{}, err
}
return SubmitTransaction(ctx, s.b, signed, true)
}

// SignTransaction will create a transaction from the given arguments and
// tries to sign it with the key associated with args.To. If the given passwd isn't
// able to decrypt the key it fails. The transaction is returned in RLP-form, not broadcast
Expand Down Expand Up @@ -1514,8 +1533,10 @@ func (args *SendTxArgs) toTransaction() *types.Transaction {
}

// SubmitTransaction is a helper function that submits tx to txPool and logs a message.
func SubmitTransaction(ctx context.Context, b Backend, tx *types.Transaction) (common.Hash, error) {
if err := b.SendTx(ctx, tx); err != nil {
func SubmitTransaction(ctx context.Context, b Backend, tx *types.Transaction, flags ...bool) (common.Hash, error) {
trusted := utils.ParseFlag(flags, 0, false)

if err := b.SendTx(ctx, tx, trusted); err != nil {
return common.Hash{}, err
}
if tx.To() == nil {
Expand Down
4 changes: 2 additions & 2 deletions ethapi/api_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func TestPrivateAccountAPI_SignAndSendTransaction(t *testing.T) {
d := uint64(1)
_, _ = api.UnlockAccount(ctx, key, "1234", &d)

b.EXPECT().SendTx(ctx, gomock.Any()).
b.EXPECT().SendTx(ctx, gomock.Any(), false).
Return(nil).
Times(1)

Expand Down Expand Up @@ -251,7 +251,7 @@ func TestPrivateAccountAPI_SendTransaction(t *testing.T) {
b.EXPECT().GetPoolNonce(ctx, gomock.Any()).
Return(uint64(1), nil).
AnyTimes()
b.EXPECT().SendTx(ctx, gomock.Any()).
b.EXPECT().SendTx(ctx, gomock.Any(), false).
Return(nil).
Times(2) // TODO: why 2 ?

Expand Down
4 changes: 2 additions & 2 deletions ethapi/api_transaction_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ func TestPublicTransactionPoolAPI_SendTransaction(t *testing.T) {
b.EXPECT().GetPoolNonce(gomock.Any(), gomock.Any()).
Return(uint64(1), nil).
AnyTimes()
b.EXPECT().SendTx(ctx, gomock.Any()).
b.EXPECT().SendTx(ctx, gomock.Any(), false).
Return(nil).
Times(2)

Expand Down Expand Up @@ -525,7 +525,7 @@ func TestPublicTransactionPoolAPI_SendRawTransaction(t *testing.T) {
ctx := context.TODO()
b := newTestBackend(t)

b.EXPECT().SendTx(ctx, gomock.Any()).
b.EXPECT().SendTx(ctx, gomock.Any(), false).
Return(nil).
Times(1)

Expand Down
2 changes: 1 addition & 1 deletion ethapi/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type Backend interface {
GetEVM(ctx context.Context, msg evmcore.Message, state *state.StateDB, header *evmcore.EvmHeader) (*vm.EVM, func() error, error)

// Transaction pool API
SendTx(ctx context.Context, signedTx *types.Transaction) error
SendTx(ctx context.Context, signedTx *types.Transaction, flags ...bool) error
GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, uint64, uint64, error)
GetPoolTransactions() (types.Transactions, error)
GetPoolTransaction(txHash common.Hash) *types.Transaction
Expand Down
13 changes: 9 additions & 4 deletions ethapi/backend_mock_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 91537c5

Please sign in to comment.