Commit graph

739 commits

Author SHA1 Message Date
Roman Khimov
a6610ba082 core: verify blocks, fix #12
This adds the following verifications:
 * merkleroot check
 * index check
 * timestamp check
 * witnesses verification

VerifyWitnesses is also renamed to verifyTxWitnesses here to not confuse it
with verifyBlockWitnesse and to hide it from external access (no users at the
moment).
2019-10-15 18:58:17 +03:00
Roman Khimov
03c20f1876 core: make ContractState receiver names consistent
Linter isn't happy with our recent changes:

pkg/core/contract_state.go:109:1: receiver name cs should be consistent with previous receiver name a for ContractState
pkg/core/contract_state.go:114:1: receiver name cs should be consistent with previous receiver name a for ContractState
pkg/core/contract_state.go:119:1: receiver name cs should be consistent with previous receiver name a for ContractState

But actually `a` here most probably is a copy-paste from AssetState methods,
so fit the old code to match the new one.
2019-10-15 12:56:25 +03:00
Roman Khimov
aec6a5f029 core/config: make block and transaction verification configurable
Enable transaction verification for privnets and tests, testnet can't
successfuly verify block number 316711 with it enabled and mainnet stops at
105829.
2019-10-15 12:56:25 +03:00
Roman Khimov
56dcff2894 core: invoke contracts for Invocation TXes
They can have some side-effects that future invocations rely on.
2019-10-15 12:56:25 +03:00
Roman Khimov
74590551c4 core: add some interops
This also changes Verify to VerifyTx and VerifyWitnesses, because there is a
need to pass a block for some interop functions.
2019-10-15 12:56:25 +03:00
Roman Khimov
667f346c04 core: improve error message in AddBlock test 2019-10-15 12:56:25 +03:00
Roman Khimov
8266a5ce19 core: export GetContractState/GetScriptHashesForVerifying via Blockchainer
These are gonna be used by interops and are also useful in general.
2019-10-15 12:56:25 +03:00
Roman Khimov
19fd7f844e core: add GetUnspentCoinState() for future interops 2019-10-15 12:56:25 +03:00
Roman Khimov
bfddf9b3f6 core: implement StorageItem for future interops 2019-10-15 12:56:25 +03:00
Roman Khimov
2156d5db73 core: add put/delete functions for ContractState
These are gonna be used by interops.
2019-10-15 12:56:25 +03:00
Roman Khimov
58d9b79fe3 core: avoid duplicates in GetScriptHashesForVerifyingClaim()
As they can break VerifyWitnesses().
2019-10-15 12:56:25 +03:00
Roman Khimov
35824728eb core: add putAssetStateIntoStore()
Will be used by interops.
2019-10-15 12:56:25 +03:00
Roman Khimov
96934cfeab core: return error from verifyOutputs()
Make debugging easier.
2019-10-15 12:56:25 +03:00
Roman Khimov
ab4ff79e02 core: fix typo in GetTransactionResults() 2019-10-15 12:56:25 +03:00
Roman Khimov
cffe8d0ee2 core: return error from verifyResults()
Make it easier to find out what the real problem is.
2019-10-15 12:56:25 +03:00
Roman Khimov
16bc5296cb block: return error from Verify
Don't hide real problem behind the bool value. Makes it easier to identify
problems when looking at log messages.
2019-10-15 12:56:25 +03:00
Roman Khimov
258f397b9a core: append transactions to the block in GetBlock()
We want to get a full block, so it has to have transactions
inside. Unfortunately our tests were used to this wrong behavior and utilized
completely bogus transactions without data that couldn't be persisted, so fix
that also.
2019-10-15 12:56:25 +03:00
Roman Khimov
d007cc00cc vm: add script check functions
These are required for future interops.
2019-10-15 12:56:25 +03:00
Roman Khimov
7ab58ff8cb keys: make public key's IsInfinity() public
It's gonna be used in interops for key validity check.
2019-10-15 12:56:25 +03:00
Roman Khimov
c5a4cfaebe smartcontract: add CreateSignatureRedeemScript()
It's very similar to the CreateMultiSigRedeemScript() and it will be used in
interop functions.
2019-10-15 12:56:25 +03:00
Roman Khimov
238c590ddb core: fix contract state's Properties to use PropertyState
PublishTX only had one of these flags, but newer contracts (created via the
interop function) can have more and these flags are aggregated into one field
that uses PropertyState enumeration (it's used to publish contract, so
supposedly it's also a nice choice for contract state storage).
2019-10-15 12:56:25 +03:00
Roman Khimov
acb7ef7fbd vm: support uint8 and uint64 in makeStackItem()
Convenience to avoid casts, uint64 is also a bit special in that it can't be
converted to int64 without data loss.
2019-10-15 12:56:25 +03:00
Roman Khimov
6b70c5f2bd keys: rename New*FromRawBytes to New*FromASN1
RawBytes is too confusing and may be read as being compatible with
NEO-serialized format.
2019-10-15 12:56:25 +03:00
Roman Khimov
782ca64d92 vm: accept uint16 in makeStackItem() 2019-10-15 12:56:25 +03:00
Roman Khimov
78861485b6 storage: simplify MemoryBatch
It's used a lot and it looks a lot like MemoryStore, it just needs not to
return errors from Put and Delete, so make it use MemoryStore internally with
adjusted interface.
2019-10-15 12:56:25 +03:00
Roman Khimov
13bf2618ef storage: improve PutBatch for MemoryStore
Make it look more like a real transaction, put/delete things with a single
lock. Make a copy of value in Put also, just for safety purposes, no one knows
how this value slice can be used after the Put.
2019-10-15 12:56:25 +03:00
Roman Khimov
3ada92944a storage: drop Len from the Batch interface
It's almost meaningless now and we can easily live without it.
2019-10-15 12:56:25 +03:00
Roman Khimov
e111892653 storage: redo DB testing
Make generic tests for all Store implementations, deduplicate tests. Implement
Delete() tests, test Seek() better, add LevelDB tests (finally!).
2019-10-15 12:56:25 +03:00
Roman Khimov
48b6a427cf storage: add Delete method for Batch and Store
It's gonna be used by Storage and Contract interops, both can delete their
data.
2019-10-07 19:40:11 +03:00
Roman Khimov
add9368e9d storage: use strings as keys for memory batch
Using pointers is just plain wrong here, because the batch can be updated with
newer values for the same keys.

Fixes Seek() to use HasPrefix also because this is the intended behavior.
2019-10-07 17:05:53 +03:00
Roman Khimov
aab2f9a837
Merge pull request #417 from nspcc-dev/various-verification-fixes2
Various verification fixes part 2, which focuses on VM improvements
necessary to make tx verification work. It's mostly related to interop
functionality, but doesn't add interops at the moment. Fixes #295 along the way.
2019-10-04 16:17:24 +03:00
Roman Khimov
dca332f333 vm: use new Context.Next() to properly dump programs
Fix #295, deduplicate code and add `inspect` parameter to the vm command to
dump AVMs (`contract inspect` works with Go code).
2019-10-04 16:13:39 +03:00
Roman Khimov
53a3b18652 vm: completely separate instruction read and execution phases
Make Context.Next() return both opcode and instruction parameter if any. This
simplifies some code and needed to deal with #295.
2019-10-04 16:13:39 +03:00
Roman Khimov
1bf232ad50 vm: introduce TryBool() for Element and use it in VerifyWitnesses
Script can return non-bool results that can still be converted to bool
according to the usual VM rules. Unfortunately Bool() panics if this
conversion fails which is OK for things done in vm.execute(), but certainly
not for VerifyWitnesses(), thus there is a need for TryBool() that will just
return an error in this case.
2019-10-04 16:13:39 +03:00
Roman Khimov
8441b31b4b vm: accept uint32 in makeStackItem()
Interop services routinely push such things (block index, blockchain height)
onto the stack.
2019-10-04 16:13:39 +03:00
Roman Khimov
d62a367900 vm: add Value() method to Element
It gives access to the internal value's Value() which is essential for interop
functions that need to get something from InteropItems. And it also simplifies
some already existing code along the way.
2019-10-04 16:13:39 +03:00
Roman Khimov
0c963875af vm: check for fault flag first in Run()
Switch cases are evaluated sequentially and the fault case is top-priority to
handle.
2019-10-04 16:13:39 +03:00
Roman Khimov
705c7f106f vm: don't panic if there is no result in PopResult()
This function is intended to be ran outside of the execute's panic recovery
mechanism, so it shouldn't panic if there is no result.
2019-10-04 16:13:39 +03:00
Roman Khimov
a357d99624 vm: introduce MaxArraySize constant
This is both for #373 and for interop functions that have to check some
inputs.
2019-10-04 16:13:39 +03:00
Roman Khimov
cfa0c13322 vm: add InteropItem type for interop data
This is an opaque data item that is to be used by the interop functions.
2019-10-04 16:13:39 +03:00
Roman Khimov
da2156f955 vm: add batched RegisterInteropFuncs 2019-10-04 16:13:39 +03:00
Roman Khimov
26e3b6abbe vm: extend interops to contain price
The same way C# node does.
2019-10-04 16:13:39 +03:00
Roman Khimov
ceca9cdb67 core/vm: implement contract storage and script retrieval
Fixes script invocations via the APPCALL instruction. Adjust contract state
field types accordingly.
2019-10-04 16:13:39 +03:00
Roman Khimov
e83dc94744
Merge pull request #415 from nspcc-dev/various-verification-fixes
This set of fixes allows Testnet synchronization (with verifyBlocks
set to true) to proceed up to the block number 4586, the block number
4587 fails at APPCALL invocation at the moment, but that's for the next
set of fixes.
2019-10-01 15:14:52 +03:00
Roman Khimov
b7c1efe7e3 core: fix References() result key type
If the block references two ouputs in some other transaction the code failed
to verify it because of key collision. C# code implements it properly by using
full CoinReference type as a key, so let's do it in a similar fashion.
2019-10-01 13:41:26 +03:00
Roman Khimov
8537700b7b core: sort hashes and witnesses in VerifyWitnesses()
Fixes failure to verify some multi-witnesses transactions in Testnet chain. C#
code contains similar logic.
2019-10-01 13:41:26 +03:00
Roman Khimov
dd0dd2d031 core: add claim-specific GetScriptHashesForVerifying()
Claim transactions have different logic in C# node, so we need to
implement it too. It's not the most elegant way to fix it, but let's make it
work first and then refactor if and where needed. Fixes verification of Claim
transactions.
2019-10-01 13:41:26 +03:00
Roman Khimov
854fb187a3 core: verify transactions in AddBlock() 2019-10-01 13:41:26 +03:00
Roman Khimov
1095abb04c core: fix default asset expiration to match C# code
Fixes bogus verification failures for non-native assets.
2019-10-01 13:41:26 +03:00
Roman Khimov
832c56f97d core: fix DutyFlag check in GetScriptHashesForVerifying()
It's a flag (used by Share and Invoice asset types), so it should be checked
like a flag, the same way C# node does.
2019-10-01 13:41:26 +03:00