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

add "block" scheduler type + sweep integration test #339

Merged
merged 7 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
52 changes: 23 additions & 29 deletions common/bip68.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package common

import (
"encoding/hex"
"fmt"

"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/txscript"
)

const (
Expand All @@ -18,48 +20,40 @@ func closerToModulo512(x uint) uint {
return x - (x % 512)
}

func BIP68EncodeAsNumber(seconds uint) (uint32, error) {
seconds = closerToModulo512(seconds)
if seconds > SECONDS_MAX {
return 0, fmt.Errorf("seconds too large, max is %d", SECONDS_MAX)
}
if seconds%SECONDS_MOD != 0 {
return 0, fmt.Errorf("seconds must be a multiple of %d", SECONDS_MOD)
func BIP68Sequence(locktime uint) (uint32, error) {
isSeconds := locktime >= 512
if isSeconds {
locktime = closerToModulo512(locktime)
if locktime > SECONDS_MAX {
return 0, fmt.Errorf("seconds too large, max is %d", SECONDS_MAX)
}
if locktime%SECONDS_MOD != 0 {
return 0, fmt.Errorf("seconds must be a multiple of %d", SECONDS_MOD)
}
}

asNumber := SEQUENCE_LOCKTIME_TYPE_FLAG | (seconds >> SEQUENCE_LOCKTIME_GRANULARITY)
return uint32(asNumber), nil
return blockchain.LockTimeToSequence(isSeconds, uint32(locktime)), nil
}

// BIP68Encode returns the encoded sequence locktime for the given number of seconds.
func BIP68Encode(seconds uint) ([]byte, error) {
asNumber, err := BIP68EncodeAsNumber(seconds)
if err != nil {
return nil, err
}
hexString := fmt.Sprintf("%x", asNumber)
reversed, err := hex.DecodeString(hexString)
func BIP68DecodeSequence(sequence []byte) (uint, error) {
scriptNumber, err := txscript.MakeScriptNum(sequence, true, len(sequence))
if err != nil {
return nil, err
return 0, err
}
for i, j := 0, len(reversed)-1; i < j; i, j = i+1, j-1 {
reversed[i], reversed[j] = reversed[j], reversed[i]
}
return reversed, nil
}

func BIP68Decode(sequence []byte) (uint, error) {
var asNumber int64
for i := len(sequence) - 1; i >= 0; i-- {
asNumber = asNumber<<8 | int64(sequence[i])
if scriptNumber >= txscript.OP_1 && scriptNumber <= txscript.OP_16 {
scriptNumber = scriptNumber - (txscript.OP_1 - 1)
}

asNumber := int64(scriptNumber)

if asNumber&SEQUENCE_LOCKTIME_DISABLE_FLAG != 0 {
return 0, fmt.Errorf("sequence is disabled")
}
if asNumber&SEQUENCE_LOCKTIME_TYPE_FLAG != 0 {
seconds := asNumber & SEQUENCE_LOCKTIME_MASK << SEQUENCE_LOCKTIME_GRANULARITY
return uint(seconds), nil
}
return 0, fmt.Errorf("sequence is encoded as block number")

return uint(asNumber), nil
}
43 changes: 0 additions & 43 deletions common/bip68_test.go

This file was deleted.

20 changes: 13 additions & 7 deletions common/bitcointree/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,12 @@ func (d *CSVSigClosure) Decode(script []byte) (bool, error) {
return false, nil
}

sequence := script[1:csvIndex]
sequence := script[:csvIndex]
if len(sequence) > 1 {
sequence = sequence[1:]
}

seconds, err := common.BIP68Decode(sequence)
seconds, err := common.BIP68DecodeSequence(sequence)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -162,15 +165,18 @@ func decodeChecksigScript(script []byte) (bool, *secp256k1.PublicKey, error) {

// checkSequenceVerifyScript without checksig
func encodeCsvScript(seconds uint) ([]byte, error) {
sequence, err := common.BIP68Encode(seconds)
sequence, err := common.BIP68Sequence(seconds)
if err != nil {
return nil, err
}

return txscript.NewScriptBuilder().AddData(sequence).AddOps([]byte{
txscript.OP_CHECKSEQUENCEVERIFY,
txscript.OP_DROP,
}).Script()
return txscript.NewScriptBuilder().
AddInt64(int64(sequence)).
AddOps([]byte{
txscript.OP_CHECKSEQUENCEVERIFY,
txscript.OP_DROP,
}).
Script()
}

// checkSequenceVerifyScript + checksig
Expand Down
30 changes: 30 additions & 0 deletions common/bitcointree/script_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package bitcointree_test

import (
"testing"

"github.com/ark-network/ark/common/bitcointree"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/stretchr/testify/require"
)

func TestRoundTripCSV(t *testing.T) {
seckey, err := secp256k1.GeneratePrivateKey()
require.NoError(t, err)

csvSig := &bitcointree.CSVSigClosure{
Pubkey: seckey.PubKey(),
Seconds: 1024,
}

leaf, err := csvSig.Leaf()
require.NoError(t, err)

var cl bitcointree.CSVSigClosure

valid, err := cl.Decode(leaf.Script)
require.NoError(t, err)
require.True(t, valid)

require.Equal(t, csvSig.Seconds, cl.Seconds)
}
13 changes: 8 additions & 5 deletions common/descriptor/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,18 @@ func (e *Older) Parse(policy string) error {
}

func (e *Older) Script(bool) (string, error) {
sequence, err := common.BIP68Encode(e.Timeout)
sequence, err := common.BIP68Sequence(e.Timeout)
if err != nil {
return "", err
}

script, err := txscript.NewScriptBuilder().AddData(sequence).AddOps([]byte{
txscript.OP_CHECKSEQUENCEVERIFY,
txscript.OP_DROP,
}).Script()
script, err := txscript.NewScriptBuilder().
AddInt64(int64(sequence)).
AddOps([]byte{
txscript.OP_CHECKSEQUENCEVERIFY,
txscript.OP_DROP,
}).
Script()
if err != nil {
return "", err
}
Expand Down
67 changes: 0 additions & 67 deletions common/fixtures/bip68.json

This file was deleted.

20 changes: 13 additions & 7 deletions common/tree/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,12 @@ func (d *CSVSigClosure) Decode(script []byte) (bool, error) {
return false, nil
}

sequence := script[1:csvIndex]
sequence := script[:csvIndex]
if len(sequence) > 1 {
sequence = sequence[1:]
}

seconds, err := common.BIP68Decode(sequence)
seconds, err := common.BIP68DecodeSequence(sequence)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -369,15 +372,18 @@ func decodeChecksigScript(script []byte) (bool, *secp256k1.PublicKey, error) {

// checkSequenceVerifyScript without checksig
func encodeCsvScript(seconds uint) ([]byte, error) {
sequence, err := common.BIP68Encode(seconds)
sequence, err := common.BIP68Sequence(seconds)
if err != nil {
return nil, err
}

return txscript.NewScriptBuilder().AddData(sequence).AddOps([]byte{
txscript.OP_CHECKSEQUENCEVERIFY,
txscript.OP_DROP,
}).Script()
return txscript.NewScriptBuilder().
AddInt64(int64(sequence)).
AddOps([]byte{
txscript.OP_CHECKSEQUENCEVERIFY,
txscript.OP_DROP,
}).
Script()
}

// checkSequenceVerifyScript + checksig
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.clark.regtest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ services:
- ARK_ROUND_INTERVAL=10
- ARK_NETWORK=regtest
- ARK_LOG_LEVEL=5
- ARK_ROUND_LIFETIME=512
- ARK_ROUND_LIFETIME=20
- ARK_TX_BUILDER_TYPE=covenantless
- ARK_ESPLORA_URL=http://chopsticks:3000
- ARK_BITCOIND_RPC_USER=admin1
- ARK_BITCOIND_RPC_PASS=123
- ARK_BITCOIND_RPC_HOST=bitcoin:18443
- ARK_SCHEDULER_TYPE=block
- ARK_NO_TLS=true
- ARK_NO_MACAROONS=true
- ARK_DATADIR=/app/data
Expand Down
4 changes: 3 additions & 1 deletion docker-compose.regtest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ services:
- ARK_ROUND_INTERVAL=10
- ARK_NETWORK=liquidregtest
- ARK_LOG_LEVEL=5
- ARK_ROUND_LIFETIME=512
- ARK_ESPLORA_URL=http://chopsticks-liquid:3000
- ARK_ROUND_LIFETIME=20
- ARK_SCHEDULER_TYPE=block
- ARK_DB_TYPE=sqlite
- ARK_TX_BUILDER_TYPE=covenant
- ARK_PORT=6060
Expand Down
2 changes: 1 addition & 1 deletion pkg/client-sdk/explorer/explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type Utxo struct {
}

func (u *Utxo) Sequence() (uint32, error) {
return common.BIP68EncodeAsNumber(u.Delay)
return common.BIP68Sequence(u.Delay)
}

func newUtxo(explorerUtxo ExplorerUtxo, delay uint) Utxo {
Expand Down
4 changes: 2 additions & 2 deletions server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ help:
## intergrationtest: runs integration tests
integrationtest:
@echo "Running integration tests..."
@go test -v -count 1 -timeout 300s github.com/ark-network/ark/server/test/e2e/covenant
@go test -v -count 1 -timeout 300s github.com/ark-network/ark/server/test/e2e/covenantless
@go test -v -count 1 -timeout 400s github.com/ark-network/ark/server/test/e2e/covenant
@go test -v -count 1 -timeout 400s github.com/ark-network/ark/server/test/e2e/covenantless

## lint: lint codebase
lint:
Expand Down
Loading
Loading