File: test_domain.py

package info (click to toggle)
python-autobahn 23.1.2%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,424 kB
  • sloc: python: 38,620; javascript: 2,705; makefile: 899; ansic: 373; sh: 63
file content (105 lines) | stat: -rw-r--r-- 3,891 bytes parent folder | download | duplicates (2)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import sys
import argparse

import web3

from autobahn import xbr

from test_accounts import addr_owner, addr_alice_market, addr_alice_market_maker1, addr_bob_market, addr_bob_market_maker1, \
    addr_charlie_provider, addr_charlie_provider_delegate1, addr_donald_provider, addr_donald_provider_delegate1, \
    addr_edith_consumer, addr_edith_consumer_delegate1, addr_frank_consumer, addr_frank_consumer_delegate1

from test_accounts import hl


def main(accounts):
    for acct in [addr_alice_market, addr_bob_market, addr_charlie_provider, addr_donald_provider, addr_edith_consumer, addr_frank_consumer]:
        level = xbr.xbrnetwork.functions.getMemberLevel(acct).call()
        if not level:
            eula = 'QmU7Gizbre17x6V2VR1Q2GJEjz6m8S1bXmBtVxS2vmvb81'
            profile = ''

            xbr.xbrnetwork.functions.register(eula, profile).transact({'from': acct, 'gas': 200000})
            print('New member {} registered in the XBR Network (eula={}, profile={})'.format(hl(acct), eula, profile))
        else:
            eula = xbr.xbrnetwork.functions.getMemberEula(acct).call()
            profile = xbr.xbrnetwork.functions.getMemberProfile(acct).call()
            print('{} is already a member (level={}, eula={}, profile={})'.format(hl(acct), hl(level), eula, profile))

    DomainStatus_NULL = 0
    DomainStatus_ACTIVE = 1
    DomainStatus_CLOSED = 2

    NodeType_NULL = 0
    NodeType_MASTER = 1
    NodeType_CORE = 2
    NodeType_EDGE = 3

    NodeLicense_NULL = 0
    NodeLicense_INFINITE = 1
    NodeLicense_FREE = 2

    _domain_id = "0x9d9827822252fbe721d45224c7db7cac"
    _domain_key = "0xfeb083ce587a4ea72681d7db776452b05aaf58dc778534a6938313e4c85912f0"
    _license = ""
    _terms = ""
    _meta = ""

    status = xbr.xbrnetwork.functions.getDomainStatus(_domain_id).call()

    if status == DomainStatus_NULL:
        print('Domain does not exist - creating ..')
        xbr.xbrnetwork.functions.createDomain(_domain_id, _domain_key, _license, _terms, _meta).transact({'from': acct, 'gas': 200000})
        print('Domain created')
    elif status == DomainStatus_ACTIVE:
        print('Domain already exists')
    elif status == DomainStatus_CLOSED:
        print('FATAL: domain is closed')
        sys.exit(1)

    _pubkey = '0xf4050a787994fcca715103a83532785d79eaff3e42d18fd3f667fa2bb4af439e'
    _node_id = '0x4570160dd5be4726b2a785499609d6ab'
    _nodeType = NodeType_MASTER
    _nodeLicense = NodeLicense_FREE
    _config = ''

    node = xbr.xbrnetwork.functions.getNodeByKey(_pubkey).call()
    if node == b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00':
        print('Node not yet paired - pairing ..')
        xbr.xbrnetwork.functions.pairNode(_node_id, _domain_id, _nodeType, _nodeLicense, _pubkey, _config).transact({'from': acct, 'gas': 200000})
        print('Node paired!')
    else:
        print('Node already paired')


if __name__ == '__main__':
    print('using web3.py v{}'.format(web3.__version__))

    parser = argparse.ArgumentParser()

    parser.add_argument('--gateway',
                        dest='gateway',
                        type=str,
                        default=None,
                        help='Ethereum HTTP gateway URL or None for auto-select (default: -, means let web3 auto-select).')

    args = parser.parse_args()

    if args.gateway:
        w3 = web3.Web3(web3.Web3.HTTPProvider(args.gateway))
    else:
        # using automatic provider detection:
        from web3.auto import w3

    # check we are connected, and check network ID
    if not w3.isConnected():
        print('could not connect to Web3/Ethereum at: {}'.format(args.gateway or 'auto'))
        sys.exit(1)
    else:
        print('connected via provider "{}"'.format(args.gateway or 'auto'))

    # set new provider on XBR library
    xbr.setProvider(w3)

    # now enter main ..
    main(w3.eth.accounts)