Skip to content

Commit c5293a3

Browse files
Merge pull request #29 from nguyenphuminh/big-rewrite
Big rewrite
2 parents 60aa324 + b6390dd commit c5293a3

25 files changed

+1553
-1335
lines changed

CONTRACT.md

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,40 @@ Before we dig in, we need to know that a contract's storage is a key-value objec
8585

8686
Arguments can be represented as `%0`, `%1`, `%2`,..., `%n`.
8787

88-
### Utils
88+
### Block data
8989

90-
* Print out a value: `log value`.
91-
* Store contract's balance into a variable: `balance var_name`.
9290
* Store block's timestamp into a variable: `timestamp var_name`.
93-
* Store sender's address into a variable: `address var_name`.
91+
* Store block's number into a variable: `blocknumber var_name`.
92+
* Store block's hash into a variable: `blockhash var_name`.
9493
* Store block's difficulty into a variable: `difficulty var_name`.
9594

95+
### Transaction data
96+
97+
* Store transaction's amount into a variable: `txvalue var_name`.
98+
* Store transaction's sender address into a variable: `txsender var_name`.
99+
* Store transaction's gas into a variable: `txgas var_name`.
100+
* Store transaction's contract execution gas into a variable: `txexecgas var_name`.
101+
102+
### Contract data
103+
104+
* Store contract's address into a variable: `address var_name`.
105+
* Store contract's balance into a variable: `selfbalance var_name`.
106+
107+
### Chain interactions
108+
109+
* Store address's balance into a variable: `balance var_name, address`.
110+
* Send Jem to an address: `send address, amount`.
111+
112+
### Others
113+
114+
* Print out a value: `log value`.
115+
* Generate SHA256 hash of a value and store into a variable: `sha256 var_name, value`.
116+
* Store remaining gas into a variable: `gas var_name`.
117+
* Stop execution: `stop`. (will not cost gas)
118+
96119
## Deploying a contract
97120

98-
A contract is attached to a transaction when deployed, so to deploy a contract, simply create a transaction, paste the contract's code into the `to` property, put `SC` at the beginning of the code to show that this is a smart contract's code and send the transaction away.
121+
A contract is attached to a transaction when deployed, so to deploy a contract, simply create a transaction, paste the contract's code into `<tx>.additionalData.scBody`, and then broadcast the transaction away.
99122

100123
```js
101124
const myContract = `
@@ -104,41 +127,37 @@ const myContract = `
104127
...
105128
`;
106129

107-
const transaction = new Transaction(publicKey, "SC" + myContract, amount, gas);
130+
const transaction = new Transaction(publicKey, "", amount, gas, {
131+
scBody: myContract;
132+
});
108133

109-
transaction.sign(keyPair);
134+
Transaction.sign(transaction, keyPair);
110135

111136
sendTransaction(transaction);
112137
```
113138

114139
## Triggering a contract
115140

116-
Just simply send a transaction to the contract address:
141+
Just simply send a transaction to the contract address, also adding the contract execution gas in `Transaction.additionalData.contractGas`:
117142
```js
118-
const transaction = new Transaction(publicKey, "some contract address", amount, gas);
143+
const transaction = new Transaction(publicKey, "some contract address", amount, gas, {
144+
contractGas: someAmount
145+
});
119146

120147
transaction.sign(keyPair);
121148

122149
sendTransaction(transaction);
123150
```
124151

125-
You can call the contract with arguments by passing in an additional array to the `Transaction` constructor:
126-
```js
127-
const transaction = new Transaction(publicKey, "some contract address", amount, gas, ["arg1", "arg2", "arg3"]);
128-
```
129-
130-
Note that all args are then stringified.
131-
132-
### Gas fee
133-
134-
To calculate gas fee, uses `calculateGasFee`:
152+
You can call the contract with arguments by passing in an additional array to `Transaction.additionalData.args`:
135153
```js
136-
const gas = calculateGasFee(contractAddress, args, from_optional);
154+
const transaction = new Transaction(publicKey, "some contract address", amount, gas, {
155+
contractGas: someAmount,
156+
args: [args, go, into, here]
157+
});
137158
```
138159

139-
If the gas provided in the transaction is not greater or equal to the gas calculated, the contract will not be triggered.
140-
141-
Note: This is not your transaction's gas fee, it is for paying the contract's fee, so you should be passing it into the transaction constructor as `amount`.
160+
Note that all args should be strings and are then stringified.
142161

143162
## Example
144163

README.md

Lines changed: 21 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -47,87 +47,39 @@ node keygen.js
4747

4848
And it will generate a public key and a private key for you.
4949

50-
### Sync chain
51-
52-
Currently, an "agreed" JeChain chain hasn't existed yet, so you can either create a new chain by skipping this whole section, or sync a chain from someone you happen to know that runs a JeChain node using `requestChain`.
50+
### Configure your node
5351

54-
In `./src/jenode.js`, at the bottom of the file, add:
52+
In `config.json`, change the props for your needs:
5553

5654
```js
57-
requestChain("Some JeChain node address");
55+
{
56+
"PORT": /*PORT that your node will run on, default is 3000*/,
57+
"RPC_PORT": /*PORT that the RPC server will run on, default is 5000*/,
58+
"PEERS": /*An array containing peers' address that the node will connect with, default is an empty array*/,
59+
"MY_ADDRESS": /*A string containing the node's address, default is "localhost:3000"*/,
60+
"PRIVATE_KEY": /*A string containing a private key*/,
61+
"ENABLE_MINING": /*Leave true if you want to mine, default is false*/
62+
"ENABLE_LOGGING": /*Leave true if you want to log out contract logs, default is false*/,
63+
"ENABLE_RPC": /*Leave true if you want to run a RPC server, default is false*/,
64+
"SYNC_FROM": /*A string containing an address to sync chain from*/,
65+
"ENABLE_CHAIN_REQUEST": /*Leave true if you want to sync chain from others, default is false*/
66+
}
5867
```
5968

60-
### Configure your node
61-
62-
In the terminal, follow this template:
69+
To see an example, `config.json` already has some data set for you to have a look at.
6370

64-
```sh
65-
# PORT=Server's port (e.g: 3000) (default is 3000)
66-
# PEERS=Addresses to connect to (e.g: ws://localhost:3001, ws://localhost:3002, ws://localhost:3003) (default is blank)
67-
# MY_ADDRESS=Server's address: ws://your.ip.and:port (e.g: ws://192.168.100.2:3004) (default is ws://localhost:3000)
68-
# PRIVATE_KEY=Your private key (default is a new randomly generated key)
69-
# ENABLE_MINING=true if you want to mine, skip otherwise (default is blank)
70-
# ENABLE_LOGGING=true if you want to log out contract messages, skip otherwise (default is blank)
71+
### Running the node
7172

