@@ -8,93 +8,81 @@ package block
88import (
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}
0 commit comments