DChain single-node blockchain + React Native messenger client. Core: - PBFT consensus with multi-sig validator admission + equivocation slashing - BadgerDB + schema migration scaffold (CurrentSchemaVersion=0) - libp2p gossipsub (tx/v1, blocks/v1, relay/v1, version/v1) - Native Go contracts (username_registry) alongside WASM (wazero) - WebSocket gateway with topic-based fanout + Ed25519-nonce auth - Relay mailbox with NaCl envelope encryption (X25519 + Ed25519) - Prometheus /metrics, per-IP rate limit, body-size cap Deployment: - Single-node compose (deploy/single/) with Caddy TLS + optional Prometheus - 3-node dev compose (docker-compose.yml) with mocked internet topology - 3-validator prod compose (deploy/prod/) for federation - Auto-update from Gitea via /api/update-check + systemd timer - Build-time version injection (ldflags → node --version) - UI / Swagger toggle flags (DCHAIN_DISABLE_UI, DCHAIN_DISABLE_SWAGGER) Client (client-app/): - Expo / React Native / NativeWind - E2E NaCl encryption, typing indicator, contact requests - Auto-discovery of canonical contracts, chain_id aware, WS reconnect on node switch Documentation: - README.md, CHANGELOG.md, CONTEXT.md - deploy/single/README.md with 6 operator scenarios - deploy/UPDATE_STRATEGY.md with 4-layer forward-compat design - docs/contracts/*.md per contract
105 lines
2.3 KiB
Go
105 lines
2.3 KiB
Go
// Package main is an example DChain smart contract written in Go.
|
|
//
|
|
// # Build
|
|
//
|
|
// tinygo build -o hello_go.wasm -target wasip1 -no-debug .
|
|
//
|
|
// # Deploy
|
|
//
|
|
// client deploy-contract --key /keys/node1.json \
|
|
// --wasm hello_go.wasm --abi hello_go_abi.json \
|
|
// --node http://localhost:8081
|
|
//
|
|
// # Use
|
|
//
|
|
// client call-contract --key /keys/node1.json --contract $ID \
|
|
// --method increment --gas 5000 --node http://localhost:8081
|
|
//
|
|
// The contract implements a simple counter with owner-gated reset.
|
|
// It demonstrates every SDK primitive: arguments, state, caller, logging,
|
|
// and inter-contract calls.
|
|
package main
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
dc "go-blockchain/contracts/sdk"
|
|
)
|
|
|
|
// increment adds 1 to the counter and logs the new value.
|
|
//
|
|
//export increment
|
|
func increment() {
|
|
v := dc.GetU64("counter")
|
|
v++
|
|
dc.PutU64("counter", v)
|
|
dc.Log("counter: " + strconv.FormatUint(v, 10))
|
|
}
|
|
|
|
// get logs the current counter value without changing state.
|
|
//
|
|
//export get
|
|
func get() {
|
|
v := dc.GetU64("counter")
|
|
dc.Log("counter: " + strconv.FormatUint(v, 10))
|
|
}
|
|
|
|
// reset sets the counter to 0. On first call the caller becomes the owner.
|
|
// Subsequent calls are restricted to the owner.
|
|
//
|
|
//export reset
|
|
func reset() {
|
|
owner := dc.GetStateStr("owner")
|
|
caller := dc.Caller()
|
|
if owner == "" {
|
|
dc.SetStateStr("owner", caller)
|
|
dc.PutU64("counter", 0)
|
|
dc.Log("initialized — owner: " + caller[:min8(caller)])
|
|
return
|
|
}
|
|
if caller != owner {
|
|
dc.Log("unauthorized: " + caller[:min8(caller)])
|
|
return
|
|
}
|
|
dc.PutU64("counter", 0)
|
|
dc.Log("reset by owner")
|
|
}
|
|
|
|
// greet logs a personalised greeting using the first call argument.
|
|
//
|
|
//export greet
|
|
func greet() {
|
|
name := dc.ArgStr(0, 64)
|
|
if name == "" {
|
|
name = "world"
|
|
}
|
|
dc.Log("hello, " + name + "! block=" + strconv.FormatUint(dc.BlockHeight(), 10))
|
|
}
|
|
|
|
// ping calls another contract's method (demonstrates inter-contract calls).
|
|
// Args: contract_id (string), method (string)
|
|
//
|
|
//export ping
|
|
func ping() {
|
|
target := dc.ArgStr(0, 64)
|
|
method := dc.ArgStr(1, 64)
|
|
if target == "" || method == "" {
|
|
dc.Log("ping: target and method required")
|
|
return
|
|
}
|
|
if dc.CallContract(target, method, "[]") {
|
|
dc.Log("ping: " + method + " ok")
|
|
} else {
|
|
dc.Log("ping: " + method + " failed")
|
|
}
|
|
}
|
|
|
|
func min8(s string) int {
|
|
if len(s) < 8 {
|
|
return len(s)
|
|
}
|
|
return 8
|
|
}
|
|
|
|
func main() {}
|