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

Finish Rosetta Endpoints #1050

Merged
merged 57 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
6040ebe
adds account balance handler
LindaOrtega May 12, 2020
85f6152
slight cleanup -- wip block info in local
LindaOrtega May 13, 2020
9ee7a94
Made account balance command use term name
LindaOrtega May 13, 2020
722b31a
WIP start setting up types for txlog lookup
LindaOrtega May 14, 2020
4c563f2
more wip
LindaOrtega May 15, 2020
dbd0deb
Merge branch 'rosetta-account-endpoint' of github.com:kadena-io/chain…
LindaOrtega May 15, 2020
5963ee9
WIP - adds logic for creating rosetta tx given tx logs and req key
LindaOrtega May 16, 2020
7ec9f7b
WIP - code cleanup, finalized tx log parsing logic
LindaOrtega May 18, 2020
ade7063
Merge branch 'master' of github.com:kadena-io/chainweb-node into rose…
LindaOrtega May 18, 2020
690d1fe
Adds logic for finding block header in curr fork by height and or hash
LindaOrtega May 18, 2020
65adb1c
Compiles -- adds logic for getting payload outputs
LindaOrtega May 19, 2020
c28e3aa
tested locally, found couple of bugs
LindaOrtega May 19, 2020
b5fdde4
fixes: genesis parsing issues, missing block coinbase tx
LindaOrtega May 19, 2020
a3d9d4d
Merge branch 'master' into rosetta-account-endpoint
emilypi May 19, 2020
cfdd3ce
Merge branch 'rosetta-account-endpoint' of github.com:kadena-io/chain…
LindaOrtega May 19, 2020
5debe86
fixes tx matching, improves performance
LindaOrtega May 20, 2020
5576553
add mempool endpoint impl
emilypi May 20, 2020
f296b3b
Merge branch 'master' into rosetta-mempool-endpoint
emilypi May 20, 2020
0472534
final /block cleanup
LindaOrtega May 20, 2020
1a3cf4a
Merge branch 'master' into rosetta-block-endpoints
LindaOrtega May 20, 2020
578ab92
Some refactoring/documentation in prep for /block/transaction
LindaOrtega May 20, 2020
03488b0
Merge branch 'rosetta-block-endpoints' of github.com:kadena-io/chainw…
LindaOrtega May 20, 2020
8c690d9
logic for /block/transaction compiling
LindaOrtega May 21, 2020
dd1819a
finish up /block/transaction and /network/options
LindaOrtega May 21, 2020
a0bda21
WIP - begin refactoring
LindaOrtega May 21, 2020
895079b
force highwater mark
emilypi May 22, 2020
7ce3e93
use mvar instead
emilypi May 22, 2020
c06d7ae
updates 4 lars
emilypi May 22, 2020
74242e2
encodeToText
emilypi May 22, 2020
952cc14
cleanup
emilypi May 22, 2020
69a386f
update rosetta + kpkgs
emilypi May 22, 2020
a9d11f6
Refactoring
LindaOrtega May 26, 2020
470b0f0
Merge branch 'rosetta-mempool-endpoint' of github.com:kadena-io/chain…
LindaOrtega May 26, 2020
7c6a356
fix failing pact tx log test
LindaOrtega May 27, 2020
674f913
Adds unit tests for checking tx log matching logic
LindaOrtega May 27, 2020
a3872fd
Add unit test for check kda to rosetta amount and validatenetworkId f…
LindaOrtega May 27, 2020
856998c
Start TransactionId cleanup, remove extra operation status
LindaOrtega May 27, 2020
d07c9f5
Address review comments
LindaOrtega May 27, 2020
8c7c07b
Merge branch 'master' of github.com:kadena-io/chainweb-node into rose…
LindaOrtega May 27, 2020
4e52412
rosetta error were replicated
LindaOrtega May 30, 2020
7f0796d
Adds historical account lookup
LindaOrtega Jun 3, 2020
81cdfd6
Fix rosetta validator issues -- remediations, error codes
LindaOrtega Jun 8, 2020
c28f21e
Fix mempool error -- use hastextrepresentation for transactionId->Tra…
LindaOrtega Jun 9, 2020
4201c99
Merge branch 'master' of github.com:kadena-io/chainweb-node into rose…
LindaOrtega Jun 9, 2020
e479699
undo changes no longer needed
LindaOrtega Jun 9, 2020
5d0c019
switch rosetta config back on
emilypi Jun 11, 2020
82935e8
first pass at dealing with edge case where coinbase fails
LindaOrtega Jun 11, 2020
1fcb891
Merge branch 'expose-rosetta-config' of github.com:kadena-io/chainweb…
LindaOrtega Jun 11, 2020
ee355f5
Review edits -- refactor matching logic, etc
LindaOrtega Jun 15, 2020
75e7bb7
Review edits -- factor out tx matching and block helpers into own module
LindaOrtega Jun 15, 2020
aabc8a5
Update src/Chainweb/Pact/Backend/InMemoryCheckpointer.hs
LindaOrtega Jun 15, 2020
21e7b72
Merge branch 'master' of github.com:kadena-io/chainweb-node into rose…
LindaOrtega Jun 15, 2020
3c40718
Merge branch 'rosetta-block-endpoints' of github.com:kadena-io/chainw…
LindaOrtega Jun 15, 2020
560d95f
Cleaned up some unused rosetta error types
LindaOrtega Jun 15, 2020
8634918
Disable checking account address is valid Name
LindaOrtega Jun 15, 2020
808bc69
Finish undoing too strict sanitization of account key
LindaOrtega Jun 17, 2020
c9f27f9
Merge branch 'master' into rosetta-block-endpoints
LindaOrtega Jun 18, 2020
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
4 changes: 4 additions & 0 deletions chainweb.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,10 @@ library
, Chainweb.RestAPI.NodeInfo
, Chainweb.RestAPI.Orphans
, Chainweb.RestAPI.Utils
, Chainweb.Rosetta.Internal
, Chainweb.Rosetta.RestAPI
, Chainweb.Rosetta.RestAPI.Server
, Chainweb.Rosetta.Util
, Chainweb.SPV
, Chainweb.SPV.CreateProof
, Chainweb.SPV.VerifyProof
Expand Down Expand Up @@ -463,6 +465,7 @@ test-suite chainweb-tests
Chainweb.Test.Pact.Utils
Chainweb.Test.RestAPI
Chainweb.Test.RestAPI.Client_
Chainweb.Test.Rosetta
Chainweb.Test.Roundtrips
Chainweb.Test.SPV
Chainweb.Test.Store.CAS
Expand Down Expand Up @@ -522,6 +525,7 @@ test-suite chainweb-tests
, resource-pool >= 0.2
, resourcet >= 1.2
, retry >= 0.7
, rosetta >= 1.0
, scheduler >= 1.4
, semialign >= 1
, servant >= 0.16
Expand Down
3 changes: 3 additions & 0 deletions src/Chainweb/Mempool/Mempool.hs
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,9 @@ instance FromJSON TransactionHash where
p :: Text -> Either SomeException TransactionHash
!p = (TransactionHash . SB.toShort <$>) . decodeB64UrlNoPaddingText

