prysm-pulse/beacon-chain/core/helpers/deposits_test.go
shayzluf 6c1740eefc Add Caching to Tree Hashing Algorithm (#1929)
* added todo to hash file in ssz

* params and copy of block cache

* start hash cache

* Hash cache implementation

* fixed some comments

* fixed promatheus duplicate counter name

* removed TODO

* change to use special expiration cache

* function name fixes junk object generator

* naming changes

* gazzle fix

* added pruning last read data test

* fixed gometallinter errors

* fix benchmarks and no int64 not serializable

* move struct from test

* add feature flag

* fix merge issues

* add featureflag to beacon and validator

* featureflag init for tests

* added feature flag to all ssz dependent tests

* remove setter func

* replace k8s tweaked expiration cache to https://github.com/karlseguin/ccache

* remove else

* change request by preston

* added init featureflags to genesis_test

* Update shared/ssz/hash_cache.go

add dot

Co-Authored-By: shayzluf <thezluf@gmail.com>

* Update shared/ssz/hash_cache.go

Co-Authored-By: shayzluf <thezluf@gmail.com>

* Update shared/ssz/hash_cache.go

remove extra space

Co-Authored-By: shayzluf <thezluf@gmail.com>

* Update shared/params/config.go

add dot

Co-Authored-By: shayzluf <thezluf@gmail.com>

* Update shared/featureconfig/config.go

remove dot

Co-Authored-By: shayzluf <thezluf@gmail.com>

* Update shared/featureconfig/config.go

remove dot

Co-Authored-By: shayzluf <thezluf@gmail.com>

* remove powchain from prometheus hash cache name

* fixes fron change requests

* fix change requests

* remove faulty merge test

* gazelle fix

* fix fmt.sprintf

* remove debug binary

* fix gazelle
2019-04-24 13:39:02 +08:00

124 lines
3.3 KiB
Go

package helpers
import (
"testing"
"time"
"github.com/gogo/protobuf/proto"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/params"
)
func init() {
featureconfig.InitFeatureConfig(&featureconfig.FeatureFlagConfig{
CacheTreeHash: false,
})
}
func TestEncodeDecodeDepositInput_Ok(t *testing.T) {
input := &pb.DepositInput{
Pubkey: []byte("key"),
WithdrawalCredentialsHash32: []byte("withdraw"),
ProofOfPossession: []byte("pop"),
}
depositTime := time.Now().Unix()
enc, err := EncodeDepositData(input, params.BeaconConfig().MaxDepositAmount, depositTime)
if err != nil {
t.Errorf("Could not encode deposit input: %v", err)
}
dec, err := DecodeDepositInput(enc)
if err != nil {
t.Errorf("Could not decode deposit input: %v", err)
}
if !proto.Equal(input, dec) {
t.Errorf("Original and decoded messages do not match, wanted %v, received %v", input, dec)
}
value, timestamp, err := DecodeDepositAmountAndTimeStamp(enc)
if err != nil {
t.Errorf("Could not decode amount and timestamp: %v", err)
}
if value != params.BeaconConfig().MaxDepositAmount || timestamp != depositTime {
t.Errorf(
"Expected value to match, received %d == %d, expected timestamp to match received %d == %d",
value,
params.BeaconConfig().MaxDepositAmount,
timestamp,
depositTime,
)
}
}
func TestDecodeDepositAmountAndTimeStamp(t *testing.T) {
tests := []struct {
depositData *pb.DepositInput
amount uint64
timestamp int64
}{
{
depositData: &pb.DepositInput{
Pubkey: []byte("testing"),
ProofOfPossession: []byte("pop"),
WithdrawalCredentialsHash32: []byte("withdraw"),
},
amount: 8749343850,
timestamp: 458739850,
},
{
depositData: &pb.DepositInput{
Pubkey: []byte("testing"),
ProofOfPossession: []byte("pop"),
WithdrawalCredentialsHash32: []byte("withdraw"),
},
amount: 657660,
timestamp: 67750,
},
{
depositData: &pb.DepositInput{
Pubkey: []byte("testing"),
ProofOfPossession: []byte("pop"),
WithdrawalCredentialsHash32: []byte("withdraw"),
},
amount: 5445540,
timestamp: 34340,
}, {
depositData: &pb.DepositInput{
Pubkey: []byte("testing"),
ProofOfPossession: []byte("pop"),
WithdrawalCredentialsHash32: []byte("withdraw"),
},
amount: 4545,
timestamp: 4343,
}, {
depositData: &pb.DepositInput{
Pubkey: []byte("testing"),
ProofOfPossession: []byte("pop"),
WithdrawalCredentialsHash32: []byte("withdraw"),
},
amount: 76706966,
timestamp: 34394393,
},
}
for _, tt := range tests {
data, err := EncodeDepositData(tt.depositData, tt.amount, tt.timestamp)
if err != nil {
t.Fatalf("could not encode data %v", err)
}
decAmount, decTimestamp, err := DecodeDepositAmountAndTimeStamp(data)
if err != nil {
t.Fatalf("could not decode data %v", err)
}
if tt.amount != decAmount {
t.Errorf("decoded amount not equal to given amount, %d : %d", decAmount, tt.amount)
}
if tt.timestamp != decTimestamp {
t.Errorf("decoded timestamp not equal to given timestamp, %d : %d", decTimestamp, tt.timestamp)
}
}
}