Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chainweb-Pact SPV Support #192

Merged
merged 130 commits into from
May 22, 2019
Merged
Show file tree
Hide file tree
Changes from 120 commits
Commits
Show all changes
130 commits
Select commit Hold shift + click to select a range
09f2873
stub out tests
emilypi May 1, 2019
43572d7
Merge branch 'master' into emilypi/spv-tests
emilypi May 2, 2019
b9b21fa
testPactExecutionService and related tools
larskuhtz May 2, 2019
8d0ad17
fix Chainweb.Test.CutDB.mine by adding pact validation for new block
larskuhtz May 2, 2019
08b5c08
add test for Chainweb.Test.CutDB
larskuhtz May 2, 2019
72fcede
remove stale SPV test stubs
emilypi May 2, 2019
e6dbd5a
stub pactSPV tests using new handles
emilypi May 2, 2019
ea9d241
suppress INFO message from pact in the tests
larskuhtz May 2, 2019
ec5813c
add function to extend a test cut db
larskuhtz May 2, 2019
ec23aed
remove missing Chainweb.Test.Pact.SPV from chainweb.cabal and replace…
larskuhtz May 3, 2019
a361b8a
Merge branch 'emilypi/spv-tests' of github.com:kadena-io/chainweb-nod…
emilypi May 3, 2019
873d71d
update tests
emilypi May 6, 2019
e0c53ab
add additional transaction builder support (add data), add spv transa…
emilypi May 6, 2019
a18d75b
add txgenerator stub
emilypi May 6, 2019
6828d78
Get initial command running on lars' scaffold
emilypi May 6, 2019
89f8404
imports
emilypi May 6, 2019
3388f2e
sender01 works
emilypi May 6, 2019
aaeee4d
finally, progress
emilypi May 6, 2019
edf1b2f
checkin for lars
emilypi May 7, 2019
9c38161
return results when extending a test cut db
larskuhtz May 8, 2019
6afa7be
Merge branch 'master' into emilypi/spv-tests
emilypi May 8, 2019
bc2f62e
split off SPV from pact service into its own module
emilypi May 8, 2019
31ffc73
small updates
emilypi May 8, 2019
adac8c5
merge master, update Pact.SPV tests
emilypi May 8, 2019
4ff49f2
better error messages
emilypi May 8, 2019
0ba13d2
unpeel txo's
emilypi May 8, 2019
458cdc5
amend documentation for Pact spv
emilypi May 8, 2019
76ef049
simplify
emilypi May 8, 2019
0fa5e82
amend block height
emilypi May 8, 2019
17a3249
stub out the proof creation work
emilypi May 8, 2019
95889d1
remove helper docs
emilypi May 8, 2019
f17a046
paren - TODO: solve the index bounds issue
emilypi May 8, 2019
cbb3c69
updates
emilypi May 9, 2019
c9125f1
add cutdb sync for spv + note with motivation
emilypi May 9, 2019
bf7343f
updates
emilypi May 9, 2019
fa98836
stopping point - potential bug in read-msg
emilypi May 9, 2019
23b1716
hardcode in the object for now
emilypi May 9, 2019
e07f8e4
apply Stuart's fixes to mkSuccess
emilypi May 9, 2019
54e5adf
last updates for the night: pactvalue changes
emilypi May 9, 2019
95bccec
hacked through to the hash
emilypi May 10, 2019
0d7c171
hacked through to the hash 2
emilypi May 10, 2019
c2fc886
Merge branch 'master' into emilypi/spv-tests
emilypi May 10, 2019
953c90f
remove old stubbed test from RemotePactTest
emilypi May 10, 2019
6ee6cd1
condense some of the spv support code
emilypi May 10, 2019
ec1513a
remove unfinished code
emilypi May 10, 2019
3470b0d
update comments, cc code
emilypi May 10, 2019
901ca23
updates
emilypi May 14, 2019
8562857
[WIP]
larskuhtz May 14, 2019
6f10cc4
some updates to lars' code
emilypi May 14, 2019
3d138f1
getTxIdx
larskuhtz May 14, 2019
f0c968b
Merge branch 'master' into emilypi/spv-tests
emilypi May 14, 2019
b9800eb
haddocks for index lookup
emilypi May 14, 2019
3aa8742
improve performance of getTxIdx
larskuhtz May 15, 2019
27c1d11
Merge branch 'emilypi/spv-tests' of github.com:kadena-io/chainweb-nod…
larskuhtz May 15, 2019
6aea89c
updates
emilypi May 15, 2019
c8d737b
stopping point
emilypi May 15, 2019
09cbc87
add iso stubs
emilypi May 15, 2019
78095f3
update todo's
emilypi May 15, 2019
ecb767b
small updates to docs, forget the Natural stuff
emilypi May 15, 2019
8411aff
cleanup
emilypi May 15, 2019
d63b022
we'll need to fix up those tests again
emilypi May 15, 2019
fe15d53
fix up some sigs
emilypi May 15, 2019
12e8bb4
return pactSPV to a good state
emilypi May 15, 2019
40bac41
TODO tomorrow: add outputs to pact test code
emilypi May 15, 2019
c33eac1
back to normal
emilypi May 16, 2019
e67131b
stub out proof to json conversion in tests
emilypi May 16, 2019
388b6ab
ready to go
emilypi May 16, 2019
bfe7a11
transaction generation finally finished
emilypi May 16, 2019
bd25ebc
spv is almost at round trip
emilypi May 16, 2019
7a7152e
works!
emilypi May 16, 2019
0314d9d
add test
emilypi May 16, 2019
16f64e3
fix coin contract syntax
emilypi May 16, 2019
0734664
regen payloads
emilypi May 16, 2019
4bcf5e6
working tests
emilypi May 16, 2019
fe83548
all good
emilypi May 16, 2019
41c33df
small cleanups
emilypi May 16, 2019
1657569
minor cleanups
emilypi May 17, 2019
9f5f6b3
add a slight delay
emilypi May 17, 2019
d437032
add a slight delay
emilypi May 17, 2019
54b9916
cleanups + rename spvError to spvMessage
emilypi May 17, 2019
a8613e7
mine more blocks
emilypi May 17, 2019
ed5120a
wait for cut db to catch up before and after extension
emilypi May 17, 2019
be3c794
add strict evaluation
emilypi May 17, 2019
bfbf252
fixed deadlock
emilypi May 17, 2019
8871cff
Merge branch 'master' into emilypi/spv-tests
emilypi May 17, 2019
0b4ed7d
add test for same chain id
emilypi May 17, 2019
1d76157
Merge branch 'emilypi/spv-tests' of github.com:kadena-io/chainweb-nod…
emilypi May 17, 2019
4df0e93
add awaits to cutdb
emilypi May 17, 2019
23e5e32
names
emilypi May 17, 2019
d048ed6
Merge branch 'master' into emilypi/spv-tests
emilypi May 17, 2019
9bc7441
updates for lars
emilypi May 17, 2019
1dab8af
Merge branch 'emilypi/spv-tests' of github.com:kadena-io/chainweb-nod…
emilypi May 17, 2019
9f349a4
updates
emilypi May 17, 2019
437e977
add pact service test scaffolding
emilypi May 18, 2019
2bcc13e
add pact service test scaffolding
emilypi May 18, 2019
c479650
Merge branch 'master' into emilypi/spv-tests
emilypi May 18, 2019
aa78446
update golden tests
emilypi May 18, 2019
39734a1
Merge branch 'emilypi/spv-tests' of github.com:kadena-io/chainweb-nod…
emilypi May 18, 2019
16ee032
add paren
emilypi May 18, 2019
db2c93e
update genesis payloads
emilypi May 18, 2019
eb8a758
regen golden
emilypi May 18, 2019
4c5ea6a
errors on unique usage now
emilypi May 18, 2019
998b8ab
remove 'outputs' call
emilypi May 18, 2019
ce0f3e9
amend coin contract
emilypi May 18, 2019
4e2a54a
fix error message
emilypi May 18, 2019
9a8d753
fix test mining to avoid local forks
larskuhtz May 18, 2019
d68815f
updates to test code, thanks lars
emilypi May 19, 2019
2e0a00d
switch off test logging
emilypi May 19, 2019
6aa9f4c
add documentation to test code
emilypi May 19, 2019
69be003
update documentation, style points
emilypi May 19, 2019
051a756
small doc updates
emilypi May 19, 2019
1c1e5f3
remove export
emilypi May 19, 2019
f4fb4a7
cosmetic stuff
emilypi May 20, 2019
23c80e9
Merge branch 'master' into emilypi/spv-tests
emilypi May 20, 2019
95f9969
works with gas
emilypi May 20, 2019
95dc11c
Merge branch 'emilypi/spv-tests' of github.com:kadena-io/chainweb-nod…
emilypi May 20, 2019
a9ec67c
update coin contract docs
emilypi May 20, 2019
1be0beb
pare down the tests by making pactTransaction maker more parametrizable
emilypi May 21, 2019
a2f1137
still cleaning up
emilypi May 21, 2019
8689388
remove unnecessary dep
emilypi May 21, 2019
cf28c9a
add additional tests against known attacks
emilypi May 21, 2019
ebbf0af
add Colin's type signatures, address Stuart's comments
emilypi May 21, 2019
7ee5572
fix ea
emilypi May 21, 2019
321a306
forgot an spv support
emilypi May 21, 2019
5daa827
fix up imports
emilypi May 21, 2019
be019fc
Merge branch 'master' into emilypi/spv-tests
emilypi May 21, 2019
f74af43
update spv tests to include blockheader
emilypi May 21, 2019
6d05699
Update test/Chainweb/Test/CutDB.hs
emilypi May 22, 2019
9bf3bf9
Update test/Chainweb/Test/CutDB.hs
emilypi May 22, 2019
f398c52
Update test/Chainweb/Test/CutDB.hs
emilypi May 22, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions chainweb.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,14 @@ library
, Chainweb.Pact.Backend.Types
, Chainweb.Pact.BloomCache
, Chainweb.Pact.PactService
, Chainweb.Pact.RestAPI
, Chainweb.Pact.RestAPI.Orphans
, Chainweb.Pact.RestAPI.Server
, Chainweb.Pact.Service.BlockValidation
, Chainweb.Pact.Service.PactInProcApi
, Chainweb.Pact.Service.PactQueue
, Chainweb.Pact.Service.Types
, Chainweb.Pact.RestAPI
, Chainweb.Pact.RestAPI.Orphans
, Chainweb.Pact.RestAPI.Server
, Chainweb.Pact.SPV
, Chainweb.Pact.TransactionExec
, Chainweb.Pact.Types
, Chainweb.Pact.Utils
Expand Down Expand Up @@ -234,7 +235,7 @@ library
, configuration-tools >= 0.4.1
, connection >=0.2
, containers >= 0.5
, cryptonite >=0.25
, cryptonite >= 0.25
, data-default >=0.7
, data-dword >= 0.3
, deepseq >= 1.4
Expand Down
90 changes: 54 additions & 36 deletions pact/coin-contract/coin.pact
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
(module coin GOVERNANCE

"'coin' represents the Kadena Coin Contract."
"'coin' represents the Kadena Coin Contract. This contract provides both the \
\buy/redeem gas support in the form of 'fund-tx', as well as transfer, \
\credit, debit, coinbase, account creation and query, as well as SPV burn \
\create. To access the coin contract, you may use its fully-qualified name, \
\or issue the '(use coin)' command in the body of a module declaration."


; (implements coin-sig)
; (implements spv-sig)

; --------------------------------------------------------------------------
; Schemas and Tables
; --------------------------------------------------------------------------

(defschema coin-schema
balance:decimal
Expand All @@ -17,15 +17,16 @@
(deftable coin-table:{coin-schema})

(defschema creates-schema
exists:string
exists:bool
)
(deftable creates-table:{creates-schema})

; --------------------------------------------------------------------------
; Capabilities
; --------------------------------------------------------------------------

(defcap GOVERNANCE () (enforce false "upgrade disabled"))
(defcap GOVERNANCE ()
"upgrade disabled"
false)

(defcap TRANSFER ()
"Autonomous capability to protect debit and credit actions"
Expand All @@ -49,7 +50,6 @@

; --------------------------------------------------------------------------
; Coin Contract
; --------------------------------------------------------------------------

(defun buy-gas:string (sender:string total:decimal)
@doc "This function describes the main 'gas buy' operation. At this point \
Expand All @@ -62,7 +62,7 @@

(require-capability (FUND_TX))
(with-capability (TRANSFER)
(debit sender total))
(debit sender total))
)

(defun redeem-gas:string (miner:string miner-guard:guard sender:string total:decimal)
Expand Down Expand Up @@ -141,10 +141,6 @@
(step (redeem-gas miner miner-guard sender total))
)

; --------------------------------------------------------------------------
; Helpers
; --------------------------------------------------------------------------

(defun debit:string (account:string amount:decimal)
@doc "Debit AMOUNT from ACCOUNT balance recording DATE and DATA"

Expand All @@ -168,54 +164,76 @@
@model [(property (> amount 0.0))]

(require-capability (TRANSFER))
(with-default-read coin-table account
{ "balance" : 0.0, "guard" : guard }
{ "balance" := balance, "guard" := retg }
; we don't want to overwrite an existing guard with the user-supplied one
(enforce (= retg guard) "account guards do not match")

(write coin-table account
{ "balance" : (+ balance amount)
, "guard" : retg
}))
)
(with-default-read coin-table account
{ "balance" : 0.0, "guard" : guard }
{ "balance" := balance, "guard" := retg }
; we don't want to overwrite an existing guard with the user-supplied one
(enforce (= retg guard)
"account guards do not match")

(write coin-table account
{ "balance" : (+ balance amount)
, "guard" : retg
})
))

(defun delete-coin (delete-account create-chain-id create-account create-account-guard quantity)
@doc "Burn QUANTITY-many coins for DELETE-ACCOUNT on the current chain, and \
\produce an SPV receipt which may be manually redeemed for an SPV \
\proof. Once a proof is obtained, the user may call 'create-coin' and \
\consume the proof on CREATE-CHAIN-ID, crediting CREATE-ACCOUNT \
\QUANTITY-many coins."

@model [(property (> amount 0.0))]

(with-capability (TRANSFER)
(debit delete-account quantity)

{ "create-chain-id": create-chain-id
, "create-account": create-account
, "create-account-guard": create-account-guard
, "quantity": quantity
, "delete-chain-id": (at "chain-id" (chain-data))
, "delete-block-height": (at 'block-height (chain-data))
, "delete-chain-id": (at 'chain-id (chain-data))
, "delete-account": delete-account
, "delete-tx-hash": (tx-hash)
}))
})
)

