-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMakefile
252 lines (216 loc) · 8.26 KB
/
Makefile
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#
# A simple format for inline Makefile documentation: https://stackoverflow.com/questions/8889035/how-to-document-a-makefile
#
##
## This Makefile builds and tests the Distrotron payment contract.
##
## -----------------------------
##
## Configurable variables for use with sandbox testing, all optional:
##
## NEARD_HOME: Home directory of neard state (including root key); shared btwn client and server
NEARD_HOME := $${HOME}/.near
## NEARD_KEY_NAME: Filename of root key generated by neard in NEARD_HOME, used by near-api-js for testing.
NEARD_KEY_NAME := validator_key.json
## NEARD_KEY: Complete path to root key.
NEARD_KEY := ${NEARD_HOME}/${NEARD_KEY_NAME}
## NEARCORE: Path to the NEAR daemon repo for local sandbox testing
NEARCORE := ./nearcore
##
## Example:
## make NEARD_HOME=~/.near-testing sandboxtest
##
#
# It's considered bad form to use environment vars in Makefiles,
# but this one already controls the near-sdk-js (node) environment, which we want to stay in sync with.
NEAR_ENV ?= localnet
## -----------------------------
##
## Valid targets:
##
help: ## Show this help.
@sed -ne '/@sed/!s/## //p' $(MAKEFILE_LIST)
##
#################
TARGET_RELEASE := $(BUILD_DIR)/release/distrotron.wasm
BUILD_DIR := target/wasm32-unknown-unknown
#
# Build targets:
#
$(BUILD_DIR)/release/distrotron.wasm: $(wildcard distrotron/src/*.rs) Cargo.toml distrotron/Cargo.toml
cargo build --target wasm32-unknown-unknown --release -p distrotron
$(BUILD_DIR)/debug/distrotron.wasm: $(wildcard distrotron/src/*.rs) Cargo.toml distrotron/Cargo.toml
cargo build --target wasm32-unknown-unknown -p distrotron
$(BUILD_DIR)/debug/stub.wasm: $(wildcard stub/src/*.rs) Cargo.toml stub/Cargo.toml
cargo build --target wasm32-unknown-unknown -p stub
$(BUILD_DIR)/release/stub.wasm: $(wildcard stub/src/*.rs) Cargo.toml stub/Cargo.toml
cargo build --target wasm32-unknown-unknown --release -p stub
## release: build distrotron
release: $(BUILD_DIR)/release/distrotron.wasm
# reduce contract size before deploying:
npx wasm-opt -Oz --strip-debug -o $(BUILD_DIR)/release/distrotron-small.wasm $(BUILD_DIR)/release/distrotron.wasm
## debug: build distrotron (debug version)
debug: $(BUILD_DIR)/debug/distrotron.wasm
## stub_release: build stub contract
stub_release: $(BUILD_DIR)/release/stub.wasm
stub_debug: $(BUILD_DIR)/debug/stub.wasm
############
# Unit test targets:
##
## unittest: build and unit-test the contracts
unittest: unittest_distro unittest_stub
## unittest_distro: build and unit-test distrotron
unittest_distro: release debug
cargo test -p distrotron -- --nocapture
## unittest_stub: build and unit-test the stub
unittest_stub: stub_release stub_debug
cargo test -p stub -- --nocapture
#########
#
# Sandbox targets:
#
# These local_sandbox targets can fetch, build, configure and run a local neard instance
# for sandbox tests.
#
##
## local_sandbox_start: (Slowly) download & build neard, then run it in the local terminal
local_sandbox_start: local_sandbox local_sandbox_init
cd ${NEARCORE}; cargo run --profile quick-release -p neard -- --home ${NEARD_HOME} run
local_sandbox: local_sandbox_repo ${NEARCORE}/target/quick-release/neard
local_sandbox_repo: ${NEARCORE}
${NEARCORE}:
echo "cloning nearcore:"
git clone https://github.com/near/nearcore ${NEARCORE}
${NEARCORE}/target/quick-release/neard:
cd ${NEARCORE}; cargo build --profile quick-release -p neard
local_sandbox_init: local_sandbox
rm -r ${NEARD_HOME}; \
mkdir ${NEARD_HOME}; \
cd ${NEARCORE}; cargo run --profile quick-release -p neard -- --home ${NEARD_HOME} init
############
#
# These targets can fetch, launch, configure and run a Docker image for sandbox tests.
# The image, nearprotocol/nearcore, is published by NEAR devs, and is compiled with
# AVX extensions that will not work on an M1 macintosh.
#
##
## docker_sandbox_start: Fetch and run a Docker neard image in the foreground.
# (neard will block all quit/stop/kill signals! To stop it, make the "docker_sandbox_stop" target in a different window.)
docker_sandbox_start:
docker start distrotron_docker_sandbox || \
mkdir ${NEARD_HOME}; \
docker run -v ${NEARD_HOME}:/root/.near -p 3030:3030 --name distrotron_docker_sandbox nearprotocol/nearcore:latest \
/bin/bash -c "neard init; chmod 644 /root/.near/${NEARD_KEY_NAME}; neard run"
## docker_sandbox_start_bg: Fetch and run a Docker neard image in the background.
docker_sandbox_start_bg:
docker start distrotron_docker_sandbox || \
mkdir ${NEARD_HOME}; \
docker run -v ${NEARD_HOME}:/root/.near -d -p 3030:3030 --name distrotron_docker_sandbox nearprotocol/nearcore:latest \
/bin/bash -c "neard init; chmod 644 /root/.near/${NEARD_KEY_NAME}; neard run"
## docker_sandbox_stop: Stop the running neard image & clean up its Docker container
docker_sandbox_stop:
docker stop distrotron_docker_sandbox || docker kill distrotron_docker_sandbox; \
docker rm distrotron_docker_sandbox
#############
#
# These targets perform sandbox tests.
# They can be run in a localnet (on the local host, a remote host or in a Docker container)
# or in the NEAR public testnet if NEAR_ENV='testnet'.
#
##
test: sandboxtest
sandboxtest: ## Default test target: reset test state and run all payment tests.
cd tests/sandbox; \
export NEARD_HOME=${NEARD_HOME}; \
export NEARD_KEY=${NEARD_KEY}; \
export NEAR_ENV=$(NEAR_ENV); \
npx jest -t setup; \
npx jest -t payment
#
sandboxtest_all: ## Reset state and run all tests, including a stress test that can take several minutes to finish.
cd tests/sandbox; \
export NEARD_HOME=${NEARD_HOME}; \
export NEARD_KEY=${NEARD_KEY}; \
export NEAR_ENV=localnet; \
npx jest
#
## sandboxtest_stress: Run just the stress test. You can set the environment variable STRESSTEST_COUNT, or use the default.
sandboxtest_stress: sandbox_stress # same thing
##
sandboxtest_setup: ## Reset test state
cd tests/sandbox; \
export NEARD_HOME=${NEARD_HOME}; \
export NEARD_KEY=${NEARD_KEY}; \
export NEAR_ENV=localnet; \
npx jest -t setup
#
###
# * test against a mintbase contract in testnet.
# (this will require various variables to be set up -- see the test script for a list.)
mintbasetest:
cd tests/sandbox; \
export NEARD_HOME=${NEARD_HOME}; \
export NEARD_KEY=${NEARD_KEY}; \
export NEAR_ENV=localnet; \
npx jest -t setup; \
npx jest -t mintbase
##
# These other targets have been useful during development:
#
# redeploy only the stub contract & user
sandbox_deploy_stub: stub_release
cd tests/sandbox; \
export NEARD_HOME=${NEARD_HOME}; \
export NEARD_KEY=${NEARD_KEY}; \
export NEAR_ENV=localnet; \
npx jest -t "deploy stub"
# redeploy only the main distro contract & user
sandbox_deploy_distro: release
cd tests/sandbox; \
export NEARD_HOME=${NEARD_HOME}; \
export NEARD_KEY=${NEARD_KEY}; \
export NEAR_ENV=localnet; \
npx jest -t "deploy distro"
# recreate all of the test users, with their initial balances
sandbox_make_users:
cd tests/sandbox; \
export NEARD_HOME=${NEARD_HOME}; \
export NEARD_KEY=${NEARD_KEY}; \
export NEAR_ENV=localnet; \
npx jest -t "make test users"
# Run all the payment tests against the current contract state.
#
# These payment tests shift balances of NEAR between users,
# and will eventually cause some test users to be bankrupt,
# which is why the main test target re-initialzes users before running them.
#
# However you can run sandbox_payment at least 3 times before this is a problem.
# This has been a time-saver during iterative development & testing.
sandbox_payment:
cd tests/sandbox; \
export NEARD_HOME=${NEARD_HOME}; \
export NEARD_KEY=${NEARD_KEY}; \
export NEAR_ENV=localnet; \
npx jest -t "payment"
sandbox_stress:
cd tests/sandbox; \
export NEARD_HOME=${NEARD_HOME}; \
export NEARD_KEY=${NEARD_KEY}; \
export NEAR_ENV=localnet; \
npx jest -t "stress"
# Build and launch the web demo
demo: demo_setup demo_build
demo_setup: web/node_modules
web/node_modules: web/package.json
cd web && npm install
demo_build:
cd web && npx parcel src/distrotron.html
#####
# doc targets:
#
##
docs: ## Build documentation
cd distrotron; cargo doc
cd stub; cargo doc
##
# inline docs FTW!