-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobjectdb.se
123 lines (109 loc) · 3.24 KB
/
objectdb.se
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# ObjectDB
#
# An object database contract for Ethereum
#
# Overview
#
# In public mode, anyone can add objects to the database.
# The first account to add an object becomes its owner.
# Only the object's owner can modify its properties.
#
# In private mode, only the database parent may add objects.
# Thus, only they will own (and be able to modify) objects.
#
# By default, public mode is enabled.
# In either mode, any account can read object properties.
#
# API
#
# "add" <id> # Add empty object (20-byte id)
# "get" <id> <key> # Get object property (12-byte key)
# "set" <id> <key> <value> # Set object property (32-byte value)
# "config" "public" <bool> # Configure mode (1 for public, 0 for private)
# "config" "parent" <address> # Configure parent (Defaults to the creator)
# "kill" # Kill the database (Parent only)
#
shared:
# Modes
PRIVATE_MODE = 0
PUBLIC_MODE = 1
DEFAULT_MODE = PUBLIC_MODE
# Byte lengths
ID_SIZE = 20
KEY_SIZE = 32 - ID_SIZE
# Numeric value limits
MIN_ID = 0
MIN_KEY = 1
MAX_ID = 256^ID_SIZE - 1
MAX_KEY = 256^KEY_SIZE - 1
# Commands
ADD = "add" * 256^(32 - 3)
GET = "get" * 256^(32 - 3)
SET = "set" * 256^(32 - 3)
CONFIG = "config" * 256^(32 - 6)
KILL = "kill" * 256^(32 - 4)
# Config keys
CONFIG_PUBLIC = "public" * 256^(32 - 6)
CONFIG_PARENT = "parent" * 256^(32 - 6)
# Namereg
NAME = "ObjectDB" * 256^(32 - 8)
NAMEREG = 0x50441127ea5b9dfd835a9aba4e1dc9c1257b58ca
REGISTER = "register" * 256^(32 - 8)
UNREGISTER = "unregister" * 256^(32 - 10)
# Storage
PARENT = 0
PUBLIC = 1
init:
call(NAMEREG, [REGISTER, NAME], 2)
contract.storage[PARENT] = msg.sender
contract.storage[PUBLIC] = DEFAULT_MODE
code:
parent = contract.storage[PARENT]
public = contract.storage[PUBLIC]
caller = msg.sender
command = msg.data[0]
id = msg.data[1]
key = msg.data[2]
value = msg.data[3]
ID = id * 256^KEY_SIZE
if command == ADD:
if !public and caller != parent:
return(0)
if contract.storage[ID] != 0:
return(1)
if id < MIN_ID:
return(2)
if id > MAX_ID:
return(3)
contract.storage[ID] = msg.sender
elif command == GET:
pos = ID + key
value = contract.storage[pos]
return(value)
elif command == SET:
owner = contract.storage[ID]
if !public and caller != parent:
return(0)
if caller != owner:
return(1)
if key < MIN_KEY:
return(2)
if key > MAX_KEY:
return(3)
pos = ID + key
contract.storage[pos] = value
elif command == CONFIG:
key = msg.data[1]
value = msg.data[2]
if caller != parent:
return(1)
if key == CONFIG_PUBLIC
mode = value
contract.storage[PUBLIC] = mode
if key == CONFIG_PARENT
address = value
contract.storage[PARENT] = address
elif command == KILL:
if caller == parent:
call(NAMEREG, UNREGISTER)
suicide(parent)