instance HasTextRepresentation TransactionHash where
toText (TransactionHash th) = encodeB64UrlNoPaddingText $ SB.fromShort th
fromText = (TransactionHash . SB.toShort <$>) . decodeB64UrlNoPaddingText

------------------------------------------------------------------------------
data TransactionMetadata = TransactionMetadata {
Expand Down
1 change: 1 addition & 0 deletions src/Chainweb/Pact/Backend/InMemoryCheckpointer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ initInMemoryCheckpointEnv loggers logger ver cid = do
, _cpRegisterProcessedTx = doRegisterSuccessful inmem
, _cpLookupProcessedTx = doLookupSuccessful inmem
, _cpGetBlockHistory = \_ _ -> error "unimplemented"
, _cpGetHistoricalLookup = \_ _ _ -> error "unimplemented"
}
, _cpeLogger = logger
})
Expand Down
62 changes: 48 additions & 14 deletions src/Chainweb/Pact/Backend/RelationalCheckpointer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ initRelationalCheckpointer' bstate sqlenv loggr v cid = do
, _cpRegisterProcessedTx = doRegisterSuccessful db
, _cpLookupProcessedTx = doLookupSuccessful db
, _cpGetBlockHistory = doGetBlockHistory db
, _cpGetHistoricalLookup = doGetHistoricalLookup db
}
, _cpeLogger = loggr
})
Expand Down Expand Up @@ -275,31 +276,25 @@ doGetBlockHistory :: FromJSON v => Db -> BlockHeader -> Domain k v -> IO BlockTx
doGetBlockHistory dbenv blockHeader d = runBlockEnv dbenv $ do
callDb "doGetBlockHistory" $ \db -> do
endTxId <- getEndTxId db bHeight (_blockHash blockHeader)
startTxId <- getEndTxId db (pred bHeight) (_blockParent blockHeader)
startTxId <- if (bHeight == genesisHeight v cid)
LindaOrtega marked this conversation as resolved.
Show resolved Hide resolved
then pure 0 -- genesis block
sirlensalot marked this conversation as resolved.
Show resolved Hide resolved
else getEndTxId db (pred bHeight) (_blockParent blockHeader)
history <- queryHistory db (domainTableName d) startTxId endTxId
return $! BlockTxHistory $ foldl' groupByTxid mempty history
where

