mirror of
https://github.com/status-im/nimbus-eth1.git
synced 2025-01-27 04:26:07 +00:00
First version of tests
This commit is contained in:
parent
8a2aa81a44
commit
e1c04e587f
@ -214,7 +214,12 @@ method getGasRemaining*(c: BaseComputation): Int256 =
|
|||||||
# Context Manager API
|
# Context Manager API
|
||||||
#
|
#
|
||||||
|
|
||||||
template inBaseComputation*(c: untyped, handler: untyped): untyped =
|
template inComputation*(c: untyped, handler: untyped): untyped =
|
||||||
|
# use similarly to the python manager
|
||||||
|
#
|
||||||
|
# inComputation(computation):
|
||||||
|
# stuff
|
||||||
|
|
||||||
`c`.logger.debug(
|
`c`.logger.debug(
|
||||||
"COMPUTATION STARTING: gas: $1 | from: $2 | to: $3 | value: $4 | depth: $5 | static: $6" % [
|
"COMPUTATION STARTING: gas: $1 | from: $2 | to: $3 | value: $4 | depth: $5 | static: $6" % [
|
||||||
$`c`.msg.gas,
|
$`c`.msg.gas,
|
||||||
|
@ -146,10 +146,10 @@ const
|
|||||||
GASEXPBYTE = 10
|
GASEXPBYTE = 10
|
||||||
GAS_MEMORY* = 3.Int256
|
GAS_MEMORY* = 3.Int256
|
||||||
GAS_TX_CREATE* = 32000.Int256
|
GAS_TX_CREATE* = 32000.Int256
|
||||||
GASTXDATAZERO = 4
|
GAS_TX_DATA_ZERO* = 4.Int256
|
||||||
GASTXDATANONZERO = 68
|
GAS_TX_DATA_NON_ZERO* = 68.Int256
|
||||||
GASTX = 21000
|
GAS_TX* = 21000.Int256
|
||||||
GASLOG = 375
|
GAS_LOG* = 375.Int256
|
||||||
GASLOGDATA = 8
|
GASLOGDATA = 8
|
||||||
GASLOGTOPIC = 375
|
GASLOGTOPIC = 375
|
||||||
GASSHA3 = 30
|
GASSHA3 = 30
|
||||||
@ -191,8 +191,8 @@ const
|
|||||||
SECPK1Gy = 0
|
SECPK1Gy = 0
|
||||||
SECPK1G = (SECPK1Gx, SECPK1Gy)
|
SECPK1G = (SECPK1Gx, SECPK1Gy)
|
||||||
EMPTYUNCLEHASH = cstring"\\x1d\\xccM\\xe8\\xde\\xc7]z\\xab\\x85\\xb5g\\xb6\\xcc\\xd4\\x1a\\xd3\\x12E\\x1b\\x94\\x8at\\x13\\xf0\\xa1B\\xfd@\\xd4\\x93G"
|
EMPTYUNCLEHASH = cstring"\\x1d\\xccM\\xe8\\xde\\xc7]z\\xab\\x85\\xb5g\\xb6\\xcc\\xd4\\x1a\\xd3\\x12E\\x1b\\x94\\x8at\\x13\\xf0\\xa1B\\xfd@\\xd4\\x93G"
|
||||||
GENESISBLOCKNUMBER = 0
|
GENESIS_BLOCK_NUMBER* = 0.Int256
|
||||||
GENESISDIFFICULTY = 131072
|
GENESIS_DIFFICULTY* = 131072.Int256
|
||||||
GENESISGASLIMIT = 3141592
|
GENESISGASLIMIT = 3141592
|
||||||
GENESISPARENTHASH = ZEROHASH32
|
GENESISPARENTHASH = ZEROHASH32
|
||||||
GENESISCOINBASE = ZEROADDRESS
|
GENESISCOINBASE = ZEROADDRESS
|
||||||
|
3
src/utils/address.nim
Normal file
3
src/utils/address.nim
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
proc toCanonicalAddress*(address: cstring): cstring =
|
||||||
|
# TODO
|
||||||
|
address
|
54
tests/helpers.nim
Normal file
54
tests/helpers.nim
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
import
|
||||||
|
.. / src / [utils / hexadecimal, utils / address, chain, constants, db, db / chain, vm / forks / frontier]
|
||||||
|
|
||||||
|
# This block is a child of the genesis defined in the chain fixture above and contains a single tx
|
||||||
|
# that transfers 10 wei from 0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b to
|
||||||
|
# 0x095e7baea6a6c7c4c2dfeb977efac326af552d87.
|
||||||
|
var validBlockRlp = decodeHex(
|
||||||
|
"0xf90260f901f9a07285abd5b24742f184ad676e31f6054663b3529bc35ea2fcad8a3e0f642a46f7a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347948888f1f195afa192cfee860698584c030f4c9db1a0964e6c9995e7e3757e934391b4f16b50c20409ee4eb9abd4c4617cb805449b9aa053d5b71a8fbb9590de82d69dfa4ac31923b0c8afce0d30d0d8d1e931f25030dca0bc37d79753ad738a6dac4921e57392f145d8887476de3f783dfa7edae9283e52b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008302000001832fefd8825208845754132380a0194605bacef646779359318c7b5899559a5bf4074bbe2cfb7e1b83b1504182dd88e0205813b22e5a9cf861f85f800a82c35094095e7baea6a6c7c4c2dfeb977efac326af552d870a801ba0f3266921c93d600c43f6fa4724b7abae079b35b9e95df592f95f9f3445e94c88a012f977552ebdb7a492cf35f3106df16ccb4576ebad4113056ee1f52cbe4978c1c0") # noqa: E501
|
||||||
|
|
||||||
|
type
|
||||||
|
PrivateKey = object # TODO
|
||||||
|
publicKey*: cstring
|
||||||
|
|
||||||
|
proc testChain*: Chain =
|
||||||
|
# Return a Chain object containing just the genesis block.
|
||||||
|
#
|
||||||
|
# This Chain does not perform any validation when importing new blocks.
|
||||||
|
#
|
||||||
|
# The Chain's state includes one funded account and a private key for it, which can be found in
|
||||||
|
# the funded_address and private_keys variables in the chain itself.
|
||||||
|
# Disable block validation so that we don't need to construct finalized blocks.
|
||||||
|
|
||||||
|
var klass = configureChain(
|
||||||
|
"TestChainWithoutBlockValidation",
|
||||||
|
constants.GENESIS_BLOCK_NUMBER,
|
||||||
|
FrontierVM,
|
||||||
|
importBlock=false,
|
||||||
|
validateBlock=false)
|
||||||
|
|
||||||
|
var privateKey = PrivateKey(publicKey: cstring"0x45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8") # TODO
|
||||||
|
var fundedAddress = privateKey.publicKey.toCanonicalAddress()
|
||||||
|
var initialBalance = 100_000_000
|
||||||
|
var genesisParams = GenesisParams(
|
||||||
|
blockNumber: constants.GENESIS_BLOCK_NUMBER,
|
||||||
|
difficulty: constants.GENESIS_DIFFICULTY,
|
||||||
|
gasLimit: constants.GENESIS_GAS_LIMIT,
|
||||||
|
parentHash: constants.GENESIS_PARENT_HASH,
|
||||||
|
coinbase: constants.GENESIS_COINBASE,
|
||||||
|
nonce: constants.GENESIS_NONCE,
|
||||||
|
mixHash: constants.GENESIS_MIX_HASH,
|
||||||
|
extraData: constants.GENESIS_EXTRA_DATA,
|
||||||
|
timestamp: 1501851927,
|
||||||
|
stateRoot: decodeHex("0x9d354f9b5ba851a35eced279ef377111387197581429cfcc7f744ef89a30b5d4"))
|
||||||
|
|
||||||
|
var genesisState = GenesisState(
|
||||||
|
fundedAddress: FundedAddress(
|
||||||
|
balance: initialBalance,
|
||||||
|
nonce: 0,
|
||||||
|
code: cstring""))
|
||||||
|
|
||||||
|
result = klass.fromGenesis(newBaseChainDB(getDbBackend()), genesisParams, genesisState)
|
||||||
|
result.fundedAddress = fundedAddress
|
||||||
|
result.fundedAddressInitialBalance = initialBalance
|
||||||
|
result.fundedAddressPrivateKey = privateKey
|
28
tests/vm_test.nim
Normal file
28
tests/vm_test.nim
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import
|
||||||
|
unittest,
|
||||||
|
helpers, .. / src / [db / backends / memory, db / chain, constants, utils / hexadecimal]
|
||||||
|
|
||||||
|
suite "vm":
|
||||||
|
test "apply no validation":
|
||||||
|
var
|
||||||
|
chain = testChain()
|
||||||
|
vm = chain.getVM()
|
||||||
|
txIdx = len(vm.`block`.transactions)
|
||||||
|
recipient = decodeHex("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0c")
|
||||||
|
amount = 100.Int256
|
||||||
|
|
||||||
|
var from = chain.fundedAddress
|
||||||
|
var tx = newTransaction(vm, from, recipient, amount, chain.fundedAddressPrivateKey)
|
||||||
|
var (computation, _) = vm.applyTransaction(tx)
|
||||||
|
var accessLogs = computation.vmState.accessLogs
|
||||||
|
|
||||||
|
check(not computation.isError)
|
||||||
|
|
||||||
|
var txGas = tx.gasPrice * constants.GAS_TX
|
||||||
|
inDb(vm.state.stateDb(readOnly=true)):
|
||||||
|
check(db.getBalance(from) == chain.fundedAddressInitialBalance - amount - txGas)
|
||||||
|
check(db.getBalance(recipient) == amount)
|
||||||
|
var b = vm.`block`
|
||||||
|
check(b.transactions[txIdx] == tx)
|
||||||
|
check(b.header.gasUsed == constants.GAS_TX)
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user