10.1.1.2. dcsim.sleepy package¶
10.1.1.2.1. Submodules¶
10.1.1.2.2. dcsim.sleepy.ChainQualityMeasurement module¶
-
class
dcsim.sleepy.ChainQualityMeasurement.
ChainQualityMeasurement
¶ Bases:
dcsim.framework.MeasurementBase.MeasurementBase
Initialize the MeasurementBase, incluing set the corrupted nodes, honest nodes, adversary Controller, te Configuration
Parameters: - corrupted_nodes – the corrupted nodes
- honest_nodes – the honest nodes
- adversary – the adversary controller is used
- config – the configuration is used
-
report_final
()¶
-
report_round
(round: int) → None¶
-
should_stop
(round) → bool¶
10.1.1.2.3. dcsim.sleepy.Configuration module¶
-
class
dcsim.sleepy.Configuration.
Configuration
(honest_node_type, adversary_controller_type, measurement_type, num_honest_nodes, num_corrupted_nodes, max_delay, confirm_time, probability, max_round)¶ Bases:
dcsim.framework.ConfigurationBase.ConfigurationBase
-
adversary_controller_type
¶
-
confirm_time
¶
-
honest_node_type
¶
-
max_delay
¶
-
max_round
¶
-
measurement_type
¶
-
num_corrupted_nodes
¶
-
num_honest_nodes
¶
-
probability
¶
-
10.1.1.2.4. dcsim.sleepy.ConsistencyAttack module¶
-
class
dcsim.sleepy.ConsistencyAttack.
BlockTree
(key) → None¶ Bases:
object
the blockchain of a node, which is actually a blocktree
initialze the blocktree with root of it
Parameters: - key – useless
- depth – length of main chain
- _blockPool – store all blocks of blocktree
- _main_chain – longest chain of blocktree
-
depth
¶ return the depth of the blocktree
Returns: the depth of the blocktree
-
insert
(cur: dcsim.sleepy.utils.TBlock)¶ insert a given TBlock into the blocktree
Parameters: cur – the inserted TBlock
-
main_chain
¶
-
class
dcsim.sleepy.ConsistencyAttack.
ConsistencyAttack
¶ Bases:
dcsim.framework.AdversaryControllerBase.AdversaryControllerBase
attack intends to break the consistency of blockchain
Initalize the Adversary Controller, set the config
Parameters: - config – Configuration of the protocol
- _chain – private chain
- _pending_messagees – the pending messages at each round
-
add_honest_node_messages
(round: int, sender_id: <function NewType.<locals>.new_type at 0x1087282f0>, messages_to_send: typing.List[_ForwardRef('MessageTuple')]) → None¶ add new messages from the honest nodes, delay all of them by delta rounds
Parameters: - round – the round that the messages are in
- sender_id – the id of the sender
- messages_to_send – A list that contains the new messages
-
get_delivered_messages
(round: int) → typing.List[_ForwardRef('MessageTuple')]¶ Get the delivered messages from all the nodes, returns a list contains all the messagetuples
Parameters: round – the round that these messages are in Returns: a list contains all the messagetuples
-
round_action
(round: int) → None¶ action of Adversary Controller in this round
-
set_trusted_third_party
(node_id: <function NewType.<locals>.new_type at 0x1087282f0>, trusted_third_party: dcsim.framework.TrustedThirdPartyCaller.TrustedThirdPartyCaller)¶
-
class
dcsim.sleepy.ConsistencyAttack.
TransactionPool
→ None¶ Bases:
object
Initialze the transactionpool
-
clear
()¶ clear all the transactions in transaction pool
-
contain_key
(tx: <function NewType.<locals>.new_type at 0x108979950>)¶ return whether a given transaction is in the pool
Parameters: tx – the given transaction Returns: whether a given transaction is in the pool
-
erase
(tx: <function NewType.<locals>.new_type at 0x108979950>)¶ delete a given trasaction from transaction pool
-
get_all
()¶ get all the transactions in transaction pool
Returns: A list contains all transactions in transaction pool
-
insert
(tx: <function NewType.<locals>.new_type at 0x108979950>)¶ insert a transaction into the transaction pool
Parameters: tx – the inserted transaction
-
-
dcsim.sleepy.ConsistencyAttack.
check
(id: int, timestamp: int, probability)¶ check whether a node is the leader at this round
Parameters: - id – the id of the node
- timestamp – the timestamp of this round
Returns: a boolean variable that decides whether this node is the leader at this round
-
dcsim.sleepy.ConsistencyAttack.
valid
(block: dcsim.sleepy.utils.TBlock, timestamp: int, probability)¶ decide whether a block is valid
Parameters: - block – the given block
- timestamp – the current timestamp
Returns: whether the block is valid
10.1.1.2.5. dcsim.sleepy.ConsistencyMeasurement module¶
-
class
dcsim.sleepy.ConsistencyMeasurement.
ConsistencyMeasurement
¶ Bases:
dcsim.framework.MeasurementBase.MeasurementBase
Initialize the MeasurementBase, incluing set the corrupted nodes, honest nodes, adversary Controller, te Configuration
Parameters: - corrupted_nodes – the corrupted nodes
- honest_nodes – the honest nodes
- adversary – the adversary controller is used
- config – the configuration is used
-
report_final
()¶ report the all the conditions and the result in the end
-
report_round
(round: int) → None¶ return the condition of each nodes ar this round
Parameters: round – the index of the round
-
should_stop
(round) → bool¶
10.1.1.2.6. dcsim.sleepy.HonestNode module¶
-
class
dcsim.sleepy.HonestNode.
HonestNode
¶ Bases:
dcsim.framework.NodeBase.NodeBase
Initilize the hoestnode
Parameters: config – the configuration of the node -
main_chain
¶ return the blockchain’s mainchain
Returns: the blockchain’s mainchain
-
recursive_add_block_from_orphan_pool
(curnode: dcsim.sleepy.utils.TNode)¶ recursively add the subtree(root has already been added and removed) of curnode to blockchain, and remove them from orphan pool
Parameters: curnode – the root of the subtree needed to be added from the orphan pool Returns: void
-
recursive_remove_block_from_orphan_pool
(block: dcsim.sleepy.utils.TBlock)¶ recursively remove the subtree(the root has already been removed) of the given block from the orphan pool
Parameters: block – the root of the subtree needed to be removed Returns: void
-
round_action
(ctx: dcsim.framework.Context.Context) → None¶ the round action of the honest node
Parameters: ctx – use the ctx to do inputs and outputs for a node Returns: none
-
set_trusted_third_party
(trusted_third_party: dcsim.framework.TrustedThirdPartyCaller.TrustedThirdPartyCaller)¶
-
10.1.1.2.7. dcsim.sleepy.SelfishMining module¶
-
class
dcsim.sleepy.SelfishMining.
SelfishMining
¶ Bases:
dcsim.framework.AdversaryControllerBase.AdversaryControllerBase
implementation of selfish mining, indends to affect the chain quality
Initialize Adversary Controller, set the config
Parameters: - config – Configuration of the protocol
- _root – blocktree
- _chain – private chain
- _tx – transaction pool
- _bad_nodes – corrupted nodes have private blocks
- _tmp_block – list of private blocks
-
add_honest_node_messages
(round: int, sender_id: <function NewType.<locals>.new_type at 0x1087282f0>, messages_to_send: typing.List[_ForwardRef('MessageTuple')]) → None¶ add new messages from the honest nodes, delay all of them by delta rounds
Parameters: - round – the round that the messages are in
- sender_id – the id of the sender
- messages_to_send – A list that contains the new messages
-
get_delivered_messages
(round: int) → typing.List[_ForwardRef('MessageTuple')]¶ Get the delivered messages from all the nodes, returns a list contains all the messagetuples
Parameters: round – the round that these messages are in Returns: a list contains all the messagetuples
-
main_chain
¶ Returns: main chain of blocktree
-
round_action
(round: int)¶ action of Adversary Controller in this round
-
set_trusted_third_party
(node_id: <function NewType.<locals>.new_type at 0x1087282f0>, trusted_third_party: dcsim.framework.TrustedThirdPartyCaller.TrustedThirdPartyCaller)¶
10.1.1.2.8. dcsim.sleepy.utils module¶
-
class
dcsim.sleepy.utils.
BlockChain
→ None¶ Bases:
object
initialize the blockchain
-
add_child
(t_node: dcsim.sleepy.utils.TNode, block: dcsim.sleepy.utils.TBlock)¶ add a child to after the given TNode
Parameters: - t_node – the node who want the this child
- block – the child to be added
Returns: the new TNode
-
find
(hash_val: <function NewType.<locals>.new_type at 0x1089799d8>) → typing.Union[_ForwardRef('TNode'), NoneType]¶ find a TNode whose hash equal to hash_val
Parameters: hash_val – the hash value given Returns: a TNode with given hash_val
-
get_top
() → dcsim.sleepy.utils.TNode¶
-
main_chain
¶ return TBlocks of mainchain
Returns: TBlocks of mainchain
-
-
class
dcsim.sleepy.utils.
OrphanBlockPool
→ None¶ Bases:
object
store the blocks that has been received but wait for the receipt of its previous block
initialize the orphan block pool
-
add_block
(ablock: dcsim.sleepy.utils.TBlock)¶ add a block to orphan pool
Parameters: ablock – block to be added
-
find
(hashval: <function NewType.<locals>.new_type at 0x1089799d8>) → typing.Union[typing.List[dcsim.sleepy.utils.TBlock], NoneType]¶ find the child whose hashvalue equal to the given hashvalue
Parameters: hashval – the given hash value Returns: the children whose hashvalue equal to the given hashvalue
-
pop_children
(hv: <function NewType.<locals>.new_type at 0x1089799d8>) → typing.Union[typing.List[dcsim.sleepy.utils.TBlock], NoneType]¶ remove the blocks whose father’s hashval is hv from orphan pool
Parameters: hv – the given hash value Returns: the removed blocks
-
-
class
dcsim.sleepy.utils.
TBlock
(pbhv: <function NewType.<locals>.new_type at 0x1089799d8>, txs: typing.List[<function NewType.<locals>.new_type at 0x108979950>], timestamp: <function NewType.<locals>.new_type at 0x108979a60>, pid: <function NewType.<locals>.new_type at 0x1087282f0>) → None¶ Bases:
object
Initialize the block of the transactions
Parameters: - pbhv – the hash of the previous block
- txs – the transactions contained in this block
- timestamp – the timestap of this block
- pid – the sender id of this block
-
hashval
¶ the hash value of this block
Returns:
-
id
¶ return the pid of the block
Returns: the pid of the block
-
round
¶ return the timestamp of this block
Returns: the timestamp of this block
-
serialize
¶ get pickle of the informations in this block
Returns: the pickle of all the informations in this block
-
class
dcsim.sleepy.utils.
TNode
(depth, block, father)¶ Bases:
object
Initialize the TNode
Parameters: - depth – the depth of the this TNode
- block – corresponding TBlock
- father – previous TNode
-
add_child
(new_node: dcsim.sleepy.utils.TNode) → bool¶ all the children to this node
Parameters: new_node – the children node to be added Returns: whether the addition succeed
-
get_child_index
(child_node: dcsim.sleepy.utils.TNode) → int¶ return the index of the child_node
Parameters: child_node – Returns: the index of child_node
-
get_children
()¶ get the children of this TNode
Returns: the chlldren of this TNode
-
search
(p_hash: <function NewType.<locals>.new_type at 0x1089799d8>) → typing.Union[_ForwardRef('TNode'), NoneType]¶ find a node in its subtree with hashval p_hash
Parameters: p_hash – the hash of the TNode to be searched Returns: the TNode found
-
transfer_chain
(i: int, j: int)¶ swap the order of two children
Parameters: - i – one node to be swap
- j – another node to be swap
-
class
dcsim.sleepy.utils.
TxPool
→ None¶ Bases:
object
store all received txs at the end of each round, all txs in the txpool form a new block, and clear it
initialize the transaction pool
-
add_tx
(tx: <function NewType.<locals>.new_type at 0x108979950>)¶ add a trasaction to the pool
Parameters: tx – the inserted transaction
-
clear_all
()¶ delete all the transactions
-
find_tx
(tx) → typing.Union[Tx, NoneType]¶ find a transaction in the pool
Parameters: tx – the given transaction Returns: the found transation or none if not found
-
get_all
()¶ get all the transactions
Returns: all the transactions
-
remove_tx
(tx) → bool¶ remove a given transaction
Parameters: tx – the transaction to be removed Returns: whether the removement is success or not
-
-
dcsim.sleepy.utils.
check_solution
(tblock: dcsim.sleepy.utils.TBlock, probability)¶ check whether the tblock is the leader
Parameters: tblock – the block to be checked Returns: whether the block satisfies the restriction
-
dcsim.sleepy.utils.
check_tx
(tx: <function NewType.<locals>.new_type at 0x108979950>)¶ check whether the transaction is none
Parameters: tx – the given transaction Returns: whether the transaction is none