Object Database

Loading...
# 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)

Serpent contract for Ethereum

Object Database in Serpent c/o @qualiabyte

GitHub

Preferences

Note: Your changes won't be saved, because not Logged-in.

Create your own Contract