Skip to content

Commit

Permalink
Merge branch 'master' into l2-engine-api
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton-Rampage authored Jul 30, 2024
2 parents 16179ac + 24bc100 commit d75ed0e
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 69 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21
require (
filippo.io/edwards25519 v1.1.0
github.com/beevik/ntp v1.4.3
github.com/btcsuite/btcd/btcec/v2 v2.3.3
github.com/btcsuite/btcd/btcec/v2 v2.3.4
github.com/cenkalti/backoff/v4 v4.3.0
github.com/cespare/xxhash/v2 v2.3.0
github.com/consensys/gnark v0.10.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM
github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ=
github.com/bsm/ginkgo/v2 v2.7.0/go.mod h1:AiKlXPm7ItEHNc/2+OkrNG4E0ITzojb9/xWzvQ9XZ9w=
github.com/bsm/gomega v1.26.0/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/btcsuite/btcd/btcec/v2 v2.3.3 h1:6+iXlDKE8RMtKsvK0gshlXIuPbyWM/h84Ensb7o3sC0=
github.com/btcsuite/btcd/btcec/v2 v2.3.3/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04=
github.com/btcsuite/btcd/btcec/v2 v2.3.4 h1:3EJjcN70HCu/mwqlUsGK8GcNVyLVxFDlWurTXGPFfiQ=
github.com/btcsuite/btcd/btcec/v2 v2.3.4/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04=
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 h1:59Kx4K6lzOW5w6nFlA0v5+lk/6sjybR934QNHSJZPTQ=
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
Expand Down
5 changes: 1 addition & 4 deletions pkg/crypto/ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,7 @@ func ECDSASign(digest []byte, sk *btcec.PrivateKey) ([]byte, error) {
if len(digest) != 32 {
return nil, errors.Errorf("hash is required to be exactly 32 bytes (%d)", len(digest))
}
sig, err := btcECDSA.SignCompact(sk, digest, false)
if err != nil {
return nil, err
}
sig := btcECDSA.SignCompact(sk, digest, false)
// Convert to Ethereum signature format with 'recovery id' v at the end.
v := sig[0] - 27
copy(sig, sig[1:])
Expand Down
13 changes: 12 additions & 1 deletion pkg/ride/compiler/stdlib/funcs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2623,9 +2623,20 @@
"return_type": "Int",
"id": "901"
}
],
"replaceByIndex": [
{
"arguments": [
"List[Any]",
"Int",
"Any"
],
"return_type": "List[Any]",
"id": "1106"
}
]
},
"remove": []
}
]
}
}
4 changes: 2 additions & 2 deletions pkg/ride/functions.gen.go

Large diffs are not rendered by default.

137 changes: 78 additions & 59 deletions pkg/ride/generate/internal/functions_generation.go
Original file line number Diff line number Diff line change
Expand Up @@ -894,80 +894,99 @@ func catalogueV8() map[string]int {
return m
}