v = _blockChainwebVersion blockHeader
cid = _blockChainId blockHeader
bHeight = _blockHeight blockHeader

groupByTxid :: Ord a => M.Map a [b] -> (a,b) -> M.Map a [b]
groupByTxid r (t,l) = M.insertWith (++) t [l] r

getEndTxId db bhi bha = do
r <- qry db
"SELECT endingtxid FROM BlockHistory WHERE blockheight = ? and hash = ?;"
[SInt $ fromIntegral $ bhi, SBlob $ encode $ bha]
[RInt]
case r of
[[SInt tid]] -> return tid
[] -> throwM $ BlockHeaderLookupFailure $ "doGetBlockHistory: not in db: " <>
sshow (bhi,bha)
_ -> internalError $ "doGetBlockHistory: expected single-row int result, got " <> sshow r

-- Start index is inclusive, while ending index is not.
-- `endingtxid` in a block is the beginning txid of the following block.
queryHistory :: Database -> Utf8 -> Int64 -> Int64 -> IO [(TxId,TxLog Value)]
queryHistory db tableName s e = do
let sql = "SELECT txid, rowkey, rowdata FROM [" <> tableName <>
"] WHERE txid > ? AND txid <= ?"
"] WHERE txid >= ? AND txid < ?"
r <- qry db sql
[SInt s,SInt e]
[RInt,RText,RBlob]
Expand All @@ -308,3 +303,42 @@ doGetBlockHistory dbenv blockHeader d = runBlockEnv dbenv $ do
err -> internalError $
"readHistoryResult': Expected single row with three columns as the \
\result, got: " <> T.pack (show err)

getEndTxId :: Database -> BlockHeight -> BlockHash -> IO Int64
getEndTxId db bhi bha = do
r <- qry db
"SELECT endingtxid FROM BlockHistory WHERE blockheight = ? and hash = ?;"
[SInt $ fromIntegral $ bhi, SBlob $ encode $ bha]
[RInt]
case r of
[[SInt tid]] -> return tid
[] -> throwM $ BlockHeaderLookupFailure $ "doGetBlockHistory: not in db: " <>
sshow (bhi,bha)
_ -> internalError $ "doGetBlockHistory: expected single-row int result, got " <> sshow r

doGetHistoricalLookup
:: FromJSON v
=> Db
-> BlockHeader
-> Domain k v
-> RowKey
-> IO (Maybe (TxLog Value))
doGetHistoricalLookup dbenv blockHeader d k = runBlockEnv dbenv $ do
callDb "doGetHistoricalLookup" $ \db -> do
endTxId <- getEndTxId db bHeight (_blockHash blockHeader)
sirlensalot marked this conversation as resolved.
Show resolved Hide resolved
latestEntry <- queryHistoryLookup db (domainTableName d) endTxId (convRowKey k)
return $! latestEntry
where
bHeight = _blockHeight blockHeader

queryHistoryLookup :: Database -> Utf8 -> Int64 -> Utf8 -> IO (Maybe (TxLog Value))
queryHistoryLookup db tableName e rowKeyName = do
let sql = "SELECT rowKey, rowdata FROM [" <> tableName <>
"] WHERE txid < ? AND rowkey = ? ORDER BY txid DESC LIMIT 1;"
r <- qry db sql
[SInt e, SText rowKeyName]
[RText, RBlob]
case r of
[[SText key, SBlob value]] -> Just <$> toTxLog d key value
[] -> pure Nothing
_ -> internalError $ "doGetHistoricalLookup: expected single-row result, got " <> sshow r
2 changes: 2 additions & 0 deletions src/Chainweb/Pact/Backend/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ data Checkpointer = Checkpointer
, _cpLookupProcessedTx :: !(P.PactHash -> IO (Maybe (T2 BlockHeight BlockHash)))
, _cpGetBlockHistory :: !(
forall k v . (FromJSON v) => BlockHeader -> Domain k v -> IO BlockTxHistory)
, _cpGetHistoricalLookup :: !(
forall k v . (FromJSON v) => BlockHeader -> Domain k v -> RowKey -> IO (Maybe (TxLog Value)))
}

data CheckpointEnv = CheckpointEnv
Expand Down
10 changes: 10 additions & 0 deletions src/Chainweb/Pact/PactService.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module Chainweb.Pact.PactService
, execLookupPactTxs
, execPreInsertCheckReq
, execBlockTxHistory
, execHistoricalLookup
, initPactService
, readCoinAccount
, readAccountBalance
Expand Down Expand Up @@ -394,6 +395,10 @@ serviceRequests logFn memPoolAccess reqQ = do
trace logFn "Chainweb.Pact.PactService.execBlockTxHistory" bh 1 $
tryOne "execBlockTxHistory" resultVar $
execBlockTxHistory bh d
HistoricalLookupMsg (HistoricalLookupReq bh d k resultVar) -> do
trace logFn "Chainweb.Pact.PactService.execHistoricalLookup" bh 1 $
tryOne "execHistoricalLookup" resultVar $
execHistoricalLookup bh d k