(defun create-coin (proof)
(let ((outputs (at "outputs" (verify-spv "TXOUT" proof))))
(enforce (= 1 (length outputs)) "only one tx in outputs")
(bind (at 0 outputs)
@doc "Consume an SPV proof for a number of coins, and credit the account \
\associated with the proof the quantify of coins burned on the source \
\chain by the burn account. Note: must be called on the correct chain \
\id as specified in the proof."

(let ((outputs (verify-spv "TXOUT" proof)))
(bind outputs
{ "create-chain-id":= create-chain-id
, "create-account" := create-account
, "create-account-guard" := create-account-guard
, "quantity" := quantity
, "delete-tx-hash" := delete-tx-hash
, "delete-chain-id" := delete-chain-id
}
(enforce (= (at "chain-id" (chain-data)) create-chain-id "enforce correct create chain ID"))
(let ((create-id (format "%:%" [delete-tx-hash delete-chain-id])))
(with-default-read create-id creates-table

(enforce
(= create-chain-id (at "chain-id" (chain-data)))
"enforce correct create chain ID")

(let ((create-id (format "{}:{}" [delete-tx-hash delete-chain-id])))
(with-default-read creates-table create-id
{ "exists": false }
{ "exists":= exists }
(enforce (not exists) (format "enforce unique usage of %" [create-id]))

(enforce (not exists)
(format "enforce unique usage of {}" [create-id]))

(insert creates-table create-id { "exists": true })

(with-capability (TRANSFER)
(credit create-account create-account-guard quantity)))
)))
)


)

(create-table coin-table)
Expand Down
9 changes: 5 additions & 4 deletions pact/coin-contract/spv-sig.pact
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

"SPV support for the Kadena Coin Contract"

(defun delete-coin:string
(defun delete-coin
( delete-acct:string
create-chain-id:integer
create-account:string quantity:decimal )
create-chain-id:string
create-account:string
quantity:decimal )

@doc "Produce an SPV receipt witnessing a cross-chain transfer from one account to another on a \
\separate chain."
)

(defun create-coin:string (proof create-account-guard:guard)
(defun create-coin:string (proof)
@doc "Consume an SPV receipt witnessing a cross chain transfer from one account to another on a \
\separate chain."
)
Expand Down
10 changes: 5 additions & 5 deletions src/Chainweb/BlockHeader/Genesis/Testnet00Payload.hs

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions src/Chainweb/BlockHeader/Genesis/Testnet01Payload.hs

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions src/Chainweb/Chainweb.hs
Original file line number Diff line number Diff line change
Expand Up @@ -317,19 +317,19 @@ withChainwebInternal
-> IO a
withChainwebInternal conf logger peer rocksDb inner = do
initializePayloadDb v payloadDb
cutMV <- newEmptyMVar
go mempty (toList cids) cutMV
cdbv <- newEmptyMVar
go mempty (toList cids) cdbv
where
payloadDb = newPayloadDb rocksDb
chainLogger cid = addLabel ("chain", toText cid) logger

-- Initialize chain resources
go cs (cid : t) mv =
withChainResources v cid rocksDb peer (chainLogger cid) mempoolConfig mv payloadDb $ \c ->
go (HM.insert cid c cs) t mv
go cs (cid : t) cdbv =
withChainResources v cid rocksDb peer (chainLogger cid) mempoolConfig cdbv payloadDb $ \c ->
go (HM.insert cid c cs) t cdbv

-- Initialize global resources
go cs [] mv = do
go cs [] cdbv = do
let webchain = mkWebBlockHeaderDb v (HM.map _chainResBlockHeaderDb cs)
pact = mkWebPactExecutionService (HM.map _chainResPact cs)
cutLogger = setComponent "cut" logger
Expand All @@ -355,7 +355,7 @@ withChainwebInternal conf logger peer rocksDb inner = do
}

-- update the cutdb mvar used by pact service with cutdb
void $! putMVar mv mCutDb
void $! putMVar cdbv mCutDb

withPactData cs cuts $ \pactData ->
withMinerResources mLogger mConf cwnid mCutDb $ \m ->
Expand Down
4 changes: 2 additions & 2 deletions src/Chainweb/Chainweb/ChainResources.hs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ withChainResources
-> PayloadDb cas
-> (ChainResources logger -> IO a)
-> IO a
withChainResources v cid rdb peer logger mempoolCfg mv payloadDb inner =
withChainResources v cid rdb peer logger mempoolCfg cdbv payloadDb inner =
Mempool.withInMemoryMempool mempoolCfg $ \mempool ->
withPactService v cid (setComponent "pact" logger) mempool mv $ \requestQ -> do
withPactService v cid (setComponent "pact" logger) mempool cdbv $ \requestQ -> do
withBlockHeaderDb rdb v cid $ \cdb -> do

-- replay pact
Expand Down
12 changes: 6 additions & 6 deletions src/Chainweb/Cut/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ testMineWithPayload
:: forall cas cid
. HasChainId cid
=> PayloadCas cas
=> Given WebBlockHeaderDb
=> Given (PayloadDb cas)
=> Nonce
=> WebBlockHeaderDb
-> PayloadDb cas
-> Nonce
-> HashTarget
-> Time Int64
-> PayloadWithOutputs
Expand All @@ -140,11 +140,11 @@ testMineWithPayload
-> Cut
-> PactExecutionService
-> IO (Either MineFailure (T2 BlockHeader Cut))
testMineWithPayload n target t payload nid i c pact =
testMineWithPayload wbdb payloadDb n target t payload nid i c pact =
forM (createNewCut n target t payloadHash nid i c) $ \p@(T2 h _) -> do
validatePayload h payload
addNewPayload (given @(PayloadDb cas)) payload
insertWebBlockHeaderDb h
addNewPayload payloadDb payload
give wbdb (insertWebBlockHeaderDb h)
return p
where
payloadHash = _payloadWithOutputsPayloadHash payload
Expand Down
6 changes: 6 additions & 0 deletions src/Chainweb/CutDB.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ module Chainweb.CutDB
, CutDb
, cutDbWebBlockHeaderDb
, cutDbWebBlockHeaderStore
, cutDbBlockHeaderDb
, cutDbPayloadCas
, cutDbPayloadStore
, cutDbStore
Expand Down Expand Up @@ -226,6 +227,11 @@ cutDbWebBlockHeaderStore :: Getter (CutDb cas) WebBlockHeaderStore
cutDbWebBlockHeaderStore = to _cutDbHeaderStore
{-# INLINE cutDbWebBlockHeaderStore #-}

-- | Access the blockerheaderdb via the cutdb for a given chain id
--
cutDbBlockHeaderDb :: HasChainId cid => cid -> Fold (CutDb cas) BlockHeaderDb
cutDbBlockHeaderDb cid = cutDbWebBlockHeaderDb . ixg (_chainId cid)

-- | Get the current 'Cut', which represent the latest chainweb state.
--
-- This the main API method of chainweb-consensus.
Expand Down
4 changes: 1 addition & 3 deletions src/Chainweb/Miner/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ module Chainweb.Miner.Test ( testMiner ) where
import Control.Concurrent (threadDelay)
import Control.Lens (view, (^?!))

import Data.Reflection (give)
import qualified Data.Sequence as Seq
import qualified Data.Text as T
import Data.Tuple.Strict (T2(..), T3(..))
Expand Down Expand Up @@ -224,8 +223,7 @@ testMiner logFun conf nid cutDb = runForever logFun "Test Miner" $ do
-- INVARIANT: `testMine` will succeed on the first attempt when
-- POW is not used.
--
result <- give payloadDb $ give wcdb
$ testMineWithPayload @cas (Nonce nonce) target ct payload nid cid c pact
result <- testMineWithPayload wcdb payloadDb (Nonce nonce) target ct payload nid cid c pact

case result of
Left BadNonce -> do
Expand Down
Loading