Skip to content

Commit

Permalink
Merge branch 'master' into feature/genesis-contract-abi
Browse files Browse the repository at this point in the history
  • Loading branch information
zone117x committed Apr 3, 2020
2 parents 5df1ba2 + cd13956 commit e5a1f88
Show file tree
Hide file tree
Showing 43 changed files with 1,184 additions and 625 deletions.
16 changes: 8 additions & 8 deletions Cargo.toml
Expand Up @@ -29,10 +29,6 @@ path = "src/blockstack_cli.rs"
name = "marf_bench"
harness = false

[features]
developer-mode = []
default = ["developer-mode"]

[dependencies]
byteorder = "1.1"
rust-ini = "0.13"
Expand All @@ -46,6 +42,8 @@ mio = "0.6.16"
libc = "0.2"
lazy_static = "1.4.0"
toml = "0.5.6"
sha2 = { version = "0.8.0", optional = true }
sha2-asm = { version="0.5.3", optional = true }

[dependencies.serde_json]
version = "1.0"
Expand All @@ -67,14 +65,16 @@ features = ["serde"]
version = "=2.0.0"
features = ["serde"]

[dependencies.sha2]
version = "0.8.0"
features = ["asm"]

[dependencies.time]
version = "0.2.1"
features = ["std"]

[dev-dependencies]
assert-json-diff = "1.0.0"
criterion = "0.3"

[features]
developer-mode = []
asm = ["sha2", "sha2-asm"]
aarch64 = ["developer-mode", "sha2"]
default = ["developer-mode", "asm"]
15 changes: 15 additions & 0 deletions Dockerfile.memtest
@@ -0,0 +1,15 @@
FROM rust:latest

WORKDIR /src/blockstack-core

RUN apt-get update
RUN apt-get install valgrind heaptrack -y
RUN apt-get install less

RUN rustup install stable

COPY . .

RUN cargo test --no-run

