Skip to content

Commit

Permalink
rpc read withdrawal claim transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricfung committed Nov 20, 2023
1 parent e1fcd30 commit 58c9c0d
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 6 deletions.
7 changes: 7 additions & 0 deletions rpc/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ func (impl *RPC) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} else {
rdr.RenderData(tx)
}
case "getwithdrawalclaim":
tx, err := readWithdrawal(impl.Store, call.Params)
if err != nil {
rdr.RenderError(err)
} else {
rdr.RenderData(tx)
}
case "getutxo":
utxo, err := getUTXO(impl.Store, call.Params)
if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions rpc/withdrawal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package rpc

import (
"encoding/hex"
"errors"
"fmt"

"github.com/MixinNetwork/mixin/crypto"
"github.com/MixinNetwork/mixin/storage"
)

func readWithdrawal(store storage.Store, params []any) (map[string]any, error) {
if len(params) != 1 {
return nil, errors.New("invalid params count")
}
hash, err := crypto.HashFromString(fmt.Sprint(params[1]))
if err != nil {
return nil, err
}

ver, snap, err := store.ReadWithdrawalClaim(hash)
if err != nil || ver == nil {
return nil, err
}

data := transactionToMap(ver)
data["hex"] = hex.EncodeToString(ver.Marshal())
if len(snap) > 0 {
data["snapshot"] = snap
}
return data, nil
}
1 change: 1 addition & 0 deletions storage/badger_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
graphPrefixGhost = "GHOST" // each output key should only be used once
graphPrefixUTXO = "UTXO" // unspent outputs, including first consumed transaction hash
graphPrefixDeposit = "DEPOSIT"
graphPrefixWithdrawal = "WITHDRAWAL"
graphPrefixMint = "MINTUNIVERSAL"
graphPrefixTransaction = "TRANSACTION" // raw transaction, may not be finalized yet, if finalized with first finalized snapshot hash
graphPrefixFinalization = "FINALIZATION" // transaction finalization hack
Expand Down
14 changes: 8 additions & 6 deletions storage/badger_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func finalizeTransaction(txn *badger.Txn, ver *common.VersionedTransaction, snap

genesis := len(ver.Inputs[0].Genesis) > 0
for _, utxo := range ver.UnspentOutputs() {
err := writeUTXO(txn, utxo, ver.Extra, snap.Timestamp, genesis)
err := writeUTXO(txn, utxo, ver, snap.Timestamp, genesis)
if err != nil {
return err
}
Expand All @@ -175,7 +175,7 @@ func finalizeTransaction(txn *badger.Txn, ver *common.VersionedTransaction, snap
return writeTotalInAsset(txn, ver)
}

func writeUTXO(txn *badger.Txn, utxo *common.UTXOWithLock, extra []byte, timestamp uint64, genesis bool) error {
func writeUTXO(txn *badger.Txn, utxo *common.UTXOWithLock, ver *common.VersionedTransaction, timestamp uint64, genesis bool) error {
for _, k := range utxo.Keys {
err := lockGhostKey(txn, k, utxo.Hash, true)
if err != nil {
Expand All @@ -190,9 +190,9 @@ func writeUTXO(txn *badger.Txn, utxo *common.UTXOWithLock, extra []byte, timesta
}

var signer, payee crypto.Key
if len(extra) >= len(signer) {
copy(signer[:], extra)
copy(payee[:], extra[len(signer):])
if len(ver.Extra) >= len(signer) {
copy(signer[:], ver.Extra)
copy(payee[:], ver.Extra[len(signer):])
}
switch utxo.Type {
case common.OutputTypeNodePledge:
Expand All @@ -204,7 +204,9 @@ func writeUTXO(txn *badger.Txn, utxo *common.UTXOWithLock, extra []byte, timesta
case common.OutputTypeNodeRemove:
return writeNodeRemove(txn, signer, payee, utxo.Hash, timestamp)
case common.OutputTypeCustodianUpdateNodes:
return writeCustodianNodes(txn, timestamp, utxo, extra, genesis)
return writeCustodianNodes(txn, timestamp, utxo, ver.Extra, genesis)
case common.OutputTypeWithdrawalClaim:
return writeWithdrawalClaim(txn, ver.References[0], ver.PayloadHash())
}

return nil
Expand Down
49 changes: 49 additions & 0 deletions storage/badger_withdrawal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package storage

import (
"encoding/hex"

"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/crypto"
"github.com/dgraph-io/badger/v4"
)

func (s *BadgerStore) ReadWithdrawalClaim(hash crypto.Hash) (*common.VersionedTransaction, string, error) {
txn := s.snapshotsDB.NewTransaction(false)
defer txn.Discard()

key := graphWithdrawalClaimKey(hash)
item, err := txn.Get(key)
if err == badger.ErrKeyNotFound {
return nil, "", nil
} else if err != nil {
return nil, "", err
}
val, err := item.ValueCopy(nil)
if err != nil {
return nil, "", err
}

var claim crypto.Hash
if len(val) != len(claim) {
panic(hex.EncodeToString(val))
}
copy(claim[:], val)
return readTransactionAndFinalization(txn, claim)
}

func writeWithdrawalClaim(txn *badger.Txn, hash, claim crypto.Hash) error {
tx, snap, err := readTransactionAndFinalization(txn, hash)
if err != nil {
return err
}
if tx == nil || len(snap) != 64 {
panic(claim.String())
}
key := graphWithdrawalClaimKey(hash)
return txn.Set(key, claim[:])
}

func graphWithdrawalClaimKey(tx crypto.Hash) []byte {
return append([]byte(graphPrefixWithdrawal), tx[:]...)
}
1 change: 1 addition & 0 deletions storage/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Store interface {
LockUTXOs(inputs []*common.Input, tx crypto.Hash, fork bool) error
ReadDepositLock(deposit *common.DepositData) (crypto.Hash, error)
LockDepositInput(deposit *common.DepositData, tx crypto.Hash, fork bool) error
ReadWithdrawalClaim(hash crypto.Hash) (*common.VersionedTransaction, string, error)
ReadGhostKeyLock(key crypto.Key) (*crypto.Hash, error)
LockGhostKeys(keys []*crypto.Key, tx crypto.Hash, fork bool) error
ReadSnapshot(hash crypto.Hash) (*common.SnapshotWithTopologicalOrder, error)
Expand Down

0 comments on commit 58c9c0d

Please sign in to comment.