72-
# ENABLE_RPC=true if you want to run an RPC server, skip otherwise (default is blank)
73-
# RPC_PORT=RPC server's port (e.g: 5000) (default is 5000)
74-
75-
# Start the node
76-
node .
77-
```
78-
79-
Use `set` on Windows to set variables.
73+
After everything is all set, simply type `node .` to run the node.
8074

8175
### Interacting with the node through JSON-RPC apis
8276

83-
(This will require you to run an RPC server).
84-
85-
To properly interact with the node, you should use the JSON-RPC apis, especially if you are creating dapps.
77+
This process will need you to run an RPC server, basically leave `true` in `ENABLE_RPC` in `config.json` to enable it.
8678

87-
[Check out docs for JSON-RPC APIs here.](./JSON-RPC.md)
79+
To properly interact with the node, you should use the JSON-RPC apis, especially if you are creating dapps. To get started, check out [docs for JSON-RPC APIs here.](./JSON-RPC.md)
8880

8981
**Note: This feature is still in its early stages, things might change when a stable release is ready.**
9082

91-
### Using the node manually through code:
92-
93-
You can also just use manual functions in `./src/jenode.js`
94-
95-
Mine a block:
96-
```js
97-
mine();
98-
```
99-
100-
Broadcast a transaction:
101-
```js
102-
sendTransaction(yourTransactionObject);
103-
```
104-
105-
To create a transaction object, use `Transaction`:
106-
107-
```js
108-
const tx = new Transaction(publicKey, "address to be sent to", amount, gas, [args_optional]);
109-
110-
// Sign the transaction
111-
tx.sign(keyPair);
112-
```
113-
114-
Request for a chain and its information from some address:
115-
```js
116-
requestChain("Some JeChain node address");
117-
```
118-
119-
If you just want to set up a node that mines continuously (like most people would), use `loopMine`:
120-
```js
121-
loopMine(optionalDelayTime);
122-
```
123-
124-
Note: `loopMine` is used by default when mining is enabled.
125-
126-
You can manually connect to a node using `connect`:
127-
```js
128-
connect("Some JeChain node address");
129-
```
130-
13183
### Run JeChain node publicly
13284

13385
Just do some port-forwarding, drop your public IP + the port you forwarded in and you are set!
@@ -163,15 +115,11 @@ Note that this is an experimental project which is still under development, and
163115

164116
## Todos
165117

118+
* Fix bugs.
119+
* Update chain sync.
166120
* Implement a proof of stake protocol.
167121
* Implement sharding.
168122
* Integrate EVM into the chain?
169-
* Use a proper database (preferably LevelDB).
170-
* Refactor codes, or rewrite in another language entirely, preferably Rust.
171-
* Port websocket to other p2p protocols.
172-
* Update missing documentation.
173-
174-
Full todo list can be seen here: https://github.com/nguyenphuminh/JeChain/projects/2
175123

176124

177125
## Support the project!

config.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"PORT": 5000,
3+
"RPC_PORT": 3000,
4+
"PEERS": [],
5+
"MY_ADDRESS": "ws://localhost:5000",
6+
"PRIVATE_KEY": "",
7+
"ENABLE_MINING": false,
8+
"ENABLE_LOGGING": false,
9+
"ENABLE_RPC": false,
10+
"SYNC_FROM": "",
11+
"ENABLE_CHAIN_REQUEST": false
12+
}

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const { startServer } = require("./src/node/server");
2+
const config = require("./config.json");
3+
4+
(async () => {
5+
await startServer(config);
6+
})();

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "jechain",
3-
"version": "0.13.1",
3+
"version": "0.14.0",
44
"description": "Node for JeChain - an experimental smart contract blockchain network",
5-
"main": "./src/jenode.js",
5+
"main": "./index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1"
88
},
@@ -11,6 +11,7 @@
1111
"dependencies": {
1212
"elliptic": "^6.5.4",
1313
"fastify": "^3.29.0",
14+
"level": "^8.0.0",
1415
"ws": "^8.2.3"
1516
}
1617
}

src/block.js

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)