Skip to content

Commit

Permalink
Merge pull request #117 from wonkr/ethereum-redesign
Browse files Browse the repository at this point in the history
Ethereum redesign
  • Loading branch information
wonkr authored Jan 18, 2023
2 parents fc31b3f + 24f4ec3 commit b4b68fa
Show file tree
Hide file tree
Showing 11 changed files with 91 additions and 1,474 deletions.
25 changes: 21 additions & 4 deletions examples/A20-nano-internet/nano-internet.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# encoding: utf-8

from seedemu import *
import os


# Create the Emulator
Expand Down Expand Up @@ -67,6 +68,8 @@
as152.createNetwork('net0')
as152.createRouter('router0').joinNetwork('net0').joinNetwork('ix101')
as152.createHost('host0').joinNetwork('net0')
as152.createHost('host1').joinNetwork('net0')
as152.createHost('host2').joinNetwork('net0')

# Install additional software on a host
as152.getHost('host0').addSoftware('telnet')
Expand Down Expand Up @@ -103,14 +106,14 @@
web = WebService()

# Create web service nodes (virtual nodes)
web.install('web01')
web.install('web02')
# add Class label to the Conatiner (please refer README.md for further information.)
web01 = web.install('web01').appendClassName("SEEDWeb")
web02 = web.install('web02').appendClassName("SyrWeb")

# Bind the virtual nodes to physical nodes
emu.addBinding(Binding('web01', filter = Filter(nodeName = 'host0', asn = 151)))
emu.addBinding(Binding('web02', filter = Filter(nodeName = 'host0', asn = 152)))


###############################################################################

emu.addLayer(base)
Expand Down Expand Up @@ -141,8 +144,22 @@
###############################################################################
# Compilation

docker = Docker()

# Use the "handsonsecurity/seed-ubuntu:small" custom image from dockerhub
docker.addImage(DockerImage('handsonsecurity/seed-ubuntu:small', [], local = False), priority=-1)
docker.setImageOverride(as152.getHost('host1'), 'handsonsecurity/seed-ubuntu:small')

# Use the "seed-ubuntu-large" custom image from local
docker.addImage(DockerImage('seed-ubuntu-large', [], local = True), priority=-1)
docker.setImageOverride(as152.getHost('host2'), 'seed-ubuntu-large')

# Generate the Docker files
emu.compile(Docker(), './output')
emu.compile(docker, './output')

# Copy the base container image to the output folder
# the base container image should be located under the ouput folder to add it as custom image.
os.system('cp -r seed-ubuntu-large ./output')

# Generate other type of outputs
#emu.compile(Graphviz(), './others/graphs')
Expand Down
13 changes: 13 additions & 0 deletions examples/B06-blockchain/blockchain-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ def test_poa_emulator_account(self):
for account in accounts:
self.assertTrue(self.wallet2._web3.eth.getBalance(account.address) >= 32*EthUnit.ETHER.value)

def test_pow_create_account(self):
account = EthAccount.createEmulatorAccountFromMnemonic(3, mnemonic="great awesome fun seed security lab protect system network prevent attack future", balance=20*EthUnit.ETHER.value, index=1, password="admin")
self.assertTrue(self.wallet1._web3.eth.getBalance(account.address) >= 20*EthUnit.ETHER.value)

def test_pow_create_accounts(self):
accounts = []
for index in range(1, 4):
accounts.append(EthAccount.createEmulatorAccountFromMnemonic(7, mnemonic="great awesome fun seed security lab protect system network prevent attack future", balance=30*EthUnit.ETHER.value, index=index, password="admin"))

for account in accounts:
self.assertTrue(self.wallet2._web3.eth.getBalance(account.address) >= 30*EthUnit.ETHER.value)


if __name__ == "__main__":

test_suite = ut.TestLoader().loadTestsFromTestCase(MultipleChainsTestCase)
Expand Down
17 changes: 5 additions & 12 deletions examples/B06-blockchain/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,11 @@
e5.setBootNode(True).unlockAccounts().startMiner()
e6.unlockAccounts().startMiner()

# ?? Initially accounts was created randomly. Now we have change it to create from mnemonic words.
# Should we need to keep this api? As of now, blockchain layer will manage how many accounts with how much balance to create. This is uniformly same in every node.
# Suggested API - remove createAccount and modify createAccounts as below. In each server, they can only set values of balance and total.
# Thus, all the nodes in the same chain should use the same mnemonic words, which can be reset in blockchain layer.
# And balance and total amount of accounts can be vary per server(node).
# EthereumServer::createAccounts(balance, total)

