Skip to content

Commit

Permalink
Added smart contract other restrictions
Browse files Browse the repository at this point in the history
  • Loading branch information
vasmohi committed Jun 23, 2024
1 parent 237c00a commit dfaf1d4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 8 deletions.
8 changes: 4 additions & 4 deletions docs/Architecture/Smart Contract/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

A blockchain platform works like a distributed database that stores all smart contracts. Each contract has a unique address used for state queries and updates. Methods in the contract code handle permission checks and logic.

## Smart Contract Parts in AElf
## Smart Contract Parts in aelf

1. **Interface**:
- Supports multiple languages.
Expand All @@ -16,13 +16,13 @@ A blockchain platform works like a distributed database that stores all smart co
- Protobuf plugins generate the smart contract skeleton.
- Developers fill in the logic by overriding methods.

Smart contracts in AElf are divided across the Kernel, the runtime, and the SDK. The Kernel handles core components and execution abstractions. Contracts rely on runtime modules and the SDK.
Smart contracts in aelf are divided across the Kernel, the runtime, and the SDK. The Kernel handles core components and execution abstractions. Contracts rely on runtime modules and the SDK.

A smart contract consists of methods that interact with state variables. Transactions trigger these methods to modify the blockchain state.

## Architecture Overview

AElf defines Smart Contracts as micro-services, making them language-independent. For example, the Consensus Protocol is a service defined by a smart contract.
aelf defines Smart Contracts as micro-services, making them language-independent. For example, the Consensus Protocol is a service defined by a smart contract.

![Smart Contract Architecture](../../_images/sc-as-service.png)

Expand Down Expand Up @@ -55,7 +55,7 @@ When a block’s transactions are executed, each transaction generates a trace c

## SDK

AElf has a native C# SDK for developing smart contracts in C#. It includes:
aelf has a native C# SDK for developing smart contracts in C#. It includes:
- Helpers to communicate with the bridge.
- Type infrastructure like `ContractState`, `MappedState`, and `SingletonState`.

Expand Down
2 changes: 1 addition & 1 deletion docs/Architecture/Smart Contract/event.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Event Option

Events in AElf smart contracts are used to represent occurrences during execution. These events are logged in the transaction traces.
Events in aelf smart contracts are used to represent occurrences during execution. These events are logged in the transaction traces.

Example of an event definition:

Expand Down
2 changes: 1 addition & 1 deletion docs/Architecture/Smart Contract/messages.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Smart Contract Messages

In AElf, we use protobuf messages to call smart contracts and serialize their state. Here's a simple example of a message definition:
In aelf, we use protobuf messages to call smart contracts and serialize their state. Here's a simple example of a message definition:

```cs
message CreateInput {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Other Restrictions

## GetHashCode Usage
The GetHashCode method can only be called within other GetHashCode methods. Calling GetHashCode from other methods is not permitted. This restriction allows developers to implement custom GetHashCode methods for their own types and also supports protobuf-generated message types.

It is prohibited to modify any field within GetHashCode methods.

## Execution Observer
aelf's contract patcher automatically includes a method call count observer for your contract. This feature prevents infinite method calls, such as recursion. During transaction execution, the observer counts the number of methods called. If this count exceeds 15,000, transaction execution pauses. Adjustments to this limit are managed by Parliament.

Additionally, aelf's contract patcher includes a method branch count observer for your contract. This prevents infinite loop scenarios by counting control transfers within your contract code during transaction execution. If the number of control transfers exceeds 15,000, transaction execution pauses. The control transfer opcodes in C# contracts are listed below.

| Opcode | Description |
|------------------|-----------------|
| OpCodes.Beq | Branch if equal |
| OpCodes.Beq_S | Branch if equal (short form) |
| OpCodes.Bge | Branch if greater than or equal |
| OpCodes.Bge_S | Branch if greater than or equal (short form) |
| OpCodes.Bge_Un | Branch if greater than or equal (unsigned) |
| OpCodes.Bge_Un_S | Branch if greater than or equal (unsigned, short form) |
| OpCodes.Bgt | Branch if greater than |
| OpCodes.Bgt_S | Branch if greater than (short form) |
| OpCodes.Ble | Branch if less than or equal |
| OpCodes.Ble_S | Branch if less than or equal (short form) |
| OpCodes.Ble_Un | Branch if less than or equal (unsigned) |
| OpCodes.Blt | Branch if less than |
| OpCodes.Bne_Un | Branch if not equal (unsigned) |
| OpCodes.Bne_Un_S | Branch if not equal (unsigned, short form) |
| OpCodes.Br | Branch unconditional |
| OpCodes.Brfalse | Branch if false |
| OpCodes.Brfalse_S| Branch if false (short form) |
| OpCodes.Brtrue | Branch if true |
| OpCodes.Brtrue_S | Branch if true (short form) |
| OpCodes.Br_S | Branch unconditional (short form) |

## State Size Limit
Data written to State is subject to a size limit enforced by aelf's contract patcher, set at 128 KB by default. This ensures contracts cannot store excessively large data. Any adjustments to this limit are decided by Parliament.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Project Properties

### Add Contract Proto File
Ensure to add a contract proto file in the contract directory of your project. This allows AElf’s contract patcher to process the DLL, enabling necessary injections for code checks during deployment. Without this, deployment will fail.
Ensure to add a contract proto file in the contract directory of your project. This allows aelf’s contract patcher to process the DLL, enabling necessary injections for code checks during deployment. Without this, deployment will fail.

```tree
src
Expand Down
2 changes: 1 addition & 1 deletion docs/Architecture/Smart Contract/service.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Smart Contract Service

When writing a smart contract in AElf, the first step is to define it so our tools can generate it. AElf contracts are defined and generated using gRPC and protobuf.
When writing a smart contract in aelf, the first step is to define it so our tools can generate it. aelf contracts are defined and generated using gRPC and protobuf.

## Example: Multi-Token Contract

Expand Down

0 comments on commit dfaf1d4

Please sign in to comment.