recovery_p2p.go 879 字节
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
package recovery

import(
	// "fmt"
	// "bytes"

	"github.com/ethereum/go-ethereum/common"
)

// P2PLocation is the address of the p2p network
type P2PLocation common.Address

// P2PNetwork interface defines the P2P operations and implements mock for test
type P2PNetwork interface {
	RetrieveData(peer P2PLocation, msgByte []byte) error
}

// P2PMock mocks a p2p network for test
type P2PMock struct{
	network map[P2PLocation][]byte
}

// RetrieveData get data from p2p network address
func (mock P2PMock) RetrieveData(peer P2PLocation, msgByte []byte) error {
	copy(msgByte, mock.network[peer])
	return nil
}

// InititalP2PMock initializes P2P mock module
func InititalP2PMock(peers []P2PLocation, dataBlks [][]byte) (P2PNetwork, error) {
	p2p := P2PMock{
		map[P2PLocation][]byte{},
	}

	for i:=0;i<len(peers);i++{
		p2p.network[peers[i]]=dataBlks[i]
	}
	return p2p, nil
}