toPactInternalError e = Left $ PactInternalError $ T.pack $ show e

Expand Down Expand Up @@ -1418,6 +1423,11 @@ execBlockTxHistory bh (Domain' d) = do
!cp <- getCheckpointer
liftIO $ _cpGetBlockHistory cp bh d

execHistoricalLookup :: BlockHeader -> Domain' -> P.RowKey -> PactServiceM cas (Maybe (P.TxLog A.Value))
execHistoricalLookup bh (Domain' d) k = do
!cp <- getCheckpointer
liftIO $ _cpGetHistoricalLookup cp bh d k

execPreInsertCheckReq
:: PayloadCasLookup cas
=> Vector ChainwebTransaction
Expand Down
16 changes: 16 additions & 0 deletions src/Chainweb/Pact/Service/BlockValidation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@ module Chainweb.Pact.Service.BlockValidation
, lookupPactTxs
, pactPreInsertCheck
, pactBlockTxHistory
, pactHistoricalLookup
) where


import Control.Concurrent.MVar.Strict

import Data.Aeson (Value)
import Data.Tuple.Strict
import Data.Vector (Vector)

import Pact.Types.Command
import Pact.Types.Hash
import Pact.Types.Persistence (RowKey, TxLog)

import Chainweb.BlockHash
import Chainweb.BlockHeader
Expand Down Expand Up @@ -109,3 +112,16 @@ pactBlockTxHistory bh d reqQ = do
let !msg = BlockTxHistoryMsg req
addRequest reqQ msg
return resultVar

pactHistoricalLookup
:: BlockHeader
-> Domain'
-> RowKey
-> PactQueue
-> IO (MVar (Either PactException (Maybe (TxLog Value))))
pactHistoricalLookup bh d k reqQ = do
resultVar <- newEmptyMVar
let !req = HistoricalLookupReq bh d k resultVar
let !msg = HistoricalLookupMsg req
addRequest reqQ msg
return resultVar
11 changes: 11 additions & 0 deletions src/Chainweb/Pact/Service/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ data RequestMsg = NewBlockMsg NewBlockReq
| LookupPactTxsMsg LookupPactTxsReq
| PreInsertCheckMsg PreInsertCheckReq
| BlockTxHistoryMsg BlockTxHistoryReq
| HistoricalLookupMsg HistoricalLookupReq
| CloseMsg
deriving (Show)

Expand Down Expand Up @@ -170,6 +171,16 @@ instance Show BlockTxHistoryReq where
show (BlockTxHistoryReq h d _) =
"BlockTxHistoryReq@" ++ show h ++ ", " ++ show d

data HistoricalLookupReq = HistoricalLookupReq
{ _historicalLookupHeader :: !BlockHeader
, _historicalLookupDomain :: !Domain'
, _historicalLookupRowKey :: !RowKey
, _historicalLookupResult :: !(PactExMVar (Maybe (TxLog Value)))
}
instance Show HistoricalLookupReq where
show (HistoricalLookupReq h d k _) =
"HistoricalLookupReq@" ++ show h ++ ", " ++ show d ++ ", " ++ show k

data SpvRequest = SpvRequest
{ _spvRequestKey :: RequestKey
, _spvTargetChainId :: Pact.ChainId
Expand Down
4 changes: 3 additions & 1 deletion src/Chainweb/RestAPI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,9 @@ someChainwebServer v dbs mr (HeaderStream hs) (Rosetta r) =
<> PactAPI.somePactServers v pacts
<> maybe mempty (Mining.someMiningServer v) mr
<> maybe mempty (someHeaderStreamServer v) (bool Nothing cuts hs)
<> maybe mempty (bool mempty (someRosettaServer v concreteMs cutPeerDb) r) cuts
<> maybe mempty (bool mempty (someRosettaServer v payloads concreteMs cutPeerDb concreteCr) r) cuts
-- TODO: not sure if passing the correct PeerDb here
-- TODO: simplify number of resources passing to rosetta
where
payloads = _chainwebServerPayloadDbs dbs
blocks = _chainwebServerBlockHeaderDbs dbs
Expand All @@ -251,6 +252,7 @@ someChainwebServer v dbs mr (HeaderStream hs) (Rosetta r) =
peers = _chainwebServerPeerDbs dbs
mempools = _chainwebServerMempools dbs
concreteMs = map (second (_chainResMempool . snd)) pacts
concreteCr = map (second snd) pacts
cutPeerDb = fromJuste $ lookup CutNetwork peers

chainwebApplication
Expand Down
Loading