Skip to content

Commit 95d8cff

Browse files
authored
update go-ecvrf (#1461)
1 parent 560e71b commit 95d8cff

File tree

3 files changed

+46
-58
lines changed

3 files changed

+46
-58
lines changed

block/block_test.go

Lines changed: 43 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,93 +8,81 @@ package block
88
import (
99
"math/big"
1010
"testing"
11-
"time"
1211

1312
"github.com/ethereum/go-ethereum/crypto"
14-
"github.com/ethereum/go-ethereum/rlp"
1513
"github.com/stretchr/testify/assert"
1614

1715
"github.com/vechain/thor/v2/thor"
1816
"github.com/vechain/thor/v2/tx"
17+
"github.com/vechain/thor/v2/vrf"
1918
)
2019

21-
func TestBlock(t *testing.T) {
20+
func TestBlockProof(t *testing.T) {
2221
tx1 := tx.NewBuilder(tx.TypeLegacy).Clause(tx.NewClause(&thor.Address{})).Clause(tx.NewClause(&thor.Address{})).Build()
2322
tx2 := tx.NewBuilder(tx.TypeDynamicFee).Clause(tx.NewClause(nil)).Build()
2423

2524
privKey := string("dce1443bd2ef0c2631adc1c67e5c93f13dc23a41c18b536effbbdcbcdb96fb65")
26-
27-
now := uint64(time.Now().UnixNano())
25+
alpha := thor.MustParseBytes32("0x68abc4fe6b911dd388eac9252513071dd4edea83e183c4b477dc65dd59359c2c")
2826

2927
var (
30-
gasUsed uint64 = 1000
31-
gasLimit uint64 = 14000
32-
totalScore uint64 = 101
33-
emptyRoot = thor.BytesToBytes32([]byte("0"))
34-
beneficiary = thor.BytesToAddress([]byte("abc"))
28+
emptyRoot = thor.BytesToBytes32([]byte("0"))
29+
beneficiary = thor.BytesToAddress([]byte("abc"))
3530
)
3631

3732
blk := new(Builder).
38-
GasUsed(gasUsed).
33+
GasUsed(1000).
3934
Transaction(tx1).
4035
Transaction(tx2).
41-
GasLimit(gasLimit).
42-
TotalScore(totalScore).
36+
GasLimit(14000).
37+
TotalScore(101).
4338
StateRoot(emptyRoot).
4439
ReceiptsRoot(emptyRoot).
45-
Timestamp(now).
40+
Timestamp(1761554386318816000).
4641
BaseFee(big.NewInt(thor.InitialBaseFee)).
4742
ParentID(emptyRoot).
43+
Alpha(alpha.Bytes()).
4844
Beneficiary(beneficiary).
4945
Build()
5046

51-
h := blk.Header()
52-
53-
txs := blk.Transactions()
54-
txsRootHash := txs.RootHash()
55-
56-
assert.Equal(t, Compose(h, txs), blk)
57-
assert.Equal(t, gasLimit, h.GasLimit())
58-
assert.Equal(t, gasUsed, h.GasUsed())
59-
assert.Equal(t, totalScore, h.TotalScore())
60-
assert.Equal(t, emptyRoot, h.StateRoot())
61-
assert.Equal(t, emptyRoot, h.ReceiptsRoot())
62-
assert.Equal(t, now, h.Timestamp())
63-
assert.Equal(t, big.NewInt(thor.InitialBaseFee), h.BaseFee())
64-
assert.Equal(t, emptyRoot, h.ParentID())
65-
assert.Equal(t, beneficiary, h.Beneficiary())
66-
assert.Equal(t, txsRootHash, h.TxsRoot())
67-
6847
key, _ := crypto.HexToECDSA(privKey)
69-
sig, _ := crypto.Sign(blk.Header().SigningHash().Bytes(), key)
70-
48+
ec, err := crypto.Sign(blk.Header().SigningHash().Bytes(), key)
49+
if err != nil {
50+
t.Fatal(err)
51+
}
52+
53+
_, proof, err := vrf.Prove(key, alpha.Bytes())
54+
if err != nil {
55+
t.Fatal(err)
56+
}
57+
58+
sig, err := NewComplexSignature(ec, proof)
59+
if err != nil {
60+
t.Fatal(err)
61+
}
7162
blk = blk.WithSignature(sig)
7263

73-
data, _ := rlp.EncodeToBytes(blk)
64+
_, err = blk.Header().Beta()
65+
assert.Nil(t, err)
7466

75-
b := Block{}
76-
rlp.DecodeBytes(data, &b)
67+
newProof := make([]byte, len(proof))
7768

78-
blk = new(Builder).
79-
GasUsed(gasUsed).
80-
GasLimit(gasLimit).
81-
TotalScore(totalScore).
82-
StateRoot(emptyRoot).
83-
ReceiptsRoot(emptyRoot).
84-
Timestamp(now).
85-
ParentID(emptyRoot).
86-
BaseFee(big.NewInt(thor.InitialBaseFee)).
87-
Beneficiary(beneficiary).
88-
TransactionFeatures(1).
89-
Build()
69+
copy(newProof, proof[0:33])
70+
sig, err = NewComplexSignature(ec, newProof)
71+
if err != nil {
72+
t.Fatal(err)
73+
}
74+
blk = blk.WithSignature(sig)
75+
76+
_, err = blk.Header().Beta()
77+
assert.ErrorContains(t, err, "invalid proof: value c is zero")
9078

91-
sig, _ = crypto.Sign(blk.Header().SigningHash().Bytes(), key)
79+
copy(newProof, proof[0:47])
80+
sig, err = NewComplexSignature(ec, newProof)
81+
if err != nil {
82+
t.Fatal(err)
83+
}
9284
blk = blk.WithSignature(sig)
9385

94-
assert.Equal(t, tx.Features(1), blk.Header().TxsFeatures())
95-
data, _ = rlp.EncodeToBytes(blk)
96-
var bx Block
97-
assert.Nil(t, rlp.DecodeBytes(data, &bx))
98-
assert.Equal(t, blk.Header().ID(), bx.Header().ID())
99-
assert.Equal(t, blk.Header().TxsFeatures(), bx.Header().TxsFeatures())
86+
_, err = blk.Header().Beta()
87+
assert.ErrorContains(t, err, "invalid proof: value s is zero")
10088
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ require (
2727
github.com/qianbin/drlp v0.0.0-20240102101024-e0e02518b5f9
2828
github.com/stretchr/testify v1.10.0
2929
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a
30-
github.com/vechain/go-ecvrf v0.0.0-20220525125849-96fa0442e765
30+
github.com/vechain/go-ecvrf v0.0.0-20251023142748-481dd12dec86
3131
golang.org/x/crypto v0.36.0
3232
golang.org/x/sync v0.17.0
3333
gopkg.in/cheggaaa/pb.v1 v1.0.28

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
154154
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
155155
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
156156
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
157-
github.com/vechain/go-ecvrf v0.0.0-20220525125849-96fa0442e765 h1:jvr+TSivjObZmOKVdqlgeLtRhaDG27gE39PMuE2IJ24=
158-
github.com/vechain/go-ecvrf v0.0.0-20220525125849-96fa0442e765/go.mod h1:cwnTMgAVzMb30xMKnGI1LdU1NjMiPllYb7i3ibj/fzE=
157+
github.com/vechain/go-ecvrf v0.0.0-20251023142748-481dd12dec86 h1:tcYrv6mkiw2DVF1pKG3wWdwLGeG0R1yCoeKHflXWoRY=
158+
github.com/vechain/go-ecvrf v0.0.0-20251023142748-481dd12dec86/go.mod h1:cwnTMgAVzMb30xMKnGI1LdU1NjMiPllYb7i3ibj/fzE=
159159
github.com/vechain/go-ethereum v1.8.15-0.20250708104014-34fea45fc2b7 h1:G+L5+ucSFFgEb8eCbfHtJ1kEZFC9zuLYITnjH2F8zJ0=
160160
github.com/vechain/go-ethereum v1.8.15-0.20250708104014-34fea45fc2b7/go.mod h1:yPUCNmntAh1PritrMfSi7noK+9vVPStZX3wgh3ieaY0=
161161
github.com/vechain/goleveldb v1.0.1-0.20220809091043-51eb019c8655 h1:CbHcWpCi7wOYfpoErRABh3Slyq9vO0Ay/EHN5GuJSXQ=

0 commit comments

Comments
 (0)