# # Create more accounts with Balance on e3 and e7
# # Create one account with createAccount() method
# # Create multiple accounts with createAccounts() method
# e3.createAccount(balance= 32 * pow(10,18), password="admin").unlockAccounts()
# e7.createAccounts(3, balance = 32*pow(10,18), password="admin")
# Create more accounts with Balance on e3 and e7
# Create one account with createAccount() method
# Create multiple accounts with createAccounts() method
e3.createAccount(balance=20, unit=EthUnit.ETHER, password="admin").unlockAccounts()
e7.createAccounts(total=3, balance=30, unit=EthUnit.ETHER, password="admin")

# Import account with balance 0 on e2
e2.importAccount(keyfilePath='./resources/keyfile_to_import', password="admin", balance=10)
Expand Down
2 changes: 1 addition & 1 deletion examples/B08-Remix-Connection/blockchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@

# If output directory exists and override is set to false, we call exit(1)
# updateOutputdirectory will not be called
emu.compile(Docker(clientEnabled=True), './output')
emu.compile(Docker(mapClientEnabled=True), './output')
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@
emu.render()

# Compile the emulation
emu.compile(Docker(clientEnabled = True), './output', override=True)
emu.compile(Docker(mapClientEnabled = True), './output', override=True)
3 changes: 1 addition & 2 deletions examples/C04-ethereum-pos/blockchain-pos.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def makeStubAs(emu: Emulator, base: Base, asn: int, exchange: int, hosts_total:
ebgp.addPrivatePeerings(104, [12], [164], PeerRelationship.Provider)

eth = EthereumService()
docker = Docker(clientEnabled=True)
docker = Docker(mapClientEnabled=True)
asns = [150, 151, 152, 153, 154, 160, 161, 162, 163, 164]

TERMINAL_TOTAL_DIFFICULTY=20
Expand All @@ -138,7 +138,6 @@ def makeStubAs(emu: Emulator, base: Base, asn: int, exchange: int, hosts_total:
# enable PoS
e.enablePoS(TERMINAL_TOTAL_DIFFICULTY)
e.setBeaconPeerCounts(10)
e.setGasLimitPerBlock(210000)

if asn == 150:
if id == 0:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,4 @@
###############################################################################
# Compilation

emu.compile(Docker(clientEnabled = True), './output')
emu.compile(Docker(mapClientEnabled = True), './output')
24 changes: 13 additions & 11 deletions seedemu/compiler/Docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,8 @@ class Docker(Compiler):
__self_managed_network: bool
__dummy_network_pool: Generator[IPv4Network, None, None]

__client_enabled: bool
__client_port: int
__map_client_enabled: bool
__map_client_port: int

__client_hide_svcnet: bool

Expand All @@ -298,8 +298,8 @@ def __init__(
selfManagedNetwork: bool = False,
dummyNetworksPool: str = '10.128.0.0/9',
dummyNetworksMask: int = 24,
clientEnabled: bool = False,
clientPort: int = 8080,
mapClientEnabled: bool = False,
mapClientPort: int = 8080,
clientHideServiceNet: bool = True
):
"""!
Expand Down Expand Up @@ -337,8 +337,8 @@ def __init__(
self.__self_managed_network = selfManagedNetwork
self.__dummy_network_pool = IPv4Network(dummyNetworksPool).subnets(new_prefix = dummyNetworksMask)

self.__client_enabled = clientEnabled
self.__client_port = clientPort
self.__map_client_enabled = mapClientEnabled
self.__map_client_port = mapClientPort

self.__client_hide_svcnet = clientHideServiceNet

Expand All @@ -349,12 +349,12 @@ def __init__(
self.__image_per_node_list = {}

for image in DefaultImages:
self.addImage(image)
self.addImage(image, priority=0)

def getName(self) -> str:
return "Docker"

def addImage(self, image: DockerImage, priority: int = 0) -> Docker:
def addImage(self, image: DockerImage, priority: int = -1) -> Docker:
"""!
@brief add an candidate image to the compiler.
Expand All @@ -363,7 +363,9 @@ def addImage(self, image: DockerImage, priority: int = 0) -> Docker:
images with same number of missing software exist. The one with highest
priority wins. If two or more images with same priority and same number
of missing software exist, the one added the last will be used. All
built-in images has priority of 0. Default to 0.
built-in images has priority of 0. Default to -1. All built-in images are
prior to the added candidate image. To set a candidate image to a node,
use setImageOverride() method.
@returns self, for chaining api calls.
"""
Expand Down Expand Up @@ -982,12 +984,12 @@ def _doCompile(self, emulator: Emulator):
self._log('compiling service node {}...'.format(name))
self.__services += self._compileNode(obj)

if self.__client_enabled:
if self.__map_client_enabled:
self._log('enabling seedemu-client...')

self.__services += DockerCompilerFileTemplates['seedemu_client'].format(
clientImage = SEEDEMU_CLIENT_IMAGE,
clientPort = self.__client_port
clientPort = self.__map_client_port
)

local_images = ''
Expand Down
Loading

0 comments on commit b4b68fa

Please sign in to comment.