Merge pull request #13 from aeternity/tb-change-load-order

Change load order
This commit is contained in:
Tino Breddin 2019-12-09 15:44:38 +02:00 committed by GitHub
commit 68aa9c7481
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 16 deletions

View File

@ -20,8 +20,10 @@ compile:
./rebar3 compile
priv/$(nif_lib): src/lib.rs
ifneq ($(ECRECOVER_DISABLE_NIF_BUILD), true)
cargo build --release
cp target/release/$(nif_lib_src) $@
endif
clean:
rm -f priv/$(nif_lib) target/release/$(nif_lib_src)

View File

@ -1,15 +1,29 @@
# ecrecover
FFI (NIF) export of Ethereum's ecrecover for use from Erlang.
### prerequisite:
- a checked out copy of my fork of parity-ethereum (https://github.com/johnsnewby/parity-ethereum) checked out into this directory this is (i.e. it will be referenced as `./parity-ethereum`)
### Prerequisites
The NIF is written in Rust, therefore the following additional build
dependencies are needed:
### to compile:
`cargo build`
- Rust
- Cargo
- Cmake
### Build
Execute:
```
make
```
To disable the local build of the NIF library, e.g. to use a prebuilt binary,
use the following command:
```
ECRECOVER_DISABLE_NIF_BUILD=true make
```
## Erlang integration
The shared library uses NIF. Use the erlang file `src/ecrecover.erl` to use this:
The shared library uses NIF. Use the Erlang file `src/ecrecover.erl` to use this:
```
c("src/ecrecover").

20
default.nix Normal file
View File

@ -0,0 +1,20 @@
let
stable = import (fetchTarball { # 19.09
url = https://github.com/NixOS/nixpkgs-channels/archive/a22b0189002.tar.gz;
sha256 = "0rgd0cbxg9mrzb830hgjlvy134ivpfcnkyhbnlvvn8vl4y20zqmz";
}) {};
in {
aeternityEnv = stable.stdenv.mkDerivation {
name = "ecrecover";
buildInputs = [
## base
stable.stdenv
## erlang
stable.erlangR21 # OTP 21.3.5.2
## rust, required for building the NIF
stable.rustc
stable.cargo
stable.cmake
];
};
}

View File

@ -11,8 +11,14 @@
%% NIF API
load() ->
SoName = filename:join(priv(), atom_to_list(?MODULE)),
ok = erlang:load_nif(SoName, 0).
% Prefer locally built NIF (good for testing and exotic platforms) over
% prebuilt binaries.
case load_local_nif() of
ok ->
ok;
{error, _} ->
load_prebuilt_nif()
end.
not_loaded(Line) ->
erlang:nif_error({error, {not_loaded, [{module, ?MODULE}, {line, Line}]}}).
@ -35,15 +41,21 @@ recover(<<_:32/binary>> = Hash, <<_:65/binary>> = Sig) ->
%%=============================================================================
%% Internal Functions
priv()->
case code:priv_dir(ecrecoverprebuilt) of
{error, _} ->
EbinDir = filename:dirname(code:which(?MODULE)),
AppPath = filename:dirname(EbinDir),
filename:join(AppPath, "priv");
Path ->
Path
end.
load_local_nif() ->
EbinDir = filename:dirname(code:which(?MODULE)),
AppDir = filename:dirname(EbinDir),
PrivDir = filename:join(AppDir, "priv"),
SoName = filename:join(PrivDir, atom_to_list(?MODULE)),
erlang:load_nif(SoName, 0).
load_prebuilt_nif() ->
case code:priv_dir(ecrecoverprebuilt) of
{error, _} ->
{error, prebuilt_priv_dir_not_found};
PrivDir ->
SoName = filename:join(PrivDir, atom_to_list(?MODULE)),
erlang:load_nif(SoName, 0)
end.
recover_(_Input) ->
not_loaded(?LINE).