Skip to content

flyskywhy/textiot

Repository files navigation

textiot

A framework for building web and native (IoT) Dapps on the IPFS network.

An monorepo textiot.git was combined with https://github.com/textileio/go-textile and related git repos for easier development.

Usage

react-native

npm install textiot --save

For RN < 0.60, run react-native link textiot.

For RN < 0.60 or >= 0.60, then:

Android

Add below into android/settings.gradle

androidx not android:

include ':textile-pb'
project(':textile-pb').projectDir = new File(rootProject.projectDir, '../node_modules/textiot/android-textile/pb-androidx')
include ':textile-mobile'
project(':textile-mobile').projectDir = new File(rootProject.projectDir, '../node_modules/textiot/android-textile/mobile')
include ':android-textile'
project(':android-textile').projectDir = new File(rootProject.projectDir, '../node_modules/textiot/react-native-sdk/textile-androidx')

and cd node_modules/textiot; ./android2androidx.sh

android not androidx:

include ':textile-pb'
project(':textile-pb').projectDir = new File(rootProject.projectDir, '../node_modules/textiot/android-textile/pb-android')
include ':textile-mobile'
project(':textile-mobile').projectDir = new File(rootProject.projectDir, '../node_modules/textiot/android-textile/mobile')
include ':android-textile'
project(':android-textile').projectDir = new File(rootProject.projectDir, '../node_modules/textiot/react-native-sdk/textile-android')

and cd node_modules/textiot; ./androidx2android.sh

require('textiot/react-native-sdk').default;

Because can not npm publish full 300MB package, so you have to install tools described below and

cd node_modules/textiot/
./build-post-npm.sh

results:

go-textile/textile
go-textile/textile.exe
go-textile/textile-arm
android-textile/mobile
react-native-sdk/textile-androidx
react-native-sdk/textile-android

iOS

Because can not npm publish full 300MB package, so you have to install tools described below and

cd node_modules/textiot/
./build-mac.sh

results:

go-textile/textile-mac
go-textile/mobile/dist/ios/Mobile.framework/
go-textile/mobile/dist/ios/protos/

Add below (after pod 'Folly') into ios/Podfile

  pod 'Textile', :path => '../node_modules/textiot/ios-textile'
  pod 'TextileCore', :path => '../node_modules/textiot/go-textile/mobile/dist/ios'
cd ios
pod install

require('textiot/react-native-sdk').default;

web

npm install textiot --save

require('textiot/js-http-client').default;

Docs

tour of Textile

concepts

Textiot 使用详解

Develop

Install tools

golang >= 1.14.15

go get golang.org/x/mobile/cmd/gomobile
export PATH=~/go/bin:$PATH
gomobile init

if unrecognized import path "golang.org/x/mobile/cmd/gomobile , then

mkdir -p ~/go
cd ~/go
mkdir -p src/github.com/golang
mkdir -p src/golang.org
cd src/golang.org
ln -s ../github.com/golang x
cd ~/go/src/github.com/golang
git clone https://github.com/golang/mobile
git clone https://github.com/golang/tools
git clone https://github.com/golang/mod
git clone https://github.com/golang/sys
git clone https://github.com/golang/xerrors
go get golang.org/x/mobile/cmd/gomobile
gomobile init

and also

export GO111MODULE=on
export GOPROXY=https://proxy.golang.com.cn,direct

android sdk

android ndk <= r21

export ANDROID_NDK_HOME=~/tools/android-sdk/ndk/21.4.7075529

21.4.7075529 is default in RN 0.66 and gradle 6.7.1

If r22 or higher, will cause

# golang.org/x/mobile/app
ld: error: duplicate symbol: display

just like x/mobile/cmd/gomobile: gomobile build on simple program returns "ld: error: duplicate symbol: x_cgo_inittls"

nodejs <=10.15.3

With go-textile/release/@textile/js-types/dist/index.d.ts generated by ./node_modules/protobufjs/bin/pbts running in nodejs 12.13.0 , will cause error in npm run build of react-native-sdk .

protoc --version to match change com.google.protobuf:protobuf-java:3.6.1

on Linux

sudo apt install protobuf-compiler

on macOS

brew install protobuf

to install protoc, and

protoc --version

to get its version e.g. 3.12.4.

Then need change com.google.protobuf:protobuf-java:3.6.1 to com.google.protobuf:protobuf-java:3.12.4 in go-textile/release/PBProject/pb/build.gradle, otherwise will cause error: cannot find symbol UnusedPrivateParameter when ./gradlew pb:build in go-textile/release/PBProject/, just as sed in ./build-post-npm.sh.

Then need change com.google.protobuf:protobuf-java:3.6.1 to com.google.protobuf:protobuf-java:3.12.4 in android-textile/textile/build.gradle and android/build.gradle, just as sed in ./build-post-npm.sh, otherwise the APP will crash E AndroidRuntime: java.lang.NoSuchMethodError: No static method internalBuildGeneratedFileFrom([Ljava/lang/String;[Lcom/google/protobuf/Descriptors$FileDescriptor;)Lcom/google/protobuf/Descriptors$FileDescriptor; in class Lcom/google/protobuf/Descriptors$FileDescriptor; or its super classes when run into list.toByteArray() of final Model.ThreadList list in textiot/react-native-sdk/android/src/main/java/io/textile/rnmobile/ThreadsBridge.java.

All will be built by build.sh on Linux except mobile/dist/ios/protos and mobile/dist/ios/Mobile.framework/ by build-mac.sh on macOS. Similarly, if protoc --version return 3.13.0 on macOS, then need change s.dependency 'Protobuf', '~> 3.7' to s.dependency 'Protobuf', '3.13.0' in ios-textile/Textile.podspec and go-textile/mobile/dist/ios/TextileCore.podspec, just as sed in ./build-mac.sh.

protoc-gen-go 7e65e51

If you modify go-textile/pb/protos/*.proto, then you also need regenerate go-textile/pb/*.pb.go by an exact version of protoc-gen-go which match protoc 3.x (or protoc-gen-go@7e65e51 described below)

go get -d -u github.com/golang/protobuf/protoc-gen-go
cd ~/go/src/github.com/golang/protobuf/protoc-gen-go
git checkout 7e65e51
go build
mv protoc-gen-go ~/go/bin/

or just

go install github.com/golang/protobuf/protoc-gen-go@7e65e51

regenerate:

cd go-textile
make protos

google.golang.org/protobuf v1.26.1-0.20210525005349-febffdd88e85

There is exact 1.26-dev version

google.golang.org/protobuf v1.26.1-0.20210525005349-febffdd88e85

in go.mod, if miss it or even a bigger version e.g.

google.golang.org/protobuf v1.27.0

will cause panic: proto: file "message.proto" is already registered when run ./textile, ref to proto: file is already registered with different packages and reflect/protoregistry: restore conflicting file names check.

Maybe can use protoc-gen-go with the same version with go.mod, because the key to resolve the panic is should only about go.mod not about the protoc-gen-go, and I validate the result of protoc is same between protoc-gen-go@7e65e51 above and protoc-gen-go@febffdd below.

go install google.golang.org/protobuf/cmd/protoc-gen-go@febffdd

If can't install above for network reason, install below is also ok

cd ~/go/pkg/mod/google.golang.org/protobuf@v1.26.1-0.20210525005349-febffdd88e85/cmd/protoc-gen-go/
go build -o ~/go/bin

regenerate:

cd go-textile
make protos

sudo apt install mingw-w64 gcc-arm-linux-gnueabihf

riscv

git clone https://github.com/riscv-collab/riscv-gnu-toolchain
cd riscv-gnu-toolchain
./configure --prefix=/opt/riscv
make linux

export PATH=/opt/riscv/bin:$PATH
cd go-textile
make textile-riscv

If run ./textile-riscv on some risc-v board got Segmentation fault, and gdb textile-riscv run got SIGSEGV, Segmentation fault. transcmp at dcigettext.c:290, then you need go to riscv-gnu-toolchain/glibc and git reset glibc from e.g. glibc-2.33 to glibc-2.37, then make linux again and make textile-riscv again.

Xcode on macOS

Build on Linux

./build.sh

results:

go-textile/textile
go-textile/textile.exe
go-textile/textile-arm
android-textile/pb-androidx
android-textile/pb-android
android-textile/mobile
react-native-sdk/textile-androidx
react-native-sdk/textile-android
react-native-sdk/dist/index.js

Build on macOS

./build-mac.sh

results:

go-textile/textile-mac
go-textile/mobile/dist/ios/Mobile.framework/
go-textile/mobile/dist/ios/protos/

Build on macOS

./build-web.sh

results:

js-http-client/dist/index.js

patch

Because go mod vendor will remove vendor/ automatically then copy again from ~/go/pkg/mod/, so if apply more than 1 patch, please only run go mod vendor once then apply the patches.

patch to avoid crash sometimes on Android

Fix node.waitAdd in Textile.java sometimes will crash. The uploadservice used by registerReceiver only needed by Cafe (server) function, and Android APP used in IoT generally does not need Cafe.

sed -i -e "s/^    private static final RequestsBroadcastReceiver/    \/\/ private static final RequestsBroadcastReceiver/" node_modules/textiot/android-textile/textile/src/main/java/io/textile/textile/Textile.java
sed -i -e "s/^        applicationContext.registerReceiver/        \/\/ applicationContext.registerReceiver/" node_modules/textiot/android-textile/textile/src/main/java/io/textile/textile/Textile.java

patch to reduce memory usage

  • Fix too many swarm peers connections cause too high memory usage, then may cause mobile APP crash when running in 2GB IoT device.

Numeric value below ref to https://osm.hpi.de/iot-lab/docs/mpss2020/scaling/scaling-ipfs/

sed -i -e "s/LowWater = 200/LowWater = 40/" node_modules/textiot/go-textile/core/config.go
sed -i -e "s/HighWater = 500/HighWater = 70\n\t\tconf.Swarm.ConnMgr.GracePeriod = \"60s\"/" node_modules/textiot/go-textile/core/config.go
sed -i -e "/time.Second/d" node_modules/textiot/go-textile/core/config.go
  • Fix IPFS 0.8.0 bug that LowWater and HighWater has no effect

Ref to

Number of peers far exceeds lowWater/highWater settings

ipfs daemon memory usage grows overtime: killed by OOM after a 10~12 days running

Garbage Collect per-peer bandwidth metrics

Before running

go mod vendor

in node_modules/textiot/build-post-npm.sh , add

github.com/libp2p/go-libp2p-peerstore v0.3.0

in node_modules/textiot/go-textile/go.mod , then run

go mod download
go mod vendor

Then copy https://github.com/flyskywhy/go-libp2p-connmgr branch ipfs-v0.8.0

connmgr.go
decay.go
options.go

to node_modules/textiot/go-textile/vendor/ to overwrite relevant files.

Copy https://github.com/flyskywhy/go-libp2p-peerstore branch ipfs-v0.8.0

pstoremem/

to node_modules/textiot/go-textile/vendor/ to overwrite relevant folder.

Copy https://github.com/flyskywhy/go-libp2p-peerstore branch ipfs-v0.8.0

metrics.go

to node_modules/textiot/go-textile/vendor/ to overwrite relevant file.

In

type Metrics interface

of node_modules/textiot/go-textile/vendor/github.com/libp2p/go-libp2p-core/peerstore/peerstore.go , add

RemovePeer(peer.ID)

Finally, run remain in node_modules/textiot/build-post-npm.sh .

patch to not write IPFS config file per APP start

If IoT device start APP, and turn off the power just while APP writing IPFS config file, the file will be corrupted, thus cause APP crash. This patch to let APP write IPFS config file only diff.

Write IPFS config file per APP start comes from SetConfig() call in node_modules/textiot/go-textile/core/config.go , and the patch is on setConfigUnsynced() in node_modules/textiot/go-textile/vendor/github.com/ipfs/go-ipfs/repo/fsrepo/fsrepo.go , just replace

	for k, v := range m {
		mapconf[k] = v
	}
	if err := serialize.WriteConfigFile(configFilename, mapconf); err != nil {
		return err
	}

with

	"reflect"
...
	if reflect.DeepEqual(m, mapconf) == false {
		for k, v := range m {
			mapconf[k] = v
		}

		if err := serialize.WriteConfigFile(configFilename, mapconf); err != nil {
			return err
		}
	}

patch to run on Android >= 11

Patch with solution in x/mobile: Calling net.InterfaceAddrs() fails on Android SDK 30.

Changelog

textiot@2.0.0

go-ipfs v0.8.0

And no ipfs repo migration from 0.4.22 to 0.8.0 support here, so you need migrate with ipfs official migration tool by yourself if you want.

textiot@1.0.0

go-ipfs v0.4.22-0.20191002225611-b15edf287df6