2018-03-14 09:36:59 +00:00
|
|
|
package network
|
|
|
|
|
|
|
|
import (
|
2019-08-23 15:19:07 +00:00
|
|
|
"math/rand"
|
2019-09-09 14:54:38 +00:00
|
|
|
"net"
|
2019-09-25 16:54:31 +00:00
|
|
|
"sync/atomic"
|
2018-03-14 09:36:59 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-03-25 15:30:21 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/mempool"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/network/payload"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2019-12-30 07:43:05 +00:00
|
|
|
"go.uber.org/zap/zaptest"
|
2018-03-14 09:36:59 +00:00
|
|
|
)
|
|
|
|
|
2019-09-25 16:54:31 +00:00
|
|
|
type testChain struct {
|
|
|
|
blockheight uint32
|
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
|
2020-02-18 17:16:38 +00:00
|
|
|
func (chain testChain) ApplyPolicyToTxSet([]mempool.TxWithFee) []mempool.TxWithFee {
|
|
|
|
panic("TODO")
|
|
|
|
}
|
2019-02-20 17:39:32 +00:00
|
|
|
func (chain testChain) GetConfig() config.ProtocolConfiguration {
|
|
|
|
panic("TODO")
|
|
|
|
}
|
2020-02-25 13:15:17 +00:00
|
|
|
func (chain testChain) CalculateClaimable(util.Fixed8, uint32, uint32) (util.Fixed8, util.Fixed8, error) {
|
|
|
|
panic("TODO")
|
|
|
|
}
|
2019-02-20 17:39:32 +00:00
|
|
|
|
2020-02-26 07:58:20 +00:00
|
|
|
func (chain testChain) References(t *transaction.Transaction) ([]transaction.InOut, error) {
|
2019-02-20 17:39:32 +00:00
|
|
|
panic("TODO")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (chain testChain) FeePerByte(t *transaction.Transaction) util.Fixed8 {
|
|
|
|
panic("TODO")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (chain testChain) SystemFee(t *transaction.Transaction) util.Fixed8 {
|
|
|
|
panic("TODO")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (chain testChain) NetworkFee(t *transaction.Transaction) util.Fixed8 {
|
|
|
|
panic("TODO")
|
|
|
|
}
|
|
|
|
|
2020-01-14 12:32:07 +00:00
|
|
|
func (chain testChain) AddHeaders(...*block.Header) error {
|
2019-02-20 17:39:32 +00:00
|
|
|
panic("TODO")
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
2020-01-14 12:32:07 +00:00
|
|
|
func (chain *testChain) AddBlock(block *block.Block) error {
|
2019-09-25 16:54:31 +00:00
|
|
|
if block.Index == chain.blockheight+1 {
|
|
|
|
atomic.StoreUint32(&chain.blockheight, block.Index)
|
|
|
|
}
|
|
|
|
return nil
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
2019-09-25 16:54:31 +00:00
|
|
|
func (chain *testChain) BlockHeight() uint32 {
|
|
|
|
return atomic.LoadUint32(&chain.blockheight)
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
2019-11-07 17:47:48 +00:00
|
|
|
func (chain *testChain) Close() {
|
|
|
|
panic("TODO")
|
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
func (chain testChain) HeaderHeight() uint32 {
|
|
|
|
return 0
|
|
|
|
}
|
2020-02-21 14:56:28 +00:00
|
|
|
func (chain testChain) GetAppExecResult(hash util.Uint256) (*state.AppExecResult, error) {
|
|
|
|
panic("TODO")
|
|
|
|
}
|
2020-01-14 12:32:07 +00:00
|
|
|
func (chain testChain) GetBlock(hash util.Uint256) (*block.Block, error) {
|
2019-02-20 17:39:32 +00:00
|
|
|
panic("TODO")
|
2018-03-23 20:36:59 +00:00
|
|
|
}
|
2019-11-28 16:06:09 +00:00
|
|
|
func (chain testChain) GetContractState(hash util.Uint160) *state.Contract {
|
2019-10-11 11:22:53 +00:00
|
|
|
panic("TODO")
|
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
func (chain testChain) GetHeaderHash(int) util.Uint256 {
|
|
|
|
return util.Uint256{}
|
|
|
|
}
|
2020-01-14 12:32:07 +00:00
|
|
|
func (chain testChain) GetHeader(hash util.Uint256) (*block.Header, error) {
|
2019-02-20 17:39:32 +00:00
|
|
|
panic("TODO")
|
|
|
|
}
|
|
|
|
|
2019-11-28 16:06:09 +00:00
|
|
|
func (chain testChain) GetAssetState(util.Uint256) *state.Asset {
|
2019-02-20 17:39:32 +00:00
|
|
|
panic("TODO")
|
2018-11-26 21:12:33 +00:00
|
|
|
}
|
2019-11-28 16:06:09 +00:00
|
|
|
func (chain testChain) GetAccountState(util.Uint160) *state.Account {
|
2019-02-20 17:39:32 +00:00
|
|
|
panic("TODO")
|
2019-02-08 08:04:38 +00:00
|
|
|
}
|
2020-03-05 12:16:03 +00:00
|
|
|
func (chain testChain) GetNEP5TransferLog(util.Uint160) *state.NEP5TransferLog {
|
|
|
|
panic("TODO")
|
|
|
|
}
|
2020-03-11 15:22:46 +00:00
|
|
|
func (chain testChain) GetNEP5Balances(util.Uint160) *state.NEP5Balances {
|
|
|
|
panic("TODO")
|
|
|
|
}
|
2019-11-11 15:25:28 +00:00
|
|
|
func (chain testChain) GetValidators(...*transaction.Transaction) ([]*keys.PublicKey, error) {
|
|
|
|
panic("TODO")
|
|
|
|
}
|
2020-03-30 08:16:53 +00:00
|
|
|
func (chain testChain) GetStandByValidators() (keys.PublicKeys, error) {
|
|
|
|
panic("TODO")
|
|
|
|
}
|
2020-03-05 14:48:30 +00:00
|
|
|
func (chain testChain) GetEnrollments() ([]*state.Validator, error) {
|
|
|
|
panic("TODO")
|
|
|
|
}
|
2019-10-11 11:22:53 +00:00
|
|
|
func (chain testChain) GetScriptHashesForVerifying(*transaction.Transaction) ([]util.Uint160, error) {
|
|
|
|
panic("TODO")
|
|
|
|
}
|
2019-11-28 16:06:09 +00:00
|
|
|
func (chain testChain) GetStorageItem(scripthash util.Uint160, key []byte) *state.StorageItem {
|
2019-10-11 11:08:21 +00:00
|
|
|
panic("TODO")
|
|
|
|
}
|
2020-04-03 06:49:01 +00:00
|
|
|
func (chain testChain) GetTestVM() *vm.VM {
|
2019-10-29 15:31:39 +00:00
|
|
|
panic("TODO")
|
|
|
|
}
|
2019-11-28 16:06:09 +00:00
|
|
|
func (chain testChain) GetStorageItems(hash util.Uint160) (map[string]*state.StorageItem, error) {
|
2019-10-11 11:08:21 +00:00
|
|
|
panic("TODO")
|
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
func (chain testChain) CurrentHeaderHash() util.Uint256 {
|
|
|
|
return util.Uint256{}
|
|
|
|
}
|
|
|
|
func (chain testChain) CurrentBlockHash() util.Uint256 {
|
|
|
|
return util.Uint256{}
|
|
|
|
}
|
|
|
|
func (chain testChain) HasBlock(util.Uint256) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
func (chain testChain) HasTransaction(util.Uint256) bool {
|
|
|
|
return false
|
|
|
|
}
|
2019-02-20 17:39:32 +00:00
|
|
|
func (chain testChain) GetTransaction(util.Uint256) (*transaction.Transaction, uint32, error) {
|
|
|
|
panic("TODO")
|
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
|
2020-03-09 13:56:37 +00:00
|
|
|
func (chain testChain) GetUnspentCoinState(util.Uint256) *state.UnspentCoin {
|
2019-10-11 11:16:53 +00:00
|
|
|
panic("TODO")
|
|
|
|
}
|
|
|
|
|
2020-02-04 14:36:11 +00:00
|
|
|
func (chain testChain) GetMemPool() *mempool.Pool {
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
panic("TODO")
|
|
|
|
}
|
|
|
|
|
2020-02-18 15:42:11 +00:00
|
|
|
func (chain testChain) IsLowPriority(util.Fixed8) bool {
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
panic("TODO")
|
|
|
|
}
|
|
|
|
|
2020-02-04 15:43:21 +00:00
|
|
|
func (chain testChain) PoolTx(*transaction.Transaction) error {
|
|
|
|
panic("TODO")
|
|
|
|
}
|
|
|
|
|
2020-01-14 12:32:07 +00:00
|
|
|
func (chain testChain) VerifyTx(*transaction.Transaction, *block.Block) error {
|
Implement rpc server method: sendrawtransaction (#174)
* Added new config attributes: 'SecondsPerBlock','LowPriorityThreshold'
* Added new files:
* Added new method: CompareTo
* Fixed empty Slice case
* Added new methods: LessThan, GreaterThan, Equal, CompareTo
* Added new method: InputIntersection
* Added MaxTransactionSize, GroupOutputByAssetID
* Added ned method: ScriptHash
* Added new method: IsDoubleSpend
* Refactor blockchainer, Added Feer interface, Verify and GetMemPool method
* 1) Added MemPool
2) Added new methods to satisfy the blockchainer interface: IsLowPriority, Verify, GetMemPool
* Added new methods: RelayTxn, RelayDirectly
* Fixed tests
* Implemented RPC server method sendrawtransaction
* Refactor getrawtransaction, sendrawtransaction in separate methods
* Moved 'secondsPerBlock' to config file
* Implemented Kim suggestions:
1) Fixed data race issues
2) refactor Verify method
3) Get rid of unused InputIntersection method due to refactoring Verify method
4) Fixed bug in https://github.com/CityOfZion/neo-go/pull/174#discussion_r264108135
5) minor simplications of the code
* Fixed minor issues related to
1) space
2) getter methods do not need pointer on the receiver
3) error message
4) refactoring CompareTo method in uint256.go
* Fixed small issues
* Use sync.RWMutex instead of sync.Mutex
* Refined (R)Lock/(R)Unlock
* return error instead of bool in Verify methods
2019-03-20 12:30:05 +00:00
|
|
|
panic("TODO")
|
|
|
|
}
|
|
|
|
|
2018-03-14 09:36:59 +00:00
|
|
|
type testDiscovery struct{}
|
|
|
|
|
2019-09-13 16:51:58 +00:00
|
|
|
func (d testDiscovery) BackFill(addrs ...string) {}
|
2020-02-24 12:39:31 +00:00
|
|
|
func (d testDiscovery) Close() {}
|
2019-09-13 16:51:58 +00:00
|
|
|
func (d testDiscovery) PoolCount() int { return 0 }
|
|
|
|
func (d testDiscovery) RegisterBadAddr(string) {}
|
|
|
|
func (d testDiscovery) RegisterGoodAddr(string) {}
|
2020-01-28 16:10:13 +00:00
|
|
|
func (d testDiscovery) RegisterConnectedAddr(string) {}
|
2019-09-13 16:51:58 +00:00
|
|
|
func (d testDiscovery) UnregisterConnectedAddr(string) {}
|
|
|
|
func (d testDiscovery) UnconnectedPeers() []string { return []string{} }
|
|
|
|
func (d testDiscovery) RequestRemote(n int) {}
|
|
|
|
func (d testDiscovery) BadPeers() []string { return []string{} }
|
|
|
|
func (d testDiscovery) GoodPeers() []string { return []string{} }
|
2018-03-14 09:36:59 +00:00
|
|
|
|
|
|
|
type localTransport struct{}
|
|
|
|
|
|
|
|
func (t localTransport) Dial(addr string, timeout time.Duration) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (t localTransport) Accept() {}
|
|
|
|
func (t localTransport) Proto() string { return "local" }
|
|
|
|
func (t localTransport) Close() {}
|
|
|
|
|
|
|
|
var defaultMessageHandler = func(t *testing.T, msg *Message) {}
|
|
|
|
|
|
|
|
type localPeer struct {
|
2019-09-09 14:54:38 +00:00
|
|
|
netaddr net.TCPAddr
|
2020-01-21 14:26:08 +00:00
|
|
|
server *Server
|
2018-03-14 09:36:59 +00:00
|
|
|
version *payload.Version
|
2020-01-17 10:17:19 +00:00
|
|
|
lastBlockIndex uint32
|
2019-09-13 12:43:22 +00:00
|
|
|
handshaked bool
|
2018-03-14 09:36:59 +00:00
|
|
|
t *testing.T
|
|
|
|
messageHandler func(t *testing.T, msg *Message)
|
2020-01-20 16:02:19 +00:00
|
|
|
pingSent int
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 14:26:08 +00:00
|
|
|
func newLocalPeer(t *testing.T, s *Server) *localPeer {
|
2019-09-09 14:54:38 +00:00
|
|
|
naddr, _ := net.ResolveTCPAddr("tcp", "0.0.0.0:0")
|
2018-03-14 09:36:59 +00:00
|
|
|
return &localPeer{
|
|
|
|
t: t,
|
2020-01-21 14:26:08 +00:00
|
|
|
server: s,
|
2019-09-09 14:54:38 +00:00
|
|
|
netaddr: *naddr,
|
2018-03-14 09:36:59 +00:00
|
|
|
messageHandler: defaultMessageHandler,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-06 07:55:21 +00:00
|
|
|
func (p *localPeer) RemoteAddr() net.Addr {
|
|
|
|
return &p.netaddr
|
|
|
|
}
|
|
|
|
func (p *localPeer) PeerAddr() net.Addr {
|
2019-09-09 14:54:38 +00:00
|
|
|
return &p.netaddr
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
2020-01-15 14:03:42 +00:00
|
|
|
func (p *localPeer) StartProtocol() {}
|
2018-03-14 09:36:59 +00:00
|
|
|
func (p *localPeer) Disconnect(err error) {}
|
2020-01-16 18:16:31 +00:00
|
|
|
|
|
|
|
func (p *localPeer) EnqueueMessage(msg *Message) error {
|
|
|
|
b, err := msg.Bytes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return p.EnqueuePacket(b)
|
|
|
|
}
|
|
|
|
func (p *localPeer) EnqueuePacket(m []byte) error {
|
|
|
|
return p.EnqueueHPPacket(m)
|
|
|
|
}
|
2020-01-23 16:40:40 +00:00
|
|
|
func (p *localPeer) EnqueueP2PMessage(msg *Message) error {
|
|
|
|
return p.EnqueueMessage(msg)
|
|
|
|
}
|
|
|
|
func (p *localPeer) EnqueueP2PPacket(m []byte) error {
|
|
|
|
return p.EnqueueHPPacket(m)
|
|
|
|
}
|
2020-01-16 18:16:31 +00:00
|
|
|
func (p *localPeer) EnqueueHPPacket(m []byte) error {
|
|
|
|
msg := &Message{}
|
|
|
|
r := io.NewBinReaderFromBuf(m)
|
|
|
|
err := msg.Decode(r)
|
|
|
|
if err == nil {
|
|
|
|
p.messageHandler(p.t, msg)
|
|
|
|
}
|
2018-04-13 10:14:08 +00:00
|
|
|
return nil
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
func (p *localPeer) Version() *payload.Version {
|
|
|
|
return p.version
|
|
|
|
}
|
2020-01-17 10:17:19 +00:00
|
|
|
func (p *localPeer) LastBlockIndex() uint32 {
|
|
|
|
return p.lastBlockIndex
|
|
|
|
}
|
2019-09-13 12:43:22 +00:00
|
|
|
func (p *localPeer) HandleVersion(v *payload.Version) error {
|
2018-04-13 10:14:08 +00:00
|
|
|
p.version = v
|
2019-09-13 12:43:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-01-21 14:26:08 +00:00
|
|
|
func (p *localPeer) SendVersion() error {
|
|
|
|
m := p.server.getVersionMsg()
|
2020-01-16 18:16:31 +00:00
|
|
|
_ = p.EnqueueMessage(m)
|
|
|
|
return nil
|
2019-09-13 12:43:22 +00:00
|
|
|
}
|
|
|
|
func (p *localPeer) SendVersionAck(m *Message) error {
|
2020-01-16 18:16:31 +00:00
|
|
|
_ = p.EnqueueMessage(m)
|
|
|
|
return nil
|
2019-09-13 12:43:22 +00:00
|
|
|
}
|
|
|
|
func (p *localPeer) HandleVersionAck() error {
|
|
|
|
p.handshaked = true
|
|
|
|
return nil
|
|
|
|
}
|
2020-01-27 09:44:05 +00:00
|
|
|
func (p *localPeer) SendPing(m *Message) error {
|
2020-01-20 16:02:19 +00:00
|
|
|
p.pingSent++
|
2020-01-27 09:44:05 +00:00
|
|
|
_ = p.EnqueueMessage(m)
|
2020-01-20 16:02:19 +00:00
|
|
|
return nil
|
2020-01-17 10:17:19 +00:00
|
|
|
}
|
2020-01-20 16:02:19 +00:00
|
|
|
func (p *localPeer) HandlePong(pong *payload.Ping) error {
|
|
|
|
p.lastBlockIndex = pong.LastBlockIndex
|
|
|
|
p.pingSent--
|
|
|
|
return nil
|
2020-01-17 10:17:19 +00:00
|
|
|
}
|
2019-09-13 12:43:22 +00:00
|
|
|
|
|
|
|
func (p *localPeer) Handshaked() bool {
|
|
|
|
return p.handshaked
|
2018-04-13 10:14:08 +00:00
|
|
|
}
|
2018-03-14 09:36:59 +00:00
|
|
|
|
2019-12-30 07:43:05 +00:00
|
|
|
func newTestServer(t *testing.T) *Server {
|
2018-03-14 09:36:59 +00:00
|
|
|
return &Server{
|
2018-03-15 20:45:37 +00:00
|
|
|
ServerConfig: ServerConfig{},
|
2019-09-25 16:54:31 +00:00
|
|
|
chain: &testChain{},
|
2018-03-15 20:45:37 +00:00
|
|
|
transport: localTransport{},
|
|
|
|
discovery: testDiscovery{},
|
2019-08-23 15:19:07 +00:00
|
|
|
id: rand.Uint32(),
|
2018-03-15 20:45:37 +00:00
|
|
|
quit: make(chan struct{}),
|
|
|
|
register: make(chan Peer),
|
|
|
|
unregister: make(chan peerDrop),
|
|
|
|
peers: make(map[Peer]bool),
|
2019-12-30 07:43:05 +00:00
|
|
|
log: zaptest.NewLogger(t),
|
2018-03-14 09:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|