Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
MetaProtocol
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Nebula
MetaProtocol
Commits
30c64eea
Commit
30c64eea
authored
Dec 01, 2022
by
Ubuntu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update unit test
parent
71bb6705
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
491 additions
and
4 deletions
+491
-4
build-tx.go
build-tx.go
+127
-0
grcp-tx_test.go
grcp-tx_test.go
+4
-4
grpc-queue-tx_test.go
grpc-queue-tx_test.go
+360
-0
No files found.
build-tx.go
0 → 100644
View file @
30c64eea
package
main
import
(
"crypto/ecdsa"
"math/big"
"sync"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
)
func
buildSendTx
(
nonce
uint64
,
to
common
.
Address
,
amount
*
big
.
Int
,
gasLimit
uint64
,
gasPrice
*
big
.
Int
,
data
[]
byte
,
chainId
*
big
.
Int
,
privateKey
*
ecdsa
.
PrivateKey
)
(
*
types
.
Transaction
,
error
)
{
return
buildTx
(
nonce
,
to
,
amount
,
gasLimit
,
gasPrice
,
data
,
chainId
,
privateKey
)
}
func
buildOriginalTx
(
nonce
uint64
,
to
common
.
Address
,
amount
int64
,
chainId
*
big
.
Int
,
privateKey
*
ecdsa
.
PrivateKey
)
(
*
types
.
Transaction
,
error
)
{
return
buildTx
(
nonce
,
to
,
big
.
NewInt
(
amount
),
0
,
big
.
NewInt
(
0
),
nil
,
chainId
,
privateKey
)
}
func
buildTx
(
nonce
uint64
,
to
common
.
Address
,
amount
*
big
.
Int
,
gasLimit
uint64
,
gasPrice
*
big
.
Int
,
data
[]
byte
,
chainId
*
big
.
Int
,
privateKey
*
ecdsa
.
PrivateKey
)
(
*
types
.
Transaction
,
error
)
{
tx
:=
types
.
NewTransaction
(
nonce
,
to
,
amount
,
gasLimit
,
gasPrice
,
data
)
if
privateKey
!=
nil
{
signedTx
,
err
:=
types
.
SignTx
(
tx
,
types
.
NewEIP155Signer
(
chainId
),
privateKey
)
if
err
!=
nil
{
return
nil
,
err
}
tx
=
signedTx
}
return
tx
,
nil
}
type
EthClient
struct
{
PrivateKey
*
ecdsa
.
PrivateKey
FromAddr
common
.
Address
NodeUrl
string
Nonce
uint64
GasPrice
*
big
.
Int
ChainId
*
big
.
Int
GasLimit
uint64
}
var
originalTxPrivateKey
string
=
"9e0944f587e1043d6e303644738b0c7c77ed15b176ca574ed0be40c0b9bbdc3a"
var
toAddress
common
.
Address
=
common
.
HexToAddress
(
"0x0071B39fd266F8aeF392fb50F078A233b2218a0b"
)
var
originalTxsQueue
chan
*
types
.
Transaction
//= make(chan *types.Transaction, 50000)
var
originalTxParam
EthClient
var
FromAddr
common
.
Address
// type TxWithFrom struct {
// From []byte
// Tx []byte
// }
func
init
()
{
originalTxPrivatekeyAsECDSA
,
err
:=
crypto
.
HexToECDSA
(
originalTxPrivateKey
)
if
err
!=
nil
{
panic
(
err
)
}
originalTxPublicKey
:=
originalTxPrivatekeyAsECDSA
.
Public
()
originalTxPublicKeyECDSA
,
ok
:=
originalTxPublicKey
.
(
*
ecdsa
.
PublicKey
)
if
!
ok
{
panic
(
"publicKey.(*ecdsa.PublicKey) not ok"
)
}
originalTxFromAddress
:=
crypto
.
PubkeyToAddress
(
*
originalTxPublicKeyECDSA
)
originalTxParam
=
EthClient
{
PrivateKey
:
originalTxPrivatekeyAsECDSA
,
FromAddr
:
originalTxFromAddress
,
Nonce
:
0
,
}
FromAddr
=
originalTxFromAddress
}
var
doneOnce
sync
.
Once
func
ProduceOriginalTx
(
firstdone
chan
bool
)
error
{
if
originalTxsQueue
==
nil
{
originalTxsQueue
=
make
(
chan
*
types
.
Transaction
,
50000
)
}
for
{
if
len
(
originalTxsQueue
)
==
50000
{
doneOnce
.
Do
(
func
()
{
firstdone
<-
true
})
}
tx
,
err
:=
buildOriginalTx
(
originalTxParam
.
Nonce
,
toAddress
,
0
,
big
.
NewInt
(
256
),
nil
)
if
err
!=
nil
{
return
err
}
originalTxParam
.
Nonce
+=
1
originalTxsQueue
<-
tx
}
}
func
ProduceOriginalTxByCount
(
count
int64
)
(
chan
*
types
.
Transaction
,
error
)
{
var
txsQueue
chan
*
types
.
Transaction
=
make
(
chan
*
types
.
Transaction
,
count
)
for
i
:=
0
;
int64
(
i
)
<
count
;
i
++
{
tx
,
err
:=
buildOriginalTx
(
originalTxParam
.
Nonce
,
toAddress
,
0
,
big
.
NewInt
(
256
),
nil
)
if
err
!=
nil
{
return
nil
,
err
}
originalTxParam
.
Nonce
+=
1
txsQueue
<-
tx
}
return
txsQueue
,
nil
}
grcp-tx_test.go
View file @
30c64eea
...
...
@@ -178,10 +178,10 @@ func pricedTransaction(to common.Address, nonce uint64, gaslimit uint64, gaspric
}
// go test -v -run EthTx -bench=. -benchtime=3s
// go test -v -run BenchmarkEthTx -bench BenchmarkEthTx -benchtime=3s
// go test -v -run BenchmarkStdTx -bench BenchmarkStdTx -benchtime=3s
// go test -v -run BenchmarkAnyTx -bench BenchmarkAnyTx -benchtime=3s
// go test -v -run BenchmarkBytesEth -bench BenchmarkBytesEth -benchtime=3s
// go test -v -run BenchmarkEthTx -bench BenchmarkEthTx -benchtime=3s
-benchmem
// go test -v -run BenchmarkStdTx -bench BenchmarkStdTx -benchtime=3s
-benchmem
// go test -v -run BenchmarkAnyTx -bench BenchmarkAnyTx -benchtime=3s
-benchmem
// go test -v -run BenchmarkBytesEth -bench BenchmarkBytesEth -benchtime=3s
-benchmem
// go test -v -run BenchmarkEthTx -bench BenchmarkEthTx -benchtime=1s
// go test -v -run BenchmarkStdTx -bench BenchmarkStdTx -benchtime=1s
...
...
grpc-queue-tx_test.go
0 → 100644
View file @
30c64eea
package
main
import
(
"bytes"
"context"
"sync"
//"encoding/hex"
"testing"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/protobuf/types/known/anypb"
base
"github.com/CaduceusMetaverseProtocol/metaprotocol/gen/proto/go/base/v1"
ring
"github.com/CaduceusMetaverseProtocol/metaprotocol/gen/proto/go/ring/v1"
)
// go test -v -run EthTx -bench=. -benchtime=3s
// go test -v -run BenchmarkQueueAnyTx -bench BenchmarkQueueAnyTx -benchtime=3s -benchmem
// go test -v -run BenchmarkQueueEthTx -bench BenchmarkQueueEthTx -benchtime=3s -benchmem
// go test -v -run BenchmarkQueueStdTx -bench BenchmarkQueueStdTx -benchtime=3s -benchmem
// go test -v -run BenchmarkQueueBytesEth -bench BenchmarkQueueBytesEth -benchtime=3s -benchmem
var
once
sync
.
Once
var
onceHash
sync
.
Once
func
BenchmarkQueueEthTx
(
b
*
testing
.
B
)
{
b
.
Logf
(
"b.N: %d
\n
"
,
b
.
N
)
// firstDone := make(chan bool, 1)
// onceFunc := func() {
// go ProduceOriginalTx(firstDone)
// <-firstDone
// }
// once.Do(onceFunc)
onceFunc
:=
func
()
{
txsQueue
,
err
:=
ProduceOriginalTxByCount
(
500000
)
if
err
!=
nil
{
b
.
Fatal
(
err
)
}
originalTxsQueue
=
txsQueue
}
once
.
Do
(
onceFunc
)
b
.
ResetTimer
()
b
.
RunParallel
(
func
(
pb
*
testing
.
PB
)
{
for
pb
.
Next
()
{
conn
,
err
:=
grpc
.
Dial
(
"127.0.0.1:9006"
,
grpc
.
WithTransportCredentials
(
insecure
.
NewCredentials
()))
if
err
!=
nil
{
b
.
Fatal
(
err
)
}
defer
conn
.
Close
()
c
:=
ring
.
NewRingServiceClient
(
conn
)
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Second
)
defer
cancel
()
tx
:=
<-
originalTxsQueue
inner
:=
base
.
EthTxData
{
AccountNonce
:
tx
.
Nonce
(),
Price
:
tx
.
GasPrice
()
.
Bytes
(),
GasLimit
:
tx
.
Gas
(),
Payload
:
tx
.
Data
(),
}
v
,
r
,
sigs
:=
tx
.
RawSignatureValues
()
inner
.
V
=
v
.
Bytes
()
inner
.
R
=
r
.
Bytes
()
inner
.
S
=
sigs
.
Bytes
()
inner
.
Amount
=
tx
.
Value
()
.
Bytes
()
addr
:=
base
.
Address
{
Address
:
tx
.
To
()
.
Bytes
()}
inner
.
Recipient
=
&
addr
inner
.
From
=
FromAddr
.
Bytes
()
res
,
err
:=
c
.
SendTxAsEth
(
ctx
,
&
base
.
TransactionEth
{
Tx
:
&
base
.
EthTx
{
Inner
:
&
inner
}})
if
err
!=
nil
{
b
.
Fatal
(
err
)
}
_
=
res
if
bytes
.
Compare
(
tx
.
Hash
()
.
Bytes
(),
res
.
TxHash
)
!=
0
{
b
.
Fatal
(
err
)
}
onceHash
.
Do
(
func
()
{
b
.
Logf
(
"response: %x local: %x
\n
"
,
res
.
TxHash
,
tx
.
Hash
()
.
Bytes
())
})
}
})
}
func
BenchmarkQueueStdTx
(
b
*
testing
.
B
)
{
b
.
Logf
(
"b.N: %d
\n
"
,
b
.
N
)
// firstDone := make(chan bool, 1)
// onceFunc := func() {
// go ProduceOriginalTx(firstDone)
// <-firstDone
// }
// once.Do(onceFunc)
onceFunc
:=
func
()
{
txsQueue
,
err
:=
ProduceOriginalTxByCount
(
500000
)
if
err
!=
nil
{
b
.
Fatal
(
err
)
}
originalTxsQueue
=
txsQueue
}
once
.
Do
(
onceFunc
)
b
.
ResetTimer
()
b
.
RunParallel
(
func
(
pb
*
testing
.
PB
)
{
for
pb
.
Next
()
{
conn
,
err
:=
grpc
.
Dial
(
"127.0.0.1:9006"
,
grpc
.
WithTransportCredentials
(
insecure
.
NewCredentials
()))
if
err
!=
nil
{
b
.
Fatal
(
err
)
}
defer
conn
.
Close
()
c
:=
ring
.
NewRingServiceClient
(
conn
)
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Second
)
defer
cancel
()
tx
:=
<-
originalTxsQueue
inner
:=
base
.
StdTxData
{
AccountNonce
:
tx
.
Nonce
(),
Price
:
tx
.
GasPrice
()
.
Bytes
(),
GasLimit
:
tx
.
Gas
(),
Payload
:
tx
.
Data
(),
}
v
,
r
,
sigs
:=
tx
.
RawSignatureValues
()
inner
.
V
=
v
.
Bytes
()
inner
.
R
=
r
.
Bytes
()
inner
.
S
=
sigs
.
Bytes
()
inner
.
Amount
=
tx
.
Value
()
.
Bytes
()
addr
:=
base
.
Address
{
Address
:
tx
.
To
()
.
Bytes
()}
inner
.
Recipient
=
&
addr
inner
.
From
=
FromAddr
.
Bytes
()
res
,
err
:=
c
.
SendTxAsStd
(
ctx
,
&
base
.
TransactionStd
{
Tx
:
&
base
.
StdTx
{
Inner
:
&
inner
}})
if
err
!=
nil
{
b
.
Fatal
(
err
)
}
_
=
res
//fmt.Printf("%x \n", res.TxHash)
if
bytes
.
Compare
(
tx
.
Hash
()
.
Bytes
(),
res
.
TxHash
)
!=
0
{
b
.
Fatal
(
err
)
}
onceHash
.
Do
(
func
()
{
b
.
Logf
(
"response: %x local: %x
\n
"
,
res
.
TxHash
,
tx
.
Hash
()
.
Bytes
())
})
}
})
}
func
BenchmarkQueueAnyTx
(
b
*
testing
.
B
)
{
b
.
Logf
(
"b.N: %d
\n
"
,
b
.
N
)
// firstDone := make(chan bool, 1)
// onceFunc := func() {
// go ProduceOriginalTx(firstDone)
// <-firstDone
// }
// once.Do(onceFunc)
onceFunc
:=
func
()
{
txsQueue
,
err
:=
ProduceOriginalTxByCount
(
500000
)
if
err
!=
nil
{
b
.
Fatal
(
err
)
}
originalTxsQueue
=
txsQueue
}
once
.
Do
(
onceFunc
)
b
.
ResetTimer
()
// The loop body is executed b.N times total across all goroutines.
b
.
RunParallel
(
func
(
pb
*
testing
.
PB
)
{
for
pb
.
Next
()
{
conn
,
err
:=
grpc
.
Dial
(
"127.0.0.1:9006"
,
grpc
.
WithTransportCredentials
(
insecure
.
NewCredentials
()))
if
err
!=
nil
{
b
.
Fatal
(
err
)
}
defer
conn
.
Close
()
c
:=
ring
.
NewRingServiceClient
(
conn
)
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Second
)
defer
cancel
()
tx
:=
<-
originalTxsQueue
inner
:=
base
.
EthTxData
{
AccountNonce
:
tx
.
Nonce
(),
Price
:
tx
.
GasPrice
()
.
Bytes
(),
GasLimit
:
tx
.
Gas
(),
Payload
:
tx
.
Data
(),
}
v
,
r
,
sigs
:=
tx
.
RawSignatureValues
()
inner
.
V
=
v
.
Bytes
()
inner
.
R
=
r
.
Bytes
()
inner
.
S
=
sigs
.
Bytes
()
inner
.
Amount
=
tx
.
Value
()
.
Bytes
()
addr
:=
base
.
Address
{
Address
:
tx
.
To
()
.
Bytes
()}
inner
.
Recipient
=
&
addr
inner
.
From
=
FromAddr
.
Bytes
()
ethTx
:=
base
.
EthTx
{
Inner
:
&
inner
}
ethTxAsAny
,
err
:=
anypb
.
New
(
&
ethTx
)
res
,
err
:=
c
.
SendTxAsAny
(
ctx
,
&
base
.
Transaction
{
Tx
:
ethTxAsAny
})
if
err
!=
nil
{
b
.
Fatal
(
err
)
}
_
=
res
if
bytes
.
Compare
(
tx
.
Hash
()
.
Bytes
(),
res
.
TxHash
)
!=
0
{
b
.
Fatal
(
err
)
}
onceHash
.
Do
(
func
()
{
b
.
Logf
(
"response: %x local: %x
\n
"
,
res
.
TxHash
,
tx
.
Hash
()
.
Bytes
())
})
}
})
}
func
BenchmarkQueueBytesEth
(
b
*
testing
.
B
)
{
b
.
Logf
(
"b.N: %d
\n
"
,
b
.
N
)
// firstDone := make(chan bool, 1)
// onceFunc := func() {
// go ProduceOriginalTx(firstDone)
// <-firstDone
// }
// once.Do(onceFunc)
onceFunc
:=
func
()
{
txsQueue
,
err
:=
ProduceOriginalTxByCount
(
500000
)
if
err
!=
nil
{
b
.
Fatal
(
err
)
}
originalTxsQueue
=
txsQueue
}
once
.
Do
(
onceFunc
)
b
.
ResetTimer
()
b
.
RunParallel
(
func
(
pb
*
testing
.
PB
)
{
for
pb
.
Next
()
{
conn
,
err
:=
grpc
.
Dial
(
"127.0.0.1:9006"
,
grpc
.
WithTransportCredentials
(
insecure
.
NewCredentials
()))
if
err
!=
nil
{
b
.
Fatal
(
err
)
}
defer
conn
.
Close
()
c
:=
ring
.
NewRingServiceClient
(
conn
)
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
time
.
Second
)
defer
cancel
()
tx
:=
<-
originalTxsQueue
txAsBytes
,
err
:=
tx
.
MarshalBinary
()
if
err
!=
nil
{
b
.
Fatal
(
err
)
}
res
,
err
:=
c
.
SendTxAsBytes
(
ctx
,
&
base
.
TransactionBytes
{
Tx
:
txAsBytes
,
From
:
FromAddr
[
:
]})
if
err
!=
nil
{
b
.
Fatal
(
err
)
}
_
=
res
if
bytes
.
Compare
(
tx
.
Hash
()
.
Bytes
(),
res
.
TxHash
)
!=
0
{
b
.
Fatal
(
err
)
}
onceHash
.
Do
(
func
()
{
b
.
Logf
(
"response: %x local: %x
\n
"
,
res
.
TxHash
,
tx
.
Hash
()
.
Bytes
())
})
}
})
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment