Skip to content

Commit 601f34e

Browse files
committed
added hub subsystem
1 parent 0c14910 commit 601f34e

File tree

10 files changed

+345
-1
lines changed

10 files changed

+345
-1
lines changed

cmd/geth/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ var (
183183
utils.PrefetchCount,
184184
utils.LogFlag,
185185
utils.MaxTxsPerBlock,
186+
utils.Hub,
186187
}
187188
)
188189

cmd/geth/usage.go

+1
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ var AppHelpFlagGroups = []flagGroup{
250250
utils.PrefetchCount,
251251
utils.LogFlag,
252252
utils.MaxTxsPerBlock,
253+
utils.Hub,
253254
},
254255
},
255256
{

cmd/utils/flags.go

+8
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,11 @@ var (
690690
Usage: "Max # of transactions in a block",
691691
Value: params.MaxTxsPerBlock,
692692
}
693+
Hub = cli.StringFlag{
694+
Name: "hub",
695+
Usage: "Id of message hub",
696+
Value: params.Hub,
697+
}
693698
)
694699

695700
// MakeDataDir retrieves the currently requested data directory, terminating
@@ -1385,6 +1390,9 @@ func SetMetadiumConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
13851390
if ctx.GlobalIsSet(MaxTxsPerBlock.Name) {
13861391
params.MaxTxsPerBlock = ctx.GlobalInt(MaxTxsPerBlock.Name)
13871392
}
1393+
if ctx.GlobalIsSet(Hub.Name) {
1394+
params.Hub = ctx.GlobalString(Hub.Name)
1395+
}
13881396

13891397
if params.ConsensusMethod == params.ConsensusInvalid {
13901398
params.ConsensusMethod = params.ConsensusPoW

eth/handler.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ func (pm *ProtocolManager) BroadcastTxs(txs types.Transactions) {
998998

999999
// Broadcast transactions to a batch of peers not knowing about it
10001000
for _, tx := range txs {
1001-
peers := pm.peers.PeersWithoutTx(tx.Hash())
1001+
peers := pm.peers.PeersWithoutTx2(tx.Hash())
10021002
for _, peer := range peers {
10031003
txset[peer] = append(txset[peer], tx)
10041004
}

eth/peer.go

+38
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ import (
2828
"github.com/ethereum/go-ethereum/common/lru"
2929
"github.com/ethereum/go-ethereum/core/types"
3030
metaapi "github.com/ethereum/go-ethereum/metadium/api"
31+
metaminer "github.com/ethereum/go-ethereum/metadium/miner"
3132
"github.com/ethereum/go-ethereum/p2p"
33+
"github.com/ethereum/go-ethereum/params"
3234
"github.com/ethereum/go-ethereum/rlp"
3335
)
3436

@@ -542,6 +544,42 @@ func (ps *peerSet) PeersWithoutTx(hash common.Hash) []*peer {
542544
return list
543545
}
544546

547+
// hub subsystem
548+
var (
549+
isHub int = -1 // -1: unset, 1: hub, 0: not hub
550+
)
551+
552+
// PeersWithoutTx2 retrieves a list of peers that do not have a given transaction
553+
// in their set of known hashes.
554+
func (ps *peerSet) PeersWithoutTx2(hash common.Hash) []*peer {
555+
if !metaminer.AmPartner() {
556+
return ps.PeersWithoutTx(hash)
557+
}
558+
559+
if isHub == -1 {
560+
isHub = metaminer.AmHub(params.Hub)
561+
}
562+
563+
if isHub == 0 {
564+
// send it to the hub if it did not come from there
565+
ps.lock.RLock()
566+
for _, p := range ps.peers {
567+
if p.id == params.Hub {
568+
var list []*peer
569+
if !p.knownTxs.Exists(hash) {
570+
list = append(list, p)
571+
}
572+
ps.lock.RUnlock()
573+
return list
574+
}
575+
}
576+
ps.lock.RUnlock()
577+
}
578+
579+
// fall back
580+
return ps.PeersWithoutTx(hash)
581+
}
582+
545583
// BestPeer retrieves the known peer with the currently highest total difficulty.
546584
func (ps *peerSet) BestPeer() *peer {
547585
ps.lock.RLock()

metadium/admin.go

+16
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,21 @@ func IsPartner(id string) bool {
10431043
return true
10441044
}
10451045

1046+
// id is v4 id
1047+
func AmHub(id string) int {
1048+
if admin == nil || admin.self == nil {
1049+
return -1
1050+
}
1051+
1052+
admin.lock.Lock()
1053+
defer admin.lock.Unlock()
1054+
if strings.HasPrefix(strings.ToUpper(admin.self.Id), strings.ToUpper(id)) {
1055+
return 1
1056+
} else {
1057+
return 0
1058+
}
1059+
}
1060+
10461061
func (ma *metaAdmin) pendingEmpty() bool {
10471062
type txpool_status struct {
10481063
Pending hexutil.Uint `json:"pending"`
@@ -1425,6 +1440,7 @@ func init() {
14251440
metaminer.IsMinerFunc = IsMiner
14261441
metaminer.AmPartnerFunc = AmPartner
14271442
metaminer.IsPartnerFunc = IsPartner
1443+
metaminer.AmHubFunc = AmHub
14281444
metaminer.LogBlockFunc = LogBlock
14291445
metaminer.SuggestGasPriceFunc = suggestGasPrice
14301446
metaminer.CalculateRewardsFunc = calculateRewards

metadium/miner/miner.go

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var (
1414
IsMinerFunc func() bool
1515
AmPartnerFunc func() bool
1616
IsPartnerFunc func(string) bool
17+
AmHubFunc func(string) int
1718
LogBlockFunc func(int64, common.Hash)
1819
CalculateRewardsFunc func(*big.Int, *big.Int, *big.Int, func(common.Address, *big.Int)) (*common.Address, []byte, error)
1920
VerifyRewardsFunc func(*big.Int, string) error
@@ -48,6 +49,14 @@ func AmPartner() bool {
4849
}
4950
}
5051

52+
func AmHub(id string) int {
53+
if AmHubFunc == nil {
54+
return -1
55+
} else {
56+
return AmHubFunc(id)
57+
}
58+
}
59+
5160
func LogBlock(height int64, hash common.Hash) {
5261
if LogBlockFunc != nil {
5362
LogBlockFunc(height, hash)

metadium/scripts/gmet.sh

+1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ function start ()
175175

176176
OPTS="$COINBASE $DISCOVER $RPCOPT $BOOT_NODES $NONCE_LIMIT $TESTNET ${GMET_OPTS}"
177177
[ "$PORT" = "" ] || OPTS="${OPTS} --port $(($PORT + 1))"
178+
[ "$HUB" = "" ] || OPTS="${OPTS} --hub ${HUB}"
178179

179180
[ -d "$d/logs" ] || mkdir -p $d/logs
180181

0 commit comments

Comments
 (0)