func evaluationCatalogueV6EvaluatorV1() map[string]int {
m := catalogueV6()
func evaluationCatalogueBuilder(
libVer ast.LibraryVersion,
catalogue func() map[string]int,
evaluatorVer int,
) map[string]int {
if libVer < ast.LibV6 {
panic(fmt.Sprintf(
"evaluationCatalogueBuilder(V%d): can be used only for library version 6 and later, got %d",
evaluatorVer, libVer,
))
}
if maxV := ast.CurrentMaxLibraryVersion(); libVer > maxV {
panic(fmt.Sprintf(
"evaluationCatalogueBuilder(V%d): library version %d is greater than the current max version %d",
evaluatorVer, libVer, maxV,
))
}

var (
constructorsComplexity int
constructorsEvaluationCatalogue func(ast.LibraryVersion, map[string]int)
)
switch evaluatorVer {
case 1:
constructorsComplexity = 0
constructorsEvaluationCatalogue = constructorsEvaluationCatalogueEvaluatorV1
case 2:
constructorsComplexity = 1
constructorsEvaluationCatalogue = constructorsEvaluationCatalogueEvaluatorV2
default:
panic(fmt.Sprintf("evaluationCatalogueBuilder: unknown evaluator version %d", evaluatorVer))
}

m := catalogue()
m["throw"] = 2
m["Ceiling"] = 0
m["Floor"] = 0
m["HalfEven"] = 0
m["Down"] = 0
m["HalfUp"] = 0
m["NoAlg"] = 0
m["Md5"] = 0
m["Sha1"] = 0
m["Sha224"] = 0
m["Sha256"] = 0
m["Sha384"] = 0
m["Sha512"] = 0
m["Sha3224"] = 0
m["Sha3256"] = 0
m["Sha3384"] = 0
m["Sha3512"] = 0
m["Unit"] = 0
m["Address"] = 0
m["Alias"] = 0
constructorsEvaluationCatalogueEvaluatorV1(ast.LibV6, m)
constructorsList := []string{
"Ceiling",
"Floor",
"HalfEven",
"Down",
"HalfUp",
"NoAlg",
"Md5",
"Sha1",
"Sha224",
"Sha256",
"Sha384",
"Sha512",
"Sha3224",
"Sha3256",
"Sha3384",
"Sha3512",
"Unit",
"Address",
"Alias",
}
for _, c := range constructorsList {
m[c] = constructorsComplexity
}
constructorsEvaluationCatalogue(libVer, m)
return m
}

func evaluationCatalogueEvaluatorV1Builder(libVer ast.LibraryVersion, catalogue func() map[string]int) map[string]int {
return evaluationCatalogueBuilder(libVer, catalogue, 1)
}

func evaluationCatalogueEvaluatorV2Builder(libVer ast.LibraryVersion, catalogue func() map[string]int) map[string]int {
return evaluationCatalogueBuilder(libVer, catalogue, 2)
}

func evaluationCatalogueV6EvaluatorV1() map[string]int {
return evaluationCatalogueEvaluatorV1Builder(ast.LibV6, catalogueV6)
}

func evaluationCatalogueV6EvaluatorV2() map[string]int {
m := catalogueV6()
m["throw"] = 2
m["Ceiling"] = 1
m["Floor"] = 1
m["HalfEven"] = 1
m["Down"] = 1
m["HalfUp"] = 1
m["NoAlg"] = 1
m["Md5"] = 1
m["Sha1"] = 1
m["Sha224"] = 1
m["Sha256"] = 1
m["Sha384"] = 1
m["Sha512"] = 1
m["Sha3224"] = 1
m["Sha3256"] = 1
m["Sha3384"] = 1
m["Sha3512"] = 1
m["Unit"] = 1
m["Address"] = 1
m["Alias"] = 1
constructorsEvaluationCatalogueEvaluatorV2(ast.LibV6, m)
return m
return evaluationCatalogueEvaluatorV2Builder(ast.LibV6, catalogueV6)
}

func evaluationCatalogueV7EvaluatorV1() map[string]int {
m := evaluationCatalogueV6EvaluatorV1()
constructorsEvaluationCatalogueEvaluatorV1(ast.LibV7, m)
return m
return evaluationCatalogueEvaluatorV1Builder(ast.LibV7, catalogueV7)
}

func evaluationCatalogueV8EvaluatorV1() map[string]int {
m := evaluationCatalogueV7EvaluatorV1()
constructorsEvaluationCatalogueEvaluatorV1(ast.LibV8, m)
return m
func evaluationCatalogueV7EvaluatorV2() map[string]int {
return evaluationCatalogueEvaluatorV2Builder(ast.LibV7, catalogueV7)
}

func evaluationCatalogueV7EvaluatorV2() map[string]int {
m := evaluationCatalogueV6EvaluatorV2()
constructorsEvaluationCatalogueEvaluatorV2(ast.LibV7, m)
return m
func evaluationCatalogueV8EvaluatorV1() map[string]int {
return evaluationCatalogueEvaluatorV1Builder(ast.LibV8, catalogueV8)
}

func evaluationCatalogueV8EvaluatorV2() map[string]int {
m := evaluationCatalogueV7EvaluatorV2()
constructorsEvaluationCatalogueEvaluatorV2(ast.LibV8, m)
return m
return evaluationCatalogueEvaluatorV2Builder(ast.LibV8, catalogueV8)
}

func constructorsFromConstants(m map[string]string, c map[string]constantDescription) {
Expand Down
10 changes: 10 additions & 0 deletions pkg/ride/test_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,16 @@ func (bb *blockBuilder) withHeight(h uint64) *blockBuilder {
return bb
}

func (bb *blockBuilder) withBaseTarget(baseTarget uint64) *blockBuilder {
bb.bt = baseTarget
return bb
}

func (bb *blockBuilder) withVRF(vrf []byte) *blockBuilder {
bb.vrf = vrf
return bb
}

func parseBase64Script(t *testing.T, src string) (proto.Script, *ast.Tree) {
script, err := base64.StdEncoding.DecodeString(src)
require.NoError(t, err)
Expand Down
74 changes: 74 additions & 0 deletions pkg/ride/tree_evaluation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6040,3 +6040,77 @@ func TestEvaluatorComplexityFailedPaymentsCheck(t *testing.T) {
})
})
}

func TestNewRideV8Functions(t *testing.T) {
sender := newTestAccount(t, "SENDER") // 3N8CkZAyS4XcDoJTJoKNuNk2xmNKmQj7myW
dApp1 := newTestAccount(t, "DAPP1") // 3MzDtgL5yw73C2xVLnLJCrT5gCL4357a4sz

vrf, err := crypto.NewDigestFromBase58("5AFgQTfL1GhVUZr64N6tkmF8usX9QZsPcJbZmsX32VgK")
require.NoError(t, err)
const bt = 142245893
testBlockInfo := protobufBlockBuilder().withGenerator(sender).withBaseTarget(bt).withVRF(vrf.Bytes()).toBlockInfo()
createEnv := func(t *testing.T, tree *ast.Tree) *testEnv {
return newTestEnv(t).withLibVersion(ast.LibV8).withComplexityLimit(ast.LibV8, 52000).
withBlockV5Activated().withProtobufTx().withRideV6Activated().
withConsensusImprovementsActivatedFunc().withBlockRewardDistribution().
withDataEntriesSizeV2().withMessageLengthV3().withValidateInternalPayments().
withThis(dApp1).withDApp(dApp1).withSender(sender).withBlock(testBlockInfo).
withInvocation("call", withTransactionID(crypto.Digest{})).withTree(dApp1, tree)
}
t.Run("calculateDelay", func(t *testing.T) {
const src = `
{-# STDLIB_VERSION 8 #-}
{-# CONTENT_TYPE DAPP #-}
{-# SCRIPT_TYPE ACCOUNT #-}

@Callable(i)
func call() = {
let minerAddress = i.caller
let minerBalance = 100500
strict delay = calculateDelay(minerAddress, minerBalance)
if delay != %d then
throw("delays are not equal")
else
[]
}
`
const expectedDelay = 703159
tree, errs := ridec.CompileToTree(fmt.Sprintf(src, expectedDelay))
require.Empty(t, errs)
te := createEnv(t, tree)
env := te.toEnv()
res, callErr := CallFunction(env, tree, proto.NewFunctionCall("call", proto.Arguments{}))
assert.NoError(t, callErr)
assert.True(t, res.Result())
assert.Empty(t, res.ScriptActions())
const expectedComplexity = 3
assert.Equal(t, expectedComplexity, res.Complexity())
})
t.Run("replaceByIndex", func(t *testing.T) {
const src = `
{-# STDLIB_VERSION 8 #-}
{-# CONTENT_TYPE DAPP #-}
{-# SCRIPT_TYPE ACCOUNT #-}

@Callable(i)
func call() = {
let list = ["Waves", 42, true]
strict listRes = replaceByIndex(list, 1, "Ride")
if listRes != ["Waves", "Ride", true] then
throw("lists are not equal")
else
[]
}
`
tree, errs := ridec.CompileToTree(src)
require.Empty(t, errs)
te := createEnv(t, tree)
env := te.toEnv()
res, callErr := CallFunction(env, tree, proto.NewFunctionCall("call", proto.Arguments{}))
assert.NoError(t, callErr)
assert.True(t, res.Result())
assert.Empty(t, res.ScriptActions())
const expectedComplexity = 12
assert.Equal(t, expectedComplexity, res.Complexity())
})
}

0 comments on commit d75ed0e

Please sign in to comment.