CMD ["bash"]
37 changes: 22 additions & 15 deletions README.md
Expand Up @@ -6,32 +6,31 @@ Reference implementation of the [Blockstack Technical Whitepaper](https://blocks

## Repository

| Blockstack Topic/Tech | Where to learn more more |
|---------------------------------|------------------------------------------------------------------------------|
| Stacks 2.0 | [master branch](https://github.com/blockstack/stacks-blockchain/tree/master) |
| Stacks 1.0 | [legacy branch](https://github.com/blockstack/stacks-blockchain/tree/stacks-1.0) |
| Use the package | [our core docs](https://docs.blockstack.org/core/naming/introduction.html) |
| Develop a Blockstack App | [our developer docs](https://docs.blockstack.org/browser/hello-blockstack.html) |
| Use a Blockstack App | [our browser docs](https://docs.blockstack.org/browser/browser-introduction.html) |
| Blockstack the company | [our website](https://blockstack.org) |
| Blockstack Topic/Tech | Where to learn more more |
| ------------------------ | --------------------------------------------------------------------------------- |
| Stacks 2.0 | [master branch](https://github.com/blockstack/stacks-blockchain/tree/master) |
| Stacks 1.0 | [legacy branch](https://github.com/blockstack/stacks-blockchain/tree/stacks-1.0) |
| Use the package | [our core docs](https://docs.blockstack.org/core/naming/introduction.html) |
| Develop a Blockstack App | [our developer docs](https://docs.blockstack.org/browser/hello-blockstack.html) |
| Use a Blockstack App | [our browser docs](https://docs.blockstack.org/browser/browser-introduction.html) |
| Blockstack the company | [our website](https://blockstack.org) |

## Design Thesis

Stacks 2.0 is an open-membership replicated state machine produced by the coordination of a non-enumerable set of peers.

To unpack this definition:

- A replicated state machine is two or more copies (“replicas”) of a given set of rules (a “machine”) that, in processing a common input (such as the same sequence of transactions), will arrive at the same configuration (“state”). Bitcoin is a replicated state machine — its state is the set of UTXOs, which each peer has a full copy of, and given a block, all peers will independently calculate the same new UTXO set from the existing one.
- A replicated state machine is two or more copies (“replicas”) of a given set of rules (a “machine”) that, in processing a common input (such as the same sequence of transactions), will arrive at the same configuration (“state”). Bitcoin is a replicated state machine — its state is the set of UTXOs, which each peer has a full copy of, and given a block, all peers will independently calculate the same new UTXO set from the existing one.
- Open-membership means that any host on the Internet can join the blockchain and independently calculate the same full replica as all other peers.
- Non-enumerable means that the set of peers that are producing the blocks don’t know about one another — they don’t know their identities, or even how many exist and are online. They are indistinguishable.
- Non-enumerable means that the set of peers that are producing the blocks don’t know about one another — they don’t know their identities, or even how many exist and are online. They are indistinguishable.

## Roadmap

- [x] [SIP 001: Burn Election](https://github.com/blockstack/stacks-blockchain/blob/master/sip/sip-001-burn-election.md)
- [x] [SIP 002: Clarity, a language for predictable smart contracts](https://github.com/blockstack/stacks-blockchain/blob/master/sip/sip-002-smart-contract-language.md)
- [x] [SIP 004: Cryptographic Committment to Materialized Views](https://github.com/blockstack/stacks-blockchain/blob/master/sip/sip-004-materialized-view.md)
- [x] [SIP 005: Blocks, Transactions, and Accounts](https://github.com/blockstack/stacks-blockchain/blob/master/sip/sip-005-blocks-and-transactions.md
)
- [x] [SIP 005: Blocks, Transactions, and Accounts](https://github.com/blockstack/stacks-blockchain/blob/master/sip/sip-005-blocks-and-transactions.md)
- [ ] [SIP 003: Peer Network](https://github.com/blockstack/stacks-blockchain/blob/master/sip/sip-003-peer-network.md) (Q1 2020)
- [ ] SIP 006: Clarity Execution Cost Assessment (Q1 2020)

Expand Down Expand Up @@ -71,7 +70,13 @@ Then build the project:
cargo build
```

And run the tests:
Building the project on ARM:

```bash
cargo build --features "aarch64" --no-default-features
```

Run the tests:

```bash
cargo test testnet -- --test-threads=1
Expand Down Expand Up @@ -213,17 +218,19 @@ Congratulations, you can now [write your own smart contracts with Clarity](https

Beyond this Github project,
Blockstack maintains a public [forum](https://forum.blockstack.org) and an
opened [Discord](https://discordapp.com/invite/9r94Xkj) channel. In addition, the project
opened [Discord](https://discordapp.com/invite/9r94Xkj) channel. In addition, the project
maintains a [mailing list](https://blockstack.org/signup) which sends out
community announcements.

The greater Blockstack community regularly hosts in-person
[meetups](https://www.meetup.com/topics/blockstack/). The project's
[meetups](https://www.meetup.com/topics/blockstack/). The project's
[YouTube channel](https://www.youtube.com/channel/UC3J2iHnyt2JtOvtGVf_jpHQ) includes
videos from some of these meetups, as well as video tutorials to help new
users get started and help developers wrap their heads around the system's
design.

For help cross-compiling on memory-constrained devices, please see the community supported documentation here: [Cross Compiling](https://github.com/dantrevino/cross-compiling-stacks-blockchain/blob/master/README.md).

## Further Reading

You can learn more by visiting [the Blockstack Website](https://blockstack.org) and checking out the in-depth articles and documentation:
Expand Down
4 changes: 4 additions & 0 deletions circle.yml
Expand Up @@ -37,6 +37,10 @@ jobs:
working_directory: ~/blockstack
steps:
- checkout
- run:
name: Install perftools
command: |
sudo apt-get update -y && sudo apt-get install libgoogle-perftools4 -y
- run:
name: Fetch latest grcov
command: |
Expand Down
85 changes: 85 additions & 0 deletions sip/sip-006-runtime-cost-assessment.md
Expand Up @@ -23,6 +23,9 @@ constants will necessarily be measured via benchmark harnesses and
regression analyses. Furthermore, the _analysis_ cost associated with
this code will not be covered by this proposal.

This document also describes the memory limit imposed during contract
execution, and the memory model for enforcing that limit.

# Measurements for Execution Cost

Execution cost of a block of Clarity code is broken into 5 categories:
Expand Down Expand Up @@ -661,3 +664,85 @@ hashed, the longer the hashing function takes.

where X is the size of the input.


# Memory Model and Limits

Clarity contract execution imposes a maximum memory usage limit for applications.
For any given Clarity value, the memory usage of that value is counted using
the _size_ of the Clarity value.

Memory is consumed by the following variable bindings:

* `let` - each value bound in the `let` consumes that amount of memory
during the execution of the `let` block.
* `match` - the bound value in a `match` statement consumes memory during
the execution of the `match` branch.
* function arguments - each bound value consumes memory during the execution
of the function. this includes user-defined functions _as well as_ native
functions.

Additionally, functions that perform _context changes_ also consume memory,
though they consume a constant amount:

* `as-contract`
* `at-block`

## Type signature size

Types in Clarity may be described using type signatures. For example,
`(tuple (a int) (b int))` describes a tuple with two keys `a` and `b`
of type `int`. These type descriptions are used by the Clarity analysis
passes to check the type correctness of Clarity code. Clarity type signatures
have varying size, e.g., the signature `int` is smaller than the signature for a
list of integers.

The size of a Clarity value is defined as follows:

```
type_size(x) :=
if x =
int => 16
uint => 16
bool => 1
principal => 148
(buff y) => 4 + y
(some y) => 1 + size(y)
(ok y) => 1 + size(y)
(err y) => 1 + size(y)
(list ...) => 4 + sum(size(z) for z in list)
(tuple ...) => 1 + 2*(count(entries))
+ sum(size(z) for each value z in tuple)
```

## Contract Memory Consumption

Contract execution requires loading the contract's program state in
memory. That program state counts towards the memory limit when
executed via a programmatic `contract-call!` or invoked by a
contract-call transaction.

The memory consumed by a contract is equal to:

```
a + b*contract_length + sum(size(x) for each constant x defined in the contract)
```

That is, a contract consumes memory which is linear in the contract's
length _plus_ the amount of memory consumed by any constants defined
using `define-constant`.

## Database Writes

While data stored in the database itself does _not_ count against the
memory limit, supporting public function abort/commit behavior requires
holding a write log in memory during the processing of a transaction.

Operations that write data to the data store therefore consume memory
_until the transaction completes_, and the write log is written to the
database. The amount of memory consumed by operations on persisted data
types is defined as:

* `data-var`: the size of the stored data var's value.
* `map`: the size of stored key + the size of the stored value.
* `nft`: the size of the NFT key
* `ft`: the size of a Clarity uint value.
6 changes: 3 additions & 3 deletions src/blockstack_cli.rs
Expand Up @@ -343,9 +343,9 @@ fn generate_secret_key(args: &[String], version: TransactionVersion) -> Result<S
version, &AddressHashMode::SerializeP2PKH, 1, &vec![pk.clone()])
.expect("Failed to generate address from public key");
Ok(format!("{{
secretKey: \"{}\",
publicKey: \"{}\",
stacksAddress: \"{}\"
\"secretKey\": \"{}\",
\"publicKey\": \"{}\",
\"stacksAddress\": \"{}\"
}}",
sk.to_hex(),
pk.to_hex(),
Expand Down
5 changes: 2 additions & 3 deletions src/burnchains/mod.rs
Expand Up @@ -694,10 +694,9 @@ pub mod test {

let last_snapshot_with_sortition = match parent_block_snapshot {
Some(sn) => sn.clone(),
None => BurnDB::get_last_snapshot_with_sortition(tx, self.block_height - 1, &self.parent_snapshot.burn_header_hash)
.expect("FATAL: failed to read last snapshot with sortition")
None => BurnDB::get_first_block_snapshot(tx).unwrap()
};

// prove on the last-ever sortition's hash to produce the new seed
let proof = miner.make_proof(&leader_key.public_key, &last_snapshot.sortition_hash)
.expect(&format!("FATAL: no private key for {}", leader_key.public_key.to_hex()));
Expand Down

0 comments on commit e5a1f88

Please sign in to comment.