yann300
10 years ago
141 changed files with 4073 additions and 2047 deletions
@ -1,27 +0,0 @@ |
|||
#!/usr/bin/python |
|||
# cpp-ethereum build script |
|||
# to be used from CI server, or to build locally |
|||
# uses python instead of bash script for better cross-platform support |
|||
|
|||
# TODO Initial version. Needs much more improvements |
|||
|
|||
import argparse |
|||
import os |
|||
import subprocess |
|||
|
|||
def build_dependencies(): |
|||
if os.path.exists("extdep"): |
|||
os.chdir("extdep") |
|||
if not os.path.exists("build"): |
|||
os.makedirs("build") |
|||
os.chdir("build") |
|||
subprocess.check_call(["cmake", ".."]) |
|||
subprocess.check_call("make") |
|||
|
|||
parser = argparse.ArgumentParser() |
|||
parser.add_argument("cmd", help="what to build") |
|||
|
|||
args = parser.parse_args() |
|||
if args.cmd == "dep": |
|||
build_dependencies() |
|||
|
@ -0,0 +1,38 @@ |
|||
#pragma once |
|||
|
|||
#include <evmjit/libevmjit/Common.h> |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
|
|||
inline u256 llvm2eth(jit::i256 _i) |
|||
{ |
|||
u256 u = 0; |
|||
u |= _i.d; |
|||
u <<= 64; |
|||
u |= _i.c; |
|||
u <<= 64; |
|||
u |= _i.b; |
|||
u <<= 64; |
|||
u |= _i.a; |
|||
return u; |
|||
} |
|||
|
|||
inline jit::i256 eth2llvm(u256 _u) |
|||
{ |
|||
jit::i256 i; |
|||
u256 mask = 0xFFFFFFFFFFFFFFFF; |
|||
i.a = static_cast<uint64_t>(_u & mask); |
|||
_u >>= 64; |
|||
i.b = static_cast<uint64_t>(_u & mask); |
|||
_u >>= 64; |
|||
i.c = static_cast<uint64_t>(_u & mask); |
|||
_u >>= 64; |
|||
i.d = static_cast<uint64_t>(_u & mask); |
|||
return i; |
|||
} |
|||
|
|||
} |
|||
} |
@ -1,237 +1,239 @@ |
|||
#include "Memory.h" |
|||
|
|||
#include <vector> |
|||
#include <iostream> |
|||
#include <iomanip> |
|||
#include <cstdint> |
|||
#include <cassert> |
|||
|
|||
#include <llvm/IR/GlobalVariable.h> |
|||
#include <llvm/IR/Function.h> |
|||
#include <llvm/IR/IntrinsicInst.h> |
|||
|
|||
#include "Type.h" |
|||
#include "Runtime.h" |
|||
#include "GasMeter.h" |
|||
#include "Endianness.h" |
|||
#include "RuntimeManager.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
namespace jit |
|||
{ |
|||
|
|||
Memory::Memory(RuntimeManager& _runtimeManager, GasMeter& _gasMeter): |
|||
RuntimeHelper(_runtimeManager), // TODO: RuntimeHelper not needed
|
|||
m_gasMeter(_gasMeter) |
|||
{ |
|||
llvm::Type* resizeArgs[] = {Type::RuntimePtr, Type::WordPtr}; |
|||
m_resize = llvm::Function::Create(llvm::FunctionType::get(Type::BytePtr, resizeArgs, false), llvm::Function::ExternalLinkage, "mem_resize", getModule()); |
|||
llvm::AttrBuilder attrBuilder; |
|||
attrBuilder.addAttribute(llvm::Attribute::NoAlias).addAttribute(llvm::Attribute::NoCapture).addAttribute(llvm::Attribute::NonNull).addAttribute(llvm::Attribute::ReadOnly); |
|||
m_resize->setAttributes(llvm::AttributeSet::get(m_resize->getContext(), 1, attrBuilder)); |
|||
|
|||
m_require = createRequireFunc(_gasMeter); |
|||
m_loadWord = createFunc(false, Type::Word, _gasMeter); |
|||
m_storeWord = createFunc(true, Type::Word, _gasMeter); |
|||
m_storeByte = createFunc(true, Type::Byte, _gasMeter); |
|||
} |
|||
|
|||
llvm::Function* Memory::createRequireFunc(GasMeter& _gasMeter) |
|||
{ |
|||
llvm::Type* argTypes[] = {Type::RuntimePtr, Type::Word, Type::Word}; |
|||
auto func = llvm::Function::Create(llvm::FunctionType::get(Type::Void, argTypes, false), llvm::Function::PrivateLinkage, "mem.require", getModule()); |
|||
auto rt = func->arg_begin(); |
|||
rt->setName("rt"); |
|||
auto offset = rt->getNextNode(); |
|||
offset->setName("offset"); |
|||
auto size = offset->getNextNode(); |
|||
size->setName("size"); |
|||
|
|||
auto preBB = llvm::BasicBlock::Create(func->getContext(), "Pre", func); |
|||
auto checkBB = llvm::BasicBlock::Create(func->getContext(), "Check", func); |
|||
auto resizeBB = llvm::BasicBlock::Create(func->getContext(), "Resize", func); |
|||
auto returnBB = llvm::BasicBlock::Create(func->getContext(), "Return", func); |
|||
|
|||
InsertPointGuard guard(m_builder); // Restores insert point at function exit
|
|||
|
|||
// BB "Pre": Ignore checks with size 0
|
|||
m_builder.SetInsertPoint(preBB); |
|||
auto sizeIsZero = m_builder.CreateICmpEQ(size, Constant::get(0)); |
|||
m_builder.CreateCondBr(sizeIsZero, returnBB, checkBB); |
|||
|
|||
// BB "Check"
|
|||
m_builder.SetInsertPoint(checkBB); |
|||
auto uaddWO = llvm::Intrinsic::getDeclaration(getModule(), llvm::Intrinsic::uadd_with_overflow, Type::Word); |
|||
auto uaddRes = m_builder.CreateCall2(uaddWO, offset, size, "res"); |
|||
auto sizeRequired = m_builder.CreateExtractValue(uaddRes, 0, "sizeReq"); |
|||
auto overflow1 = m_builder.CreateExtractValue(uaddRes, 1, "overflow1"); |
|||
auto rtPtr = getRuntimeManager().getRuntimePtr(); |
|||
auto sizePtr = m_builder.CreateStructGEP(rtPtr, 4); |
|||
auto currSize = m_builder.CreateLoad(sizePtr, "currSize"); |
|||
auto tooSmall = m_builder.CreateICmpULE(currSize, sizeRequired, "tooSmall"); |
|||
auto resizeNeeded = m_builder.CreateOr(tooSmall, overflow1, "resizeNeeded"); |
|||
m_builder.CreateCondBr(resizeNeeded, resizeBB, returnBB); // OPT branch weights?
|
|||
|
|||
// BB "Resize"
|
|||
m_builder.SetInsertPoint(resizeBB); |
|||
// Check gas first
|
|||
uaddRes = m_builder.CreateCall2(uaddWO, sizeRequired, Constant::get(31), "res"); |
|||
auto wordsRequired = m_builder.CreateExtractValue(uaddRes, 0); |
|||
auto overflow2 = m_builder.CreateExtractValue(uaddRes, 1, "overflow2"); |
|||
auto overflow = m_builder.CreateOr(overflow1, overflow2, "overflow"); |
|||
wordsRequired = m_builder.CreateSelect(overflow, Constant::get(-1), wordsRequired); |
|||
wordsRequired = m_builder.CreateUDiv(wordsRequired, Constant::get(32), "wordsReq"); |
|||
sizeRequired = m_builder.CreateMul(wordsRequired, Constant::get(32), "roundedSizeReq"); |
|||
auto words = m_builder.CreateUDiv(currSize, Constant::get(32), "words"); // size is always 32*k
|
|||
auto newWords = m_builder.CreateSub(wordsRequired, words, "addtionalWords"); |
|||
_gasMeter.countMemory(newWords); |
|||
// Resize
|
|||
m_builder.CreateStore(sizeRequired, sizePtr); |
|||
auto newData = m_builder.CreateCall2(m_resize, rt, sizePtr, "newData"); |
|||
auto dataPtr = m_builder.CreateStructGEP(rtPtr, 3); |
|||
m_builder.CreateStore(newData, dataPtr); |
|||
m_builder.CreateBr(returnBB); |
|||
|
|||
// BB "Return"
|
|||
m_builder.SetInsertPoint(returnBB); |
|||
m_builder.CreateRetVoid(); |
|||
return func; |
|||
} |
|||
|
|||
llvm::Function* Memory::createFunc(bool _isStore, llvm::Type* _valueType, GasMeter&) |
|||
{ |
|||
auto isWord = _valueType == Type::Word; |
|||
|
|||
llvm::Type* storeArgs[] = {Type::RuntimePtr, Type::Word, _valueType}; |
|||
llvm::Type* loadArgs[] = {Type::RuntimePtr, Type::Word}; |
|||
auto name = _isStore ? isWord ? "mstore" : "mstore8" : "mload"; |
|||
auto funcType = _isStore ? llvm::FunctionType::get(Type::Void, storeArgs, false) : llvm::FunctionType::get(Type::Word, loadArgs, false); |
|||
auto func = llvm::Function::Create(funcType, llvm::Function::PrivateLinkage, name, getModule()); |
|||
|
|||
InsertPointGuard guard(m_builder); // Restores insert point at function exit
|
|||
|
|||
m_builder.SetInsertPoint(llvm::BasicBlock::Create(func->getContext(), {}, func)); |
|||
auto rt = func->arg_begin(); |
|||
rt->setName("rt"); |
|||
auto index = rt->getNextNode(); |
|||
index->setName("index"); |
|||
|
|||
auto valueSize = _valueType->getPrimitiveSizeInBits() / 8; |
|||
this->require(index, Constant::get(valueSize)); |
|||
auto ptr = getBytePtr(index); |
|||
if (isWord) |
|||
ptr = m_builder.CreateBitCast(ptr, Type::WordPtr, "wordPtr"); |
|||
if (_isStore) |
|||
{ |
|||
llvm::Value* value = index->getNextNode(); |
|||
value->setName("value"); |
|||
if (isWord) |
|||
value = Endianness::toBE(m_builder, value); |
|||
m_builder.CreateStore(value, ptr); |
|||
m_builder.CreateRetVoid(); |
|||
} |
|||
else |
|||
{ |
|||
llvm::Value* ret = m_builder.CreateLoad(ptr); |
|||
ret = Endianness::toNative(m_builder, ret); |
|||
m_builder.CreateRet(ret); |
|||
} |
|||
|
|||
return func; |
|||
} |
|||
|
|||
|
|||
llvm::Value* Memory::loadWord(llvm::Value* _addr) |
|||
{ |
|||
return createCall(m_loadWord, {getRuntimeManager().getRuntimePtr(), _addr}); |
|||
} |
|||
|
|||
void Memory::storeWord(llvm::Value* _addr, llvm::Value* _word) |
|||
{ |
|||
createCall(m_storeWord, {getRuntimeManager().getRuntimePtr(), _addr, _word}); |
|||
} |
|||
|
|||
void Memory::storeByte(llvm::Value* _addr, llvm::Value* _word) |
|||
{ |
|||
auto byte = m_builder.CreateTrunc(_word, Type::Byte, "byte"); |
|||
createCall(m_storeByte, {getRuntimeManager().getRuntimePtr(), _addr, byte}); |
|||
} |
|||
|
|||
llvm::Value* Memory::getData() |
|||
{ |
|||
auto rtPtr = getRuntimeManager().getRuntimePtr(); |
|||
auto dataPtr = m_builder.CreateStructGEP(rtPtr, 3); |
|||
return m_builder.CreateLoad(dataPtr, "data"); |
|||
} |
|||
|
|||
llvm::Value* Memory::getSize() |
|||
{ |
|||
auto rtPtr = getRuntimeManager().getRuntimePtr(); |
|||
auto sizePtr = m_builder.CreateStructGEP(rtPtr, 4); |
|||
return m_builder.CreateLoad(sizePtr, "size"); |
|||
} |
|||
|
|||
llvm::Value* Memory::getBytePtr(llvm::Value* _index) |
|||
{ |
|||
return m_builder.CreateGEP(getData(), _index, "ptr"); |
|||
} |
|||
|
|||
void Memory::require(llvm::Value* _offset, llvm::Value* _size) |
|||
{ |
|||
createCall(m_require, {getRuntimeManager().getRuntimePtr(), _offset, _size}); |
|||
} |
|||
|
|||
void Memory::copyBytes(llvm::Value* _srcPtr, llvm::Value* _srcSize, llvm::Value* _srcIdx, |
|||
llvm::Value* _destMemIdx, llvm::Value* _reqBytes) |
|||
{ |
|||
require(_destMemIdx, _reqBytes); |
|||
|
|||
// Additional copy cost
|
|||
// TODO: This round ups to 32 happens in many places
|
|||
auto copyWords = m_builder.CreateUDiv(m_builder.CreateAdd(_reqBytes, Constant::get(31)), Constant::get(32)); |
|||
m_gasMeter.countCopy(copyWords); |
|||
|
|||
// Algorithm:
|
|||
// isOutsideData = idx256 >= size256
|
|||
// idx64 = trunc idx256
|
|||
// size64 = trunc size256
|
|||
// dataLeftSize = size64 - idx64 // safe if not isOutsideData
|
|||
// reqBytes64 = trunc _reqBytes // require() handles large values
|
|||
// bytesToCopy0 = select(reqBytes64 > dataLeftSize, dataSizeLeft, reqBytes64) // min
|
|||
// bytesToCopy = select(isOutsideData, 0, bytesToCopy0)
|
|||
|
|||
auto isOutsideData = m_builder.CreateICmpUGE(_srcIdx, _srcSize); |
|||
auto idx64 = m_builder.CreateTrunc(_srcIdx, Type::lowPrecision); |
|||
auto size64 = m_builder.CreateTrunc(_srcSize, Type::lowPrecision); |
|||
auto dataLeftSize = m_builder.CreateNUWSub(size64, idx64); |
|||
auto reqBytes64 = m_builder.CreateTrunc(_reqBytes, Type::lowPrecision); |
|||
auto outOfBound = m_builder.CreateICmpUGT(reqBytes64, dataLeftSize); |
|||
auto bytesToCopyInner = m_builder.CreateSelect(outOfBound, dataLeftSize, reqBytes64); |
|||
auto zero64 = llvm::ConstantInt::get(Type::lowPrecision, 0); // TODO: Cache common constants
|
|||
auto bytesToCopy = m_builder.CreateSelect(isOutsideData, zero64, bytesToCopyInner); |
|||
|
|||
auto src = m_builder.CreateGEP(_srcPtr, idx64, "src"); |
|||
auto dst = m_builder.CreateGEP(getData(), _destMemIdx, "dst"); |
|||
m_builder.CreateMemCpy(dst, src, bytesToCopy, 0); |
|||
} |
|||
|
|||
} |
|||
} |
|||
} |
|||
|
|||
|
|||
extern "C" |
|||
{ |
|||
using namespace dev::eth::jit; |
|||
|
|||
EXPORT byte* mem_resize(Runtime* _rt, i256* _size) // TODO: Use uint64 as size OR use realloc in LLVM IR
|
|||
{ |
|||
auto size = _size->a; // Trunc to 64-bit
|
|||
auto& memory = _rt->getMemory(); |
|||
memory.resize(size); |
|||
return memory.data(); |
|||
} |
|||
} |
|||
#include "Memory.h" |
|||
|
|||
#include <vector> |
|||
#include <iostream> |
|||
#include <iomanip> |
|||
#include <cstdint> |
|||
#include <cassert> |
|||
|
|||
#include <llvm/IR/GlobalVariable.h> |
|||
#include <llvm/IR/Function.h> |
|||
#include <llvm/IR/IntrinsicInst.h> |
|||
|
|||
#include "Type.h" |
|||
#include "Runtime.h" |
|||
#include "GasMeter.h" |
|||
#include "Endianness.h" |
|||
#include "RuntimeManager.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
namespace jit |
|||
{ |
|||
|
|||
Memory::Memory(RuntimeManager& _runtimeManager, GasMeter& _gasMeter): |
|||
RuntimeHelper(_runtimeManager), // TODO: RuntimeHelper not needed
|
|||
m_gasMeter(_gasMeter) |
|||
{ |
|||
llvm::Type* resizeArgs[] = {Type::RuntimePtr, Type::WordPtr}; |
|||
m_resize = llvm::Function::Create(llvm::FunctionType::get(Type::BytePtr, resizeArgs, false), llvm::Function::ExternalLinkage, "mem_resize", getModule()); |
|||
llvm::AttrBuilder attrBuilder; |
|||
attrBuilder.addAttribute(llvm::Attribute::NoAlias).addAttribute(llvm::Attribute::NoCapture).addAttribute(llvm::Attribute::NonNull).addAttribute(llvm::Attribute::ReadOnly); |
|||
m_resize->setAttributes(llvm::AttributeSet::get(m_resize->getContext(), 1, attrBuilder)); |
|||
|
|||
m_require = createRequireFunc(_gasMeter); |
|||
m_loadWord = createFunc(false, Type::Word, _gasMeter); |
|||
m_storeWord = createFunc(true, Type::Word, _gasMeter); |
|||
m_storeByte = createFunc(true, Type::Byte, _gasMeter); |
|||
} |
|||
|
|||
llvm::Function* Memory::createRequireFunc(GasMeter& _gasMeter) |
|||
{ |
|||
llvm::Type* argTypes[] = {Type::RuntimePtr, Type::Word, Type::Word}; |
|||
auto func = llvm::Function::Create(llvm::FunctionType::get(Type::Void, argTypes, false), llvm::Function::PrivateLinkage, "mem.require", getModule()); |
|||
auto rt = func->arg_begin(); |
|||
rt->setName("rt"); |
|||
auto offset = rt->getNextNode(); |
|||
offset->setName("offset"); |
|||
auto size = offset->getNextNode(); |
|||
size->setName("size"); |
|||
|
|||
auto preBB = llvm::BasicBlock::Create(func->getContext(), "Pre", func); |
|||
auto checkBB = llvm::BasicBlock::Create(func->getContext(), "Check", func); |
|||
auto resizeBB = llvm::BasicBlock::Create(func->getContext(), "Resize", func); |
|||
auto returnBB = llvm::BasicBlock::Create(func->getContext(), "Return", func); |
|||
|
|||
InsertPointGuard guard(m_builder); // Restores insert point at function exit
|
|||
|
|||
// BB "Pre": Ignore checks with size 0
|
|||
m_builder.SetInsertPoint(preBB); |
|||
auto sizeIsZero = m_builder.CreateICmpEQ(size, Constant::get(0)); |
|||
m_builder.CreateCondBr(sizeIsZero, returnBB, checkBB); |
|||
|
|||
// BB "Check"
|
|||
m_builder.SetInsertPoint(checkBB); |
|||
auto uaddWO = llvm::Intrinsic::getDeclaration(getModule(), llvm::Intrinsic::uadd_with_overflow, Type::Word); |
|||
auto uaddRes = m_builder.CreateCall2(uaddWO, offset, size, "res"); |
|||
auto sizeRequired = m_builder.CreateExtractValue(uaddRes, 0, "sizeReq"); |
|||
auto overflow1 = m_builder.CreateExtractValue(uaddRes, 1, "overflow1"); |
|||
auto rtPtr = getRuntimeManager().getRuntimePtr(); |
|||
auto sizePtr = m_builder.CreateStructGEP(rtPtr, 4); |
|||
auto currSize = m_builder.CreateLoad(sizePtr, "currSize"); |
|||
auto tooSmall = m_builder.CreateICmpULE(currSize, sizeRequired, "tooSmall"); |
|||
auto resizeNeeded = m_builder.CreateOr(tooSmall, overflow1, "resizeNeeded"); |
|||
m_builder.CreateCondBr(resizeNeeded, resizeBB, returnBB); // OPT branch weights?
|
|||
|
|||
// BB "Resize"
|
|||
m_builder.SetInsertPoint(resizeBB); |
|||
// Check gas first
|
|||
uaddRes = m_builder.CreateCall2(uaddWO, sizeRequired, Constant::get(31), "res"); |
|||
auto wordsRequired = m_builder.CreateExtractValue(uaddRes, 0); |
|||
auto overflow2 = m_builder.CreateExtractValue(uaddRes, 1, "overflow2"); |
|||
auto overflow = m_builder.CreateOr(overflow1, overflow2, "overflow"); |
|||
wordsRequired = m_builder.CreateSelect(overflow, Constant::get(-1), wordsRequired); |
|||
wordsRequired = m_builder.CreateUDiv(wordsRequired, Constant::get(32), "wordsReq"); |
|||
sizeRequired = m_builder.CreateMul(wordsRequired, Constant::get(32), "roundedSizeReq"); |
|||
auto words = m_builder.CreateUDiv(currSize, Constant::get(32), "words"); // size is always 32*k
|
|||
auto newWords = m_builder.CreateSub(wordsRequired, words, "addtionalWords"); |
|||
_gasMeter.countMemory(newWords); |
|||
// Resize
|
|||
m_builder.CreateStore(sizeRequired, sizePtr); |
|||
auto newData = m_builder.CreateCall2(m_resize, rt, sizePtr, "newData"); |
|||
auto dataPtr = m_builder.CreateStructGEP(rtPtr, 3); |
|||
m_builder.CreateStore(newData, dataPtr); |
|||
m_builder.CreateBr(returnBB); |
|||
|
|||
// BB "Return"
|
|||
m_builder.SetInsertPoint(returnBB); |
|||
m_builder.CreateRetVoid(); |
|||
return func; |
|||
} |
|||
|
|||
llvm::Function* Memory::createFunc(bool _isStore, llvm::Type* _valueType, GasMeter&) |
|||
{ |
|||
auto isWord = _valueType == Type::Word; |
|||
|
|||
llvm::Type* storeArgs[] = {Type::RuntimePtr, Type::Word, _valueType}; |
|||
llvm::Type* loadArgs[] = {Type::RuntimePtr, Type::Word}; |
|||
auto name = _isStore ? isWord ? "mstore" : "mstore8" : "mload"; |
|||
auto funcType = _isStore ? llvm::FunctionType::get(Type::Void, storeArgs, false) : llvm::FunctionType::get(Type::Word, loadArgs, false); |
|||
auto func = llvm::Function::Create(funcType, llvm::Function::PrivateLinkage, name, getModule()); |
|||
|
|||
InsertPointGuard guard(m_builder); // Restores insert point at function exit
|
|||
|
|||
m_builder.SetInsertPoint(llvm::BasicBlock::Create(func->getContext(), {}, func)); |
|||
auto rt = func->arg_begin(); |
|||
rt->setName("rt"); |
|||
auto index = rt->getNextNode(); |
|||
index->setName("index"); |
|||
|
|||
auto valueSize = _valueType->getPrimitiveSizeInBits() / 8; |
|||
this->require(index, Constant::get(valueSize)); |
|||
auto ptr = getBytePtr(index); |
|||
if (isWord) |
|||
ptr = m_builder.CreateBitCast(ptr, Type::WordPtr, "wordPtr"); |
|||
if (_isStore) |
|||
{ |
|||
llvm::Value* value = index->getNextNode(); |
|||
value->setName("value"); |
|||
if (isWord) |
|||
value = Endianness::toBE(m_builder, value); |
|||
m_builder.CreateStore(value, ptr); |
|||
m_builder.CreateRetVoid(); |
|||
} |
|||
else |
|||
{ |
|||
llvm::Value* ret = m_builder.CreateLoad(ptr); |
|||
ret = Endianness::toNative(m_builder, ret); |
|||
m_builder.CreateRet(ret); |
|||
} |
|||
|
|||
return func; |
|||
} |
|||
|
|||
|
|||
llvm::Value* Memory::loadWord(llvm::Value* _addr) |
|||
{ |
|||
return createCall(m_loadWord, {getRuntimeManager().getRuntimePtr(), _addr}); |
|||
} |
|||
|
|||
void Memory::storeWord(llvm::Value* _addr, llvm::Value* _word) |
|||
{ |
|||
createCall(m_storeWord, {getRuntimeManager().getRuntimePtr(), _addr, _word}); |
|||
} |
|||
|
|||
void Memory::storeByte(llvm::Value* _addr, llvm::Value* _word) |
|||
{ |
|||
auto byte = m_builder.CreateTrunc(_word, Type::Byte, "byte"); |
|||
createCall(m_storeByte, {getRuntimeManager().getRuntimePtr(), _addr, byte}); |
|||
} |
|||
|
|||
llvm::Value* Memory::getData() |
|||
{ |
|||
auto rtPtr = getRuntimeManager().getRuntimePtr(); |
|||
auto dataPtr = m_builder.CreateStructGEP(rtPtr, 3); |
|||
return m_builder.CreateLoad(dataPtr, "data"); |
|||
} |
|||
|
|||
llvm::Value* Memory::getSize() |
|||
{ |
|||
auto rtPtr = getRuntimeManager().getRuntimePtr(); |
|||
auto sizePtr = m_builder.CreateStructGEP(rtPtr, 4); |
|||
return m_builder.CreateLoad(sizePtr, "size"); |
|||
} |
|||
|
|||
llvm::Value* Memory::getBytePtr(llvm::Value* _index) |
|||
{ |
|||
auto idx = m_builder.CreateTrunc(_index, Type::Size, "idx"); // Never allow memory index be a type bigger than i64
|
|||
return m_builder.CreateGEP(getData(), idx, "ptr"); |
|||
} |
|||
|
|||
void Memory::require(llvm::Value* _offset, llvm::Value* _size) |
|||
{ |
|||
createCall(m_require, {getRuntimeManager().getRuntimePtr(), _offset, _size}); |
|||
} |
|||
|
|||
void Memory::copyBytes(llvm::Value* _srcPtr, llvm::Value* _srcSize, llvm::Value* _srcIdx, |
|||
llvm::Value* _destMemIdx, llvm::Value* _reqBytes) |
|||
{ |
|||
require(_destMemIdx, _reqBytes); |
|||
|
|||
// Additional copy cost
|
|||
// TODO: This round ups to 32 happens in many places
|
|||
auto copyWords = m_builder.CreateUDiv(m_builder.CreateAdd(_reqBytes, Constant::get(31)), Constant::get(32)); |
|||
m_gasMeter.countCopy(copyWords); |
|||
|
|||
// Algorithm:
|
|||
// isOutsideData = idx256 >= size256
|
|||
// idx64 = trunc idx256
|
|||
// size64 = trunc size256
|
|||
// dataLeftSize = size64 - idx64 // safe if not isOutsideData
|
|||
// reqBytes64 = trunc _reqBytes // require() handles large values
|
|||
// bytesToCopy0 = select(reqBytes64 > dataLeftSize, dataSizeLeft, reqBytes64) // min
|
|||
// bytesToCopy = select(isOutsideData, 0, bytesToCopy0)
|
|||
|
|||
auto isOutsideData = m_builder.CreateICmpUGE(_srcIdx, _srcSize); |
|||
auto idx64 = m_builder.CreateTrunc(_srcIdx, Type::lowPrecision); |
|||
auto size64 = m_builder.CreateTrunc(_srcSize, Type::lowPrecision); |
|||
auto dataLeftSize = m_builder.CreateNUWSub(size64, idx64); |
|||
auto reqBytes64 = m_builder.CreateTrunc(_reqBytes, Type::lowPrecision); |
|||
auto outOfBound = m_builder.CreateICmpUGT(reqBytes64, dataLeftSize); |
|||
auto bytesToCopyInner = m_builder.CreateSelect(outOfBound, dataLeftSize, reqBytes64); |
|||
auto zero64 = llvm::ConstantInt::get(Type::lowPrecision, 0); // TODO: Cache common constants
|
|||
auto bytesToCopy = m_builder.CreateSelect(isOutsideData, zero64, bytesToCopyInner); |
|||
|
|||
auto src = m_builder.CreateGEP(_srcPtr, idx64, "src"); |
|||
auto dstIdx = m_builder.CreateTrunc(_destMemIdx, Type::Size, "dstIdx"); // Never allow memory index be a type bigger than i64
|
|||
auto dst = m_builder.CreateGEP(getData(), dstIdx, "dst"); |
|||
m_builder.CreateMemCpy(dst, src, bytesToCopy, 0); |
|||
} |
|||
|
|||
} |
|||
} |
|||
} |
|||
|
|||
|
|||
extern "C" |
|||
{ |
|||
using namespace dev::eth::jit; |
|||
|
|||
EXPORT byte* mem_resize(Runtime* _rt, i256* _size) // TODO: Use uint64 as size OR use realloc in LLVM IR
|
|||
{ |
|||
auto size = _size->a; // Trunc to 64-bit
|
|||
auto& memory = _rt->getMemory(); |
|||
memory.resize(size); |
|||
return memory.data(); |
|||
} |
|||
} |
|||
|
@ -1,59 +1,46 @@ |
|||
|
|||
#pragma once |
|||
|
|||
#include <csetjmp> |
|||
#include <vector> |
|||
|
|||
#include "Instruction.h" |
|||
#include "CompilerHelper.h" |
|||
#include "Utils.h" |
|||
#include "Type.h" |
|||
#include "RuntimeData.h" |
|||
|
|||
|
|||
#ifdef _MSC_VER |
|||
#define EXPORT __declspec(dllexport) |
|||
#else |
|||
#define EXPORT |
|||
#endif |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
namespace jit |
|||
{ |
|||
|
|||
using StackImpl = std::vector<i256>; |
|||
using MemoryImpl = bytes; |
|||
using jmp_buf_ref = decltype(&std::jmp_buf{}[0]); |
|||
|
|||
class Runtime |
|||
{ |
|||
public: |
|||
Runtime(RuntimeData* _data, Env* _env); |
|||
|
|||
Runtime(const Runtime&) = delete; |
|||
Runtime& operator=(const Runtime&) = delete; |
|||
|
|||
StackImpl& getStack() { return m_stack; } |
|||
MemoryImpl& getMemory() { return m_memory; } |
|||
Env* getEnvPtr() { return &m_env; } |
|||
|
|||
bytes getReturnData() const; |
|||
jmp_buf_ref getJmpBuf() { return m_jmpBuf; } |
|||
|
|||
private: |
|||
RuntimeData& m_data; ///< Pointer to data. Expected by compiled contract.
|
|||
Env& m_env; ///< Pointer to environment proxy. Expected by compiled contract.
|
|||
jmp_buf_ref m_currJmpBuf; ///< Pointer to jump buffer. Expected by compiled contract.
|
|||
byte* m_memoryData = nullptr; |
|||
i256 m_memorySize; |
|||
std::jmp_buf m_jmpBuf; |
|||
StackImpl m_stack; |
|||
MemoryImpl m_memory; |
|||
}; |
|||
|
|||
} |
|||
} |
|||
} |
|||
|
|||
#pragma once |
|||
|
|||
#include <csetjmp> |
|||
#include "RuntimeData.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
namespace jit |
|||
{ |
|||
|
|||
using StackImpl = std::vector<i256>; |
|||
using MemoryImpl = bytes; |
|||
using jmp_buf_ref = decltype(&std::jmp_buf{}[0]); |
|||
|
|||
class Runtime |
|||
{ |
|||
public: |
|||
Runtime(RuntimeData* _data, Env* _env); |
|||
|
|||
Runtime(const Runtime&) = delete; |
|||
Runtime& operator=(const Runtime&) = delete; |
|||
|
|||
StackImpl& getStack() { return m_stack; } |
|||
MemoryImpl& getMemory() { return m_memory; } |
|||
Env* getEnvPtr() { return &m_env; } |
|||
|
|||
bytes_ref getReturnData() const; |
|||
jmp_buf_ref getJmpBuf() { return m_jmpBuf; } |
|||
|
|||
private: |
|||
RuntimeData& m_data; ///< Pointer to data. Expected by compiled contract.
|
|||
Env& m_env; ///< Pointer to environment proxy. Expected by compiled contract.
|
|||
jmp_buf_ref m_currJmpBuf; ///< Pointer to jump buffer. Expected by compiled contract.
|
|||
byte* m_memoryData = nullptr; |
|||
i256 m_memorySize; |
|||
std::jmp_buf m_jmpBuf; |
|||
StackImpl m_stack; |
|||
MemoryImpl m_memory; |
|||
}; |
|||
|
|||
} |
|||
} |
|||
} |
|||
|
@ -1,34 +1,41 @@ |
|||
#include "interface.h" |
|||
#include <cstdio> |
|||
#include "ExecutionEngine.h" |
|||
|
|||
extern "C" |
|||
{ |
|||
|
|||
evmjit_result evmjit_run(void* _data, void* _env) |
|||
{ |
|||
using namespace dev::eth::jit; |
|||
using namespace dev::eth::jit; |
|||
|
|||
auto data = static_cast<RuntimeData*>(_data); |
|||
#ifdef _MSC_VER |
|||
#define _ALLOW_KEYWORD_MACROS |
|||
#define noexcept throw() |
|||
#endif |
|||
|
|||
ExecutionEngine engine; |
|||
EXPORT void* evmjit_create() noexcept |
|||
{ |
|||
return new(std::nothrow) ExecutionEngine; |
|||
} |
|||
|
|||
EXPORT void evmjit_destroy(ExecutionEngine* _engine) noexcept |
|||
{ |
|||
delete _engine; |
|||
} |
|||
|
|||
auto codePtr = data->code; |
|||
auto codeSize = data->elems[RuntimeData::CodeSize].a; |
|||
bytes bytecode; |
|||
bytecode.insert(bytecode.end(), codePtr, codePtr + codeSize); |
|||
EXPORT int evmjit_run(ExecutionEngine* _engine, RuntimeData* _data, Env* _env) noexcept |
|||
{ |
|||
try |
|||
{ |
|||
auto codePtr = _data->code; |
|||
auto codeSize = _data->codeSize; |
|||
bytes bytecode; |
|||
bytecode.insert(bytecode.end(), codePtr, codePtr + codeSize); |
|||
|
|||
auto returnCode = engine.run(bytecode, data, static_cast<Env*>(_env)); |
|||
evmjit_result result = {static_cast<int32_t>(returnCode), 0, nullptr}; |
|||
if (returnCode == ReturnCode::Return && !engine.returnData.empty()) |
|||
auto returnCode = _engine->run(bytecode, _data, _env); |
|||
return static_cast<int>(returnCode); |
|||
} |
|||
catch(...) |
|||
{ |
|||
// TODO: Optimized returning data. Allocating memory on client side by callback function might be a good idea
|
|||
result.returnDataSize = engine.returnData.size(); |
|||
result.returnData = std::malloc(result.returnDataSize); |
|||
std::memcpy(result.returnData, engine.returnData.data(), result.returnDataSize); |
|||
return static_cast<int>(ReturnCode::UnexpectedException); |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
|
|||
} |
|||
|
@ -0,0 +1,86 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file CanonBlockChain.cpp
|
|||
* @author Gav Wood <i@gavwood.com> |
|||
* @date 2014 |
|||
*/ |
|||
|
|||
#include "CanonBlockChain.h" |
|||
|
|||
#include <boost/filesystem.hpp> |
|||
#include <libdevcore/Common.h> |
|||
#include <libdevcore/RLP.h> |
|||
#include <libdevcrypto/FileSystem.h> |
|||
#include <libethcore/Exceptions.h> |
|||
#include <libethcore/ProofOfWork.h> |
|||
#include <libethcore/BlockInfo.h> |
|||
#include <liblll/Compiler.h> |
|||
#include "State.h" |
|||
#include "Defaults.h" |
|||
using namespace std; |
|||
using namespace dev; |
|||
using namespace dev::eth; |
|||
|
|||
#define ETH_CATCH 1 |
|||
|
|||
std::map<Address, Account> const& dev::eth::genesisState() |
|||
{ |
|||
static std::map<Address, Account> s_ret; |
|||
if (s_ret.empty()) |
|||
{ |
|||
// Initialise.
|
|||
for (auto i: vector<string>({ |
|||
"dbdbdb2cbd23b783741e8d7fcf51e459b497e4a6", |
|||
"e6716f9544a56c530d868e4bfbacb172315bdead", |
|||
"b9c015918bdaba24b4ff057a92a3873d6eb201be", |
|||
"1a26338f0d905e295fccb71fa9ea849ffa12aaf4", |
|||
"2ef47100e0787b915105fd5e3f4ff6752079d5cb", |
|||
"cd2a3d9f938e13cd947ec05abc7fe734df8dd826", |
|||
"6c386a4b26f73c802f34673f7248bb118f97424a", |
|||
"e4157b34ea9615cfbde6b4fda419828124b70c78" |
|||
})) |
|||
s_ret[Address(fromHex(i))] = Account(u256(1) << 200, Account::NormalCreation); |
|||
} |
|||
return s_ret; |
|||
} |
|||
|
|||
std::unique_ptr<BlockInfo> CanonBlockChain::s_genesis; |
|||
boost::shared_mutex CanonBlockChain::x_genesis; |
|||
|
|||
bytes CanonBlockChain::createGenesisBlock() |
|||
{ |
|||
RLPStream block(3); |
|||
|
|||
h256 stateRoot; |
|||
{ |
|||
MemoryDB db; |
|||
TrieDB<Address, MemoryDB> state(&db); |
|||
state.init(); |
|||
dev::eth::commit(genesisState(), db, state); |
|||
stateRoot = state.root(); |
|||
} |
|||
|
|||
block.appendList(14) |
|||
<< h256() << EmptyListSHA3 << h160() << stateRoot << EmptyTrie << EmptyTrie << LogBloom() << c_genesisDifficulty << 0 << 1000000 << 0 << (unsigned)0 << string() << sha3(bytes(1, 42)); |
|||
block.appendRaw(RLPEmptyList); |
|||
block.appendRaw(RLPEmptyList); |
|||
return block.out(); |
|||
} |
|||
|
|||
CanonBlockChain::CanonBlockChain(std::string _path, bool _killExisting): BlockChain(CanonBlockChain::createGenesisBlock(), _path, _killExisting) |
|||
{ |
|||
} |
@ -0,0 +1,76 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file CanonBlockChain.h
|
|||
* @author Gav Wood <i@gavwood.com> |
|||
* @date 2014 |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#pragma warning(push) |
|||
#pragma warning(disable: 4100 4267) |
|||
#include <leveldb/db.h> |
|||
#pragma warning(pop) |
|||
|
|||
#include <mutex> |
|||
#include <libdevcore/Log.h> |
|||
#include <libdevcore/Exceptions.h> |
|||
#include <libethcore/CommonEth.h> |
|||
#include <libethcore/BlockInfo.h> |
|||
#include <libdevcore/Guards.h> |
|||
#include "BlockDetails.h" |
|||
#include "Account.h" |
|||
#include "BlockQueue.h" |
|||
#include "BlockChain.h" |
|||
namespace ldb = leveldb; |
|||
|
|||
namespace dev |
|||
{ |
|||
|
|||
namespace eth |
|||
{ |
|||
|
|||
// TODO: Move all this Genesis stuff into Genesis.h/.cpp
|
|||
std::map<Address, Account> const& genesisState(); |
|||
|
|||
/**
|
|||
* @brief Implements the blockchain database. All data this gives is disk-backed. |
|||
* @threadsafe |
|||
* @todo Make not memory hog (should actually act as a cache and deallocate old entries). |
|||
*/ |
|||
class CanonBlockChain: public BlockChain |
|||
{ |
|||
public: |
|||
CanonBlockChain(bool _killExisting = false): CanonBlockChain(std::string(), _killExisting) {} |
|||
CanonBlockChain(std::string _path, bool _killExisting = false); |
|||
~CanonBlockChain() {} |
|||
|
|||
/// @returns the genesis block header.
|
|||
static BlockInfo const& genesis() { UpgradableGuard l(x_genesis); if (!s_genesis) { auto gb = createGenesisBlock(); UpgradeGuard ul(l); s_genesis.reset(new BlockInfo); s_genesis->populate(&gb); } return *s_genesis; } |
|||
|
|||
/// @returns the genesis block as its RLP-encoded byte array.
|
|||
/// @note This is slow as it's constructed anew each call. Consider genesis() instead.
|
|||
static bytes createGenesisBlock(); |
|||
|
|||
private: |
|||
/// Static genesis info and its lock.
|
|||
static boost::shared_mutex x_genesis; |
|||
static std::unique_ptr<BlockInfo> s_genesis; |
|||
}; |
|||
|
|||
} |
|||
} |
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1,66 @@ |
|||
<!doctype> |
|||
<html> |
|||
<head> |
|||
<script type="text/javascript" src="js/bignumber.js/bignumber.min.js"></script> |
|||
<script type="text/javascript" src="../dist/ethereum.js"></script> |
|||
<script type="text/javascript"> |
|||
var web3 = require('web3'); |
|||
web3.setProvider(new web3.providers.HttpSyncProvider('http://localhost:8080')); |
|||
|
|||
var source = "" + |
|||
"contract Contract { " + |
|||
" event Incremented(bool indexed odd, uint x); " + |
|||
" function Contract() { " + |
|||
" x = 69; " + |
|||
" } " + |
|||
" function inc() { " + |
|||
" ++x; " + |
|||
" Incremented(x % 2 == 1, x); " + |
|||
" } " + |
|||
" uint x; " + |
|||
"}"; |
|||
|
|||
var desc = [{ |
|||
"type":"event", |
|||
"name":"Incremented", |
|||
"inputs": [{"name":"odd","type":"bool","indexed":true},{"name":"x","type":"uint","indexed":false}], |
|||
}, { |
|||
"type":"function", |
|||
"name":"inc", |
|||
"inputs": [], |
|||
"outputs": [] |
|||
}]; |
|||
|
|||
var address; |
|||
var contract; |
|||
|
|||
var update = function (x) { |
|||
document.getElementById('result').innerText = JSON.stringify(x); |
|||
}; |
|||
|
|||
var createContract = function () { |
|||
address = web3.eth.transact({code: web3.eth.solidity(source)}); |
|||
contract = web3.eth.contract(address, desc); |
|||
contract.Incremented({odd: true}).changed(update); |
|||
|
|||
}; |
|||
|
|||
var callContract = function () { |
|||
contract.call().inc(); |
|||
}; |
|||
|
|||
|
|||
</script> |
|||
</head> |
|||
|
|||
<body> |
|||
<div> |
|||
<button type="button" onClick="createContract();">create contract</button> |
|||
</div> |
|||
<div> |
|||
<button type="button" onClick="callContract();">test1</button> |
|||
</div> |
|||
<div id="result"> |
|||
</div> |
|||
</body> |
|||
</html> |
@ -0,0 +1,65 @@ |
|||
/* |
|||
This file is part of ethereum.js. |
|||
|
|||
ethereum.js is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU Lesser General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
ethereum.js is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU Lesser General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU Lesser General Public License |
|||
along with ethereum.js. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file jsonrpc.js |
|||
* @authors: |
|||
* Marek Kotewicz <marek@ethdev.com> |
|||
* @date 2015 |
|||
*/ |
|||
|
|||
var messageId = 1; |
|||
|
|||
/// Should be called to valid json create payload object
|
|||
/// @param method of jsonrpc call, required
|
|||
/// @param params, an array of method params, optional
|
|||
/// @returns valid jsonrpc payload object
|
|||
var toPayload = function (method, params) { |
|||
if (!method) |
|||
console.error('jsonrpc method should be specified!'); |
|||
|
|||
return { |
|||
jsonrpc: '2.0', |
|||
method: method, |
|||
params: params || [], |
|||
id: messageId++ |
|||
}; |
|||
}; |
|||
|
|||
/// Should be called to check if jsonrpc response is valid
|
|||
/// @returns true if response is valid, otherwise false
|
|||
var isValidResponse = function (response) { |
|||
return !!response && |
|||
!response.error && |
|||
response.jsonrpc === '2.0' && |
|||
typeof response.id === 'number' && |
|||
response.result !== undefined; // only undefined is not valid json object
|
|||
}; |
|||
|
|||
/// Should be called to create batch payload object
|
|||
/// @param messages, an array of objects with method (required) and params (optional) fields
|
|||
var toBatchPayload = function (messages) { |
|||
return messages.map(function (message) { |
|||
return toPayload(message.method, message.params); |
|||
}); |
|||
}; |
|||
|
|||
module.exports = { |
|||
toPayload: toPayload, |
|||
isValidResponse: isValidResponse, |
|||
toBatchPayload: toBatchPayload |
|||
}; |
|||
|
|||
|
@ -0,0 +1,461 @@ |
|||
var assert = require('assert'); |
|||
var BigNumber = require('bignumber.js'); |
|||
var abi = require('../lib/abi.js'); |
|||
var clone = function (object) { return JSON.parse(JSON.stringify(object)); }; |
|||
|
|||
var description = [{ |
|||
"name": "test", |
|||
"type": "function", |
|||
"inputs": [{ |
|||
"name": "a", |
|||
"type": "uint256" |
|||
} |
|||
], |
|||
"outputs": [ |
|||
{ |
|||
"name": "d", |
|||
"type": "uint256" |
|||
} |
|||
] |
|||
}]; |
|||
|
|||
describe('abi', function() { |
|||
describe('outputParser', function() { |
|||
it('should parse output string', function() { |
|||
|
|||
// given
|
|||
var d = clone(description); |
|||
|
|||
d[0].outputs = [ |
|||
{ type: "string" } |
|||
]; |
|||
|
|||
// when
|
|||
var parser = abi.outputParser(d); |
|||
|
|||
// then
|
|||
assert.equal( |
|||
parser.test("0x" + |
|||
"0000000000000000000000000000000000000000000000000000000000000005" + |
|||
"68656c6c6f000000000000000000000000000000000000000000000000000000")[0], |
|||
'hello' |
|||
); |
|||
assert.equal( |
|||
parser.test("0x" + |
|||
"0000000000000000000000000000000000000000000000000000000000000005" + |
|||
"776f726c64000000000000000000000000000000000000000000000000000000")[0], |
|||
'world' |
|||
); |
|||
|
|||
}); |
|||
|
|||
it('should parse output uint', function() { |
|||
|
|||
// given
|
|||
var d = clone(description); |
|||
|
|||
d[0].outputs = [ |
|||
{ type: 'uint' } |
|||
]; |
|||
|
|||
// when
|
|||
var parser = abi.outputParser(d); |
|||
|
|||
// then
|
|||
assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1); |
|||
assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10); |
|||
assert.equal( |
|||
parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0].toString(10), |
|||
new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).toString(10) |
|||
); |
|||
assert.equal( |
|||
parser.test("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0].toString(10), |
|||
new BigNumber("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0", 16).toString(10) |
|||
); |
|||
}); |
|||
|
|||
it('should parse output uint256', function() { |
|||
|
|||
// given
|
|||
var d = clone(description); |
|||
|
|||
d[0].outputs = [ |
|||
{ type: 'uint256' } |
|||
]; |
|||
|
|||
// when
|
|||
var parser = abi.outputParser(d); |
|||
|
|||
// then
|
|||
assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1); |
|||
assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10); |
|||
assert.equal( |
|||
parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0].toString(10), |
|||
new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).toString(10) |
|||
); |
|||
assert.equal( |
|||
parser.test("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0].toString(10), |
|||
new BigNumber("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0", 16).toString(10) |
|||
); |
|||
}); |
|||
|
|||
it('should parse output uint128', function() { |
|||
|
|||
// given
|
|||
var d = clone(description); |
|||
|
|||
d[0].outputs = [ |
|||
{ type: 'uint128' } |
|||
]; |
|||
|
|||
// when
|
|||
var parser = abi.outputParser(d); |
|||
|
|||
// then
|
|||
assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1); |
|||
assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10); |
|||
assert.equal( |
|||
parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0].toString(10), |
|||
new BigNumber("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16).toString(10) |
|||
); |
|||
assert.equal( |
|||
parser.test("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0].toString(10), |
|||
new BigNumber("fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0", 16).toString(10) |
|||
); |
|||
}); |
|||
|
|||
it('should parse output int', function() { |
|||
|
|||
// given
|
|||
var d = clone(description); |
|||
|
|||
d[0].outputs = [ |
|||
{ type: 'int' } |
|||
]; |
|||
|
|||
// when
|
|||
var parser = abi.outputParser(d); |
|||
|
|||
// then
|
|||
assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1); |
|||
assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10); |
|||
assert.equal(parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0], -1); |
|||
assert.equal(parser.test("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0], -16); |
|||
}); |
|||
|
|||
it('should parse output int256', function() { |
|||
|
|||
// given
|
|||
var d = clone(description); |
|||
|
|||
d[0].outputs = [ |
|||
{ type: 'int256' } |
|||
]; |
|||
|
|||
// when
|
|||
var parser = abi.outputParser(d); |
|||
|
|||
// then
|
|||
assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1); |
|||
assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10); |
|||
assert.equal(parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0], -1); |
|||
assert.equal(parser.test("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0], -16); |
|||
}); |
|||
|
|||
it('should parse output int128', function() { |
|||
|
|||
// given
|
|||
var d = clone(description); |
|||
|
|||
d[0].outputs = [ |
|||
{ type: 'int128' } |
|||
]; |
|||
|
|||
// when
|
|||
var parser = abi.outputParser(d); |
|||
|
|||
// then
|
|||
assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1); |
|||
assert.equal(parser.test("0x000000000000000000000000000000000000000000000000000000000000000a")[0], 10); |
|||
assert.equal(parser.test("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")[0], -1); |
|||
assert.equal(parser.test("0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0")[0], -16); |
|||
}); |
|||
|
|||
it('should parse output hash', function() { |
|||
|
|||
// given
|
|||
var d = clone(description); |
|||
|
|||
d[0].outputs = [ |
|||
{ type: 'hash' } |
|||
]; |
|||
|
|||
// when
|
|||
var parser = abi.outputParser(d); |
|||
|
|||
// then
|
|||
assert.equal( |
|||
parser.test("0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1")[0], |
|||
"0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1" |
|||
); |
|||
}); |
|||
|
|||
it('should parse output hash256', function() { |
|||
|
|||
// given
|
|||
var d = clone(description); |
|||
|
|||
d[0].outputs = [ |
|||
{ type: 'hash256' } |
|||
]; |
|||
|
|||
// when
|
|||
var parser = abi.outputParser(d); |
|||
|
|||
// then
|
|||
assert.equal( |
|||
parser.test("0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1")[0], |
|||
"0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1" |
|||
); |
|||
}); |
|||
|
|||
it('should parse output hash160', function() { |
|||
|
|||
// given
|
|||
var d = clone(description); |
|||
|
|||
d[0].outputs = [ |
|||
{ type: 'hash160' } |
|||
]; |
|||
|
|||
// when
|
|||
var parser = abi.outputParser(d); |
|||
|
|||
// then
|
|||
assert.equal( |
|||
parser.test("0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1")[0], |
|||
"0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1" |
|||
); |
|||
// TODO shouldnt' the expected hash be shorter?
|
|||
}); |
|||
|
|||
it('should parse output address', function() { |
|||
|
|||
// given
|
|||
var d = clone(description); |
|||
|
|||
d[0].outputs = [ |
|||
{ type: 'address' } |
|||
]; |
|||
|
|||
// when
|
|||
var parser = abi.outputParser(d); |
|||
|
|||
// then
|
|||
assert.equal( |
|||
parser.test("0x000000000000000000000000407d73d8a49eeb85d32cf465507dd71d507100c1")[0], |
|||
"0x407d73d8a49eeb85d32cf465507dd71d507100c1" |
|||
); |
|||
}); |
|||
|
|||
it('should parse output bool', function() { |
|||
|
|||
// given
|
|||
var d = clone(description); |
|||
|
|||
d[0].outputs = [ |
|||
{ type: 'bool' } |
|||
]; |
|||
|
|||
// when
|
|||
var parser = abi.outputParser(d); |
|||
|
|||
// then
|
|||
assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000001")[0], true); |
|||
assert.equal(parser.test("0x0000000000000000000000000000000000000000000000000000000000000000")[0], false); |
|||
|
|||
|
|||
}); |
|||
|
|||
it('should parse output real', function() { |
|||
|
|||
// given
|
|||
var d = clone(description); |
|||
|
|||
d[0].outputs = [ |
|||
{ type: 'real' } |
|||
]; |
|||
|
|||
// when
|
|||
var parser = abi.outputParser(d); |
|||
|
|||
// then
|
|||
assert.equal(parser.test("0x0000000000000000000000000000000100000000000000000000000000000000")[0], 1); |
|||
assert.equal(parser.test("0x0000000000000000000000000000000220000000000000000000000000000000")[0], 2.125); |
|||
assert.equal(parser.test("0x0000000000000000000000000000000880000000000000000000000000000000")[0], 8.5); |
|||
assert.equal(parser.test("0xffffffffffffffffffffffffffffffff00000000000000000000000000000000")[0], -1); |
|||
|
|||
}); |
|||
|
|||
it('should parse output ureal', function() { |
|||
|
|||
// given
|
|||
var d = clone(description); |
|||
|
|||
d[0].outputs = [ |
|||
{ type: 'ureal' } |
|||
]; |
|||
|
|||
// when
|
|||
var parser = abi.outputParser(d); |
|||
|
|||
// then
|
|||
assert.equal(parser.test("0x0000000000000000000000000000000100000000000000000000000000000000")[0], 1); |
|||
assert.equal(parser.test("0x0000000000000000000000000000000220000000000000000000000000000000")[0], 2.125); |
|||
assert.equal(parser.test("0x0000000000000000000000000000000880000000000000000000000000000000")[0], 8.5); |
|||
|
|||
}); |
|||
|
|||
|
|||
it('should parse multiple output strings', function() { |
|||
|
|||
// given
|
|||
var d = clone(description); |
|||
|
|||
d[0].outputs = [ |
|||
{ type: "string" }, |
|||
{ type: "string" } |
|||
]; |
|||
|
|||
// when
|
|||
var parser = abi.outputParser(d); |
|||
|
|||
// then
|
|||
assert.equal( |
|||
parser.test("0x" + |
|||
"0000000000000000000000000000000000000000000000000000000000000005" + |
|||
"0000000000000000000000000000000000000000000000000000000000000005" + |
|||
"68656c6c6f000000000000000000000000000000000000000000000000000000" + |
|||
"776f726c64000000000000000000000000000000000000000000000000000000")[0], |
|||
'hello' |
|||
); |
|||
assert.equal( |
|||
parser.test("0x" + |
|||
"0000000000000000000000000000000000000000000000000000000000000005" + |
|||
"0000000000000000000000000000000000000000000000000000000000000005" + |
|||
"68656c6c6f000000000000000000000000000000000000000000000000000000" + |
|||
"776f726c64000000000000000000000000000000000000000000000000000000")[1], |
|||
'world' |
|||
); |
|||
|
|||
}); |
|||
|
|||
it('should use proper method name', function () { |
|||
|
|||
// given
|
|||
var d = clone(description); |
|||
d[0].name = 'helloworld(int)'; |
|||
d[0].outputs = [ |
|||
{ type: "int" } |
|||
]; |
|||
|
|||
// when
|
|||
var parser = abi.outputParser(d); |
|||
|
|||
// then
|
|||
assert.equal(parser.helloworld("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1); |
|||
assert.equal(parser.helloworld['int']("0x0000000000000000000000000000000000000000000000000000000000000001")[0], 1); |
|||
|
|||
}); |
|||
|
|||
|
|||
it('should parse multiple methods', function () { |
|||
|
|||
// given
|
|||
var d = [{ |
|||
name: "test", |
|||
type: "function", |
|||
inputs: [{ type: "int" }], |
|||
outputs: [{ type: "int" }] |
|||
},{ |
|||
name: "test2", |
|||
type: "function", |
|||
inputs: [{ type: "string" }], |
|||
outputs: [{ type: "string" }] |
|||
}]; |
|||
|
|||
// when
|
|||
var parser = abi.outputParser(d); |
|||
|
|||
//then
|
|||
assert.equal(parser.test("0000000000000000000000000000000000000000000000000000000000000001")[0], 1); |
|||
assert.equal(parser.test2("0x" + |
|||
"0000000000000000000000000000000000000000000000000000000000000005" + |
|||
"68656c6c6f000000000000000000000000000000000000000000000000000000")[0], |
|||
"hello" |
|||
); |
|||
|
|||
}); |
|||
|
|||
it('should parse output array', function () { |
|||
|
|||
// given
|
|||
var d = clone(description); |
|||
d[0].outputs = [ |
|||
{ type: 'int[]' } |
|||
]; |
|||
|
|||
// when
|
|||
var parser = abi.outputParser(d); |
|||
|
|||
// then
|
|||
assert.equal(parser.test("0x" + |
|||
"0000000000000000000000000000000000000000000000000000000000000002" + |
|||
"0000000000000000000000000000000000000000000000000000000000000005" + |
|||
"0000000000000000000000000000000000000000000000000000000000000006")[0][0], |
|||
5 |
|||
); |
|||
assert.equal(parser.test("0x" + |
|||
"0000000000000000000000000000000000000000000000000000000000000002" + |
|||
"0000000000000000000000000000000000000000000000000000000000000005" + |
|||
"0000000000000000000000000000000000000000000000000000000000000006")[0][1], |
|||
6 |
|||
); |
|||
|
|||
}); |
|||
|
|||
it('should parse 0x value', function () { |
|||
|
|||
// given
|
|||
var d = clone(description); |
|||
d[0].outputs = [ |
|||
{ type: 'int' } |
|||
]; |
|||
|
|||
// when
|
|||
var parser = abi.outputParser(d); |
|||
|
|||
// then
|
|||
assert.equal(parser.test("0x")[0], 0); |
|||
|
|||
}); |
|||
|
|||
it('should parse 0x value', function () { |
|||
|
|||
// given
|
|||
var d = clone(description); |
|||
d[0].outputs = [ |
|||
{ type: 'uint' } |
|||
]; |
|||
|
|||
// when
|
|||
var parser = abi.outputParser(d); |
|||
|
|||
// then
|
|||
assert.equal(parser.test("0x")[0], 0); |
|||
|
|||
}); |
|||
|
|||
}); |
|||
}); |
|||
|
@ -0,0 +1,125 @@ |
|||
var assert = require('assert'); |
|||
var event = require('../lib/event.js'); |
|||
var f = require('../lib/formatters.js'); |
|||
|
|||
describe('event', function () { |
|||
describe('inputParser', function () { |
|||
it('should create basic filter input object', function () { |
|||
|
|||
// given
|
|||
var address = '0x012345'; |
|||
var signature = '0x987654'; |
|||
var e = { |
|||
name: 'Event', |
|||
inputs: [{"name":"a","type":"uint256","indexed":true},{"name":"b","type":"hash256","indexed":false}] |
|||
}; |
|||
|
|||
// when
|
|||
var impl = event.inputParser(address, signature, e); |
|||
var result = impl(); |
|||
|
|||
// then
|
|||
assert.equal(result.address, address); |
|||
assert.equal(result.topic.length, 1); |
|||
assert.equal(result.topic[0], signature); |
|||
|
|||
}); |
|||
|
|||
it('should create filter input object with options', function () { |
|||
|
|||
// given
|
|||
var address = '0x012345'; |
|||
var signature = '0x987654'; |
|||
var options = { |
|||
earliest: 1, |
|||
latest: 2, |
|||
offset: 3, |
|||
max: 4 |
|||
}; |
|||
var e = { |
|||
name: 'Event', |
|||
inputs: [{"name":"a","type":"uint256","indexed":true},{"name":"b","type":"hash256","indexed":false}] |
|||
}; |
|||
|
|||
// when
|
|||
var impl = event.inputParser(address, signature, e); |
|||
var result = impl({}, options); |
|||
|
|||
// then
|
|||
assert.equal(result.address, address); |
|||
assert.equal(result.topic.length, 1); |
|||
assert.equal(result.topic[0], signature); |
|||
assert.equal(result.earliest, options.earliest); |
|||
assert.equal(result.latest, options.latest); |
|||
assert.equal(result.offset, options.offset); |
|||
assert.equal(result.max, options.max); |
|||
|
|||
}); |
|||
|
|||
it('should create filter input object with indexed params', function () { |
|||
|
|||
// given
|
|||
var address = '0x012345'; |
|||
var signature = '0x987654'; |
|||
var options = { |
|||
earliest: 1, |
|||
latest: 2, |
|||
offset: 3, |
|||
max: 4 |
|||
}; |
|||
var e = { |
|||
name: 'Event', |
|||
inputs: [{"name":"a","type":"uint256","indexed":true},{"name":"b","type":"hash256","indexed":false}] |
|||
}; |
|||
|
|||
// when
|
|||
var impl = event.inputParser(address, signature, e); |
|||
var result = impl({a: 4}, options); |
|||
|
|||
// then
|
|||
assert.equal(result.address, address); |
|||
assert.equal(result.topic.length, 2); |
|||
assert.equal(result.topic[0], signature); |
|||
assert.equal(result.topic[1], f.formatInputInt(4)); |
|||
assert.equal(result.earliest, options.earliest); |
|||
assert.equal(result.latest, options.latest); |
|||
assert.equal(result.offset, options.offset); |
|||
assert.equal(result.max, options.max); |
|||
|
|||
}); |
|||
|
|||
it('should create filter input object with an array of indexed params', function () { |
|||
|
|||
// given
|
|||
var address = '0x012345'; |
|||
var signature = '0x987654'; |
|||
var options = { |
|||
earliest: 1, |
|||
latest: 2, |
|||
offset: 3, |
|||
max: 4 |
|||
}; |
|||
var e = { |
|||
name: 'Event', |
|||
inputs: [{"name":"a","type":"uint256","indexed":true},{"name":"b","type":"hash256","indexed":false}] |
|||
}; |
|||
|
|||
// when
|
|||
var impl = event.inputParser(address, signature, e); |
|||
var result = impl({a: [4, 69]}, options); |
|||
|
|||
// then
|
|||
assert.equal(result.address, address); |
|||
assert.equal(result.topic.length, 2); |
|||
assert.equal(result.topic[0], signature); |
|||
assert.equal(result.topic[1][0], f.formatInputInt(4)); |
|||
assert.equal(result.topic[1][1], f.formatInputInt(69)); |
|||
assert.equal(result.earliest, options.earliest); |
|||
assert.equal(result.latest, options.latest); |
|||
assert.equal(result.offset, options.offset); |
|||
assert.equal(result.max, options.max); |
|||
|
|||
}); |
|||
}); |
|||
}); |
|||
|
@ -1,124 +0,0 @@ |
|||
var assert = require('assert'); |
|||
var event = require('../lib/event.js'); |
|||
var f = require('../lib/formatters.js'); |
|||
|
|||
describe('event', function () { |
|||
it('should create basic filter input object', function () { |
|||
|
|||
// given
|
|||
var address = '0x012345'; |
|||
var signature = '0x987654'; |
|||
var e = { |
|||
name: 'Event', |
|||
inputs: [{"name":"a","type":"uint256","indexed":true},{"name":"b","type":"hash256","indexed":false}] |
|||
}; |
|||
|
|||
// when
|
|||
var impl = event(address, signature, e); |
|||
var result = impl(); |
|||
|
|||
// then
|
|||
assert.equal(result.address, address); |
|||
assert.equal(result.topic.length, 1); |
|||
assert.equal(result.topic[0], signature); |
|||
|
|||
}); |
|||
|
|||
it('should create filter input object with options', function () { |
|||
|
|||
// given
|
|||
var address = '0x012345'; |
|||
var signature = '0x987654'; |
|||
var options = { |
|||
earliest: 1, |
|||
latest: 2, |
|||
offset: 3, |
|||
max: 4 |
|||
}; |
|||
var e = { |
|||
name: 'Event', |
|||
inputs: [{"name":"a","type":"uint256","indexed":true},{"name":"b","type":"hash256","indexed":false}] |
|||
}; |
|||
|
|||
// when
|
|||
var impl = event(address, signature, e); |
|||
var result = impl({}, options); |
|||
|
|||
// then
|
|||
assert.equal(result.address, address); |
|||
assert.equal(result.topic.length, 1); |
|||
assert.equal(result.topic[0], signature); |
|||
assert.equal(result.earliest, options.earliest); |
|||
assert.equal(result.latest, options.latest); |
|||
assert.equal(result.offset, options.offset); |
|||
assert.equal(result.max, options.max); |
|||
|
|||
}); |
|||
|
|||
it('should create filter input object with indexed params', function () { |
|||
|
|||
// given
|
|||
var address = '0x012345'; |
|||
var signature = '0x987654'; |
|||
var options = { |
|||
earliest: 1, |
|||
latest: 2, |
|||
offset: 3, |
|||
max: 4 |
|||
}; |
|||
var e = { |
|||
name: 'Event', |
|||
inputs: [{"name":"a","type":"uint256","indexed":true},{"name":"b","type":"hash256","indexed":false}] |
|||
}; |
|||
|
|||
// when
|
|||
var impl = event(address, signature, e); |
|||
var result = impl({a: 4}, options); |
|||
|
|||
// then
|
|||
assert.equal(result.address, address); |
|||
assert.equal(result.topic.length, 2); |
|||
assert.equal(result.topic[0], signature); |
|||
assert.equal(result.topic[1], f.formatInputInt(4)); |
|||
assert.equal(result.earliest, options.earliest); |
|||
assert.equal(result.latest, options.latest); |
|||
assert.equal(result.offset, options.offset); |
|||
assert.equal(result.max, options.max); |
|||
|
|||
}); |
|||
|
|||
it('should create filter input object with an array of indexed params', function () { |
|||
|
|||
// given
|
|||
var address = '0x012345'; |
|||
var signature = '0x987654'; |
|||
var options = { |
|||
earliest: 1, |
|||
latest: 2, |
|||
offset: 3, |
|||
max: 4 |
|||
}; |
|||
var e = { |
|||
name: 'Event', |
|||
inputs: [{"name":"a","type":"uint256","indexed":true},{"name":"b","type":"hash256","indexed":false}] |
|||
}; |
|||
|
|||
// when
|
|||
var impl = event(address, signature, e); |
|||
var result = impl({a: [4, 69]}, options); |
|||
|
|||
// then
|
|||
assert.equal(result.address, address); |
|||
assert.equal(result.topic.length, 2); |
|||
assert.equal(result.topic[0], signature); |
|||
assert.equal(result.topic[1][0], f.formatInputInt(4)); |
|||
assert.equal(result.topic[1][1], f.formatInputInt(69)); |
|||
assert.equal(result.earliest, options.earliest); |
|||
assert.equal(result.latest, options.latest); |
|||
assert.equal(result.offset, options.offset); |
|||
assert.equal(result.max, options.max); |
|||
|
|||
}); |
|||
|
|||
}); |
|||
|
@ -0,0 +1,81 @@ |
|||
var assert = require('assert'); |
|||
var event = require('../lib/event.js'); |
|||
|
|||
describe('event', function () { |
|||
describe('outputParser', function () { |
|||
it('should parse basic event output object', function () { |
|||
|
|||
// given
|
|||
var output = { |
|||
"address":"0x78dfc5983baecf65f73e3de3a96cee24e6b7981e", |
|||
"data":"0x000000000000000000000000000000000000000000000000000000000000004b", |
|||
"number":2, |
|||
"topic":[ |
|||
"0x6e61ef44ac2747ff8b84d353a908eb8bd5c3fb118334d57698c5cfc7041196ad", |
|||
"0x0000000000000000000000000000000000000000000000000000000000000001" |
|||
] |
|||
}; |
|||
|
|||
var e = { |
|||
name: 'Event', |
|||
inputs: [{"name":"a","type":"bool","indexed":true},{"name":"b","type":"uint256","indexed":false}] |
|||
}; |
|||
|
|||
// when
|
|||
var impl = event.outputParser(e); |
|||
var result = impl(output); |
|||
|
|||
// then
|
|||
assert.equal(result.event, 'Event'); |
|||
assert.equal(result.number, 2); |
|||
assert.equal(Object.keys(result.args).length, 2); |
|||
assert.equal(result.args.a, true); |
|||
assert.equal(result.args.b, 75); |
|||
}); |
|||
|
|||
it('should parse event output object arguments in correct order', function () { |
|||
|
|||
// given
|
|||
var output = { |
|||
"address":"0x78dfc5983baecf65f73e3de3a96cee24e6b7981e", |
|||
"data": "0x" + |
|||
"000000000000000000000000000000000000000000000000000000000000004b" + |
|||
"000000000000000000000000000000000000000000000000000000000000004c" + |
|||
"0000000000000000000000000000000000000000000000000000000000000001", |
|||
"number":3, |
|||
"topic":[ |
|||
"0x6e61ef44ac2747ff8b84d353a908eb8bd5c3fb118334d57698c5cfc7041196ad", |
|||
"0x0000000000000000000000000000000000000000000000000000000000000001", |
|||
"0x0000000000000000000000000000000000000000000000000000000000000005" |
|||
] |
|||
}; |
|||
|
|||
var e = { |
|||
name: 'Event2', |
|||
inputs: [ |
|||
{"name":"a","type":"bool","indexed":true}, |
|||
{"name":"b","type":"int","indexed":false}, |
|||
{"name":"c","type":"int","indexed":false}, |
|||
{"name":"d","type":"int","indexed":true}, |
|||
{"name":"e","type":"bool","indexed":false} |
|||
] |
|||
}; |
|||
|
|||
// when
|
|||
var impl = event.outputParser(e); |
|||
var result = impl(output); |
|||
|
|||
// then
|
|||
assert.equal(result.event, 'Event2'); |
|||
assert.equal(result.number, 3); |
|||
assert.equal(Object.keys(result.args).length, 5); |
|||
assert.equal(result.args.a, true); |
|||
assert.equal(result.args.b, 75); |
|||
assert.equal(result.args.c, 76); |
|||
assert.equal(result.args.d, 5); |
|||
assert.equal(result.args.e, true); |
|||
|
|||
}); |
|||
}); |
|||
}); |
|||
|
@ -0,0 +1,143 @@ |
|||
var assert = require('assert'); |
|||
var jsonrpc = require('../lib/jsonrpc'); |
|||
|
|||
describe('jsonrpc', function () { |
|||
describe('isValidResponse', function () { |
|||
it('should validate basic jsonrpc response', function () { |
|||
|
|||
// given
|
|||
var response = { |
|||
jsonrpc: '2.0', |
|||
id: 1, |
|||
result: [] |
|||
}; |
|||
|
|||
// when
|
|||
var valid = jsonrpc.isValidResponse(response); |
|||
|
|||
// then
|
|||
assert.equal(valid, true); |
|||
}); |
|||
|
|||
it('should validate basic undefined response', function () { |
|||
|
|||
// given
|
|||
var response = undefined; |
|||
|
|||
// when
|
|||
var valid = jsonrpc.isValidResponse(response); |
|||
|
|||
// then
|
|||
assert.equal(valid, false); |
|||
}); |
|||
|
|||
it('should validate jsonrpc response without jsonrpc field', function () { |
|||
|
|||
// given
|
|||
var response = { |
|||
id: 1, |
|||
result: [] |
|||
}; |
|||
|
|||
// when
|
|||
var valid = jsonrpc.isValidResponse(response); |
|||
|
|||
// then
|
|||
assert.equal(valid, false); |
|||
}); |
|||
|
|||
it('should validate jsonrpc response with wrong jsonrpc version', function () { |
|||
|
|||
// given
|
|||
var response = { |
|||
jsonrpc: '1.0', |
|||
id: 1, |
|||
result: [] |
|||
}; |
|||
|
|||
// when
|
|||
var valid = jsonrpc.isValidResponse(response); |
|||
|
|||
// then
|
|||
assert.equal(valid, false); |
|||
}); |
|||
|
|||
it('should validate jsonrpc response without id number', function () { |
|||
|
|||
// given
|
|||
var response = { |
|||
jsonrpc: '2.0', |
|||
result: [] |
|||
}; |
|||
|
|||
// when
|
|||
var valid = jsonrpc.isValidResponse(response); |
|||
|
|||
// then
|
|||
assert.equal(valid, false); |
|||
}); |
|||
|
|||
it('should validate jsonrpc response with wrong id field', function () { |
|||
|
|||
// given
|
|||
var response = { |
|||
jsonrpc: '2.0', |
|||
id: 'x', |
|||
result: [] |
|||
}; |
|||
|
|||
// when
|
|||
var valid = jsonrpc.isValidResponse(response); |
|||
|
|||
// then
|
|||
assert.equal(valid, false); |
|||
}); |
|||
|
|||
it('should validate jsonrpc response without result field', function () { |
|||
|
|||
// given
|
|||
var response = { |
|||
jsonrpc: '2.0', |
|||
id: 1 |
|||
}; |
|||
|
|||
// when
|
|||
var valid = jsonrpc.isValidResponse(response); |
|||
|
|||
// then
|
|||
assert.equal(valid, false); |
|||
}); |
|||
|
|||
it('should validate jsonrpc response with result field === false', function () { |
|||
|
|||
// given
|
|||
var response = { |
|||
jsonrpc: '2.0', |
|||
id: 1, |
|||
result: false |
|||
}; |
|||
|
|||
// when
|
|||
var valid = jsonrpc.isValidResponse(response); |
|||
|
|||
// then
|
|||
assert.equal(valid, true); |
|||
}); |
|||
|
|||
it('should validate jsonrpc response with result field === 0', function () { |
|||
|
|||
// given
|
|||
var response = { |
|||
jsonrpc: '2.0', |
|||
id: 1, |
|||
result: 0 |
|||
}; |
|||
|
|||
// when
|
|||
var valid = jsonrpc.isValidResponse(response); |
|||
|
|||
// then
|
|||
assert.equal(valid, true); |
|||
}); |
|||
}); |
|||
}); |
@ -0,0 +1,47 @@ |
|||
var assert = require('assert'); |
|||
var jsonrpc = require('../lib/jsonrpc'); |
|||
|
|||
describe('jsonrpc', function () { |
|||
describe('toBatchPayload', function () { |
|||
it('should create basic batch payload', function () { |
|||
|
|||
// given
|
|||
var messages = [{ |
|||
method: 'helloworld' |
|||
}, { |
|||
method: 'test2', |
|||
params: [1] |
|||
}]; |
|||
|
|||
// when
|
|||
var payload = jsonrpc.toBatchPayload(messages); |
|||
|
|||
// then
|
|||
assert.equal(payload instanceof Array, true); |
|||
assert.equal(payload.length, 2); |
|||
assert.equal(payload[0].jsonrpc, '2.0'); |
|||
assert.equal(payload[1].jsonrpc, '2.0'); |
|||
assert.equal(payload[0].method, 'helloworld'); |
|||
assert.equal(payload[1].method, 'test2'); |
|||
assert.equal(payload[0].params instanceof Array, true); |
|||
assert.equal(payload[1].params.length, 1); |
|||
assert.equal(payload[1].params[0], 1); |
|||
assert.equal(typeof payload[0].id, 'number'); |
|||
assert.equal(typeof payload[1].id, 'number'); |
|||
assert.equal(payload[0].id + 1, payload[1].id); |
|||
}); |
|||
|
|||
it('should create batch payload for empty input array', function () { |
|||
|
|||
// given
|
|||
var messages = []; |
|||
|
|||
// when
|
|||
var payload = jsonrpc.toBatchPayload(messages); |
|||
|
|||
// then
|
|||
assert.equal(payload instanceof Array, true); |
|||
assert.equal(payload.length, 0); |
|||
}); |
|||
}); |
|||
}); |
@ -0,0 +1,40 @@ |
|||
var assert = require('assert'); |
|||
var jsonrpc = require('../lib/jsonrpc'); |
|||
|
|||
describe('jsonrpc', function () { |
|||
describe('toPayload', function () { |
|||
it('should create basic payload', function () { |
|||
|
|||
// given
|
|||
var method = 'helloworld'; |
|||
|
|||
// when
|
|||
var payload = jsonrpc.toPayload(method); |
|||
|
|||
// then
|
|||
assert.equal(payload.jsonrpc, '2.0'); |
|||
assert.equal(payload.method, method); |
|||
assert.equal(payload.params instanceof Array, true); |
|||
assert.equal(payload.params.length, 0); |
|||
assert.equal(typeof payload.id, 'number'); |
|||
}); |
|||
|
|||
it('should create payload with params', function () { |
|||
|
|||
// given
|
|||
var method = 'helloworld1'; |
|||
var params = [123, 'test']; |
|||
|
|||
// when
|
|||
var payload = jsonrpc.toPayload(method, params); |
|||
|
|||
// then
|
|||
assert.equal(payload.jsonrpc, '2.0'); |
|||
assert.equal(payload.method, method); |
|||
assert.equal(payload.params.length, 2); |
|||
assert.equal(payload.params[0], params[0]); |
|||
assert.equal(payload.params[1], params[1]); |
|||
assert.equal(typeof payload.id, 'number'); |
|||
}); |
|||
}); |
|||
}); |
@ -0,0 +1,42 @@ |
|||
var assert = require('assert'); |
|||
var utils = require('../lib/utils.js'); |
|||
|
|||
describe('utils', function () { |
|||
describe('extractDisplayName', function () { |
|||
it('should extract display name from method with no params', function () { |
|||
|
|||
// given
|
|||
var test = 'helloworld()'; |
|||
|
|||
// when
|
|||
var displayName = utils.extractDisplayName(test); |
|||
|
|||
// then
|
|||
assert.equal(displayName, 'helloworld'); |
|||
}); |
|||
|
|||
it('should extract display name from method with one param' , function () { |
|||
|
|||
// given
|
|||
var test = 'helloworld1(int)'; |
|||
|
|||
// when
|
|||
var displayName = utils.extractDisplayName(test); |
|||
|
|||
// then
|
|||
assert.equal(displayName, 'helloworld1'); |
|||
}); |
|||
|
|||
it('should extract display name from method with two params' , function () { |
|||
|
|||
// given
|
|||
var test = 'helloworld2(int,string)'; |
|||
|
|||
// when
|
|||
var displayName = utils.extractDisplayName(test); |
|||
|
|||
// then
|
|||
assert.equal(displayName, 'helloworld2'); |
|||
}); |
|||
}); |
|||
}); |
@ -0,0 +1,55 @@ |
|||
var assert = require('assert'); |
|||
var utils = require('../lib/utils.js'); |
|||
|
|||
describe('utils', function () { |
|||
describe('extractTypeName', function () { |
|||
it('should extract type name from method with no params', function () { |
|||
|
|||
// given
|
|||
var test = 'helloworld()'; |
|||
|
|||
// when
|
|||
var typeName = utils.extractTypeName(test); |
|||
|
|||
// then
|
|||
assert.equal(typeName, ''); |
|||
}); |
|||
|
|||
it('should extract type name from method with one param', function () { |
|||
|
|||
// given
|
|||
var test = 'helloworld1(int)'; |
|||
|
|||
// when
|
|||
var typeName = utils.extractTypeName(test); |
|||
|
|||
// then
|
|||
assert.equal(typeName, 'int'); |
|||
}); |
|||
|
|||
it('should extract type name from method with two params', function () { |
|||
|
|||
// given
|
|||
var test = 'helloworld2(int,string)'; |
|||
|
|||
// when
|
|||
var typeName = utils.extractTypeName(test); |
|||
|
|||
// then
|
|||
assert.equal(typeName, 'int,string'); |
|||
}); |
|||
|
|||
it('should extract type name from method with spaces between params', function () { |
|||
|
|||
// given
|
|||
var test = 'helloworld3(int, string)'; |
|||
|
|||
// when
|
|||
var typeName = utils.extractTypeName(test); |
|||
|
|||
// then
|
|||
assert.equal(typeName, 'int,string'); |
|||
}); |
|||
|
|||
}); |
|||
}); |
@ -0,0 +1,45 @@ |
|||
#!/bin/bash |
|||
|
|||
# solves problem with macdeployqt on Qt 5.4 RC |
|||
# http://qt-project.org/forums/viewthread/50118 |
|||
|
|||
BUILD_FOLDER_PATH=$1 |
|||
BUILD_QML_FOLDER_PATH="$BUILD_FOLDER_PATH/Resources/qml" |
|||
BUILD_PLUGINS_FOLDER_PATH="$BUILD_FOLDER_PATH/PlugIns" |
|||
|
|||
if [ ! -d ${BUILD_QML_FOLDER_PATH} ]; then |
|||
# we are not using any qml files |
|||
# gracefully exit |
|||
exit 0 |
|||
fi |
|||
|
|||
declare -a BROKEN_FILES; |
|||
k=0; |
|||
for j in $(find ${BUILD_QML_FOLDER_PATH} -name *.dylib); do |
|||
BROKEN_FILES[${k}]=$j |
|||
|
|||
((k=k+1)) |
|||
done |
|||
|
|||
|
|||
for i in "${BROKEN_FILES[@]}"; do |
|||
REPLACE_STRING="$BUILD_FOLDER_PATH/" |
|||
APP_CONTENT_FILE=${i//$REPLACE_STRING/""} |
|||
IFS='/' read -a array <<< "$APP_CONTENT_FILE" |
|||
LENGTH=${#array[@]} |
|||
LAST_ITEM_INDEX=$((LENGTH-1)) |
|||
FILE=${array[${LENGTH} - 1]} |
|||
|
|||
ORIGINE_PATH=$(find ${BUILD_PLUGINS_FOLDER_PATH} -name ${FILE}) |
|||
ORIGINE_PATH=${ORIGINE_PATH//$REPLACE_STRING/""} |
|||
s="" |
|||
for((l=0;l<${LAST_ITEM_INDEX};l++)) do |
|||
s=$s"../" |
|||
done |
|||
s=$s$ORIGINE_PATH |
|||
echo "s: $s" |
|||
|
|||
REMOVE_BROKEN_ALIAS=$(rm -rf $i) |
|||
RESULT=$(ln -s $s $i) |
|||
done |
|||
|
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue